org.el 906 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936
  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: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-cache-reset "org-element" (&optional all))
  128. (declare-function org-element-contents "org-element" (element))
  129. (declare-function org-element-context "org-element" (&optional element))
  130. (declare-function org-element-interpret-data "org-element"
  131. (data &optional parent))
  132. (declare-function org-element-map "org-element"
  133. (data types fun &optional info first-match no-recursion))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element"
  136. (&optional granularity visible-only))
  137. (declare-function org-element-property "org-element" (property element))
  138. (declare-function org-element-put-property "org-element"
  139. (element property value))
  140. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  141. (declare-function org-element--parse-objects "org-element"
  142. (beg end acc restriction))
  143. (declare-function org-element-parse-buffer "org-element"
  144. (&optional granularity visible-only))
  145. (declare-function org-element-restriction "org-element" (element))
  146. (declare-function org-element-type "org-element" (element))
  147. ;; load languages based on value of `org-babel-load-languages'
  148. (defvar org-babel-load-languages)
  149. ;;;###autoload
  150. (defun org-babel-do-load-languages (sym value)
  151. "Load the languages defined in `org-babel-load-languages'."
  152. (set-default sym value)
  153. (mapc (lambda (pair)
  154. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  155. (if active
  156. (progn
  157. (require (intern (concat "ob-" lang))))
  158. (progn
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-execute:" lang)))
  161. (funcall 'fmakunbound
  162. (intern (concat "org-babel-expand-body:" lang)))))))
  163. org-babel-load-languages))
  164. ;;;###autoload
  165. (defun org-babel-load-file (file &optional compile)
  166. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  167. This function exports the source code using `org-babel-tangle'
  168. and then loads the resulting file using `load-file'. With prefix
  169. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  170. file to byte-code before it is loaded."
  171. (interactive "fFile to load: \nP")
  172. (require 'ob-core)
  173. (let* ((age (lambda (file)
  174. (float-time
  175. (time-subtract (current-time)
  176. (nth 5 (or (file-attributes (file-truename file))
  177. (file-attributes file)))))))
  178. (base-name (file-name-sans-extension file))
  179. (exported-file (concat base-name ".el")))
  180. ;; tangle if the org-mode file is newer than the elisp file
  181. (unless (and (file-exists-p exported-file)
  182. (> (funcall age file) (funcall age exported-file)))
  183. (setq exported-file
  184. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  185. (message "%s %s"
  186. (if compile
  187. (progn (byte-compile-file exported-file 'load)
  188. "Compiled and loaded")
  189. (progn (load-file exported-file) "Loaded"))
  190. exported-file)))
  191. (defcustom org-babel-load-languages '((emacs-lisp . t))
  192. "Languages which can be evaluated in Org-mode buffers.
  193. This list can be used to load support for any of the languages
  194. below, note that each language will depend on a different set of
  195. system executables and/or Emacs modes. When a language is
  196. \"loaded\", then code blocks in that language can be evaluated
  197. with `org-babel-execute-src-block' bound by default to C-c
  198. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  199. be set to remove code block evaluation from the C-c C-c
  200. keybinding. By default only Emacs Lisp (which has no
  201. requirements) is loaded."
  202. :group 'org-babel
  203. :set 'org-babel-do-load-languages
  204. :version "24.1"
  205. :type '(alist :tag "Babel Languages"
  206. :key-type
  207. (choice
  208. (const :tag "Awk" awk)
  209. (const :tag "C" C)
  210. (const :tag "R" R)
  211. (const :tag "Asymptote" asymptote)
  212. (const :tag "Calc" calc)
  213. (const :tag "Clojure" clojure)
  214. (const :tag "CSS" css)
  215. (const :tag "Ditaa" ditaa)
  216. (const :tag "Dot" dot)
  217. (const :tag "Emacs Lisp" emacs-lisp)
  218. (const :tag "Fortran" fortran)
  219. (const :tag "Gnuplot" gnuplot)
  220. (const :tag "Haskell" haskell)
  221. (const :tag "IO" io)
  222. (const :tag "Java" java)
  223. (const :tag "Javascript" js)
  224. (const :tag "LaTeX" latex)
  225. (const :tag "Ledger" ledger)
  226. (const :tag "Lilypond" lilypond)
  227. (const :tag "Lisp" lisp)
  228. (const :tag "Makefile" makefile)
  229. (const :tag "Maxima" maxima)
  230. (const :tag "Matlab" matlab)
  231. (const :tag "Mscgen" mscgen)
  232. (const :tag "Ocaml" ocaml)
  233. (const :tag "Octave" octave)
  234. (const :tag "Org" org)
  235. (const :tag "Perl" perl)
  236. (const :tag "Pico Lisp" picolisp)
  237. (const :tag "PlantUML" plantuml)
  238. (const :tag "Python" python)
  239. (const :tag "Ruby" ruby)
  240. (const :tag "Sass" sass)
  241. (const :tag "Scala" scala)
  242. (const :tag "Scheme" scheme)
  243. (const :tag "Screen" screen)
  244. (const :tag "Shell Script" sh)
  245. (const :tag "Shen" shen)
  246. (const :tag "Sql" sql)
  247. (const :tag "Sqlite" sqlite)
  248. (const :tag "ebnf2ps" ebnf2ps))
  249. :value-type (boolean :tag "Activate" :value t)))
  250. ;;;; Customization variables
  251. (defcustom org-clone-delete-id nil
  252. "Remove ID property of clones of a subtree.
  253. When non-nil, clones of a subtree don't inherit the ID property.
  254. Otherwise they inherit the ID property with a new unique
  255. identifier."
  256. :type 'boolean
  257. :version "24.1"
  258. :group 'org-id)
  259. ;;; Version
  260. (org-check-version)
  261. ;;;###autoload
  262. (defun org-version (&optional here full message)
  263. "Show the org-mode version in the echo area.
  264. With prefix argument HERE, insert it at point.
  265. When FULL is non-nil, use a verbose version string.
  266. When MESSAGE is non-nil, display a message with the version."
  267. (interactive "P")
  268. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  269. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  270. (load-suffixes (list ".el"))
  271. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  272. (org-trash (or
  273. (and (fboundp 'org-release) (fboundp 'org-git-version))
  274. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  275. (load-suffixes save-load-suffixes)
  276. (org-version (org-release))
  277. (git-version (org-git-version))
  278. (version (format "Org-mode version %s (%s @ %s)"
  279. org-version
  280. git-version
  281. (if org-install-dir
  282. (if (string= org-dir org-install-dir)
  283. org-install-dir
  284. (concat "mixed installation! " org-install-dir " and " org-dir))
  285. "org-loaddefs.el can not be found!")))
  286. (_version (if full version org-version)))
  287. (if (org-called-interactively-p 'interactive)
  288. (if here
  289. (insert version)
  290. (message version))
  291. (if message (message _version))
  292. _version)))
  293. (defconst org-version (org-version))
  294. ;;; Compatibility constants
  295. ;;; The custom variables
  296. (defgroup org nil
  297. "Outline-based notes management and organizer."
  298. :tag "Org"
  299. :group 'outlines
  300. :group 'calendar)
  301. (defcustom org-mode-hook nil
  302. "Mode hook for Org-mode, run after the mode was turned on."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-load-hook nil
  306. "Hook that is run after org.el has been loaded."
  307. :group 'org
  308. :type 'hook)
  309. (defcustom org-log-buffer-setup-hook nil
  310. "Hook that is run after an Org log buffer is created."
  311. :group 'org
  312. :version "24.1"
  313. :type 'hook)
  314. (defvar org-modules) ; defined below
  315. (defvar org-modules-loaded nil
  316. "Have the modules been loaded already?")
  317. (defun org-load-modules-maybe (&optional force)
  318. "Load all extensions listed in `org-modules'."
  319. (when (or force (not org-modules-loaded))
  320. (mapc (lambda (ext)
  321. (condition-case nil (require ext)
  322. (error (message "Problems while trying to load feature `%s'" ext))))
  323. org-modules)
  324. (setq org-modules-loaded t)))
  325. (defun org-set-modules (var value)
  326. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  327. (set var value)
  328. (when (featurep 'org)
  329. (org-load-modules-maybe 'force)
  330. (org-element-cache-reset 'all)))
  331. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  332. "Modules that should always be loaded together with org.el.
  333. If a description starts with <C>, the file is not part of Emacs
  334. and loading it will require that you have downloaded and properly
  335. installed the Org mode distribution.
  336. You can also use this system to load external packages (i.e. neither Org
  337. core modules, nor modules from the CONTRIB directory). Just add symbols
  338. to the end of the list. If the package is called org-xyz.el, then you need
  339. to add the symbol `xyz', and the package must have a call to:
  340. \(provide 'org-xyz)
  341. For export specific modules, see also `org-export-backends'."
  342. :group 'org
  343. :set 'org-set-modules
  344. :version "24.4"
  345. :package-version '(Org . "8.0")
  346. :type
  347. '(set :greedy t
  348. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  349. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  350. (const :tag " crypt: Encryption of subtrees" org-crypt)
  351. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  352. (const :tag " docview: Links to doc-view buffers" org-docview)
  353. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  354. (const :tag " habit: Track your consistency with habits" org-habit)
  355. (const :tag " id: Global IDs for identifying entries" org-id)
  356. (const :tag " info: Links to Info nodes" org-info)
  357. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  358. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  359. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  360. (const :tag " mouse: Additional mouse support" org-mouse)
  361. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  362. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  363. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  364. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  365. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  366. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  367. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  368. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  369. (const :tag "C collector: Collect properties into tables" org-collector)
  370. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  371. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  372. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  373. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  374. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  375. (const :tag "C eval: Include command output as text" org-eval)
  376. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  377. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  378. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  379. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  380. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  381. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  382. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  383. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  384. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  385. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  386. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  387. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  388. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  389. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  390. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  391. (const :tag "C registry: A registry for Org-mode links" org-registry)
  392. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  393. (const :tag "C secretary: Team management with org-mode" org-secretary)
  394. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  395. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  396. (const :tag "C track: Keep up with Org-mode development" org-track)
  397. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  398. (const :tag "C vm: Links to VM folders/messages" org-vm)
  399. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  400. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  401. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  402. (defvar org-export--registered-backends) ; From ox.el.
  403. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  404. (declare-function org-export-backend-name "ox" (backend))
  405. (defcustom org-export-backends '(ascii html icalendar latex)
  406. "List of export back-ends that should be always available.
  407. If a description starts with <C>, the file is not part of Emacs
  408. and loading it will require that you have downloaded and properly
  409. installed the Org mode distribution.
  410. Unlike to `org-modules', libraries in this list will not be
  411. loaded along with Org, but only once the export framework is
  412. needed.
  413. This variable needs to be set before org.el is loaded. If you
  414. need to make a change while Emacs is running, use the customize
  415. interface or run the following code, where VAL stands for the new
  416. value of the variable, after updating it:
  417. \(progn
  418. \(setq org-export--registered-backends
  419. \(org-remove-if-not
  420. \(lambda (backend)
  421. \(let ((name (org-export-backend-name backend)))
  422. \(or (memq name val)
  423. \(catch 'parentp
  424. \(dolist (b val)
  425. \(and (org-export-derived-backend-p b name)
  426. \(throw 'parentp t)))))))
  427. org-export--registered-backends))
  428. \(let ((new-list (mapcar 'org-export-backend-name
  429. org-export--registered-backends)))
  430. \(dolist (backend val)
  431. \(cond
  432. \((not (load (format \"ox-%s\" backend) t t))
  433. \(message \"Problems while trying to load export back-end `%s'\"
  434. backend))
  435. \((not (memq backend new-list)) (push backend new-list))))
  436. \(set-default 'org-export-backends new-list)))
  437. Adding a back-end to this list will also pull the back-end it
  438. depends on, if any."
  439. :group 'org
  440. :group 'org-export
  441. :version "24.4"
  442. :package-version '(Org . "8.0")
  443. :initialize 'custom-initialize-set
  444. :set (lambda (var val)
  445. (if (not (featurep 'ox)) (set-default var val)
  446. ;; Any back-end not required anymore (not present in VAL and not
  447. ;; a parent of any back-end in the new value) is removed from the
  448. ;; list of registered back-ends.
  449. (setq org-export--registered-backends
  450. (org-remove-if-not
  451. (lambda (backend)
  452. (let ((name (org-export-backend-name backend)))
  453. (or (memq name val)
  454. (catch 'parentp
  455. (dolist (b val)
  456. (and (org-export-derived-backend-p b name)
  457. (throw 'parentp t)))))))
  458. org-export--registered-backends))
  459. ;; Now build NEW-LIST of both new back-ends and required
  460. ;; parents.
  461. (let ((new-list (mapcar 'org-export-backend-name
  462. org-export--registered-backends)))
  463. (dolist (backend val)
  464. (cond
  465. ((not (load (format "ox-%s" backend) t t))
  466. (message "Problems while trying to load export back-end `%s'"
  467. backend))
  468. ((not (memq backend new-list)) (push backend new-list))))
  469. ;; Set VAR to that list with fixed dependencies.
  470. (set-default var new-list))))
  471. :type '(set :greedy t
  472. (const :tag " ascii Export buffer to ASCII format" ascii)
  473. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  474. (const :tag " html Export buffer to HTML format" html)
  475. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  476. (const :tag " latex Export buffer to LaTeX format" latex)
  477. (const :tag " man Export buffer to MAN format" man)
  478. (const :tag " md Export buffer to Markdown format" md)
  479. (const :tag " odt Export buffer to ODT format" odt)
  480. (const :tag " org Export buffer to Org format" org)
  481. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  482. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  483. (const :tag "C deck Export buffer to deck.js presentations" deck)
  484. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  485. (const :tag "C groff Export buffer to Groff format" groff)
  486. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  487. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  488. (const :tag "C s5 Export buffer to s5 presentations" s5)
  489. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  490. (eval-after-load 'ox
  491. '(mapc
  492. (lambda (backend)
  493. (condition-case nil (require (intern (format "ox-%s" backend)))
  494. (error (message "Problems while trying to load export back-end `%s'"
  495. backend))))
  496. org-export-backends))
  497. (defcustom org-support-shift-select nil
  498. "Non-nil means make shift-cursor commands select text when possible.
  499. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  500. start selecting a region, or enlarge regions started in this way.
  501. In Org-mode, in special contexts, these same keys are used for
  502. other purposes, important enough to compete with shift selection.
  503. Org tries to balance these needs by supporting `shift-select-mode'
  504. outside these special contexts, under control of this variable.
  505. The default of this variable is nil, to avoid confusing behavior. Shifted
  506. cursor keys will then execute Org commands in the following contexts:
  507. - on a headline, changing TODO state (left/right) and priority (up/down)
  508. - on a time stamp, changing the time
  509. - in a plain list item, changing the bullet type
  510. - in a property definition line, switching between allowed values
  511. - in the BEGIN line of a clock table (changing the time block).
  512. Outside these contexts, the commands will throw an error.
  513. When this variable is t and the cursor is not in a special
  514. context, Org-mode will support shift-selection for making and
  515. enlarging regions. To make this more effective, the bullet
  516. cycling will no longer happen anywhere in an item line, but only
  517. if the cursor is exactly on the bullet.
  518. If you set this variable to the symbol `always', then the keys
  519. will not be special in headlines, property lines, and item lines,
  520. to make shift selection work there as well. If this is what you
  521. want, you can use the following alternative commands: `C-c C-t'
  522. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  523. can be used to switch TODO sets, `C-c -' to cycle item bullet
  524. types, and properties can be edited by hand or in column view.
  525. However, when the cursor is on a timestamp, shift-cursor commands
  526. will still edit the time stamp - this is just too good to give up.
  527. XEmacs user should have this variable set to nil, because
  528. `shift-select-mode' is in Emacs 23 or later only."
  529. :group 'org
  530. :type '(choice
  531. (const :tag "Never" nil)
  532. (const :tag "When outside special context" t)
  533. (const :tag "Everywhere except timestamps" always)))
  534. (defcustom org-loop-over-headlines-in-active-region nil
  535. "Shall some commands act upon headlines in the active region?
  536. When set to `t', some commands will be performed in all headlines
  537. within the active region.
  538. When set to `start-level', some commands will be performed in all
  539. headlines within the active region, provided that these headlines
  540. are of the same level than the first one.
  541. When set to a string, those commands will be performed on the
  542. matching headlines within the active region. Such string must be
  543. a tags/property/todo match as it is used in the agenda tags view.
  544. The list of commands is: `org-schedule', `org-deadline',
  545. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  546. `org-archive-to-archive-sibling'. The archiving commands skip
  547. already archived entries."
  548. :type '(choice (const :tag "Don't loop" nil)
  549. (const :tag "All headlines in active region" t)
  550. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  551. (string :tag "Tags/Property/Todo matcher"))
  552. :version "24.1"
  553. :group 'org-todo
  554. :group 'org-archive)
  555. (defgroup org-startup nil
  556. "Options concerning startup of Org-mode."
  557. :tag "Org Startup"
  558. :group 'org)
  559. (defcustom org-startup-folded t
  560. "Non-nil means entering Org-mode will switch to OVERVIEW.
  561. This can also be configured on a per-file basis by adding one of
  562. the following lines anywhere in the buffer:
  563. #+STARTUP: fold (or `overview', this is equivalent)
  564. #+STARTUP: nofold (or `showall', this is equivalent)
  565. #+STARTUP: content
  566. #+STARTUP: showeverything
  567. By default, this option is ignored when Org opens agenda files
  568. for the first time. If you want the agenda to honor the startup
  569. option, set `org-agenda-inhibit-startup' to nil."
  570. :group 'org-startup
  571. :type '(choice
  572. (const :tag "nofold: show all" nil)
  573. (const :tag "fold: overview" t)
  574. (const :tag "content: all headlines" content)
  575. (const :tag "show everything, even drawers" showeverything)))
  576. (defcustom org-startup-truncated t
  577. "Non-nil means entering Org-mode will set `truncate-lines'.
  578. This is useful since some lines containing links can be very long and
  579. uninteresting. Also tables look terrible when wrapped."
  580. :group 'org-startup
  581. :type 'boolean)
  582. (defcustom org-startup-indented nil
  583. "Non-nil means turn on `org-indent-mode' on startup.
  584. This can also be configured on a per-file basis by adding one of
  585. the following lines anywhere in the buffer:
  586. #+STARTUP: indent
  587. #+STARTUP: noindent"
  588. :group 'org-structure
  589. :type '(choice
  590. (const :tag "Not" nil)
  591. (const :tag "Globally (slow on startup in large files)" t)))
  592. (defcustom org-use-sub-superscripts t
  593. "Non-nil means interpret \"_\" and \"^\" for display.
  594. When this option is turned on, you can use TeX-like syntax for sub- and
  595. superscripts. Several characters after \"_\" or \"^\" will be
  596. considered as a single item - so grouping with {} is normally not
  597. needed. For example, the following things will be parsed as single
  598. sub- or superscripts.
  599. 10^24 or 10^tau several digits will be considered 1 item.
  600. 10^-12 or 10^-tau a leading sign with digits or a word
  601. x^2-y^3 will be read as x^2 - y^3, because items are
  602. terminated by almost any nonword/nondigit char.
  603. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  604. Still, ambiguity is possible - so when in doubt use {} to enclose
  605. the sub/superscript. If you set this variable to the symbol
  606. `{}', the braces are *required* in order to trigger
  607. interpretations as sub/superscript. This can be helpful in
  608. documents that need \"_\" frequently in plain text."
  609. :group 'org-startup
  610. :version "24.1"
  611. :type '(choice
  612. (const :tag "Always interpret" t)
  613. (const :tag "Only with braces" {})
  614. (const :tag "Never interpret" nil)))
  615. (defcustom org-startup-with-beamer-mode nil
  616. "Non-nil means turn on `org-beamer-mode' on startup.
  617. This can also be configured on a per-file basis by adding one of
  618. the following lines anywhere in the buffer:
  619. #+STARTUP: beamer"
  620. :group 'org-startup
  621. :version "24.1"
  622. :type 'boolean)
  623. (defcustom org-startup-align-all-tables nil
  624. "Non-nil means align all tables when visiting a file.
  625. This is useful when the column width in tables is forced with <N> cookies
  626. in table fields. Such tables will look correct only after the first re-align.
  627. This can also be configured on a per-file basis by adding one of
  628. the following lines anywhere in the buffer:
  629. #+STARTUP: align
  630. #+STARTUP: noalign"
  631. :group 'org-startup
  632. :type 'boolean)
  633. (defcustom org-startup-with-inline-images nil
  634. "Non-nil means show inline images when loading a new Org file.
  635. This can also be configured on a per-file basis by adding one of
  636. the following lines anywhere in the buffer:
  637. #+STARTUP: inlineimages
  638. #+STARTUP: noinlineimages"
  639. :group 'org-startup
  640. :version "24.1"
  641. :type 'boolean)
  642. (defcustom org-startup-with-latex-preview nil
  643. "Non-nil means preview LaTeX fragments when loading a new Org file.
  644. This can also be configured on a per-file basis by adding one of
  645. the followinglines anywhere in the buffer:
  646. #+STARTUP: latexpreview
  647. #+STARTUP: nolatexpreview"
  648. :group 'org-startup
  649. :version "24.4"
  650. :package-version '(Org . "8.0")
  651. :type 'boolean)
  652. (defcustom org-insert-mode-line-in-empty-file nil
  653. "Non-nil means insert the first line setting Org-mode in empty files.
  654. When the function `org-mode' is called interactively in an empty file, this
  655. normally means that the file name does not automatically trigger Org-mode.
  656. To ensure that the file will always be in Org-mode in the future, a
  657. line enforcing Org-mode will be inserted into the buffer, if this option
  658. has been set."
  659. :group 'org-startup
  660. :type 'boolean)
  661. (defcustom org-replace-disputed-keys nil
  662. "Non-nil means use alternative key bindings for some keys.
  663. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  664. These keys are also used by other packages like shift-selection-mode'
  665. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  666. If you want to use Org-mode together with one of these other modes,
  667. or more generally if you would like to move some Org-mode commands to
  668. other keys, set this variable and configure the keys with the variable
  669. `org-disputed-keys'.
  670. This option is only relevant at load-time of Org-mode, and must be set
  671. *before* org.el is loaded. Changing it requires a restart of Emacs to
  672. become effective."
  673. :group 'org-startup
  674. :type 'boolean)
  675. (defcustom org-use-extra-keys nil
  676. "Non-nil means use extra key sequence definitions for certain commands.
  677. This happens automatically if you run XEmacs or if `window-system'
  678. is nil. This variable lets you do the same manually. You must
  679. set it before loading org.
  680. Example: on Carbon Emacs 22 running graphically, with an external
  681. keyboard on a Powerbook, the default way of setting M-left might
  682. not work for either Alt or ESC. Setting this variable will make
  683. it work for ESC."
  684. :group 'org-startup
  685. :type 'boolean)
  686. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  687. (defcustom org-disputed-keys
  688. '(([(shift up)] . [(meta p)])
  689. ([(shift down)] . [(meta n)])
  690. ([(shift left)] . [(meta -)])
  691. ([(shift right)] . [(meta +)])
  692. ([(control shift right)] . [(meta shift +)])
  693. ([(control shift left)] . [(meta shift -)]))
  694. "Keys for which Org-mode and other modes compete.
  695. This is an alist, cars are the default keys, second element specifies
  696. the alternative to use when `org-replace-disputed-keys' is t.
  697. Keys can be specified in any syntax supported by `define-key'.
  698. The value of this option takes effect only at Org-mode's startup,
  699. therefore you'll have to restart Emacs to apply it after changing."
  700. :group 'org-startup
  701. :type 'alist)
  702. (defun org-key (key)
  703. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  704. Or return the original if not disputed.
  705. Also apply the translations defined in `org-xemacs-key-equivalents'."
  706. (when org-replace-disputed-keys
  707. (let* ((nkey (key-description key))
  708. (x (org-find-if (lambda (x)
  709. (equal (key-description (car x)) nkey))
  710. org-disputed-keys)))
  711. (setq key (if x (cdr x) key))))
  712. (when (featurep 'xemacs)
  713. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  714. key)
  715. (defun org-find-if (predicate seq)
  716. (catch 'exit
  717. (while seq
  718. (if (funcall predicate (car seq))
  719. (throw 'exit (car seq))
  720. (pop seq)))))
  721. (defun org-defkey (keymap key def)
  722. "Define a key, possibly translated, as returned by `org-key'."
  723. (define-key keymap (org-key key) def))
  724. (defcustom org-ellipsis nil
  725. "The ellipsis to use in the Org-mode outline.
  726. When nil, just use the standard three dots. When a string, use that instead,
  727. When a face, use the standard 3 dots, but with the specified face.
  728. The change affects only Org-mode (which will then use its own display table).
  729. Changing this requires executing `M-x org-mode' in a buffer to become
  730. effective."
  731. :group 'org-startup
  732. :type '(choice (const :tag "Default" nil)
  733. (face :tag "Face" :value org-warning)
  734. (string :tag "String" :value "...#")))
  735. (defvar org-display-table nil
  736. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  737. (defgroup org-keywords nil
  738. "Keywords in Org-mode."
  739. :tag "Org Keywords"
  740. :group 'org)
  741. (defcustom org-deadline-string "DEADLINE:"
  742. "String to mark deadline entries.
  743. A deadline is this string, followed by a time stamp. Should be a word,
  744. terminated by a colon. You can insert a schedule keyword and
  745. a timestamp with \\[org-deadline].
  746. Changes become only effective after restarting Emacs."
  747. :group 'org-keywords
  748. :type 'string)
  749. (defcustom org-scheduled-string "SCHEDULED:"
  750. "String to mark scheduled TODO entries.
  751. A schedule is this string, followed by a time stamp. Should be a word,
  752. terminated by a colon. You can insert a schedule keyword and
  753. a timestamp with \\[org-schedule].
  754. Changes become only effective after restarting Emacs."
  755. :group 'org-keywords
  756. :type 'string)
  757. (defcustom org-closed-string "CLOSED:"
  758. "String used as the prefix for timestamps logging closing a TODO entry."
  759. :group 'org-keywords
  760. :type 'string)
  761. (defcustom org-clock-string "CLOCK:"
  762. "String used as prefix for timestamps clocking work hours on an item."
  763. :group 'org-keywords
  764. :type 'string)
  765. (defcustom org-closed-keep-when-no-todo nil
  766. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  767. :group 'org-todo
  768. :group 'org-keywords
  769. :version "24.4"
  770. :package-version '(Org . "8.0")
  771. :type 'boolean)
  772. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  773. org-scheduled-string "\\|"
  774. org-deadline-string "\\|"
  775. org-closed-string "\\|"
  776. org-clock-string "\\)")
  777. "Matches a line with planning or clock info.")
  778. (defcustom org-comment-string "COMMENT"
  779. "Entries starting with this keyword will never be exported.
  780. An entry can be toggled between COMMENT and normal with
  781. \\[org-toggle-comment].
  782. Changes become only effective after restarting Emacs."
  783. :group 'org-keywords
  784. :type 'string)
  785. (defcustom org-quote-string "QUOTE"
  786. "Entries starting with this keyword will be exported in fixed-width font.
  787. Quoting applies only to the text in the entry following the headline, and does
  788. not extend beyond the next headline, even if that is lower level.
  789. An entry can be toggled between QUOTE and normal with
  790. \\[org-toggle-fixed-width-section]."
  791. :group 'org-keywords
  792. :type 'string)
  793. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  794. "Matches first line of a hidden block.
  795. Group 1 contains drawer's name.")
  796. (defconst org-repeat-re
  797. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  798. "Regular expression for specifying repeated events.
  799. After a match, group 1 contains the repeat expression.")
  800. (defgroup org-structure nil
  801. "Options concerning the general structure of Org-mode files."
  802. :tag "Org Structure"
  803. :group 'org)
  804. (defgroup org-reveal-location nil
  805. "Options about how to make context of a location visible."
  806. :tag "Org Reveal Location"
  807. :group 'org-structure)
  808. (defconst org-context-choice
  809. '(choice
  810. (const :tag "Always" t)
  811. (const :tag "Never" nil)
  812. (repeat :greedy t :tag "Individual contexts"
  813. (cons
  814. (choice :tag "Context"
  815. (const agenda)
  816. (const org-goto)
  817. (const occur-tree)
  818. (const tags-tree)
  819. (const link-search)
  820. (const mark-goto)
  821. (const bookmark-jump)
  822. (const isearch)
  823. (const default))
  824. (boolean))))
  825. "Contexts for the reveal options.")
  826. (defcustom org-show-hierarchy-above '((default . t))
  827. "Non-nil means show full hierarchy when revealing a location.
  828. Org-mode often shows locations in an org-mode file which might have
  829. been invisible before. When this is set, the hierarchy of headings
  830. above the exposed location is shown.
  831. Turning this off for example for sparse trees makes them very compact.
  832. Instead of t, this can also be an alist specifying this option for different
  833. contexts. Valid contexts are
  834. agenda when exposing an entry from the agenda
  835. org-goto when using the command `org-goto' on key C-c C-j
  836. occur-tree when using the command `org-occur' on key C-c /
  837. tags-tree when constructing a sparse tree based on tags matches
  838. link-search when exposing search matches associated with a link
  839. mark-goto when exposing the jump goal of a mark
  840. bookmark-jump when exposing a bookmark location
  841. isearch when exiting from an incremental search
  842. default default for all contexts not set explicitly"
  843. :group 'org-reveal-location
  844. :type org-context-choice)
  845. (defcustom org-show-following-heading '((default . nil))
  846. "Non-nil means show following heading when revealing a location.
  847. Org-mode often shows locations in an org-mode file which might have
  848. been invisible before. When this is set, the heading following the
  849. match is shown.
  850. Turning this off for example for sparse trees makes them very compact,
  851. but makes it harder to edit the location of the match. In such a case,
  852. use the command \\[org-reveal] to show more context.
  853. Instead of t, this can also be an alist specifying this option for different
  854. contexts. See `org-show-hierarchy-above' for valid contexts."
  855. :group 'org-reveal-location
  856. :type org-context-choice)
  857. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  858. "Non-nil means show all sibling heading when revealing a location.
  859. Org-mode often shows locations in an org-mode file which might have
  860. been invisible before. When this is set, the sibling of the current entry
  861. heading are all made visible. If `org-show-hierarchy-above' is t,
  862. the same happens on each level of the hierarchy above the current entry.
  863. By default this is on for the isearch context, off for all other contexts.
  864. Turning this off for example for sparse trees makes them very compact,
  865. but makes it harder to edit the location of the match. In such a case,
  866. use the command \\[org-reveal] to show more context.
  867. Instead of t, this can also be an alist specifying this option for different
  868. contexts. See `org-show-hierarchy-above' for valid contexts."
  869. :group 'org-reveal-location
  870. :type org-context-choice
  871. :version "24.4"
  872. :package-version '(Org . "8.0"))
  873. (defcustom org-show-entry-below '((default . nil))
  874. "Non-nil means show the entry below a headline when revealing a location.
  875. Org-mode often shows locations in an org-mode file which might have
  876. been invisible before. When this is set, the text below the headline that is
  877. exposed is also shown.
  878. By default this is off for all contexts.
  879. Instead of t, this can also be an alist specifying this option for different
  880. contexts. See `org-show-hierarchy-above' for valid contexts."
  881. :group 'org-reveal-location
  882. :type org-context-choice)
  883. (defcustom org-indirect-buffer-display 'other-window
  884. "How should indirect tree buffers be displayed?
  885. This applies to indirect buffers created with the commands
  886. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  887. Valid values are:
  888. current-window Display in the current window
  889. other-window Just display in another window.
  890. dedicated-frame Create one new frame, and re-use it each time.
  891. new-frame Make a new frame each time. Note that in this case
  892. previously-made indirect buffers are kept, and you need to
  893. kill these buffers yourself."
  894. :group 'org-structure
  895. :group 'org-agenda-windows
  896. :type '(choice
  897. (const :tag "In current window" current-window)
  898. (const :tag "In current frame, other window" other-window)
  899. (const :tag "Each time a new frame" new-frame)
  900. (const :tag "One dedicated frame" dedicated-frame)))
  901. (defcustom org-use-speed-commands nil
  902. "Non-nil means activate single letter commands at beginning of a headline.
  903. This may also be a function to test for appropriate locations where speed
  904. commands should be active."
  905. :group 'org-structure
  906. :type '(choice
  907. (const :tag "Never" nil)
  908. (const :tag "At beginning of headline stars" t)
  909. (function)))
  910. (defcustom org-speed-commands-user nil
  911. "Alist of additional speed commands.
  912. This list will be checked before `org-speed-commands-default'
  913. when the variable `org-use-speed-commands' is non-nil
  914. and when the cursor is at the beginning of a headline.
  915. The car if each entry is a string with a single letter, which must
  916. be assigned to `self-insert-command' in the global map.
  917. The cdr is either a command to be called interactively, a function
  918. to be called, or a form to be evaluated.
  919. An entry that is just a list with a single string will be interpreted
  920. as a descriptive headline that will be added when listing the speed
  921. commands in the Help buffer using the `?' speed command."
  922. :group 'org-structure
  923. :type '(repeat :value ("k" . ignore)
  924. (choice :value ("k" . ignore)
  925. (list :tag "Descriptive Headline" (string :tag "Headline"))
  926. (cons :tag "Letter and Command"
  927. (string :tag "Command letter")
  928. (choice
  929. (function)
  930. (sexp))))))
  931. (defcustom org-bookmark-names-plist
  932. '(:last-capture "org-capture-last-stored"
  933. :last-refile "org-refile-last-stored"
  934. :last-capture-marker "org-capture-last-stored-marker")
  935. "Names for bookmarks automatically set by some Org commands.
  936. This can provide strings as names for a number of bookmakrs Org sets
  937. automatically. The following keys are currently implemented:
  938. :last-capture
  939. :last-capture-marker
  940. :last-refile
  941. When a key does not show up in the property list, the corresponding bookmark
  942. is not set."
  943. :group 'org-structure
  944. :type 'plist)
  945. (defgroup org-cycle nil
  946. "Options concerning visibility cycling in Org-mode."
  947. :tag "Org Cycle"
  948. :group 'org-structure)
  949. (defcustom org-cycle-skip-children-state-if-no-children t
  950. "Non-nil means skip CHILDREN state in entries that don't have any."
  951. :group 'org-cycle
  952. :type 'boolean)
  953. (defcustom org-cycle-max-level nil
  954. "Maximum level which should still be subject to visibility cycling.
  955. Levels higher than this will, for cycling, be treated as text, not a headline.
  956. When `org-odd-levels-only' is set, a value of N in this variable actually
  957. means 2N-1 stars as the limiting headline.
  958. When nil, cycle all levels.
  959. Note that the limiting level of cycling is also influenced by
  960. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  961. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  962. than its value."
  963. :group 'org-cycle
  964. :type '(choice
  965. (const :tag "No limit" nil)
  966. (integer :tag "Maximum level")))
  967. (defcustom org-hide-block-startup nil
  968. "Non-nil means entering Org-mode will fold all blocks.
  969. This can also be set in on a per-file basis with
  970. #+STARTUP: hideblocks
  971. #+STARTUP: showblocks"
  972. :group 'org-startup
  973. :group 'org-cycle
  974. :type 'boolean)
  975. (defcustom org-cycle-global-at-bob nil
  976. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  977. This makes it possible to do global cycling without having to use S-TAB or
  978. \\[universal-argument] TAB. For this special case to work, the first line
  979. of the buffer must not be a headline -- it may be empty or some other text.
  980. When used in this way, `org-cycle-hook' is disabled temporarily to make
  981. sure the cursor stays at the beginning of the buffer. When this option is
  982. nil, don't do anything special at the beginning of the buffer."
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-level-after-item/entry-creation t
  986. "Non-nil means cycle entry level or item indentation in new empty entries.
  987. When the cursor is at the end of an empty headline, i.e., with only stars
  988. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  989. and then all possible ancestor states, before returning to the original state.
  990. This makes data entry extremely fast: M-RET to create a new headline,
  991. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  992. When the cursor is at the end of an empty plain list item, one TAB will
  993. make it a subitem, two or more tabs will back up to make this an item
  994. higher up in the item hierarchy."
  995. :group 'org-cycle
  996. :type 'boolean)
  997. (defcustom org-cycle-emulate-tab t
  998. "Where should `org-cycle' emulate TAB.
  999. nil Never
  1000. white Only in completely white lines
  1001. whitestart Only at the beginning of lines, before the first non-white char
  1002. t Everywhere except in headlines
  1003. exc-hl-bol Everywhere except at the start of a headline
  1004. If TAB is used in a place where it does not emulate TAB, the current subtree
  1005. visibility is cycled."
  1006. :group 'org-cycle
  1007. :type '(choice (const :tag "Never" nil)
  1008. (const :tag "Only in completely white lines" white)
  1009. (const :tag "Before first char in a line" whitestart)
  1010. (const :tag "Everywhere except in headlines" t)
  1011. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1012. (defcustom org-cycle-separator-lines 2
  1013. "Number of empty lines needed to keep an empty line between collapsed trees.
  1014. If you leave an empty line between the end of a subtree and the following
  1015. headline, this empty line is hidden when the subtree is folded.
  1016. Org-mode will leave (exactly) one empty line visible if the number of
  1017. empty lines is equal or larger to the number given in this variable.
  1018. So the default 2 means at least 2 empty lines after the end of a subtree
  1019. are needed to produce free space between a collapsed subtree and the
  1020. following headline.
  1021. If the number is negative, and the number of empty lines is at least -N,
  1022. all empty lines are shown.
  1023. Special case: when 0, never leave empty lines in collapsed view."
  1024. :group 'org-cycle
  1025. :type 'integer)
  1026. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1027. (defcustom org-pre-cycle-hook nil
  1028. "Hook that is run before visibility cycling is happening.
  1029. The function(s) in this hook must accept a single argument which indicates
  1030. the new state that will be set right after running this hook. The
  1031. argument is a symbol. Before a global state change, it can have the values
  1032. `overview', `content', or `all'. Before a local state change, it can have
  1033. the values `folded', `children', or `subtree'."
  1034. :group 'org-cycle
  1035. :type 'hook)
  1036. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1037. org-cycle-hide-drawers
  1038. org-cycle-hide-inline-tasks
  1039. org-cycle-show-empty-lines
  1040. org-optimize-window-after-visibility-change)
  1041. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1042. The function(s) in this hook must accept a single argument which indicates
  1043. the new state that was set by the most recent `org-cycle' command. The
  1044. argument is a symbol. After a global state change, it can have the values
  1045. `overview', `contents', or `all'. After a local state change, it can have
  1046. the values `folded', `children', or `subtree'."
  1047. :group 'org-cycle
  1048. :type 'hook)
  1049. (defgroup org-edit-structure nil
  1050. "Options concerning structure editing in Org-mode."
  1051. :tag "Org Edit Structure"
  1052. :group 'org-structure)
  1053. (defcustom org-odd-levels-only nil
  1054. "Non-nil means skip even levels and only use odd levels for the outline.
  1055. This has the effect that two stars are being added/taken away in
  1056. promotion/demotion commands. It also influences how levels are
  1057. handled by the exporters.
  1058. Changing it requires restart of `font-lock-mode' to become effective
  1059. for fontification also in regions already fontified.
  1060. You may also set this on a per-file basis by adding one of the following
  1061. lines to the buffer:
  1062. #+STARTUP: odd
  1063. #+STARTUP: oddeven"
  1064. :group 'org-edit-structure
  1065. :group 'org-appearance
  1066. :type 'boolean)
  1067. (defcustom org-adapt-indentation t
  1068. "Non-nil means adapt indentation to outline node level.
  1069. When this variable is set, Org assumes that you write outlines by
  1070. indenting text in each node to align with the headline (after the stars).
  1071. The following issues are influenced by this variable:
  1072. - When this is set and the *entire* text in an entry is indented, the
  1073. indentation is increased by one space in a demotion command, and
  1074. decreased by one in a promotion command. If any line in the entry
  1075. body starts with text at column 0, indentation is not changed at all.
  1076. - Property drawers and planning information is inserted indented when
  1077. this variable s set. When nil, they will not be indented.
  1078. - TAB indents a line relative to context. The lines below a headline
  1079. will be indented when this variable is set.
  1080. Note that this is all about true indentation, by adding and removing
  1081. space characters. See also `org-indent.el' which does level-dependent
  1082. indentation in a virtual way, i.e. at display time in Emacs."
  1083. :group 'org-edit-structure
  1084. :type 'boolean)
  1085. (defcustom org-special-ctrl-a/e nil
  1086. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1087. When t, `C-a' will bring back the cursor to the beginning of the
  1088. headline text, i.e. after the stars and after a possible TODO
  1089. keyword. In an item, this will be the position after bullet and
  1090. check-box, if any. When the cursor is already at that position,
  1091. another `C-a' will bring it to the beginning of the line.
  1092. `C-e' will jump to the end of the headline, ignoring the presence
  1093. of tags in the headline. A second `C-e' will then jump to the
  1094. true end of the line, after any tags. This also means that, when
  1095. this variable is non-nil, `C-e' also will never jump beyond the
  1096. end of the heading of a folded section, i.e. not after the
  1097. ellipses.
  1098. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1099. normally, going to the true line boundary first. Only a directly
  1100. following, identical keypress will bring the cursor to the
  1101. special positions.
  1102. This may also be a cons cell where the behavior for `C-a' and
  1103. `C-e' is set separately."
  1104. :group 'org-edit-structure
  1105. :type '(choice
  1106. (const :tag "off" nil)
  1107. (const :tag "on: after stars/bullet and before tags first" t)
  1108. (const :tag "reversed: true line boundary first" reversed)
  1109. (cons :tag "Set C-a and C-e separately"
  1110. (choice :tag "Special C-a"
  1111. (const :tag "off" nil)
  1112. (const :tag "on: after stars/bullet first" t)
  1113. (const :tag "reversed: before stars/bullet first" reversed))
  1114. (choice :tag "Special C-e"
  1115. (const :tag "off" nil)
  1116. (const :tag "on: before tags first" t)
  1117. (const :tag "reversed: after tags first" reversed)))))
  1118. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1119. (defcustom org-special-ctrl-k nil
  1120. "Non-nil means `C-k' will behave specially in headlines.
  1121. When nil, `C-k' will call the default `kill-line' command.
  1122. When t, the following will happen while the cursor is in the headline:
  1123. - When the cursor is at the beginning of a headline, kill the entire
  1124. line and possible the folded subtree below the line.
  1125. - When in the middle of the headline text, kill the headline up to the tags.
  1126. - When after the headline text, kill the tags."
  1127. :group 'org-edit-structure
  1128. :type 'boolean)
  1129. (defcustom org-ctrl-k-protect-subtree nil
  1130. "Non-nil means, do not delete a hidden subtree with C-k.
  1131. When set to the symbol `error', simply throw an error when C-k is
  1132. used to kill (part-of) a headline that has hidden text behind it.
  1133. Any other non-nil value will result in a query to the user, if it is
  1134. OK to kill that hidden subtree. When nil, kill without remorse."
  1135. :group 'org-edit-structure
  1136. :version "24.1"
  1137. :type '(choice
  1138. (const :tag "Do not protect hidden subtrees" nil)
  1139. (const :tag "Protect hidden subtrees with a security query" t)
  1140. (const :tag "Never kill a hidden subtree with C-k" error)))
  1141. (defcustom org-special-ctrl-o t
  1142. "Non-nil means, make `C-o' insert a row in tables."
  1143. :group 'org-edit-structure
  1144. :type 'boolean)
  1145. (defcustom org-catch-invisible-edits nil
  1146. "Check if in invisible region before inserting or deleting a character.
  1147. Valid values are:
  1148. nil Do not check, so just do invisible edits.
  1149. error Throw an error and do nothing.
  1150. show Make point visible, and do the requested edit.
  1151. show-and-error Make point visible, then throw an error and abort the edit.
  1152. smart Make point visible, and do insertion/deletion if it is
  1153. adjacent to visible text and the change feels predictable.
  1154. Never delete a previously invisible character or add in the
  1155. middle or right after an invisible region. Basically, this
  1156. allows insertion and backward-delete right before ellipses.
  1157. FIXME: maybe in this case we should not even show?"
  1158. :group 'org-edit-structure
  1159. :version "24.1"
  1160. :type '(choice
  1161. (const :tag "Do not check" nil)
  1162. (const :tag "Throw error when trying to edit" error)
  1163. (const :tag "Unhide, but do not do the edit" show-and-error)
  1164. (const :tag "Show invisible part and do the edit" show)
  1165. (const :tag "Be smart and do the right thing" smart)))
  1166. (defcustom org-yank-folded-subtrees t
  1167. "Non-nil means when yanking subtrees, fold them.
  1168. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1169. it starts with a heading and all other headings in it are either children
  1170. or siblings, then fold all the subtrees. However, do this only if no
  1171. text after the yank would be swallowed into a folded tree by this action."
  1172. :group 'org-edit-structure
  1173. :type 'boolean)
  1174. (defcustom org-yank-adjusted-subtrees nil
  1175. "Non-nil means when yanking subtrees, adjust the level.
  1176. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1177. this function for details."
  1178. :group 'org-edit-structure
  1179. :type 'boolean)
  1180. (defcustom org-M-RET-may-split-line '((default . t))
  1181. "Non-nil means M-RET will split the line at the cursor position.
  1182. When nil, it will go to the end of the line before making a
  1183. new line.
  1184. You may also set this option in a different way for different
  1185. contexts. Valid contexts are:
  1186. headline when creating a new headline
  1187. item when creating a new item
  1188. table in a table field
  1189. default the value to be used for all contexts not explicitly
  1190. customized"
  1191. :group 'org-structure
  1192. :group 'org-table
  1193. :type '(choice
  1194. (const :tag "Always" t)
  1195. (const :tag "Never" nil)
  1196. (repeat :greedy t :tag "Individual contexts"
  1197. (cons
  1198. (choice :tag "Context"
  1199. (const headline)
  1200. (const item)
  1201. (const table)
  1202. (const default))
  1203. (boolean)))))
  1204. (defcustom org-insert-heading-respect-content nil
  1205. "Non-nil means insert new headings after the current subtree.
  1206. When nil, the new heading is created directly after the current line.
  1207. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1208. this variable on for the duration of the command."
  1209. :group 'org-structure
  1210. :type 'boolean)
  1211. (defcustom org-blank-before-new-entry '((heading . auto)
  1212. (plain-list-item . auto))
  1213. "Should `org-insert-heading' leave a blank line before new heading/item?
  1214. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1215. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1216. which case Org will look at the surrounding headings/items and try to
  1217. make an intelligent decision whether to insert a blank line or not.
  1218. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1219. the setting here is ignored and no empty line is inserted to avoid breaking
  1220. the list structure."
  1221. :group 'org-edit-structure
  1222. :type '(list
  1223. (cons (const heading)
  1224. (choice (const :tag "Never" nil)
  1225. (const :tag "Always" t)
  1226. (const :tag "Auto" auto)))
  1227. (cons (const plain-list-item)
  1228. (choice (const :tag "Never" nil)
  1229. (const :tag "Always" t)
  1230. (const :tag "Auto" auto)))))
  1231. (defcustom org-insert-heading-hook nil
  1232. "Hook being run after inserting a new heading."
  1233. :group 'org-edit-structure
  1234. :type 'hook)
  1235. (defcustom org-enable-fixed-width-editor t
  1236. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1237. This currently only means they are never auto-wrapped.
  1238. When nil, such lines will be treated like ordinary lines.
  1239. See also the QUOTE keyword."
  1240. :group 'org-edit-structure
  1241. :type 'boolean)
  1242. (defcustom org-goto-auto-isearch t
  1243. "Non-nil means typing characters in `org-goto' starts incremental search.
  1244. When nil, you can use these keybindings to navigate the buffer:
  1245. q Quit the org-goto interface
  1246. n Go to the next visible heading
  1247. p Go to the previous visible heading
  1248. f Go one heading forward on same level
  1249. b Go one heading backward on same level
  1250. u Go one heading up"
  1251. :group 'org-edit-structure
  1252. :type 'boolean)
  1253. (defgroup org-sparse-trees nil
  1254. "Options concerning sparse trees in Org-mode."
  1255. :tag "Org Sparse Trees"
  1256. :group 'org-structure)
  1257. (defcustom org-highlight-sparse-tree-matches t
  1258. "Non-nil means highlight all matches that define a sparse tree.
  1259. The highlights will automatically disappear the next time the buffer is
  1260. changed by an edit command."
  1261. :group 'org-sparse-trees
  1262. :type 'boolean)
  1263. (defcustom org-remove-highlights-with-change t
  1264. "Non-nil means any change to the buffer will remove temporary highlights.
  1265. Such highlights are created by `org-occur' and `org-clock-display'.
  1266. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1267. The highlights created by `org-preview-latex-fragment' always need
  1268. `C-c C-c' to be removed."
  1269. :group 'org-sparse-trees
  1270. :group 'org-time
  1271. :type 'boolean)
  1272. (defcustom org-occur-hook '(org-first-headline-recenter)
  1273. "Hook that is run after `org-occur' has constructed a sparse tree.
  1274. This can be used to recenter the window to show as much of the structure
  1275. as possible."
  1276. :group 'org-sparse-trees
  1277. :type 'hook)
  1278. (defgroup org-imenu-and-speedbar nil
  1279. "Options concerning imenu and speedbar in Org-mode."
  1280. :tag "Org Imenu and Speedbar"
  1281. :group 'org-structure)
  1282. (defcustom org-imenu-depth 2
  1283. "The maximum level for Imenu access to Org-mode headlines.
  1284. This also applied for speedbar access."
  1285. :group 'org-imenu-and-speedbar
  1286. :type 'integer)
  1287. (defgroup org-table nil
  1288. "Options concerning tables in Org-mode."
  1289. :tag "Org Table"
  1290. :group 'org)
  1291. (defcustom org-enable-table-editor 'optimized
  1292. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1293. When nil, such lines will be treated like ordinary lines.
  1294. When equal to the symbol `optimized', the table editor will be optimized to
  1295. do the following:
  1296. - Automatic overwrite mode in front of whitespace in table fields.
  1297. This makes the structure of the table stay in tact as long as the edited
  1298. field does not exceed the column width.
  1299. - Minimize the number of realigns. Normally, the table is aligned each time
  1300. TAB or RET are pressed to move to another field. With optimization this
  1301. happens only if changes to a field might have changed the column width.
  1302. Optimization requires replacing the functions `self-insert-command',
  1303. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1304. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1305. very good at guessing when a re-align will be necessary, but you can always
  1306. force one with \\[org-ctrl-c-ctrl-c].
  1307. If you would like to use the optimized version in Org-mode, but the
  1308. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1309. This variable can be used to turn on and off the table editor during a session,
  1310. but in order to toggle optimization, a restart is required.
  1311. See also the variable `org-table-auto-blank-field'."
  1312. :group 'org-table
  1313. :type '(choice
  1314. (const :tag "off" nil)
  1315. (const :tag "on" t)
  1316. (const :tag "on, optimized" optimized)))
  1317. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1318. (version<= emacs-version "24.1"))
  1319. "Non-nil means cluster self-insert commands for undo when possible.
  1320. If this is set, then, like in the Emacs command loop, 20 consecutive
  1321. characters will be undone together.
  1322. This is configurable, because there is some impact on typing performance."
  1323. :group 'org-table
  1324. :type 'boolean)
  1325. (defcustom org-table-tab-recognizes-table.el t
  1326. "Non-nil means TAB will automatically notice a table.el table.
  1327. When it sees such a table, it moves point into it and - if necessary -
  1328. calls `table-recognize-table'."
  1329. :group 'org-table-editing
  1330. :type 'boolean)
  1331. (defgroup org-link nil
  1332. "Options concerning links in Org-mode."
  1333. :tag "Org Link"
  1334. :group 'org)
  1335. (defvar org-link-abbrev-alist-local nil
  1336. "Buffer-local version of `org-link-abbrev-alist', which see.
  1337. The value of this is taken from the #+LINK lines.")
  1338. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1339. (defcustom org-link-abbrev-alist nil
  1340. "Alist of link abbreviations.
  1341. The car of each element is a string, to be replaced at the start of a link.
  1342. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1343. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1344. [[linkkey:tag][description]]
  1345. The 'linkkey' must be a word word, starting with a letter, followed
  1346. by letters, numbers, '-' or '_'.
  1347. If REPLACE is a string, the tag will simply be appended to create the link.
  1348. If the string contains \"%s\", the tag will be inserted there. If the string
  1349. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1350. at that point (see the function `url-hexify-string'). If the string contains
  1351. the specifier \"%(my-function)\", then the custom function `my-function' will
  1352. be invoked: this function takes the tag as its only argument and must return
  1353. a string.
  1354. REPLACE may also be a function that will be called with the tag as the
  1355. only argument to create the link, which should be returned as a string.
  1356. See the manual for examples."
  1357. :group 'org-link
  1358. :type '(repeat
  1359. (cons
  1360. (string :tag "Protocol")
  1361. (choice
  1362. (string :tag "Format")
  1363. (function)))))
  1364. (defcustom org-descriptive-links t
  1365. "Non-nil means Org will display descriptive links.
  1366. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1367. \"Org Website\", hiding the link itself and just displaying its
  1368. description. When set to `nil', Org will display the full links
  1369. literally.
  1370. You can interactively set the value of this variable by calling
  1371. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1372. :group 'org-link
  1373. :type 'boolean)
  1374. (defcustom org-link-file-path-type 'adaptive
  1375. "How the path name in file links should be stored.
  1376. Valid values are:
  1377. relative Relative to the current directory, i.e. the directory of the file
  1378. into which the link is being inserted.
  1379. absolute Absolute path, if possible with ~ for home directory.
  1380. noabbrev Absolute path, no abbreviation of home directory.
  1381. adaptive Use relative path for files in the current directory and sub-
  1382. directories of it. For other files, use an absolute path."
  1383. :group 'org-link
  1384. :type '(choice
  1385. (const relative)
  1386. (const absolute)
  1387. (const noabbrev)
  1388. (const adaptive)))
  1389. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1390. "Types of links that should be activated in Org-mode files.
  1391. This is a list of symbols, each leading to the activation of a certain link
  1392. type. In principle, it does not hurt to turn on most link types - there may
  1393. be a small gain when turning off unused link types. The types are:
  1394. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1395. angle Links in angular brackets that may contain whitespace like
  1396. <bbdb:Carsten Dominik>.
  1397. plain Plain links in normal text, no whitespace, like http://google.com.
  1398. radio Text that is matched by a radio target, see manual for details.
  1399. tag Tag settings in a headline (link to tag search).
  1400. date Time stamps (link to calendar).
  1401. footnote Footnote labels.
  1402. Changing this variable requires a restart of Emacs to become effective."
  1403. :group 'org-link
  1404. :type '(set :greedy t
  1405. (const :tag "Double bracket links" bracket)
  1406. (const :tag "Angular bracket links" angle)
  1407. (const :tag "Plain text links" plain)
  1408. (const :tag "Radio target matches" radio)
  1409. (const :tag "Tags" tag)
  1410. (const :tag "Timestamps" date)
  1411. (const :tag "Footnotes" footnote)))
  1412. (defcustom org-make-link-description-function nil
  1413. "Function to use for generating link descriptions from links.
  1414. When nil, the link location will be used. This function must take
  1415. two parameters: the first one is the link, the second one is the
  1416. description generated by `org-insert-link'. The function should
  1417. return the description to use."
  1418. :group 'org-link
  1419. :type '(choice (const nil) (function)))
  1420. (defgroup org-link-store nil
  1421. "Options concerning storing links in Org-mode."
  1422. :tag "Org Store Link"
  1423. :group 'org-link)
  1424. (defcustom org-url-hexify-p t
  1425. "When non-nil, hexify URL when creating a link."
  1426. :type 'boolean
  1427. :version "24.3"
  1428. :group 'org-link-store)
  1429. (defcustom org-email-link-description-format "Email %c: %.30s"
  1430. "Format of the description part of a link to an email or usenet message.
  1431. The following %-escapes will be replaced by corresponding information:
  1432. %F full \"From\" field
  1433. %f name, taken from \"From\" field, address if no name
  1434. %T full \"To\" field
  1435. %t first name in \"To\" field, address if no name
  1436. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1437. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1438. %s subject
  1439. %d date
  1440. %m message-id.
  1441. You may use normal field width specification between the % and the letter.
  1442. This is for example useful to limit the length of the subject.
  1443. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1444. :group 'org-link-store
  1445. :type 'string)
  1446. (defcustom org-from-is-user-regexp
  1447. (let (r1 r2)
  1448. (when (and user-mail-address (not (string= user-mail-address "")))
  1449. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1450. (when (and user-full-name (not (string= user-full-name "")))
  1451. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1452. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1453. "Regexp matched against the \"From:\" header of an email or usenet message.
  1454. It should match if the message is from the user him/herself."
  1455. :group 'org-link-store
  1456. :type 'regexp)
  1457. (defcustom org-context-in-file-links t
  1458. "Non-nil means file links from `org-store-link' contain context.
  1459. A search string will be added to the file name with :: as separator and
  1460. used to find the context when the link is activated by the command
  1461. `org-open-at-point'. When this option is t, the entire active region
  1462. will be placed in the search string of the file link. If set to a
  1463. positive integer, only the first n lines of context will be stored.
  1464. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1465. negates this setting for the duration of the command."
  1466. :group 'org-link-store
  1467. :type '(choice boolean integer))
  1468. (defcustom org-keep-stored-link-after-insertion nil
  1469. "Non-nil means keep link in list for entire session.
  1470. The command `org-store-link' adds a link pointing to the current
  1471. location to an internal list. These links accumulate during a session.
  1472. The command `org-insert-link' can be used to insert links into any
  1473. Org-mode file (offering completion for all stored links). When this
  1474. option is nil, every link which has been inserted once using \\[org-insert-link]
  1475. will be removed from the list, to make completing the unused links
  1476. more efficient."
  1477. :group 'org-link-store
  1478. :type 'boolean)
  1479. (defgroup org-link-follow nil
  1480. "Options concerning following links in Org-mode."
  1481. :tag "Org Follow Link"
  1482. :group 'org-link)
  1483. (defcustom org-link-translation-function nil
  1484. "Function to translate links with different syntax to Org syntax.
  1485. This can be used to translate links created for example by the Planner
  1486. or emacs-wiki packages to Org syntax.
  1487. The function must accept two parameters, a TYPE containing the link
  1488. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1489. which is everything after the link protocol. It should return a cons
  1490. with possibly modified values of type and path.
  1491. Org contains a function for this, so if you set this variable to
  1492. `org-translate-link-from-planner', you should be able follow many
  1493. links created by planner."
  1494. :group 'org-link-follow
  1495. :type '(choice (const nil) (function)))
  1496. (defcustom org-follow-link-hook nil
  1497. "Hook that is run after a link has been followed."
  1498. :group 'org-link-follow
  1499. :type 'hook)
  1500. (defcustom org-tab-follows-link nil
  1501. "Non-nil means on links TAB will follow the link.
  1502. Needs to be set before org.el is loaded.
  1503. This really should not be used, it does not make sense, and the
  1504. implementation is bad."
  1505. :group 'org-link-follow
  1506. :type 'boolean)
  1507. (defcustom org-return-follows-link nil
  1508. "Non-nil means on links RET will follow the link.
  1509. In tables, the special behavior of RET has precedence."
  1510. :group 'org-link-follow
  1511. :type 'boolean)
  1512. (defcustom org-mouse-1-follows-link
  1513. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1514. "Non-nil means mouse-1 on a link will follow the link.
  1515. A longer mouse click will still set point. Does not work on XEmacs.
  1516. Needs to be set before org.el is loaded."
  1517. :group 'org-link-follow
  1518. :type 'boolean)
  1519. (defcustom org-mark-ring-length 4
  1520. "Number of different positions to be recorded in the ring.
  1521. Changing this requires a restart of Emacs to work correctly."
  1522. :group 'org-link-follow
  1523. :type 'integer)
  1524. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1525. "Non-nil means internal links in Org files must exactly match a headline.
  1526. When nil, the link search tries to match a phrase with all words
  1527. in the search text."
  1528. :group 'org-link-follow
  1529. :version "24.1"
  1530. :type '(choice
  1531. (const :tag "Use fuzzy text search" nil)
  1532. (const :tag "Match only exact headline" t)
  1533. (const :tag "Match exact headline or query to create it"
  1534. query-to-create)))
  1535. (defcustom org-link-frame-setup
  1536. '((vm . vm-visit-folder-other-frame)
  1537. (vm-imap . vm-visit-imap-folder-other-frame)
  1538. (gnus . org-gnus-no-new-news)
  1539. (file . find-file-other-window)
  1540. (wl . wl-other-frame))
  1541. "Setup the frame configuration for following links.
  1542. When following a link with Emacs, it may often be useful to display
  1543. this link in another window or frame. This variable can be used to
  1544. set this up for the different types of links.
  1545. For VM, use any of
  1546. `vm-visit-folder'
  1547. `vm-visit-folder-other-window'
  1548. `vm-visit-folder-other-frame'
  1549. For Gnus, use any of
  1550. `gnus'
  1551. `gnus-other-frame'
  1552. `org-gnus-no-new-news'
  1553. For FILE, use any of
  1554. `find-file'
  1555. `find-file-other-window'
  1556. `find-file-other-frame'
  1557. For Wanderlust use any of
  1558. `wl'
  1559. `wl-other-frame'
  1560. For the calendar, use the variable `calendar-setup'.
  1561. For BBDB, it is currently only possible to display the matches in
  1562. another window."
  1563. :group 'org-link-follow
  1564. :type '(list
  1565. (cons (const vm)
  1566. (choice
  1567. (const vm-visit-folder)
  1568. (const vm-visit-folder-other-window)
  1569. (const vm-visit-folder-other-frame)))
  1570. (cons (const vm-imap)
  1571. (choice
  1572. (const vm-visit-imap-folder)
  1573. (const vm-visit-imap-folder-other-window)
  1574. (const vm-visit-imap-folder-other-frame)))
  1575. (cons (const gnus)
  1576. (choice
  1577. (const gnus)
  1578. (const gnus-other-frame)
  1579. (const org-gnus-no-new-news)))
  1580. (cons (const file)
  1581. (choice
  1582. (const find-file)
  1583. (const find-file-other-window)
  1584. (const find-file-other-frame)))
  1585. (cons (const wl)
  1586. (choice
  1587. (const wl)
  1588. (const wl-other-frame)))))
  1589. (defcustom org-display-internal-link-with-indirect-buffer nil
  1590. "Non-nil means use indirect buffer to display infile links.
  1591. Activating internal links (from one location in a file to another location
  1592. in the same file) normally just jumps to the location. When the link is
  1593. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1594. is displayed in
  1595. another window. When this option is set, the other window actually displays
  1596. an indirect buffer clone of the current buffer, to avoid any visibility
  1597. changes to the current buffer."
  1598. :group 'org-link-follow
  1599. :type 'boolean)
  1600. (defcustom org-open-non-existing-files nil
  1601. "Non-nil means `org-open-file' will open non-existing files.
  1602. When nil, an error will be generated.
  1603. This variable applies only to external applications because they
  1604. might choke on non-existing files. If the link is to a file that
  1605. will be opened in Emacs, the variable is ignored."
  1606. :group 'org-link-follow
  1607. :type 'boolean)
  1608. (defcustom org-open-directory-means-index-dot-org nil
  1609. "Non-nil means a link to a directory really means to index.org.
  1610. When nil, following a directory link will run dired or open a finder/explorer
  1611. window on that directory."
  1612. :group 'org-link-follow
  1613. :type 'boolean)
  1614. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1615. "Function and arguments to call for following mailto links.
  1616. This is a list with the first element being a Lisp function, and the
  1617. remaining elements being arguments to the function. In string arguments,
  1618. %a will be replaced by the address, and %s will be replaced by the subject
  1619. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1620. :group 'org-link-follow
  1621. :type '(choice
  1622. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1623. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1624. (const :tag "message-mail" (message-mail "%a" "%s"))
  1625. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1626. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1627. "Non-nil means ask for confirmation before executing shell links.
  1628. Shell links can be dangerous: just think about a link
  1629. [[shell:rm -rf ~/*][Google Search]]
  1630. This link would show up in your Org-mode document as \"Google Search\",
  1631. but really it would remove your entire home directory.
  1632. Therefore we advise against setting this variable to nil.
  1633. Just change it to `y-or-n-p' if you want to confirm with a
  1634. single keystroke rather than having to type \"yes\"."
  1635. :group 'org-link-follow
  1636. :type '(choice
  1637. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1638. (const :tag "with y-or-n (faster)" y-or-n-p)
  1639. (const :tag "no confirmation (dangerous)" nil)))
  1640. (put 'org-confirm-shell-link-function
  1641. 'safe-local-variable
  1642. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1643. (defcustom org-confirm-shell-link-not-regexp ""
  1644. "A regexp to skip confirmation for shell links."
  1645. :group 'org-link-follow
  1646. :version "24.1"
  1647. :type 'regexp)
  1648. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1649. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1650. Elisp links can be dangerous: just think about a link
  1651. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1652. This link would show up in your Org-mode document as \"Google Search\",
  1653. but really it would remove your entire home directory.
  1654. Therefore we advise against setting this variable to nil.
  1655. Just change it to `y-or-n-p' if you want to confirm with a
  1656. single keystroke rather than having to type \"yes\"."
  1657. :group 'org-link-follow
  1658. :type '(choice
  1659. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1660. (const :tag "with y-or-n (faster)" y-or-n-p)
  1661. (const :tag "no confirmation (dangerous)" nil)))
  1662. (put 'org-confirm-shell-link-function
  1663. 'safe-local-variable
  1664. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1665. (defcustom org-confirm-elisp-link-not-regexp ""
  1666. "A regexp to skip confirmation for Elisp links."
  1667. :group 'org-link-follow
  1668. :version "24.1"
  1669. :type 'regexp)
  1670. (defconst org-file-apps-defaults-gnu
  1671. '((remote . emacs)
  1672. (system . mailcap)
  1673. (t . mailcap))
  1674. "Default file applications on a UNIX or GNU/Linux system.
  1675. See `org-file-apps'.")
  1676. (defconst org-file-apps-defaults-macosx
  1677. '((remote . emacs)
  1678. (t . "open %s")
  1679. (system . "open %s")
  1680. ("ps.gz" . "gv %s")
  1681. ("eps.gz" . "gv %s")
  1682. ("dvi" . "xdvi %s")
  1683. ("fig" . "xfig %s"))
  1684. "Default file applications on a MacOS X system.
  1685. The system \"open\" is known as a default, but we use X11 applications
  1686. for some files for which the OS does not have a good default.
  1687. See `org-file-apps'.")
  1688. (defconst org-file-apps-defaults-windowsnt
  1689. (list
  1690. '(remote . emacs)
  1691. (cons t
  1692. (list (if (featurep 'xemacs)
  1693. 'mswindows-shell-execute
  1694. 'w32-shell-execute)
  1695. "open" 'file))
  1696. (cons 'system
  1697. (list (if (featurep 'xemacs)
  1698. 'mswindows-shell-execute
  1699. 'w32-shell-execute)
  1700. "open" 'file)))
  1701. "Default file applications on a Windows NT system.
  1702. The system \"open\" is used for most files.
  1703. See `org-file-apps'.")
  1704. (defcustom org-file-apps
  1705. '((auto-mode . emacs)
  1706. ("\\.mm\\'" . default)
  1707. ("\\.x?html?\\'" . default)
  1708. ("\\.pdf\\'" . default))
  1709. "External applications for opening `file:path' items in a document.
  1710. Org-mode uses system defaults for different file types, but
  1711. you can use this variable to set the application for a given file
  1712. extension. The entries in this list are cons cells where the car identifies
  1713. files and the cdr the corresponding command. Possible values for the
  1714. file identifier are
  1715. \"string\" A string as a file identifier can be interpreted in different
  1716. ways, depending on its contents:
  1717. - Alphanumeric characters only:
  1718. Match links with this file extension.
  1719. Example: (\"pdf\" . \"evince %s\")
  1720. to open PDFs with evince.
  1721. - Regular expression: Match links where the
  1722. filename matches the regexp. If you want to
  1723. use groups here, use shy groups.
  1724. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1725. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1726. to open *.html and *.xhtml with firefox.
  1727. - Regular expression which contains (non-shy) groups:
  1728. Match links where the whole link, including \"::\", and
  1729. anything after that, matches the regexp.
  1730. In a custom command string, %1, %2, etc. are replaced with
  1731. the parts of the link that were matched by the groups.
  1732. For backwards compatibility, if a command string is given
  1733. that does not use any of the group matches, this case is
  1734. handled identically to the second one (i.e. match against
  1735. file name only).
  1736. In a custom lisp form, you can access the group matches with
  1737. (match-string n link).
  1738. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1739. to open [[file:document.pdf::5]] with evince at page 5.
  1740. `directory' Matches a directory
  1741. `remote' Matches a remote file, accessible through tramp or efs.
  1742. Remote files most likely should be visited through Emacs
  1743. because external applications cannot handle such paths.
  1744. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1745. so all files Emacs knows how to handle. Using this with
  1746. command `emacs' will open most files in Emacs. Beware that this
  1747. will also open html files inside Emacs, unless you add
  1748. (\"html\" . default) to the list as well.
  1749. t Default for files not matched by any of the other options.
  1750. `system' The system command to open files, like `open' on Windows
  1751. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1752. that will be selected if you call `C-c C-o' with a double
  1753. \\[universal-argument] \\[universal-argument] prefix.
  1754. Possible values for the command are:
  1755. `emacs' The file will be visited by the current Emacs process.
  1756. `default' Use the default application for this file type, which is the
  1757. association for t in the list, most likely in the system-specific
  1758. part.
  1759. This can be used to overrule an unwanted setting in the
  1760. system-specific variable.
  1761. `system' Use the system command for opening files, like \"open\".
  1762. This command is specified by the entry whose car is `system'.
  1763. Most likely, the system-specific version of this variable
  1764. does define this command, but you can overrule/replace it
  1765. here.
  1766. string A command to be executed by a shell; %s will be replaced
  1767. by the path to the file.
  1768. sexp A Lisp form which will be evaluated. The file path will
  1769. be available in the Lisp variable `file'.
  1770. For more examples, see the system specific constants
  1771. `org-file-apps-defaults-macosx'
  1772. `org-file-apps-defaults-windowsnt'
  1773. `org-file-apps-defaults-gnu'."
  1774. :group 'org-link-follow
  1775. :type '(repeat
  1776. (cons (choice :value ""
  1777. (string :tag "Extension")
  1778. (const :tag "System command to open files" system)
  1779. (const :tag "Default for unrecognized files" t)
  1780. (const :tag "Remote file" remote)
  1781. (const :tag "Links to a directory" directory)
  1782. (const :tag "Any files that have Emacs modes"
  1783. auto-mode))
  1784. (choice :value ""
  1785. (const :tag "Visit with Emacs" emacs)
  1786. (const :tag "Use default" default)
  1787. (const :tag "Use the system command" system)
  1788. (string :tag "Command")
  1789. (sexp :tag "Lisp form")))))
  1790. (defcustom org-doi-server-url "http://dx.doi.org/"
  1791. "The URL of the DOI server."
  1792. :type 'string
  1793. :version "24.3"
  1794. :group 'org-link-follow)
  1795. (defgroup org-refile nil
  1796. "Options concerning refiling entries in Org-mode."
  1797. :tag "Org Refile"
  1798. :group 'org)
  1799. (defcustom org-directory "~/org"
  1800. "Directory with org files.
  1801. This is just a default location to look for Org files. There is no need
  1802. at all to put your files into this directory. It is only used in the
  1803. following situations:
  1804. 1. When a capture template specifies a target file that is not an
  1805. absolute path. The path will then be interpreted relative to
  1806. `org-directory'
  1807. 2. When a capture note is filed away in an interactive way (when exiting the
  1808. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1809. with `org-directory' as the default path."
  1810. :group 'org-refile
  1811. :group 'org-remember
  1812. :group 'org-capture
  1813. :type 'directory)
  1814. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1815. "Default target for storing notes.
  1816. Used as a fall back file for org-remember.el and org-capture.el, for
  1817. templates that do not specify a target file."
  1818. :group 'org-refile
  1819. :group 'org-remember
  1820. :group 'org-capture
  1821. :type '(choice
  1822. (const :tag "Default from remember-data-file" nil)
  1823. file))
  1824. (defcustom org-goto-interface 'outline
  1825. "The default interface to be used for `org-goto'.
  1826. Allowed values are:
  1827. outline The interface shows an outline of the relevant file
  1828. and the correct heading is found by moving through
  1829. the outline or by searching with incremental search.
  1830. outline-path-completion Headlines in the current buffer are offered via
  1831. completion. This is the interface also used by
  1832. the refile command."
  1833. :group 'org-refile
  1834. :type '(choice
  1835. (const :tag "Outline" outline)
  1836. (const :tag "Outline-path-completion" outline-path-completion)))
  1837. (defcustom org-goto-max-level 5
  1838. "Maximum target level when running `org-goto' with refile interface."
  1839. :group 'org-refile
  1840. :type 'integer)
  1841. (defcustom org-reverse-note-order nil
  1842. "Non-nil means store new notes at the beginning of a file or entry.
  1843. When nil, new notes will be filed to the end of a file or entry.
  1844. This can also be a list with cons cells of regular expressions that
  1845. are matched against file names, and values."
  1846. :group 'org-remember
  1847. :group 'org-capture
  1848. :group 'org-refile
  1849. :type '(choice
  1850. (const :tag "Reverse always" t)
  1851. (const :tag "Reverse never" nil)
  1852. (repeat :tag "By file name regexp"
  1853. (cons regexp boolean))))
  1854. (defcustom org-log-refile nil
  1855. "Information to record when a task is refiled.
  1856. Possible values are:
  1857. nil Don't add anything
  1858. time Add a time stamp to the task
  1859. note Prompt for a note and add it with template `org-log-note-headings'
  1860. This option can also be set with on a per-file-basis with
  1861. #+STARTUP: nologrefile
  1862. #+STARTUP: logrefile
  1863. #+STARTUP: lognoterefile
  1864. You can have local logging settings for a subtree by setting the LOGGING
  1865. property to one or more of these keywords.
  1866. When bulk-refiling from the agenda, the value `note' is forbidden and
  1867. will temporarily be changed to `time'."
  1868. :group 'org-refile
  1869. :group 'org-progress
  1870. :version "24.1"
  1871. :type '(choice
  1872. (const :tag "No logging" nil)
  1873. (const :tag "Record timestamp" time)
  1874. (const :tag "Record timestamp with note." note)))
  1875. (defcustom org-refile-targets nil
  1876. "Targets for refiling entries with \\[org-refile].
  1877. This is a list of cons cells. Each cell contains:
  1878. - a specification of the files to be considered, either a list of files,
  1879. or a symbol whose function or variable value will be used to retrieve
  1880. a file name or a list of file names. If you use `org-agenda-files' for
  1881. that, all agenda files will be scanned for targets. Nil means consider
  1882. headings in the current buffer.
  1883. - A specification of how to find candidate refile targets. This may be
  1884. any of:
  1885. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1886. This tag has to be present in all target headlines, inheritance will
  1887. not be considered.
  1888. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1889. todo keyword.
  1890. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1891. headlines that are refiling targets.
  1892. - a cons cell (:level . N). Any headline of level N is considered a target.
  1893. Note that, when `org-odd-levels-only' is set, level corresponds to
  1894. order in hierarchy, not to the number of stars.
  1895. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1896. Note that, when `org-odd-levels-only' is set, level corresponds to
  1897. order in hierarchy, not to the number of stars.
  1898. Each element of this list generates a set of possible targets.
  1899. The union of these sets is presented (with completion) to
  1900. the user by `org-refile'.
  1901. You can set the variable `org-refile-target-verify-function' to a function
  1902. to verify each headline found by the simple criteria above.
  1903. When this variable is nil, all top-level headlines in the current buffer
  1904. are used, equivalent to the value `((nil . (:level . 1))'."
  1905. :group 'org-refile
  1906. :type '(repeat
  1907. (cons
  1908. (choice :value org-agenda-files
  1909. (const :tag "All agenda files" org-agenda-files)
  1910. (const :tag "Current buffer" nil)
  1911. (function) (variable) (file))
  1912. (choice :tag "Identify target headline by"
  1913. (cons :tag "Specific tag" (const :value :tag) (string))
  1914. (cons :tag "TODO keyword" (const :value :todo) (string))
  1915. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1916. (cons :tag "Level number" (const :value :level) (integer))
  1917. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1918. (defcustom org-refile-target-verify-function nil
  1919. "Function to verify if the headline at point should be a refile target.
  1920. The function will be called without arguments, with point at the
  1921. beginning of the headline. It should return t and leave point
  1922. where it is if the headline is a valid target for refiling.
  1923. If the target should not be selected, the function must return nil.
  1924. In addition to this, it may move point to a place from where the search
  1925. should be continued. For example, the function may decide that the entire
  1926. subtree of the current entry should be excluded and move point to the end
  1927. of the subtree."
  1928. :group 'org-refile
  1929. :type '(choice
  1930. (const nil)
  1931. (function)))
  1932. (defcustom org-refile-use-cache nil
  1933. "Non-nil means cache refile targets to speed up the process.
  1934. The cache for a particular file will be updated automatically when
  1935. the buffer has been killed, or when any of the marker used for flagging
  1936. refile targets no longer points at a live buffer.
  1937. If you have added new entries to a buffer that might themselves be targets,
  1938. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1939. find that easier, `C-u C-u C-u C-c C-w'."
  1940. :group 'org-refile
  1941. :version "24.1"
  1942. :type 'boolean)
  1943. (defcustom org-refile-use-outline-path nil
  1944. "Non-nil means provide refile targets as paths.
  1945. So a level 3 headline will be available as level1/level2/level3.
  1946. When the value is `file', also include the file name (without directory)
  1947. into the path. In this case, you can also stop the completion after
  1948. the file name, to get entries inserted as top level in the file.
  1949. When `full-file-path', include the full file path."
  1950. :group 'org-refile
  1951. :type '(choice
  1952. (const :tag "Not" nil)
  1953. (const :tag "Yes" t)
  1954. (const :tag "Start with file name" file)
  1955. (const :tag "Start with full file path" full-file-path)))
  1956. (defcustom org-outline-path-complete-in-steps t
  1957. "Non-nil means complete the outline path in hierarchical steps.
  1958. When Org-mode uses the refile interface to select an outline path
  1959. \(see variable `org-refile-use-outline-path'), the completion of
  1960. the path can be done is a single go, or if can be done in steps down
  1961. the headline hierarchy. Going in steps is probably the best if you
  1962. do not use a special completion package like `ido' or `icicles'.
  1963. However, when using these packages, going in one step can be very
  1964. fast, while still showing the whole path to the entry."
  1965. :group 'org-refile
  1966. :type 'boolean)
  1967. (defcustom org-refile-allow-creating-parent-nodes nil
  1968. "Non-nil means allow to create new nodes as refile targets.
  1969. New nodes are then created by adding \"/new node name\" to the completion
  1970. of an existing node. When the value of this variable is `confirm',
  1971. new node creation must be confirmed by the user (recommended)
  1972. When nil, the completion must match an existing entry.
  1973. Note that, if the new heading is not seen by the criteria
  1974. listed in `org-refile-targets', multiple instances of the same
  1975. heading would be created by trying again to file under the new
  1976. heading."
  1977. :group 'org-refile
  1978. :type '(choice
  1979. (const :tag "Never" nil)
  1980. (const :tag "Always" t)
  1981. (const :tag "Prompt for confirmation" confirm)))
  1982. (defcustom org-refile-active-region-within-subtree nil
  1983. "Non-nil means also refile active region within a subtree.
  1984. By default `org-refile' doesn't allow refiling regions if they
  1985. don't contain a set of subtrees, but it might be convenient to
  1986. do so sometimes: in that case, the first line of the region is
  1987. converted to a headline before refiling."
  1988. :group 'org-refile
  1989. :version "24.1"
  1990. :type 'boolean)
  1991. (defgroup org-todo nil
  1992. "Options concerning TODO items in Org-mode."
  1993. :tag "Org TODO"
  1994. :group 'org)
  1995. (defgroup org-progress nil
  1996. "Options concerning Progress logging in Org-mode."
  1997. :tag "Org Progress"
  1998. :group 'org-time)
  1999. (defvar org-todo-interpretation-widgets
  2000. '((:tag "Sequence (cycling hits every state)" sequence)
  2001. (:tag "Type (cycling directly to DONE)" type))
  2002. "The available interpretation symbols for customizing `org-todo-keywords'.
  2003. Interested libraries should add to this list.")
  2004. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2005. "List of TODO entry keyword sequences and their interpretation.
  2006. \\<org-mode-map>This is a list of sequences.
  2007. Each sequence starts with a symbol, either `sequence' or `type',
  2008. indicating if the keywords should be interpreted as a sequence of
  2009. action steps, or as different types of TODO items. The first
  2010. keywords are states requiring action - these states will select a headline
  2011. for inclusion into the global TODO list Org-mode produces. If one of
  2012. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2013. signify that no further action is necessary. If \"|\" is not found,
  2014. the last keyword is treated as the only DONE state of the sequence.
  2015. The command \\[org-todo] cycles an entry through these states, and one
  2016. additional state where no keyword is present. For details about this
  2017. cycling, see the manual.
  2018. TODO keywords and interpretation can also be set on a per-file basis with
  2019. the special #+SEQ_TODO and #+TYP_TODO lines.
  2020. Each keyword can optionally specify a character for fast state selection
  2021. \(in combination with the variable `org-use-fast-todo-selection')
  2022. and specifiers for state change logging, using the same syntax that
  2023. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2024. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2025. indicates to record a time stamp each time this state is selected.
  2026. Each keyword may also specify if a timestamp or a note should be
  2027. recorded when entering or leaving the state, by adding additional
  2028. characters in the parenthesis after the keyword. This looks like this:
  2029. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2030. record only the time of the state change. With X and Y being either
  2031. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2032. Y when leaving the state if and only if the *target* state does not
  2033. define X. You may omit any of the fast-selection key or X or /Y,
  2034. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2035. For backward compatibility, this variable may also be just a list
  2036. of keywords. In this case the interpretation (sequence or type) will be
  2037. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2038. :group 'org-todo
  2039. :group 'org-keywords
  2040. :type '(choice
  2041. (repeat :tag "Old syntax, just keywords"
  2042. (string :tag "Keyword"))
  2043. (repeat :tag "New syntax"
  2044. (cons
  2045. (choice
  2046. :tag "Interpretation"
  2047. ;;Quick and dirty way to see
  2048. ;;`org-todo-interpretations'. This takes the
  2049. ;;place of item arguments
  2050. :convert-widget
  2051. (lambda (widget)
  2052. (widget-put widget
  2053. :args (mapcar
  2054. #'(lambda (x)
  2055. (widget-convert
  2056. (cons 'const x)))
  2057. org-todo-interpretation-widgets))
  2058. widget))
  2059. (repeat
  2060. (string :tag "Keyword"))))))
  2061. (defvar org-todo-keywords-1 nil
  2062. "All TODO and DONE keywords active in a buffer.")
  2063. (make-variable-buffer-local 'org-todo-keywords-1)
  2064. (defvar org-todo-keywords-for-agenda nil)
  2065. (defvar org-done-keywords-for-agenda nil)
  2066. (defvar org-todo-keyword-alist-for-agenda nil)
  2067. (defvar org-tag-alist-for-agenda nil
  2068. "Alist of all tags from all agenda files.")
  2069. (defvar org-tag-groups-alist-for-agenda nil
  2070. "Alist of all groups tags from all current agenda files.")
  2071. (defvar org-tag-groups-alist nil)
  2072. (make-variable-buffer-local 'org-tag-groups-alist)
  2073. (defvar org-agenda-contributing-files nil)
  2074. (defvar org-not-done-keywords nil)
  2075. (make-variable-buffer-local 'org-not-done-keywords)
  2076. (defvar org-done-keywords nil)
  2077. (make-variable-buffer-local 'org-done-keywords)
  2078. (defvar org-todo-heads nil)
  2079. (make-variable-buffer-local 'org-todo-heads)
  2080. (defvar org-todo-sets nil)
  2081. (make-variable-buffer-local 'org-todo-sets)
  2082. (defvar org-todo-log-states nil)
  2083. (make-variable-buffer-local 'org-todo-log-states)
  2084. (defvar org-todo-kwd-alist nil)
  2085. (make-variable-buffer-local 'org-todo-kwd-alist)
  2086. (defvar org-todo-key-alist nil)
  2087. (make-variable-buffer-local 'org-todo-key-alist)
  2088. (defvar org-todo-key-trigger nil)
  2089. (make-variable-buffer-local 'org-todo-key-trigger)
  2090. (defcustom org-todo-interpretation 'sequence
  2091. "Controls how TODO keywords are interpreted.
  2092. This variable is in principle obsolete and is only used for
  2093. backward compatibility, if the interpretation of todo keywords is
  2094. not given already in `org-todo-keywords'. See that variable for
  2095. more information."
  2096. :group 'org-todo
  2097. :group 'org-keywords
  2098. :type '(choice (const sequence)
  2099. (const type)))
  2100. (defcustom org-use-fast-todo-selection t
  2101. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2102. This variable describes if and under what circumstances the cycling
  2103. mechanism for TODO keywords will be replaced by a single-key, direct
  2104. selection scheme.
  2105. When nil, fast selection is never used.
  2106. When the symbol `prefix', it will be used when `org-todo' is called
  2107. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2108. `C-u t' in an agenda buffer.
  2109. When t, fast selection is used by default. In this case, the prefix
  2110. argument forces cycling instead.
  2111. In all cases, the special interface is only used if access keys have
  2112. actually been assigned by the user, i.e. if keywords in the configuration
  2113. are followed by a letter in parenthesis, like TODO(t)."
  2114. :group 'org-todo
  2115. :type '(choice
  2116. (const :tag "Never" nil)
  2117. (const :tag "By default" t)
  2118. (const :tag "Only with C-u C-c C-t" prefix)))
  2119. (defcustom org-provide-todo-statistics t
  2120. "Non-nil means update todo statistics after insert and toggle.
  2121. ALL-HEADLINES means update todo statistics by including headlines
  2122. with no TODO keyword as well, counting them as not done.
  2123. A list of TODO keywords means the same, but skip keywords that are
  2124. not in this list.
  2125. When this is set, todo statistics is updated in the parent of the
  2126. current entry each time a todo state is changed."
  2127. :group 'org-todo
  2128. :type '(choice
  2129. (const :tag "Yes, only for TODO entries" t)
  2130. (const :tag "Yes, including all entries" 'all-headlines)
  2131. (repeat :tag "Yes, for TODOs in this list"
  2132. (string :tag "TODO keyword"))
  2133. (other :tag "No TODO statistics" nil)))
  2134. (defcustom org-hierarchical-todo-statistics t
  2135. "Non-nil means TODO statistics covers just direct children.
  2136. When nil, all entries in the subtree are considered.
  2137. This has only an effect if `org-provide-todo-statistics' is set.
  2138. To set this to nil for only a single subtree, use a COOKIE_DATA
  2139. property and include the word \"recursive\" into the value."
  2140. :group 'org-todo
  2141. :type 'boolean)
  2142. (defcustom org-after-todo-state-change-hook nil
  2143. "Hook which is run after the state of a TODO item was changed.
  2144. The new state (a string with a TODO keyword, or nil) is available in the
  2145. Lisp variable `org-state'."
  2146. :group 'org-todo
  2147. :type 'hook)
  2148. (defvar org-blocker-hook nil
  2149. "Hook for functions that are allowed to block a state change.
  2150. Functions in this hook should not modify the buffer.
  2151. Each function gets as its single argument a property list,
  2152. see `org-trigger-hook' for more information about this list.
  2153. If any of the functions in this hook returns nil, the state change
  2154. is blocked.")
  2155. (defvar org-trigger-hook nil
  2156. "Hook for functions that are triggered by a state change.
  2157. Each function gets as its single argument a property list with at
  2158. least the following elements:
  2159. (:type type-of-change :position pos-at-entry-start
  2160. :from old-state :to new-state)
  2161. Depending on the type, more properties may be present.
  2162. This mechanism is currently implemented for:
  2163. TODO state changes
  2164. ------------------
  2165. :type todo-state-change
  2166. :from previous state (keyword as a string), or nil, or a symbol
  2167. 'todo' or 'done', to indicate the general type of state.
  2168. :to new state, like in :from")
  2169. (defcustom org-enforce-todo-dependencies nil
  2170. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2171. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2172. be blocked if any prior sibling is not yet done.
  2173. Finally, if the parent is blocked because of ordered siblings of its own,
  2174. the child will also be blocked."
  2175. :set (lambda (var val)
  2176. (set var val)
  2177. (if val
  2178. (add-hook 'org-blocker-hook
  2179. 'org-block-todo-from-children-or-siblings-or-parent)
  2180. (remove-hook 'org-blocker-hook
  2181. 'org-block-todo-from-children-or-siblings-or-parent)))
  2182. :group 'org-todo
  2183. :type 'boolean)
  2184. (defcustom org-enforce-todo-checkbox-dependencies nil
  2185. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2186. When this is nil, checkboxes have no influence on switching TODO states.
  2187. When non-nil, you first need to check off all check boxes before the TODO
  2188. entry can be switched to DONE.
  2189. This variable needs to be set before org.el is loaded, and you need to
  2190. restart Emacs after a change to make the change effective. The only way
  2191. to change is while Emacs is running is through the customize interface."
  2192. :set (lambda (var val)
  2193. (set var val)
  2194. (if val
  2195. (add-hook 'org-blocker-hook
  2196. 'org-block-todo-from-checkboxes)
  2197. (remove-hook 'org-blocker-hook
  2198. 'org-block-todo-from-checkboxes)))
  2199. :group 'org-todo
  2200. :type 'boolean)
  2201. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2202. "Non-nil means inserting a TODO heading is treated as state change.
  2203. So when the command \\[org-insert-todo-heading] is used, state change
  2204. logging will apply if appropriate. When nil, the new TODO item will
  2205. be inserted directly, and no logging will take place."
  2206. :group 'org-todo
  2207. :type 'boolean)
  2208. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2209. "Non-nil means switching TODO states with S-cursor counts as state change.
  2210. This is the default behavior. However, setting this to nil allows a
  2211. convenient way to select a TODO state and bypass any logging associated
  2212. with that."
  2213. :group 'org-todo
  2214. :type 'boolean)
  2215. (defcustom org-todo-state-tags-triggers nil
  2216. "Tag changes that should be triggered by TODO state changes.
  2217. This is a list. Each entry is
  2218. (state-change (tag . flag) .......)
  2219. State-change can be a string with a state, and empty string to indicate the
  2220. state that has no TODO keyword, or it can be one of the symbols `todo'
  2221. or `done', meaning any not-done or done state, respectively."
  2222. :group 'org-todo
  2223. :group 'org-tags
  2224. :type '(repeat
  2225. (cons (choice :tag "When changing to"
  2226. (const :tag "Not-done state" todo)
  2227. (const :tag "Done state" done)
  2228. (string :tag "State"))
  2229. (repeat
  2230. (cons :tag "Tag action"
  2231. (string :tag "Tag")
  2232. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2233. (defcustom org-log-done nil
  2234. "Information to record when a task moves to the DONE state.
  2235. Possible values are:
  2236. nil Don't add anything, just change the keyword
  2237. time Add a time stamp to the task
  2238. note Prompt for a note and add it with template `org-log-note-headings'
  2239. This option can also be set with on a per-file-basis with
  2240. #+STARTUP: nologdone
  2241. #+STARTUP: logdone
  2242. #+STARTUP: lognotedone
  2243. You can have local logging settings for a subtree by setting the LOGGING
  2244. property to one or more of these keywords."
  2245. :group 'org-todo
  2246. :group 'org-progress
  2247. :type '(choice
  2248. (const :tag "No logging" nil)
  2249. (const :tag "Record CLOSED timestamp" time)
  2250. (const :tag "Record CLOSED timestamp with note." note)))
  2251. ;; Normalize old uses of org-log-done.
  2252. (cond
  2253. ((eq org-log-done t) (setq org-log-done 'time))
  2254. ((and (listp org-log-done) (memq 'done org-log-done))
  2255. (setq org-log-done 'note)))
  2256. (defcustom org-log-reschedule nil
  2257. "Information to record when the scheduling date of a tasks is modified.
  2258. Possible values are:
  2259. nil Don't add anything, just change the date
  2260. time Add a time stamp to the task
  2261. note Prompt for a note and add it with template `org-log-note-headings'
  2262. This option can also be set with on a per-file-basis with
  2263. #+STARTUP: nologreschedule
  2264. #+STARTUP: logreschedule
  2265. #+STARTUP: lognotereschedule"
  2266. :group 'org-todo
  2267. :group 'org-progress
  2268. :type '(choice
  2269. (const :tag "No logging" nil)
  2270. (const :tag "Record timestamp" time)
  2271. (const :tag "Record timestamp with note." note)))
  2272. (defcustom org-log-redeadline nil
  2273. "Information to record when the deadline date of a tasks is modified.
  2274. Possible values are:
  2275. nil Don't add anything, just change the date
  2276. time Add a time stamp to the task
  2277. note Prompt for a note and add it with template `org-log-note-headings'
  2278. This option can also be set with on a per-file-basis with
  2279. #+STARTUP: nologredeadline
  2280. #+STARTUP: logredeadline
  2281. #+STARTUP: lognoteredeadline
  2282. You can have local logging settings for a subtree by setting the LOGGING
  2283. property to one or more of these keywords."
  2284. :group 'org-todo
  2285. :group 'org-progress
  2286. :type '(choice
  2287. (const :tag "No logging" nil)
  2288. (const :tag "Record timestamp" time)
  2289. (const :tag "Record timestamp with note." note)))
  2290. (defcustom org-log-note-clock-out nil
  2291. "Non-nil means record a note when clocking out of an item.
  2292. This can also be configured on a per-file basis by adding one of
  2293. the following lines anywhere in the buffer:
  2294. #+STARTUP: lognoteclock-out
  2295. #+STARTUP: nolognoteclock-out"
  2296. :group 'org-todo
  2297. :group 'org-progress
  2298. :type 'boolean)
  2299. (defcustom org-log-done-with-time t
  2300. "Non-nil means the CLOSED time stamp will contain date and time.
  2301. When nil, only the date will be recorded."
  2302. :group 'org-progress
  2303. :type 'boolean)
  2304. (defcustom org-log-note-headings
  2305. '((done . "CLOSING NOTE %t")
  2306. (state . "State %-12s from %-12S %t")
  2307. (note . "Note taken on %t")
  2308. (reschedule . "Rescheduled from %S on %t")
  2309. (delschedule . "Not scheduled, was %S on %t")
  2310. (redeadline . "New deadline from %S on %t")
  2311. (deldeadline . "Removed deadline, was %S on %t")
  2312. (refile . "Refiled on %t")
  2313. (clock-out . ""))
  2314. "Headings for notes added to entries.
  2315. The value is an alist, with the car being a symbol indicating the note
  2316. context, and the cdr is the heading to be used. The heading may also be the
  2317. empty string.
  2318. %t in the heading will be replaced by a time stamp.
  2319. %T will be an active time stamp instead the default inactive one
  2320. %d will be replaced by a short-format time stamp.
  2321. %D will be replaced by an active short-format time stamp.
  2322. %s will be replaced by the new TODO state, in double quotes.
  2323. %S will be replaced by the old TODO state, in double quotes.
  2324. %u will be replaced by the user name.
  2325. %U will be replaced by the full user name.
  2326. In fact, it is not a good idea to change the `state' entry, because
  2327. agenda log mode depends on the format of these entries."
  2328. :group 'org-todo
  2329. :group 'org-progress
  2330. :type '(list :greedy t
  2331. (cons (const :tag "Heading when closing an item" done) string)
  2332. (cons (const :tag
  2333. "Heading when changing todo state (todo sequence only)"
  2334. state) string)
  2335. (cons (const :tag "Heading when just taking a note" note) string)
  2336. (cons (const :tag "Heading when clocking out" clock-out) string)
  2337. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2338. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2339. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2340. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2341. (cons (const :tag "Heading when refiling" refile) string)))
  2342. (unless (assq 'note org-log-note-headings)
  2343. (push '(note . "%t") org-log-note-headings))
  2344. (defcustom org-log-into-drawer nil
  2345. "Non-nil means insert state change notes and time stamps into a drawer.
  2346. When nil, state changes notes will be inserted after the headline and
  2347. any scheduling and clock lines, but not inside a drawer.
  2348. The value of this variable should be the name of the drawer to use.
  2349. LOGBOOK is proposed as the default drawer for this purpose, you can
  2350. also set this to a string to define the drawer of your choice.
  2351. A value of t is also allowed, representing \"LOGBOOK\".
  2352. A value of t or nil can also be set with on a per-file-basis with
  2353. #+STARTUP: logdrawer
  2354. #+STARTUP: nologdrawer
  2355. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2356. will be ignored.
  2357. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2358. a subtree."
  2359. :group 'org-todo
  2360. :group 'org-progress
  2361. :type '(choice
  2362. (const :tag "Not into a drawer" nil)
  2363. (const :tag "LOGBOOK" t)
  2364. (string :tag "Other")))
  2365. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2366. (defun org-log-into-drawer ()
  2367. "Return the value of `org-log-into-drawer', but let properties overrule.
  2368. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2369. used instead of the default value."
  2370. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2371. (cond
  2372. ((not p) org-log-into-drawer)
  2373. ((equal p "nil") nil)
  2374. ((equal p "t") "LOGBOOK")
  2375. (t p))))
  2376. (defcustom org-log-state-notes-insert-after-drawers nil
  2377. "Non-nil means insert state change notes after any drawers in entry.
  2378. Only the drawers that *immediately* follow the headline and the
  2379. deadline/scheduled line are skipped.
  2380. When nil, insert notes right after the heading and perhaps the line
  2381. with deadline/scheduling if present.
  2382. This variable will have no effect if `org-log-into-drawer' is
  2383. set."
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-log-states-order-reversed t
  2388. "Non-nil means the latest state note will be directly after heading.
  2389. When nil, the state change notes will be ordered according to time.
  2390. This option can also be set with on a per-file-basis with
  2391. #+STARTUP: logstatesreversed
  2392. #+STARTUP: nologstatesreversed"
  2393. :group 'org-todo
  2394. :group 'org-progress
  2395. :type 'boolean)
  2396. (defcustom org-todo-repeat-to-state nil
  2397. "The TODO state to which a repeater should return the repeating task.
  2398. By default this is the first task in a TODO sequence, or the previous state
  2399. in a TODO_TYP set. But you can specify another task here.
  2400. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2401. :group 'org-todo
  2402. :version "24.1"
  2403. :type '(choice (const :tag "Head of sequence" nil)
  2404. (string :tag "Specific state")))
  2405. (defcustom org-log-repeat 'time
  2406. "Non-nil means record moving through the DONE state when triggering repeat.
  2407. An auto-repeating task is immediately switched back to TODO when
  2408. marked DONE. If you are not logging state changes (by adding \"@\"
  2409. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2410. record a closing note, there will be no record of the task moving
  2411. through DONE. This variable forces taking a note anyway.
  2412. nil Don't force a record
  2413. time Record a time stamp
  2414. note Prompt for a note and add it with template `org-log-note-headings'
  2415. This option can also be set with on a per-file-basis with
  2416. #+STARTUP: nologrepeat
  2417. #+STARTUP: logrepeat
  2418. #+STARTUP: lognoterepeat
  2419. You can have local logging settings for a subtree by setting the LOGGING
  2420. property to one or more of these keywords."
  2421. :group 'org-todo
  2422. :group 'org-progress
  2423. :type '(choice
  2424. (const :tag "Don't force a record" nil)
  2425. (const :tag "Force recording the DONE state" time)
  2426. (const :tag "Force recording a note with the DONE state" note)))
  2427. (defgroup org-priorities nil
  2428. "Priorities in Org-mode."
  2429. :tag "Org Priorities"
  2430. :group 'org-todo)
  2431. (defcustom org-enable-priority-commands t
  2432. "Non-nil means priority commands are active.
  2433. When nil, these commands will be disabled, so that you never accidentally
  2434. set a priority."
  2435. :group 'org-priorities
  2436. :type 'boolean)
  2437. (defcustom org-highest-priority ?A
  2438. "The highest priority of TODO items. A character like ?A, ?B etc.
  2439. Must have a smaller ASCII number than `org-lowest-priority'."
  2440. :group 'org-priorities
  2441. :type 'character)
  2442. (defcustom org-lowest-priority ?C
  2443. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2444. Must have a larger ASCII number than `org-highest-priority'."
  2445. :group 'org-priorities
  2446. :type 'character)
  2447. (defcustom org-default-priority ?B
  2448. "The default priority of TODO items.
  2449. This is the priority an item gets if no explicit priority is given.
  2450. When starting to cycle on an empty priority the first step in the cycle
  2451. depends on `org-priority-start-cycle-with-default'. The resulting first
  2452. step priority must not exceed the range from `org-highest-priority' to
  2453. `org-lowest-priority' which means that `org-default-priority' has to be
  2454. in this range exclusive or inclusive the range boundaries. Else the
  2455. first step refuses to set the default and the second will fall back
  2456. to (depending on the command used) the highest or lowest priority."
  2457. :group 'org-priorities
  2458. :type 'character)
  2459. (defcustom org-priority-start-cycle-with-default t
  2460. "Non-nil means start with default priority when starting to cycle.
  2461. When this is nil, the first step in the cycle will be (depending on the
  2462. command used) one higher or lower than the default priority.
  2463. See also `org-default-priority'."
  2464. :group 'org-priorities
  2465. :type 'boolean)
  2466. (defcustom org-get-priority-function nil
  2467. "Function to extract the priority from a string.
  2468. The string is normally the headline. If this is nil Org computes the
  2469. priority from the priority cookie like [#A] in the headline. It returns
  2470. an integer, increasing by 1000 for each priority level.
  2471. The user can set a different function here, which should take a string
  2472. as an argument and return the numeric priority."
  2473. :group 'org-priorities
  2474. :version "24.1"
  2475. :type '(choice
  2476. (const nil)
  2477. (function)))
  2478. (defgroup org-time nil
  2479. "Options concerning time stamps and deadlines in Org-mode."
  2480. :tag "Org Time"
  2481. :group 'org)
  2482. (defcustom org-insert-labeled-timestamps-at-point nil
  2483. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2484. When nil, these labeled time stamps are forces into the second line of an
  2485. entry, just after the headline. When scheduling from the global TODO list,
  2486. the time stamp will always be forced into the second line."
  2487. :group 'org-time
  2488. :type 'boolean)
  2489. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2490. "Formats for `format-time-string' which are used for time stamps.
  2491. It is not recommended to change this constant.")
  2492. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2493. "Number of minutes to round time stamps to.
  2494. These are two values, the first applies when first creating a time stamp.
  2495. The second applies when changing it with the commands `S-up' and `S-down'.
  2496. When changing the time stamp, this means that it will change in steps
  2497. of N minutes, as given by the second value.
  2498. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2499. numbers should be factors of 60, so for example 5, 10, 15.
  2500. When this is larger than 1, you can still force an exact time stamp by using
  2501. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2502. and by using a prefix arg to `S-up/down' to specify the exact number
  2503. of minutes to shift."
  2504. :group 'org-time
  2505. :get #'(lambda (var) ; Make sure both elements are there
  2506. (if (integerp (default-value var))
  2507. (list (default-value var) 5)
  2508. (default-value var)))
  2509. :type '(list
  2510. (integer :tag "when inserting times")
  2511. (integer :tag "when modifying times")))
  2512. ;; Normalize old customizations of this variable.
  2513. (when (integerp org-time-stamp-rounding-minutes)
  2514. (setq org-time-stamp-rounding-minutes
  2515. (list org-time-stamp-rounding-minutes
  2516. org-time-stamp-rounding-minutes)))
  2517. (defcustom org-display-custom-times nil
  2518. "Non-nil means overlay custom formats over all time stamps.
  2519. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2520. To turn this on on a per-file basis, insert anywhere in the file:
  2521. #+STARTUP: customtime"
  2522. :group 'org-time
  2523. :set 'set-default
  2524. :type 'sexp)
  2525. (make-variable-buffer-local 'org-display-custom-times)
  2526. (defcustom org-time-stamp-custom-formats
  2527. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2528. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2529. These are overlaid over the default ISO format if the variable
  2530. `org-display-custom-times' is set. Time like %H:%M should be at the
  2531. end of the second format. The custom formats are also honored by export
  2532. commands, if custom time display is turned on at the time of export."
  2533. :group 'org-time
  2534. :type 'sexp)
  2535. (defun org-time-stamp-format (&optional long inactive)
  2536. "Get the right format for a time string."
  2537. (let ((f (if long (cdr org-time-stamp-formats)
  2538. (car org-time-stamp-formats))))
  2539. (if inactive
  2540. (concat "[" (substring f 1 -1) "]")
  2541. f)))
  2542. (defcustom org-time-clocksum-format
  2543. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2544. "The format string used when creating CLOCKSUM lines.
  2545. This is also used when Org mode generates a time duration.
  2546. The value can be a single format string containing two
  2547. %-sequences, which will be filled with the number of hours and
  2548. minutes in that order.
  2549. Alternatively, the value can be a plist associating any of the
  2550. keys :years, :months, :weeks, :days, :hours or :minutes with
  2551. format strings. The time duration is formatted using only the
  2552. time components that are needed and concatenating the results.
  2553. If a time unit in absent, it falls back to the next smallest
  2554. unit.
  2555. The keys :require-years, :require-months, :require-days,
  2556. :require-weeks, :require-hours, :require-minutes are also
  2557. meaningful. A non-nil value for these keys indicates that the
  2558. corresponding time component should always be included, even if
  2559. its value is 0.
  2560. For example,
  2561. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2562. :require-minutes t)
  2563. means durations longer than a day will be expressed in days,
  2564. hours and minutes, and durations less than a day will always be
  2565. expressed in hours and minutes (even for durations less than an
  2566. hour).
  2567. The value
  2568. \(:days \"%dd\" :minutes \"%dm\")
  2569. means durations longer than a day will be expressed in days and
  2570. minutes, and durations less than a day will be expressed entirely
  2571. in minutes (even for durations longer than an hour)."
  2572. :group 'org-time
  2573. :group 'org-clock
  2574. :version "24.4"
  2575. :package-version '(Org . "8.0")
  2576. :type '(choice (string :tag "Format string")
  2577. (set :tag "Plist"
  2578. (group :inline t (const :tag "Years" :years)
  2579. (string :tag "Format string"))
  2580. (group :inline t
  2581. (const :tag "Always show years" :require-years)
  2582. (const t))
  2583. (group :inline t (const :tag "Months" :months)
  2584. (string :tag "Format string"))
  2585. (group :inline t
  2586. (const :tag "Always show months" :require-months)
  2587. (const t))
  2588. (group :inline t (const :tag "Weeks" :weeks)
  2589. (string :tag "Format string"))
  2590. (group :inline t
  2591. (const :tag "Always show weeks" :require-weeks)
  2592. (const t))
  2593. (group :inline t (const :tag "Days" :days)
  2594. (string :tag "Format string"))
  2595. (group :inline t
  2596. (const :tag "Always show days" :require-days)
  2597. (const t))
  2598. (group :inline t (const :tag "Hours" :hours)
  2599. (string :tag "Format string"))
  2600. (group :inline t
  2601. (const :tag "Always show hours" :require-hours)
  2602. (const t))
  2603. (group :inline t (const :tag "Minutes" :minutes)
  2604. (string :tag "Format string"))
  2605. (group :inline t
  2606. (const :tag "Always show minutes" :require-minutes)
  2607. (const t)))))
  2608. (defcustom org-time-clocksum-use-fractional nil
  2609. "When non-nil, \\[org-clock-display] uses fractional times.
  2610. See `org-time-clocksum-format' for more on time clock formats."
  2611. :group 'org-time
  2612. :group 'org-clock
  2613. :version "24.3"
  2614. :type 'boolean)
  2615. (defcustom org-time-clocksum-use-effort-durations nil
  2616. "When non-nil, \\[org-clock-display] uses effort durations.
  2617. E.g. by default, one day is considered to be a 8 hours effort,
  2618. so a task that has been clocked for 16 hours will be displayed
  2619. as during 2 days in the clock display or in the clocktable.
  2620. See `org-effort-durations' on how to set effort durations
  2621. and `org-time-clocksum-format' for more on time clock formats."
  2622. :group 'org-time
  2623. :group 'org-clock
  2624. :version "24.4"
  2625. :package-version '(Org . "8.0")
  2626. :type 'boolean)
  2627. (defcustom org-time-clocksum-fractional-format "%.2f"
  2628. "The format string used when creating CLOCKSUM lines,
  2629. or when Org mode generates a time duration, if
  2630. `org-time-clocksum-use-fractional' is enabled.
  2631. The value can be a single format string containing one
  2632. %-sequence, which will be filled with the number of hours as
  2633. a float.
  2634. Alternatively, the value can be a plist associating any of the
  2635. keys :years, :months, :weeks, :days, :hours or :minutes with
  2636. a format string. The time duration is formatted using the
  2637. largest time unit which gives a non-zero integer part. If all
  2638. specified formats have zero integer part, the smallest time unit
  2639. is used."
  2640. :group 'org-time
  2641. :type '(choice (string :tag "Format string")
  2642. (set (group :inline t (const :tag "Years" :years)
  2643. (string :tag "Format string"))
  2644. (group :inline t (const :tag "Months" :months)
  2645. (string :tag "Format string"))
  2646. (group :inline t (const :tag "Weeks" :weeks)
  2647. (string :tag "Format string"))
  2648. (group :inline t (const :tag "Days" :days)
  2649. (string :tag "Format string"))
  2650. (group :inline t (const :tag "Hours" :hours)
  2651. (string :tag "Format string"))
  2652. (group :inline t (const :tag "Minutes" :minutes)
  2653. (string :tag "Format string")))))
  2654. (defcustom org-deadline-warning-days 14
  2655. "Number of days before expiration during which a deadline becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. When 0 or negative, it means use this number (the absolute value of it)
  2658. even if a deadline has a different individual lead time specified.
  2659. Custom commands can set this variable in the options section."
  2660. :group 'org-time
  2661. :group 'org-agenda-daily/weekly
  2662. :type 'integer)
  2663. (defcustom org-scheduled-delay-days 0
  2664. "Number of days before a scheduled item becomes active.
  2665. This variable governs the display in sparse trees and in the agenda.
  2666. The default value (i.e. 0) means: don't delay scheduled item.
  2667. When negative, it means use this number (the absolute value of it)
  2668. even if a scheduled item has a different individual delay time
  2669. specified.
  2670. Custom commands can set this variable in the options section."
  2671. :group 'org-time
  2672. :group 'org-agenda-daily/weekly
  2673. :version "24.4"
  2674. :package-version '(Org . "8.0")
  2675. :type 'integer)
  2676. (defcustom org-read-date-prefer-future t
  2677. "Non-nil means assume future for incomplete date input from user.
  2678. This affects the following situations:
  2679. 1. The user gives a month but not a year.
  2680. For example, if it is April and you enter \"feb 2\", this will be read
  2681. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2682. 2. The user gives a day, but no month.
  2683. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2684. read this as the third of *next* month. However, if you enter \"17\",
  2685. it will be considered as *this* month.
  2686. If you set this variable to the symbol `time', then also the following
  2687. will work:
  2688. 3. If the user gives a time.
  2689. If the time is before now, it will be interpreted as tomorrow.
  2690. Currently none of this works for ISO week specifications.
  2691. When this option is nil, the current day, month and year will always be
  2692. used as defaults.
  2693. See also `org-agenda-jump-prefer-future'."
  2694. :group 'org-time
  2695. :type '(choice
  2696. (const :tag "Never" nil)
  2697. (const :tag "Check month and day" t)
  2698. (const :tag "Check month, day, and time" time)))
  2699. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2700. "Should the agenda jump command prefer the future for incomplete dates?
  2701. The default is to do the same as configured in `org-read-date-prefer-future'.
  2702. But you can also set a deviating value here.
  2703. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2704. :group 'org-agenda
  2705. :group 'org-time
  2706. :version "24.1"
  2707. :type '(choice
  2708. (const :tag "Use org-read-date-prefer-future"
  2709. org-read-date-prefer-future)
  2710. (const :tag "Never" nil)
  2711. (const :tag "Always" t)))
  2712. (defcustom org-read-date-force-compatible-dates t
  2713. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2714. Depending on the system Emacs is running on, certain dates cannot
  2715. be represented with the type used internally to represent time.
  2716. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2717. correctly. Some systems allow for earlier dates, some for later,
  2718. some for both. One way to find out it to insert any date into an
  2719. Org buffer, putting the cursor on the year and hitting S-up and
  2720. S-down to test the range.
  2721. When this variable is set to t, the date/time prompt will not let
  2722. you specify dates outside the 1970-2037 range, so it is certain that
  2723. these dates will work in whatever version of Emacs you are
  2724. running, and also that you can move a file from one Emacs implementation
  2725. to another. WHenever Org is forcing the year for you, it will display
  2726. a message and beep.
  2727. When this variable is nil, Org will check if the date is
  2728. representable in the specific Emacs implementation you are using.
  2729. If not, it will force a year, usually the current year, and beep
  2730. to remind you. Currently this setting is not recommended because
  2731. the likelihood that you will open your Org files in an Emacs that
  2732. has limited date range is not negligible.
  2733. A workaround for this problem is to use diary sexp dates for time
  2734. stamps outside of this range."
  2735. :group 'org-time
  2736. :version "24.1"
  2737. :type 'boolean)
  2738. (defcustom org-read-date-display-live t
  2739. "Non-nil means display current interpretation of date prompt live.
  2740. This display will be in an overlay, in the minibuffer."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defcustom org-read-date-popup-calendar t
  2744. "Non-nil means pop up a calendar when prompting for a date.
  2745. In the calendar, the date can be selected with mouse-1. However, the
  2746. minibuffer will also be active, and you can simply enter the date as well.
  2747. When nil, only the minibuffer will be available."
  2748. :group 'org-time
  2749. :type 'boolean)
  2750. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2751. 'org-read-date-popup-calendar)
  2752. (make-obsolete-variable
  2753. 'org-read-date-minibuffer-setup-hook
  2754. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2755. (defcustom org-read-date-minibuffer-setup-hook nil
  2756. "Hook to be used to set up keys for the date/time interface.
  2757. Add key definitions to `minibuffer-local-map', which will be a
  2758. temporary copy.
  2759. WARNING: This option is obsolete, you should use
  2760. `org-read-date-minibuffer-local-map' to set up keys."
  2761. :group 'org-time
  2762. :type 'hook)
  2763. (defcustom org-extend-today-until 0
  2764. "The hour when your day really ends. Must be an integer.
  2765. This has influence for the following applications:
  2766. - When switching the agenda to \"today\". It it is still earlier than
  2767. the time given here, the day recognized as TODAY is actually yesterday.
  2768. - When a date is read from the user and it is still before the time given
  2769. here, the current date and time will be assumed to be yesterday, 23:59.
  2770. Also, timestamps inserted in capture templates follow this rule.
  2771. IMPORTANT: This is a feature whose implementation is and likely will
  2772. remain incomplete. Really, it is only here because past midnight seems to
  2773. be the favorite working time of John Wiegley :-)"
  2774. :group 'org-time
  2775. :type 'integer)
  2776. (defcustom org-use-effective-time nil
  2777. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2778. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2779. \"effective time\" of any timestamps between midnight and 8am will be
  2780. 23:59 of the previous day."
  2781. :group 'org-time
  2782. :version "24.1"
  2783. :type 'boolean)
  2784. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2785. "When non-nil, use the last clock out time for `org-todo'.
  2786. Note that this option has precedence over the combined use of
  2787. `org-use-effective-time' and `org-extend-today-until'."
  2788. :group 'org-time
  2789. :version "24.4"
  2790. :package-version '(Org . "8.0")
  2791. :type 'boolean)
  2792. (defcustom org-edit-timestamp-down-means-later nil
  2793. "Non-nil means S-down will increase the time in a time stamp.
  2794. When nil, S-up will increase."
  2795. :group 'org-time
  2796. :type 'boolean)
  2797. (defcustom org-calendar-follow-timestamp-change t
  2798. "Non-nil means make the calendar window follow timestamp changes.
  2799. When a timestamp is modified and the calendar window is visible, it will be
  2800. moved to the new date."
  2801. :group 'org-time
  2802. :type 'boolean)
  2803. (defgroup org-tags nil
  2804. "Options concerning tags in Org-mode."
  2805. :tag "Org Tags"
  2806. :group 'org)
  2807. (defcustom org-tag-alist nil
  2808. "List of tags allowed in Org-mode files.
  2809. When this list is nil, Org-mode will base TAG input on what is already in the
  2810. buffer.
  2811. The value of this variable is an alist, the car of each entry must be a
  2812. keyword as a string, the cdr may be a character that is used to select
  2813. that tag through the fast-tag-selection interface.
  2814. See the manual for details."
  2815. :group 'org-tags
  2816. :type '(repeat
  2817. (choice
  2818. (cons (string :tag "Tag name")
  2819. (character :tag "Access char"))
  2820. (list :tag "Start radio group"
  2821. (const :startgroup)
  2822. (option (string :tag "Group description")))
  2823. (list :tag "Group tags delimiter"
  2824. (const :grouptags))
  2825. (list :tag "End radio group"
  2826. (const :endgroup)
  2827. (option (string :tag "Group description")))
  2828. (const :tag "New line" (:newline)))))
  2829. (defcustom org-tag-persistent-alist nil
  2830. "List of tags that will always appear in all Org-mode files.
  2831. This is in addition to any in buffer settings or customizations
  2832. of `org-tag-alist'.
  2833. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2834. The value of this variable is an alist, the car of each entry must be a
  2835. keyword as a string, the cdr may be a character that is used to select
  2836. that tag through the fast-tag-selection interface.
  2837. See the manual for details.
  2838. To disable these tags on a per-file basis, insert anywhere in the file:
  2839. #+STARTUP: noptag"
  2840. :group 'org-tags
  2841. :type '(repeat
  2842. (choice
  2843. (cons (string :tag "Tag name")
  2844. (character :tag "Access char"))
  2845. (const :tag "Start radio group" (:startgroup))
  2846. (const :tag "Group tags delimiter" (:grouptags))
  2847. (const :tag "End radio group" (:endgroup))
  2848. (const :tag "New line" (:newline)))))
  2849. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2850. "If non-nil, always offer completion for all tags of all agenda files.
  2851. Instead of customizing this variable directly, you might want to
  2852. set it locally for capture buffers, because there no list of
  2853. tags in that file can be created dynamically (there are none).
  2854. (add-hook 'org-capture-mode-hook
  2855. (lambda ()
  2856. (set (make-local-variable
  2857. 'org-complete-tags-always-offer-all-agenda-tags)
  2858. t)))"
  2859. :group 'org-tags
  2860. :version "24.1"
  2861. :type 'boolean)
  2862. (defvar org-file-tags nil
  2863. "List of tags that can be inherited by all entries in the file.
  2864. The tags will be inherited if the variable `org-use-tag-inheritance'
  2865. says they should be.
  2866. This variable is populated from #+FILETAGS lines.")
  2867. (defcustom org-use-fast-tag-selection 'auto
  2868. "Non-nil means use fast tag selection scheme.
  2869. This is a special interface to select and deselect tags with single keys.
  2870. When nil, fast selection is never used.
  2871. When the symbol `auto', fast selection is used if and only if selection
  2872. characters for tags have been configured, either through the variable
  2873. `org-tag-alist' or through a #+TAGS line in the buffer.
  2874. When t, fast selection is always used and selection keys are assigned
  2875. automatically if necessary."
  2876. :group 'org-tags
  2877. :type '(choice
  2878. (const :tag "Always" t)
  2879. (const :tag "Never" nil)
  2880. (const :tag "When selection characters are configured" auto)))
  2881. (defcustom org-fast-tag-selection-single-key nil
  2882. "Non-nil means fast tag selection exits after first change.
  2883. When nil, you have to press RET to exit it.
  2884. During fast tag selection, you can toggle this flag with `C-c'.
  2885. This variable can also have the value `expert'. In this case, the window
  2886. displaying the tags menu is not even shown, until you press C-c again."
  2887. :group 'org-tags
  2888. :type '(choice
  2889. (const :tag "No" nil)
  2890. (const :tag "Yes" t)
  2891. (const :tag "Expert" expert)))
  2892. (defvar org-fast-tag-selection-include-todo nil
  2893. "Non-nil means fast tags selection interface will also offer TODO states.
  2894. This is an undocumented feature, you should not rely on it.")
  2895. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2896. "The column to which tags should be indented in a headline.
  2897. If this number is positive, it specifies the column. If it is negative,
  2898. it means that the tags should be flushright to that column. For example,
  2899. -80 works well for a normal 80 character screen.
  2900. When 0, place tags directly after headline text, with only one space in
  2901. between."
  2902. :group 'org-tags
  2903. :type 'integer)
  2904. (defcustom org-auto-align-tags t
  2905. "Non-nil keeps tags aligned when modifying headlines.
  2906. Some operations (i.e. demoting) change the length of a headline and
  2907. therefore shift the tags around. With this option turned on, after
  2908. each such operation the tags are again aligned to `org-tags-column'."
  2909. :group 'org-tags
  2910. :type 'boolean)
  2911. (defcustom org-use-tag-inheritance t
  2912. "Non-nil means tags in levels apply also for sublevels.
  2913. When nil, only the tags directly given in a specific line apply there.
  2914. This may also be a list of tags that should be inherited, or a regexp that
  2915. matches tags that should be inherited. Additional control is possible
  2916. with the variable `org-tags-exclude-from-inheritance' which gives an
  2917. explicit list of tags to be excluded from inheritance, even if the value of
  2918. `org-use-tag-inheritance' would select it for inheritance.
  2919. If this option is t, a match early-on in a tree can lead to a large
  2920. number of matches in the subtree when constructing the agenda or creating
  2921. a sparse tree. If you only want to see the first match in a tree during
  2922. a search, check out the variable `org-tags-match-list-sublevels'."
  2923. :group 'org-tags
  2924. :type '(choice
  2925. (const :tag "Not" nil)
  2926. (const :tag "Always" t)
  2927. (repeat :tag "Specific tags" (string :tag "Tag"))
  2928. (regexp :tag "Tags matched by regexp")))
  2929. (defcustom org-tags-exclude-from-inheritance nil
  2930. "List of tags that should never be inherited.
  2931. This is a way to exclude a few tags from inheritance. For way to do
  2932. the opposite, to actively allow inheritance for selected tags,
  2933. see the variable `org-use-tag-inheritance'."
  2934. :group 'org-tags
  2935. :type '(repeat (string :tag "Tag")))
  2936. (defun org-tag-inherit-p (tag)
  2937. "Check if TAG is one that should be inherited."
  2938. (cond
  2939. ((member tag org-tags-exclude-from-inheritance) nil)
  2940. ((eq org-use-tag-inheritance t) t)
  2941. ((not org-use-tag-inheritance) nil)
  2942. ((stringp org-use-tag-inheritance)
  2943. (string-match org-use-tag-inheritance tag))
  2944. ((listp org-use-tag-inheritance)
  2945. (member tag org-use-tag-inheritance))
  2946. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2947. (defcustom org-tags-match-list-sublevels t
  2948. "Non-nil means list also sublevels of headlines matching a search.
  2949. This variable applies to tags/property searches, and also to stuck
  2950. projects because this search is based on a tags match as well.
  2951. When set to the symbol `indented', sublevels are indented with
  2952. leading dots.
  2953. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2954. the sublevels of a headline matching a tag search often also match
  2955. the same search. Listing all of them can create very long lists.
  2956. Setting this variable to nil causes subtrees of a match to be skipped.
  2957. This variable is semi-obsolete and probably should always be true. It
  2958. is better to limit inheritance to certain tags using the variables
  2959. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2960. :group 'org-tags
  2961. :type '(choice
  2962. (const :tag "No, don't list them" nil)
  2963. (const :tag "Yes, do list them" t)
  2964. (const :tag "List them, indented with leading dots" indented)))
  2965. (defcustom org-tags-sort-function nil
  2966. "When set, tags are sorted using this function as a comparator."
  2967. :group 'org-tags
  2968. :type '(choice
  2969. (const :tag "No sorting" nil)
  2970. (const :tag "Alphabetical" string<)
  2971. (const :tag "Reverse alphabetical" string>)
  2972. (function :tag "Custom function" nil)))
  2973. (defvar org-tags-history nil
  2974. "History of minibuffer reads for tags.")
  2975. (defvar org-last-tags-completion-table nil
  2976. "The last used completion table for tags.")
  2977. (defvar org-after-tags-change-hook nil
  2978. "Hook that is run after the tags in a line have changed.")
  2979. (defgroup org-properties nil
  2980. "Options concerning properties in Org-mode."
  2981. :tag "Org Properties"
  2982. :group 'org)
  2983. (defcustom org-property-format "%-10s %s"
  2984. "How property key/value pairs should be formatted by `indent-line'.
  2985. When `indent-line' hits a property definition, it will format the line
  2986. according to this format, mainly to make sure that the values are
  2987. lined-up with respect to each other."
  2988. :group 'org-properties
  2989. :type 'string)
  2990. (defcustom org-properties-postprocess-alist nil
  2991. "Alist of properties and functions to adjust inserted values.
  2992. Elements of this alist must be of the form
  2993. ([string] [function])
  2994. where [string] must be a property name and [function] must be a
  2995. lambda expression: this lambda expression must take one argument,
  2996. the value to adjust, and return the new value as a string.
  2997. For example, this element will allow the property \"Remaining\"
  2998. to be updated wrt the relation between the \"Effort\" property
  2999. and the clock summary:
  3000. ((\"Remaining\" (lambda(value)
  3001. (let ((clocksum (org-clock-sum-current-item))
  3002. (effort (org-duration-string-to-minutes
  3003. (org-entry-get (point) \"Effort\"))))
  3004. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3005. :group 'org-properties
  3006. :version "24.1"
  3007. :type '(alist :key-type (string :tag "Property")
  3008. :value-type (function :tag "Function")))
  3009. (defcustom org-use-property-inheritance nil
  3010. "Non-nil means properties apply also for sublevels.
  3011. This setting is chiefly used during property searches. Turning it on can
  3012. cause significant overhead when doing a search, which is why it is not
  3013. on by default.
  3014. When nil, only the properties directly given in the current entry count.
  3015. When t, every property is inherited. The value may also be a list of
  3016. properties that should have inheritance, or a regular expression matching
  3017. properties that should be inherited.
  3018. However, note that some special properties use inheritance under special
  3019. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3020. and the properties ending in \"_ALL\" when they are used as descriptor
  3021. for valid values of a property.
  3022. Note for programmers:
  3023. When querying an entry with `org-entry-get', you can control if inheritance
  3024. should be used. By default, `org-entry-get' looks only at the local
  3025. properties. You can request inheritance by setting the inherit argument
  3026. to t (to force inheritance) or to `selective' (to respect the setting
  3027. in this variable)."
  3028. :group 'org-properties
  3029. :type '(choice
  3030. (const :tag "Not" nil)
  3031. (const :tag "Always" t)
  3032. (repeat :tag "Specific properties" (string :tag "Property"))
  3033. (regexp :tag "Properties matched by regexp")))
  3034. (defun org-property-inherit-p (property)
  3035. "Check if PROPERTY is one that should be inherited."
  3036. (cond
  3037. ((eq org-use-property-inheritance t) t)
  3038. ((not org-use-property-inheritance) nil)
  3039. ((stringp org-use-property-inheritance)
  3040. (string-match org-use-property-inheritance property))
  3041. ((listp org-use-property-inheritance)
  3042. (member property org-use-property-inheritance))
  3043. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3044. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3045. "The default column format, if no other format has been defined.
  3046. This variable can be set on the per-file basis by inserting a line
  3047. #+COLUMNS: %25ITEM ....."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-ellipses ".."
  3051. "The ellipses to be used when a field in column view is truncated.
  3052. When this is the empty string, as many characters as possible are shown,
  3053. but then there will be no visual indication that the field has been truncated.
  3054. When this is a string of length N, the last N characters of a truncated
  3055. field are replaced by this string. If the column is narrower than the
  3056. ellipses string, only part of the ellipses string will be shown."
  3057. :group 'org-properties
  3058. :type 'string)
  3059. (defcustom org-columns-modify-value-for-display-function nil
  3060. "Function that modifies values for display in column view.
  3061. For example, it can be used to cut out a certain part from a time stamp.
  3062. The function must take 2 arguments:
  3063. column-title The title of the column (*not* the property name)
  3064. value The value that should be modified.
  3065. The function should return the value that should be displayed,
  3066. or nil if the normal value should be used."
  3067. :group 'org-properties
  3068. :type '(choice (const nil) (function)))
  3069. (defcustom org-effort-property "Effort"
  3070. "The property that is being used to keep track of effort estimates.
  3071. Effort estimates given in this property need to have the format H:MM."
  3072. :group 'org-properties
  3073. :group 'org-progress
  3074. :type '(string :tag "Property"))
  3075. (defconst org-global-properties-fixed
  3076. '(("VISIBILITY_ALL" . "folded children content all")
  3077. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3078. "List of property/value pairs that can be inherited by any entry.
  3079. These are fixed values, for the preset properties. The user variable
  3080. that can be used to add to this list is `org-global-properties'.
  3081. The entries in this list are cons cells where the car is a property
  3082. name and cdr is a string with the value. If the value represents
  3083. multiple items like an \"_ALL\" property, separate the items by
  3084. spaces.")
  3085. (defcustom org-global-properties nil
  3086. "List of property/value pairs that can be inherited by any entry.
  3087. This list will be combined with the constant `org-global-properties-fixed'.
  3088. The entries in this list are cons cells where the car is a property
  3089. name and cdr is a string with the value.
  3090. You can set buffer-local values for the same purpose in the variable
  3091. `org-file-properties' this by adding lines like
  3092. #+PROPERTY: NAME VALUE"
  3093. :group 'org-properties
  3094. :type '(repeat
  3095. (cons (string :tag "Property")
  3096. (string :tag "Value"))))
  3097. (defvar org-file-properties nil
  3098. "List of property/value pairs that can be inherited by any entry.
  3099. Valid for the current buffer.
  3100. This variable is populated from #+PROPERTY lines.")
  3101. (make-variable-buffer-local 'org-file-properties)
  3102. (defgroup org-agenda nil
  3103. "Options concerning agenda views in Org-mode."
  3104. :tag "Org Agenda"
  3105. :group 'org)
  3106. (defvar org-category nil
  3107. "Variable used by org files to set a category for agenda display.
  3108. Such files should use a file variable to set it, for example
  3109. # -*- mode: org; org-category: \"ELisp\"
  3110. or contain a special line
  3111. #+CATEGORY: ELisp
  3112. If the file does not specify a category, then file's base name
  3113. is used instead.")
  3114. (make-variable-buffer-local 'org-category)
  3115. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3116. (defcustom org-agenda-files nil
  3117. "The files to be used for agenda display.
  3118. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3119. \\[org-remove-file]. You can also use customize to edit the list.
  3120. If an entry is a directory, all files in that directory that are matched by
  3121. `org-agenda-file-regexp' will be part of the file list.
  3122. If the value of the variable is not a list but a single file name, then
  3123. the list of agenda files is actually stored and maintained in that file, one
  3124. agenda file per line. In this file paths can be given relative to
  3125. `org-directory'. Tilde expansion and environment variable substitution
  3126. are also made."
  3127. :group 'org-agenda
  3128. :type '(choice
  3129. (repeat :tag "List of files and directories" file)
  3130. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3131. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3132. "Regular expression to match files for `org-agenda-files'.
  3133. If any element in the list in that variable contains a directory instead
  3134. of a normal file, all files in that directory that are matched by this
  3135. regular expression will be included."
  3136. :group 'org-agenda
  3137. :type 'regexp)
  3138. (defcustom org-agenda-text-search-extra-files nil
  3139. "List of extra files to be searched by text search commands.
  3140. These files will be searched in addition to the agenda files by the
  3141. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3142. Note that these files will only be searched for text search commands,
  3143. not for the other agenda views like todo lists, tag searches or the weekly
  3144. agenda. This variable is intended to list notes and possibly archive files
  3145. that should also be searched by these two commands.
  3146. In fact, if the first element in the list is the symbol `agenda-archives',
  3147. then all archive files of all agenda files will be added to the search
  3148. scope."
  3149. :group 'org-agenda
  3150. :type '(set :greedy t
  3151. (const :tag "Agenda Archives" agenda-archives)
  3152. (repeat :inline t (file))))
  3153. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3154. 'org-agenda-text-search-extra-files)
  3155. (defcustom org-agenda-skip-unavailable-files nil
  3156. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3157. A nil value means to remove them, after a query, from the list."
  3158. :group 'org-agenda
  3159. :type 'boolean)
  3160. (defcustom org-calendar-to-agenda-key [?c]
  3161. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3162. The command `org-calendar-goto-agenda' will be bound to this key. The
  3163. default is the character `c' because then `c' can be used to switch back and
  3164. forth between agenda and calendar."
  3165. :group 'org-agenda
  3166. :type 'sexp)
  3167. (defcustom org-calendar-insert-diary-entry-key [?i]
  3168. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3169. This option is irrelevant until `org-agenda-diary-file' has been configured
  3170. to point to an Org-mode file. When that is the case, the command
  3171. `org-agenda-diary-entry' will be bound to the key given here, by default
  3172. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3173. if you want to continue doing this, you need to change this to a different
  3174. key."
  3175. :group 'org-agenda
  3176. :type 'sexp)
  3177. (defcustom org-agenda-diary-file 'diary-file
  3178. "File to which to add new entries with the `i' key in agenda and calendar.
  3179. When this is the symbol `diary-file', the functionality in the Emacs
  3180. calendar will be used to add entries to the `diary-file'. But when this
  3181. points to a file, `org-agenda-diary-entry' will be used instead."
  3182. :group 'org-agenda
  3183. :type '(choice
  3184. (const :tag "The standard Emacs diary file" diary-file)
  3185. (file :tag "Special Org file diary entries")))
  3186. (eval-after-load "calendar"
  3187. '(progn
  3188. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3189. 'org-calendar-goto-agenda)
  3190. (add-hook 'calendar-mode-hook
  3191. (lambda ()
  3192. (unless (eq org-agenda-diary-file 'diary-file)
  3193. (define-key calendar-mode-map
  3194. org-calendar-insert-diary-entry-key
  3195. 'org-agenda-diary-entry))))))
  3196. (defgroup org-latex nil
  3197. "Options for embedding LaTeX code into Org-mode."
  3198. :tag "Org LaTeX"
  3199. :group 'org)
  3200. (defcustom org-format-latex-options
  3201. '(:foreground default :background default :scale 1.0
  3202. :html-foreground "Black" :html-background "Transparent"
  3203. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3204. "Options for creating images from LaTeX fragments.
  3205. This is a property list with the following properties:
  3206. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3207. `default' means use the foreground of the default face.
  3208. `auto' means use the foreground from the text face.
  3209. :background the background color, or \"Transparent\".
  3210. `default' means use the background of the default face.
  3211. `auto' means use the background from the text face.
  3212. :scale a scaling factor for the size of the images, to get more pixels
  3213. :html-foreground, :html-background, :html-scale
  3214. the same numbers for HTML export.
  3215. :matchers a list indicating which matchers should be used to
  3216. find LaTeX fragments. Valid members of this list are:
  3217. \"begin\" find environments
  3218. \"$1\" find single characters surrounded by $.$
  3219. \"$\" find math expressions surrounded by $...$
  3220. \"$$\" find math expressions surrounded by $$....$$
  3221. \"\\(\" find math expressions surrounded by \\(...\\)
  3222. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3223. :group 'org-latex
  3224. :type 'plist)
  3225. (defcustom org-format-latex-signal-error t
  3226. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3227. When nil, just push out a message."
  3228. :group 'org-latex
  3229. :version "24.1"
  3230. :type 'boolean)
  3231. (defcustom org-latex-to-mathml-jar-file nil
  3232. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3233. Use this to specify additional executable file say a jar file.
  3234. When using MathToWeb as the converter, specify the full-path to
  3235. your mathtoweb.jar file."
  3236. :group 'org-latex
  3237. :version "24.1"
  3238. :type '(choice
  3239. (const :tag "None" nil)
  3240. (file :tag "JAR file" :must-match t)))
  3241. (defcustom org-latex-to-mathml-convert-command nil
  3242. "Command to convert LaTeX fragments to MathML.
  3243. Replace format-specifiers in the command as noted below and use
  3244. `shell-command' to convert LaTeX to MathML.
  3245. %j: Executable file in fully expanded form as specified by
  3246. `org-latex-to-mathml-jar-file'.
  3247. %I: Input LaTeX file in fully expanded form
  3248. %o: Output MathML file
  3249. This command is used by `org-create-math-formula'.
  3250. When using MathToWeb as the converter, set this to
  3251. \"java -jar %j -unicode -force -df %o %I\"."
  3252. :group 'org-latex
  3253. :version "24.1"
  3254. :type '(choice
  3255. (const :tag "None" nil)
  3256. (string :tag "\nShell command")))
  3257. (defcustom org-latex-create-formula-image-program 'dvipng
  3258. "Program to convert LaTeX fragments with.
  3259. dvipng Process the LaTeX fragments to dvi file, then convert
  3260. dvi files to png files using dvipng.
  3261. This will also include processing of non-math environments.
  3262. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3263. to convert pdf files to png files"
  3264. :group 'org-latex
  3265. :version "24.1"
  3266. :type '(choice
  3267. (const :tag "dvipng" dvipng)
  3268. (const :tag "imagemagick" imagemagick)))
  3269. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3270. "Path to store latex preview images.
  3271. A relative path here creates many directories relative to the
  3272. processed org files paths. An absolute path puts all preview
  3273. images at the same place."
  3274. :group 'org-latex
  3275. :version "24.3"
  3276. :type 'string)
  3277. (defun org-format-latex-mathml-available-p ()
  3278. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3279. (save-match-data
  3280. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3281. org-latex-to-mathml-convert-command)
  3282. (let ((executable (car (split-string
  3283. org-latex-to-mathml-convert-command))))
  3284. (when (executable-find executable)
  3285. (if (string-match
  3286. "%j" org-latex-to-mathml-convert-command)
  3287. (file-readable-p org-latex-to-mathml-jar-file)
  3288. t))))))
  3289. (defcustom org-format-latex-header "\\documentclass{article}
  3290. \\usepackage[usenames]{color}
  3291. \[PACKAGES]
  3292. \[DEFAULT-PACKAGES]
  3293. \\pagestyle{empty} % do not remove
  3294. % The settings below are copied from fullpage.sty
  3295. \\setlength{\\textwidth}{\\paperwidth}
  3296. \\addtolength{\\textwidth}{-3cm}
  3297. \\setlength{\\oddsidemargin}{1.5cm}
  3298. \\addtolength{\\oddsidemargin}{-2.54cm}
  3299. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3300. \\setlength{\\textheight}{\\paperheight}
  3301. \\addtolength{\\textheight}{-\\headheight}
  3302. \\addtolength{\\textheight}{-\\headsep}
  3303. \\addtolength{\\textheight}{-\\footskip}
  3304. \\addtolength{\\textheight}{-3cm}
  3305. \\setlength{\\topmargin}{1.5cm}
  3306. \\addtolength{\\topmargin}{-2.54cm}"
  3307. "The document header used for processing LaTeX fragments.
  3308. It is imperative that this header make sure that no page number
  3309. appears on the page. The package defined in the variables
  3310. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3311. will either replace the placeholder \"[PACKAGES]\" in this
  3312. header, or they will be appended."
  3313. :group 'org-latex
  3314. :type 'string)
  3315. (defun org-set-packages-alist (var val)
  3316. "Set the packages alist and make sure it has 3 elements per entry."
  3317. (set var (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. val)))
  3322. (defun org-get-packages-alist (var)
  3323. "Get the packages alist and make sure it has 3 elements per entry."
  3324. (mapcar (lambda (x)
  3325. (if (and (consp x) (= (length x) 2))
  3326. (list (car x) (nth 1 x) t)
  3327. x))
  3328. (default-value var)))
  3329. (defcustom org-latex-default-packages-alist
  3330. '(("AUTO" "inputenc" t)
  3331. ("T1" "fontenc" t)
  3332. ("" "fixltx2e" nil)
  3333. ("" "graphicx" t)
  3334. ("" "longtable" nil)
  3335. ("" "float" nil)
  3336. ("" "wrapfig" nil)
  3337. ("" "rotating" nil)
  3338. ("normalem" "ulem" t)
  3339. ("" "amsmath" t)
  3340. ("" "textcomp" t)
  3341. ("" "marvosym" t)
  3342. ("" "wasysym" t)
  3343. ("" "amssymb" t)
  3344. ("" "hyperref" nil)
  3345. "\\tolerance=1000")
  3346. "Alist of default packages to be inserted in the header.
  3347. Change this only if one of the packages here causes an
  3348. incompatibility with another package you are using.
  3349. The packages in this list are needed by one part or another of
  3350. Org mode to function properly:
  3351. - inputenc, fontenc: for basic font and character selection
  3352. - fixltx2e: Important patches of LaTeX itself
  3353. - graphicx: for including images
  3354. - longtable: For multipage tables
  3355. - float, wrapfig: for figure placement
  3356. - rotating: for sideways figures and tables
  3357. - ulem: for underline and strike-through
  3358. - amsmath: for subscript and superscript and math environments
  3359. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3360. for interpreting the entities in `org-entities'. You can skip
  3361. some of these packages if you don't use any of their symbols.
  3362. - hyperref: for cross references
  3363. Therefore you should not modify this variable unless you know
  3364. what you are doing. The one reason to change it anyway is that
  3365. you might be loading some other package that conflicts with one
  3366. of the default packages. Each cell is of the format
  3367. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3368. the package also needs to be included when compiling LaTeX
  3369. snippets into images for inclusion into non-LaTeX output."
  3370. :group 'org-latex
  3371. :group 'org-export-latex
  3372. :set 'org-set-packages-alist
  3373. :get 'org-get-packages-alist
  3374. :version "24.1"
  3375. :type '(repeat
  3376. (choice
  3377. (list :tag "options/package pair"
  3378. (string :tag "options")
  3379. (string :tag "package")
  3380. (boolean :tag "Snippet"))
  3381. (string :tag "A line of LaTeX"))))
  3382. (defcustom org-latex-packages-alist nil
  3383. "Alist of packages to be inserted in every LaTeX header.
  3384. These will be inserted after `org-latex-default-packages-alist'.
  3385. Each cell is of the format:
  3386. \(\"options\" \"package\" snippet-flag)
  3387. SNIPPET-FLAG, when t, indicates that this package is also needed
  3388. when turning LaTeX snippets into images for inclusion into
  3389. non-LaTeX output.
  3390. Make sure that you only list packages here which:
  3391. - you want in every file
  3392. - do not conflict with the setup in `org-format-latex-header'.
  3393. - do not conflict with the default packages in
  3394. `org-latex-default-packages-alist'."
  3395. :group 'org-latex
  3396. :group 'org-export-latex
  3397. :set 'org-set-packages-alist
  3398. :get 'org-get-packages-alist
  3399. :type '(repeat
  3400. (choice
  3401. (list :tag "options/package pair"
  3402. (string :tag "options")
  3403. (string :tag "package")
  3404. (boolean :tag "Snippet"))
  3405. (string :tag "A line of LaTeX"))))
  3406. (defgroup org-appearance nil
  3407. "Settings for Org-mode appearance."
  3408. :tag "Org Appearance"
  3409. :group 'org)
  3410. (defcustom org-level-color-stars-only nil
  3411. "Non-nil means fontify only the stars in each headline.
  3412. When nil, the entire headline is fontified.
  3413. Changing it requires restart of `font-lock-mode' to become effective
  3414. also in regions already fontified."
  3415. :group 'org-appearance
  3416. :type 'boolean)
  3417. (defcustom org-hide-leading-stars nil
  3418. "Non-nil means hide the first N-1 stars in a headline.
  3419. This works by using the face `org-hide' for these stars. This
  3420. face is white for a light background, and black for a dark
  3421. background. You may have to customize the face `org-hide' to
  3422. make this work.
  3423. Changing it requires restart of `font-lock-mode' to become effective
  3424. also in regions already fontified.
  3425. You may also set this on a per-file basis by adding one of the following
  3426. lines to the buffer:
  3427. #+STARTUP: hidestars
  3428. #+STARTUP: showstars"
  3429. :group 'org-appearance
  3430. :type 'boolean)
  3431. (defcustom org-hidden-keywords nil
  3432. "List of symbols corresponding to keywords to be hidden the org buffer.
  3433. For example, a value '(title) for this list will make the document's title
  3434. appear in the buffer without the initial #+TITLE: keyword."
  3435. :group 'org-appearance
  3436. :version "24.1"
  3437. :type '(set (const :tag "#+AUTHOR" author)
  3438. (const :tag "#+DATE" date)
  3439. (const :tag "#+EMAIL" email)
  3440. (const :tag "#+TITLE" title)))
  3441. (defcustom org-custom-properties nil
  3442. "List of properties (as strings) with a special meaning.
  3443. The default use of these custom properties is to let the user
  3444. hide them with `org-toggle-custom-properties-visibility'."
  3445. :group 'org-properties
  3446. :group 'org-appearance
  3447. :version "24.3"
  3448. :type '(repeat (string :tag "Property Name")))
  3449. (defcustom org-fontify-done-headline nil
  3450. "Non-nil means change the face of a headline if it is marked DONE.
  3451. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3452. When this is non-nil, the headline after the keyword is set to the
  3453. `org-headline-done' as an additional indication."
  3454. :group 'org-appearance
  3455. :type 'boolean)
  3456. (defcustom org-fontify-emphasized-text t
  3457. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3458. Changing this variable requires a restart of Emacs to take effect."
  3459. :group 'org-appearance
  3460. :type 'boolean)
  3461. (defcustom org-fontify-whole-heading-line nil
  3462. "Non-nil means fontify the whole line for headings.
  3463. This is useful when setting a background color for the
  3464. org-level-* faces."
  3465. :group 'org-appearance
  3466. :type 'boolean)
  3467. (defcustom org-highlight-latex-and-related nil
  3468. "Non-nil means highlight LaTeX related syntax in the buffer.
  3469. When non nil, the value should be a list containing any of the
  3470. following symbols:
  3471. `latex' Highlight LaTeX snippets and environments.
  3472. `script' Highlight subscript and superscript.
  3473. `entities' Highlight entities."
  3474. :group 'org-appearance
  3475. :version "24.4"
  3476. :package-version '(Org . "8.0")
  3477. :type '(choice
  3478. (const :tag "No highlighting" nil)
  3479. (set :greedy t :tag "Highlight"
  3480. (const :tag "LaTeX snippets and environments" latex)
  3481. (const :tag "Subscript and superscript" script)
  3482. (const :tag "Entities" entities))))
  3483. (defcustom org-hide-emphasis-markers nil
  3484. "Non-nil mean font-lock should hide the emphasis marker characters."
  3485. :group 'org-appearance
  3486. :type 'boolean)
  3487. (defcustom org-pretty-entities nil
  3488. "Non-nil means show entities as UTF8 characters.
  3489. When nil, the \\name form remains in the buffer."
  3490. :group 'org-appearance
  3491. :version "24.1"
  3492. :type 'boolean)
  3493. (defcustom org-pretty-entities-include-sub-superscripts t
  3494. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3495. :group 'org-appearance
  3496. :version "24.1"
  3497. :type 'boolean)
  3498. (defvar org-emph-re nil
  3499. "Regular expression for matching emphasis.
  3500. After a match, the match groups contain these elements:
  3501. 0 The match of the full regular expression, including the characters
  3502. before and after the proper match
  3503. 1 The character before the proper match, or empty at beginning of line
  3504. 2 The proper match, including the leading and trailing markers
  3505. 3 The leading marker like * or /, indicating the type of highlighting
  3506. 4 The text between the emphasis markers, not including the markers
  3507. 5 The character after the match, empty at the end of a line")
  3508. (defvar org-verbatim-re nil
  3509. "Regular expression for matching verbatim text.")
  3510. (defvar org-emphasis-regexp-components) ; defined just below
  3511. (defvar org-emphasis-alist) ; defined just below
  3512. (defun org-set-emph-re (var val)
  3513. "Set variable and compute the emphasis regular expression."
  3514. (set var val)
  3515. (when (and (boundp 'org-emphasis-alist)
  3516. (boundp 'org-emphasis-regexp-components)
  3517. org-emphasis-alist org-emphasis-regexp-components)
  3518. (let* ((e org-emphasis-regexp-components)
  3519. (pre (car e))
  3520. (post (nth 1 e))
  3521. (border (nth 2 e))
  3522. (body (nth 3 e))
  3523. (nl (nth 4 e))
  3524. (body1 (concat body "*?"))
  3525. (markers (mapconcat 'car org-emphasis-alist ""))
  3526. (vmarkers (mapconcat
  3527. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3528. org-emphasis-alist "")))
  3529. ;; make sure special characters appear at the right position in the class
  3530. (if (string-match "\\^" markers)
  3531. (setq markers (concat (replace-match "" t t markers) "^")))
  3532. (if (string-match "-" markers)
  3533. (setq markers (concat (replace-match "" t t markers) "-")))
  3534. (if (string-match "\\^" vmarkers)
  3535. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3536. (if (string-match "-" vmarkers)
  3537. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3538. (if (> nl 0)
  3539. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3540. (int-to-string nl) "\\}")))
  3541. ;; Make the regexp
  3542. (setq org-emph-re
  3543. (concat "\\([" pre "]\\|^\\)"
  3544. "\\("
  3545. "\\([" markers "]\\)"
  3546. "\\("
  3547. "[^" border "]\\|"
  3548. "[^" border "]"
  3549. body1
  3550. "[^" border "]"
  3551. "\\)"
  3552. "\\3\\)"
  3553. "\\([" post "]\\|$\\)"))
  3554. (setq org-verbatim-re
  3555. (concat "\\([" pre "]\\|^\\)"
  3556. "\\("
  3557. "\\([" vmarkers "]\\)"
  3558. "\\("
  3559. "[^" border "]\\|"
  3560. "[^" border "]"
  3561. body1
  3562. "[^" border "]"
  3563. "\\)"
  3564. "\\3\\)"
  3565. "\\([" post "]\\|$\\)")))))
  3566. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3567. ;; set this option proved cumbersome. See this message/thread:
  3568. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3569. (defvar org-emphasis-regexp-components
  3570. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3571. "Components used to build the regular expression for emphasis.
  3572. This is a list with five entries. Terminology: In an emphasis string
  3573. like \" *strong word* \", we call the initial space PREMATCH, the final
  3574. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3575. and \"trong wor\" is the body. The different components in this variable
  3576. specify what is allowed/forbidden in each part:
  3577. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3578. post Chars allowed as postmatch. End of line will be allowed too.
  3579. border The chars *forbidden* as border characters.
  3580. body-regexp A regexp like \".\" to match a body character. Don't use
  3581. non-shy groups here, and don't allow newline here.
  3582. newline The maximum number of newlines allowed in an emphasis exp.
  3583. You need to reload Org or to restart Emacs after customizing this.")
  3584. (defcustom org-emphasis-alist
  3585. `(("*" bold)
  3586. ("/" italic)
  3587. ("_" underline)
  3588. ("=" org-code verbatim)
  3589. ("~" org-verbatim verbatim)
  3590. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3591. "Alist of characters and faces to emphasize text.
  3592. Text starting and ending with a special character will be emphasized,
  3593. for example *bold*, _underlined_ and /italic/. This variable sets the
  3594. marker characters and the face to be used by font-lock for highlighting
  3595. in Org-mode Emacs buffers.
  3596. You need to reload Org or to restart Emacs after customizing this."
  3597. :group 'org-appearance
  3598. :set 'org-set-emph-re
  3599. :version "24.4"
  3600. :package-version '(Org . "8.0")
  3601. :type '(repeat
  3602. (list
  3603. (string :tag "Marker character")
  3604. (choice
  3605. (face :tag "Font-lock-face")
  3606. (plist :tag "Face property list"))
  3607. (option (const verbatim)))))
  3608. (defvar org-protecting-blocks
  3609. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3610. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3611. This is needed for font-lock setup.")
  3612. ;;; Miscellaneous options
  3613. (defgroup org-completion nil
  3614. "Completion in Org-mode."
  3615. :tag "Org Completion"
  3616. :group 'org)
  3617. (defcustom org-completion-use-ido nil
  3618. "Non-nil means use ido completion wherever possible.
  3619. Note that `ido-mode' must be active for this variable to be relevant.
  3620. If you decide to turn this variable on, you might well want to turn off
  3621. `org-outline-path-complete-in-steps'.
  3622. See also `org-completion-use-iswitchb'."
  3623. :group 'org-completion
  3624. :type 'boolean)
  3625. (defcustom org-completion-use-iswitchb nil
  3626. "Non-nil means use iswitchb completion wherever possible.
  3627. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3628. If you decide to turn this variable on, you might well want to turn off
  3629. `org-outline-path-complete-in-steps'.
  3630. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3631. :group 'org-completion
  3632. :type 'boolean)
  3633. (defcustom org-completion-fallback-command 'hippie-expand
  3634. "The expansion command called by \\[pcomplete] in normal context.
  3635. Normal means, no org-mode-specific context."
  3636. :group 'org-completion
  3637. :type 'function)
  3638. ;;; Functions and variables from their packages
  3639. ;; Declared here to avoid compiler warnings
  3640. ;; XEmacs only
  3641. (defvar outline-mode-menu-heading)
  3642. (defvar outline-mode-menu-show)
  3643. (defvar outline-mode-menu-hide)
  3644. (defvar zmacs-regions) ; XEmacs regions
  3645. ;; Emacs only
  3646. (defvar mark-active)
  3647. ;; Various packages
  3648. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3649. (declare-function calendar-forward-day "cal-move" (arg))
  3650. (declare-function calendar-goto-date "cal-move" (date))
  3651. (declare-function calendar-goto-today "cal-move" ())
  3652. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3653. (defvar calc-embedded-close-formula)
  3654. (defvar calc-embedded-open-formula)
  3655. (declare-function cdlatex-tab "ext:cdlatex" ())
  3656. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3657. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3658. (defvar font-lock-unfontify-region-function)
  3659. (declare-function iswitchb-read-buffer "iswitchb"
  3660. (prompt &optional default require-match start matches-set))
  3661. (defvar iswitchb-temp-buflist)
  3662. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3663. (defvar org-agenda-tags-todo-honor-ignore-options)
  3664. (declare-function org-agenda-skip "org-agenda" ())
  3665. (declare-function
  3666. org-agenda-format-item "org-agenda"
  3667. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3668. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3669. (declare-function org-agenda-change-all-lines "org-agenda"
  3670. (newhead hdmarker &optional fixface just-this))
  3671. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3672. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3673. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3674. (beg end))
  3675. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3676. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3677. "org-agenda" (&optional end))
  3678. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3679. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3680. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3681. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3682. (declare-function org-indent-mode "org-indent" (&optional arg))
  3683. (declare-function parse-time-string "parse-time" (string))
  3684. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3685. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3686. (defvar remember-data-file)
  3687. (defvar texmathp-why)
  3688. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3689. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3690. (defvar org-latex-regexps)
  3691. ;;; Autoload and prepare some org modules
  3692. ;; Some table stuff that needs to be defined here, because it is used
  3693. ;; by the functions setting up org-mode or checking for table context.
  3694. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3695. "Detect an org-type or table-type table.")
  3696. (defconst org-table-line-regexp "^[ \t]*|"
  3697. "Detect an org-type table line.")
  3698. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3699. "Detect an org-type table line.")
  3700. (defconst org-table-hline-regexp "^[ \t]*|-"
  3701. "Detect an org-type table hline.")
  3702. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3703. "Detect a table-type table hline.")
  3704. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3705. "Detect the first line outside a table when searching from within it.
  3706. This works for both table types.")
  3707. ;; Autoload the functions in org-table.el that are needed by functions here.
  3708. (eval-and-compile
  3709. (org-autoload "org-table"
  3710. '(org-table-begin org-table-blank-field org-table-end)))
  3711. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3712. "Detect a #+TBLFM line.")
  3713. ;;;###autoload
  3714. (defun turn-on-orgtbl ()
  3715. "Unconditionally turn on `orgtbl-mode'."
  3716. (require 'org-table)
  3717. (orgtbl-mode 1))
  3718. (defun org-at-table-p (&optional table-type)
  3719. "Return t if the cursor is inside an org-type table.
  3720. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3721. (if org-enable-table-editor
  3722. (save-excursion
  3723. (beginning-of-line 1)
  3724. (looking-at (if table-type org-table-any-line-regexp
  3725. org-table-line-regexp)))
  3726. nil))
  3727. (defsubst org-table-p () (org-at-table-p))
  3728. (defun org-at-table.el-p ()
  3729. "Return t if and only if we are at a table.el table."
  3730. (and (org-at-table-p 'any)
  3731. (save-excursion
  3732. (goto-char (org-table-begin 'any))
  3733. (looking-at org-table1-hline-regexp))))
  3734. (defun org-table-recognize-table.el ()
  3735. "If there is a table.el table nearby, recognize it and move into it."
  3736. (if org-table-tab-recognizes-table.el
  3737. (if (org-at-table.el-p)
  3738. (progn
  3739. (beginning-of-line 1)
  3740. (if (looking-at org-table-dataline-regexp)
  3741. nil
  3742. (if (looking-at org-table1-hline-regexp)
  3743. (progn
  3744. (beginning-of-line 2)
  3745. (if (looking-at org-table-any-border-regexp)
  3746. (beginning-of-line -1)))))
  3747. (if (re-search-forward "|" (org-table-end t) t)
  3748. (progn
  3749. (require 'table)
  3750. (if (table--at-cell-p (point))
  3751. t
  3752. (message "recognizing table.el table...")
  3753. (table-recognize-table)
  3754. (message "recognizing table.el table...done")))
  3755. (error "This should not happen"))
  3756. t)
  3757. nil)
  3758. nil))
  3759. (defun org-at-table-hline-p ()
  3760. "Return t if the cursor is inside a hline in a table."
  3761. (if org-enable-table-editor
  3762. (save-excursion
  3763. (beginning-of-line 1)
  3764. (looking-at org-table-hline-regexp))
  3765. nil))
  3766. (defun org-table-map-tables (function &optional quietly)
  3767. "Apply FUNCTION to the start of all tables in the buffer."
  3768. (save-excursion
  3769. (save-restriction
  3770. (widen)
  3771. (goto-char (point-min))
  3772. (while (re-search-forward org-table-any-line-regexp nil t)
  3773. (unless quietly
  3774. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3775. (beginning-of-line 1)
  3776. (when (and (looking-at org-table-line-regexp)
  3777. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3778. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3779. (save-excursion (funcall function))
  3780. (or (looking-at org-table-line-regexp)
  3781. (forward-char 1)))
  3782. (re-search-forward org-table-any-border-regexp nil 1))))
  3783. (unless quietly (message "Mapping tables: done")))
  3784. ;; Declare and autoload functions from org-agenda.el
  3785. (eval-and-compile
  3786. (org-autoload "org-agenda"
  3787. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3788. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3789. (declare-function org-clock-update-mode-line "org-clock" ())
  3790. (declare-function org-resolve-clocks "org-clock"
  3791. (&optional also-non-dangling-p prompt last-valid))
  3792. (defun org-at-TBLFM-p (&optional pos)
  3793. "Return t when point (or POS) is in #+TBLFM line."
  3794. (save-excursion
  3795. (let ((pos pos)))
  3796. (goto-char (or pos (point)))
  3797. (beginning-of-line 1)
  3798. (looking-at org-TBLFM-regexp)))
  3799. (defvar org-clock-start-time)
  3800. (defvar org-clock-marker (make-marker)
  3801. "Marker recording the last clock-in.")
  3802. (defvar org-clock-hd-marker (make-marker)
  3803. "Marker recording the last clock-in, but the headline position.")
  3804. (defvar org-clock-heading ""
  3805. "The heading of the current clock entry.")
  3806. (defun org-clock-is-active ()
  3807. "Return the buffer where the clock is currently running.
  3808. Return nil if no clock is running."
  3809. (marker-buffer org-clock-marker))
  3810. (eval-and-compile
  3811. (org-autoload "org-clock" '(org-clock-remove-overlays
  3812. org-clock-update-time-maybe
  3813. org-clocktable-shift)))
  3814. (defun org-check-running-clock ()
  3815. "Check if the current buffer contains the running clock.
  3816. If yes, offer to stop it and to save the buffer with the changes."
  3817. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3818. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3819. (buffer-name))))
  3820. (org-clock-out)
  3821. (when (y-or-n-p "Save changed buffer?")
  3822. (save-buffer))))
  3823. (defun org-clocktable-try-shift (dir n)
  3824. "Check if this line starts a clock table, if yes, shift the time block."
  3825. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3826. (org-clocktable-shift dir n)))
  3827. ;;;###autoload
  3828. (defun org-clock-persistence-insinuate ()
  3829. "Set up hooks for clock persistence."
  3830. (require 'org-clock)
  3831. (add-hook 'org-mode-hook 'org-clock-load)
  3832. (add-hook 'kill-emacs-hook 'org-clock-save))
  3833. ;; Define the variable already here, to make sure we have it.
  3834. (defvar org-indent-mode nil
  3835. "Non-nil if Org-Indent mode is enabled.
  3836. Use the command `org-indent-mode' to change this variable.")
  3837. ;; Autoload archiving code
  3838. ;; The stuff that is needed for cycling and tags has to be defined here.
  3839. (defgroup org-archive nil
  3840. "Options concerning archiving in Org-mode."
  3841. :tag "Org Archive"
  3842. :group 'org-structure)
  3843. (defcustom org-archive-location "%s_archive::"
  3844. "The location where subtrees should be archived.
  3845. The value of this variable is a string, consisting of two parts,
  3846. separated by a double-colon. The first part is a filename and
  3847. the second part is a headline.
  3848. When the filename is omitted, archiving happens in the same file.
  3849. %s in the filename will be replaced by the current file
  3850. name (without the directory part). Archiving to a different file
  3851. is useful to keep archived entries from contributing to the
  3852. Org-mode Agenda.
  3853. The archived entries will be filed as subtrees of the specified
  3854. headline. When the headline is omitted, the subtrees are simply
  3855. filed away at the end of the file, as top-level entries. Also in
  3856. the heading you can use %s to represent the file name, this can be
  3857. useful when using the same archive for a number of different files.
  3858. Here are a few examples:
  3859. \"%s_archive::\"
  3860. If the current file is Projects.org, archive in file
  3861. Projects.org_archive, as top-level trees. This is the default.
  3862. \"::* Archived Tasks\"
  3863. Archive in the current file, under the top-level headline
  3864. \"* Archived Tasks\".
  3865. \"~/org/archive.org::\"
  3866. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3867. \"~/org/archive.org::* From %s\"
  3868. Archive in file ~/org/archive.org (absolute path), under headlines
  3869. \"From FILENAME\" where file name is the current file name.
  3870. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3871. The \"datetree/\" string is special, signifying to archive
  3872. items to the datetree. Items are placed in either the CLOSED
  3873. date of the item, or the current date if there is no CLOSED date.
  3874. The heading will be a subentry to the current date. There doesn't
  3875. need to be a heading, but there always needs to be a slash after
  3876. datetree. For example, to store archived items directly in the
  3877. datetree, use \"~/org/datetree.org::datetree/\".
  3878. \"basement::** Finished Tasks\"
  3879. Archive in file ./basement (relative path), as level 3 trees
  3880. below the level 2 heading \"** Finished Tasks\".
  3881. You may set this option on a per-file basis by adding to the buffer a
  3882. line like
  3883. #+ARCHIVE: basement::** Finished Tasks
  3884. You may also define it locally for a subtree by setting an ARCHIVE property
  3885. in the entry. If such a property is found in an entry, or anywhere up
  3886. the hierarchy, it will be used."
  3887. :group 'org-archive
  3888. :type 'string)
  3889. (defcustom org-archive-tag "ARCHIVE"
  3890. "The tag that marks a subtree as archived.
  3891. An archived subtree does not open during visibility cycling, and does
  3892. not contribute to the agenda listings.
  3893. After changing this, font-lock must be restarted in the relevant buffers to
  3894. get the proper fontification."
  3895. :group 'org-archive
  3896. :group 'org-keywords
  3897. :type 'string)
  3898. (defcustom org-agenda-skip-archived-trees t
  3899. "Non-nil means the agenda will skip any items located in archived trees.
  3900. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3901. variable is no longer recommended, you should leave it at the value t.
  3902. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3903. :group 'org-archive
  3904. :group 'org-agenda-skip
  3905. :type 'boolean)
  3906. (defcustom org-columns-skip-archived-trees t
  3907. "Non-nil means ignore archived trees when creating column view."
  3908. :group 'org-archive
  3909. :group 'org-properties
  3910. :type 'boolean)
  3911. (defcustom org-cycle-open-archived-trees nil
  3912. "Non-nil means `org-cycle' will open archived trees.
  3913. An archived tree is a tree marked with the tag ARCHIVE.
  3914. When nil, archived trees will stay folded. You can still open them with
  3915. normal outline commands like `show-all', but not with the cycling commands."
  3916. :group 'org-archive
  3917. :group 'org-cycle
  3918. :type 'boolean)
  3919. (defcustom org-sparse-tree-open-archived-trees nil
  3920. "Non-nil means sparse tree construction shows matches in archived trees.
  3921. When nil, matches in these trees are highlighted, but the trees are kept in
  3922. collapsed state."
  3923. :group 'org-archive
  3924. :group 'org-sparse-trees
  3925. :type 'boolean)
  3926. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3927. "The default date type when building a sparse tree.
  3928. When this is nil, a date is a scheduled or a deadline timestamp.
  3929. Otherwise, these types are allowed:
  3930. all: all timestamps
  3931. active: only active timestamps (<...>)
  3932. inactive: only inactive timestamps (<...)
  3933. scheduled: only scheduled timestamps
  3934. deadline: only deadline timestamps"
  3935. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3936. (const :tag "All timestamps" all)
  3937. (const :tag "Only active timestamps" active)
  3938. (const :tag "Only inactive timestamps" inactive)
  3939. (const :tag "Only scheduled timestamps" scheduled)
  3940. (const :tag "Only deadline timestamps" deadline)
  3941. (const :tag "Only closed timestamps" closed))
  3942. :version "24.3"
  3943. :group 'org-sparse-trees)
  3944. (defun org-cycle-hide-archived-subtrees (state)
  3945. "Re-hide all archived subtrees after a visibility state change."
  3946. (when (and (not org-cycle-open-archived-trees)
  3947. (not (memq state '(overview folded))))
  3948. (save-excursion
  3949. (let* ((globalp (memq state '(contents all)))
  3950. (beg (if globalp (point-min) (point)))
  3951. (end (if globalp (point-max) (org-end-of-subtree t))))
  3952. (org-hide-archived-subtrees beg end)
  3953. (goto-char beg)
  3954. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3955. (message "%s" (substitute-command-keys
  3956. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3957. (defun org-force-cycle-archived ()
  3958. "Cycle subtree even if it is archived."
  3959. (interactive)
  3960. (setq this-command 'org-cycle)
  3961. (let ((org-cycle-open-archived-trees t))
  3962. (call-interactively 'org-cycle)))
  3963. (defun org-hide-archived-subtrees (beg end)
  3964. "Re-hide all archived subtrees after a visibility state change."
  3965. (save-excursion
  3966. (let* ((re (concat ":" org-archive-tag ":")))
  3967. (goto-char beg)
  3968. (while (re-search-forward re end t)
  3969. (when (org-at-heading-p)
  3970. (org-flag-subtree t)
  3971. (org-end-of-subtree t))))))
  3972. (declare-function outline-end-of-heading "outline" ())
  3973. (declare-function outline-flag-region "outline" (from to flag))
  3974. (defun org-flag-subtree (flag)
  3975. (save-excursion
  3976. (org-back-to-heading t)
  3977. (outline-end-of-heading)
  3978. (outline-flag-region (point)
  3979. (progn (org-end-of-subtree t) (point))
  3980. flag)))
  3981. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3982. (eval-and-compile
  3983. (org-autoload "org-archive"
  3984. '(org-add-archive-files)))
  3985. ;; Autoload Column View Code
  3986. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3987. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3988. (declare-function org-columns-compute "org-colview" (property))
  3989. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3990. '(org-columns-number-to-string
  3991. org-columns-get-format-and-top-level
  3992. org-columns-compute
  3993. org-columns-remove-overlays))
  3994. ;; Autoload ID code
  3995. (declare-function org-id-store-link "org-id")
  3996. (declare-function org-id-locations-load "org-id")
  3997. (declare-function org-id-locations-save "org-id")
  3998. (defvar org-id-track-globally)
  3999. (org-autoload "org-id"
  4000. '(org-id-new
  4001. org-id-copy
  4002. org-id-get-with-outline-path-completion
  4003. org-id-get-with-outline-drilling))
  4004. ;;; Variables for pre-computed regular expressions, all buffer local
  4005. (defvar org-todo-regexp nil
  4006. "Matches any of the TODO state keywords.")
  4007. (make-variable-buffer-local 'org-todo-regexp)
  4008. (defvar org-not-done-regexp nil
  4009. "Matches any of the TODO state keywords except the last one.")
  4010. (make-variable-buffer-local 'org-not-done-regexp)
  4011. (defvar org-not-done-heading-regexp nil
  4012. "Matches a TODO headline that is not done.")
  4013. (make-variable-buffer-local 'org-not-done-regexp)
  4014. (defvar org-todo-line-regexp nil
  4015. "Matches a headline and puts TODO state into group 2 if present.")
  4016. (make-variable-buffer-local 'org-todo-line-regexp)
  4017. (defvar org-complex-heading-regexp nil
  4018. "Matches a headline and puts everything into groups:
  4019. group 1: the stars
  4020. group 2: The todo keyword, maybe
  4021. group 3: Priority cookie
  4022. group 4: True headline
  4023. group 5: Tags")
  4024. (make-variable-buffer-local 'org-complex-heading-regexp)
  4025. (defvar org-complex-heading-regexp-format nil
  4026. "Printf format to make regexp to match an exact headline.
  4027. This regexp will match the headline of any node which has the
  4028. exact headline text that is put into the format, but may have any
  4029. TODO state, priority and tags.")
  4030. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4031. (defvar org-todo-line-tags-regexp nil
  4032. "Matches a headline and puts TODO state into group 2 if present.
  4033. Also put tags into group 4 if tags are present.")
  4034. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4035. (defvar org-ds-keyword-length 12
  4036. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4037. (make-variable-buffer-local 'org-ds-keyword-length)
  4038. (defvar org-deadline-regexp nil
  4039. "Matches the DEADLINE keyword.")
  4040. (make-variable-buffer-local 'org-deadline-regexp)
  4041. (defvar org-deadline-time-regexp nil
  4042. "Matches the DEADLINE keyword together with a time stamp.")
  4043. (make-variable-buffer-local 'org-deadline-time-regexp)
  4044. (defvar org-deadline-time-hour-regexp nil
  4045. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4046. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4047. (defvar org-deadline-line-regexp nil
  4048. "Matches the DEADLINE keyword and the rest of the line.")
  4049. (make-variable-buffer-local 'org-deadline-line-regexp)
  4050. (defvar org-scheduled-regexp nil
  4051. "Matches the SCHEDULED keyword.")
  4052. (make-variable-buffer-local 'org-scheduled-regexp)
  4053. (defvar org-scheduled-time-regexp nil
  4054. "Matches the SCHEDULED keyword together with a time stamp.")
  4055. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4056. (defvar org-scheduled-time-hour-regexp nil
  4057. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4058. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4059. (defvar org-closed-time-regexp nil
  4060. "Matches the CLOSED keyword together with a time stamp.")
  4061. (make-variable-buffer-local 'org-closed-time-regexp)
  4062. (defvar org-keyword-time-regexp nil
  4063. "Matches any of the 4 keywords, together with the time stamp.")
  4064. (make-variable-buffer-local 'org-keyword-time-regexp)
  4065. (defvar org-keyword-time-not-clock-regexp nil
  4066. "Matches any of the 3 keywords, together with the time stamp.")
  4067. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4068. (defvar org-maybe-keyword-time-regexp nil
  4069. "Matches a timestamp, possibly preceded by a keyword.")
  4070. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4071. (defvar org-all-time-keywords nil
  4072. "List of time keywords.")
  4073. (make-variable-buffer-local 'org-all-time-keywords)
  4074. (defconst org-plain-time-of-day-regexp
  4075. (concat
  4076. "\\(\\<[012]?[0-9]"
  4077. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4078. "\\(--?"
  4079. "\\(\\<[012]?[0-9]"
  4080. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4081. "\\)?")
  4082. "Regular expression to match a plain time or time range.
  4083. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4084. groups carry important information:
  4085. 0 the full match
  4086. 1 the first time, range or not
  4087. 8 the second time, if it is a range.")
  4088. (defconst org-plain-time-extension-regexp
  4089. (concat
  4090. "\\(\\<[012]?[0-9]"
  4091. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4092. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4093. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4094. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4095. groups carry important information:
  4096. 0 the full match
  4097. 7 hours of duration
  4098. 9 minutes of duration")
  4099. (defconst org-stamp-time-of-day-regexp
  4100. (concat
  4101. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4102. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4103. "\\(--?"
  4104. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4105. "Regular expression to match a timestamp time or time range.
  4106. After a match, the following groups carry important information:
  4107. 0 the full match
  4108. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4109. 2 the first time, range or not
  4110. 4 the second time, if it is a range.")
  4111. (defconst org-startup-options
  4112. '(("fold" org-startup-folded t)
  4113. ("overview" org-startup-folded t)
  4114. ("nofold" org-startup-folded nil)
  4115. ("showall" org-startup-folded nil)
  4116. ("showeverything" org-startup-folded showeverything)
  4117. ("content" org-startup-folded content)
  4118. ("indent" org-startup-indented t)
  4119. ("noindent" org-startup-indented nil)
  4120. ("hidestars" org-hide-leading-stars t)
  4121. ("showstars" org-hide-leading-stars nil)
  4122. ("odd" org-odd-levels-only t)
  4123. ("oddeven" org-odd-levels-only nil)
  4124. ("align" org-startup-align-all-tables t)
  4125. ("noalign" org-startup-align-all-tables nil)
  4126. ("inlineimages" org-startup-with-inline-images t)
  4127. ("noinlineimages" org-startup-with-inline-images nil)
  4128. ("latexpreview" org-startup-with-latex-preview t)
  4129. ("nolatexpreview" org-startup-with-latex-preview nil)
  4130. ("customtime" org-display-custom-times t)
  4131. ("logdone" org-log-done time)
  4132. ("lognotedone" org-log-done note)
  4133. ("nologdone" org-log-done nil)
  4134. ("lognoteclock-out" org-log-note-clock-out t)
  4135. ("nolognoteclock-out" org-log-note-clock-out nil)
  4136. ("logrepeat" org-log-repeat state)
  4137. ("lognoterepeat" org-log-repeat note)
  4138. ("logdrawer" org-log-into-drawer t)
  4139. ("nologdrawer" org-log-into-drawer nil)
  4140. ("logstatesreversed" org-log-states-order-reversed t)
  4141. ("nologstatesreversed" org-log-states-order-reversed nil)
  4142. ("nologrepeat" org-log-repeat nil)
  4143. ("logreschedule" org-log-reschedule time)
  4144. ("lognotereschedule" org-log-reschedule note)
  4145. ("nologreschedule" org-log-reschedule nil)
  4146. ("logredeadline" org-log-redeadline time)
  4147. ("lognoteredeadline" org-log-redeadline note)
  4148. ("nologredeadline" org-log-redeadline nil)
  4149. ("logrefile" org-log-refile time)
  4150. ("lognoterefile" org-log-refile note)
  4151. ("nologrefile" org-log-refile nil)
  4152. ("fninline" org-footnote-define-inline t)
  4153. ("nofninline" org-footnote-define-inline nil)
  4154. ("fnlocal" org-footnote-section nil)
  4155. ("fnauto" org-footnote-auto-label t)
  4156. ("fnprompt" org-footnote-auto-label nil)
  4157. ("fnconfirm" org-footnote-auto-label confirm)
  4158. ("fnplain" org-footnote-auto-label plain)
  4159. ("fnadjust" org-footnote-auto-adjust t)
  4160. ("nofnadjust" org-footnote-auto-adjust nil)
  4161. ("constcgs" constants-unit-system cgs)
  4162. ("constSI" constants-unit-system SI)
  4163. ("noptag" org-tag-persistent-alist nil)
  4164. ("hideblocks" org-hide-block-startup t)
  4165. ("nohideblocks" org-hide-block-startup nil)
  4166. ("beamer" org-startup-with-beamer-mode t)
  4167. ("entitiespretty" org-pretty-entities t)
  4168. ("entitiesplain" org-pretty-entities nil))
  4169. "Variable associated with STARTUP options for org-mode.
  4170. Each element is a list of three items: the startup options (as written
  4171. in the #+STARTUP line), the corresponding variable, and the value to set
  4172. this variable to if the option is found. An optional forth element PUSH
  4173. means to push this value onto the list in the variable.")
  4174. (defun org-update-property-plist (key val props)
  4175. "Update PROPS with KEY and VAL."
  4176. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4177. (key (if appending (substring key 0 (- (length key) 1)) key))
  4178. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4179. (previous (cdr (assoc key props))))
  4180. (if appending
  4181. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4182. (cons (cons key val) remainder))))
  4183. (defconst org-block-regexp
  4184. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4185. "Regular expression for hiding blocks.")
  4186. (defconst org-heading-keyword-regexp-format
  4187. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4188. "Printf format for a regexp matching a headline with some keyword.
  4189. This regexp will match the headline of any node which has the
  4190. exact keyword that is put into the format. The keyword isn't in
  4191. any group by default, but the stars and the body are.")
  4192. (defconst org-heading-keyword-maybe-regexp-format
  4193. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4194. "Printf format for a regexp matching a headline, possibly with some keyword.
  4195. This regexp can match any headline with the specified keyword, or
  4196. without a keyword. The keyword isn't in any group by default,
  4197. but the stars and the body are.")
  4198. (defcustom org-group-tags t
  4199. "When non-nil (the default), use group tags.
  4200. This can be turned on/off through `org-toggle-tags-groups'."
  4201. :group 'org-tags
  4202. :group 'org-startup
  4203. :type 'boolean)
  4204. (defun org-toggle-tags-groups ()
  4205. "Toggle support for group tags.
  4206. Support for group tags is controlled by the option
  4207. `org-group-tags', which is non-nil by default."
  4208. (interactive)
  4209. (setq org-group-tags (not org-group-tags))
  4210. (cond ((and (derived-mode-p 'org-agenda-mode)
  4211. org-group-tags)
  4212. (org-agenda-redo))
  4213. ((derived-mode-p 'org-mode)
  4214. (let ((org-inhibit-startup t)) (org-mode))))
  4215. (message "Groups tags support has been turned %s"
  4216. (if org-group-tags "on" "off")))
  4217. (defun org-set-regexps-and-options-for-tags ()
  4218. "Precompute variables used for tags."
  4219. (when (derived-mode-p 'org-mode)
  4220. (org-set-local 'org-file-tags nil)
  4221. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4222. (splitre "[ \t]+")
  4223. (start 0)
  4224. tags ftags key value)
  4225. (save-excursion
  4226. (save-restriction
  4227. (widen)
  4228. (goto-char (point-min))
  4229. (while (re-search-forward re nil t)
  4230. (setq key (upcase (org-match-string-no-properties 1))
  4231. value (org-match-string-no-properties 2))
  4232. (if (stringp value) (setq value (org-trim value)))
  4233. (cond
  4234. ((equal key "TAGS")
  4235. (setq tags (append tags (if tags '("\\n") nil)
  4236. (org-split-string value splitre))))
  4237. ((equal key "FILETAGS")
  4238. (when (string-match "\\S-" value)
  4239. (setq ftags
  4240. (append
  4241. ftags
  4242. (apply 'append
  4243. (mapcar (lambda (x) (org-split-string x ":"))
  4244. (org-split-string value)))))))))))
  4245. ;; Process the file tags.
  4246. (and ftags (org-set-local 'org-file-tags
  4247. (mapcar 'org-add-prop-inherited ftags)))
  4248. (org-set-local 'org-tag-groups-alist nil)
  4249. ;; Process the tags.
  4250. (when (and (not tags) org-tag-alist)
  4251. (setq tags
  4252. (mapcar
  4253. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4254. ((eq (car tg) :endgroup) "}")
  4255. ((eq (car tg) :grouptags) ":")
  4256. ((eq (car tg) :newline) "\n")
  4257. (t (concat (car tg)
  4258. (if (characterp (cdr tg))
  4259. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4260. org-tag-alist)))
  4261. (let (e tgs g)
  4262. (while (setq e (pop tags))
  4263. (cond
  4264. ((equal e "{")
  4265. (progn (push '(:startgroup) tgs)
  4266. (when (equal (nth 1 tags) ":")
  4267. (push (list (replace-regexp-in-string
  4268. "(.+)$" "" (nth 0 tags)))
  4269. org-tag-groups-alist)
  4270. (setq g 0))))
  4271. ((equal e ":") (push '(:grouptags) tgs))
  4272. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4273. ((equal e "\\n") (push '(:newline) tgs))
  4274. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4275. (push (cons (match-string 1 e)
  4276. (string-to-char (match-string 2 e))) tgs)
  4277. (if (and g (> g 0))
  4278. (setcar org-tag-groups-alist
  4279. (append (car org-tag-groups-alist)
  4280. (list (match-string 1 e)))))
  4281. (if g (setq g (1+ g))))
  4282. (t (push (list e) tgs)
  4283. (if (and g (> g 0))
  4284. (setcar org-tag-groups-alist
  4285. (append (car org-tag-groups-alist) (list e))))
  4286. (if g (setq g (1+ g))))))
  4287. (org-set-local 'org-tag-alist nil)
  4288. (while (setq e (pop tgs))
  4289. (or (and (stringp (car e))
  4290. (assoc (car e) org-tag-alist))
  4291. (push e org-tag-alist)))
  4292. ;; Return a list with tag variables
  4293. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4294. (defvar org-ota nil)
  4295. (defun org-set-regexps-and-options ()
  4296. "Precompute regular expressions used in the current buffer."
  4297. (when (derived-mode-p 'org-mode)
  4298. (org-set-local 'org-todo-kwd-alist nil)
  4299. (org-set-local 'org-todo-key-alist nil)
  4300. (org-set-local 'org-todo-key-trigger nil)
  4301. (org-set-local 'org-todo-keywords-1 nil)
  4302. (org-set-local 'org-done-keywords nil)
  4303. (org-set-local 'org-todo-heads nil)
  4304. (org-set-local 'org-todo-sets nil)
  4305. (org-set-local 'org-todo-log-states nil)
  4306. (org-set-local 'org-file-properties nil)
  4307. (let ((re (org-make-options-regexp
  4308. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4309. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4310. "SETUPFILE" "OPTIONS")
  4311. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4312. (splitre "[ \t]+")
  4313. (scripts org-use-sub-superscripts)
  4314. kwds kws0 kwsa key log value cat arch const links hw dws
  4315. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4316. (start 0))
  4317. (save-excursion
  4318. (save-restriction
  4319. (widen)
  4320. (goto-char (point-min))
  4321. (while
  4322. (or (and
  4323. ext-setup-or-nil
  4324. (not org-ota)
  4325. (let (ret)
  4326. (with-temp-buffer
  4327. (insert ext-setup-or-nil)
  4328. (let ((major-mode 'org-mode) org-ota)
  4329. (setq ret (save-match-data
  4330. (org-set-regexps-and-options-for-tags)))))
  4331. ;; Append setupfile tags to existing tags
  4332. (setq org-ota t)
  4333. (setq org-file-tags
  4334. (delq nil (append org-file-tags (nth 0 ret)))
  4335. org-tag-alist
  4336. (delq nil (append org-tag-alist (nth 1 ret)))
  4337. org-tag-groups-alist
  4338. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4339. (and ext-setup-or-nil
  4340. (string-match re ext-setup-or-nil start)
  4341. (setq start (match-end 0)))
  4342. (and (setq ext-setup-or-nil nil start 0)
  4343. (re-search-forward re nil t)))
  4344. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4345. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4346. (if (stringp value) (setq value (org-trim value)))
  4347. (cond
  4348. ((equal key "CATEGORY")
  4349. (setq cat value))
  4350. ((member key '("SEQ_TODO" "TODO"))
  4351. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4352. ((equal key "TYP_TODO")
  4353. (push (cons 'type (org-split-string value splitre)) kwds))
  4354. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4355. ;; general TODO-like setup
  4356. (push (cons (intern (downcase (match-string 1 key)))
  4357. (org-split-string value splitre)) kwds))
  4358. ((equal key "COLUMNS")
  4359. (org-set-local 'org-columns-default-format value))
  4360. ((equal key "LINK")
  4361. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4362. (push (cons (match-string 1 value)
  4363. (org-trim (match-string 2 value)))
  4364. links)))
  4365. ((equal key "PRIORITIES")
  4366. (setq prio (org-split-string value " +")))
  4367. ((equal key "PROPERTY")
  4368. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4369. (setq props (org-update-property-plist (match-string 1 value)
  4370. (match-string 2 value)
  4371. props))))
  4372. ((equal key "CONSTANTS")
  4373. (org-table-set-constants))
  4374. ((equal key "STARTUP")
  4375. (let ((opts (org-split-string value splitre))
  4376. l var val)
  4377. (while (setq l (pop opts))
  4378. (when (setq l (assoc l org-startup-options))
  4379. (setq var (nth 1 l) val (nth 2 l))
  4380. (if (not (nth 3 l))
  4381. (set (make-local-variable var) val)
  4382. (if (not (listp (symbol-value var)))
  4383. (set (make-local-variable var) nil))
  4384. (set (make-local-variable var) (symbol-value var))
  4385. (add-to-list var val))))))
  4386. ((equal key "ARCHIVE")
  4387. (setq arch value)
  4388. (remove-text-properties 0 (length arch)
  4389. '(face t fontified t) arch))
  4390. ((equal key "OPTIONS")
  4391. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4392. (setq scripts (read (match-string 2 value)))))
  4393. ((and (equal key "SETUPFILE")
  4394. ;; Prevent checking in Gnus messages
  4395. (not buffer-read-only))
  4396. (setq setup-contents (org-file-contents
  4397. (expand-file-name
  4398. (org-remove-double-quotes value))
  4399. 'noerror))
  4400. (if (not ext-setup-or-nil)
  4401. (setq ext-setup-or-nil setup-contents start 0)
  4402. (setq ext-setup-or-nil
  4403. (concat (substring ext-setup-or-nil 0 start)
  4404. "\n" setup-contents "\n"
  4405. (substring ext-setup-or-nil start)))))))
  4406. ;; search for property blocks
  4407. (goto-char (point-min))
  4408. (while (re-search-forward org-block-regexp nil t)
  4409. (when (equal "PROPERTY" (upcase (match-string 1)))
  4410. (setq value (replace-regexp-in-string
  4411. "[\n\r]" " " (match-string 4)))
  4412. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4413. (setq props (org-update-property-plist (match-string 1 value)
  4414. (match-string 2 value)
  4415. props)))))))
  4416. (org-set-local 'org-use-sub-superscripts scripts)
  4417. (when cat
  4418. (org-set-local 'org-category (intern cat))
  4419. (push (cons "CATEGORY" cat) props))
  4420. (when prio
  4421. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4422. (setq prio (mapcar 'string-to-char prio))
  4423. (org-set-local 'org-highest-priority (nth 0 prio))
  4424. (org-set-local 'org-lowest-priority (nth 1 prio))
  4425. (org-set-local 'org-default-priority (nth 2 prio)))
  4426. (and props (org-set-local 'org-file-properties (nreverse props)))
  4427. (and arch (org-set-local 'org-archive-location arch))
  4428. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4429. ;; Process the TODO keywords
  4430. (unless kwds
  4431. ;; Use the global values as if they had been given locally.
  4432. (setq kwds (default-value 'org-todo-keywords))
  4433. (if (stringp (car kwds))
  4434. (setq kwds (list (cons org-todo-interpretation
  4435. (default-value 'org-todo-keywords)))))
  4436. (setq kwds (reverse kwds)))
  4437. (setq kwds (nreverse kwds))
  4438. (let (inter kws kw)
  4439. (while (setq kws (pop kwds))
  4440. (let ((kws (or
  4441. (run-hook-with-args-until-success
  4442. 'org-todo-setup-filter-hook kws)
  4443. kws)))
  4444. (setq inter (pop kws) sep (member "|" kws)
  4445. kws0 (delete "|" (copy-sequence kws))
  4446. kwsa nil
  4447. kws1 (mapcar
  4448. (lambda (x)
  4449. ;; 1 2
  4450. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4451. (progn
  4452. (setq kw (match-string 1 x)
  4453. key (and (match-end 2) (match-string 2 x))
  4454. log (org-extract-log-state-settings x))
  4455. (push (cons kw (and key (string-to-char key))) kwsa)
  4456. (and log (push log org-todo-log-states))
  4457. kw)
  4458. (error "Invalid TODO keyword %s" x)))
  4459. kws0)
  4460. kwsa (if kwsa (append '((:startgroup))
  4461. (nreverse kwsa)
  4462. '((:endgroup))))
  4463. hw (car kws1)
  4464. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4465. tail (list inter hw (car dws) (org-last dws))))
  4466. (add-to-list 'org-todo-heads hw 'append)
  4467. (push kws1 org-todo-sets)
  4468. (setq org-done-keywords (append org-done-keywords dws nil))
  4469. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4470. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4471. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4472. (setq org-todo-sets (nreverse org-todo-sets)
  4473. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4474. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4475. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4476. ;; Compute the regular expressions and other local variables.
  4477. ;; Using `org-outline-regexp-bol' would complicate them much,
  4478. ;; because of the fixed white space at the end of that string.
  4479. (if (not org-done-keywords)
  4480. (setq org-done-keywords (and org-todo-keywords-1
  4481. (list (org-last org-todo-keywords-1)))))
  4482. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4483. (length org-scheduled-string)
  4484. (length org-clock-string)
  4485. (length org-closed-string)))
  4486. org-not-done-keywords
  4487. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4488. org-todo-regexp
  4489. (concat "\\("
  4490. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4491. "\\)")
  4492. org-not-done-regexp
  4493. (concat "\\("
  4494. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4495. "\\)")
  4496. org-not-done-heading-regexp
  4497. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4498. org-todo-line-regexp
  4499. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4500. org-complex-heading-regexp
  4501. (concat "^\\(\\*+\\)"
  4502. "\\(?: +" org-todo-regexp "\\)?"
  4503. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4504. "\\(?: +\\(.*?\\)\\)??"
  4505. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4506. "[ \t]*$")
  4507. org-complex-heading-regexp-format
  4508. (concat "^\\(\\*+\\)"
  4509. "\\(?: +" org-todo-regexp "\\)?"
  4510. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4511. "\\(?: +"
  4512. ;; Stats cookies can be stuck to body.
  4513. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4514. "\\(%s\\)"
  4515. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4516. "\\)"
  4517. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4518. "[ \t]*$")
  4519. org-todo-line-tags-regexp
  4520. (concat "^\\(\\*+\\)"
  4521. "\\(?: +" org-todo-regexp "\\)?"
  4522. "\\(?: +\\(.*?\\)\\)??"
  4523. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4524. "[ \t]*$")
  4525. org-deadline-regexp (concat "\\<" org-deadline-string)
  4526. org-deadline-time-regexp
  4527. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4528. org-deadline-time-hour-regexp
  4529. (concat "\\<" org-deadline-string
  4530. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4531. org-deadline-line-regexp
  4532. (concat "\\<\\(" org-deadline-string "\\).*")
  4533. org-scheduled-regexp
  4534. (concat "\\<" org-scheduled-string)
  4535. org-scheduled-time-regexp
  4536. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4537. org-scheduled-time-hour-regexp
  4538. (concat "\\<" org-scheduled-string
  4539. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4540. org-closed-time-regexp
  4541. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4542. org-keyword-time-regexp
  4543. (concat "\\<\\(" org-scheduled-string
  4544. "\\|" org-deadline-string
  4545. "\\|" org-closed-string
  4546. "\\|" org-clock-string "\\)"
  4547. " *[[<]\\([^]>]+\\)[]>]")
  4548. org-keyword-time-not-clock-regexp
  4549. (concat "\\<\\(" org-scheduled-string
  4550. "\\|" org-deadline-string
  4551. "\\|" org-closed-string
  4552. "\\)"
  4553. " *[[<]\\([^]>]+\\)[]>]")
  4554. org-maybe-keyword-time-regexp
  4555. (concat "\\(\\<\\(" org-scheduled-string
  4556. "\\|" org-deadline-string
  4557. "\\|" org-closed-string
  4558. "\\|" org-clock-string "\\)\\)?"
  4559. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4560. org-all-time-keywords
  4561. (mapcar (lambda (w) (substring w 0 -1))
  4562. (list org-scheduled-string org-deadline-string
  4563. org-clock-string org-closed-string)))
  4564. (setq org-ota nil)
  4565. (org-compute-latex-and-related-regexp))))
  4566. (defun org-file-contents (file &optional noerror)
  4567. "Return the contents of FILE, as a string."
  4568. (if (or (not file)
  4569. (not (file-readable-p file)))
  4570. (if noerror
  4571. (message "Cannot read file \"%s\"" file)
  4572. (error "Cannot read file \"%s\"" file))
  4573. (with-temp-buffer
  4574. (insert-file-contents file)
  4575. (buffer-string))))
  4576. (defun org-extract-log-state-settings (x)
  4577. "Extract the log state setting from a TODO keyword string.
  4578. This will extract info from a string like \"WAIT(w@/!)\"."
  4579. (let (kw key log1 log2)
  4580. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4581. (setq kw (match-string 1 x)
  4582. key (and (match-end 2) (match-string 2 x))
  4583. log1 (and (match-end 3) (match-string 3 x))
  4584. log2 (and (match-end 4) (match-string 4 x)))
  4585. (and (or log1 log2)
  4586. (list kw
  4587. (and log1 (if (equal log1 "!") 'time 'note))
  4588. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4589. (defun org-remove-keyword-keys (list)
  4590. "Remove a pair of parenthesis at the end of each string in LIST."
  4591. (mapcar (lambda (x)
  4592. (if (string-match "(.*)$" x)
  4593. (substring x 0 (match-beginning 0))
  4594. x))
  4595. list))
  4596. (defun org-assign-fast-keys (alist)
  4597. "Assign fast keys to a keyword-key alist.
  4598. Respect keys that are already there."
  4599. (let (new e (alt ?0))
  4600. (while (setq e (pop alist))
  4601. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4602. (cdr e)) ;; Key already assigned.
  4603. (push e new)
  4604. (let ((clist (string-to-list (downcase (car e))))
  4605. (used (append new alist)))
  4606. (when (= (car clist) ?@)
  4607. (pop clist))
  4608. (while (and clist (rassoc (car clist) used))
  4609. (pop clist))
  4610. (unless clist
  4611. (while (rassoc alt used)
  4612. (incf alt)))
  4613. (push (cons (car e) (or (car clist) alt)) new))))
  4614. (nreverse new)))
  4615. ;;; Some variables used in various places
  4616. (defvar org-window-configuration nil
  4617. "Used in various places to store a window configuration.")
  4618. (defvar org-selected-window nil
  4619. "Used in various places to store a window configuration.")
  4620. (defvar org-finish-function nil
  4621. "Function to be called when `C-c C-c' is used.
  4622. This is for getting out of special buffers like capture.")
  4623. ;; FIXME: Occasionally check by commenting these, to make sure
  4624. ;; no other functions uses these, forgetting to let-bind them.
  4625. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4626. (defvar org-last-state)
  4627. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4628. ;; Defined somewhere in this file, but used before definition.
  4629. (defvar org-entities) ;; defined in org-entities.el
  4630. (defvar org-struct-menu)
  4631. (defvar org-org-menu)
  4632. (defvar org-tbl-menu)
  4633. ;;;; Define the Org-mode
  4634. ;; We use a before-change function to check if a table might need
  4635. ;; an update.
  4636. (defvar org-table-may-need-update t
  4637. "Indicates that a table might need an update.
  4638. This variable is set by `org-before-change-function'.
  4639. `org-table-align' sets it back to nil.")
  4640. (defun org-before-change-function (beg end)
  4641. "Every change indicates that a table might need an update."
  4642. (setq org-table-may-need-update t))
  4643. (defvar org-mode-map)
  4644. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4645. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4646. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4647. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4648. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4649. (defvar org-table-buffer-is-an nil)
  4650. (defvar bidi-paragraph-direction)
  4651. (defvar buffer-face-mode-face)
  4652. (require 'outline)
  4653. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4654. (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"))
  4655. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4656. ;; Other stuff we need.
  4657. (require 'time-date)
  4658. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4659. (require 'easymenu)
  4660. (require 'overlay)
  4661. ;; (require 'org-macs) moved higher up in the file before it is first used
  4662. (require 'org-entities)
  4663. ;; (require 'org-compat) moved higher up in the file before it is first used
  4664. (require 'org-faces)
  4665. (require 'org-list)
  4666. (require 'org-pcomplete)
  4667. (require 'org-src)
  4668. (require 'org-footnote)
  4669. (require 'org-macro)
  4670. ;; babel
  4671. (require 'ob)
  4672. ;;;###autoload
  4673. (define-derived-mode org-mode outline-mode "Org"
  4674. "Outline-based notes management and organizer, alias
  4675. \"Carsten's outline-mode for keeping track of everything.\"
  4676. Org-mode develops organizational tasks around a NOTES file which
  4677. contains information about projects as plain text. Org-mode is
  4678. implemented on top of outline-mode, which is ideal to keep the content
  4679. of large files well structured. It supports ToDo items, deadlines and
  4680. time stamps, which magically appear in the diary listing of the Emacs
  4681. calendar. Tables are easily created with a built-in table editor.
  4682. Plain text URL-like links connect to websites, emails (VM), Usenet
  4683. messages (Gnus), BBDB entries, and any files related to the project.
  4684. For printing and sharing of notes, an Org-mode file (or a part of it)
  4685. can be exported as a structured ASCII or HTML file.
  4686. The following commands are available:
  4687. \\{org-mode-map}"
  4688. ;; Get rid of Outline menus, they are not needed
  4689. ;; Need to do this here because define-derived-mode sets up
  4690. ;; the keymap so late. Still, it is a waste to call this each time
  4691. ;; we switch another buffer into org-mode.
  4692. (if (featurep 'xemacs)
  4693. (when (boundp 'outline-mode-menu-heading)
  4694. ;; Assume this is Greg's port, it uses easymenu
  4695. (easy-menu-remove outline-mode-menu-heading)
  4696. (easy-menu-remove outline-mode-menu-show)
  4697. (easy-menu-remove outline-mode-menu-hide))
  4698. (define-key org-mode-map [menu-bar headings] 'undefined)
  4699. (define-key org-mode-map [menu-bar hide] 'undefined)
  4700. (define-key org-mode-map [menu-bar show] 'undefined))
  4701. (org-load-modules-maybe)
  4702. (easy-menu-add org-org-menu)
  4703. (easy-menu-add org-tbl-menu)
  4704. (org-install-agenda-files-menu)
  4705. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4706. (add-to-invisibility-spec '(org-cwidth))
  4707. (add-to-invisibility-spec '(org-hide-block . t))
  4708. (when (featurep 'xemacs)
  4709. (org-set-local 'line-move-ignore-invisible t))
  4710. (org-set-local 'outline-regexp org-outline-regexp)
  4711. (org-set-local 'outline-level 'org-outline-level)
  4712. (setq bidi-paragraph-direction 'left-to-right)
  4713. (when (and org-ellipsis
  4714. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4715. (fboundp 'make-glyph-code))
  4716. (unless org-display-table
  4717. (setq org-display-table (make-display-table)))
  4718. (set-display-table-slot
  4719. org-display-table 4
  4720. (vconcat (mapcar
  4721. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4722. org-ellipsis)))
  4723. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4724. (setq buffer-display-table org-display-table))
  4725. (org-set-regexps-and-options-for-tags)
  4726. (org-set-regexps-and-options)
  4727. (org-set-font-lock-defaults)
  4728. (when (and org-tag-faces (not org-tags-special-faces-re))
  4729. ;; tag faces set outside customize.... force initialization.
  4730. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4731. ;; Calc embedded
  4732. (org-set-local 'calc-embedded-open-mode "# ")
  4733. ;; Modify a few syntax entries
  4734. (modify-syntax-entry ?@ "w")
  4735. (modify-syntax-entry ?\" "\"")
  4736. (if org-startup-truncated (setq truncate-lines t))
  4737. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4738. (org-set-local 'font-lock-unfontify-region-function
  4739. 'org-unfontify-region)
  4740. ;; Activate before-change-function
  4741. (org-set-local 'org-table-may-need-update t)
  4742. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4743. 'local)
  4744. ;; Check for running clock before killing a buffer
  4745. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4746. ;; Initialize macros templates.
  4747. (org-macro-initialize-templates)
  4748. ;; Initialize radio targets.
  4749. (org-update-radio-target-regexp)
  4750. ;; Indentation.
  4751. (org-set-local 'indent-line-function 'org-indent-line)
  4752. (org-set-local 'indent-region-function 'org-indent-region)
  4753. ;; Filling and auto-filling.
  4754. (org-setup-filling)
  4755. ;; Comments.
  4756. (org-setup-comments-handling)
  4757. ;; Initialize cache.
  4758. (org-element-cache-reset)
  4759. ;; Beginning/end of defun
  4760. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4761. (org-set-local 'end-of-defun-function 'org-forward-element)
  4762. ;; Next error for sparse trees
  4763. (org-set-local 'next-error-function 'org-occur-next-match)
  4764. ;; Make sure dependence stuff works reliably, even for users who set it
  4765. ;; too late :-(
  4766. (if org-enforce-todo-dependencies
  4767. (add-hook 'org-blocker-hook
  4768. 'org-block-todo-from-children-or-siblings-or-parent)
  4769. (remove-hook 'org-blocker-hook
  4770. 'org-block-todo-from-children-or-siblings-or-parent))
  4771. (if org-enforce-todo-checkbox-dependencies
  4772. (add-hook 'org-blocker-hook
  4773. 'org-block-todo-from-checkboxes)
  4774. (remove-hook 'org-blocker-hook
  4775. 'org-block-todo-from-checkboxes))
  4776. ;; Align options lines
  4777. (org-set-local
  4778. 'align-mode-rules-list
  4779. '((org-in-buffer-settings
  4780. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4781. (modes . '(org-mode)))))
  4782. ;; Imenu
  4783. (org-set-local 'imenu-create-index-function
  4784. 'org-imenu-get-tree)
  4785. ;; Make isearch reveal context
  4786. (if (or (featurep 'xemacs)
  4787. (not (boundp 'outline-isearch-open-invisible-function)))
  4788. ;; Emacs 21 and XEmacs make use of the hook
  4789. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4790. ;; Emacs 22 deals with this through a special variable
  4791. (org-set-local 'outline-isearch-open-invisible-function
  4792. (lambda (&rest ignore) (org-show-context 'isearch)))
  4793. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4794. ;; Setup the pcomplete hooks
  4795. (set (make-local-variable 'pcomplete-command-completion-function)
  4796. 'org-pcomplete-initial)
  4797. (set (make-local-variable 'pcomplete-command-name-function)
  4798. 'org-command-at-point)
  4799. (set (make-local-variable 'pcomplete-default-completion-function)
  4800. 'ignore)
  4801. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4802. 'org-parse-arguments)
  4803. (set (make-local-variable 'pcomplete-termination-string) "")
  4804. (when (>= emacs-major-version 23)
  4805. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4806. ;; If empty file that did not turn on org-mode automatically, make it to.
  4807. (if (and org-insert-mode-line-in-empty-file
  4808. (org-called-interactively-p 'any)
  4809. (= (point-min) (point-max)))
  4810. (insert "# -*- mode: org -*-\n\n"))
  4811. (unless org-inhibit-startup
  4812. (org-unmodified
  4813. (and org-startup-with-beamer-mode (org-beamer-mode))
  4814. (when org-startup-align-all-tables
  4815. (org-table-map-tables 'org-table-align 'quietly))
  4816. (when org-startup-with-inline-images
  4817. (org-display-inline-images))
  4818. (when org-startup-with-latex-preview
  4819. (org-preview-latex-fragment))
  4820. (unless org-inhibit-startup-visibility-stuff
  4821. (org-set-startup-visibility))))
  4822. ;; Try to set org-hide correctly
  4823. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4824. ;; Update `customize-package-emacs-version-alist'
  4825. (add-to-list 'customize-package-emacs-version-alist
  4826. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4827. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4828. ("8.0" . "24.4")))
  4829. (defvar org-mode-transpose-word-syntax-table
  4830. (let ((st (make-syntax-table)))
  4831. (mapc (lambda(c) (modify-syntax-entry
  4832. (string-to-char (car c)) "w p" st))
  4833. org-emphasis-alist)
  4834. st))
  4835. (when (fboundp 'abbrev-table-put)
  4836. (abbrev-table-put org-mode-abbrev-table
  4837. :parents (list text-mode-abbrev-table)))
  4838. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4839. (defsubst org-fix-ellipsis-at-bol ()
  4840. (save-excursion (goto-char (window-start)) (recenter 0)))
  4841. (defun org-find-invisible-foreground ()
  4842. (let ((candidates (remove
  4843. "unspecified-bg"
  4844. (nconc
  4845. (list (face-background 'default)
  4846. (face-background 'org-default))
  4847. (mapcar
  4848. (lambda (alist)
  4849. (when (boundp alist)
  4850. (cdr (assoc 'background-color (symbol-value alist)))))
  4851. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4852. (list (face-foreground 'org-hide))))))
  4853. (car (remove nil candidates))))
  4854. (defun org-current-time (&optional rounding-minutes past)
  4855. "Current time, possibly rounded to ROUNDING-MINUTES.
  4856. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4857. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4858. the rounding returns a past time."
  4859. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4860. (car org-time-stamp-rounding-minutes)))
  4861. (time (decode-time)) res)
  4862. (if (< r 1)
  4863. (current-time)
  4864. (setq res
  4865. (apply 'encode-time
  4866. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4867. (nthcdr 2 time))))
  4868. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4869. (seconds-to-time (- (org-float-time res) (* r 60)))
  4870. res))))
  4871. (defun org-today ()
  4872. "Return today date, considering `org-extend-today-until'."
  4873. (time-to-days
  4874. (time-subtract (current-time)
  4875. (list 0 (* 3600 org-extend-today-until) 0))))
  4876. ;;;; Font-Lock stuff, including the activators
  4877. (defvar org-mouse-map (make-sparse-keymap))
  4878. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4879. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4880. (when org-mouse-1-follows-link
  4881. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4882. (when org-tab-follows-link
  4883. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4884. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4885. (require 'font-lock)
  4886. (defconst org-non-link-chars "]\t\n\r<>")
  4887. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4888. "shell" "elisp" "doi" "message"))
  4889. (defvar org-link-types-re nil
  4890. "Matches a link that has a url-like prefix like \"http:\"")
  4891. (defvar org-link-re-with-space nil
  4892. "Matches a link with spaces, optional angular brackets around it.")
  4893. (defvar org-link-re-with-space2 nil
  4894. "Matches a link with spaces, optional angular brackets around it.")
  4895. (defvar org-link-re-with-space3 nil
  4896. "Matches a link with spaces, only for internal part in bracket links.")
  4897. (defvar org-angle-link-re nil
  4898. "Matches link with angular brackets, spaces are allowed.")
  4899. (defvar org-plain-link-re nil
  4900. "Matches plain link, without spaces.")
  4901. (defvar org-bracket-link-regexp nil
  4902. "Matches a link in double brackets.")
  4903. (defvar org-bracket-link-analytic-regexp nil
  4904. "Regular expression used to analyze links.
  4905. Here is what the match groups contain after a match:
  4906. 1: http:
  4907. 2: http
  4908. 3: path
  4909. 4: [desc]
  4910. 5: desc")
  4911. (defvar org-bracket-link-analytic-regexp++ nil
  4912. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4913. (defvar org-any-link-re nil
  4914. "Regular expression matching any link.")
  4915. (defconst org-match-sexp-depth 3
  4916. "Number of stacked braces for sub/superscript matching.")
  4917. (defun org-create-multibrace-regexp (left right n)
  4918. "Create a regular expression which will match a balanced sexp.
  4919. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4920. as single character strings.
  4921. The regexp returned will match the entire expression including the
  4922. delimiters. It will also define a single group which contains the
  4923. match except for the outermost delimiters. The maximum depth of
  4924. stacked delimiters is N. Escaping delimiters is not possible."
  4925. (let* ((nothing (concat "[^" left right "]*?"))
  4926. (or "\\|")
  4927. (re nothing)
  4928. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4929. (while (> n 1)
  4930. (setq n (1- n)
  4931. re (concat re or next)
  4932. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4933. (concat left "\\(" re "\\)" right)))
  4934. (defvar org-match-substring-regexp
  4935. (concat
  4936. "\\(\\S-\\)\\([_^]\\)\\("
  4937. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4938. "\\|"
  4939. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4940. "\\|"
  4941. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4942. "The regular expression matching a sub- or superscript.")
  4943. (defvar org-match-substring-with-braces-regexp
  4944. (concat
  4945. "\\(\\S-\\)\\([_^]\\)\\("
  4946. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4947. "\\)")
  4948. "The regular expression matching a sub- or superscript, forcing braces.")
  4949. (defun org-make-link-regexps ()
  4950. "Update the link regular expressions.
  4951. This should be called after the variable `org-link-types' has changed."
  4952. (setq org-link-types-re
  4953. (concat
  4954. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4955. org-link-re-with-space
  4956. (concat
  4957. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. "\\([^" org-non-link-chars " ]"
  4959. "[^" org-non-link-chars "]*"
  4960. "[^" org-non-link-chars " ]\\)>?")
  4961. org-link-re-with-space2
  4962. (concat
  4963. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4964. "\\([^" org-non-link-chars " ]"
  4965. "[^\t\n\r]*"
  4966. "[^" org-non-link-chars " ]\\)>?")
  4967. org-link-re-with-space3
  4968. (concat
  4969. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4970. "\\([^" org-non-link-chars " ]"
  4971. "[^\t\n\r]*\\)")
  4972. org-angle-link-re
  4973. (concat
  4974. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4975. "\\([^" org-non-link-chars " ]"
  4976. "[^" org-non-link-chars "]*"
  4977. "\\)>")
  4978. org-plain-link-re
  4979. (concat
  4980. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4981. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4982. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4983. org-bracket-link-regexp
  4984. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4985. org-bracket-link-analytic-regexp
  4986. (concat
  4987. "\\[\\["
  4988. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4989. "\\([^]]+\\)"
  4990. "\\]"
  4991. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4992. "\\]")
  4993. org-bracket-link-analytic-regexp++
  4994. (concat
  4995. "\\[\\["
  4996. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4997. "\\([^]]+\\)"
  4998. "\\]"
  4999. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5000. "\\]")
  5001. org-any-link-re
  5002. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5003. org-angle-link-re "\\)\\|\\("
  5004. org-plain-link-re "\\)")))
  5005. (org-make-link-regexps)
  5006. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5007. "Regular expression for fast time stamp matching.")
  5008. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5009. "Regular expression for fast time stamp matching.")
  5010. (defconst org-ts-regexp0
  5011. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5012. "Regular expression matching time strings for analysis.
  5013. This one does not require the space after the date, so it can be used
  5014. on a string that terminates immediately after the date.")
  5015. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5016. "Regular expression matching time strings for analysis.")
  5017. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5018. "Regular expression matching time stamps, with groups.")
  5019. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5020. "Regular expression matching time stamps (also [..]), with groups.")
  5021. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5022. "Regular expression matching a time stamp range.")
  5023. (defconst org-tr-regexp-both
  5024. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5025. "Regular expression matching a time stamp range.")
  5026. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5027. org-ts-regexp "\\)?")
  5028. "Regular expression matching a time stamp or time stamp range.")
  5029. (defconst org-tsr-regexp-both
  5030. (concat org-ts-regexp-both "\\(--?-?"
  5031. org-ts-regexp-both "\\)?")
  5032. "Regular expression matching a time stamp or time stamp range.
  5033. The time stamps may be either active or inactive.")
  5034. (defvar org-emph-face nil)
  5035. (defun org-do-emphasis-faces (limit)
  5036. "Run through the buffer and add overlays to emphasized strings."
  5037. (let (rtn a)
  5038. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5039. (if (not (= (char-after (match-beginning 3))
  5040. (char-after (match-beginning 4))))
  5041. (progn
  5042. (setq rtn t)
  5043. (setq a (assoc (match-string 3) org-emphasis-alist))
  5044. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5045. 'face
  5046. (nth 1 a))
  5047. (and (nth 2 a)
  5048. (org-remove-flyspell-overlays-in
  5049. (match-beginning 0) (match-end 0)))
  5050. (add-text-properties (match-beginning 2) (match-end 2)
  5051. '(font-lock-multiline t org-emphasis t))
  5052. (when org-hide-emphasis-markers
  5053. (add-text-properties (match-end 4) (match-beginning 5)
  5054. '(invisible org-link))
  5055. (add-text-properties (match-beginning 3) (match-end 3)
  5056. '(invisible org-link)))))
  5057. (backward-char 1))
  5058. rtn))
  5059. (defun org-emphasize (&optional char)
  5060. "Insert or change an emphasis, i.e. a font like bold or italic.
  5061. If there is an active region, change that region to a new emphasis.
  5062. If there is no region, just insert the marker characters and position
  5063. the cursor between them.
  5064. CHAR should be the marker character. If it is a space, it means to
  5065. remove the emphasis of the selected region.
  5066. If CHAR is not given (for example in an interactive call) it will be
  5067. prompted for."
  5068. (interactive)
  5069. (let ((erc org-emphasis-regexp-components)
  5070. (prompt "")
  5071. (string "") beg end move c s)
  5072. (if (org-region-active-p)
  5073. (setq beg (region-beginning) end (region-end)
  5074. string (buffer-substring beg end))
  5075. (setq move t))
  5076. (unless char
  5077. (message "Emphasis marker or tag: [%s]"
  5078. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5079. (setq char (read-char-exclusive)))
  5080. (if (equal char ?\ )
  5081. (setq s "" move nil)
  5082. (unless (assoc (char-to-string char) org-emphasis-alist)
  5083. (user-error "No such emphasis marker: \"%c\"" char))
  5084. (setq s (char-to-string char)))
  5085. (while (and (> (length string) 1)
  5086. (equal (substring string 0 1) (substring string -1))
  5087. (assoc (substring string 0 1) org-emphasis-alist))
  5088. (setq string (substring string 1 -1)))
  5089. (setq string (concat s string s))
  5090. (if beg (delete-region beg end))
  5091. (unless (or (bolp)
  5092. (string-match (concat "[" (nth 0 erc) "\n]")
  5093. (char-to-string (char-before (point)))))
  5094. (insert " "))
  5095. (unless (or (eobp)
  5096. (string-match (concat "[" (nth 1 erc) "\n]")
  5097. (char-to-string (char-after (point)))))
  5098. (insert " ") (backward-char 1))
  5099. (insert string)
  5100. (and move (backward-char 1))))
  5101. (defconst org-nonsticky-props
  5102. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5103. (defsubst org-rear-nonsticky-at (pos)
  5104. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5105. (defun org-activate-plain-links (limit)
  5106. "Run through the buffer and add overlays to links."
  5107. (let (f hl)
  5108. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5109. (not (org-in-src-block-p)))
  5110. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5111. (setq f (get-text-property (match-beginning 0) 'face))
  5112. (setq hl (org-match-string-no-properties 0))
  5113. (if (or (eq f 'org-tag)
  5114. (and (listp f) (memq 'org-tag f)))
  5115. nil
  5116. (add-text-properties (match-beginning 0) (match-end 0)
  5117. (list 'mouse-face 'highlight
  5118. 'face 'org-link
  5119. 'htmlize-link `(:uri ,hl)
  5120. 'keymap org-mouse-map))
  5121. (org-rear-nonsticky-at (match-end 0)))
  5122. t)))
  5123. (defun org-activate-code (limit)
  5124. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5125. (progn
  5126. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5127. (remove-text-properties (match-beginning 0) (match-end 0)
  5128. '(display t invisible t intangible t))
  5129. t)))
  5130. (defcustom org-src-fontify-natively nil
  5131. "When non-nil, fontify code in code blocks."
  5132. :type 'boolean
  5133. :version "24.1"
  5134. :group 'org-appearance
  5135. :group 'org-babel)
  5136. (defcustom org-allow-promoting-top-level-subtree nil
  5137. "When non-nil, allow promoting a top level subtree.
  5138. The leading star of the top level headline will be replaced
  5139. by a #."
  5140. :type 'boolean
  5141. :version "24.1"
  5142. :group 'org-appearance)
  5143. (defun org-fontify-meta-lines-and-blocks (limit)
  5144. (condition-case nil
  5145. (org-fontify-meta-lines-and-blocks-1 limit)
  5146. (error (message "org-mode fontification error"))))
  5147. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5148. "Fontify #+ lines and blocks."
  5149. (let ((case-fold-search t))
  5150. (if (re-search-forward
  5151. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5152. limit t)
  5153. (let ((beg (match-beginning 0))
  5154. (block-start (match-end 0))
  5155. (block-end nil)
  5156. (lang (match-string 7))
  5157. (beg1 (line-beginning-position 2))
  5158. (dc1 (downcase (match-string 2)))
  5159. (dc3 (downcase (match-string 3)))
  5160. end end1 quoting block-type ovl)
  5161. (cond
  5162. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5163. ;; a single line of backend-specific content
  5164. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5165. (remove-text-properties (match-beginning 0) (match-end 0)
  5166. '(display t invisible t intangible t))
  5167. (add-text-properties (match-beginning 1) (match-end 3)
  5168. '(font-lock-fontified t face org-meta-line))
  5169. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5170. '(font-lock-fontified t face org-block))
  5171. ; for backend-specific code
  5172. t)
  5173. ((and (match-end 4) (equal dc3 "+begin"))
  5174. ;; Truly a block
  5175. (setq block-type (downcase (match-string 5))
  5176. quoting (member block-type org-protecting-blocks))
  5177. (when (re-search-forward
  5178. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5179. nil t) ;; on purpose, we look further than LIMIT
  5180. (setq end (min (point-max) (match-end 0))
  5181. end1 (min (point-max) (1- (match-beginning 0))))
  5182. (setq block-end (match-beginning 0))
  5183. (when quoting
  5184. (remove-text-properties beg end
  5185. '(display t invisible t intangible t)))
  5186. (add-text-properties
  5187. beg end
  5188. '(font-lock-fontified t font-lock-multiline t))
  5189. (add-text-properties beg beg1 '(face org-meta-line))
  5190. (add-text-properties end1 (min (point-max) (1+ end))
  5191. '(face org-meta-line)) ; for end_src
  5192. (cond
  5193. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5194. (org-src-font-lock-fontify-block lang block-start block-end)
  5195. ;; remove old background overlays
  5196. (mapc (lambda (ov)
  5197. (if (eq (overlay-get ov 'face) 'org-block-background)
  5198. (delete-overlay ov)))
  5199. (overlays-at (/ (+ beg1 block-end) 2)))
  5200. ;; add a background overlay
  5201. (setq ovl (make-overlay beg1 block-end))
  5202. (overlay-put ovl 'face 'org-block-background)
  5203. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5204. (quoting
  5205. (add-text-properties beg1 (min (point-max) (1+ end1))
  5206. '(face org-block))) ; end of source block
  5207. ((not org-fontify-quote-and-verse-blocks))
  5208. ((string= block-type "quote")
  5209. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5210. ((string= block-type "verse")
  5211. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5212. (add-text-properties beg beg1 '(face org-block-begin-line))
  5213. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5214. '(face org-block-end-line))
  5215. t))
  5216. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5217. (add-text-properties
  5218. beg (match-end 3)
  5219. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5220. '(font-lock-fontified t invisible t)
  5221. '(font-lock-fontified t face org-document-info-keyword)))
  5222. (add-text-properties
  5223. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5224. (if (string-equal dc1 "+title:")
  5225. '(font-lock-fontified t face org-document-title)
  5226. '(font-lock-fontified t face org-document-info))))
  5227. ((or (equal dc1 "+results")
  5228. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5229. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5230. "+call:" "+header:" "+headers:" "+name:"))
  5231. (and (match-end 4) (equal dc3 "+attr")))
  5232. (add-text-properties
  5233. beg (match-end 0)
  5234. '(font-lock-fontified t face org-meta-line))
  5235. t)
  5236. ((member dc3 '(" " ""))
  5237. (add-text-properties
  5238. beg (match-end 0)
  5239. '(font-lock-fontified t face font-lock-comment-face)))
  5240. ((not (member (char-after beg) '(?\ ?\t)))
  5241. ;; just any other in-buffer setting, but not indented
  5242. (add-text-properties
  5243. beg (match-end 0)
  5244. '(font-lock-fontified t face org-meta-line))
  5245. t)
  5246. (t nil))))))
  5247. (defun org-activate-angle-links (limit)
  5248. "Run through the buffer and add overlays to links."
  5249. (if (and (re-search-forward org-angle-link-re limit t)
  5250. (not (org-in-src-block-p)))
  5251. (progn
  5252. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5253. (add-text-properties (match-beginning 0) (match-end 0)
  5254. (list 'mouse-face 'highlight
  5255. 'keymap org-mouse-map))
  5256. (org-rear-nonsticky-at (match-end 0))
  5257. t)))
  5258. (defun org-activate-footnote-links (limit)
  5259. "Run through the buffer and add overlays to footnotes."
  5260. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5261. (when fn
  5262. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5263. (org-remove-flyspell-overlays-in beg end)
  5264. (add-text-properties beg end
  5265. (list 'mouse-face 'highlight
  5266. 'keymap org-mouse-map
  5267. 'help-echo
  5268. (if (= (point-at-bol) beg)
  5269. "Footnote definition"
  5270. "Footnote reference")
  5271. 'font-lock-fontified t
  5272. 'font-lock-multiline t
  5273. 'face 'org-footnote))))))
  5274. (defun org-activate-bracket-links (limit)
  5275. "Run through the buffer and add overlays to bracketed links."
  5276. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5277. (not (org-in-src-block-p)))
  5278. (let* ((hl (org-match-string-no-properties 1))
  5279. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5280. (ip (org-maybe-intangible
  5281. (list 'invisible 'org-link
  5282. 'keymap org-mouse-map 'mouse-face 'highlight
  5283. 'font-lock-multiline t 'help-echo help
  5284. 'htmlize-link `(:uri ,hl))))
  5285. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5286. 'font-lock-multiline t 'help-echo help
  5287. 'htmlize-link `(:uri ,hl))))
  5288. ;; We need to remove the invisible property here. Table narrowing
  5289. ;; may have made some of this invisible.
  5290. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5291. (remove-text-properties (match-beginning 0) (match-end 0)
  5292. '(invisible nil))
  5293. (if (match-end 3)
  5294. (progn
  5295. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5296. (org-rear-nonsticky-at (match-beginning 3))
  5297. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5298. (org-rear-nonsticky-at (match-end 3))
  5299. (add-text-properties (match-end 3) (match-end 0) ip)
  5300. (org-rear-nonsticky-at (match-end 0)))
  5301. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5302. (org-rear-nonsticky-at (match-beginning 1))
  5303. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5304. (org-rear-nonsticky-at (match-end 1))
  5305. (add-text-properties (match-end 1) (match-end 0) ip)
  5306. (org-rear-nonsticky-at (match-end 0)))
  5307. t)))
  5308. (defun org-activate-dates (limit)
  5309. "Run through the buffer and add overlays to dates."
  5310. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5311. (not (equal (char-before (match-beginning 0)) 91)))
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map))
  5317. (org-rear-nonsticky-at (match-end 0))
  5318. (when org-display-custom-times
  5319. (if (match-end 3)
  5320. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5321. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5322. t)))
  5323. (defvar org-target-link-regexp nil
  5324. "Regular expression matching radio targets in plain text.")
  5325. (make-variable-buffer-local 'org-target-link-regexp)
  5326. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5327. "Regular expression matching a link target.")
  5328. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5329. "Regular expression matching a radio target.")
  5330. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5331. "Regular expression matching any target.")
  5332. (defun org-activate-target-links (limit)
  5333. "Run through the buffer and add overlays to target matches."
  5334. (when org-target-link-regexp
  5335. (let ((case-fold-search t))
  5336. (if (re-search-forward org-target-link-regexp limit t)
  5337. (progn
  5338. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5339. (add-text-properties (match-beginning 0) (match-end 0)
  5340. (list 'mouse-face 'highlight
  5341. 'keymap org-mouse-map
  5342. 'help-echo "Radio target link"
  5343. 'org-linked-text t))
  5344. (org-rear-nonsticky-at (match-end 0))
  5345. t)))))
  5346. (defun org-update-radio-target-regexp ()
  5347. "Find all radio targets in this file and update the regular expression."
  5348. (interactive)
  5349. (when (memq 'radio org-activate-links)
  5350. (setq org-target-link-regexp
  5351. (org-make-target-link-regexp (org-all-targets 'radio)))
  5352. (org-restart-font-lock)))
  5353. (defun org-hide-wide-columns (limit)
  5354. (let (s e)
  5355. (setq s (text-property-any (point) (or limit (point-max))
  5356. 'org-cwidth t))
  5357. (when s
  5358. (setq e (next-single-property-change s 'org-cwidth))
  5359. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5360. (goto-char e)
  5361. t)))
  5362. (defvar org-latex-and-related-regexp nil
  5363. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5364. (defvar org-match-substring-regexp)
  5365. (defvar org-match-substring-with-braces-regexp)
  5366. (defun org-compute-latex-and-related-regexp ()
  5367. "Compute regular expression for LaTeX, entities and sub/superscript.
  5368. Result depends on variable `org-highlight-latex-and-related'."
  5369. (org-set-local
  5370. 'org-latex-and-related-regexp
  5371. (let* ((re-sub
  5372. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5373. ((eq org-use-sub-superscripts '{})
  5374. (list org-match-substring-with-braces-regexp))
  5375. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5376. (re-latex
  5377. (when (memq 'latex org-highlight-latex-and-related)
  5378. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5379. (delq nil
  5380. (mapcar (lambda (x)
  5381. (and (member (car x) matchers) (nth 1 x)))
  5382. org-latex-regexps)))))
  5383. (re-entities
  5384. (when (memq 'entities org-highlight-latex-and-related)
  5385. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5386. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5387. (defun org-do-latex-and-related (limit)
  5388. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5389. LIMIT bounds the search for syntax to highlight. Stop at first
  5390. highlighted object, if any. Return t if some highlighting was
  5391. done, nil otherwise."
  5392. (when (org-string-nw-p org-latex-and-related-regexp)
  5393. (catch 'found
  5394. (while (re-search-forward org-latex-and-related-regexp limit t)
  5395. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5396. 'face))
  5397. '(org-code org-verbatim underline))
  5398. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5399. '(?_ ?^))
  5400. 1
  5401. 0)))
  5402. (font-lock-prepend-text-property
  5403. (+ offset (match-beginning 0)) (match-end 0)
  5404. 'face 'org-latex-and-related)
  5405. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5406. '(font-lock-multiline t)))
  5407. (throw 'found t)))
  5408. nil)))
  5409. (defun org-restart-font-lock ()
  5410. "Restart `font-lock-mode', to force refontification."
  5411. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5412. (font-lock-mode -1)
  5413. (font-lock-mode 1)))
  5414. (defun org-all-targets (&optional radio)
  5415. "Return a list of all targets in this file.
  5416. When optional argument RADIO is non-nil, only find radio
  5417. targets."
  5418. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5419. (save-excursion
  5420. (goto-char (point-min))
  5421. (while (re-search-forward re nil t)
  5422. ;; Make sure point is really within the object.
  5423. (backward-char)
  5424. (let ((obj (org-element-context)))
  5425. (when (memq (org-element-type obj) '(radio-target target))
  5426. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5427. rtn)))
  5428. (defun org-make-target-link-regexp (targets)
  5429. "Make regular expression matching all strings in TARGETS.
  5430. The regular expression finds the targets also if there is a line break
  5431. between words."
  5432. (and targets
  5433. (concat
  5434. "\\<\\("
  5435. (mapconcat
  5436. (lambda (x)
  5437. (setq x (regexp-quote x))
  5438. (while (string-match " +" x)
  5439. (setq x (replace-match "\\s-+" t t x)))
  5440. x)
  5441. targets
  5442. "\\|")
  5443. "\\)\\>")))
  5444. (defun org-activate-tags (limit)
  5445. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5446. (progn
  5447. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5448. (add-text-properties (match-beginning 1) (match-end 1)
  5449. (list 'mouse-face 'highlight
  5450. 'keymap org-mouse-map))
  5451. (org-rear-nonsticky-at (match-end 1))
  5452. t)))
  5453. (defun org-outline-level ()
  5454. "Compute the outline level of the heading at point.
  5455. If this is called at a normal headline, the level is the number of stars.
  5456. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5457. (save-excursion
  5458. (if (not (condition-case nil
  5459. (org-back-to-heading t)
  5460. (error nil)))
  5461. 0
  5462. (looking-at org-outline-regexp)
  5463. (1- (- (match-end 0) (match-beginning 0))))))
  5464. (defvar org-font-lock-keywords nil)
  5465. (defsubst org-re-property (property &optional literal)
  5466. "Return a regexp matching a PROPERTY line.
  5467. Match group 3 will be set to the value if it exists."
  5468. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5469. (if literal property (regexp-quote property))
  5470. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5471. (defconst org-property-re
  5472. (org-re-property ".*?" 'literal)
  5473. "Regular expression matching a property line.
  5474. There are four matching groups:
  5475. 1: :PROPKEY: including the leading and trailing colon,
  5476. 2: PROPKEY without the leading and trailing colon,
  5477. 3: PROPVAL without leading or trailing spaces,
  5478. 4: the indentation of the current line,
  5479. 5: trailing whitespace.")
  5480. (defvar org-font-lock-hook nil
  5481. "Functions to be called for special font lock stuff.")
  5482. (defvar org-font-lock-set-keywords-hook nil
  5483. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5484. This is called after `org-font-lock-extra-keywords' is defined, but before
  5485. it is installed to be used by font lock. This can be useful if something
  5486. needs to be inserted at a specific position in the font-lock sequence.")
  5487. (defun org-font-lock-hook (limit)
  5488. "Run `org-font-lock-hook' within LIMIT."
  5489. (run-hook-with-args 'org-font-lock-hook limit))
  5490. (defun org-set-font-lock-defaults ()
  5491. "Set font lock defaults for the current buffer."
  5492. (let* ((em org-fontify-emphasized-text)
  5493. (lk org-activate-links)
  5494. (org-font-lock-extra-keywords
  5495. (list
  5496. ;; Call the hook
  5497. '(org-font-lock-hook)
  5498. ;; Headlines
  5499. `(,(if org-fontify-whole-heading-line
  5500. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5501. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5502. (1 (org-get-level-face 1))
  5503. (2 (org-get-level-face 2))
  5504. (3 (org-get-level-face 3)))
  5505. ;; Table lines
  5506. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5507. (1 'org-table t))
  5508. ;; Table internals
  5509. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5510. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5511. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5512. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5513. ;; Drawers
  5514. (list org-drawer-regexp '(0 'org-special-keyword t))
  5515. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5516. ;; Properties
  5517. (list org-property-re
  5518. '(1 'org-special-keyword t)
  5519. '(3 'org-property-value t))
  5520. ;; Links
  5521. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5522. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5523. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5524. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5525. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5526. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5527. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5528. ;; Targets.
  5529. (list org-any-target-regexp '(0 'org-target t))
  5530. ;; Diary sexps.
  5531. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5532. ;; Macro
  5533. '("{{{.+}}}" (0 'org-macro t))
  5534. '(org-hide-wide-columns (0 nil append))
  5535. ;; TODO keyword
  5536. (list (format org-heading-keyword-regexp-format
  5537. org-todo-regexp)
  5538. '(2 (org-get-todo-face 2) t))
  5539. ;; DONE
  5540. (if org-fontify-done-headline
  5541. (list (format org-heading-keyword-regexp-format
  5542. (concat
  5543. "\\(?:"
  5544. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5545. "\\)"))
  5546. '(2 'org-headline-done t))
  5547. nil)
  5548. ;; Priorities
  5549. '(org-font-lock-add-priority-faces)
  5550. ;; Tags
  5551. '(org-font-lock-add-tag-faces)
  5552. ;; Tags groups
  5553. (if (and org-group-tags org-tag-groups-alist)
  5554. (list (concat org-outline-regexp-bol ".+\\(:"
  5555. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5556. ":\\).*$")
  5557. '(1 'org-tag-group prepend)))
  5558. ;; Special keywords
  5559. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5560. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5561. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5562. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5563. ;; Emphasis
  5564. (if em
  5565. (if (featurep 'xemacs)
  5566. '(org-do-emphasis-faces (0 nil append))
  5567. '(org-do-emphasis-faces)))
  5568. ;; Checkboxes
  5569. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5570. 1 'org-checkbox prepend)
  5571. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5572. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5573. (0 (org-get-checkbox-statistics-face) t)))
  5574. ;; Description list items
  5575. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5576. 1 'org-list-dt prepend)
  5577. ;; ARCHIVEd headings
  5578. (list (concat
  5579. org-outline-regexp-bol
  5580. "\\(.*:" org-archive-tag ":.*\\)")
  5581. '(1 'org-archived prepend))
  5582. ;; Specials
  5583. '(org-do-latex-and-related)
  5584. '(org-fontify-entities)
  5585. '(org-raise-scripts)
  5586. ;; Code
  5587. '(org-activate-code (1 'org-code t))
  5588. ;; COMMENT
  5589. (list (format org-heading-keyword-regexp-format
  5590. (concat "\\("
  5591. org-comment-string "\\|" org-quote-string
  5592. "\\)"))
  5593. '(2 'org-special-keyword t))
  5594. ;; Blocks and meta lines
  5595. '(org-fontify-meta-lines-and-blocks))))
  5596. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5597. (run-hooks 'org-font-lock-set-keywords-hook)
  5598. ;; Now set the full font-lock-keywords
  5599. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5600. (org-set-local 'font-lock-defaults
  5601. '(org-font-lock-keywords t nil nil backward-paragraph))
  5602. (kill-local-variable 'font-lock-keywords) nil))
  5603. (defun org-toggle-pretty-entities ()
  5604. "Toggle the composition display of entities as UTF8 characters."
  5605. (interactive)
  5606. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5607. (org-restart-font-lock)
  5608. (if org-pretty-entities
  5609. (message "Entities are now displayed as UTF8 characters")
  5610. (save-restriction
  5611. (widen)
  5612. (org-decompose-region (point-min) (point-max))
  5613. (message "Entities are now displayed as plain text"))))
  5614. (defvar org-custom-properties-overlays nil
  5615. "List of overlays used for custom properties.")
  5616. (make-variable-buffer-local 'org-custom-properties-overlays)
  5617. (defun org-toggle-custom-properties-visibility ()
  5618. "Display or hide properties in `org-custom-properties'."
  5619. (interactive)
  5620. (if org-custom-properties-overlays
  5621. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5622. (setq org-custom-properties-overlays nil))
  5623. (unless (not org-custom-properties)
  5624. (save-excursion
  5625. (save-restriction
  5626. (widen)
  5627. (goto-char (point-min))
  5628. (while (re-search-forward org-property-re nil t)
  5629. (mapc (lambda(p)
  5630. (when (equal p (substring (match-string 1) 1 -1))
  5631. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5632. (overlay-put o 'invisible t)
  5633. (overlay-put o 'org-custom-property t)
  5634. (push o org-custom-properties-overlays))))
  5635. org-custom-properties)))))))
  5636. (defun org-fontify-entities (limit)
  5637. "Find an entity to fontify."
  5638. (let (ee)
  5639. (when org-pretty-entities
  5640. (catch 'match
  5641. (while (re-search-forward
  5642. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5643. limit t)
  5644. (if (and (not (org-in-indented-comment-line))
  5645. (setq ee (org-entity-get (match-string 1)))
  5646. (= (length (nth 6 ee)) 1))
  5647. (let*
  5648. ((end (if (equal (match-string 2) "{}")
  5649. (match-end 2)
  5650. (match-end 1))))
  5651. (add-text-properties
  5652. (match-beginning 0) end
  5653. (list 'font-lock-fontified t))
  5654. (compose-region (match-beginning 0) end
  5655. (nth 6 ee) nil)
  5656. (backward-char 1)
  5657. (throw 'match t))))
  5658. nil))))
  5659. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5660. "Fontify string S like in Org-mode."
  5661. (with-temp-buffer
  5662. (insert s)
  5663. (let ((org-odd-levels-only odd-levels))
  5664. (org-mode)
  5665. (font-lock-fontify-buffer)
  5666. (buffer-string))))
  5667. (defvar org-m nil)
  5668. (defvar org-l nil)
  5669. (defvar org-f nil)
  5670. (defun org-get-level-face (n)
  5671. "Get the right face for match N in font-lock matching of headlines."
  5672. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5673. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5674. (if org-cycle-level-faces
  5675. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5676. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5677. (cond
  5678. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5679. ((eq n 2) org-f)
  5680. (t (if org-level-color-stars-only nil org-f))))
  5681. (defun org-get-todo-face (kwd)
  5682. "Get the right face for a TODO keyword KWD.
  5683. If KWD is a number, get the corresponding match group."
  5684. (if (numberp kwd) (setq kwd (match-string kwd)))
  5685. (or (org-face-from-face-or-color
  5686. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5687. (and (member kwd org-done-keywords) 'org-done)
  5688. 'org-todo))
  5689. (defun org-face-from-face-or-color (context inherit face-or-color)
  5690. "Create a face list that inherits INHERIT, but sets the foreground color.
  5691. When FACE-OR-COLOR is not a string, just return it."
  5692. (if (stringp face-or-color)
  5693. (list :inherit inherit
  5694. (cdr (assoc context org-faces-easy-properties))
  5695. face-or-color)
  5696. face-or-color))
  5697. (defun org-font-lock-add-tag-faces (limit)
  5698. "Add the special tag faces."
  5699. (when (and org-tag-faces org-tags-special-faces-re)
  5700. (while (re-search-forward org-tags-special-faces-re limit t)
  5701. (add-text-properties (match-beginning 1) (match-end 1)
  5702. (list 'face (org-get-tag-face 1)
  5703. 'font-lock-fontified t))
  5704. (backward-char 1))))
  5705. (defun org-font-lock-add-priority-faces (limit)
  5706. "Add the special priority faces."
  5707. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5708. (when (save-match-data (org-at-heading-p))
  5709. (add-text-properties
  5710. (match-beginning 0) (match-end 0)
  5711. (list 'face (or (org-face-from-face-or-color
  5712. 'priority 'org-priority
  5713. (cdr (assoc (char-after (match-beginning 1))
  5714. org-priority-faces)))
  5715. 'org-priority)
  5716. 'font-lock-fontified t)))))
  5717. (defun org-get-tag-face (kwd)
  5718. "Get the right face for a TODO keyword KWD.
  5719. If KWD is a number, get the corresponding match group."
  5720. (if (numberp kwd) (setq kwd (match-string kwd)))
  5721. (or (org-face-from-face-or-color
  5722. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5723. 'org-tag))
  5724. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5725. "Remove fontification and activation overlays from links."
  5726. (font-lock-default-unfontify-region beg end)
  5727. (let* ((buffer-undo-list t)
  5728. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5729. (inhibit-modification-hooks t)
  5730. deactivate-mark buffer-file-name buffer-file-truename)
  5731. (org-decompose-region beg end)
  5732. (remove-text-properties beg end
  5733. '(mouse-face t keymap t org-linked-text t
  5734. invisible t intangible t
  5735. org-no-flyspell t org-emphasis t))
  5736. (org-remove-font-lock-display-properties beg end)))
  5737. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5738. ((raise 0.3) (height 0.7))
  5739. ((raise -0.5))
  5740. ((raise 0.5)))
  5741. "Display properties for showing superscripts and subscripts.")
  5742. (defun org-remove-font-lock-display-properties (beg end)
  5743. "Remove specific display properties that have been added by font lock.
  5744. The will remove the raise properties that are used to show superscripts
  5745. and subscripts."
  5746. (let (next prop)
  5747. (while (< beg end)
  5748. (setq next (next-single-property-change beg 'display nil end)
  5749. prop (get-text-property beg 'display))
  5750. (if (member prop org-script-display)
  5751. (put-text-property beg next 'display nil))
  5752. (setq beg next))))
  5753. (defun org-raise-scripts (limit)
  5754. "Add raise properties to sub/superscripts."
  5755. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5756. (if (re-search-forward
  5757. (if (eq org-use-sub-superscripts t)
  5758. org-match-substring-regexp
  5759. org-match-substring-with-braces-regexp)
  5760. limit t)
  5761. (let* ((pos (point)) table-p comment-p
  5762. (mpos (match-beginning 3))
  5763. (emph-p (get-text-property mpos 'org-emphasis))
  5764. (link-p (get-text-property mpos 'mouse-face))
  5765. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5766. (goto-char (point-at-bol))
  5767. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5768. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5769. (goto-char pos)
  5770. ;; Handle a_b^c
  5771. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5772. (if (or comment-p emph-p link-p keyw-p)
  5773. t
  5774. (put-text-property (match-beginning 3) (match-end 0)
  5775. 'display
  5776. (if (equal (char-after (match-beginning 2)) ?^)
  5777. (nth (if table-p 3 1) org-script-display)
  5778. (nth (if table-p 2 0) org-script-display)))
  5779. (add-text-properties (match-beginning 2) (match-end 2)
  5780. (list 'invisible t
  5781. 'org-dwidth t 'org-dwidth-n 1))
  5782. (if (and (eq (char-after (match-beginning 3)) ?{)
  5783. (eq (char-before (match-end 3)) ?}))
  5784. (progn
  5785. (add-text-properties
  5786. (match-beginning 3) (1+ (match-beginning 3))
  5787. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5788. (add-text-properties
  5789. (1- (match-end 3)) (match-end 3)
  5790. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5791. t)))))
  5792. ;;;; Visibility cycling, including org-goto and indirect buffer
  5793. ;;; Cycling
  5794. (defvar org-cycle-global-status nil)
  5795. (make-variable-buffer-local 'org-cycle-global-status)
  5796. (put 'org-cycle-global-status 'org-state t)
  5797. (defvar org-cycle-subtree-status nil)
  5798. (make-variable-buffer-local 'org-cycle-subtree-status)
  5799. (put 'org-cycle-subtree-status 'org-state t)
  5800. (defvar org-inlinetask-min-level)
  5801. (defun org-unlogged-message (&rest args)
  5802. "Display a message, but avoid logging it in the *Messages* buffer."
  5803. (let ((message-log-max nil))
  5804. (apply 'message args)))
  5805. ;;;###autoload
  5806. (defun org-cycle (&optional arg)
  5807. "TAB-action and visibility cycling for Org-mode.
  5808. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5809. is outline visibility cycling, but it also invokes other actions
  5810. in special contexts.
  5811. - When this function is called with a prefix argument, rotate the entire
  5812. buffer through 3 states (global cycling)
  5813. 1. OVERVIEW: Show only top-level headlines.
  5814. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5815. 3. SHOW ALL: Show everything.
  5816. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5817. determined by the variable `org-startup-folded', and by any VISIBILITY
  5818. properties in the buffer.
  5819. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5820. including any drawers.
  5821. - When inside a table, re-align the table and move to the next field.
  5822. - When point is at the beginning of a headline, rotate the subtree started
  5823. by this line through 3 different states (local cycling)
  5824. 1. FOLDED: Only the main headline is shown.
  5825. 2. CHILDREN: The main headline and the direct children are shown.
  5826. From this state, you can move to one of the children
  5827. and zoom in further.
  5828. 3. SUBTREE: Show the entire subtree, including body text.
  5829. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5830. - When point is at the beginning of an empty headline and the variable
  5831. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5832. of the headline by demoting and promoting it to likely levels. This
  5833. speeds up creation document structure by pressing TAB once or several
  5834. times right after creating a new headline.
  5835. - When there is a numeric prefix, go up to a heading with level ARG, do
  5836. a `show-subtree' and return to the previous cursor position. If ARG
  5837. is negative, go up that many levels.
  5838. - When point is not at the beginning of a headline, execute the global
  5839. binding for TAB, which is re-indenting the line. See the option
  5840. `org-cycle-emulate-tab' for details.
  5841. - Special case: if point is at the beginning of the buffer and there is
  5842. no headline in line 1, this function will act as if called with prefix arg
  5843. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5844. But only if also the variable `org-cycle-global-at-bob' is t."
  5845. (interactive "P")
  5846. (org-load-modules-maybe)
  5847. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5848. (and org-cycle-level-after-item/entry-creation
  5849. (or (org-cycle-level)
  5850. (org-cycle-item-indentation))))
  5851. (let* ((limit-level
  5852. (or org-cycle-max-level
  5853. (and (boundp 'org-inlinetask-min-level)
  5854. org-inlinetask-min-level
  5855. (1- org-inlinetask-min-level))))
  5856. (nstars (and limit-level
  5857. (if org-odd-levels-only
  5858. (and limit-level (1- (* limit-level 2)))
  5859. limit-level)))
  5860. (org-outline-regexp
  5861. (if (not (derived-mode-p 'org-mode))
  5862. outline-regexp
  5863. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5864. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5865. (not (looking-at org-outline-regexp))))
  5866. (org-cycle-hook
  5867. (if bob-special
  5868. (delq 'org-optimize-window-after-visibility-change
  5869. (copy-sequence org-cycle-hook))
  5870. org-cycle-hook))
  5871. (pos (point)))
  5872. (if (or bob-special (equal arg '(4)))
  5873. ;; special case: use global cycling
  5874. (setq arg t))
  5875. (cond
  5876. ((equal arg '(16))
  5877. (setq last-command 'dummy)
  5878. (org-set-startup-visibility)
  5879. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5880. ((equal arg '(64))
  5881. (show-all)
  5882. (org-unlogged-message "Entire buffer visible, including drawers"))
  5883. ;; Table: enter it or move to the next field.
  5884. ((org-at-table-p 'any)
  5885. (if (org-at-table.el-p)
  5886. (message "Use C-c ' to edit table.el tables")
  5887. (if arg (org-table-edit-field t)
  5888. (org-table-justify-field-maybe)
  5889. (call-interactively 'org-table-next-field))))
  5890. ((run-hook-with-args-until-success
  5891. 'org-tab-after-check-for-table-hook))
  5892. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5893. ((eq arg t) (org-cycle-internal-global))
  5894. ;; Drawers: delegate to `org-flag-drawer'.
  5895. ((save-excursion
  5896. (beginning-of-line 1)
  5897. (looking-at org-drawer-regexp))
  5898. (org-flag-drawer ; toggle block visibility
  5899. (not (get-char-property (match-end 0) 'invisible))))
  5900. ;; Show-subtree, ARG levels up from here.
  5901. ((integerp arg)
  5902. (save-excursion
  5903. (org-back-to-heading)
  5904. (outline-up-heading (if (< arg 0) (- arg)
  5905. (- (funcall outline-level) arg)))
  5906. (org-show-subtree)))
  5907. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5908. ((and (featurep 'org-inlinetask)
  5909. (org-inlinetask-at-task-p)
  5910. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5911. (org-inlinetask-toggle-visibility))
  5912. ((org-try-cdlatex-tab))
  5913. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5914. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5915. (save-excursion (beginning-of-line 1)
  5916. (looking-at org-outline-regexp)))
  5917. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5918. (org-cycle-internal-local))
  5919. ;; From there: TAB emulation and template completion.
  5920. (buffer-read-only (org-back-to-heading))
  5921. ((run-hook-with-args-until-success
  5922. 'org-tab-after-check-for-cycling-hook))
  5923. ((org-try-structure-completion))
  5924. ((run-hook-with-args-until-success
  5925. 'org-tab-before-tab-emulation-hook))
  5926. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5927. (or (not (bolp))
  5928. (not (looking-at org-outline-regexp))))
  5929. (call-interactively (global-key-binding "\t")))
  5930. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5931. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5932. (or (and (eq org-cycle-emulate-tab 'white)
  5933. (= (match-end 0) (point-at-eol)))
  5934. (and (eq org-cycle-emulate-tab 'whitestart)
  5935. (>= (match-end 0) pos))))
  5936. t
  5937. (eq org-cycle-emulate-tab t))
  5938. (call-interactively (global-key-binding "\t")))
  5939. (t (save-excursion
  5940. (org-back-to-heading)
  5941. (org-cycle)))))))
  5942. (defun org-cycle-internal-global ()
  5943. "Do the global cycling action."
  5944. ;; Hack to avoid display of messages for .org attachments in Gnus
  5945. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5946. (cond
  5947. ((and (eq last-command this-command)
  5948. (eq org-cycle-global-status 'overview))
  5949. ;; We just created the overview - now do table of contents
  5950. ;; This can be slow in very large buffers, so indicate action
  5951. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5952. (unless ga (org-unlogged-message "CONTENTS..."))
  5953. (org-content)
  5954. (unless ga (org-unlogged-message "CONTENTS...done"))
  5955. (setq org-cycle-global-status 'contents)
  5956. (run-hook-with-args 'org-cycle-hook 'contents))
  5957. ((and (eq last-command this-command)
  5958. (eq org-cycle-global-status 'contents))
  5959. ;; We just showed the table of contents - now show everything
  5960. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5961. (show-all)
  5962. (unless ga (org-unlogged-message "SHOW ALL"))
  5963. (setq org-cycle-global-status 'all)
  5964. (run-hook-with-args 'org-cycle-hook 'all))
  5965. (t
  5966. ;; Default action: go to overview
  5967. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5968. (org-overview)
  5969. (unless ga (org-unlogged-message "OVERVIEW"))
  5970. (setq org-cycle-global-status 'overview)
  5971. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5972. (defun org-cycle-internal-local ()
  5973. "Do the local cycling action."
  5974. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5975. ;; First, determine end of headline (EOH), end of subtree or item
  5976. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5977. (save-excursion
  5978. (if (org-at-item-p)
  5979. (progn
  5980. (beginning-of-line)
  5981. (setq struct (org-list-struct))
  5982. (setq eoh (point-at-eol))
  5983. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5984. (setq has-children (org-list-has-child-p (point) struct)))
  5985. (org-back-to-heading)
  5986. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5987. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5988. (setq has-children
  5989. (or (save-excursion
  5990. (let ((level (funcall outline-level)))
  5991. (outline-next-heading)
  5992. (and (org-at-heading-p t)
  5993. (> (funcall outline-level) level))))
  5994. (save-excursion
  5995. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5996. ;; Determine end invisible part of buffer (EOL)
  5997. (beginning-of-line 2)
  5998. ;; XEmacs doesn't have `next-single-char-property-change'
  5999. (if (featurep 'xemacs)
  6000. (while (and (not (eobp)) ;; this is like `next-line'
  6001. (get-char-property (1- (point)) 'invisible))
  6002. (beginning-of-line 2))
  6003. (while (and (not (eobp)) ;; this is like `next-line'
  6004. (get-char-property (1- (point)) 'invisible))
  6005. (goto-char (next-single-char-property-change (point) 'invisible))
  6006. (and (eolp) (beginning-of-line 2))))
  6007. (setq eol (point)))
  6008. ;; Find out what to do next and set `this-command'
  6009. (cond
  6010. ((= eos eoh)
  6011. ;; Nothing is hidden behind this heading
  6012. (unless (org-before-first-heading-p)
  6013. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6014. (org-unlogged-message "EMPTY ENTRY")
  6015. (setq org-cycle-subtree-status nil)
  6016. (save-excursion
  6017. (goto-char eos)
  6018. (outline-next-heading)
  6019. (if (outline-invisible-p) (org-flag-heading nil))))
  6020. ((and (or (>= eol eos)
  6021. (not (string-match "\\S-" (buffer-substring eol eos))))
  6022. (or has-children
  6023. (not (setq children-skipped
  6024. org-cycle-skip-children-state-if-no-children))))
  6025. ;; Entire subtree is hidden in one line: children view
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6028. (if (org-at-item-p)
  6029. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6030. (org-show-entry)
  6031. (org-with-limited-levels (show-children))
  6032. ;; FIXME: This slows down the func way too much.
  6033. ;; How keep drawers hidden in subtree anyway?
  6034. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6035. ;; (org-cycle-hide-drawers 'subtree))
  6036. ;; Fold every list in subtree to top-level items.
  6037. (when (eq org-cycle-include-plain-lists 'integrate)
  6038. (save-excursion
  6039. (org-back-to-heading)
  6040. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6041. (beginning-of-line 1)
  6042. (let* ((struct (org-list-struct))
  6043. (prevs (org-list-prevs-alist struct))
  6044. (end (org-list-get-bottom-point struct)))
  6045. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6046. (org-list-get-all-items (point) struct prevs))
  6047. (goto-char (if (< end eos) end eos)))))))
  6048. (org-unlogged-message "CHILDREN")
  6049. (save-excursion
  6050. (goto-char eos)
  6051. (outline-next-heading)
  6052. (if (outline-invisible-p) (org-flag-heading nil)))
  6053. (setq org-cycle-subtree-status 'children)
  6054. (unless (org-before-first-heading-p)
  6055. (run-hook-with-args 'org-cycle-hook 'children)))
  6056. ((or children-skipped
  6057. (and (eq last-command this-command)
  6058. (eq org-cycle-subtree-status 'children)))
  6059. ;; We just showed the children, or no children are there,
  6060. ;; now show everything.
  6061. (unless (org-before-first-heading-p)
  6062. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6063. (outline-flag-region eoh eos nil)
  6064. (org-unlogged-message
  6065. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6066. (setq org-cycle-subtree-status 'subtree)
  6067. (unless (org-before-first-heading-p)
  6068. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6069. (t
  6070. ;; Default action: hide the subtree.
  6071. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6072. (outline-flag-region eoh eos t)
  6073. (org-unlogged-message "FOLDED")
  6074. (setq org-cycle-subtree-status 'folded)
  6075. (unless (org-before-first-heading-p)
  6076. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6077. ;;;###autoload
  6078. (defun org-global-cycle (&optional arg)
  6079. "Cycle the global visibility. For details see `org-cycle'.
  6080. With \\[universal-argument] prefix arg, switch to startup visibility.
  6081. With a numeric prefix, show all headlines up to that level."
  6082. (interactive "P")
  6083. (let ((org-cycle-include-plain-lists
  6084. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6085. (cond
  6086. ((integerp arg)
  6087. (show-all)
  6088. (hide-sublevels arg)
  6089. (setq org-cycle-global-status 'contents))
  6090. ((equal arg '(4))
  6091. (org-set-startup-visibility)
  6092. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6093. (t
  6094. (org-cycle '(4))))))
  6095. (defun org-set-startup-visibility ()
  6096. "Set the visibility required by startup options and properties."
  6097. (cond
  6098. ((eq org-startup-folded t)
  6099. (org-cycle '(4)))
  6100. ((eq org-startup-folded 'content)
  6101. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6102. (org-cycle '(4)) (org-cycle '(4)))))
  6103. (unless (eq org-startup-folded 'showeverything)
  6104. (if org-hide-block-startup (org-hide-block-all))
  6105. (org-set-visibility-according-to-property 'no-cleanup)
  6106. (org-cycle-hide-archived-subtrees 'all)
  6107. (org-cycle-hide-drawers 'all)
  6108. (org-cycle-show-empty-lines t)))
  6109. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6110. "Switch subtree visibilities according to :VISIBILITY: property."
  6111. (interactive)
  6112. (let (org-show-entry-below state)
  6113. (save-excursion
  6114. (goto-char (point-min))
  6115. (while (re-search-forward
  6116. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6117. nil t)
  6118. (setq state (match-string 1))
  6119. (save-excursion
  6120. (org-back-to-heading t)
  6121. (hide-subtree)
  6122. (org-reveal)
  6123. (cond
  6124. ((equal state '("fold" "folded"))
  6125. (hide-subtree))
  6126. ((equal state "children")
  6127. (org-show-hidden-entry)
  6128. (show-children))
  6129. ((equal state "content")
  6130. (save-excursion
  6131. (save-restriction
  6132. (org-narrow-to-subtree)
  6133. (org-content))))
  6134. ((member state '("all" "showall"))
  6135. (show-subtree)))))
  6136. (unless no-cleanup
  6137. (org-cycle-hide-archived-subtrees 'all)
  6138. (org-cycle-hide-drawers 'all)
  6139. (org-cycle-show-empty-lines 'all)))))
  6140. ;; This function uses outline-regexp instead of the more fundamental
  6141. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6142. ;; buffers, where outline-regexp is needed.
  6143. (defun org-overview ()
  6144. "Switch to overview mode, showing only top-level headlines.
  6145. Really, this shows all headlines with level equal or greater than the level
  6146. of the first headline in the buffer. This is important, because if the
  6147. first headline is not level one, then (hide-sublevels 1) gives confusing
  6148. results."
  6149. (interactive)
  6150. (let ((pos (point))
  6151. (level (save-excursion
  6152. (goto-char (point-min))
  6153. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6154. (progn
  6155. (goto-char (match-beginning 0))
  6156. (funcall outline-level))))))
  6157. (and level (hide-sublevels level))
  6158. (recenter '(4))
  6159. (goto-char pos)))
  6160. (defun org-content (&optional arg)
  6161. "Show all headlines in the buffer, like a table of contents.
  6162. With numerical argument N, show content up to level N."
  6163. (interactive "P")
  6164. (save-excursion
  6165. ;; Visit all headings and show their offspring
  6166. (and (integerp arg) (org-overview))
  6167. (goto-char (point-max))
  6168. (catch 'exit
  6169. (while (and (progn (condition-case nil
  6170. (outline-previous-visible-heading 1)
  6171. (error (goto-char (point-min))))
  6172. t)
  6173. (looking-at org-outline-regexp))
  6174. (if (integerp arg)
  6175. (show-children (1- arg))
  6176. (show-branches))
  6177. (if (bobp) (throw 'exit nil))))))
  6178. (defun org-optimize-window-after-visibility-change (state)
  6179. "Adjust the window after a change in outline visibility.
  6180. This function is the default value of the hook `org-cycle-hook'."
  6181. (when (get-buffer-window (current-buffer))
  6182. (cond
  6183. ((eq state 'content) nil)
  6184. ((eq state 'all) nil)
  6185. ((eq state 'folded) nil)
  6186. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6187. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6188. (defun org-remove-empty-overlays-at (pos)
  6189. "Remove outline overlays that do not contain non-white stuff."
  6190. (mapc
  6191. (lambda (o)
  6192. (and (eq 'outline (overlay-get o 'invisible))
  6193. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6194. (overlay-end o))))
  6195. (delete-overlay o)))
  6196. (overlays-at pos)))
  6197. (defun org-clean-visibility-after-subtree-move ()
  6198. "Fix visibility issues after moving a subtree."
  6199. ;; First, find a reasonable region to look at:
  6200. ;; Start two siblings above, end three below
  6201. (let* ((beg (save-excursion
  6202. (and (org-get-last-sibling)
  6203. (org-get-last-sibling))
  6204. (point)))
  6205. (end (save-excursion
  6206. (and (org-get-next-sibling)
  6207. (org-get-next-sibling)
  6208. (org-get-next-sibling))
  6209. (if (org-at-heading-p)
  6210. (point-at-eol)
  6211. (point))))
  6212. (level (looking-at "\\*+"))
  6213. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6214. (save-excursion
  6215. (save-restriction
  6216. (narrow-to-region beg end)
  6217. (when re
  6218. ;; Properly fold already folded siblings
  6219. (goto-char (point-min))
  6220. (while (re-search-forward re nil t)
  6221. (if (and (not (outline-invisible-p))
  6222. (save-excursion
  6223. (goto-char (point-at-eol)) (outline-invisible-p)))
  6224. (hide-entry))))
  6225. (org-cycle-show-empty-lines 'overview)
  6226. (org-cycle-hide-drawers 'overview)))))
  6227. (defun org-cycle-show-empty-lines (state)
  6228. "Show empty lines above all visible headlines.
  6229. The region to be covered depends on STATE when called through
  6230. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6231. entire buffer covered. Note that an empty line is only shown if there
  6232. are at least `org-cycle-separator-lines' empty lines before the headline."
  6233. (when (not (= org-cycle-separator-lines 0))
  6234. (save-excursion
  6235. (let* ((n (abs org-cycle-separator-lines))
  6236. (re (cond
  6237. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6238. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6239. (t (let ((ns (number-to-string (- n 2))))
  6240. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6241. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6242. beg end b e)
  6243. (cond
  6244. ((memq state '(overview contents t))
  6245. (setq beg (point-min) end (point-max)))
  6246. ((memq state '(children folded))
  6247. (setq beg (point) end (progn (org-end-of-subtree t t)
  6248. (beginning-of-line 2)
  6249. (point)))))
  6250. (when beg
  6251. (goto-char beg)
  6252. (while (re-search-forward re end t)
  6253. (unless (get-char-property (match-end 1) 'invisible)
  6254. (setq e (match-end 1))
  6255. (if (< org-cycle-separator-lines 0)
  6256. (setq b (save-excursion
  6257. (goto-char (match-beginning 0))
  6258. (org-back-over-empty-lines)
  6259. (if (save-excursion
  6260. (goto-char (max (point-min) (1- (point))))
  6261. (org-at-heading-p))
  6262. (1- (point))
  6263. (point))))
  6264. (setq b (match-beginning 1)))
  6265. (outline-flag-region b e nil)))))))
  6266. ;; Never hide empty lines at the end of the file.
  6267. (save-excursion
  6268. (goto-char (point-max))
  6269. (outline-previous-heading)
  6270. (outline-end-of-heading)
  6271. (if (and (looking-at "[ \t\n]+")
  6272. (= (match-end 0) (point-max)))
  6273. (outline-flag-region (point) (match-end 0) nil))))
  6274. (defun org-show-empty-lines-in-parent ()
  6275. "Move to the parent and re-show empty lines before visible headlines."
  6276. (save-excursion
  6277. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6278. (org-cycle-show-empty-lines context))))
  6279. (defun org-files-list ()
  6280. "Return `org-agenda-files' list, plus all open org-mode files.
  6281. This is useful for operations that need to scan all of a user's
  6282. open and agenda-wise Org files."
  6283. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6284. (dolist (buf (buffer-list))
  6285. (with-current-buffer buf
  6286. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6287. (let ((file (expand-file-name (buffer-file-name))))
  6288. (unless (member file files)
  6289. (push file files))))))
  6290. files))
  6291. (defsubst org-entry-beginning-position ()
  6292. "Return the beginning position of the current entry."
  6293. (save-excursion (outline-back-to-heading t) (point)))
  6294. (defsubst org-entry-end-position ()
  6295. "Return the end position of the current entry."
  6296. (save-excursion (outline-next-heading) (point)))
  6297. (defun org-cycle-hide-drawers (state &optional exceptions)
  6298. "Re-hide all drawers after a visibility state change.
  6299. When non-nil, optional argument EXCEPTIONS is a list of strings
  6300. specifying which drawers should not be hidden."
  6301. (when (and (derived-mode-p 'org-mode)
  6302. (not (memq state '(overview folded contents))))
  6303. (save-excursion
  6304. (let* ((globalp (memq state '(contents all)))
  6305. (beg (if globalp (point-min) (point)))
  6306. (end (if globalp (point-max)
  6307. (if (eq state 'children)
  6308. (save-excursion (outline-next-heading) (point))
  6309. (org-end-of-subtree t)))))
  6310. (goto-char beg)
  6311. (while (re-search-forward org-drawer-regexp end t)
  6312. (unless (member-ignore-case (match-string 1) exceptions)
  6313. (org-flag-drawer t)))))))
  6314. (defun org-cycle-hide-inline-tasks (state)
  6315. "Re-hide inline task when switching to 'contents visibility state."
  6316. (when (and (eq state 'contents)
  6317. (boundp 'org-inlinetask-min-level)
  6318. org-inlinetask-min-level)
  6319. (hide-sublevels (1- org-inlinetask-min-level))))
  6320. (defun org-flag-drawer (flag &optional element)
  6321. "When FLAG is non-nil, hide the drawer we are at.
  6322. Otherwise make it visible. When optional argument ELEMENT is
  6323. a parsed drawer, as returned by `org-element-at-point', hide or
  6324. show that drawer instead."
  6325. (let ((drawer (or element (org-element-at-point))))
  6326. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6327. (save-excursion
  6328. (goto-char (org-element-property :post-affiliated drawer))
  6329. (outline-flag-region
  6330. (line-end-position)
  6331. (progn (goto-char (org-element-property :end drawer))
  6332. (skip-chars-backward " \r\t\n")
  6333. (line-end-position))
  6334. flag)))))
  6335. (defun org-subtree-end-visible-p ()
  6336. "Is the end of the current subtree visible?"
  6337. (pos-visible-in-window-p
  6338. (save-excursion (org-end-of-subtree t) (point))))
  6339. (defun org-first-headline-recenter (&optional N)
  6340. "Move cursor to the first headline and recenter the headline.
  6341. Optional argument N means put the headline into the Nth line of the window."
  6342. (goto-char (point-min))
  6343. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6344. (beginning-of-line)
  6345. (recenter (prefix-numeric-value N))))
  6346. ;;; Saving and restoring visibility
  6347. (defun org-outline-overlay-data (&optional use-markers)
  6348. "Return a list of the locations of all outline overlays.
  6349. These are overlays with the `invisible' property value `outline'.
  6350. The return value is a list of cons cells, with start and stop
  6351. positions for each overlay.
  6352. If USE-MARKERS is set, return the positions as markers."
  6353. (let (beg end)
  6354. (save-excursion
  6355. (save-restriction
  6356. (widen)
  6357. (delq nil
  6358. (mapcar (lambda (o)
  6359. (when (eq (overlay-get o 'invisible) 'outline)
  6360. (setq beg (overlay-start o)
  6361. end (overlay-end o))
  6362. (and beg end (> end beg)
  6363. (if use-markers
  6364. (cons (move-marker (make-marker) beg)
  6365. (move-marker (make-marker) end))
  6366. (cons beg end)))))
  6367. (overlays-in (point-min) (point-max))))))))
  6368. (defun org-set-outline-overlay-data (data)
  6369. "Create visibility overlays for all positions in DATA.
  6370. DATA should have been made by `org-outline-overlay-data'."
  6371. (let (o)
  6372. (save-excursion
  6373. (save-restriction
  6374. (widen)
  6375. (show-all)
  6376. (mapc (lambda (c)
  6377. (outline-flag-region (car c) (cdr c) t))
  6378. data)))))
  6379. ;;; Folding of blocks
  6380. (defvar org-hide-block-overlays nil
  6381. "Overlays hiding blocks.")
  6382. (make-variable-buffer-local 'org-hide-block-overlays)
  6383. (defun org-block-map (function &optional start end)
  6384. "Call FUNCTION at the head of all source blocks in the current buffer.
  6385. Optional arguments START and END can be used to limit the range."
  6386. (let ((start (or start (point-min)))
  6387. (end (or end (point-max))))
  6388. (save-excursion
  6389. (goto-char start)
  6390. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6391. (save-excursion
  6392. (save-match-data
  6393. (goto-char (match-beginning 0))
  6394. (funcall function)))))))
  6395. (defun org-hide-block-toggle-all ()
  6396. "Toggle the visibility of all blocks in the current buffer."
  6397. (org-block-map #'org-hide-block-toggle))
  6398. (defun org-hide-block-all ()
  6399. "Fold all blocks in the current buffer."
  6400. (interactive)
  6401. (org-show-block-all)
  6402. (org-block-map #'org-hide-block-toggle-maybe))
  6403. (defun org-show-block-all ()
  6404. "Unfold all blocks in the current buffer."
  6405. (interactive)
  6406. (mapc 'delete-overlay org-hide-block-overlays)
  6407. (setq org-hide-block-overlays nil))
  6408. (defun org-hide-block-toggle-maybe ()
  6409. "Toggle visibility of block at point."
  6410. (interactive)
  6411. (let ((case-fold-search t))
  6412. (if (save-excursion
  6413. (beginning-of-line 1)
  6414. (looking-at org-block-regexp))
  6415. (progn (org-hide-block-toggle)
  6416. t) ;; to signal that we took action
  6417. nil))) ;; to signal that we did not
  6418. (defun org-hide-block-toggle (&optional force)
  6419. "Toggle the visibility of the current block."
  6420. (interactive)
  6421. (save-excursion
  6422. (beginning-of-line)
  6423. (if (re-search-forward org-block-regexp nil t)
  6424. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6425. (end (match-end 0)) ;; end of entire body
  6426. ov)
  6427. (if (memq t (mapcar (lambda (overlay)
  6428. (eq (overlay-get overlay 'invisible)
  6429. 'org-hide-block))
  6430. (overlays-at start)))
  6431. (if (or (not force) (eq force 'off))
  6432. (mapc (lambda (ov)
  6433. (when (member ov org-hide-block-overlays)
  6434. (setq org-hide-block-overlays
  6435. (delq ov org-hide-block-overlays)))
  6436. (when (eq (overlay-get ov 'invisible)
  6437. 'org-hide-block)
  6438. (delete-overlay ov)))
  6439. (overlays-at start)))
  6440. (setq ov (make-overlay start end))
  6441. (overlay-put ov 'invisible 'org-hide-block)
  6442. ;; make the block accessible to isearch
  6443. (overlay-put
  6444. ov 'isearch-open-invisible
  6445. (lambda (ov)
  6446. (when (member ov org-hide-block-overlays)
  6447. (setq org-hide-block-overlays
  6448. (delq ov org-hide-block-overlays)))
  6449. (when (eq (overlay-get ov 'invisible)
  6450. 'org-hide-block)
  6451. (delete-overlay ov))))
  6452. (push ov org-hide-block-overlays)))
  6453. (user-error "Not looking at a source block"))))
  6454. ;; org-tab-after-check-for-cycling-hook
  6455. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6456. ;; Remove overlays when changing major mode
  6457. (add-hook 'org-mode-hook
  6458. (lambda () (org-add-hook 'change-major-mode-hook
  6459. 'org-show-block-all 'append 'local)))
  6460. ;;; Org-goto
  6461. (defvar org-goto-window-configuration nil)
  6462. (defvar org-goto-marker nil)
  6463. (defvar org-goto-map)
  6464. (defun org-goto-map ()
  6465. "Set the keymap `org-goto'."
  6466. (setq org-goto-map
  6467. (let ((map (make-sparse-keymap)))
  6468. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6469. mouse-drag-region universal-argument org-occur))
  6470. cmd)
  6471. (while (setq cmd (pop cmds))
  6472. (substitute-key-definition cmd cmd map global-map)))
  6473. (suppress-keymap map)
  6474. (org-defkey map "\C-m" 'org-goto-ret)
  6475. (org-defkey map [(return)] 'org-goto-ret)
  6476. (org-defkey map [(left)] 'org-goto-left)
  6477. (org-defkey map [(right)] 'org-goto-right)
  6478. (org-defkey map [(control ?g)] 'org-goto-quit)
  6479. (org-defkey map "\C-i" 'org-cycle)
  6480. (org-defkey map [(tab)] 'org-cycle)
  6481. (org-defkey map [(down)] 'outline-next-visible-heading)
  6482. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6483. (if org-goto-auto-isearch
  6484. (if (fboundp 'define-key-after)
  6485. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6486. nil)
  6487. (org-defkey map "q" 'org-goto-quit)
  6488. (org-defkey map "n" 'outline-next-visible-heading)
  6489. (org-defkey map "p" 'outline-previous-visible-heading)
  6490. (org-defkey map "f" 'outline-forward-same-level)
  6491. (org-defkey map "b" 'outline-backward-same-level)
  6492. (org-defkey map "u" 'outline-up-heading))
  6493. (org-defkey map "/" 'org-occur)
  6494. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6495. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6496. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6497. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6498. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6499. map)))
  6500. (defconst org-goto-help
  6501. "Browse buffer copy, to find location or copy text.%s
  6502. RET=jump to location C-g=quit and return to previous location
  6503. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6504. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6505. (defun org-goto (&optional alternative-interface)
  6506. "Look up a different location in the current file, keeping current visibility.
  6507. When you want look-up or go to a different location in a
  6508. document, the fastest way is often to fold the entire buffer and
  6509. then dive into the tree. This method has the disadvantage, that
  6510. the previous location will be folded, which may not be what you
  6511. want.
  6512. This command works around this by showing a copy of the current
  6513. buffer in an indirect buffer, in overview mode. You can dive
  6514. into the tree in that copy, use org-occur and incremental search
  6515. to find a location. When pressing RET or `Q', the command
  6516. returns to the original buffer in which the visibility is still
  6517. unchanged. After RET it will also jump to the location selected
  6518. in the indirect buffer and expose the headline hierarchy above.
  6519. With a prefix argument, use the alternative interface: e.g. if
  6520. `org-goto-interface' is 'outline use 'outline-path-completion."
  6521. (interactive "P")
  6522. (org-goto-map)
  6523. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6524. (org-refile-use-outline-path t)
  6525. (org-refile-target-verify-function nil)
  6526. (interface
  6527. (if (not alternative-interface)
  6528. org-goto-interface
  6529. (if (eq org-goto-interface 'outline)
  6530. 'outline-path-completion
  6531. 'outline)))
  6532. (org-goto-start-pos (point))
  6533. (selected-point
  6534. (if (eq interface 'outline)
  6535. (car (org-get-location (current-buffer) org-goto-help))
  6536. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6537. (org-refile-check-position pa)
  6538. (nth 3 pa)))))
  6539. (if selected-point
  6540. (progn
  6541. (org-mark-ring-push org-goto-start-pos)
  6542. (goto-char selected-point)
  6543. (if (or (outline-invisible-p) (org-invisible-p2))
  6544. (org-show-context 'org-goto)))
  6545. (message "Quit"))))
  6546. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6547. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6548. (defvar org-goto-local-auto-isearch-map) ; defined below
  6549. (defun org-get-location (buf help)
  6550. "Let the user select a location in the Org-mode buffer BUF.
  6551. This function uses a recursive edit. It returns the selected position
  6552. or nil."
  6553. (org-no-popups
  6554. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6555. (isearch-hide-immediately nil)
  6556. (isearch-search-fun-function
  6557. (lambda () 'org-goto-local-search-headings))
  6558. (org-goto-selected-point org-goto-exit-command))
  6559. (save-excursion
  6560. (save-window-excursion
  6561. (delete-other-windows)
  6562. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6563. (org-pop-to-buffer-same-window
  6564. (condition-case nil
  6565. (make-indirect-buffer (current-buffer) "*org-goto*")
  6566. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6567. (with-output-to-temp-buffer "*Org Help*"
  6568. (princ (format help (if org-goto-auto-isearch
  6569. " Just type for auto-isearch."
  6570. " n/p/f/b/u to navigate, q to quit."))))
  6571. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6572. (setq buffer-read-only nil)
  6573. (let ((org-startup-truncated t)
  6574. (org-startup-folded nil)
  6575. (org-startup-align-all-tables nil))
  6576. (org-mode)
  6577. (org-overview))
  6578. (setq buffer-read-only t)
  6579. (if (and (boundp 'org-goto-start-pos)
  6580. (integer-or-marker-p org-goto-start-pos))
  6581. (let ((org-show-hierarchy-above t)
  6582. (org-show-siblings t)
  6583. (org-show-following-heading t))
  6584. (goto-char org-goto-start-pos)
  6585. (and (outline-invisible-p) (org-show-context)))
  6586. (goto-char (point-min)))
  6587. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6588. (message "Select location and press RET")
  6589. (use-local-map org-goto-map)
  6590. (recursive-edit)))
  6591. (kill-buffer "*org-goto*")
  6592. (cons org-goto-selected-point org-goto-exit-command))))
  6593. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6594. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6595. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6596. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6597. (defun org-goto-local-search-headings (string bound noerror)
  6598. "Search and make sure that any matches are in headlines."
  6599. (catch 'return
  6600. (while (if isearch-forward
  6601. (search-forward string bound noerror)
  6602. (search-backward string bound noerror))
  6603. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6604. (and (member :headline context)
  6605. (not (member :tags context))))
  6606. (throw 'return (point))))))
  6607. (defun org-goto-local-auto-isearch ()
  6608. "Start isearch."
  6609. (interactive)
  6610. (goto-char (point-min))
  6611. (let ((keys (this-command-keys)))
  6612. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6613. (isearch-mode t)
  6614. (isearch-process-search-char (string-to-char keys)))))
  6615. (defun org-goto-ret (&optional arg)
  6616. "Finish `org-goto' by going to the new location."
  6617. (interactive "P")
  6618. (setq org-goto-selected-point (point)
  6619. org-goto-exit-command 'return)
  6620. (throw 'exit nil))
  6621. (defun org-goto-left ()
  6622. "Finish `org-goto' by going to the new location."
  6623. (interactive)
  6624. (if (org-at-heading-p)
  6625. (progn
  6626. (beginning-of-line 1)
  6627. (setq org-goto-selected-point (point)
  6628. org-goto-exit-command 'left)
  6629. (throw 'exit nil))
  6630. (user-error "Not on a heading")))
  6631. (defun org-goto-right ()
  6632. "Finish `org-goto' by going to the new location."
  6633. (interactive)
  6634. (if (org-at-heading-p)
  6635. (progn
  6636. (setq org-goto-selected-point (point)
  6637. org-goto-exit-command 'right)
  6638. (throw 'exit nil))
  6639. (user-error "Not on a heading")))
  6640. (defun org-goto-quit ()
  6641. "Finish `org-goto' without cursor motion."
  6642. (interactive)
  6643. (setq org-goto-selected-point nil)
  6644. (setq org-goto-exit-command 'quit)
  6645. (throw 'exit nil))
  6646. ;;; Indirect buffer display of subtrees
  6647. (defvar org-indirect-dedicated-frame nil
  6648. "This is the frame being used for indirect tree display.")
  6649. (defvar org-last-indirect-buffer nil)
  6650. (defun org-tree-to-indirect-buffer (&optional arg)
  6651. "Create indirect buffer and narrow it to current subtree.
  6652. With a numerical prefix ARG, go up to this level and then take that tree.
  6653. If ARG is negative, go up that many levels.
  6654. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6655. indirect buffer previously made with this command, to avoid proliferation of
  6656. indirect buffers. However, when you call the command with a \
  6657. \\[universal-argument] prefix, or
  6658. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6659. is kept so that you can work with several indirect buffers at the same time.
  6660. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6661. \\[universal-argument] prefix also
  6662. requests that a new frame be made for the new buffer, so that the dedicated
  6663. frame is not changed."
  6664. (interactive "P")
  6665. (let ((cbuf (current-buffer))
  6666. (cwin (selected-window))
  6667. (pos (point))
  6668. beg end level heading ibuf)
  6669. (save-excursion
  6670. (org-back-to-heading t)
  6671. (when (numberp arg)
  6672. (setq level (org-outline-level))
  6673. (if (< arg 0) (setq arg (+ level arg)))
  6674. (while (> (setq level (org-outline-level)) arg)
  6675. (org-up-heading-safe)))
  6676. (setq beg (point)
  6677. heading (org-get-heading))
  6678. (org-end-of-subtree t t)
  6679. (if (org-at-heading-p) (backward-char 1))
  6680. (setq end (point)))
  6681. (if (and (buffer-live-p org-last-indirect-buffer)
  6682. (not (eq org-indirect-buffer-display 'new-frame))
  6683. (not arg))
  6684. (kill-buffer org-last-indirect-buffer))
  6685. (setq ibuf (org-get-indirect-buffer cbuf)
  6686. org-last-indirect-buffer ibuf)
  6687. (cond
  6688. ((or (eq org-indirect-buffer-display 'new-frame)
  6689. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6690. (select-frame (make-frame))
  6691. (delete-other-windows)
  6692. (org-pop-to-buffer-same-window ibuf)
  6693. (org-set-frame-title heading))
  6694. ((eq org-indirect-buffer-display 'dedicated-frame)
  6695. (raise-frame
  6696. (select-frame (or (and org-indirect-dedicated-frame
  6697. (frame-live-p org-indirect-dedicated-frame)
  6698. org-indirect-dedicated-frame)
  6699. (setq org-indirect-dedicated-frame (make-frame)))))
  6700. (delete-other-windows)
  6701. (org-pop-to-buffer-same-window ibuf)
  6702. (org-set-frame-title (concat "Indirect: " heading)))
  6703. ((eq org-indirect-buffer-display 'current-window)
  6704. (org-pop-to-buffer-same-window ibuf))
  6705. ((eq org-indirect-buffer-display 'other-window)
  6706. (pop-to-buffer ibuf))
  6707. (t (error "Invalid value")))
  6708. (if (featurep 'xemacs)
  6709. (save-excursion (org-mode) (turn-on-font-lock)))
  6710. (narrow-to-region beg end)
  6711. (show-all)
  6712. (goto-char pos)
  6713. (run-hook-with-args 'org-cycle-hook 'all)
  6714. (and (window-live-p cwin) (select-window cwin))))
  6715. (defun org-get-indirect-buffer (&optional buffer)
  6716. (setq buffer (or buffer (current-buffer)))
  6717. (let ((n 1) (base (buffer-name buffer)) bname)
  6718. (while (buffer-live-p
  6719. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6720. (setq n (1+ n)))
  6721. (condition-case nil
  6722. (make-indirect-buffer buffer bname 'clone)
  6723. (error (make-indirect-buffer buffer bname)))))
  6724. (defun org-set-frame-title (title)
  6725. "Set the title of the current frame to the string TITLE."
  6726. ;; FIXME: how to name a single frame in XEmacs???
  6727. (unless (featurep 'xemacs)
  6728. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6729. ;;;; Structure editing
  6730. ;;; Inserting headlines
  6731. (defun org-previous-line-empty-p (&optional next)
  6732. "Is the previous line a blank line?
  6733. When NEXT is non-nil, check the next line instead."
  6734. (save-excursion
  6735. (and (not (bobp))
  6736. (or (beginning-of-line (if next 2 0)) t)
  6737. (save-match-data
  6738. (looking-at "[ \t]*$")))))
  6739. (defun org-insert-heading (&optional arg invisible-ok)
  6740. "Insert a new heading or item with same depth at point.
  6741. If point is in a plain list and ARG is nil, create a new list item.
  6742. With one universal prefix argument, insert a heading even in lists.
  6743. With two universal prefix arguments, insert the heading at the end
  6744. of the parent subtree.
  6745. If point is at the beginning of a headline, insert a sibling before
  6746. the current headline. If point is not at the beginning, split the line
  6747. and create a new headline with the text in the current line after point
  6748. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6749. If point is at the beginning of a normal line, turn this line into
  6750. a heading.
  6751. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6752. This is important for non-interactive uses of the command."
  6753. (interactive "P")
  6754. (if (org-called-interactively-p 'any) (org-reveal))
  6755. (let ((itemp (org-in-item-p))
  6756. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6757. (respect-content (or org-insert-heading-respect-content
  6758. (equal arg '(16))))
  6759. (initial-content "")
  6760. (adjust-empty-lines t))
  6761. (cond
  6762. ((or (= (buffer-size) 0)
  6763. (and (not (save-excursion
  6764. (and (ignore-errors (org-back-to-heading invisible-ok))
  6765. (org-at-heading-p))))
  6766. (or arg (not itemp))))
  6767. ;; At beginning of buffer or so high up that only a heading
  6768. ;; makes sense.
  6769. (insert
  6770. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6771. (if (org-in-src-block-p) ",* " "* "))
  6772. (run-hooks 'org-insert-heading-hook))
  6773. ((and itemp (not (equal arg '(4))))
  6774. ;; Insert an item
  6775. (org-insert-item))
  6776. (t
  6777. ;; Insert a heading
  6778. (save-restriction
  6779. (widen)
  6780. (let* ((level nil)
  6781. (on-heading (org-at-heading-p))
  6782. (empty-line-p (if on-heading
  6783. (org-previous-line-empty-p)
  6784. ;; We will decide later
  6785. nil))
  6786. ;; Get a level string to fall back on
  6787. (fix-level
  6788. (save-excursion
  6789. (org-back-to-heading t)
  6790. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6791. (looking-at org-outline-regexp)
  6792. (make-string (1- (length (match-string 0))) ?*)))
  6793. (stars
  6794. (save-excursion
  6795. (condition-case nil
  6796. (progn
  6797. (org-back-to-heading invisible-ok)
  6798. (when (and (not on-heading)
  6799. (featurep 'org-inlinetask)
  6800. (integerp org-inlinetask-min-level)
  6801. (>= (length (match-string 0))
  6802. org-inlinetask-min-level))
  6803. ;; Find a heading level before the inline task
  6804. (while (and (setq level (org-up-heading-safe))
  6805. (>= level org-inlinetask-min-level)))
  6806. (if (org-at-heading-p)
  6807. (org-back-to-heading invisible-ok)
  6808. (error "This should not happen")))
  6809. (unless (and (save-excursion
  6810. (save-match-data
  6811. (org-backward-heading-same-level
  6812. 1 invisible-ok))
  6813. (= (point) (match-beginning 0)))
  6814. (not (org-previous-line-empty-p t)))
  6815. (setq empty-line-p (or empty-line-p
  6816. (org-previous-line-empty-p))))
  6817. (match-string 0))
  6818. (error (or fix-level "* ")))))
  6819. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6820. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6821. pos hide-previous previous-pos)
  6822. ;; If we insert after content, move there and clean up whitespace
  6823. (when respect-content
  6824. (org-end-of-subtree nil t)
  6825. (skip-chars-backward " \r\n")
  6826. (and (looking-at "[ \t]+") (replace-match ""))
  6827. (unless (eobp) (forward-char 1))
  6828. (when (looking-at "^\\*")
  6829. (unless (bobp) (backward-char 1))
  6830. (insert "\n")))
  6831. ;; If we are splitting, grab the text that should be moved to the new headline
  6832. (when may-split
  6833. (if (org-on-heading-p)
  6834. ;; This is a heading, we split intelligently (keeping tags)
  6835. (let ((pos (point)))
  6836. (goto-char (point-at-bol))
  6837. (unless (looking-at org-complex-heading-regexp)
  6838. (error "This should not happen"))
  6839. (when (and (match-beginning 4)
  6840. (> pos (match-beginning 4))
  6841. (< pos (match-end 4)))
  6842. (setq initial-content (buffer-substring pos (match-end 4)))
  6843. (goto-char pos)
  6844. (delete-region (point) (match-end 4))
  6845. (if (looking-at "[ \t]*$")
  6846. (replace-match "")
  6847. (insert (make-string (length initial-content) ?\ )))
  6848. (setq initial-content (org-trim initial-content)))
  6849. (goto-char pos))
  6850. ;; a normal line
  6851. (unless (bolp)
  6852. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6853. (delete-region (point) (point-at-eol))
  6854. (setq initial-content (org-trim initial-content)))))
  6855. ;; If we are at the beginning of the line, insert before it. Else after
  6856. (cond
  6857. ((and (bolp) (looking-at "[ \t]*$")))
  6858. ((and (bolp) (not (looking-at "[ \t]*$")))
  6859. (open-line 1))
  6860. (t
  6861. (goto-char (point-at-eol))
  6862. (insert "\n")))
  6863. ;; Insert the new heading
  6864. (insert stars)
  6865. (just-one-space)
  6866. (insert initial-content)
  6867. (when adjust-empty-lines
  6868. (if (or (not blank)
  6869. (and blank (not (org-previous-line-empty-p))))
  6870. (org-N-empty-lines-before-current (if blank 1 0))))
  6871. (run-hooks 'org-insert-heading-hook)))))))
  6872. (defun org-N-empty-lines-before-current (N)
  6873. "Make the number of empty lines before current exactly N.
  6874. So this will delete or add empty lines."
  6875. (save-excursion
  6876. (goto-char (point-at-bol))
  6877. (if (looking-back "\\s-+" nil 'greedy)
  6878. (replace-match ""))
  6879. (or (bobp) (insert "\n"))
  6880. (while (> N 0)
  6881. (insert "\n")
  6882. (setq N (1- N)))))
  6883. (defun org-get-heading (&optional no-tags no-todo)
  6884. "Return the heading of the current entry, without the stars.
  6885. When NO-TAGS is non-nil, don't include tags.
  6886. When NO-TODO is non-nil, don't include TODO keywords."
  6887. (save-excursion
  6888. (org-back-to-heading t)
  6889. (cond
  6890. ((and no-tags no-todo)
  6891. (looking-at org-complex-heading-regexp)
  6892. (match-string 4))
  6893. (no-tags
  6894. (looking-at (concat org-outline-regexp
  6895. "\\(.*?\\)"
  6896. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6897. (match-string 1))
  6898. (no-todo
  6899. (looking-at org-todo-line-regexp)
  6900. (match-string 3))
  6901. (t (looking-at org-heading-regexp)
  6902. (match-string 2)))))
  6903. (defvar orgstruct-mode) ; defined below
  6904. (defun org-heading-components ()
  6905. "Return the components of the current heading.
  6906. This is a list with the following elements:
  6907. - the level as an integer
  6908. - the reduced level, different if `org-odd-levels-only' is set.
  6909. - the TODO keyword, or nil
  6910. - the priority character, like ?A, or nil if no priority is given
  6911. - the headline text itself, or the tags string if no headline text
  6912. - the tags string, or nil."
  6913. (save-excursion
  6914. (org-back-to-heading t)
  6915. (if (let (case-fold-search)
  6916. (looking-at
  6917. (if orgstruct-mode
  6918. org-heading-regexp
  6919. org-complex-heading-regexp)))
  6920. (if orgstruct-mode
  6921. (list (length (match-string 1))
  6922. (org-reduced-level (length (match-string 1)))
  6923. nil
  6924. nil
  6925. (match-string 2)
  6926. nil)
  6927. (list (length (match-string 1))
  6928. (org-reduced-level (length (match-string 1)))
  6929. (org-match-string-no-properties 2)
  6930. (and (match-end 3) (aref (match-string 3) 2))
  6931. (org-match-string-no-properties 4)
  6932. (org-match-string-no-properties 5))))))
  6933. (defun org-get-entry ()
  6934. "Get the entry text, after heading, entire subtree."
  6935. (save-excursion
  6936. (org-back-to-heading t)
  6937. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6938. (defun org-insert-heading-after-current ()
  6939. "Insert a new heading with same level as current, after current subtree."
  6940. (interactive)
  6941. (org-back-to-heading)
  6942. (org-insert-heading)
  6943. (org-move-subtree-down)
  6944. (end-of-line 1))
  6945. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6946. "Insert heading with `org-insert-heading-respect-content' set to t."
  6947. (interactive "P")
  6948. (let ((org-insert-heading-respect-content t))
  6949. (org-insert-heading '(4) invisible-ok)))
  6950. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6951. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6952. (interactive "P")
  6953. (let ((org-insert-heading-respect-content t))
  6954. (org-insert-todo-heading force-state '(4))))
  6955. (defun org-insert-todo-heading (arg &optional force-heading)
  6956. "Insert a new heading with the same level and TODO state as current heading.
  6957. If the heading has no TODO state, or if the state is DONE, use the first
  6958. state (TODO by default). Also one prefix arg, force first state. With two
  6959. prefix args, force inserting at the end of the parent subtree."
  6960. (interactive "P")
  6961. (when (or force-heading (not (org-insert-item 'checkbox)))
  6962. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6963. force-heading))
  6964. (save-excursion
  6965. (org-back-to-heading)
  6966. (outline-previous-heading)
  6967. (looking-at org-todo-line-regexp))
  6968. (let*
  6969. ((new-mark-x
  6970. (if (or arg
  6971. (not (match-beginning 2))
  6972. (member (match-string 2) org-done-keywords))
  6973. (car org-todo-keywords-1)
  6974. (match-string 2)))
  6975. (new-mark
  6976. (or
  6977. (run-hook-with-args-until-success
  6978. 'org-todo-get-default-hook new-mark-x nil)
  6979. new-mark-x)))
  6980. (beginning-of-line 1)
  6981. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6982. (if org-treat-insert-todo-heading-as-state-change
  6983. (org-todo new-mark)
  6984. (insert new-mark " "))))
  6985. (when org-provide-todo-statistics
  6986. (org-update-parent-todo-statistics))))
  6987. (defun org-insert-subheading (arg)
  6988. "Insert a new subheading and demote it.
  6989. Works for outline headings and for plain lists alike."
  6990. (interactive "P")
  6991. (org-insert-heading arg)
  6992. (cond
  6993. ((org-at-heading-p) (org-do-demote))
  6994. ((org-at-item-p) (org-indent-item))))
  6995. (defun org-insert-todo-subheading (arg)
  6996. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6997. Works for outline headings and for plain lists alike."
  6998. (interactive "P")
  6999. (org-insert-todo-heading arg)
  7000. (cond
  7001. ((org-at-heading-p) (org-do-demote))
  7002. ((org-at-item-p) (org-indent-item))))
  7003. ;;; Promotion and Demotion
  7004. (defvar org-after-demote-entry-hook nil
  7005. "Hook run after an entry has been demoted.
  7006. The cursor will be at the beginning of the entry.
  7007. When a subtree is being demoted, the hook will be called for each node.")
  7008. (defvar org-after-promote-entry-hook nil
  7009. "Hook run after an entry has been promoted.
  7010. The cursor will be at the beginning of the entry.
  7011. When a subtree is being promoted, the hook will be called for each node.")
  7012. (defun org-promote-subtree ()
  7013. "Promote the entire subtree.
  7014. See also `org-promote'."
  7015. (interactive)
  7016. (save-excursion
  7017. (org-with-limited-levels (org-map-tree 'org-promote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-demote-subtree ()
  7020. "Demote the entire subtree. See `org-demote'.
  7021. See also `org-promote'."
  7022. (interactive)
  7023. (save-excursion
  7024. (org-with-limited-levels (org-map-tree 'org-demote)))
  7025. (org-fix-position-after-promote))
  7026. (defun org-do-promote ()
  7027. "Promote the current heading higher up the tree.
  7028. If the region is active in `transient-mark-mode', promote all headings
  7029. in the region."
  7030. (interactive)
  7031. (save-excursion
  7032. (if (org-region-active-p)
  7033. (org-map-region 'org-promote (region-beginning) (region-end))
  7034. (org-promote)))
  7035. (org-fix-position-after-promote))
  7036. (defun org-do-demote ()
  7037. "Demote the current heading lower down the tree.
  7038. If the region is active in `transient-mark-mode', demote all headings
  7039. in the region."
  7040. (interactive)
  7041. (save-excursion
  7042. (if (org-region-active-p)
  7043. (org-map-region 'org-demote (region-beginning) (region-end))
  7044. (org-demote)))
  7045. (org-fix-position-after-promote))
  7046. (defun org-fix-position-after-promote ()
  7047. "Make sure that after pro/demotion cursor position is right."
  7048. (let ((pos (point)))
  7049. (when (save-excursion
  7050. (beginning-of-line 1)
  7051. (looking-at org-todo-line-regexp)
  7052. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7053. (cond ((eobp) (insert " "))
  7054. ((eolp) (insert " "))
  7055. ((equal (char-after) ?\ ) (forward-char 1))))))
  7056. (defun org-current-level ()
  7057. "Return the level of the current entry, or nil if before the first headline.
  7058. The level is the number of stars at the beginning of the headline."
  7059. (save-excursion
  7060. (org-with-limited-levels
  7061. (if (ignore-errors (org-back-to-heading t))
  7062. (funcall outline-level)))))
  7063. (defun org-get-previous-line-level ()
  7064. "Return the outline depth of the last headline before the current line.
  7065. Returns 0 for the first headline in the buffer, and nil if before the
  7066. first headline."
  7067. (let ((current-level (org-current-level))
  7068. (prev-level (when (> (line-number-at-pos) 1)
  7069. (save-excursion
  7070. (beginning-of-line 0)
  7071. (org-current-level)))))
  7072. (cond ((null current-level) nil) ; Before first headline
  7073. ((null prev-level) 0) ; At first headline
  7074. (prev-level))))
  7075. (defun org-reduced-level (l)
  7076. "Compute the effective level of a heading.
  7077. This takes into account the setting of `org-odd-levels-only'."
  7078. (cond
  7079. ((zerop l) 0)
  7080. (org-odd-levels-only (1+ (floor (/ l 2))))
  7081. (t l)))
  7082. (defun org-level-increment ()
  7083. "Return the number of stars that will be added or removed at a
  7084. time to headlines when structure editing, based on the value of
  7085. `org-odd-levels-only'."
  7086. (if org-odd-levels-only 2 1))
  7087. (defun org-get-valid-level (level &optional change)
  7088. "Rectify a level change under the influence of `org-odd-levels-only'
  7089. LEVEL is a current level, CHANGE is by how much the level should be
  7090. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7091. even level numbers will become the next higher odd number."
  7092. (if org-odd-levels-only
  7093. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7094. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7095. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7096. (max 1 (+ level (or change 0)))))
  7097. (if (boundp 'define-obsolete-function-alias)
  7098. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7099. (define-obsolete-function-alias 'org-get-legal-level
  7100. 'org-get-valid-level)
  7101. (define-obsolete-function-alias 'org-get-legal-level
  7102. 'org-get-valid-level "23.1")))
  7103. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7104. ;; ̀org-with-limited-levels'
  7105. (defun org-promote ()
  7106. "Promote the current heading higher up the tree.
  7107. If the region is active in `transient-mark-mode', promote all headings
  7108. in the region."
  7109. (org-back-to-heading t)
  7110. (let* ((level (save-match-data (funcall outline-level)))
  7111. (after-change-functions (remove 'flyspell-after-change-function
  7112. after-change-functions))
  7113. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7114. (diff (abs (- level (length up-head) -1))))
  7115. (cond ((and (= level 1) org-called-with-limited-levels
  7116. org-allow-promoting-top-level-subtree)
  7117. (replace-match "# " nil t))
  7118. ((= level 1)
  7119. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7120. (t (replace-match up-head nil t)))
  7121. ;; Fixup tag positioning
  7122. (unless (= level 1)
  7123. (and org-auto-align-tags (org-set-tags nil t))
  7124. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7125. (run-hooks 'org-after-promote-entry-hook)))
  7126. (defun org-demote ()
  7127. "Demote the current heading lower down the tree.
  7128. If the region is active in `transient-mark-mode', demote all headings
  7129. in the region."
  7130. (org-back-to-heading t)
  7131. (let* ((level (save-match-data (funcall outline-level)))
  7132. (after-change-functions (remove 'flyspell-after-change-function
  7133. after-change-functions))
  7134. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7135. (diff (abs (- level (length down-head) -1))))
  7136. (replace-match down-head nil t)
  7137. ;; Fixup tag positioning
  7138. (and org-auto-align-tags (org-set-tags nil t))
  7139. (if org-adapt-indentation (org-fixup-indentation diff))
  7140. (run-hooks 'org-after-demote-entry-hook)))
  7141. (defun org-cycle-level ()
  7142. "Cycle the level of an empty headline through possible states.
  7143. This goes first to child, then to parent, level, then up the hierarchy.
  7144. After top level, it switches back to sibling level."
  7145. (interactive)
  7146. (let ((org-adapt-indentation nil))
  7147. (when (org-point-at-end-of-empty-headline)
  7148. (setq this-command 'org-cycle-level) ; Only needed for caching
  7149. (let ((cur-level (org-current-level))
  7150. (prev-level (org-get-previous-line-level)))
  7151. (cond
  7152. ;; If first headline in file, promote to top-level.
  7153. ((= prev-level 0)
  7154. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7155. do (org-do-promote)))
  7156. ;; If same level as prev, demote one.
  7157. ((= prev-level cur-level)
  7158. (org-do-demote))
  7159. ;; If parent is top-level, promote to top level if not already.
  7160. ((= prev-level 1)
  7161. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7162. do (org-do-promote)))
  7163. ;; If top-level, return to prev-level.
  7164. ((= cur-level 1)
  7165. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7166. do (org-do-demote)))
  7167. ;; If less than prev-level, promote one.
  7168. ((< cur-level prev-level)
  7169. (org-do-promote))
  7170. ;; If deeper than prev-level, promote until higher than
  7171. ;; prev-level.
  7172. ((> cur-level prev-level)
  7173. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7174. do (org-do-promote))))
  7175. t))))
  7176. (defun org-map-tree (fun)
  7177. "Call FUN for every heading underneath the current one."
  7178. (org-back-to-heading)
  7179. (let ((level (funcall outline-level)))
  7180. (save-excursion
  7181. (funcall fun)
  7182. (while (and (progn
  7183. (outline-next-heading)
  7184. (> (funcall outline-level) level))
  7185. (not (eobp)))
  7186. (funcall fun)))))
  7187. (defun org-map-region (fun beg end)
  7188. "Call FUN for every heading between BEG and END."
  7189. (let ((org-ignore-region t))
  7190. (save-excursion
  7191. (setq end (copy-marker end))
  7192. (goto-char beg)
  7193. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7194. (< (point) end))
  7195. (funcall fun))
  7196. (while (and (progn
  7197. (outline-next-heading)
  7198. (< (point) end))
  7199. (not (eobp)))
  7200. (funcall fun)))))
  7201. (defvar org-property-end-re) ; silence byte-compiler
  7202. (defun org-fixup-indentation (diff)
  7203. "Change the indentation in the current entry by DIFF.
  7204. However, if any line in the current entry has no indentation, or if it
  7205. would end up with no indentation after the change, nothing at all is done."
  7206. (save-excursion
  7207. (let ((end (save-excursion (outline-next-heading)
  7208. (point-marker)))
  7209. (prohibit (if (> diff 0)
  7210. "^\\S-"
  7211. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7212. col)
  7213. (unless (save-excursion (end-of-line 1)
  7214. (re-search-forward prohibit end t))
  7215. (while (and (< (point) end)
  7216. (re-search-forward "^[ \t]+" end t))
  7217. (goto-char (match-end 0))
  7218. (setq col (current-column))
  7219. (if (< diff 0) (replace-match ""))
  7220. (org-indent-to-column (+ diff col))))
  7221. (move-marker end nil))))
  7222. (defun org-convert-to-odd-levels ()
  7223. "Convert an org-mode file with all levels allowed to one with odd levels.
  7224. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7225. level 5 etc."
  7226. (interactive)
  7227. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7228. (let ((outline-level 'org-outline-level)
  7229. (org-odd-levels-only nil) n)
  7230. (save-excursion
  7231. (goto-char (point-min))
  7232. (while (re-search-forward "^\\*\\*+ " nil t)
  7233. (setq n (- (length (match-string 0)) 2))
  7234. (while (>= (setq n (1- n)) 0)
  7235. (org-demote))
  7236. (end-of-line 1))))))
  7237. (defun org-convert-to-oddeven-levels ()
  7238. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7239. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7240. file contains a section with an even level, conversion would
  7241. destroy the structure of the file. An error is signaled in this
  7242. case."
  7243. (interactive)
  7244. (goto-char (point-min))
  7245. ;; First check if there are no even levels
  7246. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7247. (org-show-context t)
  7248. (error "Not all levels are odd in this file. Conversion not possible"))
  7249. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7250. (let ((outline-regexp org-outline-regexp)
  7251. (outline-level 'org-outline-level)
  7252. (org-odd-levels-only nil) n)
  7253. (save-excursion
  7254. (goto-char (point-min))
  7255. (while (re-search-forward "^\\*\\*+ " nil t)
  7256. (setq n (/ (1- (length (match-string 0))) 2))
  7257. (while (>= (setq n (1- n)) 0)
  7258. (org-promote))
  7259. (end-of-line 1))))))
  7260. (defun org-tr-level (n)
  7261. "Make N odd if required."
  7262. (if org-odd-levels-only (1+ (/ n 2)) n))
  7263. ;;; Vertical tree motion, cutting and pasting of subtrees
  7264. (defun org-move-subtree-up (&optional arg)
  7265. "Move the current subtree up past ARG headlines of the same level."
  7266. (interactive "p")
  7267. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7268. (defun org-move-subtree-down (&optional arg)
  7269. "Move the current subtree down past ARG headlines of the same level."
  7270. (interactive "p")
  7271. (setq arg (prefix-numeric-value arg))
  7272. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7273. 'org-get-last-sibling))
  7274. (ins-point (make-marker))
  7275. (cnt (abs arg))
  7276. (col (current-column))
  7277. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7278. ;; Select the tree
  7279. (org-back-to-heading)
  7280. (setq beg0 (point))
  7281. (save-excursion
  7282. (setq ne-beg (org-back-over-empty-lines))
  7283. (setq beg (point)))
  7284. (save-match-data
  7285. (save-excursion (outline-end-of-heading)
  7286. (setq folded (outline-invisible-p)))
  7287. (outline-end-of-subtree))
  7288. (outline-next-heading)
  7289. (setq ne-end (org-back-over-empty-lines))
  7290. (setq end (point))
  7291. (goto-char beg0)
  7292. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7293. ;; include less whitespace
  7294. (save-excursion
  7295. (goto-char beg)
  7296. (forward-line (- ne-beg ne-end))
  7297. (setq beg (point))))
  7298. ;; Find insertion point, with error handling
  7299. (while (> cnt 0)
  7300. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7301. (progn (goto-char beg0)
  7302. (user-error "Cannot move past superior level or buffer limit")))
  7303. (setq cnt (1- cnt)))
  7304. (if (> arg 0)
  7305. ;; Moving forward - still need to move over subtree
  7306. (progn (org-end-of-subtree t t)
  7307. (save-excursion
  7308. (org-back-over-empty-lines)
  7309. (or (bolp) (newline)))))
  7310. (setq ne-ins (org-back-over-empty-lines))
  7311. (move-marker ins-point (point))
  7312. (setq txt (buffer-substring beg end))
  7313. (org-save-markers-in-region beg end)
  7314. (delete-region beg end)
  7315. (org-remove-empty-overlays-at beg)
  7316. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7317. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7318. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7319. (let ((bbb (point)))
  7320. (insert-before-markers txt)
  7321. (org-reinstall-markers-in-region bbb)
  7322. (move-marker ins-point bbb))
  7323. (or (bolp) (insert "\n"))
  7324. (setq ins-end (point))
  7325. (goto-char ins-point)
  7326. (org-skip-whitespace)
  7327. (when (and (< arg 0)
  7328. (org-first-sibling-p)
  7329. (> ne-ins ne-beg))
  7330. ;; Move whitespace back to beginning
  7331. (save-excursion
  7332. (goto-char ins-end)
  7333. (let ((kill-whole-line t))
  7334. (kill-line (- ne-ins ne-beg)) (point)))
  7335. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7336. (move-marker ins-point nil)
  7337. (if folded
  7338. (hide-subtree)
  7339. (org-show-entry)
  7340. (show-children)
  7341. (org-cycle-hide-drawers 'children))
  7342. (org-clean-visibility-after-subtree-move)
  7343. ;; move back to the initial column we were at
  7344. (move-to-column col)))
  7345. (defvar org-subtree-clip ""
  7346. "Clipboard for cut and paste of subtrees.
  7347. This is actually only a copy of the kill, because we use the normal kill
  7348. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7349. (defvar org-subtree-clip-folded nil
  7350. "Was the last copied subtree folded?
  7351. This is used to fold the tree back after pasting.")
  7352. (defun org-cut-subtree (&optional n)
  7353. "Cut the current subtree into the clipboard.
  7354. With prefix arg N, cut this many sequential subtrees.
  7355. This is a short-hand for marking the subtree and then cutting it."
  7356. (interactive "p")
  7357. (org-copy-subtree n 'cut))
  7358. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7359. "Copy the current subtree it in the clipboard.
  7360. With prefix arg N, copy this many sequential subtrees.
  7361. This is a short-hand for marking the subtree and then copying it.
  7362. If CUT is non-nil, actually cut the subtree.
  7363. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7364. of some markers in the region, even if CUT is non-nil. This is
  7365. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7366. (interactive "p")
  7367. (let (beg end folded (beg0 (point)))
  7368. (if (org-called-interactively-p 'any)
  7369. (org-back-to-heading nil) ; take what looks like a subtree
  7370. (org-back-to-heading t)) ; take what is really there
  7371. (setq beg (point))
  7372. (skip-chars-forward " \t\r\n")
  7373. (save-match-data
  7374. (if nosubtrees
  7375. (outline-next-heading)
  7376. (save-excursion (outline-end-of-heading)
  7377. (setq folded (outline-invisible-p)))
  7378. (condition-case nil
  7379. (org-forward-heading-same-level (1- n) t)
  7380. (error nil))
  7381. (org-end-of-subtree t t)))
  7382. (setq end (point))
  7383. (goto-char beg0)
  7384. (when (> end beg)
  7385. (setq org-subtree-clip-folded folded)
  7386. (when (or cut force-store-markers)
  7387. (org-save-markers-in-region beg end))
  7388. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7389. (setq org-subtree-clip (current-kill 0))
  7390. (message "%s: Subtree(s) with %d characters"
  7391. (if cut "Cut" "Copied")
  7392. (length org-subtree-clip)))))
  7393. (defun org-paste-subtree (&optional level tree for-yank)
  7394. "Paste the clipboard as a subtree, with modification of headline level.
  7395. The entire subtree is promoted or demoted in order to match a new headline
  7396. level.
  7397. If the cursor is at the beginning of a headline, the same level as
  7398. that headline is used to paste the tree.
  7399. If not, the new level is derived from the *visible* headings
  7400. before and after the insertion point, and taken to be the inferior headline
  7401. level of the two. So if the previous visible heading is level 3 and the
  7402. next is level 4 (or vice versa), level 4 will be used for insertion.
  7403. This makes sure that the subtree remains an independent subtree and does
  7404. not swallow low level entries.
  7405. You can also force a different level, either by using a numeric prefix
  7406. argument, or by inserting the heading marker by hand. For example, if the
  7407. cursor is after \"*****\", then the tree will be shifted to level 5.
  7408. If optional TREE is given, use this text instead of the kill ring.
  7409. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7410. move back over whitespace before inserting, and move point to the end of
  7411. the inserted text when done."
  7412. (interactive "P")
  7413. (setq tree (or tree (and kill-ring (current-kill 0))))
  7414. (unless (org-kill-is-subtree-p tree)
  7415. (user-error "%s"
  7416. (substitute-command-keys
  7417. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7418. (org-with-limited-levels
  7419. (let* ((visp (not (outline-invisible-p)))
  7420. (txt tree)
  7421. (^re_ "\\(\\*+\\)[ \t]*")
  7422. (old-level (if (string-match org-outline-regexp-bol txt)
  7423. (- (match-end 0) (match-beginning 0) 1)
  7424. -1))
  7425. (force-level (cond (level (prefix-numeric-value level))
  7426. ((and (looking-at "[ \t]*$")
  7427. (string-match
  7428. "^\\*+$" (buffer-substring
  7429. (point-at-bol) (point))))
  7430. (- (match-end 1) (match-beginning 1)))
  7431. ((and (bolp)
  7432. (looking-at org-outline-regexp))
  7433. (- (match-end 0) (point) 1))))
  7434. (previous-level (save-excursion
  7435. (condition-case nil
  7436. (progn
  7437. (outline-previous-visible-heading 1)
  7438. (if (looking-at ^re_)
  7439. (- (match-end 0) (match-beginning 0) 1)
  7440. 1))
  7441. (error 1))))
  7442. (next-level (save-excursion
  7443. (condition-case nil
  7444. (progn
  7445. (or (looking-at org-outline-regexp)
  7446. (outline-next-visible-heading 1))
  7447. (if (looking-at ^re_)
  7448. (- (match-end 0) (match-beginning 0) 1)
  7449. 1))
  7450. (error 1))))
  7451. (new-level (or force-level (max previous-level next-level)))
  7452. (shift (if (or (= old-level -1)
  7453. (= new-level -1)
  7454. (= old-level new-level))
  7455. 0
  7456. (- new-level old-level)))
  7457. (delta (if (> shift 0) -1 1))
  7458. (func (if (> shift 0) 'org-demote 'org-promote))
  7459. (org-odd-levels-only nil)
  7460. beg end newend)
  7461. ;; Remove the forced level indicator
  7462. (if force-level
  7463. (delete-region (point-at-bol) (point)))
  7464. ;; Paste
  7465. (beginning-of-line (if (bolp) 1 2))
  7466. (setq beg (point))
  7467. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7468. (insert-before-markers txt)
  7469. (unless (string-match "\n\\'" txt) (insert "\n"))
  7470. (setq newend (point))
  7471. (org-reinstall-markers-in-region beg)
  7472. (setq end (point))
  7473. (goto-char beg)
  7474. (skip-chars-forward " \t\n\r")
  7475. (setq beg (point))
  7476. (if (and (outline-invisible-p) visp)
  7477. (save-excursion (outline-show-heading)))
  7478. ;; Shift if necessary
  7479. (unless (= shift 0)
  7480. (save-restriction
  7481. (narrow-to-region beg end)
  7482. (while (not (= shift 0))
  7483. (org-map-region func (point-min) (point-max))
  7484. (setq shift (+ delta shift)))
  7485. (goto-char (point-min))
  7486. (setq newend (point-max))))
  7487. (when (or (org-called-interactively-p 'interactive) for-yank)
  7488. (message "Clipboard pasted as level %d subtree" new-level))
  7489. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7490. kill-ring
  7491. (eq org-subtree-clip (current-kill 0))
  7492. org-subtree-clip-folded)
  7493. ;; The tree was folded before it was killed/copied
  7494. (hide-subtree))
  7495. (and for-yank (goto-char newend)))))
  7496. (defun org-kill-is-subtree-p (&optional txt)
  7497. "Check if the current kill is an outline subtree, or a set of trees.
  7498. Returns nil if kill does not start with a headline, or if the first
  7499. headline level is not the largest headline level in the tree.
  7500. So this will actually accept several entries of equal levels as well,
  7501. which is OK for `org-paste-subtree'.
  7502. If optional TXT is given, check this string instead of the current kill."
  7503. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7504. (re (org-get-limited-outline-regexp))
  7505. (^re (concat "^" re))
  7506. (start-level (and kill
  7507. (string-match
  7508. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7509. kill)
  7510. (- (match-end 2) (match-beginning 2) 1)))
  7511. (start (1+ (or (match-beginning 2) -1))))
  7512. (if (not start-level)
  7513. (progn
  7514. nil) ;; does not even start with a heading
  7515. (catch 'exit
  7516. (while (setq start (string-match ^re kill (1+ start)))
  7517. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7518. (throw 'exit nil)))
  7519. t))))
  7520. (defvar org-markers-to-move nil
  7521. "Markers that should be moved with a cut-and-paste operation.
  7522. Those markers are stored together with their positions relative to
  7523. the start of the region.")
  7524. (defun org-save-markers-in-region (beg end)
  7525. "Check markers in region.
  7526. If these markers are between BEG and END, record their position relative
  7527. to BEG, so that after moving the block of text, we can put the markers back
  7528. into place.
  7529. This function gets called just before an entry or tree gets cut from the
  7530. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7531. called immediately, to move the markers with the entries."
  7532. (setq org-markers-to-move nil)
  7533. (when (featurep 'org-clock)
  7534. (org-clock-save-markers-for-cut-and-paste beg end))
  7535. (when (featurep 'org-agenda)
  7536. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7537. (defun org-check-and-save-marker (marker beg end)
  7538. "Check if MARKER is between BEG and END.
  7539. If yes, remember the marker and the distance to BEG."
  7540. (when (and (marker-buffer marker)
  7541. (equal (marker-buffer marker) (current-buffer)))
  7542. (if (and (>= marker beg) (< marker end))
  7543. (push (cons marker (- marker beg)) org-markers-to-move))))
  7544. (defun org-reinstall-markers-in-region (beg)
  7545. "Move all remembered markers to their position relative to BEG."
  7546. (mapc (lambda (x)
  7547. (move-marker (car x) (+ beg (cdr x))))
  7548. org-markers-to-move)
  7549. (setq org-markers-to-move nil))
  7550. (defun org-narrow-to-subtree ()
  7551. "Narrow buffer to the current subtree."
  7552. (interactive)
  7553. (save-excursion
  7554. (save-match-data
  7555. (org-with-limited-levels
  7556. (narrow-to-region
  7557. (progn (org-back-to-heading t) (point))
  7558. (progn (org-end-of-subtree t t)
  7559. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7560. (point)))))))
  7561. (defun org-narrow-to-block ()
  7562. "Narrow buffer to the current block."
  7563. (interactive)
  7564. (let* ((case-fold-search t)
  7565. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7566. "^[ \t]*#\\+end_.*")))
  7567. (if blockp
  7568. (narrow-to-region (car blockp) (cdr blockp))
  7569. (user-error "Not in a block"))))
  7570. (eval-when-compile
  7571. (defvar org-property-drawer-re))
  7572. (defvar org-property-start-re) ;; defined below
  7573. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7574. "Clone the task (subtree) at point N times.
  7575. The clones will be inserted as siblings.
  7576. In interactive use, the user will be prompted for the number of
  7577. clones to be produced. If the entry has a timestamp, the user
  7578. will also be prompted for a time shift, which may be a repeater
  7579. as used in time stamps, for example `+3d'. To disable this,
  7580. you can call the function with a universal prefix argument.
  7581. When a valid repeater is given and the entry contains any time
  7582. stamps, the clones will become a sequence in time, with time
  7583. stamps in the subtree shifted for each clone produced. If SHIFT
  7584. is nil or the empty string, time stamps will be left alone. The
  7585. ID property of the original subtree is removed.
  7586. If the original subtree did contain time stamps with a repeater,
  7587. the following will happen:
  7588. - the repeater will be removed in each clone
  7589. - an additional clone will be produced, with the current, unshifted
  7590. date(s) in the entry.
  7591. - the original entry will be placed *after* all the clones, with
  7592. repeater intact.
  7593. - the start days in the repeater in the original entry will be shifted
  7594. to past the last clone.
  7595. In this way you can spell out a number of instances of a repeating task,
  7596. and still retain the repeater to cover future instances of the task."
  7597. (interactive "nNumber of clones to produce: ")
  7598. (let ((shift
  7599. (or shift
  7600. (if (and (not (equal current-prefix-arg '(4)))
  7601. (save-excursion
  7602. (re-search-forward org-ts-regexp-both
  7603. (save-excursion
  7604. (org-end-of-subtree t)
  7605. (point)) t)))
  7606. (read-from-minibuffer
  7607. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7608. ""))) ;; No time shift
  7609. (n-no-remove -1)
  7610. (drawer-re org-drawer-regexp)
  7611. beg end template task idprop
  7612. shift-n shift-what doshift nmin nmax)
  7613. (if (not (and (integerp n) (> n 0)))
  7614. (error "Invalid number of replications %s" n))
  7615. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7616. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7617. shift)))
  7618. (error "Invalid shift specification %s" shift))
  7619. (when doshift
  7620. (setq shift-n (string-to-number (match-string 1 shift))
  7621. shift-what (cdr (assoc (match-string 2 shift)
  7622. '(("d" . day) ("w" . week)
  7623. ("m" . month) ("y" . year))))))
  7624. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7625. (setq nmin 1 nmax n)
  7626. (org-back-to-heading t)
  7627. (setq beg (point))
  7628. (setq idprop (org-entry-get nil "ID"))
  7629. (org-end-of-subtree t t)
  7630. (or (bolp) (insert "\n"))
  7631. (setq end (point))
  7632. (setq template (buffer-substring beg end))
  7633. (when (and doshift
  7634. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7635. (delete-region beg end)
  7636. (setq end beg)
  7637. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7638. (goto-char end)
  7639. (loop for n from nmin to nmax do
  7640. ;; prepare clone
  7641. (with-temp-buffer
  7642. (insert template)
  7643. (org-mode)
  7644. (goto-char (point-min))
  7645. (org-show-subtree)
  7646. (and idprop (if org-clone-delete-id
  7647. (org-entry-delete nil "ID")
  7648. (org-id-get-create t)))
  7649. (unless (= n 0)
  7650. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7651. (kill-whole-line))
  7652. (goto-char (point-min))
  7653. (while (re-search-forward drawer-re nil t)
  7654. (org-remove-empty-drawer-at (point))))
  7655. (goto-char (point-min))
  7656. (when doshift
  7657. (while (re-search-forward org-ts-regexp-both nil t)
  7658. (org-timestamp-change (* n shift-n) shift-what))
  7659. (unless (= n n-no-remove)
  7660. (goto-char (point-min))
  7661. (while (re-search-forward org-ts-regexp nil t)
  7662. (save-excursion
  7663. (goto-char (match-beginning 0))
  7664. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7665. (delete-region (match-beginning 1) (match-end 1)))))))
  7666. (setq task (buffer-string)))
  7667. (insert task))
  7668. (goto-char beg)))
  7669. ;;; Outline Sorting
  7670. (defun org-sort (with-case)
  7671. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7672. Optional argument WITH-CASE means sort case-sensitively."
  7673. (interactive "P")
  7674. (cond
  7675. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7676. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7677. (t
  7678. (org-call-with-arg 'org-sort-entries with-case))))
  7679. (defun org-sort-remove-invisible (s)
  7680. "Remove invisible links from string S."
  7681. (remove-text-properties 0 (length s) org-rm-props s)
  7682. (while (string-match org-bracket-link-regexp s)
  7683. (setq s (replace-match (if (match-end 2)
  7684. (match-string 3 s)
  7685. (match-string 1 s)) t t s)))
  7686. (let ((st (format " %s " s)))
  7687. (while (string-match org-emph-re st)
  7688. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7689. (setq s (substring st 1 -1)))
  7690. s)
  7691. (defvar org-priority-regexp) ; defined later in the file
  7692. (defvar org-after-sorting-entries-or-items-hook nil
  7693. "Hook that is run after a bunch of entries or items have been sorted.
  7694. When children are sorted, the cursor is in the parent line when this
  7695. hook gets called. When a region or a plain list is sorted, the cursor
  7696. will be in the first entry of the sorted region/list.")
  7697. (defun org-sort-entries
  7698. (&optional with-case sorting-type getkey-func compare-func property)
  7699. "Sort entries on a certain level of an outline tree.
  7700. If there is an active region, the entries in the region are sorted.
  7701. Else, if the cursor is before the first entry, sort the top-level items.
  7702. Else, the children of the entry at point are sorted.
  7703. Sorting can be alphabetically, numerically, by date/time as given by
  7704. a time stamp, by a property, by priority order, or by a custom function.
  7705. The command prompts for the sorting type unless it has been given to the
  7706. function through the SORTING-TYPE argument, which needs to be a character,
  7707. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7708. precise meaning of each character:
  7709. n Numerically, by converting the beginning of the entry/item to a number.
  7710. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7711. o By order of TODO keywords.
  7712. t By date/time, either the first active time stamp in the entry, or, if
  7713. none exist, by the first inactive one.
  7714. s By the scheduled date/time.
  7715. d By deadline date/time.
  7716. c By creation time, which is assumed to be the first inactive time stamp
  7717. at the beginning of a line.
  7718. p By priority according to the cookie.
  7719. r By the value of a property.
  7720. Capital letters will reverse the sort order.
  7721. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7722. called with point at the beginning of the record. It must return either
  7723. a string or a number that should serve as the sorting key for that record.
  7724. Comparing entries ignores case by default. However, with an optional argument
  7725. WITH-CASE, the sorting considers case as well.
  7726. Sorting is done against the visible part of the headlines, it ignores hidden
  7727. links."
  7728. (interactive "P")
  7729. (let ((case-func (if with-case 'identity 'downcase))
  7730. (cmstr
  7731. ;; The clock marker is lost when using `sort-subr', let's
  7732. ;; store the clocking string.
  7733. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7734. (save-excursion
  7735. (goto-char org-clock-marker)
  7736. (looking-back "^.*") (match-string-no-properties 0))))
  7737. start beg end stars re re2
  7738. txt what tmp)
  7739. ;; Find beginning and end of region to sort
  7740. (cond
  7741. ((org-region-active-p)
  7742. ;; we will sort the region
  7743. (setq end (region-end)
  7744. what "region")
  7745. (goto-char (region-beginning))
  7746. (if (not (org-at-heading-p)) (outline-next-heading))
  7747. (setq start (point)))
  7748. ((or (org-at-heading-p)
  7749. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7750. ;; we will sort the children of the current headline
  7751. (org-back-to-heading)
  7752. (setq start (point)
  7753. end (progn (org-end-of-subtree t t)
  7754. (or (bolp) (insert "\n"))
  7755. (org-back-over-empty-lines)
  7756. (point))
  7757. what "children")
  7758. (goto-char start)
  7759. (show-subtree)
  7760. (outline-next-heading))
  7761. (t
  7762. ;; we will sort the top-level entries in this file
  7763. (goto-char (point-min))
  7764. (or (org-at-heading-p) (outline-next-heading))
  7765. (setq start (point))
  7766. (goto-char (point-max))
  7767. (beginning-of-line 1)
  7768. (when (looking-at ".*?\\S-")
  7769. ;; File ends in a non-white line
  7770. (end-of-line 1)
  7771. (insert "\n"))
  7772. (setq end (point-max))
  7773. (setq what "top-level")
  7774. (goto-char start)
  7775. (show-all)))
  7776. (setq beg (point))
  7777. (if (>= beg end) (user-error "Nothing to sort"))
  7778. (looking-at "\\(\\*+\\)")
  7779. (setq stars (match-string 1)
  7780. re (concat "^" (regexp-quote stars) " +")
  7781. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7782. txt (buffer-substring beg end))
  7783. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7784. (if (and (not (equal stars "*")) (string-match re2 txt))
  7785. (user-error "Region to sort contains a level above the first entry"))
  7786. (unless sorting-type
  7787. (message
  7788. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7789. [t]ime [s]cheduled [d]eadline [c]reated
  7790. A/N/P/R/O/F/T/S/D/C means reversed:"
  7791. what)
  7792. (setq sorting-type (read-char-exclusive))
  7793. (unless getkey-func
  7794. (and (= (downcase sorting-type) ?f)
  7795. (setq getkey-func
  7796. (org-icompleting-read "Sort using function: "
  7797. obarray 'fboundp t nil nil))
  7798. (setq getkey-func (intern getkey-func))))
  7799. (and (= (downcase sorting-type) ?r)
  7800. (not property)
  7801. (setq property
  7802. (org-icompleting-read "Property: "
  7803. (mapcar 'list (org-buffer-property-keys t))
  7804. nil t))))
  7805. (message "Sorting entries...")
  7806. (save-restriction
  7807. (narrow-to-region start end)
  7808. (let ((dcst (downcase sorting-type))
  7809. (case-fold-search nil)
  7810. (now (current-time)))
  7811. (sort-subr
  7812. (/= dcst sorting-type)
  7813. ;; This function moves to the beginning character of the "record" to
  7814. ;; be sorted.
  7815. (lambda nil
  7816. (if (re-search-forward re nil t)
  7817. (goto-char (match-beginning 0))
  7818. (goto-char (point-max))))
  7819. ;; This function moves to the last character of the "record" being
  7820. ;; sorted.
  7821. (lambda nil
  7822. (save-match-data
  7823. (condition-case nil
  7824. (outline-forward-same-level 1)
  7825. (error
  7826. (goto-char (point-max))))))
  7827. ;; This function returns the value that gets sorted against.
  7828. (lambda nil
  7829. (cond
  7830. ((= dcst ?n)
  7831. (if (looking-at org-complex-heading-regexp)
  7832. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7833. nil))
  7834. ((= dcst ?a)
  7835. (if (looking-at org-complex-heading-regexp)
  7836. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7837. nil))
  7838. ((= dcst ?t)
  7839. (let ((end (save-excursion (outline-next-heading) (point))))
  7840. (if (or (re-search-forward org-ts-regexp end t)
  7841. (re-search-forward org-ts-regexp-both end t))
  7842. (org-time-string-to-seconds (match-string 0))
  7843. (org-float-time now))))
  7844. ((= dcst ?c)
  7845. (let ((end (save-excursion (outline-next-heading) (point))))
  7846. (if (re-search-forward
  7847. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7848. end t)
  7849. (org-time-string-to-seconds (match-string 0))
  7850. (org-float-time now))))
  7851. ((= dcst ?s)
  7852. (let ((end (save-excursion (outline-next-heading) (point))))
  7853. (if (re-search-forward org-scheduled-time-regexp end t)
  7854. (org-time-string-to-seconds (match-string 1))
  7855. (org-float-time now))))
  7856. ((= dcst ?d)
  7857. (let ((end (save-excursion (outline-next-heading) (point))))
  7858. (if (re-search-forward org-deadline-time-regexp end t)
  7859. (org-time-string-to-seconds (match-string 1))
  7860. (org-float-time now))))
  7861. ((= dcst ?p)
  7862. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7863. (string-to-char (match-string 2))
  7864. org-default-priority))
  7865. ((= dcst ?r)
  7866. (or (org-entry-get nil property) ""))
  7867. ((= dcst ?o)
  7868. (if (looking-at org-complex-heading-regexp)
  7869. (- 9999 (length (member (match-string 2)
  7870. org-todo-keywords-1)))))
  7871. ((= dcst ?f)
  7872. (if getkey-func
  7873. (progn
  7874. (setq tmp (funcall getkey-func))
  7875. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7876. tmp)
  7877. (error "Invalid key function `%s'" getkey-func)))
  7878. (t (error "Invalid sorting type `%c'" sorting-type))))
  7879. nil
  7880. (cond
  7881. ((= dcst ?a) 'string<)
  7882. ((= dcst ?f) compare-func)
  7883. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7884. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7885. ;; Reset the clock marker if needed
  7886. (when cmstr
  7887. (save-excursion
  7888. (goto-char start)
  7889. (search-forward cmstr nil t)
  7890. (move-marker org-clock-marker (point))))
  7891. (message "Sorting entries...done")))
  7892. (defun org-do-sort (table what &optional with-case sorting-type)
  7893. "Sort TABLE of WHAT according to SORTING-TYPE.
  7894. The user will be prompted for the SORTING-TYPE if the call to this
  7895. function does not specify it. WHAT is only for the prompt, to indicate
  7896. what is being sorted. The sorting key will be extracted from
  7897. the car of the elements of the table.
  7898. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7899. (unless sorting-type
  7900. (message
  7901. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7902. what)
  7903. (setq sorting-type (read-char-exclusive)))
  7904. (let ((dcst (downcase sorting-type))
  7905. extractfun comparefun)
  7906. ;; Define the appropriate functions
  7907. (cond
  7908. ((= dcst ?n)
  7909. (setq extractfun 'string-to-number
  7910. comparefun (if (= dcst sorting-type) '< '>)))
  7911. ((= dcst ?a)
  7912. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7913. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7914. comparefun (if (= dcst sorting-type)
  7915. 'string<
  7916. (lambda (a b) (and (not (string< a b))
  7917. (not (string= a b)))))))
  7918. ((= dcst ?t)
  7919. (setq extractfun
  7920. (lambda (x)
  7921. (if (or (string-match org-ts-regexp x)
  7922. (string-match org-ts-regexp-both x))
  7923. (org-float-time
  7924. (org-time-string-to-time (match-string 0 x)))
  7925. 0))
  7926. comparefun (if (= dcst sorting-type) '< '>)))
  7927. (t (error "Invalid sorting type `%c'" sorting-type)))
  7928. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7929. table)
  7930. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7931. ;;; The orgstruct minor mode
  7932. ;; Define a minor mode which can be used in other modes in order to
  7933. ;; integrate the org-mode structure editing commands.
  7934. ;; This is really a hack, because the org-mode structure commands use
  7935. ;; keys which normally belong to the major mode. Here is how it
  7936. ;; works: The minor mode defines all the keys necessary to operate the
  7937. ;; structure commands, but wraps the commands into a function which
  7938. ;; tests if the cursor is currently at a headline or a plain list
  7939. ;; item. If that is the case, the structure command is used,
  7940. ;; temporarily setting many Org-mode variables like regular
  7941. ;; expressions for filling etc. However, when any of those keys is
  7942. ;; used at a different location, function uses `key-binding' to look
  7943. ;; up if the key has an associated command in another currently active
  7944. ;; keymap (minor modes, major mode, global), and executes that
  7945. ;; command. There might be problems if any of the keys is otherwise
  7946. ;; used as a prefix key.
  7947. (defcustom orgstruct-heading-prefix-regexp nil
  7948. "Regexp that matches the custom prefix of Org headlines in
  7949. orgstruct(++)-mode."
  7950. :group 'org
  7951. :version "24.4"
  7952. :package-version '(Org . "8.0")
  7953. :type 'string)
  7954. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7955. (defcustom orgstruct-setup-hook nil
  7956. "Hook run after orgstruct-mode-map is filled."
  7957. :group 'org
  7958. :version "24.4"
  7959. :package-version '(Org . "8.0")
  7960. :type 'hook)
  7961. (defvar orgstruct-initialized nil)
  7962. (defvar org-local-vars nil
  7963. "List of local variables, for use by `orgstruct-mode'.")
  7964. ;;;###autoload
  7965. (define-minor-mode orgstruct-mode
  7966. "Toggle the minor mode `orgstruct-mode'.
  7967. This mode is for using Org-mode structure commands in other
  7968. modes. The following keys behave as if Org-mode were active, if
  7969. the cursor is on a headline, or on a plain list item (both as
  7970. defined by Org-mode)."
  7971. nil " OrgStruct" (make-sparse-keymap)
  7972. (funcall (if orgstruct-mode
  7973. 'add-to-invisibility-spec
  7974. 'remove-from-invisibility-spec)
  7975. '(outline . t))
  7976. (when orgstruct-mode
  7977. (org-load-modules-maybe)
  7978. (unless orgstruct-initialized
  7979. (orgstruct-setup)
  7980. (setq orgstruct-initialized t))))
  7981. ;;;###autoload
  7982. (defun turn-on-orgstruct ()
  7983. "Unconditionally turn on `orgstruct-mode'."
  7984. (orgstruct-mode 1))
  7985. (defvar org-fb-vars nil)
  7986. (make-variable-buffer-local 'org-fb-vars)
  7987. (defun orgstruct++-mode (&optional arg)
  7988. "Toggle `orgstruct-mode', the enhanced version of it.
  7989. In addition to setting orgstruct-mode, this also exports all
  7990. indentation and autofilling variables from org-mode into the
  7991. buffer. It will also recognize item context in multiline items."
  7992. (interactive "P")
  7993. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7994. (if (< arg 1)
  7995. (progn (orgstruct-mode -1)
  7996. (mapc (lambda(v)
  7997. (org-set-local (car v)
  7998. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7999. org-fb-vars))
  8000. (orgstruct-mode 1)
  8001. (setq org-fb-vars nil)
  8002. (unless org-local-vars
  8003. (setq org-local-vars (org-get-local-variables)))
  8004. (let (var val)
  8005. (mapc
  8006. (lambda (x)
  8007. (when (string-match
  8008. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8009. (symbol-name (car x)))
  8010. (setq var (car x) val (nth 1 x))
  8011. (push (list var `(quote ,(eval var))) org-fb-vars)
  8012. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8013. org-local-vars)
  8014. (org-set-local 'orgstruct-is-++ t))))
  8015. (defvar orgstruct-is-++ nil
  8016. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8017. (make-variable-buffer-local 'orgstruct-is-++)
  8018. ;;;###autoload
  8019. (defun turn-on-orgstruct++ ()
  8020. "Unconditionally turn on `orgstruct++-mode'."
  8021. (orgstruct++-mode 1))
  8022. (defun orgstruct-error ()
  8023. "Error when there is no default binding for a structure key."
  8024. (interactive)
  8025. (funcall (if (fboundp 'user-error)
  8026. 'user-error
  8027. 'error)
  8028. "This key has no function outside structure elements"))
  8029. (defun orgstruct-setup ()
  8030. "Setup orgstruct keymap."
  8031. (dolist (cell '((org-demote . t)
  8032. (org-metaleft . t)
  8033. (org-metaright . t)
  8034. (org-promote . t)
  8035. (org-shiftmetaleft . t)
  8036. (org-shiftmetaright . t)
  8037. org-backward-element
  8038. org-backward-heading-same-level
  8039. org-ctrl-c-ret
  8040. org-ctrl-c-minus
  8041. org-ctrl-c-star
  8042. org-cycle
  8043. org-forward-heading-same-level
  8044. org-insert-heading
  8045. org-insert-heading-respect-content
  8046. org-kill-note-or-show-branches
  8047. org-mark-subtree
  8048. org-meta-return
  8049. org-metadown
  8050. org-metaup
  8051. org-narrow-to-subtree
  8052. org-promote-subtree
  8053. org-reveal
  8054. org-shiftdown
  8055. org-shiftleft
  8056. org-shiftmetadown
  8057. org-shiftmetaup
  8058. org-shiftright
  8059. org-shifttab
  8060. org-shifttab
  8061. org-shiftup
  8062. org-show-subtree
  8063. org-sort
  8064. org-up-element
  8065. outline-demote
  8066. outline-next-visible-heading
  8067. outline-previous-visible-heading
  8068. outline-promote
  8069. outline-up-heading
  8070. show-children))
  8071. (let ((f (or (car-safe cell) cell))
  8072. (disable-when-heading-prefix (cdr-safe cell)))
  8073. (when (fboundp f)
  8074. (let ((new-bindings))
  8075. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8076. (where-is-internal f outline-mode-map)))
  8077. (push binding new-bindings)
  8078. ;; TODO use local-function-key-map
  8079. (dolist (rep '(("<tab>" . "TAB")
  8080. ("<return>" . "RET")
  8081. ("<escape>" . "ESC")
  8082. ("<delete>" . "DEL")))
  8083. (setq binding (read-kbd-macro
  8084. (let ((case-fold-search))
  8085. (replace-regexp-in-string
  8086. (regexp-quote (cdr rep))
  8087. (car rep)
  8088. (key-description binding)))))
  8089. (pushnew binding new-bindings :test 'equal)))
  8090. (dolist (binding new-bindings)
  8091. (let ((key (lookup-key orgstruct-mode-map binding)))
  8092. (when (or (not key) (numberp key))
  8093. (condition-case nil
  8094. (org-defkey orgstruct-mode-map
  8095. binding
  8096. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8097. (error nil)))))))))
  8098. (run-hooks 'orgstruct-setup-hook))
  8099. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8100. "Create a function for binding in the structure minor mode.
  8101. FUN is the command to call inside a table. KEY is the key that
  8102. should be checked in for a command to execute outside of tables.
  8103. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8104. if `orgstruct-heading-prefix-regexp' is non-nil."
  8105. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8106. (let ((nname name)
  8107. (i 0))
  8108. (while (fboundp (intern nname))
  8109. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8110. (setq name (intern nname)))
  8111. (eval
  8112. (let ((bindings '((org-heading-regexp
  8113. (concat "^"
  8114. orgstruct-heading-prefix-regexp
  8115. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8116. (org-outline-regexp
  8117. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8118. (org-outline-regexp-bol
  8119. (concat "^" org-outline-regexp))
  8120. (outline-regexp org-outline-regexp)
  8121. (outline-heading-end-regexp "\n")
  8122. (outline-level 'org-outline-level)
  8123. (outline-heading-alist))))
  8124. `(defun ,name (arg)
  8125. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8126. "Outside of structure, run the binding of `"
  8127. (key-description key) "'."
  8128. (when disable-when-heading-prefix
  8129. (concat
  8130. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8131. "back to the default binding due to limitations of Org's implementation of\n"
  8132. "`" (symbol-name fun) "'.")))
  8133. (interactive "p")
  8134. (let* ((disable
  8135. ,(when disable-when-heading-prefix
  8136. '(and orgstruct-heading-prefix-regexp
  8137. (not (string= orgstruct-heading-prefix-regexp "")))))
  8138. (fallback
  8139. (or disable
  8140. (not
  8141. (let* ,bindings
  8142. (org-context-p 'headline 'item
  8143. ,(when (memq fun
  8144. '(org-insert-heading
  8145. org-insert-heading-respect-content
  8146. org-meta-return))
  8147. '(when orgstruct-is-++
  8148. 'item-body))))))))
  8149. (if fallback
  8150. (let* ((orgstruct-mode)
  8151. (binding
  8152. (loop with key = ,key
  8153. for rep in
  8154. '(nil
  8155. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8156. ("<\\([^>]*\\)return>" . "\\1RET")
  8157. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8158. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8159. do
  8160. (when rep
  8161. (setq key (read-kbd-macro
  8162. (let ((case-fold-search))
  8163. (replace-regexp-in-string
  8164. (car rep)
  8165. (cdr rep)
  8166. (key-description key))))))
  8167. thereis (key-binding key))))
  8168. (if (keymapp binding)
  8169. (set-temporary-overlay-map binding)
  8170. (let ((func (or binding
  8171. (unless disable
  8172. 'orgstruct-error))))
  8173. (when func
  8174. (call-interactively func)))))
  8175. (org-run-like-in-org-mode
  8176. (lambda ()
  8177. (interactive)
  8178. (let* ,bindings
  8179. (call-interactively ',fun)))))))))
  8180. name))
  8181. (defun org-contextualize-keys (alist contexts)
  8182. "Return valid elements in ALIST depending on CONTEXTS.
  8183. `org-agenda-custom-commands' or `org-capture-templates' are the
  8184. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8185. or `org-capture-templates-contexts' are the associated contexts
  8186. definitions."
  8187. (let ((contexts
  8188. ;; normalize contexts
  8189. (mapcar
  8190. (lambda(c) (cond ((listp (cadr c))
  8191. (list (car c) (car c) (cadr c)))
  8192. ((string= "" (cadr c))
  8193. (list (car c) (car c) (caddr c)))
  8194. (t c))) contexts))
  8195. (a alist) c r s)
  8196. ;; loop over all commands or templates
  8197. (while (setq c (pop a))
  8198. (let (vrules repl)
  8199. (cond
  8200. ((not (assoc (car c) contexts))
  8201. (push c r))
  8202. ((and (assoc (car c) contexts)
  8203. (setq vrules (org-contextualize-validate-key
  8204. (car c) contexts)))
  8205. (mapc (lambda (vr)
  8206. (when (not (equal (car vr) (cadr vr)))
  8207. (setq repl vr))) vrules)
  8208. (if (not repl) (push c r)
  8209. (push (cadr repl) s)
  8210. (push
  8211. (cons (car c)
  8212. (cdr (or (assoc (cadr repl) alist)
  8213. (error "Undefined key `%s' as contextual replacement for `%s'"
  8214. (cadr repl) (car c)))))
  8215. r))))))
  8216. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8217. (delq
  8218. nil
  8219. (delete-dups
  8220. (mapcar (lambda (x)
  8221. (let ((tpl (car x)))
  8222. (when (not (delq
  8223. nil
  8224. (mapcar (lambda(y)
  8225. (equal y tpl)) s))) x)))
  8226. (reverse r))))))
  8227. (defun org-contextualize-validate-key (key contexts)
  8228. "Check CONTEXTS for agenda or capture KEY."
  8229. (let (r rr res)
  8230. (while (setq r (pop contexts))
  8231. (mapc
  8232. (lambda (rr)
  8233. (when
  8234. (and (equal key (car r))
  8235. (if (functionp rr) (funcall rr)
  8236. (or (and (eq (car rr) 'in-file)
  8237. (buffer-file-name)
  8238. (string-match (cdr rr) (buffer-file-name)))
  8239. (and (eq (car rr) 'in-mode)
  8240. (string-match (cdr rr) (symbol-name major-mode)))
  8241. (and (eq (car rr) 'in-buffer)
  8242. (string-match (cdr rr) (buffer-name)))
  8243. (when (and (eq (car rr) 'not-in-file)
  8244. (buffer-file-name))
  8245. (not (string-match (cdr rr) (buffer-file-name))))
  8246. (when (eq (car rr) 'not-in-mode)
  8247. (not (string-match (cdr rr) (symbol-name major-mode))))
  8248. (when (eq (car rr) 'not-in-buffer)
  8249. (not (string-match (cdr rr) (buffer-name)))))))
  8250. (push r res)))
  8251. (car (last r))))
  8252. (delete-dups (delq nil res))))
  8253. (defun org-context-p (&rest contexts)
  8254. "Check if local context is any of CONTEXTS.
  8255. Possible values in the list of contexts are `table', `headline', and `item'."
  8256. (let ((pos (point)))
  8257. (goto-char (point-at-bol))
  8258. (prog1 (or (and (memq 'table contexts)
  8259. (looking-at "[ \t]*|"))
  8260. (and (memq 'headline contexts)
  8261. (looking-at org-outline-regexp))
  8262. (and (memq 'item contexts)
  8263. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8264. (and (memq 'item-body contexts)
  8265. (org-in-item-p)))
  8266. (goto-char pos))))
  8267. (defun org-get-local-variables ()
  8268. "Return a list of all local variables in an Org mode buffer."
  8269. (let (varlist)
  8270. (with-current-buffer (get-buffer-create "*Org tmp*")
  8271. (erase-buffer)
  8272. (org-mode)
  8273. (setq varlist (buffer-local-variables)))
  8274. (kill-buffer "*Org tmp*")
  8275. (delq nil
  8276. (mapcar
  8277. (lambda (x)
  8278. (setq x
  8279. (if (symbolp x)
  8280. (list x)
  8281. (list (car x) (cdr x))))
  8282. (if (and (not (get (car x) 'org-state))
  8283. (string-match
  8284. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8285. (symbol-name (car x))))
  8286. x nil))
  8287. varlist))))
  8288. (defun org-clone-local-variables (from-buffer &optional regexp)
  8289. "Clone local variables from FROM-BUFFER.
  8290. Optional argument REGEXP selects variables to clone."
  8291. (mapc
  8292. (lambda (pair)
  8293. (and (symbolp (car pair))
  8294. (or (null regexp)
  8295. (string-match regexp (symbol-name (car pair))))
  8296. (set (make-local-variable (car pair))
  8297. (cdr pair))))
  8298. (buffer-local-variables from-buffer)))
  8299. ;;;###autoload
  8300. (defun org-run-like-in-org-mode (cmd)
  8301. "Run a command, pretending that the current buffer is in Org-mode.
  8302. This will temporarily bind local variables that are typically bound in
  8303. Org-mode to the values they have in Org-mode, and then interactively
  8304. call CMD."
  8305. (org-load-modules-maybe)
  8306. (unless org-local-vars
  8307. (setq org-local-vars (org-get-local-variables)))
  8308. (let (binds)
  8309. (dolist (var org-local-vars)
  8310. (when (or (not (boundp (car var)))
  8311. (eq (symbol-value (car var))
  8312. (default-value (car var))))
  8313. (push (list (car var) `(quote ,(cadr var))) binds)))
  8314. (eval `(let ,binds
  8315. (call-interactively (quote ,cmd))))))
  8316. ;;;; Archiving
  8317. (defun org-get-category (&optional pos force-refresh)
  8318. "Get the category applying to position POS."
  8319. (save-match-data
  8320. (if force-refresh (org-refresh-category-properties))
  8321. (let ((pos (or pos (point))))
  8322. (or (get-text-property pos 'org-category)
  8323. (progn (org-refresh-category-properties)
  8324. (get-text-property pos 'org-category))))))
  8325. (defun org-refresh-category-properties ()
  8326. "Refresh category text properties in the buffer."
  8327. (let ((case-fold-search t)
  8328. (inhibit-read-only t)
  8329. (def-cat (cond
  8330. ((null org-category)
  8331. (if buffer-file-name
  8332. (file-name-sans-extension
  8333. (file-name-nondirectory buffer-file-name))
  8334. "???"))
  8335. ((symbolp org-category) (symbol-name org-category))
  8336. (t org-category)))
  8337. beg end cat pos optionp)
  8338. (org-with-silent-modifications
  8339. (save-excursion
  8340. (save-restriction
  8341. (widen)
  8342. (goto-char (point-min))
  8343. (put-text-property (point) (point-max) 'org-category def-cat)
  8344. (while (re-search-forward
  8345. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8346. (setq pos (match-end 0)
  8347. optionp (equal (char-after (match-beginning 0)) ?#)
  8348. cat (org-trim (match-string 2)))
  8349. (if optionp
  8350. (setq beg (point-at-bol) end (point-max))
  8351. (org-back-to-heading t)
  8352. (setq beg (point) end (org-end-of-subtree t t)))
  8353. (put-text-property beg end 'org-category cat)
  8354. (put-text-property beg end 'org-category-position beg)
  8355. (goto-char pos)))))))
  8356. (defun org-refresh-properties (dprop tprop)
  8357. "Refresh buffer text properties.
  8358. DPROP is the drawer property and TPROP is the corresponding text
  8359. property to set."
  8360. (let ((case-fold-search t)
  8361. (inhibit-read-only t) p)
  8362. (org-with-silent-modifications
  8363. (save-excursion
  8364. (save-restriction
  8365. (widen)
  8366. (goto-char (point-min))
  8367. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8368. (setq p (org-match-string-no-properties 1))
  8369. (save-excursion
  8370. (org-back-to-heading t)
  8371. (put-text-property
  8372. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8373. ;;;; Link Stuff
  8374. ;;; Link abbreviations
  8375. (defun org-link-expand-abbrev (link)
  8376. "Apply replacements as defined in `org-link-abbrev-alist'."
  8377. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8378. (let* ((key (match-string 1 link))
  8379. (as (or (assoc key org-link-abbrev-alist-local)
  8380. (assoc key org-link-abbrev-alist)))
  8381. (tag (and (match-end 2) (match-string 3 link)))
  8382. rpl)
  8383. (if (not as)
  8384. link
  8385. (setq rpl (cdr as))
  8386. (cond
  8387. ((symbolp rpl) (funcall rpl tag))
  8388. ((string-match "%(\\([^)]+\\))" rpl)
  8389. (replace-match
  8390. (save-match-data
  8391. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8392. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8393. ((string-match "%h" rpl)
  8394. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8395. (t (concat rpl tag)))))
  8396. link))
  8397. ;;; Storing and inserting links
  8398. (defvar org-insert-link-history nil
  8399. "Minibuffer history for links inserted with `org-insert-link'.")
  8400. (defvar org-stored-links nil
  8401. "Contains the links stored with `org-store-link'.")
  8402. (defvar org-store-link-plist nil
  8403. "Plist with info about the most recently link created with `org-store-link'.")
  8404. (defvar org-link-protocols nil
  8405. "Link protocols added to Org-mode using `org-add-link-type'.")
  8406. (defvar org-store-link-functions nil
  8407. "List of functions that are called to create and store a link.
  8408. Each function will be called in turn until one returns a non-nil
  8409. value. Each function should check if it is responsible for creating
  8410. this link (for example by looking at the major mode).
  8411. If not, it must exit and return nil.
  8412. If yes, it should return a non-nil value after a calling
  8413. `org-store-link-props' with a list of properties and values.
  8414. Special properties are:
  8415. :type The link prefix, like \"http\". This must be given.
  8416. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8417. This is obligatory as well.
  8418. :description Optional default description for the second pair
  8419. of brackets in an Org-mode link. The user can still change
  8420. this when inserting this link into an Org-mode buffer.
  8421. In addition to these, any additional properties can be specified
  8422. and then used in capture templates.")
  8423. (defun org-add-link-type (type &optional follow export)
  8424. "Add TYPE to the list of `org-link-types'.
  8425. Re-compute all regular expressions depending on `org-link-types'
  8426. FOLLOW and EXPORT are two functions.
  8427. FOLLOW should take the link path as the single argument and do whatever
  8428. is necessary to follow the link, for example find a file or display
  8429. a mail message.
  8430. EXPORT should format the link path for export to one of the export formats.
  8431. It should be a function accepting three arguments:
  8432. path the path of the link, the text after the prefix (like \"http:\")
  8433. desc the description of the link, if any, or a description added by
  8434. org-export-normalize-links if there is none
  8435. format the export format, a symbol like `html' or `latex' or `ascii'..
  8436. The function may use the FORMAT information to return different values
  8437. depending on the format. The return value will be put literally into
  8438. the exported file. If the return value is nil, this means Org should
  8439. do what it normally does with links which do not have EXPORT defined.
  8440. Org-mode has a built-in default for exporting links. If you are happy with
  8441. this default, there is no need to define an export function for the link
  8442. type. For a simple example of an export function, see `org-bbdb.el'."
  8443. (add-to-list 'org-link-types type t)
  8444. (org-make-link-regexps)
  8445. (if (assoc type org-link-protocols)
  8446. (setcdr (assoc type org-link-protocols) (list follow export))
  8447. (push (list type follow export) org-link-protocols)))
  8448. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8449. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8450. ;;;###autoload
  8451. (defun org-store-link (arg)
  8452. "\\<org-mode-map>Store an org-link to the current location.
  8453. This link is added to `org-stored-links' and can later be inserted
  8454. into an org-buffer with \\[org-insert-link].
  8455. For some link types, a prefix arg is interpreted.
  8456. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8457. For file links, arg negates `org-context-in-file-links'.
  8458. A double prefix arg force skipping storing functions that are not
  8459. part of Org's core.
  8460. A triple prefix arg force storing a link for each line in the
  8461. active region."
  8462. (interactive "P")
  8463. (org-load-modules-maybe)
  8464. (if (and (equal arg '(64)) (org-region-active-p))
  8465. (save-excursion
  8466. (let ((end (region-end)))
  8467. (goto-char (region-beginning))
  8468. (set-mark (point))
  8469. (while (< (point-at-eol) end)
  8470. (move-end-of-line 1) (activate-mark)
  8471. (let (current-prefix-arg)
  8472. (call-interactively 'org-store-link))
  8473. (move-beginning-of-line 2)
  8474. (set-mark (point)))))
  8475. (org-with-limited-levels
  8476. (setq org-store-link-plist nil)
  8477. (let (link cpltxt desc description search
  8478. txt custom-id agenda-link sfuns sfunsn)
  8479. (cond
  8480. ;; Store a link using an external link type
  8481. ((and (not (equal arg '(16)))
  8482. (setq sfuns
  8483. (delq
  8484. nil (mapcar (lambda (f)
  8485. (let (fs) (if (funcall f) (push f fs))))
  8486. org-store-link-functions))
  8487. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8488. (or (and (cdr sfuns)
  8489. (funcall (intern
  8490. (completing-read
  8491. "Which function for creating the link? "
  8492. sfunsn t (car sfunsn)))))
  8493. (funcall (caar sfuns)))
  8494. (setq link (plist-get org-store-link-plist :link)
  8495. desc (or (plist-get org-store-link-plist
  8496. :description) link))))
  8497. ;; Store a link from a source code buffer
  8498. ((org-src-edit-buffer-p)
  8499. (let (label gc)
  8500. (while (or (not label)
  8501. (save-excursion
  8502. (save-restriction
  8503. (widen)
  8504. (goto-char (point-min))
  8505. (re-search-forward
  8506. (regexp-quote (format org-coderef-label-format label))
  8507. nil t))))
  8508. (when label (message "Label exists already") (sit-for 2))
  8509. (setq label (read-string "Code line label: " label)))
  8510. (end-of-line 1)
  8511. (setq link (format org-coderef-label-format label))
  8512. (setq gc (- 79 (length link)))
  8513. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8514. (insert link)
  8515. (setq link (concat "(" label ")") desc nil)))
  8516. ;; We are in the agenda, link to referenced location
  8517. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8518. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8519. (get-text-property (point) 'org-marker))))
  8520. (when m
  8521. (org-with-point-at m
  8522. (setq agenda-link
  8523. (if (org-called-interactively-p 'any)
  8524. (call-interactively 'org-store-link)
  8525. (org-store-link nil)))))))
  8526. ((eq major-mode 'calendar-mode)
  8527. (let ((cd (calendar-cursor-to-date)))
  8528. (setq link
  8529. (format-time-string
  8530. (car org-time-stamp-formats)
  8531. (apply 'encode-time
  8532. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8533. nil nil nil))))
  8534. (org-store-link-props :type "calendar" :date cd)))
  8535. ((eq major-mode 'help-mode)
  8536. (setq link (concat "help:" (save-excursion
  8537. (goto-char (point-min))
  8538. (looking-at "^[^ ]+")
  8539. (match-string 0))))
  8540. (org-store-link-props :type "help"))
  8541. ((eq major-mode 'w3-mode)
  8542. (setq cpltxt (if (and (buffer-name)
  8543. (not (string-match "Untitled" (buffer-name))))
  8544. (buffer-name)
  8545. (url-view-url t))
  8546. link (url-view-url t))
  8547. (org-store-link-props :type "w3" :url (url-view-url t)))
  8548. ((eq major-mode 'image-mode)
  8549. (setq cpltxt (concat "file:"
  8550. (abbreviate-file-name buffer-file-name))
  8551. link cpltxt)
  8552. (org-store-link-props :type "image" :file buffer-file-name))
  8553. ;; In dired, store a link to the file of the current line
  8554. ((eq major-mode 'dired-mode)
  8555. (let ((file (dired-get-filename nil t)))
  8556. (setq file (if file
  8557. (abbreviate-file-name
  8558. (expand-file-name (dired-get-filename nil t)))
  8559. ;; otherwise, no file so use current directory.
  8560. default-directory))
  8561. (setq cpltxt (concat "file:" file)
  8562. link cpltxt)))
  8563. ((setq search (run-hook-with-args-until-success
  8564. 'org-create-file-search-functions))
  8565. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8566. "::" search))
  8567. (setq cpltxt (or description link)))
  8568. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8569. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8570. (cond
  8571. ;; Store a link using the target at point
  8572. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8573. (setq cpltxt
  8574. (concat "file:"
  8575. (abbreviate-file-name
  8576. (buffer-file-name (buffer-base-buffer)))
  8577. "::" (match-string 1))
  8578. link cpltxt))
  8579. ((and (featurep 'org-id)
  8580. (or (eq org-id-link-to-org-use-id t)
  8581. (and (org-called-interactively-p 'any)
  8582. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8583. (and (eq org-id-link-to-org-use-id
  8584. 'create-if-interactive-and-no-custom-id)
  8585. (not custom-id))))
  8586. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8587. ;; Store a link using the ID at point
  8588. (setq link (condition-case nil
  8589. (prog1 (org-id-store-link)
  8590. (setq desc (plist-get org-store-link-plist
  8591. :description)))
  8592. (error
  8593. ;; Probably before first headline, link only to file
  8594. (concat "file:"
  8595. (abbreviate-file-name
  8596. (buffer-file-name (buffer-base-buffer))))))))
  8597. (t
  8598. ;; Just link to current headline
  8599. (setq cpltxt (concat "file:"
  8600. (abbreviate-file-name
  8601. (buffer-file-name (buffer-base-buffer)))))
  8602. ;; Add a context search string
  8603. (when (org-xor org-context-in-file-links arg)
  8604. (let* ((ee (org-element-at-point))
  8605. (et (org-element-type ee))
  8606. (ev (plist-get (cadr ee) :value))
  8607. (ek (plist-get (cadr ee) :key))
  8608. (eok (and (stringp ek) (string-match "name" ek))))
  8609. (setq txt (cond
  8610. ((org-at-heading-p) nil)
  8611. ((and (eq et 'keyword) eok) ev)
  8612. ((org-region-active-p)
  8613. (buffer-substring (region-beginning) (region-end)))))
  8614. (when (or (null txt) (string-match "\\S-" txt))
  8615. (setq cpltxt
  8616. (concat cpltxt "::"
  8617. (condition-case nil
  8618. (org-make-org-heading-search-string txt)
  8619. (error "")))
  8620. desc (or (and (eq et 'keyword) eok ev)
  8621. (nth 4 (ignore-errors (org-heading-components)))
  8622. "NONE")))))
  8623. (if (string-match "::\\'" cpltxt)
  8624. (setq cpltxt (substring cpltxt 0 -2)))
  8625. (setq link cpltxt))))
  8626. ((buffer-file-name (buffer-base-buffer))
  8627. ;; Just link to this file here.
  8628. (setq cpltxt (concat "file:"
  8629. (abbreviate-file-name
  8630. (buffer-file-name (buffer-base-buffer)))))
  8631. ;; Add a context string.
  8632. (when (org-xor org-context-in-file-links arg)
  8633. (setq txt (if (org-region-active-p)
  8634. (buffer-substring (region-beginning) (region-end))
  8635. (buffer-substring (point-at-bol) (point-at-eol))))
  8636. ;; Only use search option if there is some text.
  8637. (when (string-match "\\S-" txt)
  8638. (setq cpltxt
  8639. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8640. desc "NONE")))
  8641. (setq link cpltxt))
  8642. ((org-called-interactively-p 'interactive)
  8643. (user-error "No method for storing a link from this buffer"))
  8644. (t (setq link nil)))
  8645. ;; We're done setting link and desc, clean up
  8646. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8647. (setq link (or link cpltxt)
  8648. desc (or desc cpltxt))
  8649. (cond ((equal desc "NONE") (setq desc nil))
  8650. ((string-match org-bracket-link-analytic-regexp desc)
  8651. (let ((d0 (match-string 3 desc))
  8652. (p0 (match-string 5 desc)))
  8653. (setq desc
  8654. (replace-regexp-in-string
  8655. org-bracket-link-regexp
  8656. (concat (or p0 d0)
  8657. (if (equal (length (match-string 0 desc))
  8658. (length desc)) "*" "")) desc)))))
  8659. ;; Return the link
  8660. (if (not (and (or (org-called-interactively-p 'any)
  8661. executing-kbd-macro) link))
  8662. (or agenda-link (and link (org-make-link-string link desc)))
  8663. (push (list link desc) org-stored-links)
  8664. (message "Stored: %s" (or desc link))
  8665. (when custom-id
  8666. (setq link (concat "file:" (abbreviate-file-name
  8667. (buffer-file-name)) "::#" custom-id))
  8668. (push (list link desc) org-stored-links)))))))
  8669. (defun org-store-link-props (&rest plist)
  8670. "Store link properties, extract names and addresses."
  8671. (let (x adr)
  8672. (when (setq x (plist-get plist :from))
  8673. (setq adr (mail-extract-address-components x))
  8674. (setq plist (plist-put plist :fromname (car adr)))
  8675. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8676. (when (setq x (plist-get plist :to))
  8677. (setq adr (mail-extract-address-components x))
  8678. (setq plist (plist-put plist :toname (car adr)))
  8679. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8680. (let ((from (plist-get plist :from))
  8681. (to (plist-get plist :to)))
  8682. (when (and from to org-from-is-user-regexp)
  8683. (setq plist
  8684. (plist-put plist :fromto
  8685. (if (string-match org-from-is-user-regexp from)
  8686. (concat "to %t")
  8687. (concat "from %f"))))))
  8688. (setq org-store-link-plist plist))
  8689. (defun org-add-link-props (&rest plist)
  8690. "Add these properties to the link property list."
  8691. (let (key value)
  8692. (while plist
  8693. (setq key (pop plist) value (pop plist))
  8694. (setq org-store-link-plist
  8695. (plist-put org-store-link-plist key value)))))
  8696. (defun org-email-link-description (&optional fmt)
  8697. "Return the description part of an email link.
  8698. This takes information from `org-store-link-plist' and formats it
  8699. according to FMT (default from `org-email-link-description-format')."
  8700. (setq fmt (or fmt org-email-link-description-format))
  8701. (let* ((p org-store-link-plist)
  8702. (to (plist-get p :toaddress))
  8703. (from (plist-get p :fromaddress))
  8704. (table
  8705. (list
  8706. (cons "%c" (plist-get p :fromto))
  8707. (cons "%F" (plist-get p :from))
  8708. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8709. (cons "%T" (plist-get p :to))
  8710. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8711. (cons "%s" (plist-get p :subject))
  8712. (cons "%d" (plist-get p :date))
  8713. (cons "%m" (plist-get p :message-id)))))
  8714. (when (string-match "%c" fmt)
  8715. ;; Check if the user wrote this message
  8716. (if (and org-from-is-user-regexp from to
  8717. (save-match-data (string-match org-from-is-user-regexp from)))
  8718. (setq fmt (replace-match "to %t" t t fmt))
  8719. (setq fmt (replace-match "from %f" t t fmt))))
  8720. (org-replace-escapes fmt table)))
  8721. (defun org-make-org-heading-search-string (&optional string)
  8722. "Make search string for the current headline or STRING."
  8723. (let ((s (or string
  8724. (and (derived-mode-p 'org-mode)
  8725. (save-excursion
  8726. (org-back-to-heading t)
  8727. (org-element-property :raw-value (org-element-at-point))))))
  8728. (lines org-context-in-file-links))
  8729. (or string (setq s (concat "*" s))) ; Add * for headlines
  8730. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8731. (when (and string (integerp lines) (> lines 0))
  8732. (let ((slines (org-split-string s "\n")))
  8733. (when (< lines (length slines))
  8734. (setq s (mapconcat
  8735. 'identity
  8736. (reverse (nthcdr (- (length slines) lines)
  8737. (reverse slines))) "\n")))))
  8738. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8739. (defun org-make-link-string (link &optional description)
  8740. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8741. (unless (string-match "\\S-" link)
  8742. (error "Empty link"))
  8743. (when (and description
  8744. (stringp description)
  8745. (not (string-match "\\S-" description)))
  8746. (setq description nil))
  8747. (when (stringp description)
  8748. ;; Remove brackets from the description, they are fatal.
  8749. (while (string-match "\\[" description)
  8750. (setq description (replace-match "{" t t description)))
  8751. (while (string-match "\\]" description)
  8752. (setq description (replace-match "}" t t description))))
  8753. (when (equal link description)
  8754. ;; No description needed, it is identical
  8755. (setq description nil))
  8756. (when (and (not description)
  8757. (not (string-match (org-image-file-name-regexp) link))
  8758. (not (equal link (org-link-escape link))))
  8759. (setq description (org-extract-attributes link)))
  8760. (setq link
  8761. (cond ((string-match (org-image-file-name-regexp) link) link)
  8762. ((string-match org-link-types-re link)
  8763. (concat (match-string 1 link)
  8764. (org-link-escape (substring link (match-end 1)))))
  8765. (t (org-link-escape link))))
  8766. (concat "[[" link "]"
  8767. (if description (concat "[" description "]") "")
  8768. "]"))
  8769. (defconst org-link-escape-chars
  8770. ;;%20 %2B %3B %3D %5B %5D
  8771. '(?\ ?\+ ?\; ?\= ?\[ ?\])
  8772. "List of characters that should be escaped in link.
  8773. This is the list that is used for internal purposes.")
  8774. (defconst org-link-escape-chars-browser
  8775. ;;%20 %22
  8776. '(?\ ?\")
  8777. "List of escapes for characters that are problematic in links.
  8778. This is the list that is used before handing over to the browser.")
  8779. (defun org-link-escape (text &optional table merge)
  8780. "Return percent escaped representation of TEXT.
  8781. TEXT is a string with the text to escape.
  8782. Optional argument TABLE is a list with characters that should be
  8783. escaped. When nil, `org-link-escape-chars' is used.
  8784. If optional argument MERGE is set, merge TABLE into
  8785. `org-link-escape-chars'."
  8786. (cond
  8787. ((and table merge)
  8788. (mapc (lambda (defchr)
  8789. (unless (member defchr table)
  8790. (setq table (cons defchr table)))) org-link-escape-chars))
  8791. ((null table)
  8792. (setq table org-link-escape-chars)))
  8793. (mapconcat
  8794. (lambda (char)
  8795. (if (or (member char table)
  8796. (and (or (< char 32) (= char ?\%) (> char 126))
  8797. org-url-hexify-p))
  8798. (mapconcat (lambda (sequence-element)
  8799. (format "%%%.2X" sequence-element))
  8800. (or (encode-coding-char char 'utf-8)
  8801. (error "Unable to percent escape character: %s"
  8802. (char-to-string char))) "")
  8803. (char-to-string char))) text ""))
  8804. (defun org-link-escape-browser (text)
  8805. (if (org-string-match-p
  8806. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8807. text)
  8808. (org-link-escape text org-link-escape-chars-browser)
  8809. text))
  8810. (defun org-link-unescape (str)
  8811. "Unhex hexified Unicode strings as returned from the JavaScript function
  8812. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8813. (unless (and (null str) (string= "" str))
  8814. (let ((pos 0) (case-fold-search t) unhexed)
  8815. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8816. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8817. (setq str (replace-match unhexed t t str))
  8818. (setq pos (+ pos (length unhexed))))))
  8819. str)
  8820. (defun org-link-unescape-compound (hex)
  8821. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8822. Note: this function also decodes single byte encodings like
  8823. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8824. (save-match-data
  8825. (let* ((bytes (cdr (split-string hex "%")))
  8826. (ret "")
  8827. (eat 0)
  8828. (sum 0))
  8829. (while bytes
  8830. (let* ((val (string-to-number (pop bytes) 16))
  8831. (shift-xor
  8832. (if (= 0 eat)
  8833. (cond
  8834. ((>= val 252) (cons 6 252))
  8835. ((>= val 248) (cons 5 248))
  8836. ((>= val 240) (cons 4 240))
  8837. ((>= val 224) (cons 3 224))
  8838. ((>= val 192) (cons 2 192))
  8839. (t (cons 0 0)))
  8840. (cons 6 128))))
  8841. (if (>= val 192) (setq eat (car shift-xor)))
  8842. (setq val (logxor val (cdr shift-xor)))
  8843. (setq sum (+ (lsh sum (car shift-xor)) val))
  8844. (if (> eat 0) (setq eat (- eat 1)))
  8845. (cond
  8846. ((= 0 eat) ;multi byte
  8847. (setq ret (concat ret (org-char-to-string sum)))
  8848. (setq sum 0))
  8849. ((not bytes) ; single byte(s)
  8850. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8851. )) ;; end (while bytes
  8852. ret )))
  8853. (defun org-link-unescape-single-byte-sequence (hex)
  8854. "Unhexify hex-encoded single byte character sequences."
  8855. (mapconcat (lambda (byte)
  8856. (char-to-string (string-to-number byte 16)))
  8857. (cdr (split-string hex "%")) ""))
  8858. (defun org-xor (a b)
  8859. "Exclusive or."
  8860. (if a (not b) b))
  8861. (defun org-fixup-message-id-for-http (s)
  8862. "Replace special characters in a message id, so it can be used in an http query."
  8863. (when (string-match "%" s)
  8864. (setq s (mapconcat (lambda (c)
  8865. (if (eq c ?%)
  8866. "%25"
  8867. (char-to-string c)))
  8868. s "")))
  8869. (while (string-match "<" s)
  8870. (setq s (replace-match "%3C" t t s)))
  8871. (while (string-match ">" s)
  8872. (setq s (replace-match "%3E" t t s)))
  8873. (while (string-match "@" s)
  8874. (setq s (replace-match "%40" t t s)))
  8875. s)
  8876. (defun org-link-prettify (link)
  8877. "Return a human-readable representation of LINK.
  8878. The car of LINK must be a raw link the cdr of LINK must be either
  8879. a link description or nil."
  8880. (let ((desc (or (cadr link) "<no description>")))
  8881. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8882. "<" (car link) ">")))
  8883. ;;;###autoload
  8884. (defun org-insert-link-global ()
  8885. "Insert a link like Org-mode does.
  8886. This command can be called in any mode to insert a link in Org-mode syntax."
  8887. (interactive)
  8888. (org-load-modules-maybe)
  8889. (org-run-like-in-org-mode 'org-insert-link))
  8890. (defun org-insert-all-links (&optional keep)
  8891. "Insert all links in `org-stored-links'."
  8892. (interactive "P")
  8893. (let ((links (copy-sequence org-stored-links)) l)
  8894. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8895. (insert "- ")
  8896. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8897. (insert "\n"))))
  8898. (defun org-link-fontify-links-to-this-file ()
  8899. "Fontify links to the current file in `org-stored-links'."
  8900. (let ((f (buffer-file-name)) a b)
  8901. (setq a (mapcar (lambda(l)
  8902. (let ((ll (car l)))
  8903. (when (and (string-match "^file:\\(.+\\)::" ll)
  8904. (equal f (expand-file-name (match-string 1 ll))))
  8905. ll)))
  8906. org-stored-links))
  8907. (when (featurep 'org-id)
  8908. (setq b (mapcar (lambda(l)
  8909. (let ((ll (car l)))
  8910. (when (and (string-match "^id:\\(.+\\)$" ll)
  8911. (equal f (expand-file-name
  8912. (or (org-id-find-id-file
  8913. (match-string 1 ll)) ""))))
  8914. ll)))
  8915. org-stored-links)))
  8916. (mapcar (lambda(l)
  8917. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8918. (delq nil (append a b)))))
  8919. (defvar org-link-links-in-this-file nil)
  8920. (defun org-insert-link (&optional complete-file link-location default-description)
  8921. "Insert a link. At the prompt, enter the link.
  8922. Completion can be used to insert any of the link protocol prefixes like
  8923. http or ftp in use.
  8924. The history can be used to select a link previously stored with
  8925. `org-store-link'. When the empty string is entered (i.e. if you just
  8926. press RET at the prompt), the link defaults to the most recently
  8927. stored link. As SPC triggers completion in the minibuffer, you need to
  8928. use M-SPC or C-q SPC to force the insertion of a space character.
  8929. You will also be prompted for a description, and if one is given, it will
  8930. be displayed in the buffer instead of the link.
  8931. If there is already a link at point, this command will allow you to edit link
  8932. and description parts.
  8933. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8934. be selected using completion. The path to the file will be relative to the
  8935. current directory if the file is in the current directory or a subdirectory.
  8936. Otherwise, the link will be the absolute path as completed in the minibuffer
  8937. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8938. option `org-link-file-path-type'.
  8939. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8940. the current directory or below.
  8941. With three \\[universal-argument] prefixes, negate the meaning of
  8942. `org-keep-stored-link-after-insertion'.
  8943. If `org-make-link-description-function' is non-nil, this function will be
  8944. called with the link target, and the result will be the default
  8945. link description.
  8946. If the LINK-LOCATION parameter is non-nil, this value will be
  8947. used as the link location instead of reading one interactively.
  8948. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8949. be used as the default description."
  8950. (interactive "P")
  8951. (let* ((wcf (current-window-configuration))
  8952. (origbuf (current-buffer))
  8953. (region (if (org-region-active-p)
  8954. (buffer-substring (region-beginning) (region-end))))
  8955. (remove (and region (list (region-beginning) (region-end))))
  8956. (desc region)
  8957. tmphist ; byte-compile incorrectly complains about this
  8958. (link link-location)
  8959. (abbrevs org-link-abbrev-alist-local)
  8960. entry file all-prefixes auto-desc)
  8961. (cond
  8962. (link-location) ; specified by arg, just use it.
  8963. ((org-in-regexp org-bracket-link-regexp 1)
  8964. ;; We do have a link at point, and we are going to edit it.
  8965. (setq remove (list (match-beginning 0) (match-end 0)))
  8966. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8967. (setq link (read-string "Link: "
  8968. (org-link-unescape
  8969. (org-match-string-no-properties 1)))))
  8970. ((or (org-in-regexp org-angle-link-re)
  8971. (org-in-regexp org-plain-link-re))
  8972. ;; Convert to bracket link
  8973. (setq remove (list (match-beginning 0) (match-end 0))
  8974. link (read-string "Link: "
  8975. (org-remove-angle-brackets (match-string 0)))))
  8976. ((member complete-file '((4) (16)))
  8977. ;; Completing read for file names.
  8978. (setq link (org-file-complete-link complete-file)))
  8979. (t
  8980. ;; Read link, with completion for stored links.
  8981. (org-link-fontify-links-to-this-file)
  8982. (org-switch-to-buffer-other-window "*Org Links*")
  8983. (with-current-buffer "*Org Links*"
  8984. (erase-buffer)
  8985. (insert "Insert a link.
  8986. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8987. (when org-stored-links
  8988. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8989. (insert (mapconcat 'org-link-prettify
  8990. (reverse org-stored-links) "\n")))
  8991. (goto-char (point-min)))
  8992. (let ((cw (selected-window)))
  8993. (select-window (get-buffer-window "*Org Links*" 'visible))
  8994. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8995. (unless (pos-visible-in-window-p (point-max))
  8996. (org-fit-window-to-buffer))
  8997. (and (window-live-p cw) (select-window cw)))
  8998. ;; Fake a link history, containing the stored links.
  8999. (setq tmphist (append (mapcar 'car org-stored-links)
  9000. org-insert-link-history))
  9001. (setq all-prefixes (append (mapcar 'car abbrevs)
  9002. (mapcar 'car org-link-abbrev-alist)
  9003. org-link-types))
  9004. (unwind-protect
  9005. (progn
  9006. (setq link
  9007. (org-completing-read
  9008. "Link: "
  9009. (append
  9010. (mapcar (lambda (x) (concat x ":"))
  9011. all-prefixes)
  9012. (mapcar 'car org-stored-links))
  9013. nil nil nil
  9014. 'tmphist
  9015. (caar org-stored-links)))
  9016. (if (not (string-match "\\S-" link))
  9017. (user-error "No link selected"))
  9018. (mapc (lambda(l)
  9019. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9020. org-stored-links)
  9021. (if (or (member link all-prefixes)
  9022. (and (equal ":" (substring link -1))
  9023. (member (substring link 0 -1) all-prefixes)
  9024. (setq link (substring link 0 -1))))
  9025. (setq link (with-current-buffer origbuf
  9026. (org-link-try-special-completion link)))))
  9027. (set-window-configuration wcf)
  9028. (kill-buffer "*Org Links*"))
  9029. (setq entry (assoc link org-stored-links))
  9030. (or entry (push link org-insert-link-history))
  9031. (setq desc (or desc (nth 1 entry)))))
  9032. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9033. (not org-keep-stored-link-after-insertion))
  9034. (setq org-stored-links (delq (assoc link org-stored-links)
  9035. org-stored-links)))
  9036. (if (string-match org-plain-link-re link)
  9037. ;; URL-like link, normalize the use of angular brackets.
  9038. (setq link (org-remove-angle-brackets link)))
  9039. ;; Check if we are linking to the current file with a search
  9040. ;; option If yes, simplify the link by using only the search
  9041. ;; option.
  9042. (when (and buffer-file-name
  9043. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9044. (let* ((path (match-string 1 link))
  9045. (case-fold-search nil)
  9046. (search (match-string 2 link)))
  9047. (save-match-data
  9048. (if (equal (file-truename buffer-file-name) (file-truename path))
  9049. ;; We are linking to this same file, with a search option
  9050. (setq link search)))))
  9051. ;; Check if we can/should use a relative path. If yes, simplify the link
  9052. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9053. (let* ((type (match-string 1 link))
  9054. (path (match-string 2 link))
  9055. (origpath path)
  9056. (case-fold-search nil))
  9057. (cond
  9058. ((or (eq org-link-file-path-type 'absolute)
  9059. (equal complete-file '(16)))
  9060. (setq path (abbreviate-file-name (expand-file-name path))))
  9061. ((eq org-link-file-path-type 'noabbrev)
  9062. (setq path (expand-file-name path)))
  9063. ((eq org-link-file-path-type 'relative)
  9064. (setq path (file-relative-name path)))
  9065. (t
  9066. (save-match-data
  9067. (if (string-match (concat "^" (regexp-quote
  9068. (expand-file-name
  9069. (file-name-as-directory
  9070. default-directory))))
  9071. (expand-file-name path))
  9072. ;; We are linking a file with relative path name.
  9073. (setq path (substring (expand-file-name path)
  9074. (match-end 0)))
  9075. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9076. (setq link (concat type path))
  9077. (if (equal desc origpath)
  9078. (setq desc path))))
  9079. (if org-make-link-description-function
  9080. (setq desc
  9081. (or (condition-case nil
  9082. (funcall org-make-link-description-function link desc)
  9083. (error (progn (message "Can't get link description from `%s'"
  9084. (symbol-name org-make-link-description-function))
  9085. (sit-for 2) nil)))
  9086. (read-string "Description: " default-description)))
  9087. (if default-description (setq desc default-description)
  9088. (setq desc (or (and auto-desc desc)
  9089. (read-string "Description: " desc)))))
  9090. (unless (string-match "\\S-" desc) (setq desc nil))
  9091. (if remove (apply 'delete-region remove))
  9092. (insert (org-make-link-string link desc))))
  9093. (defun org-link-try-special-completion (type)
  9094. "If there is completion support for link type TYPE, offer it."
  9095. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9096. (if (functionp fun)
  9097. (funcall fun)
  9098. (read-string "Link (no completion support): " (concat type ":")))))
  9099. (defun org-file-complete-link (&optional arg)
  9100. "Create a file link using completion."
  9101. (let (file link)
  9102. (setq file (org-iread-file-name "File: "))
  9103. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9104. (pwd1 (file-name-as-directory (abbreviate-file-name
  9105. (expand-file-name ".")))))
  9106. (cond
  9107. ((equal arg '(16))
  9108. (setq link (concat
  9109. "file:"
  9110. (abbreviate-file-name (expand-file-name file)))))
  9111. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9112. (setq link (concat "file:" (match-string 1 file))))
  9113. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9114. (expand-file-name file))
  9115. (setq link (concat
  9116. "file:" (match-string 1 (expand-file-name file)))))
  9117. (t (setq link (concat "file:" file)))))
  9118. link))
  9119. (defun org-iread-file-name (&rest args)
  9120. "Read-file-name using `ido-mode' speedup if available.
  9121. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9122. See `read-file-name' for a description of parameters."
  9123. (org-without-partial-completion
  9124. (if (and org-completion-use-ido
  9125. (fboundp 'ido-read-file-name)
  9126. (boundp 'ido-mode) ido-mode
  9127. (listp (second args)))
  9128. (let ((ido-enter-matching-directory nil))
  9129. (apply 'ido-read-file-name args))
  9130. (apply 'read-file-name args))))
  9131. (defun org-completing-read (&rest args)
  9132. "Completing-read with SPACE being a normal character."
  9133. (let ((enable-recursive-minibuffers t)
  9134. (minibuffer-local-completion-map
  9135. (copy-keymap minibuffer-local-completion-map)))
  9136. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9137. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9138. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9139. (apply 'org-icompleting-read args)))
  9140. (defun org-completing-read-no-i (&rest args)
  9141. (let (org-completion-use-ido org-completion-use-iswitchb)
  9142. (apply 'org-completing-read args)))
  9143. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9144. "Use iswitch as a completing-read replacement to choose from choices.
  9145. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9146. from."
  9147. (let* ((iswitchb-use-virtual-buffers nil)
  9148. (iswitchb-make-buflist-hook
  9149. (lambda ()
  9150. (setq iswitchb-temp-buflist choices))))
  9151. (iswitchb-read-buffer prompt)))
  9152. (defun org-icompleting-read (&rest args)
  9153. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9154. (org-without-partial-completion
  9155. (if (and org-completion-use-ido
  9156. (fboundp 'ido-completing-read)
  9157. (boundp 'ido-mode) ido-mode
  9158. (listp (second args)))
  9159. (let ((ido-enter-matching-directory nil))
  9160. (apply 'ido-completing-read (concat (car args))
  9161. (if (consp (car (nth 1 args)))
  9162. (mapcar 'car (nth 1 args))
  9163. (nth 1 args))
  9164. (cddr args)))
  9165. (if (and org-completion-use-iswitchb
  9166. (boundp 'iswitchb-mode) iswitchb-mode
  9167. (listp (second args)))
  9168. (apply 'org-iswitchb-completing-read (concat (car args))
  9169. (if (consp (car (nth 1 args)))
  9170. (mapcar 'car (nth 1 args))
  9171. (nth 1 args))
  9172. (cddr args))
  9173. (apply 'completing-read args)))))
  9174. (defun org-extract-attributes (s)
  9175. "Extract the attributes cookie from a string and set as text property."
  9176. (let (a attr (start 0) key value)
  9177. (save-match-data
  9178. (when (string-match "{{\\([^}]+\\)}}$" s)
  9179. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9180. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9181. (setq key (match-string 1 a) value (match-string 2 a)
  9182. start (match-end 0)
  9183. attr (plist-put attr (intern key) value))))
  9184. (org-add-props s nil 'org-attr attr))
  9185. s))
  9186. ;;; Opening/following a link
  9187. (defvar org-link-search-failed nil)
  9188. (defvar org-open-link-functions nil
  9189. "Hook for functions finding a plain text link.
  9190. These functions must take a single argument, the link content.
  9191. They will be called for links that look like [[link text][description]]
  9192. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9193. like a filename (e.g. \"./blue.png\").
  9194. These functions will be called *before* Org attempts to resolve the
  9195. link by doing text searches in the current buffer - so if you want a
  9196. link \"[[target]]\" to still find \"<<target>>\", your function should
  9197. handle this as a special case.
  9198. When the function does handle the link, it must return a non-nil value.
  9199. If it decides that it is not responsible for this link, it must return
  9200. nil to indicate that that Org-mode can continue with other options
  9201. like exact and fuzzy text search.")
  9202. (defun org-next-link (&optional search-backward)
  9203. "Move forward to the next link.
  9204. If the link is in hidden text, expose it."
  9205. (interactive "P")
  9206. (when (and org-link-search-failed (eq this-command last-command))
  9207. (goto-char (point-min))
  9208. (message "Link search wrapped back to beginning of buffer"))
  9209. (setq org-link-search-failed nil)
  9210. (let* ((pos (point))
  9211. (ct (org-context))
  9212. (a (assoc :link ct))
  9213. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9214. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9215. ((looking-at org-any-link-re)
  9216. ;; Don't stay stuck at link without an org-link face
  9217. (forward-char (if search-backward -1 1))))
  9218. (if (funcall srch-fun org-any-link-re nil t)
  9219. (progn
  9220. (goto-char (match-beginning 0))
  9221. (if (outline-invisible-p) (org-show-context)))
  9222. (goto-char pos)
  9223. (setq org-link-search-failed t)
  9224. (message "No further link found"))))
  9225. (defun org-previous-link ()
  9226. "Move backward to the previous link.
  9227. If the link is in hidden text, expose it."
  9228. (interactive)
  9229. (funcall 'org-next-link t))
  9230. (defun org-translate-link (s)
  9231. "Translate a link string if a translation function has been defined."
  9232. (if (and org-link-translation-function
  9233. (fboundp org-link-translation-function)
  9234. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9235. (progn
  9236. (setq s (funcall org-link-translation-function
  9237. (match-string 1 s) (match-string 2 s)))
  9238. (concat (car s) ":" (cdr s)))
  9239. s))
  9240. (defun org-translate-link-from-planner (type path)
  9241. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9242. This is still an experimental function, your mileage may vary."
  9243. (cond
  9244. ((member type '("http" "https" "news" "ftp"))
  9245. ;; standard Internet links are the same.
  9246. nil)
  9247. ((and (equal type "irc") (string-match "^//" path))
  9248. ;; Planner has two / at the beginning of an irc link, we have 1.
  9249. ;; We should have zero, actually....
  9250. (setq path (substring path 1)))
  9251. ((and (equal type "lisp") (string-match "^/" path))
  9252. ;; Planner has a slash, we do not.
  9253. (setq type "elisp" path (substring path 1)))
  9254. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9255. ;; A typical message link. Planner has the id after the final slash,
  9256. ;; we separate it with a hash mark
  9257. (setq path (concat (match-string 1 path) "#"
  9258. (org-remove-angle-brackets (match-string 2 path))))))
  9259. (cons type path))
  9260. (defun org-find-file-at-mouse (ev)
  9261. "Open file link or URL at mouse."
  9262. (interactive "e")
  9263. (mouse-set-point ev)
  9264. (org-open-at-point 'in-emacs))
  9265. (defun org-open-at-mouse (ev)
  9266. "Open file link or URL at mouse.
  9267. See the docstring of `org-open-file' for details."
  9268. (interactive "e")
  9269. (mouse-set-point ev)
  9270. (if (eq major-mode 'org-agenda-mode)
  9271. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9272. (org-open-at-point))
  9273. (defvar org-window-config-before-follow-link nil
  9274. "The window configuration before following a link.
  9275. This is saved in case the need arises to restore it.")
  9276. (defvar org-open-link-marker (make-marker)
  9277. "Marker pointing to the location where `org-open-at-point; was called.")
  9278. ;;;###autoload
  9279. (defun org-open-at-point-global ()
  9280. "Follow a link like Org-mode does.
  9281. This command can be called in any mode to follow a link that has
  9282. Org-mode syntax."
  9283. (interactive)
  9284. (org-run-like-in-org-mode 'org-open-at-point))
  9285. ;;;###autoload
  9286. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9287. "Open a link in the string S, as if it was in Org-mode."
  9288. (interactive "sLink: \nP")
  9289. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9290. (with-temp-buffer
  9291. (let ((org-inhibit-startup (not reference-buffer)))
  9292. (org-mode)
  9293. (insert s)
  9294. (goto-char (point-min))
  9295. (when reference-buffer
  9296. (setq org-link-abbrev-alist-local
  9297. (with-current-buffer reference-buffer
  9298. org-link-abbrev-alist-local)))
  9299. (org-open-at-point arg reference-buffer)))))
  9300. (defvar org-open-at-point-functions nil
  9301. "Hook that is run when following a link at point.
  9302. Functions in this hook must return t if they identify and follow
  9303. a link at point. If they don't find anything interesting at point,
  9304. they must return nil.")
  9305. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9306. (defun org-open-at-point (&optional arg reference-buffer)
  9307. "Open link at or after point.
  9308. If there is no link at point, this function will search forward up to
  9309. the end of the current line.
  9310. Normally, files will be opened by an appropriate application. If the
  9311. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9312. With a double prefix argument, try to open outside of Emacs, in the
  9313. application the system uses for this file type."
  9314. (interactive "P")
  9315. ;; if in a code block, then open the block's results
  9316. (unless (call-interactively #'org-babel-open-src-block-result)
  9317. (org-load-modules-maybe)
  9318. (move-marker org-open-link-marker (point))
  9319. (setq org-window-config-before-follow-link (current-window-configuration))
  9320. (org-remove-occur-highlights nil nil t)
  9321. (cond
  9322. ((and (org-at-heading-p)
  9323. (not (org-at-timestamp-p t))
  9324. (not (org-in-regexp
  9325. (concat org-plain-link-re "\\|"
  9326. org-bracket-link-regexp "\\|"
  9327. org-angle-link-re "\\|"
  9328. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9329. (not (get-text-property (point) 'org-linked-text)))
  9330. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9331. (lk0 (car lkall))
  9332. (lk (if (stringp lk0) (list lk0) lk0))
  9333. (lkend (cdr lkall)))
  9334. (mapcar (lambda(l)
  9335. (search-forward l nil lkend)
  9336. (goto-char (match-beginning 0))
  9337. (org-open-at-point))
  9338. lk))
  9339. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9340. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9341. ((and (org-at-timestamp-p t)
  9342. (not (org-in-regexp org-bracket-link-regexp)))
  9343. (org-follow-timestamp-link))
  9344. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9345. (not (org-in-regexp org-any-link-re)))
  9346. (org-footnote-action))
  9347. (t
  9348. (let (type path link line search (pos (point)))
  9349. (catch 'match
  9350. (save-excursion
  9351. (skip-chars-forward "^]\n\r")
  9352. (when (org-in-regexp org-bracket-link-regexp 1)
  9353. (setq link (org-extract-attributes
  9354. (org-link-unescape (org-match-string-no-properties 1))))
  9355. (while (string-match " *\n *" link)
  9356. (setq link (replace-match " " t t link)))
  9357. (setq link (org-link-expand-abbrev link))
  9358. (cond
  9359. ((or (file-name-absolute-p link)
  9360. (string-match "^\\.\\.?/" link))
  9361. (setq type "file" path link))
  9362. ((string-match org-link-re-with-space3 link)
  9363. (setq type (match-string 1 link) path (match-string 2 link)))
  9364. ((string-match "^help:+\\(.+\\)" link)
  9365. (setq type "help" path (match-string 1 link)))
  9366. (t (setq type "thisfile" path link)))
  9367. (throw 'match t)))
  9368. (when (get-text-property (point) 'org-linked-text)
  9369. (setq type "thisfile"
  9370. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9371. (1+ (point)) (point))
  9372. path (buffer-substring
  9373. (or (previous-single-property-change pos 'org-linked-text)
  9374. (point-min))
  9375. (or (next-single-property-change pos 'org-linked-text)
  9376. (point-max)))
  9377. ;; Ensure we will search for a <<<radio>>> link, not
  9378. ;; a simple reference like <<ref>>
  9379. path (concat "<" path))
  9380. (throw 'match t))
  9381. (save-excursion
  9382. (when (or (org-in-regexp org-angle-link-re)
  9383. (let ((match (org-in-regexp org-plain-link-re)))
  9384. ;; Check a plain link is not within a bracket link
  9385. (and match
  9386. (save-excursion
  9387. (progn
  9388. (goto-char (car match))
  9389. (not (org-in-regexp org-bracket-link-regexp))))))
  9390. (let ((line_ending (save-excursion (end-of-line) (point))))
  9391. ;; We are in a line before a plain or bracket link
  9392. (or (re-search-forward org-plain-link-re line_ending t)
  9393. (re-search-forward org-bracket-link-regexp line_ending t))))
  9394. (setq type (match-string 1)
  9395. path (org-link-unescape (match-string 2)))
  9396. (throw 'match t)))
  9397. (save-excursion
  9398. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9399. (setq type "tags"
  9400. path (match-string 1))
  9401. (while (string-match ":" path)
  9402. (setq path (replace-match "+" t t path)))
  9403. (throw 'match t)))
  9404. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9405. (setq type "tree-match"
  9406. path (match-string 1))
  9407. (throw 'match t)))
  9408. (unless path
  9409. (user-error "No link found"))
  9410. ;; switch back to reference buffer
  9411. ;; needed when if called in a temporary buffer through
  9412. ;; org-open-link-from-string
  9413. (with-current-buffer (or reference-buffer (current-buffer))
  9414. ;; Remove any trailing spaces in path
  9415. (if (string-match " +\\'" path)
  9416. (setq path (replace-match "" t t path)))
  9417. (if (and org-link-translation-function
  9418. (fboundp org-link-translation-function))
  9419. ;; Check if we need to translate the link
  9420. (let ((tmp (funcall org-link-translation-function type path)))
  9421. (setq type (car tmp) path (cdr tmp))))
  9422. (cond
  9423. ((assoc type org-link-protocols)
  9424. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9425. ((equal type "help")
  9426. (let ((f-or-v (intern path)))
  9427. (cond ((fboundp f-or-v)
  9428. (describe-function f-or-v))
  9429. ((boundp f-or-v)
  9430. (describe-variable f-or-v))
  9431. (t (error "Not a known function or variable")))))
  9432. ((equal type "mailto")
  9433. (let ((cmd (car org-link-mailto-program))
  9434. (args (cdr org-link-mailto-program)) args1
  9435. (address path) (subject "") a)
  9436. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9437. (setq address (match-string 1 path)
  9438. subject (org-link-escape (match-string 2 path))))
  9439. (while args
  9440. (cond
  9441. ((not (stringp (car args))) (push (pop args) args1))
  9442. (t (setq a (pop args))
  9443. (if (string-match "%a" a)
  9444. (setq a (replace-match address t t a)))
  9445. (if (string-match "%s" a)
  9446. (setq a (replace-match subject t t a)))
  9447. (push a args1))))
  9448. (apply cmd (nreverse args1))))
  9449. ((member type '("http" "https" "ftp" "news"))
  9450. (browse-url (concat type ":" (org-link-escape-browser path))))
  9451. ((string= type "doi")
  9452. (browse-url (concat org-doi-server-url
  9453. (org-link-escape-browser path))))
  9454. ((member type '("message"))
  9455. (browse-url (concat type ":" path)))
  9456. ((string= type "tags")
  9457. (org-tags-view arg path))
  9458. ((string= type "tree-match")
  9459. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9460. ((string= type "file")
  9461. (if (string-match "::\\([0-9]+\\)\\'" path)
  9462. (setq line (string-to-number (match-string 1 path))
  9463. path (substring path 0 (match-beginning 0)))
  9464. (if (string-match "::\\(.+\\)\\'" path)
  9465. (setq search (match-string 1 path)
  9466. path (substring path 0 (match-beginning 0)))))
  9467. (if (string-match "[*?{]" (file-name-nondirectory path))
  9468. (dired path)
  9469. (org-open-file path arg line search)))
  9470. ((string= type "shell")
  9471. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9472. (cmd path))
  9473. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9474. (string-match org-confirm-shell-link-not-regexp cmd))
  9475. (not org-confirm-shell-link-function)
  9476. (funcall org-confirm-shell-link-function
  9477. (format "Execute \"%s\" in shell? "
  9478. (org-add-props cmd nil
  9479. 'face 'org-warning))))
  9480. (progn
  9481. (message "Executing %s" cmd)
  9482. (shell-command cmd buf)
  9483. (if (featurep 'midnight)
  9484. (setq clean-buffer-list-kill-buffer-names
  9485. (cons buf clean-buffer-list-kill-buffer-names))))
  9486. (error "Abort"))))
  9487. ((string= type "elisp")
  9488. (let ((cmd path))
  9489. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9490. (string-match org-confirm-elisp-link-not-regexp cmd))
  9491. (not org-confirm-elisp-link-function)
  9492. (funcall org-confirm-elisp-link-function
  9493. (format "Execute \"%s\" as elisp? "
  9494. (org-add-props cmd nil
  9495. 'face 'org-warning))))
  9496. (message "%s => %s" cmd
  9497. (if (equal (string-to-char cmd) ?\()
  9498. (eval (read cmd))
  9499. (call-interactively (read cmd))))
  9500. (error "Abort"))))
  9501. ((and (string= type "thisfile")
  9502. (or (run-hook-with-args-until-success
  9503. 'org-open-link-functions path)
  9504. (and link
  9505. (string-match "^id:" link)
  9506. (or (featurep 'org-id) (require 'org-id))
  9507. (progn
  9508. (funcall (nth 1 (assoc "id" org-link-protocols))
  9509. (substring path 3))
  9510. t)))))
  9511. ((string= type "thisfile")
  9512. (if arg
  9513. (switch-to-buffer-other-window
  9514. (org-get-buffer-for-internal-link (current-buffer)))
  9515. (org-mark-ring-push))
  9516. (let ((cmd `(org-link-search
  9517. ,path
  9518. ,(cond ((equal arg '(4)) ''occur)
  9519. ((equal arg '(16)) ''org-occur))
  9520. ,pos)))
  9521. (condition-case nil (let ((org-link-search-inhibit-query t))
  9522. (eval cmd))
  9523. (error (progn (widen) (eval cmd))))))
  9524. (t (browse-url-at-point)))))))
  9525. (move-marker org-open-link-marker nil)
  9526. (run-hook-with-args 'org-follow-link-hook)))
  9527. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9528. "Offer links in the current entry and return the selected link.
  9529. If there is only one link, return it.
  9530. If NTH is an integer, return the NTH link found.
  9531. If ZERO is a string, check also this string for a link, and if
  9532. there is one, return it."
  9533. (with-current-buffer buffer
  9534. (save-excursion
  9535. (save-restriction
  9536. (widen)
  9537. (goto-char marker)
  9538. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9539. "\\(" org-angle-link-re "\\)\\|"
  9540. "\\(" org-plain-link-re "\\)"))
  9541. (cnt ?0)
  9542. (in-emacs (if (integerp nth) nil nth))
  9543. have-zero end links link c)
  9544. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9545. (push (match-string 0 zero) links)
  9546. (setq cnt (1- cnt) have-zero t))
  9547. (save-excursion
  9548. (org-back-to-heading t)
  9549. (setq end (save-excursion (outline-next-heading) (point)))
  9550. (while (re-search-forward re end t)
  9551. (push (match-string 0) links))
  9552. (setq links (org-uniquify (reverse links))))
  9553. (cond
  9554. ((null links)
  9555. (message "No links"))
  9556. ((equal (length links) 1)
  9557. (setq link (car links)))
  9558. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9559. (setq link (nth (if have-zero nth (1- nth)) links)))
  9560. (t ; we have to select a link
  9561. (save-excursion
  9562. (save-window-excursion
  9563. (delete-other-windows)
  9564. (with-output-to-temp-buffer "*Select Link*"
  9565. (mapc (lambda (l)
  9566. (if (not (string-match org-bracket-link-regexp l))
  9567. (princ (format "[%c] %s\n" (incf cnt)
  9568. (org-remove-angle-brackets l)))
  9569. (if (match-end 3)
  9570. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9571. (match-string 3 l) (match-string 1 l)))
  9572. (princ (format "[%c] %s\n" (incf cnt)
  9573. (match-string 1 l))))))
  9574. links))
  9575. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9576. (message "Select link to open, RET to open all:")
  9577. (setq c (read-char-exclusive))
  9578. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9579. (when (equal c ?q) (error "Abort"))
  9580. (if (equal c ?\C-m)
  9581. (setq link links)
  9582. (setq nth (- c ?0))
  9583. (if have-zero (setq nth (1+ nth)))
  9584. (unless (and (integerp nth) (>= (length links) nth))
  9585. (user-error "Invalid link selection"))
  9586. (setq link (nth (1- nth) links)))))
  9587. (cons link end))))))
  9588. ;; Add special file links that specify the way of opening
  9589. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9590. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9591. (defun org-open-file-with-system (path)
  9592. "Open file at PATH using the system way of opening it."
  9593. (org-open-file path 'system))
  9594. (defun org-open-file-with-emacs (path)
  9595. "Open file at PATH in Emacs."
  9596. (org-open-file path 'emacs))
  9597. ;;; File search
  9598. (defvar org-create-file-search-functions nil
  9599. "List of functions to construct the right search string for a file link.
  9600. These functions are called in turn with point at the location to
  9601. which the link should point.
  9602. A function in the hook should first test if it would like to
  9603. handle this file type, for example by checking the `major-mode'
  9604. or the file extension. If it decides not to handle this file, it
  9605. should just return nil to give other functions a chance. If it
  9606. does handle the file, it must return the search string to be used
  9607. when following the link. The search string will be part of the
  9608. file link, given after a double colon, and `org-open-at-point'
  9609. will automatically search for it. If special measures must be
  9610. taken to make the search successful, another function should be
  9611. added to the companion hook `org-execute-file-search-functions',
  9612. which see.
  9613. A function in this hook may also use `setq' to set the variable
  9614. `description' to provide a suggestion for the descriptive text to
  9615. be used for this link when it gets inserted into an Org-mode
  9616. buffer with \\[org-insert-link].")
  9617. (defvar org-execute-file-search-functions nil
  9618. "List of functions to execute a file search triggered by a link.
  9619. Functions added to this hook must accept a single argument, the
  9620. search string that was part of the file link, the part after the
  9621. double colon. The function must first check if it would like to
  9622. handle this search, for example by checking the `major-mode' or
  9623. the file extension. If it decides not to handle this search, it
  9624. should just return nil to give other functions a chance. If it
  9625. does handle the search, it must return a non-nil value to keep
  9626. other functions from trying.
  9627. Each function can access the current prefix argument through the
  9628. variable `current-prefix-arg'. Note that a single prefix is used
  9629. to force opening a link in Emacs, so it may be good to only use a
  9630. numeric or double prefix to guide the search function.
  9631. In case this is needed, a function in this hook can also restore
  9632. the window configuration before `org-open-at-point' was called using:
  9633. (set-window-configuration org-window-config-before-follow-link)")
  9634. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9635. (defun org-link-search (s &optional type avoid-pos stealth)
  9636. "Search for a link search option.
  9637. If S is surrounded by forward slashes, it is interpreted as a
  9638. regular expression. In org-mode files, this will create an `org-occur'
  9639. sparse tree. In ordinary files, `occur' will be used to list matches.
  9640. If the current buffer is in `dired-mode', grep will be used to search
  9641. in all files. If AVOID-POS is given, ignore matches near that position.
  9642. When optional argument STEALTH is non-nil, do not modify
  9643. visibility around point, thus ignoring
  9644. `org-show-hierarchy-above', `org-show-following-heading' and
  9645. `org-show-siblings' variables."
  9646. (let ((case-fold-search t)
  9647. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9648. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9649. (append '(("") (" ") ("\t") ("\n"))
  9650. org-emphasis-alist)
  9651. "\\|") "\\)"))
  9652. (pos (point))
  9653. (pre nil) (post nil)
  9654. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9655. (cond
  9656. ;; First check if there are any special search functions
  9657. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9658. ;; Now try the builtin stuff
  9659. ((and (equal (string-to-char s0) ?#)
  9660. (> (length s0) 1)
  9661. (save-excursion
  9662. (goto-char (point-min))
  9663. (and
  9664. (re-search-forward
  9665. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9666. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9667. (setq type 'dedicated
  9668. pos (match-beginning 0))))
  9669. ;; There is an exact target for this
  9670. (goto-char pos)
  9671. (org-back-to-heading t)))
  9672. ((save-excursion
  9673. (goto-char (point-min))
  9674. (and
  9675. (re-search-forward
  9676. (concat "<<" (regexp-quote s0) ">>") nil t)
  9677. (setq type 'dedicated
  9678. pos (match-beginning 0))))
  9679. ;; There is an exact target for this
  9680. (goto-char pos))
  9681. ((save-excursion
  9682. (goto-char (point-min))
  9683. (and
  9684. (re-search-forward
  9685. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9686. (setq type 'dedicated pos (match-beginning 0))))
  9687. ;; Found an element with a matching #+name affiliated keyword.
  9688. (goto-char pos))
  9689. ((and (string-match "^(\\(.*\\))$" s0)
  9690. (save-excursion
  9691. (goto-char (point-min))
  9692. (and
  9693. (re-search-forward
  9694. (concat "[^[]" (regexp-quote
  9695. (format org-coderef-label-format
  9696. (match-string 1 s0))))
  9697. nil t)
  9698. (setq type 'dedicated
  9699. pos (1+ (match-beginning 0))))))
  9700. ;; There is a coderef target for this
  9701. (goto-char pos))
  9702. ((string-match "^/\\(.*\\)/$" s)
  9703. ;; A regular expression
  9704. (cond
  9705. ((derived-mode-p 'org-mode)
  9706. (org-occur (match-string 1 s)))
  9707. (t (org-do-occur (match-string 1 s)))))
  9708. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9709. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9710. (goto-char (point-min))
  9711. (cond
  9712. ((let (case-fold-search)
  9713. (re-search-forward (format org-complex-heading-regexp-format
  9714. (regexp-quote s))
  9715. nil t))
  9716. ;; OK, found a match
  9717. (setq type 'dedicated)
  9718. (goto-char (match-beginning 0)))
  9719. ((and (not org-link-search-inhibit-query)
  9720. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9721. (y-or-n-p "No match - create this as a new heading? "))
  9722. (goto-char (point-max))
  9723. (or (bolp) (newline))
  9724. (insert "* " s "\n")
  9725. (beginning-of-line 0))
  9726. (t
  9727. (goto-char pos)
  9728. (error "No match"))))
  9729. (t
  9730. ;; A normal search string
  9731. (when (equal (string-to-char s) ?*)
  9732. ;; Anchor on headlines, post may include tags.
  9733. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9734. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9735. s (substring s 1)))
  9736. (remove-text-properties
  9737. 0 (length s)
  9738. '(face nil mouse-face nil keymap nil fontified nil) s)
  9739. ;; Make a series of regular expressions to find a match
  9740. (setq words (org-split-string s "[ \n\r\t]+")
  9741. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9742. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9743. "\\)" markers)
  9744. re2a_ (concat "\\(" (mapconcat 'downcase words
  9745. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9746. re2a (concat "[ \t\r\n]" re2a_)
  9747. re4_ (concat "\\(" (mapconcat 'downcase words
  9748. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9749. re4 (concat "[^a-zA-Z_]" re4_)
  9750. re1 (concat pre re2 post)
  9751. re3 (concat pre (if pre re4_ re4) post)
  9752. re5 (concat pre ".*" re4)
  9753. re2 (concat pre re2)
  9754. re2a (concat pre (if pre re2a_ re2a))
  9755. re4 (concat pre (if pre re4_ re4))
  9756. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9757. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9758. re5 "\\)"))
  9759. (cond
  9760. ((eq type 'org-occur) (org-occur reall))
  9761. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9762. (t (goto-char (point-min))
  9763. (setq type 'fuzzy)
  9764. (if (or (and (org-search-not-self 1 re0 nil t)
  9765. (setq type 'dedicated))
  9766. (org-search-not-self 1 re1 nil t)
  9767. (org-search-not-self 1 re2 nil t)
  9768. (org-search-not-self 1 re2a nil t)
  9769. (org-search-not-self 1 re3 nil t)
  9770. (org-search-not-self 1 re4 nil t)
  9771. (org-search-not-self 1 re5 nil t))
  9772. (goto-char (match-beginning 1))
  9773. (goto-char pos)
  9774. (error "No match"))))))
  9775. (and (derived-mode-p 'org-mode)
  9776. (not stealth)
  9777. (org-show-context 'link-search))
  9778. type))
  9779. (defun org-search-not-self (group &rest args)
  9780. "Execute `re-search-forward', but only accept matches that do not
  9781. enclose the position of `org-open-link-marker'."
  9782. (let ((m org-open-link-marker))
  9783. (catch 'exit
  9784. (while (apply 're-search-forward args)
  9785. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9786. (goto-char (match-end group))
  9787. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9788. (> (match-beginning 0) (marker-position m))
  9789. (< (match-end 0) (marker-position m)))
  9790. (save-match-data
  9791. (or (not (org-in-regexp
  9792. org-bracket-link-analytic-regexp 1))
  9793. (not (match-end 4)) ; no description
  9794. (and (<= (match-beginning 4) (point))
  9795. (>= (match-end 4) (point))))))
  9796. (throw 'exit (point))))))))
  9797. (defun org-get-buffer-for-internal-link (buffer)
  9798. "Return a buffer to be used for displaying the link target of internal links."
  9799. (cond
  9800. ((not org-display-internal-link-with-indirect-buffer)
  9801. buffer)
  9802. ((string-match "(Clone)$" (buffer-name buffer))
  9803. (message "Buffer is already a clone, not making another one")
  9804. ;; we also do not modify visibility in this case
  9805. buffer)
  9806. (t ; make a new indirect buffer for displaying the link
  9807. (let* ((bn (buffer-name buffer))
  9808. (ibn (concat bn "(Clone)"))
  9809. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9810. (with-current-buffer ib (org-overview))
  9811. ib))))
  9812. (defun org-do-occur (regexp &optional cleanup)
  9813. "Call the Emacs command `occur'.
  9814. If CLEANUP is non-nil, remove the printout of the regular expression
  9815. in the *Occur* buffer. This is useful if the regex is long and not useful
  9816. to read."
  9817. (occur regexp)
  9818. (when cleanup
  9819. (let ((cwin (selected-window)) win beg end)
  9820. (when (setq win (get-buffer-window "*Occur*"))
  9821. (select-window win))
  9822. (goto-char (point-min))
  9823. (when (re-search-forward "match[a-z]+" nil t)
  9824. (setq beg (match-end 0))
  9825. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9826. (setq end (1- (match-beginning 0)))))
  9827. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9828. (goto-char (point-min))
  9829. (select-window cwin))))
  9830. ;;; The mark ring for links jumps
  9831. (defvar org-mark-ring nil
  9832. "Mark ring for positions before jumps in Org-mode.")
  9833. (defvar org-mark-ring-last-goto nil
  9834. "Last position in the mark ring used to go back.")
  9835. ;; Fill and close the ring
  9836. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9837. (loop for i from 1 to org-mark-ring-length do
  9838. (push (make-marker) org-mark-ring))
  9839. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9840. org-mark-ring)
  9841. (defun org-mark-ring-push (&optional pos buffer)
  9842. "Put the current position or POS into the mark ring and rotate it."
  9843. (interactive)
  9844. (setq pos (or pos (point)))
  9845. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9846. (move-marker (car org-mark-ring)
  9847. (or pos (point))
  9848. (or buffer (current-buffer)))
  9849. (message "%s"
  9850. (substitute-command-keys
  9851. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9852. (defun org-mark-ring-goto (&optional n)
  9853. "Jump to the previous position in the mark ring.
  9854. With prefix arg N, jump back that many stored positions. When
  9855. called several times in succession, walk through the entire ring.
  9856. Org-mode commands jumping to a different position in the current file,
  9857. or to another Org-mode file, automatically push the old position
  9858. onto the ring."
  9859. (interactive "p")
  9860. (let (p m)
  9861. (if (eq last-command this-command)
  9862. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9863. (setq p org-mark-ring))
  9864. (setq org-mark-ring-last-goto p)
  9865. (setq m (car p))
  9866. (org-pop-to-buffer-same-window (marker-buffer m))
  9867. (goto-char m)
  9868. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9869. (defun org-remove-angle-brackets (s)
  9870. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9871. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9872. s)
  9873. (defun org-add-angle-brackets (s)
  9874. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9875. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9876. s)
  9877. (defun org-remove-double-quotes (s)
  9878. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9879. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9880. s)
  9881. ;;; Following specific links
  9882. (defun org-follow-timestamp-link ()
  9883. "Open an agenda view for the time-stamp date/range at point."
  9884. (cond
  9885. ((org-at-date-range-p t)
  9886. (let ((org-agenda-start-on-weekday)
  9887. (t1 (match-string 1))
  9888. (t2 (match-string 2)) tt1 tt2)
  9889. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9890. tt2 (time-to-days (org-time-string-to-time t2)))
  9891. (let ((org-agenda-buffer-tmp-name
  9892. (format "*Org Agenda(a:%s)"
  9893. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9894. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9895. ((org-at-timestamp-p t)
  9896. (let ((org-agenda-buffer-tmp-name
  9897. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9898. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9899. (substring (match-string 1) 0 10)))
  9900. 1)))
  9901. (t (error "This should not happen"))))
  9902. ;;; Following file links
  9903. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9904. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9905. (declare-function mailcap-mime-info
  9906. "mailcap" (string &optional request no-decode))
  9907. (defvar org-wait nil)
  9908. (defun org-open-file (path &optional in-emacs line search)
  9909. "Open the file at PATH.
  9910. First, this expands any special file name abbreviations. Then the
  9911. configuration variable `org-file-apps' is checked if it contains an
  9912. entry for this file type, and if yes, the corresponding command is launched.
  9913. If no application is found, Emacs simply visits the file.
  9914. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9915. With a double \\[universal-argument] \\[universal-argument] \
  9916. prefix arg, Org tries to avoid opening in Emacs
  9917. and to use an external application to visit the file.
  9918. Optional LINE specifies a line to go to, optional SEARCH a string
  9919. to search for. If LINE or SEARCH is given, the file will be
  9920. opened in Emacs, unless an entry from org-file-apps that makes
  9921. use of groups in a regexp matches.
  9922. If you want to change the way frames are used when following a
  9923. link, please customize `org-link-frame-setup'.
  9924. If the file does not exist, an error is thrown."
  9925. (let* ((file (if (equal path "")
  9926. buffer-file-name
  9927. (substitute-in-file-name (expand-file-name path))))
  9928. (file-apps (append org-file-apps (org-default-apps)))
  9929. (apps (org-remove-if
  9930. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9931. (apps-dlink (org-remove-if-not
  9932. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9933. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9934. (dirp (if remp nil (file-directory-p file)))
  9935. (file (if (and dirp org-open-directory-means-index-dot-org)
  9936. (concat (file-name-as-directory file) "index.org")
  9937. file))
  9938. (a-m-a-p (assq 'auto-mode apps))
  9939. (dfile (downcase file))
  9940. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9941. (link (cond ((and (eq line nil)
  9942. (eq search nil))
  9943. file)
  9944. (line
  9945. (concat file "::" (number-to-string line)))
  9946. (search
  9947. (concat file "::" search))))
  9948. (dlink (downcase link))
  9949. (old-buffer (current-buffer))
  9950. (old-pos (point))
  9951. (old-mode major-mode)
  9952. ext cmd link-match-data)
  9953. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9954. (setq ext (match-string 1 dfile))
  9955. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9956. (setq ext (match-string 1 dfile))))
  9957. (cond
  9958. ((member in-emacs '((16) system))
  9959. (setq cmd (cdr (assoc 'system apps))))
  9960. (in-emacs (setq cmd 'emacs))
  9961. (t
  9962. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9963. (and dirp (cdr (assoc 'directory apps)))
  9964. ; first, try matching against apps-dlink
  9965. ; if we get a match here, store the match data for later
  9966. (let ((match (assoc-default dlink apps-dlink
  9967. 'string-match)))
  9968. (if match
  9969. (progn (setq link-match-data (match-data))
  9970. match)
  9971. (progn (setq in-emacs (or in-emacs line search))
  9972. nil))) ; if we have no match in apps-dlink,
  9973. ; always open the file in emacs if line or search
  9974. ; is given (for backwards compatibility)
  9975. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9976. 'string-match)
  9977. (cdr (assoc ext apps))
  9978. (cdr (assoc t apps))))))
  9979. (when (eq cmd 'system)
  9980. (setq cmd (cdr (assoc 'system apps))))
  9981. (when (eq cmd 'default)
  9982. (setq cmd (cdr (assoc t apps))))
  9983. (when (eq cmd 'mailcap)
  9984. (require 'mailcap)
  9985. (mailcap-parse-mailcaps)
  9986. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9987. (command (mailcap-mime-info mime-type)))
  9988. (if (stringp command)
  9989. (setq cmd command)
  9990. (setq cmd 'emacs))))
  9991. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9992. (not (file-exists-p file))
  9993. (not org-open-non-existing-files))
  9994. (user-error "No such file: %s" file))
  9995. (cond
  9996. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9997. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9998. (while (string-match "['\"]%s['\"]" cmd)
  9999. (setq cmd (replace-match "%s" t t cmd)))
  10000. (while (string-match "%s" cmd)
  10001. (setq cmd (replace-match
  10002. (save-match-data
  10003. (shell-quote-argument
  10004. (convert-standard-filename file)))
  10005. t t cmd)))
  10006. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10007. (save-match-data
  10008. (let ((match-index 1)
  10009. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10010. (set-match-data link-match-data)
  10011. (while (<= match-index number-of-groups)
  10012. (let ((regex (concat "%" (number-to-string match-index)))
  10013. (replace-with (match-string match-index dlink)))
  10014. (while (string-match regex cmd)
  10015. (setq cmd (replace-match replace-with t t cmd))))
  10016. (setq match-index (+ match-index 1)))))
  10017. (save-window-excursion
  10018. (message "Running %s...done" cmd)
  10019. (start-process-shell-command cmd nil cmd)
  10020. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10021. ((or (stringp cmd)
  10022. (eq cmd 'emacs))
  10023. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10024. (widen)
  10025. (if line (org-goto-line line)
  10026. (if search (org-link-search search))))
  10027. ((consp cmd)
  10028. (let ((file (convert-standard-filename file)))
  10029. (save-match-data
  10030. (set-match-data link-match-data)
  10031. (eval cmd))))
  10032. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10033. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10034. (or (not (equal old-buffer (current-buffer)))
  10035. (not (equal old-pos (point))))
  10036. (org-mark-ring-push old-pos old-buffer))))
  10037. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10038. "This function returns non-nil if `entry' uses a regular
  10039. expression which should be matched against the whole link by
  10040. org-open-file.
  10041. It assumes that is the case when the entry uses a regular
  10042. expression which has at least one grouping construct and the
  10043. action is either a lisp form or a command string containing
  10044. '%1', i.e. using at least one subexpression match as a
  10045. parameter."
  10046. (let ((selector (car entry))
  10047. (action (cdr entry)))
  10048. (if (stringp selector)
  10049. (and (> (regexp-opt-depth selector) 0)
  10050. (or (and (stringp action)
  10051. (string-match "%[0-9]" action))
  10052. (consp action)))
  10053. nil)))
  10054. (defun org-default-apps ()
  10055. "Return the default applications for this operating system."
  10056. (cond
  10057. ((eq system-type 'darwin)
  10058. org-file-apps-defaults-macosx)
  10059. ((eq system-type 'windows-nt)
  10060. org-file-apps-defaults-windowsnt)
  10061. (t org-file-apps-defaults-gnu)))
  10062. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10063. "Convert extensions to regular expressions in the cars of LIST.
  10064. Also, weed out any non-string entries, because the return value is used
  10065. only for regexp matching.
  10066. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10067. point to the symbol `emacs', indicating that the file should
  10068. be opened in Emacs."
  10069. (append
  10070. (delq nil
  10071. (mapcar (lambda (x)
  10072. (if (not (stringp (car x)))
  10073. nil
  10074. (if (string-match "\\W" (car x))
  10075. x
  10076. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10077. list))
  10078. (if add-auto-mode
  10079. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10080. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10081. (defun org-file-remote-p (file)
  10082. "Test whether FILE specifies a location on a remote system.
  10083. Return non-nil if the location is indeed remote.
  10084. For example, the filename \"/user@host:/foo\" specifies a location
  10085. on the system \"/user@host:\"."
  10086. (cond ((fboundp 'file-remote-p)
  10087. (file-remote-p file))
  10088. ((fboundp 'tramp-handle-file-remote-p)
  10089. (tramp-handle-file-remote-p file))
  10090. ((and (boundp 'ange-ftp-name-format)
  10091. (string-match (car ange-ftp-name-format) file))
  10092. t)))
  10093. ;;;; Refiling
  10094. (defun org-get-org-file ()
  10095. "Read a filename, with default directory `org-directory'."
  10096. (let ((default (or org-default-notes-file remember-data-file)))
  10097. (read-file-name (format "File name [%s]: " default)
  10098. (file-name-as-directory org-directory)
  10099. default)))
  10100. (defun org-notes-order-reversed-p ()
  10101. "Check if the current file should receive notes in reversed order."
  10102. (cond
  10103. ((not org-reverse-note-order) nil)
  10104. ((eq t org-reverse-note-order) t)
  10105. ((not (listp org-reverse-note-order)) nil)
  10106. (t (catch 'exit
  10107. (let ((all org-reverse-note-order)
  10108. entry)
  10109. (while (setq entry (pop all))
  10110. (if (string-match (car entry) buffer-file-name)
  10111. (throw 'exit (cdr entry))))
  10112. nil)))))
  10113. (defvar org-refile-target-table nil
  10114. "The list of refile targets, created by `org-refile'.")
  10115. (defvar org-agenda-new-buffers nil
  10116. "Buffers created to visit agenda files.")
  10117. (defvar org-refile-cache nil
  10118. "Cache for refile targets.")
  10119. (defvar org-refile-markers nil
  10120. "All the markers used for caching refile locations.")
  10121. (defun org-refile-marker (pos)
  10122. "Get a new refile marker, but only if caching is in use."
  10123. (if (not org-refile-use-cache)
  10124. pos
  10125. (let ((m (make-marker)))
  10126. (move-marker m pos)
  10127. (push m org-refile-markers)
  10128. m)))
  10129. (defun org-refile-cache-clear ()
  10130. "Clear the refile cache and disable all the markers."
  10131. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10132. (setq org-refile-markers nil)
  10133. (setq org-refile-cache nil)
  10134. (message "Refile cache has been cleared"))
  10135. (defun org-refile-cache-check-set (set)
  10136. "Check if all the markers in the cache still have live buffers."
  10137. (let (marker)
  10138. (catch 'exit
  10139. (while (and set (setq marker (nth 3 (pop set))))
  10140. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10141. (when (and marker (null (marker-buffer marker)))
  10142. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10143. (sit-for 3)
  10144. (throw 'exit nil)))
  10145. t)))
  10146. (defun org-refile-cache-put (set &rest identifiers)
  10147. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10148. (let* ((key (sha1 (prin1-to-string identifiers)))
  10149. (entry (assoc key org-refile-cache)))
  10150. (if entry
  10151. (setcdr entry set)
  10152. (push (cons key set) org-refile-cache))))
  10153. (defun org-refile-cache-get (&rest identifiers)
  10154. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10155. (cond
  10156. ((not org-refile-cache) nil)
  10157. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10158. (t
  10159. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10160. org-refile-cache))))
  10161. (and set (org-refile-cache-check-set set) set)))))
  10162. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10163. "Produce a table with refile targets."
  10164. (let ((case-fold-search nil)
  10165. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10166. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10167. targets tgs txt re files f desc descre fast-path-p level pos0)
  10168. (message "Getting targets...")
  10169. (with-current-buffer (or default-buffer (current-buffer))
  10170. (while (setq entry (pop entries))
  10171. (setq files (car entry) desc (cdr entry))
  10172. (setq fast-path-p nil)
  10173. (cond
  10174. ((null files) (setq files (list (current-buffer))))
  10175. ((eq files 'org-agenda-files)
  10176. (setq files (org-agenda-files 'unrestricted)))
  10177. ((and (symbolp files) (fboundp files))
  10178. (setq files (funcall files)))
  10179. ((and (symbolp files) (boundp files))
  10180. (setq files (symbol-value files))))
  10181. (if (stringp files) (setq files (list files)))
  10182. (cond
  10183. ((eq (car desc) :tag)
  10184. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10185. ((eq (car desc) :todo)
  10186. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10187. ((eq (car desc) :regexp)
  10188. (setq descre (cdr desc)))
  10189. ((eq (car desc) :level)
  10190. (setq descre (concat "^\\*\\{" (number-to-string
  10191. (if org-odd-levels-only
  10192. (1- (* 2 (cdr desc)))
  10193. (cdr desc)))
  10194. "\\}[ \t]")))
  10195. ((eq (car desc) :maxlevel)
  10196. (setq fast-path-p t)
  10197. (setq descre (concat "^\\*\\{1," (number-to-string
  10198. (if org-odd-levels-only
  10199. (1- (* 2 (cdr desc)))
  10200. (cdr desc)))
  10201. "\\}[ \t]")))
  10202. (t (error "Bad refiling target description %s" desc)))
  10203. (while (setq f (pop files))
  10204. (with-current-buffer
  10205. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10206. (or
  10207. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10208. (progn
  10209. (if (bufferp f) (setq f (buffer-file-name
  10210. (buffer-base-buffer f))))
  10211. (setq f (and f (expand-file-name f)))
  10212. (if (eq org-refile-use-outline-path 'file)
  10213. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10214. (save-excursion
  10215. (save-restriction
  10216. (widen)
  10217. (goto-char (point-min))
  10218. (while (re-search-forward descre nil t)
  10219. (goto-char (setq pos0 (point-at-bol)))
  10220. (catch 'next
  10221. (when org-refile-target-verify-function
  10222. (save-match-data
  10223. (or (funcall org-refile-target-verify-function)
  10224. (throw 'next t))))
  10225. (when (and (looking-at org-complex-heading-regexp)
  10226. (not (member (match-string 4) excluded-entries))
  10227. (match-string 4))
  10228. (setq level (org-reduced-level
  10229. (- (match-end 1) (match-beginning 1)))
  10230. txt (org-link-display-format (match-string 4))
  10231. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10232. re (format org-complex-heading-regexp-format
  10233. (regexp-quote (match-string 4))))
  10234. (when org-refile-use-outline-path
  10235. (setq txt (mapconcat
  10236. 'org-protect-slash
  10237. (append
  10238. (if (eq org-refile-use-outline-path
  10239. 'file)
  10240. (list (file-name-nondirectory
  10241. (buffer-file-name
  10242. (buffer-base-buffer))))
  10243. (if (eq org-refile-use-outline-path
  10244. 'full-file-path)
  10245. (list (buffer-file-name
  10246. (buffer-base-buffer)))))
  10247. (org-get-outline-path fast-path-p
  10248. level txt)
  10249. (list txt))
  10250. "/")))
  10251. (push (list txt f re (org-refile-marker (point)))
  10252. tgs)))
  10253. (when (= (point) pos0)
  10254. ;; verification function has not moved point
  10255. (goto-char (point-at-eol))))))))
  10256. (when org-refile-use-cache
  10257. (org-refile-cache-put tgs (buffer-file-name) descre))
  10258. (setq targets (append tgs targets))))))
  10259. (message "Getting targets...done")
  10260. (nreverse targets)))
  10261. (defun org-protect-slash (s)
  10262. (while (string-match "/" s)
  10263. (setq s (replace-match "\\" t t s)))
  10264. s)
  10265. (defvar org-olpa (make-vector 20 nil))
  10266. (defun org-get-outline-path (&optional fastp level heading)
  10267. "Return the outline path to the current entry, as a list.
  10268. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10269. routine which makes outline path derivations for an entire file,
  10270. avoiding backtracing. Refile target collection makes use of that."
  10271. (if fastp
  10272. (progn
  10273. (if (> level 19)
  10274. (error "Outline path failure, more than 19 levels"))
  10275. (loop for i from level upto 19 do
  10276. (aset org-olpa i nil))
  10277. (prog1
  10278. (delq nil (append org-olpa nil))
  10279. (aset org-olpa level heading)))
  10280. (let (rtn case-fold-search)
  10281. (save-excursion
  10282. (save-restriction
  10283. (widen)
  10284. (while (org-up-heading-safe)
  10285. (when (looking-at org-complex-heading-regexp)
  10286. (push (org-trim
  10287. (replace-regexp-in-string
  10288. ;; Remove statistical/checkboxes cookies
  10289. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10290. (org-match-string-no-properties 4)))
  10291. rtn)))
  10292. rtn)))))
  10293. (defun org-format-outline-path (path &optional width prefix separator)
  10294. "Format the outline path PATH for display.
  10295. WIDTH is the maximum number of characters that is available.
  10296. PREFIX is a prefix to be included in the returned string,
  10297. such as the file name.
  10298. SEPARATOR is inserted between the different parts of the path,
  10299. the default is \"/\"."
  10300. (setq width (or width 79))
  10301. (if prefix (setq width (- width (length prefix))))
  10302. (if (not path)
  10303. (or prefix "")
  10304. (let* ((nsteps (length path))
  10305. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10306. (maxwidth (if (<= total-width width)
  10307. 10000 ;; everything fits
  10308. ;; we need to shorten the level headings
  10309. (/ (- width nsteps) nsteps)))
  10310. (org-odd-levels-only nil)
  10311. (n 0)
  10312. (total (1+ (length prefix))))
  10313. (setq maxwidth (max maxwidth 10))
  10314. (concat prefix
  10315. (if prefix (or separator "/"))
  10316. (mapconcat
  10317. (lambda (h)
  10318. (setq n (1+ n))
  10319. (if (and (= n nsteps) (< maxwidth 10000))
  10320. (setq maxwidth (- total-width total)))
  10321. (if (< (length h) maxwidth)
  10322. (progn (setq total (+ total (length h) 1)) h)
  10323. (setq h (substring h 0 (- maxwidth 2))
  10324. total (+ total maxwidth 1))
  10325. (if (string-match "[ \t]+\\'" h)
  10326. (setq h (substring h 0 (match-beginning 0))))
  10327. (setq h (concat h "..")))
  10328. (org-add-props h nil 'face
  10329. (nth (% (1- n) org-n-level-faces)
  10330. org-level-faces))
  10331. h)
  10332. path (or separator "/"))))))
  10333. (defun org-display-outline-path (&optional file current separator just-return-string)
  10334. "Display the current outline path in the echo area.
  10335. If FILE is non-nil, prepend the output with the file name.
  10336. If CURRENT is non-nil, append the current heading to the output.
  10337. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10338. the different parts of the path and defaults to \"/\".
  10339. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10340. (interactive "P")
  10341. (let* (case-fold-search
  10342. (bfn (buffer-file-name (buffer-base-buffer)))
  10343. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10344. res)
  10345. (if current (setq path (append path
  10346. (save-excursion
  10347. (org-back-to-heading t)
  10348. (if (looking-at org-complex-heading-regexp)
  10349. (list (match-string 4)))))))
  10350. (setq res
  10351. (org-format-outline-path
  10352. path
  10353. (1- (frame-width))
  10354. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10355. separator))
  10356. (if just-return-string
  10357. (org-no-properties res)
  10358. (org-unlogged-message "%s" res))))
  10359. (defvar org-refile-history nil
  10360. "History for refiling operations.")
  10361. (defvar org-after-refile-insert-hook nil
  10362. "Hook run after `org-refile' has inserted its stuff at the new location.
  10363. Note that this is still *before* the stuff will be removed from
  10364. the *old* location.")
  10365. (defvar org-capture-last-stored-marker)
  10366. (defvar org-refile-keep nil
  10367. "Non-nil means `org-refile' will copy instead of refile.")
  10368. (defun org-copy ()
  10369. "Like `org-refile', but copy."
  10370. (interactive)
  10371. (let ((org-refile-keep t))
  10372. (funcall 'org-refile nil nil nil "Copy")))
  10373. (defun org-refile (&optional goto default-buffer rfloc msg)
  10374. "Move the entry or entries at point to another heading.
  10375. The list of target headings is compiled using the information in
  10376. `org-refile-targets', which see.
  10377. At the target location, the entry is filed as a subitem of the target
  10378. heading. Depending on `org-reverse-note-order', the new subitem will
  10379. either be the first or the last subitem.
  10380. If there is an active region, all entries in that region will be moved.
  10381. However, the region must fulfill the requirement that the first heading
  10382. is the first one sets the top-level of the moved text - at most siblings
  10383. below it are allowed.
  10384. With prefix arg GOTO, the command will only visit the target location
  10385. and not actually move anything.
  10386. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10387. go to the location where the last refiling operation has put the subtree.
  10388. With a numeric prefix argument of `2', refile to the running clock.
  10389. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10390. being set to `t' and copy to the target location, don't move it.
  10391. Beware that keeping refiled entries may result in duplicated ID
  10392. properties.
  10393. RFLOC can be a refile location obtained in a different way.
  10394. MSG is a string to replace \"Refile\" in the default prompt with
  10395. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10396. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10397. If you are using target caching (see `org-refile-use-cache'),
  10398. you have to clear the target cache in order to find new targets.
  10399. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10400. prefix argument (`C-u C-u C-u C-c C-w')."
  10401. (interactive "P")
  10402. (if (member goto '(0 (64)))
  10403. (org-refile-cache-clear)
  10404. (let* ((actionmsg (or msg "Refile"))
  10405. (cbuf (current-buffer))
  10406. (regionp (org-region-active-p))
  10407. (region-start (and regionp (region-beginning)))
  10408. (region-end (and regionp (region-end)))
  10409. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10410. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10411. pos it nbuf file re level reversed)
  10412. (setq last-command nil)
  10413. (when regionp
  10414. (goto-char region-start)
  10415. (or (bolp) (goto-char (point-at-bol)))
  10416. (setq region-start (point))
  10417. (unless (or (org-kill-is-subtree-p
  10418. (buffer-substring region-start region-end))
  10419. (prog1 org-refile-active-region-within-subtree
  10420. (let ((s (point-at-eol)))
  10421. (org-toggle-heading)
  10422. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10423. (user-error "The region is not a (sequence of) subtree(s)")))
  10424. (if (equal goto '(16))
  10425. (org-refile-goto-last-stored)
  10426. (when (or
  10427. (and (equal goto 2)
  10428. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10429. (prog1
  10430. (setq it (list (or org-clock-heading "running clock")
  10431. (buffer-file-name
  10432. (marker-buffer org-clock-hd-marker))
  10433. ""
  10434. (marker-position org-clock-hd-marker)))
  10435. (setq goto nil)))
  10436. (setq it (or rfloc
  10437. (let (heading-text)
  10438. (save-excursion
  10439. (unless goto
  10440. (org-back-to-heading t)
  10441. (setq heading-text
  10442. (nth 4 (org-heading-components))))
  10443. (org-refile-get-location
  10444. (cond (goto "Goto")
  10445. (regionp (concat actionmsg " region to"))
  10446. (t (concat actionmsg " subtree \""
  10447. heading-text "\" to")))
  10448. default-buffer
  10449. (and (not (equal '(4) goto))
  10450. org-refile-allow-creating-parent-nodes)
  10451. goto))))))
  10452. (setq file (nth 1 it)
  10453. re (nth 2 it)
  10454. pos (nth 3 it))
  10455. (if (and (not goto)
  10456. pos
  10457. (equal (buffer-file-name) file)
  10458. (if regionp
  10459. (and (>= pos region-start)
  10460. (<= pos region-end))
  10461. (and (>= pos (point))
  10462. (< pos (save-excursion
  10463. (org-end-of-subtree t t))))))
  10464. (error "Cannot refile to position inside the tree or region"))
  10465. (setq nbuf (or (find-buffer-visiting file)
  10466. (find-file-noselect file)))
  10467. (if (and goto (not (equal goto 3)))
  10468. (progn
  10469. (org-pop-to-buffer-same-window nbuf)
  10470. (goto-char pos)
  10471. (org-show-context 'org-goto))
  10472. (if regionp
  10473. (progn
  10474. (org-kill-new (buffer-substring region-start region-end))
  10475. (org-save-markers-in-region region-start region-end))
  10476. (org-copy-subtree 1 nil t))
  10477. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10478. (find-file-noselect file)))
  10479. (setq reversed (org-notes-order-reversed-p))
  10480. (save-excursion
  10481. (save-restriction
  10482. (widen)
  10483. (if pos
  10484. (progn
  10485. (goto-char pos)
  10486. (looking-at org-outline-regexp)
  10487. (setq level (org-get-valid-level (funcall outline-level) 1))
  10488. (goto-char
  10489. (if reversed
  10490. (or (outline-next-heading) (point-max))
  10491. (or (save-excursion (org-get-next-sibling))
  10492. (org-end-of-subtree t t)
  10493. (point-max)))))
  10494. (setq level 1)
  10495. (if (not reversed)
  10496. (goto-char (point-max))
  10497. (goto-char (point-min))
  10498. (or (outline-next-heading) (goto-char (point-max)))))
  10499. (if (not (bolp)) (newline))
  10500. (org-paste-subtree level)
  10501. (when org-log-refile
  10502. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10503. (unless (eq org-log-refile 'note)
  10504. (save-excursion (org-add-log-note))))
  10505. (and org-auto-align-tags
  10506. (let ((org-loop-over-headlines-in-active-region nil))
  10507. (org-set-tags nil t)))
  10508. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10509. :last-refile)))
  10510. (when bookmark-name
  10511. (with-demoted-errors
  10512. (bookmark-set bookmark-name))))
  10513. ;; If we are refiling for capture, make sure that the
  10514. ;; last-capture pointers point here
  10515. (when (org-bound-and-true-p org-refile-for-capture)
  10516. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10517. :last-capture-marker)))
  10518. (when bookmark-name
  10519. (with-demoted-errors
  10520. (bookmark-set bookmark-name))))
  10521. (move-marker org-capture-last-stored-marker (point)))
  10522. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10523. (run-hooks 'org-after-refile-insert-hook))))
  10524. (unless org-refile-keep
  10525. (if regionp
  10526. (delete-region (point) (+ (point) (- region-end region-start)))
  10527. (delete-region
  10528. (and (org-back-to-heading t) (point))
  10529. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10530. (when (featurep 'org-inlinetask)
  10531. (org-inlinetask-remove-END-maybe))
  10532. (setq org-markers-to-move nil)
  10533. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10534. (defun org-refile-goto-last-stored ()
  10535. "Go to the location where the last refile was stored."
  10536. (interactive)
  10537. (bookmark-jump "org-refile-last-stored")
  10538. (message "This is the location of the last refile"))
  10539. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10540. no-exclude)
  10541. "Prompt the user for a refile location, using PROMPT.
  10542. PROMPT should not be suffixed with a colon and a space, because
  10543. this function appends the default value from
  10544. `org-refile-history' automatically, if that is not empty.
  10545. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10546. this is used for the GOTO interface."
  10547. (let ((org-refile-targets org-refile-targets)
  10548. (org-refile-use-outline-path org-refile-use-outline-path)
  10549. excluded-entries)
  10550. (when (and (derived-mode-p 'org-mode)
  10551. (not org-refile-use-cache)
  10552. (not no-exclude))
  10553. (org-map-tree
  10554. (lambda()
  10555. (setq excluded-entries
  10556. (append excluded-entries (list (org-get-heading t t)))))))
  10557. (setq org-refile-target-table
  10558. (org-refile-get-targets default-buffer excluded-entries)))
  10559. (unless org-refile-target-table
  10560. (user-error "No refile targets"))
  10561. (let* ((cbuf (current-buffer))
  10562. (partial-completion-mode nil)
  10563. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10564. (cfunc (if (and org-refile-use-outline-path
  10565. org-outline-path-complete-in-steps)
  10566. 'org-olpath-completing-read
  10567. 'org-icompleting-read))
  10568. (extra (if org-refile-use-outline-path "/" ""))
  10569. (cbnex (concat (buffer-name) extra))
  10570. (filename (and cfn (expand-file-name cfn)))
  10571. (tbl (mapcar
  10572. (lambda (x)
  10573. (if (and (not (member org-refile-use-outline-path
  10574. '(file full-file-path)))
  10575. (not (equal filename (nth 1 x))))
  10576. (cons (concat (car x) extra " ("
  10577. (file-name-nondirectory (nth 1 x)) ")")
  10578. (cdr x))
  10579. (cons (concat (car x) extra) (cdr x))))
  10580. org-refile-target-table))
  10581. (completion-ignore-case t)
  10582. cdef
  10583. (prompt (concat prompt
  10584. (or (and (car org-refile-history)
  10585. (concat " (default " (car org-refile-history) ")"))
  10586. (and (assoc cbnex tbl) (setq cdef cbnex)
  10587. (concat " (default " cbnex ")"))) ": "))
  10588. pa answ parent-target child parent old-hist)
  10589. (setq old-hist org-refile-history)
  10590. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10591. nil 'org-refile-history (or cdef (car org-refile-history))))
  10592. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10593. (org-refile-check-position pa)
  10594. (if pa
  10595. (progn
  10596. (when (or (not org-refile-history)
  10597. (not (eq old-hist org-refile-history))
  10598. (not (equal (car pa) (car org-refile-history))))
  10599. (setq org-refile-history
  10600. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10601. org-refile-history
  10602. (cdr org-refile-history))))
  10603. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10604. (pop org-refile-history)))
  10605. pa)
  10606. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10607. (progn
  10608. (setq parent (match-string 1 answ)
  10609. child (match-string 2 answ))
  10610. (setq parent-target (or (assoc parent tbl)
  10611. (assoc (concat parent "/") tbl)))
  10612. (when (and parent-target
  10613. (or (eq new-nodes t)
  10614. (and (eq new-nodes 'confirm)
  10615. (y-or-n-p (format "Create new node \"%s\"? "
  10616. child)))))
  10617. (org-refile-new-child parent-target child)))
  10618. (user-error "Invalid target location")))))
  10619. (declare-function org-string-nw-p "org-macs" (s))
  10620. (defun org-refile-check-position (refile-pointer)
  10621. "Check if the refile pointer matches the headline to which it points."
  10622. (let* ((file (nth 1 refile-pointer))
  10623. (re (nth 2 refile-pointer))
  10624. (pos (nth 3 refile-pointer))
  10625. buffer)
  10626. (if (and (not (markerp pos)) (not file))
  10627. (user-error "Please save the buffer to a file before refiling")
  10628. (when (org-string-nw-p re)
  10629. (setq buffer (if (markerp pos)
  10630. (marker-buffer pos)
  10631. (or (find-buffer-visiting file)
  10632. (find-file-noselect file))))
  10633. (with-current-buffer buffer
  10634. (save-excursion
  10635. (save-restriction
  10636. (widen)
  10637. (goto-char pos)
  10638. (beginning-of-line 1)
  10639. (unless (org-looking-at-p re)
  10640. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10641. (defun org-refile-new-child (parent-target child)
  10642. "Use refile target PARENT-TARGET to add new CHILD below it."
  10643. (unless parent-target
  10644. (error "Cannot find parent for new node"))
  10645. (let ((file (nth 1 parent-target))
  10646. (pos (nth 3 parent-target))
  10647. level)
  10648. (with-current-buffer (or (find-buffer-visiting file)
  10649. (find-file-noselect file))
  10650. (save-excursion
  10651. (save-restriction
  10652. (widen)
  10653. (if pos
  10654. (goto-char pos)
  10655. (goto-char (point-max))
  10656. (if (not (bolp)) (newline)))
  10657. (when (looking-at org-outline-regexp)
  10658. (setq level (funcall outline-level))
  10659. (org-end-of-subtree t t))
  10660. (org-back-over-empty-lines)
  10661. (insert "\n" (make-string
  10662. (if pos (org-get-valid-level level 1) 1) ?*)
  10663. " " child "\n")
  10664. (beginning-of-line 0)
  10665. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10666. (defun org-olpath-completing-read (prompt collection &rest args)
  10667. "Read an outline path like a file name."
  10668. (let ((thetable collection)
  10669. (org-completion-use-ido nil) ; does not work with ido.
  10670. (org-completion-use-iswitchb nil)) ; or iswitchb
  10671. (apply
  10672. 'org-icompleting-read prompt
  10673. (lambda (string predicate &optional flag)
  10674. (let (rtn r f (l (length string)))
  10675. (cond
  10676. ((eq flag nil)
  10677. ;; try completion
  10678. (try-completion string thetable))
  10679. ((eq flag t)
  10680. ;; all-completions
  10681. (setq rtn (all-completions string thetable predicate))
  10682. (mapcar
  10683. (lambda (x)
  10684. (setq r (substring x l))
  10685. (if (string-match " ([^)]*)$" x)
  10686. (setq f (match-string 0 x))
  10687. (setq f ""))
  10688. (if (string-match "/" r)
  10689. (concat string (substring r 0 (match-end 0)) f)
  10690. x))
  10691. rtn))
  10692. ((eq flag 'lambda)
  10693. ;; exact match?
  10694. (assoc string thetable)))))
  10695. args)))
  10696. ;;;; Dynamic blocks
  10697. (defun org-find-dblock (name)
  10698. "Find the first dynamic block with name NAME in the buffer.
  10699. If not found, stay at current position and return nil."
  10700. (let ((case-fold-search t) pos)
  10701. (save-excursion
  10702. (goto-char (point-min))
  10703. (setq pos (and (re-search-forward
  10704. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10705. (match-beginning 0))))
  10706. (if pos (goto-char pos))
  10707. pos))
  10708. (defconst org-dblock-start-re
  10709. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10710. "Matches the start line of a dynamic block, with parameters.")
  10711. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10712. "Matches the end of a dynamic block.")
  10713. (defun org-create-dblock (plist)
  10714. "Create a dynamic block section, with parameters taken from PLIST.
  10715. PLIST must contain a :name entry which is used as name of the block."
  10716. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10717. (end-of-line 1)
  10718. (newline))
  10719. (let ((col (current-column))
  10720. (name (plist-get plist :name)))
  10721. (insert "#+BEGIN: " name)
  10722. (while plist
  10723. (if (eq (car plist) :name)
  10724. (setq plist (cddr plist))
  10725. (insert " " (prin1-to-string (pop plist)))))
  10726. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10727. (beginning-of-line -2)))
  10728. (defun org-prepare-dblock ()
  10729. "Prepare dynamic block for refresh.
  10730. This empties the block, puts the cursor at the insert position and returns
  10731. the property list including an extra property :name with the block name."
  10732. (unless (looking-at org-dblock-start-re)
  10733. (user-error "Not at a dynamic block"))
  10734. (let* ((begdel (1+ (match-end 0)))
  10735. (name (org-no-properties (match-string 1)))
  10736. (params (append (list :name name)
  10737. (read (concat "(" (match-string 3) ")")))))
  10738. (save-excursion
  10739. (beginning-of-line 1)
  10740. (skip-chars-forward " \t")
  10741. (setq params (plist-put params :indentation-column (current-column))))
  10742. (unless (re-search-forward org-dblock-end-re nil t)
  10743. (error "Dynamic block not terminated"))
  10744. (setq params
  10745. (append params
  10746. (list :content (buffer-substring
  10747. begdel (match-beginning 0)))))
  10748. (delete-region begdel (match-beginning 0))
  10749. (goto-char begdel)
  10750. (open-line 1)
  10751. params))
  10752. (defun org-map-dblocks (&optional command)
  10753. "Apply COMMAND to all dynamic blocks in the current buffer.
  10754. If COMMAND is not given, use `org-update-dblock'."
  10755. (let ((cmd (or command 'org-update-dblock)))
  10756. (save-excursion
  10757. (goto-char (point-min))
  10758. (while (re-search-forward org-dblock-start-re nil t)
  10759. (goto-char (match-beginning 0))
  10760. (save-excursion
  10761. (condition-case nil
  10762. (funcall cmd)
  10763. (error (message "Error during update of dynamic block"))))
  10764. (unless (re-search-forward org-dblock-end-re nil t)
  10765. (error "Dynamic block not terminated"))))))
  10766. (defun org-dblock-update (&optional arg)
  10767. "User command for updating dynamic blocks.
  10768. Update the dynamic block at point. With prefix ARG, update all dynamic
  10769. blocks in the buffer."
  10770. (interactive "P")
  10771. (if arg
  10772. (org-update-all-dblocks)
  10773. (or (looking-at org-dblock-start-re)
  10774. (org-beginning-of-dblock))
  10775. (org-update-dblock)))
  10776. (defun org-update-dblock ()
  10777. "Update the dynamic block at point.
  10778. This means to empty the block, parse for parameters and then call
  10779. the correct writing function."
  10780. (interactive)
  10781. (save-window-excursion
  10782. (let* ((pos (point))
  10783. (line (org-current-line))
  10784. (params (org-prepare-dblock))
  10785. (name (plist-get params :name))
  10786. (indent (plist-get params :indentation-column))
  10787. (cmd (intern (concat "org-dblock-write:" name))))
  10788. (message "Updating dynamic block `%s' at line %d..." name line)
  10789. (funcall cmd params)
  10790. (message "Updating dynamic block `%s' at line %d...done" name line)
  10791. (goto-char pos)
  10792. (when (and indent (> indent 0))
  10793. (setq indent (make-string indent ?\ ))
  10794. (save-excursion
  10795. (org-beginning-of-dblock)
  10796. (forward-line 1)
  10797. (while (not (looking-at org-dblock-end-re))
  10798. (insert indent)
  10799. (beginning-of-line 2))
  10800. (when (looking-at org-dblock-end-re)
  10801. (and (looking-at "[ \t]+")
  10802. (replace-match ""))
  10803. (insert indent)))))))
  10804. (defun org-beginning-of-dblock ()
  10805. "Find the beginning of the dynamic block at point.
  10806. Error if there is no such block at point."
  10807. (let ((pos (point))
  10808. beg)
  10809. (end-of-line 1)
  10810. (if (and (re-search-backward org-dblock-start-re nil t)
  10811. (setq beg (match-beginning 0))
  10812. (re-search-forward org-dblock-end-re nil t)
  10813. (> (match-end 0) pos))
  10814. (goto-char beg)
  10815. (goto-char pos)
  10816. (error "Not in a dynamic block"))))
  10817. (defun org-update-all-dblocks ()
  10818. "Update all dynamic blocks in the buffer.
  10819. This function can be used in a hook."
  10820. (interactive)
  10821. (when (derived-mode-p 'org-mode)
  10822. (org-map-dblocks 'org-update-dblock)))
  10823. ;;;; Completion
  10824. (declare-function org-export-backend-name "org-export" (cl-x))
  10825. (declare-function org-export-backend-options "org-export" (cl-x))
  10826. (defun org-get-export-keywords ()
  10827. "Return a list of all currently understood export keywords.
  10828. Export keywords include options, block names, attributes and
  10829. keywords relative to each registered export back-end."
  10830. (let (keywords)
  10831. (dolist (backend
  10832. (org-bound-and-true-p org-export--registered-backends)
  10833. (delq nil keywords))
  10834. ;; Back-end name (for keywords, like #+LATEX:)
  10835. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10836. (dolist (option-entry (org-export-backend-options backend))
  10837. ;; Back-end options.
  10838. (push (nth 1 option-entry) keywords)))))
  10839. (defconst org-options-keywords
  10840. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10841. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10842. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10843. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10844. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10845. (defcustom org-structure-template-alist
  10846. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10847. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10848. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10849. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10850. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10851. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10852. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10853. "<literal style=\"latex\">\n?\n</literal>")
  10854. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10855. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10856. "<literal style=\"html\">\n?\n</literal>")
  10857. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10858. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10859. ("A" "#+ASCII: " "")
  10860. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10861. ("I" "#+INCLUDE: %file ?"
  10862. "<include file=%file markup=\"?\">"))
  10863. "Structure completion elements.
  10864. This is a list of abbreviation keys and values. The value gets inserted
  10865. if you type `<' followed by the key and then press the completion key,
  10866. usually `M-TAB'. %file will be replaced by a file name after prompting
  10867. for the file using completion. The cursor will be placed at the position
  10868. of the `?` in the template.
  10869. There are two templates for each key, the first uses the original Org syntax,
  10870. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10871. the default when the /org-mtags.el/ module has been loaded. See also the
  10872. variable `org-mtags-prefer-muse-templates'."
  10873. :group 'org-completion
  10874. :type '(repeat
  10875. (list
  10876. (string :tag "Key")
  10877. (string :tag "Template")
  10878. (string :tag "Muse Template"))))
  10879. (defun org-try-structure-completion ()
  10880. "Try to complete a structure template before point.
  10881. This looks for strings like \"<e\" on an otherwise empty line and
  10882. expands them."
  10883. (let ((l (buffer-substring (point-at-bol) (point)))
  10884. a)
  10885. (when (and (looking-at "[ \t]*$")
  10886. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10887. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10888. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10889. (match-beginning 1)) a)
  10890. t)))
  10891. (defun org-complete-expand-structure-template (start cell)
  10892. "Expand a structure template."
  10893. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10894. (rpl (nth (if musep 2 1) cell))
  10895. (ind ""))
  10896. (delete-region start (point))
  10897. (when (string-match "\\`#\\+" rpl)
  10898. (cond
  10899. ((bolp))
  10900. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10901. (setq ind (buffer-substring (point-at-bol) (point))))
  10902. (t (newline))))
  10903. (setq start (point))
  10904. (if (string-match "%file" rpl)
  10905. (setq rpl (replace-match
  10906. (concat
  10907. "\""
  10908. (save-match-data
  10909. (abbreviate-file-name (read-file-name "Include file: ")))
  10910. "\"")
  10911. t t rpl)))
  10912. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10913. (concat "\n" ind)))
  10914. (insert rpl)
  10915. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10916. ;;;; TODO, DEADLINE, Comments
  10917. (defun org-toggle-comment ()
  10918. "Change the COMMENT state of an entry."
  10919. (interactive)
  10920. (save-excursion
  10921. (org-back-to-heading)
  10922. (let (case-fold-search)
  10923. (cond
  10924. ((looking-at (format org-heading-keyword-regexp-format
  10925. org-comment-string))
  10926. (goto-char (match-end 1))
  10927. (looking-at (concat " +" org-comment-string))
  10928. (replace-match "" t t)
  10929. (when (eolp) (insert " ")))
  10930. ((looking-at org-outline-regexp)
  10931. (goto-char (match-end 0))
  10932. (insert org-comment-string " "))))))
  10933. (defvar org-last-todo-state-is-todo nil
  10934. "This is non-nil when the last TODO state change led to a TODO state.
  10935. If the last change removed the TODO tag or switched to DONE, then
  10936. this is nil.")
  10937. (defvar org-setting-tags nil) ; dynamically skipped
  10938. (defvar org-todo-setup-filter-hook nil
  10939. "Hook for functions that pre-filter todo specs.
  10940. Each function takes a todo spec and returns either nil or the spec
  10941. transformed into canonical form." )
  10942. (defvar org-todo-get-default-hook nil
  10943. "Hook for functions that get a default item for todo.
  10944. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10945. nil or a string to be used for the todo mark." )
  10946. (defvar org-agenda-headline-snapshot-before-repeat)
  10947. (defun org-current-effective-time ()
  10948. "Return current time adjusted for `org-extend-today-until' variable."
  10949. (let* ((ct (org-current-time))
  10950. (dct (decode-time ct))
  10951. (ct1
  10952. (cond
  10953. (org-use-last-clock-out-time-as-effective-time
  10954. (or (org-clock-get-last-clock-out-time) ct))
  10955. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10956. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10957. (t ct))))
  10958. ct1))
  10959. (defun org-todo-yesterday (&optional arg)
  10960. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10961. (interactive "P")
  10962. (if (eq major-mode 'org-agenda-mode)
  10963. (apply 'org-agenda-todo-yesterday arg)
  10964. (let* ((hour (third (decode-time
  10965. (org-current-time))))
  10966. (org-extend-today-until (1+ hour)))
  10967. (org-todo arg))))
  10968. (defvar org-block-entry-blocking ""
  10969. "First entry preventing the TODO state change.")
  10970. (defun org-todo (&optional arg)
  10971. "Change the TODO state of an item.
  10972. The state of an item is given by a keyword at the start of the heading,
  10973. like
  10974. *** TODO Write paper
  10975. *** DONE Call mom
  10976. The different keywords are specified in the variable `org-todo-keywords'.
  10977. By default the available states are \"TODO\" and \"DONE\".
  10978. So for this example: when the item starts with TODO, it is changed to DONE.
  10979. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10980. DONE are present, add TODO at the beginning of the heading.
  10981. With \\[universal-argument] prefix arg, use completion to determine the new \
  10982. state.
  10983. With numeric prefix arg, switch to that state.
  10984. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10985. keywords (nextset).
  10986. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10987. With a numeric prefix arg of 0, inhibit note taking for the change.
  10988. For calling through lisp, arg is also interpreted in the following way:
  10989. 'none -> empty state
  10990. \"\"(empty string) -> switch to empty state
  10991. 'done -> switch to DONE
  10992. 'nextset -> switch to the next set of keywords
  10993. 'previousset -> switch to the previous set of keywords
  10994. \"WAITING\" -> switch to the specified keyword, but only if it
  10995. really is a member of `org-todo-keywords'."
  10996. (interactive "P")
  10997. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10998. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10999. 'region-start-level 'region))
  11000. org-loop-over-headlines-in-active-region)
  11001. (org-map-entries
  11002. `(org-todo ,arg)
  11003. org-loop-over-headlines-in-active-region
  11004. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11005. (if (equal arg '(16)) (setq arg 'nextset))
  11006. (let ((org-blocker-hook org-blocker-hook)
  11007. commentp
  11008. case-fold-search)
  11009. (when (equal arg '(64))
  11010. (setq arg nil org-blocker-hook nil))
  11011. (when (and org-blocker-hook
  11012. (or org-inhibit-blocking
  11013. (org-entry-get nil "NOBLOCKING")))
  11014. (setq org-blocker-hook nil))
  11015. (save-excursion
  11016. (catch 'exit
  11017. (org-back-to-heading t)
  11018. (when (looking-at (concat "^\\*+ " org-comment-string))
  11019. (org-toggle-comment)
  11020. (setq commentp t))
  11021. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11022. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11023. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11024. (let* ((match-data (match-data))
  11025. (startpos (point-at-bol))
  11026. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11027. (org-log-done org-log-done)
  11028. (org-log-repeat org-log-repeat)
  11029. (org-todo-log-states org-todo-log-states)
  11030. (org-inhibit-logging
  11031. (if (equal arg 0)
  11032. (progn (setq arg nil) 'note) org-inhibit-logging))
  11033. (this (match-string 1))
  11034. (hl-pos (match-beginning 0))
  11035. (head (org-get-todo-sequence-head this))
  11036. (ass (assoc head org-todo-kwd-alist))
  11037. (interpret (nth 1 ass))
  11038. (done-word (nth 3 ass))
  11039. (final-done-word (nth 4 ass))
  11040. (org-last-state (or this ""))
  11041. (completion-ignore-case t)
  11042. (member (member this org-todo-keywords-1))
  11043. (tail (cdr member))
  11044. (org-state (cond
  11045. ((and org-todo-key-trigger
  11046. (or (and (equal arg '(4))
  11047. (eq org-use-fast-todo-selection 'prefix))
  11048. (and (not arg) org-use-fast-todo-selection
  11049. (not (eq org-use-fast-todo-selection
  11050. 'prefix)))))
  11051. ;; Use fast selection
  11052. (org-fast-todo-selection))
  11053. ((and (equal arg '(4))
  11054. (or (not org-use-fast-todo-selection)
  11055. (not org-todo-key-trigger)))
  11056. ;; Read a state with completion
  11057. (org-icompleting-read
  11058. "State: " (mapcar 'list org-todo-keywords-1)
  11059. nil t))
  11060. ((eq arg 'right)
  11061. (if this
  11062. (if tail (car tail) nil)
  11063. (car org-todo-keywords-1)))
  11064. ((eq arg 'left)
  11065. (if (equal member org-todo-keywords-1)
  11066. nil
  11067. (if this
  11068. (nth (- (length org-todo-keywords-1)
  11069. (length tail) 2)
  11070. org-todo-keywords-1)
  11071. (org-last org-todo-keywords-1))))
  11072. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11073. (setq arg nil))) ; hack to fall back to cycling
  11074. (arg
  11075. ;; user or caller requests a specific state
  11076. (cond
  11077. ((equal arg "") nil)
  11078. ((eq arg 'none) nil)
  11079. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11080. ((eq arg 'nextset)
  11081. (or (car (cdr (member head org-todo-heads)))
  11082. (car org-todo-heads)))
  11083. ((eq arg 'previousset)
  11084. (let ((org-todo-heads (reverse org-todo-heads)))
  11085. (or (car (cdr (member head org-todo-heads)))
  11086. (car org-todo-heads))))
  11087. ((car (member arg org-todo-keywords-1)))
  11088. ((stringp arg)
  11089. (user-error "State `%s' not valid in this file" arg))
  11090. ((nth (1- (prefix-numeric-value arg))
  11091. org-todo-keywords-1))))
  11092. ((null member) (or head (car org-todo-keywords-1)))
  11093. ((equal this final-done-word) nil) ;; -> make empty
  11094. ((null tail) nil) ;; -> first entry
  11095. ((memq interpret '(type priority))
  11096. (if (eq this-command last-command)
  11097. (car tail)
  11098. (if (> (length tail) 0)
  11099. (or done-word (car org-done-keywords))
  11100. nil)))
  11101. (t
  11102. (car tail))))
  11103. (org-state (or
  11104. (run-hook-with-args-until-success
  11105. 'org-todo-get-default-hook org-state org-last-state)
  11106. org-state))
  11107. (next (if org-state (concat " " org-state " ") " "))
  11108. (change-plist (list :type 'todo-state-change :from this :to org-state
  11109. :position startpos))
  11110. dolog now-done-p)
  11111. (when org-blocker-hook
  11112. (setq org-last-todo-state-is-todo
  11113. (not (member this org-done-keywords)))
  11114. (unless (save-excursion
  11115. (save-match-data
  11116. (org-with-wide-buffer
  11117. (run-hook-with-args-until-failure
  11118. 'org-blocker-hook change-plist))))
  11119. (if (org-called-interactively-p 'interactive)
  11120. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11121. this org-state org-block-entry-blocking)
  11122. ;; fail silently
  11123. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11124. this org-state org-block-entry-blocking)
  11125. (throw 'exit nil))))
  11126. (store-match-data match-data)
  11127. (replace-match next t t)
  11128. (unless (pos-visible-in-window-p hl-pos)
  11129. (message "TODO state changed to %s" (org-trim next)))
  11130. (unless head
  11131. (setq head (org-get-todo-sequence-head org-state)
  11132. ass (assoc head org-todo-kwd-alist)
  11133. interpret (nth 1 ass)
  11134. done-word (nth 3 ass)
  11135. final-done-word (nth 4 ass)))
  11136. (when (memq arg '(nextset previousset))
  11137. (message "Keyword-Set %d/%d: %s"
  11138. (- (length org-todo-sets) -1
  11139. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11140. (length org-todo-sets)
  11141. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11142. (setq org-last-todo-state-is-todo
  11143. (not (member org-state org-done-keywords)))
  11144. (setq now-done-p (and (member org-state org-done-keywords)
  11145. (not (member this org-done-keywords))))
  11146. (and logging (org-local-logging logging))
  11147. (when (and (or org-todo-log-states org-log-done)
  11148. (not (eq org-inhibit-logging t))
  11149. (not (memq arg '(nextset previousset))))
  11150. ;; we need to look at recording a time and note
  11151. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11152. (nth 2 (assoc this org-todo-log-states))))
  11153. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11154. (setq dolog 'time))
  11155. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11156. (and org-state
  11157. (member org-state org-not-done-keywords)
  11158. (not (member this org-not-done-keywords))))
  11159. ;; This is now a todo state and was not one before
  11160. ;; If there was a CLOSED time stamp, get rid of it.
  11161. (org-add-planning-info nil nil 'closed))
  11162. (when (and now-done-p org-log-done)
  11163. ;; It is now done, and it was not done before
  11164. (org-add-planning-info 'closed (org-current-effective-time))
  11165. (if (and (not dolog) (eq 'note org-log-done))
  11166. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11167. (when (and org-state dolog)
  11168. ;; This is a non-nil state, and we need to log it
  11169. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11170. ;; Fixup tag positioning
  11171. (org-todo-trigger-tag-changes org-state)
  11172. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11173. (when org-provide-todo-statistics
  11174. (org-update-parent-todo-statistics))
  11175. (run-hooks 'org-after-todo-state-change-hook)
  11176. (if (and arg (not (member org-state org-done-keywords)))
  11177. (setq head (org-get-todo-sequence-head org-state)))
  11178. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11179. ;; Do we need to trigger a repeat?
  11180. (when now-done-p
  11181. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11182. ;; This is for the agenda, take a snapshot of the headline.
  11183. (save-match-data
  11184. (setq org-agenda-headline-snapshot-before-repeat
  11185. (org-get-heading))))
  11186. (org-auto-repeat-maybe org-state))
  11187. ;; Fixup cursor location if close to the keyword
  11188. (if (and (outline-on-heading-p)
  11189. (not (bolp))
  11190. (save-excursion (beginning-of-line 1)
  11191. (looking-at org-todo-line-regexp))
  11192. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11193. (progn
  11194. (goto-char (or (match-end 2) (match-end 1)))
  11195. (and (looking-at " ") (just-one-space))))
  11196. (when org-trigger-hook
  11197. (save-excursion
  11198. (run-hook-with-args 'org-trigger-hook change-plist)))
  11199. (when commentp (org-toggle-comment))))))))
  11200. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11201. "Block turning an entry into a TODO, using the hierarchy.
  11202. This checks whether the current task should be blocked from state
  11203. changes. Such blocking occurs when:
  11204. 1. The task has children which are not all in a completed state.
  11205. 2. A task has a parent with the property :ORDERED:, and there
  11206. are siblings prior to the current task with incomplete
  11207. status.
  11208. 3. The parent of the task is blocked because it has siblings that should
  11209. be done first, or is child of a block grandparent TODO entry."
  11210. (if (not org-enforce-todo-dependencies)
  11211. t ; if locally turned off don't block
  11212. (catch 'dont-block
  11213. ;; If this is not a todo state change, or if this entry is already DONE,
  11214. ;; do not block
  11215. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11216. (member (plist-get change-plist :from)
  11217. (cons 'done org-done-keywords))
  11218. (member (plist-get change-plist :to)
  11219. (cons 'todo org-not-done-keywords))
  11220. (not (plist-get change-plist :to)))
  11221. (throw 'dont-block t))
  11222. ;; If this task has children, and any are undone, it's blocked
  11223. (save-excursion
  11224. (org-back-to-heading t)
  11225. (let ((this-level (funcall outline-level)))
  11226. (outline-next-heading)
  11227. (let ((child-level (funcall outline-level)))
  11228. (while (and (not (eobp))
  11229. (> child-level this-level))
  11230. ;; this todo has children, check whether they are all
  11231. ;; completed
  11232. (if (and (not (org-entry-is-done-p))
  11233. (org-entry-is-todo-p))
  11234. (progn (setq org-block-entry-blocking (org-get-heading))
  11235. (throw 'dont-block nil)))
  11236. (outline-next-heading)
  11237. (setq child-level (funcall outline-level))))))
  11238. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11239. ;; any previous siblings are undone, it's blocked
  11240. (save-excursion
  11241. (org-back-to-heading t)
  11242. (let* ((pos (point))
  11243. (parent-pos (and (org-up-heading-safe) (point))))
  11244. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11245. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11246. (forward-line 1)
  11247. (re-search-forward org-not-done-heading-regexp pos t))
  11248. (setq org-block-entry-blocking (match-string 0))
  11249. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11250. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11251. (while t
  11252. (goto-char parent-pos)
  11253. (if (not (looking-at org-not-done-heading-regexp))
  11254. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11255. (setq pos (point))
  11256. (setq parent-pos (and (org-up-heading-safe) (point)))
  11257. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11258. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11259. (forward-line 1)
  11260. (re-search-forward org-not-done-heading-regexp pos t)
  11261. (setq org-block-entry-blocking (org-get-heading)))
  11262. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11263. (defcustom org-track-ordered-property-with-tag nil
  11264. "Should the ORDERED property also be shown as a tag?
  11265. The ORDERED property decides if an entry should require subtasks to be
  11266. completed in sequence. Since a property is not very visible, setting
  11267. this option means that toggling the ORDERED property with the command
  11268. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11269. not relevant for the behavior, but it makes things more visible.
  11270. Note that toggling the tag with tags commands will not change the property
  11271. and therefore not influence behavior!
  11272. This can be t, meaning the tag ORDERED should be used, It can also be a
  11273. string to select a different tag for this task."
  11274. :group 'org-todo
  11275. :type '(choice
  11276. (const :tag "No tracking" nil)
  11277. (const :tag "Track with ORDERED tag" t)
  11278. (string :tag "Use other tag")))
  11279. (defun org-toggle-ordered-property ()
  11280. "Toggle the ORDERED property of the current entry.
  11281. For better visibility, you can track the value of this property with a tag.
  11282. See variable `org-track-ordered-property-with-tag'."
  11283. (interactive)
  11284. (let* ((t1 org-track-ordered-property-with-tag)
  11285. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11286. (save-excursion
  11287. (org-back-to-heading)
  11288. (if (org-entry-get nil "ORDERED")
  11289. (progn
  11290. (org-delete-property "ORDERED")
  11291. (and tag (org-toggle-tag tag 'off))
  11292. (message "Subtasks can be completed in arbitrary order"))
  11293. (org-entry-put nil "ORDERED" "t")
  11294. (and tag (org-toggle-tag tag 'on))
  11295. (message "Subtasks must be completed in sequence")))))
  11296. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11297. (defun org-block-todo-from-checkboxes (change-plist)
  11298. "Block turning an entry into a TODO, using checkboxes.
  11299. This checks whether the current task should be blocked from state
  11300. changes because there are unchecked boxes in this entry."
  11301. (if (not org-enforce-todo-checkbox-dependencies)
  11302. t ; if locally turned off don't block
  11303. (catch 'dont-block
  11304. ;; If this is not a todo state change, or if this entry is already DONE,
  11305. ;; do not block
  11306. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11307. (member (plist-get change-plist :from)
  11308. (cons 'done org-done-keywords))
  11309. (member (plist-get change-plist :to)
  11310. (cons 'todo org-not-done-keywords))
  11311. (not (plist-get change-plist :to)))
  11312. (throw 'dont-block t))
  11313. ;; If this task has checkboxes that are not checked, it's blocked
  11314. (save-excursion
  11315. (org-back-to-heading t)
  11316. (let ((beg (point)) end)
  11317. (outline-next-heading)
  11318. (setq end (point))
  11319. (goto-char beg)
  11320. (if (org-list-search-forward
  11321. (concat (org-item-beginning-re)
  11322. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11323. "\\[[- ]\\]")
  11324. end t)
  11325. (progn
  11326. (if (boundp 'org-blocked-by-checkboxes)
  11327. (setq org-blocked-by-checkboxes t))
  11328. (throw 'dont-block nil)))))
  11329. t))) ; do not block
  11330. (defun org-entry-blocked-p ()
  11331. "Is the current entry blocked?"
  11332. (org-with-silent-modifications
  11333. (if (org-entry-get nil "NOBLOCKING")
  11334. nil ;; Never block this entry
  11335. (not (run-hook-with-args-until-failure
  11336. 'org-blocker-hook
  11337. (list :type 'todo-state-change
  11338. :position (point)
  11339. :from 'todo
  11340. :to 'done))))))
  11341. (defun org-update-statistics-cookies (all)
  11342. "Update the statistics cookie, either from TODO or from checkboxes.
  11343. This should be called with the cursor in a line with a statistics cookie."
  11344. (interactive "P")
  11345. (if all
  11346. (progn
  11347. (org-update-checkbox-count 'all)
  11348. (org-map-entries 'org-update-parent-todo-statistics))
  11349. (if (not (org-at-heading-p))
  11350. (org-update-checkbox-count)
  11351. (let ((pos (point-marker))
  11352. end l1 l2)
  11353. (ignore-errors (org-back-to-heading t))
  11354. (if (not (org-at-heading-p))
  11355. (org-update-checkbox-count)
  11356. (setq l1 (org-outline-level))
  11357. (setq end (save-excursion
  11358. (outline-next-heading)
  11359. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11360. (point)))
  11361. (if (and (save-excursion
  11362. (re-search-forward
  11363. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11364. (not (save-excursion (re-search-forward
  11365. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11366. (org-update-checkbox-count)
  11367. (if (and l2 (> l2 l1))
  11368. (progn
  11369. (goto-char end)
  11370. (org-update-parent-todo-statistics))
  11371. (goto-char pos)
  11372. (beginning-of-line 1)
  11373. (while (re-search-forward
  11374. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11375. (point-at-eol) t)
  11376. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11377. (goto-char pos)
  11378. (move-marker pos nil)))))
  11379. (defvar org-entry-property-inherited-from) ;; defined below
  11380. (defun org-update-parent-todo-statistics ()
  11381. "Update any statistics cookie in the parent of the current headline.
  11382. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11383. the entire subtree and this will travel up the hierarchy and update
  11384. statistics everywhere."
  11385. (let* ((prop (save-excursion (org-up-heading-safe)
  11386. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11387. (recursive (or (not org-hierarchical-todo-statistics)
  11388. (and prop (string-match "\\<recursive\\>" prop))))
  11389. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11390. 0))
  11391. (first t)
  11392. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11393. level ltoggle l1 new ndel
  11394. (cnt-all 0) (cnt-done 0) is-percent kwd
  11395. checkbox-beg ov ovs ove cookie-present)
  11396. (catch 'exit
  11397. (save-excursion
  11398. (beginning-of-line 1)
  11399. (setq ltoggle (funcall outline-level))
  11400. ;; Three situations are to consider:
  11401. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11402. ;; to the top-level ancestor on the headline;
  11403. ;; 2. If parent has "recursive" property, repeat up to the
  11404. ;; headline setting that property, taking inheritance into
  11405. ;; account;
  11406. ;; 3. Else, move up to direct parent and proceed only once.
  11407. (while (and (setq level (org-up-heading-safe))
  11408. (or recursive first)
  11409. (>= (point) lim))
  11410. (setq first nil cookie-present nil)
  11411. (unless (and level
  11412. (not (string-match
  11413. "\\<checkbox\\>"
  11414. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11415. "")))))
  11416. (throw 'exit nil))
  11417. (while (re-search-forward box-re (point-at-eol) t)
  11418. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11419. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11420. (save-match-data
  11421. (unless (outline-next-heading) (throw 'exit nil))
  11422. (while (and (looking-at org-complex-heading-regexp)
  11423. (> (setq l1 (length (match-string 1))) level))
  11424. (setq kwd (and (or recursive (= l1 ltoggle))
  11425. (match-string 2)))
  11426. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11427. (and (listp org-provide-todo-statistics)
  11428. (or (member kwd org-provide-todo-statistics)
  11429. (member kwd org-done-keywords))))
  11430. (setq cnt-all (1+ cnt-all))
  11431. (if (eq org-provide-todo-statistics t)
  11432. (and kwd (setq cnt-all (1+ cnt-all)))))
  11433. (and (member kwd org-done-keywords)
  11434. (setq cnt-done (1+ cnt-done)))
  11435. (outline-next-heading)))
  11436. (setq new
  11437. (if is-percent
  11438. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11439. (format "[%d/%d]" cnt-done cnt-all))
  11440. ndel (- (match-end 0) checkbox-beg))
  11441. ;; handle overlays when updating cookie from column view
  11442. (when (setq ov (car (overlays-at checkbox-beg)))
  11443. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11444. (delete-overlay ov))
  11445. (goto-char checkbox-beg)
  11446. (insert new)
  11447. (delete-region (point) (+ (point) ndel))
  11448. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11449. (when ov (move-overlay ov ovs ove)))
  11450. (when cookie-present
  11451. (run-hook-with-args 'org-after-todo-statistics-hook
  11452. cnt-done (- cnt-all cnt-done))))))
  11453. (run-hooks 'org-todo-statistics-hook)))
  11454. (defvar org-after-todo-statistics-hook nil
  11455. "Hook that is called after a TODO statistics cookie has been updated.
  11456. Each function is called with two arguments: the number of not-done entries
  11457. and the number of done entries.
  11458. For example, the following function, when added to this hook, will switch
  11459. an entry to DONE when all children are done, and back to TODO when new
  11460. entries are set to a TODO status. Note that this hook is only called
  11461. when there is a statistics cookie in the headline!
  11462. (defun org-summary-todo (n-done n-not-done)
  11463. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11464. (let (org-log-done org-log-states) ; turn off logging
  11465. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11466. ")
  11467. (defvar org-todo-statistics-hook nil
  11468. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11469. This hook runs even if there is no statistics cookie present, in which case
  11470. `org-after-todo-statistics-hook' would not run.")
  11471. (defun org-todo-trigger-tag-changes (state)
  11472. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11473. (let ((l org-todo-state-tags-triggers)
  11474. changes)
  11475. (when (or (not state) (equal state ""))
  11476. (setq changes (append changes (cdr (assoc "" l)))))
  11477. (when (and (stringp state) (> (length state) 0))
  11478. (setq changes (append changes (cdr (assoc state l)))))
  11479. (when (member state org-not-done-keywords)
  11480. (setq changes (append changes (cdr (assoc 'todo l)))))
  11481. (when (member state org-done-keywords)
  11482. (setq changes (append changes (cdr (assoc 'done l)))))
  11483. (dolist (c changes)
  11484. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11485. (defun org-local-logging (value)
  11486. "Get logging settings from a property VALUE."
  11487. (let* (words w a)
  11488. ;; directly set the variables, they are already local.
  11489. (setq org-log-done nil
  11490. org-log-repeat nil
  11491. org-todo-log-states nil)
  11492. (setq words (org-split-string value))
  11493. (while (setq w (pop words))
  11494. (cond
  11495. ((setq a (assoc w org-startup-options))
  11496. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11497. (set (nth 1 a) (nth 2 a))))
  11498. ((setq a (org-extract-log-state-settings w))
  11499. (and (member (car a) org-todo-keywords-1)
  11500. (push a org-todo-log-states)))))))
  11501. (defun org-get-todo-sequence-head (kwd)
  11502. "Return the head of the TODO sequence to which KWD belongs.
  11503. If KWD is not set, check if there is a text property remembering the
  11504. right sequence."
  11505. (let (p)
  11506. (cond
  11507. ((not kwd)
  11508. (or (get-text-property (point-at-bol) 'org-todo-head)
  11509. (progn
  11510. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11511. nil (point-at-eol)))
  11512. (get-text-property p 'org-todo-head))))
  11513. ((not (member kwd org-todo-keywords-1))
  11514. (car org-todo-keywords-1))
  11515. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11516. (defun org-fast-todo-selection ()
  11517. "Fast TODO keyword selection with single keys.
  11518. Returns the new TODO keyword, or nil if no state change should occur."
  11519. (let* ((fulltable org-todo-key-alist)
  11520. (done-keywords org-done-keywords) ;; needed for the faces.
  11521. (maxlen (apply 'max (mapcar
  11522. (lambda (x)
  11523. (if (stringp (car x)) (string-width (car x)) 0))
  11524. fulltable)))
  11525. (expert nil)
  11526. (fwidth (+ maxlen 3 1 3))
  11527. (ncol (/ (- (window-width) 4) fwidth))
  11528. tg cnt e c tbl
  11529. groups ingroup)
  11530. (save-excursion
  11531. (save-window-excursion
  11532. (if expert
  11533. (set-buffer (get-buffer-create " *Org todo*"))
  11534. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11535. (erase-buffer)
  11536. (org-set-local 'org-done-keywords done-keywords)
  11537. (setq tbl fulltable cnt 0)
  11538. (while (setq e (pop tbl))
  11539. (cond
  11540. ((equal e '(:startgroup))
  11541. (push '() groups) (setq ingroup t)
  11542. (when (not (= cnt 0))
  11543. (setq cnt 0)
  11544. (insert "\n"))
  11545. (insert "{ "))
  11546. ((equal e '(:endgroup))
  11547. (setq ingroup nil cnt 0)
  11548. (insert "}\n"))
  11549. ((equal e '(:newline))
  11550. (when (not (= cnt 0))
  11551. (setq cnt 0)
  11552. (insert "\n")
  11553. (setq e (car tbl))
  11554. (while (equal (car tbl) '(:newline))
  11555. (insert "\n")
  11556. (setq tbl (cdr tbl)))))
  11557. (t
  11558. (setq tg (car e) c (cdr e))
  11559. (if ingroup (push tg (car groups)))
  11560. (setq tg (org-add-props tg nil 'face
  11561. (org-get-todo-face tg)))
  11562. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11563. (insert "[" c "] " tg (make-string
  11564. (- fwidth 4 (length tg)) ?\ ))
  11565. (when (= (setq cnt (1+ cnt)) ncol)
  11566. (insert "\n")
  11567. (if ingroup (insert " "))
  11568. (setq cnt 0)))))
  11569. (insert "\n")
  11570. (goto-char (point-min))
  11571. (if (not expert) (org-fit-window-to-buffer))
  11572. (message "[a-z..]:Set [SPC]:clear")
  11573. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11574. (cond
  11575. ((or (= c ?\C-g)
  11576. (and (= c ?q) (not (rassoc c fulltable))))
  11577. (setq quit-flag t))
  11578. ((= c ?\ ) nil)
  11579. ((setq e (rassoc c fulltable) tg (car e))
  11580. tg)
  11581. (t (setq quit-flag t)))))))
  11582. (defun org-entry-is-todo-p ()
  11583. (member (org-get-todo-state) org-not-done-keywords))
  11584. (defun org-entry-is-done-p ()
  11585. (member (org-get-todo-state) org-done-keywords))
  11586. (defun org-get-todo-state ()
  11587. "Return the TODO keyword of the current subtree."
  11588. (save-excursion
  11589. (org-back-to-heading t)
  11590. (and (looking-at org-todo-line-regexp)
  11591. (match-end 2)
  11592. (match-string 2))))
  11593. (defun org-at-date-range-p (&optional inactive-ok)
  11594. "Is the cursor inside a date range?"
  11595. (interactive)
  11596. (save-excursion
  11597. (catch 'exit
  11598. (let ((pos (point)))
  11599. (skip-chars-backward "^[<\r\n")
  11600. (skip-chars-backward "<[")
  11601. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11602. (>= (match-end 0) pos)
  11603. (throw 'exit t))
  11604. (skip-chars-backward "^<[\r\n")
  11605. (skip-chars-backward "<[")
  11606. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11607. (>= (match-end 0) pos)
  11608. (throw 'exit t)))
  11609. nil)))
  11610. (defun org-get-repeat (&optional tagline)
  11611. "Check if there is a deadline/schedule with repeater in this entry."
  11612. (save-match-data
  11613. (save-excursion
  11614. (org-back-to-heading t)
  11615. (and (re-search-forward (if tagline
  11616. (concat tagline "\\s-*" org-repeat-re)
  11617. org-repeat-re)
  11618. (org-entry-end-position) t)
  11619. (match-string-no-properties 1)))))
  11620. (defvar org-last-changed-timestamp)
  11621. (defvar org-last-inserted-timestamp)
  11622. (defvar org-log-post-message)
  11623. (defvar org-log-note-purpose)
  11624. (defvar org-log-note-how)
  11625. (defvar org-log-note-extra)
  11626. (defun org-auto-repeat-maybe (done-word)
  11627. "Check if the current headline contains a repeated deadline/schedule.
  11628. If yes, set TODO state back to what it was and change the base date
  11629. of repeating deadline/scheduled time stamps to new date.
  11630. This function is run automatically after each state change to a DONE state."
  11631. ;; last-state is dynamically scoped into this function
  11632. (let* ((repeat (org-get-repeat))
  11633. (aa (assoc org-last-state org-todo-kwd-alist))
  11634. (interpret (nth 1 aa))
  11635. (head (nth 2 aa))
  11636. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11637. (msg "Entry repeats: ")
  11638. (org-log-done nil)
  11639. (org-todo-log-states nil)
  11640. re type n what ts time to-state)
  11641. (when repeat
  11642. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11643. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11644. org-todo-repeat-to-state))
  11645. (unless (and to-state (member to-state org-todo-keywords-1))
  11646. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11647. (org-todo to-state)
  11648. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11649. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11650. (org-time-stamp-format t t))))
  11651. (when org-log-repeat
  11652. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11653. (memq 'org-add-log-note post-command-hook))
  11654. ;; OK, we are already setup for some record
  11655. (if (eq org-log-repeat 'note)
  11656. ;; make sure we take a note, not only a time stamp
  11657. (setq org-log-note-how 'note))
  11658. ;; Set up for taking a record
  11659. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11660. org-last-state
  11661. 'findpos org-log-repeat)))
  11662. (org-back-to-heading t)
  11663. (org-add-planning-info nil nil 'closed)
  11664. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11665. org-deadline-time-regexp "\\)\\|\\("
  11666. org-ts-regexp "\\)"))
  11667. (while (re-search-forward
  11668. re (save-excursion (outline-next-heading) (point)) t)
  11669. (setq type (if (match-end 1) org-scheduled-string
  11670. (if (match-end 3) org-deadline-string "Plain:"))
  11671. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11672. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11673. (setq n (string-to-number (match-string 2 ts))
  11674. what (match-string 3 ts))
  11675. (if (equal what "w") (setq n (* n 7) what "d"))
  11676. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11677. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11678. ;; Preparation, see if we need to modify the start date for the change
  11679. (when (match-end 1)
  11680. (setq time (save-match-data (org-time-string-to-time ts)))
  11681. (cond
  11682. ((equal (match-string 1 ts) ".")
  11683. ;; Shift starting date to today
  11684. (org-timestamp-change
  11685. (- (org-today) (time-to-days time))
  11686. 'day))
  11687. ((equal (match-string 1 ts) "+")
  11688. (let ((nshiftmax 10) (nshift 0))
  11689. (while (or (= nshift 0)
  11690. (<= (time-to-days time)
  11691. (time-to-days (current-time))))
  11692. (when (= (incf nshift) nshiftmax)
  11693. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11694. (error "Abort")))
  11695. (org-timestamp-change n (cdr (assoc what whata)))
  11696. (org-at-timestamp-p t)
  11697. (setq ts (match-string 1))
  11698. (setq time (save-match-data (org-time-string-to-time ts)))))
  11699. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11700. ;; rematch, so that we have everything in place for the real shift
  11701. (org-at-timestamp-p t)
  11702. (setq ts (match-string 1))
  11703. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11704. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11705. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11706. (setq org-log-post-message msg)
  11707. (message "%s" msg))))
  11708. (defun org-show-todo-tree (arg)
  11709. "Make a compact tree which shows all headlines marked with TODO.
  11710. The tree will show the lines where the regexp matches, and all higher
  11711. headlines above the match.
  11712. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11713. With a numeric prefix N, construct a sparse tree for the Nth element
  11714. of `org-todo-keywords-1'."
  11715. (interactive "P")
  11716. (let ((case-fold-search nil)
  11717. (kwd-re
  11718. (cond ((null arg) org-not-done-regexp)
  11719. ((equal arg '(4))
  11720. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11721. (mapcar 'list org-todo-keywords-1))))
  11722. (concat "\\("
  11723. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11724. "\\)\\>")))
  11725. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11726. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11727. org-todo-keywords-1)))
  11728. (t (user-error "Invalid prefix argument: %s" arg)))))
  11729. (message "%d TODO entries found"
  11730. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11731. (defun org-deadline (arg &optional time)
  11732. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11733. With one universal prefix argument, remove any deadline from the item.
  11734. With two universal prefix arguments, prompt for a warning delay.
  11735. With argument TIME, set the deadline at the corresponding date. TIME
  11736. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11737. (interactive "P")
  11738. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11739. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11740. 'region-start-level 'region))
  11741. org-loop-over-headlines-in-active-region)
  11742. (org-map-entries
  11743. `(org-deadline ',arg ,time)
  11744. org-loop-over-headlines-in-active-region
  11745. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11746. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11747. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11748. (repeater (and old-date
  11749. (string-match
  11750. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11751. old-date)
  11752. (match-string 1 old-date))))
  11753. (cond
  11754. ((equal arg '(4))
  11755. (when (and old-date org-log-redeadline)
  11756. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11757. org-log-redeadline))
  11758. (org-remove-timestamp-with-keyword org-deadline-string)
  11759. (message "Item no longer has a deadline."))
  11760. ((equal arg '(16))
  11761. (save-excursion
  11762. (if (re-search-forward
  11763. org-deadline-time-regexp
  11764. (save-excursion (outline-next-heading) (point)) t)
  11765. (let* ((rpl0 (match-string 1))
  11766. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11767. (replace-match
  11768. (concat org-deadline-string
  11769. " <" rpl
  11770. (format " -%dd"
  11771. (abs
  11772. (- (time-to-days
  11773. (save-match-data
  11774. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11775. (time-to-days old-date-time))))
  11776. ">") t t))
  11777. (user-error "No deadline information to update"))))
  11778. (t
  11779. (org-add-planning-info 'deadline time 'closed)
  11780. (when (and old-date org-log-redeadline
  11781. (not (equal old-date
  11782. (substring org-last-inserted-timestamp 1 -1))))
  11783. (org-add-log-setup 'redeadline nil old-date 'findpos
  11784. org-log-redeadline))
  11785. (when repeater
  11786. (save-excursion
  11787. (org-back-to-heading t)
  11788. (when (re-search-forward (concat org-deadline-string " "
  11789. org-last-inserted-timestamp)
  11790. (save-excursion
  11791. (outline-next-heading) (point)) t)
  11792. (goto-char (1- (match-end 0)))
  11793. (insert " " repeater)
  11794. (setq org-last-inserted-timestamp
  11795. (concat (substring org-last-inserted-timestamp 0 -1)
  11796. " " repeater
  11797. (substring org-last-inserted-timestamp -1))))))
  11798. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11799. (defun org-schedule (arg &optional time)
  11800. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11801. With one universal prefix argument, remove any scheduling date from the item.
  11802. With two universal prefix arguments, prompt for a delay cookie.
  11803. With argument TIME, scheduled at the corresponding date. TIME can
  11804. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11805. (interactive "P")
  11806. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11807. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11808. 'region-start-level 'region))
  11809. org-loop-over-headlines-in-active-region)
  11810. (org-map-entries
  11811. `(org-schedule ',arg ,time)
  11812. org-loop-over-headlines-in-active-region
  11813. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11814. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11815. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11816. (repeater (and old-date
  11817. (string-match
  11818. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11819. old-date)
  11820. (match-string 1 old-date))))
  11821. (cond
  11822. ((equal arg '(4))
  11823. (progn
  11824. (when (and old-date org-log-reschedule)
  11825. (org-add-log-setup 'delschedule nil old-date 'findpos
  11826. org-log-reschedule))
  11827. (org-remove-timestamp-with-keyword org-scheduled-string)
  11828. (message "Item is no longer scheduled.")))
  11829. ((equal arg '(16))
  11830. (save-excursion
  11831. (if (re-search-forward
  11832. org-scheduled-time-regexp
  11833. (save-excursion (outline-next-heading) (point)) t)
  11834. (let* ((rpl0 (match-string 1))
  11835. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11836. (replace-match
  11837. (concat org-scheduled-string
  11838. " <" rpl
  11839. (format " -%dd"
  11840. (abs
  11841. (- (time-to-days
  11842. (save-match-data
  11843. (org-read-date nil t nil "Delay until" old-date-time)))
  11844. (time-to-days old-date-time))))
  11845. ">") t t))
  11846. (user-error "No scheduled information to update"))))
  11847. (t
  11848. (org-add-planning-info 'scheduled time 'closed)
  11849. (when (and old-date org-log-reschedule
  11850. (not (equal old-date
  11851. (substring org-last-inserted-timestamp 1 -1))))
  11852. (org-add-log-setup 'reschedule nil old-date 'findpos
  11853. org-log-reschedule))
  11854. (when repeater
  11855. (save-excursion
  11856. (org-back-to-heading t)
  11857. (when (re-search-forward (concat org-scheduled-string " "
  11858. org-last-inserted-timestamp)
  11859. (save-excursion
  11860. (outline-next-heading) (point)) t)
  11861. (goto-char (1- (match-end 0)))
  11862. (insert " " repeater)
  11863. (setq org-last-inserted-timestamp
  11864. (concat (substring org-last-inserted-timestamp 0 -1)
  11865. " " repeater
  11866. (substring org-last-inserted-timestamp -1))))))
  11867. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11868. (defun org-get-scheduled-time (pom &optional inherit)
  11869. "Get the scheduled time as a time tuple, of a format suitable
  11870. for calling org-schedule with, or if there is no scheduling,
  11871. returns nil."
  11872. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11873. (when time
  11874. (apply 'encode-time (org-parse-time-string time)))))
  11875. (defun org-get-deadline-time (pom &optional inherit)
  11876. "Get the deadline as a time tuple, of a format suitable for
  11877. calling org-deadline with, or if there is no scheduling, returns
  11878. nil."
  11879. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11880. (when time
  11881. (apply 'encode-time (org-parse-time-string time)))))
  11882. (defun org-remove-timestamp-with-keyword (keyword)
  11883. "Remove all time stamps with KEYWORD in the current entry."
  11884. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11885. beg)
  11886. (save-excursion
  11887. (org-back-to-heading t)
  11888. (setq beg (point))
  11889. (outline-next-heading)
  11890. (while (re-search-backward re beg t)
  11891. (replace-match "")
  11892. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11893. (equal (char-before) ?\ ))
  11894. (backward-delete-char 1)
  11895. (if (string-match "^[ \t]*$" (buffer-substring
  11896. (point-at-bol) (point-at-eol)))
  11897. (delete-region (point-at-bol)
  11898. (min (point-max) (1+ (point-at-eol))))))))))
  11899. (defun org-add-planning-info (what &optional time &rest remove)
  11900. "Insert new timestamp with keyword in the line directly after the headline.
  11901. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11902. If non is given, the user is prompted for a date.
  11903. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11904. be removed."
  11905. (interactive)
  11906. (let (org-time-was-given org-end-time-was-given ts
  11907. end default-time default-input)
  11908. (catch 'exit
  11909. (when (and (memq what '(scheduled deadline))
  11910. (or (not time)
  11911. (and (stringp time)
  11912. (string-match "^[-+]+[0-9]" time))))
  11913. ;; Try to get a default date/time from existing timestamp
  11914. (save-excursion
  11915. (org-back-to-heading t)
  11916. (setq end (save-excursion (outline-next-heading) (point)))
  11917. (when (re-search-forward (if (eq what 'scheduled)
  11918. org-scheduled-time-regexp
  11919. org-deadline-time-regexp)
  11920. end t)
  11921. (setq ts (match-string 1)
  11922. default-time
  11923. (apply 'encode-time (org-parse-time-string ts))
  11924. default-input (and ts (org-get-compact-tod ts))))))
  11925. (when what
  11926. (setq time
  11927. (if (stringp time)
  11928. ;; This is a string (relative or absolute), set proper date
  11929. (apply 'encode-time
  11930. (org-read-date-analyze
  11931. time default-time (decode-time default-time)))
  11932. ;; If necessary, get the time from the user
  11933. (or time (org-read-date nil 'to-time nil nil
  11934. default-time default-input)))))
  11935. (when (and org-insert-labeled-timestamps-at-point
  11936. (member what '(scheduled deadline)))
  11937. (insert
  11938. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11939. (org-insert-time-stamp time org-time-was-given
  11940. nil nil nil (list org-end-time-was-given))
  11941. (setq what nil))
  11942. (save-excursion
  11943. (save-restriction
  11944. (let (col list elt ts buffer-invisibility-spec)
  11945. (org-back-to-heading t)
  11946. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11947. (goto-char (match-end 1))
  11948. (setq col (current-column))
  11949. (goto-char (match-end 0))
  11950. (if (eobp) (insert "\n") (forward-char 1))
  11951. (when (and (not what)
  11952. (not (looking-at
  11953. (concat "[ \t]*"
  11954. org-keyword-time-not-clock-regexp))))
  11955. ;; Nothing to add, nothing to remove...... :-)
  11956. (throw 'exit nil))
  11957. (if (and (not (looking-at org-outline-regexp))
  11958. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11959. "[^\r\n]*"))
  11960. (not (equal (match-string 1) org-clock-string)))
  11961. (narrow-to-region (match-beginning 0) (match-end 0))
  11962. (insert-before-markers "\n")
  11963. (backward-char 1)
  11964. (narrow-to-region (point) (point))
  11965. (and org-adapt-indentation (org-indent-to-column col)))
  11966. ;; Check if we have to remove something.
  11967. (setq list (cons what remove))
  11968. (while list
  11969. (setq elt (pop list))
  11970. (when (or (and (eq elt 'scheduled)
  11971. (re-search-forward org-scheduled-time-regexp nil t))
  11972. (and (eq elt 'deadline)
  11973. (re-search-forward org-deadline-time-regexp nil t))
  11974. (and (eq elt 'closed)
  11975. (re-search-forward org-closed-time-regexp nil t)))
  11976. (replace-match "")
  11977. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11978. (and (looking-at "[ \t]+") (replace-match ""))
  11979. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11980. (when what
  11981. (insert
  11982. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11983. (cond ((eq what 'scheduled) org-scheduled-string)
  11984. ((eq what 'deadline) org-deadline-string)
  11985. ((eq what 'closed) org-closed-string))
  11986. " ")
  11987. (setq ts (org-insert-time-stamp
  11988. time
  11989. (or org-time-was-given
  11990. (and (eq what 'closed) org-log-done-with-time))
  11991. (eq what 'closed)
  11992. nil nil (list org-end-time-was-given)))
  11993. (insert
  11994. (if (not (or (bolp) (eq (char-before) ?\ )
  11995. (memq (char-after) '(32 10))
  11996. (eobp))) " " ""))
  11997. (end-of-line 1))
  11998. (goto-char (point-min))
  11999. (widen)
  12000. (if (and (looking-at "[ \t]*\n")
  12001. (equal (char-before) ?\n))
  12002. (delete-region (1- (point)) (point-at-eol)))
  12003. ts))))))
  12004. (defvar org-log-note-marker (make-marker))
  12005. (defvar org-log-note-purpose nil)
  12006. (defvar org-log-note-state nil)
  12007. (defvar org-log-note-previous-state nil)
  12008. (defvar org-log-note-how nil)
  12009. (defvar org-log-note-extra nil)
  12010. (defvar org-log-note-window-configuration nil)
  12011. (defvar org-log-note-return-to (make-marker))
  12012. (defvar org-log-note-effective-time nil
  12013. "Remembered current time so that dynamically scoped
  12014. `org-extend-today-until' affects tha timestamps in state change
  12015. log")
  12016. (defvar org-log-post-message nil
  12017. "Message to be displayed after a log note has been stored.
  12018. The auto-repeater uses this.")
  12019. (defun org-add-note ()
  12020. "Add a note to the current entry.
  12021. This is done in the same way as adding a state change note."
  12022. (interactive)
  12023. (org-add-log-setup 'note nil nil 'findpos nil))
  12024. (defvar org-property-end-re)
  12025. (defun org-add-log-setup (&optional purpose state prev-state
  12026. findpos how extra)
  12027. "Set up the post command hook to take a note.
  12028. If this is about to TODO state change, the new state is expected in STATE.
  12029. When FINDPOS is non-nil, find the correct position for the note in
  12030. the current entry. If not, assume that it can be inserted at point.
  12031. HOW is an indicator what kind of note should be created.
  12032. EXTRA is additional text that will be inserted into the notes buffer."
  12033. (let* ((org-log-into-drawer (org-log-into-drawer))
  12034. (drawer (cond ((stringp org-log-into-drawer)
  12035. org-log-into-drawer)
  12036. (org-log-into-drawer "LOGBOOK"))))
  12037. (save-restriction
  12038. (save-excursion
  12039. (when findpos
  12040. (org-back-to-heading t)
  12041. (narrow-to-region (point) (save-excursion
  12042. (outline-next-heading) (point)))
  12043. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12044. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12045. "[^\r\n]*\\)?"))
  12046. (goto-char (match-end 0))
  12047. (cond
  12048. (drawer
  12049. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12050. nil t)
  12051. (progn
  12052. (goto-char (match-end 0))
  12053. (or org-log-states-order-reversed
  12054. (and (re-search-forward org-property-end-re nil t)
  12055. (goto-char (1- (match-beginning 0))))))
  12056. (insert "\n:" drawer ":\n:END:")
  12057. (beginning-of-line 0)
  12058. (org-indent-line)
  12059. (beginning-of-line 2)
  12060. (org-indent-line)
  12061. (end-of-line 0)))
  12062. ((and org-log-state-notes-insert-after-drawers
  12063. (save-excursion
  12064. (forward-line) (looking-at org-drawer-regexp)))
  12065. (forward-line)
  12066. (while (looking-at org-drawer-regexp)
  12067. (goto-char (match-end 0))
  12068. (re-search-forward org-property-end-re (point-max) t)
  12069. (forward-line))
  12070. (forward-line -1)))
  12071. (unless org-log-states-order-reversed
  12072. (and (= (char-after) ?\n) (forward-char 1))
  12073. (org-skip-over-state-notes)
  12074. (skip-chars-backward " \t\n\r")))
  12075. (move-marker org-log-note-marker (point))
  12076. (setq org-log-note-purpose purpose
  12077. org-log-note-state state
  12078. org-log-note-previous-state prev-state
  12079. org-log-note-how how
  12080. org-log-note-extra extra
  12081. org-log-note-effective-time (org-current-effective-time))
  12082. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12083. (defun org-skip-over-state-notes ()
  12084. "Skip past the list of State notes in an entry."
  12085. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12086. (when (ignore-errors (goto-char (org-in-item-p)))
  12087. (let* ((struct (org-list-struct))
  12088. (prevs (org-list-prevs-alist struct)))
  12089. (while (looking-at "[ \t]*- State")
  12090. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12091. (org-list-get-item-end (point) struct)))))))
  12092. (defun org-add-log-note (&optional purpose)
  12093. "Pop up a window for taking a note, and add this note later at point."
  12094. (remove-hook 'post-command-hook 'org-add-log-note)
  12095. (setq org-log-note-window-configuration (current-window-configuration))
  12096. (delete-other-windows)
  12097. (move-marker org-log-note-return-to (point))
  12098. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12099. (goto-char org-log-note-marker)
  12100. (org-switch-to-buffer-other-window "*Org Note*")
  12101. (erase-buffer)
  12102. (if (memq org-log-note-how '(time state))
  12103. (let (current-prefix-arg) (org-store-log-note))
  12104. (let ((org-inhibit-startup t)) (org-mode))
  12105. (insert (format "# Insert note for %s.
  12106. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12107. (cond
  12108. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12109. ((eq org-log-note-purpose 'done) "closed todo item")
  12110. ((eq org-log-note-purpose 'state)
  12111. (format "state change from \"%s\" to \"%s\""
  12112. (or org-log-note-previous-state "")
  12113. (or org-log-note-state "")))
  12114. ((eq org-log-note-purpose 'reschedule)
  12115. "rescheduling")
  12116. ((eq org-log-note-purpose 'delschedule)
  12117. "no longer scheduled")
  12118. ((eq org-log-note-purpose 'redeadline)
  12119. "changing deadline")
  12120. ((eq org-log-note-purpose 'deldeadline)
  12121. "removing deadline")
  12122. ((eq org-log-note-purpose 'refile)
  12123. "refiling")
  12124. ((eq org-log-note-purpose 'note)
  12125. "this entry")
  12126. (t (error "This should not happen")))))
  12127. (if org-log-note-extra (insert org-log-note-extra))
  12128. (org-set-local 'org-finish-function 'org-store-log-note)
  12129. (run-hooks 'org-log-buffer-setup-hook)))
  12130. (defvar org-note-abort nil) ; dynamically scoped
  12131. (defun org-store-log-note ()
  12132. "Finish taking a log note, and insert it to where it belongs."
  12133. (let ((txt (buffer-string)))
  12134. (kill-buffer (current-buffer))
  12135. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12136. lines ind bul)
  12137. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12138. (setq txt (replace-match "" t t txt)))
  12139. (if (string-match "\\s-+\\'" txt)
  12140. (setq txt (replace-match "" t t txt)))
  12141. (setq lines (org-split-string txt "\n"))
  12142. (when (and note (string-match "\\S-" note))
  12143. (setq note
  12144. (org-replace-escapes
  12145. note
  12146. (list (cons "%u" (user-login-name))
  12147. (cons "%U" user-full-name)
  12148. (cons "%t" (format-time-string
  12149. (org-time-stamp-format 'long 'inactive)
  12150. org-log-note-effective-time))
  12151. (cons "%T" (format-time-string
  12152. (org-time-stamp-format 'long nil)
  12153. org-log-note-effective-time))
  12154. (cons "%d" (format-time-string
  12155. (org-time-stamp-format nil 'inactive)
  12156. org-log-note-effective-time))
  12157. (cons "%D" (format-time-string
  12158. (org-time-stamp-format nil nil)
  12159. org-log-note-effective-time))
  12160. (cons "%s" (if org-log-note-state
  12161. (concat "\"" org-log-note-state "\"")
  12162. ""))
  12163. (cons "%S" (if org-log-note-previous-state
  12164. (concat "\"" org-log-note-previous-state "\"")
  12165. "\"\"")))))
  12166. (if lines (setq note (concat note " \\\\")))
  12167. (push note lines))
  12168. (when (or current-prefix-arg org-note-abort)
  12169. (when org-log-into-drawer
  12170. (org-remove-empty-drawer-at org-log-note-marker))
  12171. (setq lines nil))
  12172. (when lines
  12173. (with-current-buffer (marker-buffer org-log-note-marker)
  12174. (save-excursion
  12175. (goto-char org-log-note-marker)
  12176. (move-marker org-log-note-marker nil)
  12177. (end-of-line 1)
  12178. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12179. (setq ind (save-excursion
  12180. (if (ignore-errors (goto-char (org-in-item-p)))
  12181. (let ((struct (org-list-struct)))
  12182. (org-list-get-ind
  12183. (org-list-get-top-point struct) struct))
  12184. (skip-chars-backward " \r\t\n")
  12185. (cond
  12186. ((and (org-at-heading-p)
  12187. org-adapt-indentation)
  12188. (1+ (org-current-level)))
  12189. ((org-at-heading-p) 0)
  12190. (t (org-get-indentation))))))
  12191. (setq bul (org-list-bullet-string "-"))
  12192. (org-indent-line-to ind)
  12193. (insert bul (pop lines))
  12194. (let ((ind-body (+ (length bul) ind)))
  12195. (while lines
  12196. (insert "\n")
  12197. (org-indent-line-to ind-body)
  12198. (insert (pop lines))))
  12199. (message "Note stored")
  12200. (org-back-to-heading t)
  12201. (org-cycle-hide-drawers 'children))
  12202. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12203. ;; from within `org-add-log-note' because `buffer-undo-list'
  12204. ;; is then modified outside of `org-with-remote-undo'.
  12205. (when (eq this-command 'org-agenda-todo)
  12206. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12207. ;; Don't add undo information when called from `org-agenda-todo'
  12208. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12209. (set-window-configuration org-log-note-window-configuration)
  12210. (with-current-buffer (marker-buffer org-log-note-return-to)
  12211. (goto-char org-log-note-return-to))
  12212. (move-marker org-log-note-return-to nil)
  12213. (and org-log-post-message (message "%s" org-log-post-message))))
  12214. (defun org-remove-empty-drawer-at (pos)
  12215. "Remove an empty drawer at position POS.
  12216. POS may also be a marker."
  12217. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12218. (org-with-wide-buffer
  12219. (goto-char pos)
  12220. (let ((drawer (org-element-at-point)))
  12221. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12222. (not (org-element-property :contents-begin drawer)))
  12223. (delete-region (org-element-property :begin drawer)
  12224. (progn (goto-char (org-element-property :end drawer))
  12225. (skip-chars-backward " \r\t\n")
  12226. (forward-line)
  12227. (point))))))))
  12228. (defvar org-ts-type nil)
  12229. (defun org-sparse-tree (&optional arg type)
  12230. "Create a sparse tree, prompt for the details.
  12231. This command can create sparse trees. You first need to select the type
  12232. of match used to create the tree:
  12233. t Show all TODO entries.
  12234. T Show entries with a specific TODO keyword.
  12235. m Show entries selected by a tags/property match.
  12236. p Enter a property name and its value (both with completion on existing
  12237. names/values) and show entries with that property.
  12238. r Show entries matching a regular expression (`/' can be used as well).
  12239. b Show deadlines and scheduled items before a date.
  12240. a Show deadlines and scheduled items after a date.
  12241. d Show deadlines due within `org-deadline-warning-days'.
  12242. D Show deadlines and scheduled items between a date range."
  12243. (interactive "P")
  12244. (let (ans kwd value ts-type)
  12245. (setq type (or type org-sparse-tree-default-date-type))
  12246. (setq org-ts-type type)
  12247. (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"
  12248. (cond ((eq type 'all) "all timestamps")
  12249. ((eq type 'scheduled) "only scheduled")
  12250. ((eq type 'deadline) "only deadline")
  12251. ((eq type 'active) "only active timestamps")
  12252. ((eq type 'inactive) "only inactive timestamps")
  12253. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12254. ((eq type 'closed) "with a closed time-stamp")
  12255. (t "scheduled/deadline")))
  12256. (setq ans (read-char-exclusive))
  12257. (cond
  12258. ((equal ans ?c)
  12259. (org-sparse-tree
  12260. arg (cadr (member type '(scheduled-or-deadline
  12261. all scheduled deadline active inactive closed)))))
  12262. ((equal ans ?d)
  12263. (call-interactively 'org-check-deadlines))
  12264. ((equal ans ?b)
  12265. (call-interactively 'org-check-before-date))
  12266. ((equal ans ?a)
  12267. (call-interactively 'org-check-after-date))
  12268. ((equal ans ?D)
  12269. (call-interactively 'org-check-dates-range))
  12270. ((equal ans ?t)
  12271. (call-interactively 'org-show-todo-tree))
  12272. ((equal ans ?T)
  12273. (org-show-todo-tree '(4)))
  12274. ((member ans '(?T ?m))
  12275. (call-interactively 'org-match-sparse-tree))
  12276. ((member ans '(?p ?P))
  12277. (setq kwd (org-icompleting-read "Property: "
  12278. (mapcar 'list (org-buffer-property-keys))))
  12279. (setq value (org-icompleting-read "Value: "
  12280. (mapcar 'list (org-property-values kwd))))
  12281. (unless (string-match "\\`{.*}\\'" value)
  12282. (setq value (concat "\"" value "\"")))
  12283. (org-match-sparse-tree arg (concat kwd "=" value)))
  12284. ((member ans '(?r ?R ?/))
  12285. (call-interactively 'org-occur))
  12286. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12287. (defvar org-occur-highlights nil
  12288. "List of overlays used for occur matches.")
  12289. (make-variable-buffer-local 'org-occur-highlights)
  12290. (defvar org-occur-parameters nil
  12291. "Parameters of the active org-occur calls.
  12292. This is a list, each call to org-occur pushes as cons cell,
  12293. containing the regular expression and the callback, onto the list.
  12294. The list can contain several entries if `org-occur' has been called
  12295. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12296. will only contain one set of parameters. When the highlights are
  12297. removed (for example with `C-c C-c', or with the next edit (depending
  12298. on `org-remove-highlights-with-change'), this variable is emptied
  12299. as well.")
  12300. (make-variable-buffer-local 'org-occur-parameters)
  12301. (defun org-occur (regexp &optional keep-previous callback)
  12302. "Make a compact tree which shows all matches of REGEXP.
  12303. The tree will show the lines where the regexp matches, and all higher
  12304. headlines above the match. It will also show the heading after the match,
  12305. to make sure editing the matching entry is easy.
  12306. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12307. call to `org-occur' will be kept, to allow stacking of calls to this
  12308. command.
  12309. If CALLBACK is non-nil, it is a function which is called to confirm
  12310. that the match should indeed be shown."
  12311. (interactive "sRegexp: \nP")
  12312. (when (equal regexp "")
  12313. (user-error "Regexp cannot be empty"))
  12314. (unless keep-previous
  12315. (org-remove-occur-highlights nil nil t))
  12316. (push (cons regexp callback) org-occur-parameters)
  12317. (let ((cnt 0))
  12318. (save-excursion
  12319. (goto-char (point-min))
  12320. (if (or (not keep-previous) ; do not want to keep
  12321. (not org-occur-highlights)) ; no previous matches
  12322. ;; hide everything
  12323. (org-overview))
  12324. (while (re-search-forward regexp nil t)
  12325. (when (or (not callback)
  12326. (save-match-data (funcall callback)))
  12327. (setq cnt (1+ cnt))
  12328. (when org-highlight-sparse-tree-matches
  12329. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12330. (org-show-context 'occur-tree))))
  12331. (when org-remove-highlights-with-change
  12332. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12333. nil 'local))
  12334. (unless org-sparse-tree-open-archived-trees
  12335. (org-hide-archived-subtrees (point-min) (point-max)))
  12336. (run-hooks 'org-occur-hook)
  12337. (if (org-called-interactively-p 'interactive)
  12338. (message "%d match(es) for regexp %s" cnt regexp))
  12339. cnt))
  12340. (defun org-occur-next-match (&optional n reset)
  12341. "Function for `next-error-function' to find sparse tree matches.
  12342. N is the number of matches to move, when negative move backwards.
  12343. RESET is entirely ignored - this function always goes back to the
  12344. starting point when no match is found."
  12345. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12346. (search-func (if (< n 0)
  12347. 'previous-single-char-property-change
  12348. 'next-single-char-property-change))
  12349. (n (abs n))
  12350. (pos (point))
  12351. p1)
  12352. (catch 'exit
  12353. (while (setq p1 (funcall search-func (point) 'org-type))
  12354. (when (equal p1 limit)
  12355. (goto-char pos)
  12356. (error "No more matches"))
  12357. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12358. (setq n (1- n))
  12359. (when (= n 0)
  12360. (goto-char p1)
  12361. (throw 'exit (point))))
  12362. (goto-char p1))
  12363. (goto-char p1)
  12364. (error "No more matches"))))
  12365. (defun org-show-context (&optional key)
  12366. "Make sure point and context are visible.
  12367. How much context is shown depends upon the variables
  12368. `org-show-hierarchy-above', `org-show-following-heading',
  12369. `org-show-entry-below' and `org-show-siblings'."
  12370. (let ((heading-p (org-at-heading-p t))
  12371. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12372. (following-p (org-get-alist-option org-show-following-heading key))
  12373. (entry-p (org-get-alist-option org-show-entry-below key))
  12374. (siblings-p (org-get-alist-option org-show-siblings key)))
  12375. ;; Show heading or entry text
  12376. (if (and heading-p (not entry-p))
  12377. (org-flag-heading nil) ; only show the heading
  12378. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12379. (org-show-hidden-entry))) ; show entire entry
  12380. (when following-p
  12381. ;; Show next sibling, or heading below text
  12382. (save-excursion
  12383. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12384. (org-flag-heading nil))))
  12385. (when siblings-p (org-show-siblings))
  12386. (when hierarchy-p
  12387. ;; show all higher headings, possibly with siblings
  12388. (save-excursion
  12389. (while (and (condition-case nil
  12390. (progn (org-up-heading-all 1) t)
  12391. (error nil))
  12392. (not (bobp)))
  12393. (org-flag-heading nil)
  12394. (when siblings-p (org-show-siblings)))))
  12395. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12396. (defvar org-reveal-start-hook nil
  12397. "Hook run before revealing a location.")
  12398. (defun org-reveal (&optional siblings)
  12399. "Show current entry, hierarchy above it, and the following headline.
  12400. This can be used to show a consistent set of context around locations
  12401. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12402. not t for the search context.
  12403. With optional argument SIBLINGS, on each level of the hierarchy all
  12404. siblings are shown. This repairs the tree structure to what it would
  12405. look like when opened with hierarchical calls to `org-cycle'.
  12406. With double optional argument \\[universal-argument] \\[universal-argument], \
  12407. go to the parent and show the
  12408. entire tree."
  12409. (interactive "P")
  12410. (run-hooks 'org-reveal-start-hook)
  12411. (let ((org-show-hierarchy-above t)
  12412. (org-show-following-heading t)
  12413. (org-show-siblings (if siblings t org-show-siblings)))
  12414. (org-show-context nil))
  12415. (when (equal siblings '(16))
  12416. (save-excursion
  12417. (when (org-up-heading-safe)
  12418. (org-show-subtree)
  12419. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12420. (defun org-highlight-new-match (beg end)
  12421. "Highlight from BEG to END and mark the highlight is an occur headline."
  12422. (let ((ov (make-overlay beg end)))
  12423. (overlay-put ov 'face 'secondary-selection)
  12424. (overlay-put ov 'org-type 'org-occur)
  12425. (push ov org-occur-highlights)))
  12426. (defun org-remove-occur-highlights (&optional beg end noremove)
  12427. "Remove the occur highlights from the buffer.
  12428. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12429. from the `before-change-functions' in the current buffer."
  12430. (interactive)
  12431. (unless org-inhibit-highlight-removal
  12432. (mapc 'delete-overlay org-occur-highlights)
  12433. (setq org-occur-highlights nil)
  12434. (setq org-occur-parameters nil)
  12435. (unless noremove
  12436. (remove-hook 'before-change-functions
  12437. 'org-remove-occur-highlights 'local))))
  12438. ;;;; Priorities
  12439. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12440. "Regular expression matching the priority indicator.")
  12441. (defvar org-remove-priority-next-time nil)
  12442. (defun org-priority-up ()
  12443. "Increase the priority of the current item."
  12444. (interactive)
  12445. (org-priority 'up))
  12446. (defun org-priority-down ()
  12447. "Decrease the priority of the current item."
  12448. (interactive)
  12449. (org-priority 'down))
  12450. (defun org-priority (&optional action show)
  12451. "Change the priority of an item.
  12452. ACTION can be `set', `up', `down', or a character."
  12453. (interactive "P")
  12454. (if (equal action '(4))
  12455. (org-show-priority)
  12456. (unless org-enable-priority-commands
  12457. (user-error "Priority commands are disabled"))
  12458. (setq action (or action 'set))
  12459. (let (current new news have remove)
  12460. (save-excursion
  12461. (org-back-to-heading t)
  12462. (if (looking-at org-priority-regexp)
  12463. (setq current (string-to-char (match-string 2))
  12464. have t))
  12465. (cond
  12466. ((eq action 'remove)
  12467. (setq remove t new ?\ ))
  12468. ((or (eq action 'set)
  12469. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12470. (if (not (eq action 'set))
  12471. (setq new action)
  12472. (message "Priority %c-%c, SPC to remove: "
  12473. org-highest-priority org-lowest-priority)
  12474. (save-match-data
  12475. (setq new (read-char-exclusive))))
  12476. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12477. (= (upcase org-lowest-priority) org-lowest-priority))
  12478. (setq new (upcase new)))
  12479. (cond ((equal new ?\ ) (setq remove t))
  12480. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12481. (user-error "Priority must be between `%c' and `%c'"
  12482. org-highest-priority org-lowest-priority))))
  12483. ((eq action 'up)
  12484. (setq new (if have
  12485. (1- current) ; normal cycling
  12486. ;; last priority was empty
  12487. (if (eq last-command this-command)
  12488. org-lowest-priority ; wrap around empty to lowest
  12489. ;; default
  12490. (if org-priority-start-cycle-with-default
  12491. org-default-priority
  12492. (1- org-default-priority))))))
  12493. ((eq action 'down)
  12494. (setq new (if have
  12495. (1+ current) ; normal cycling
  12496. ;; last priority was empty
  12497. (if (eq last-command this-command)
  12498. org-highest-priority ; wrap around empty to highest
  12499. ;; default
  12500. (if org-priority-start-cycle-with-default
  12501. org-default-priority
  12502. (1+ org-default-priority))))))
  12503. (t (user-error "Invalid action")))
  12504. (if (or (< (upcase new) org-highest-priority)
  12505. (> (upcase new) org-lowest-priority))
  12506. (if (and (memq action '(up down))
  12507. (not have) (not (eq last-command this-command)))
  12508. ;; `new' is from default priority
  12509. (error
  12510. "The default can not be set, see `org-default-priority' why")
  12511. ;; normal cycling: `new' is beyond highest/lowest priority
  12512. ;; and is wrapped around to the empty priority
  12513. (setq remove t)))
  12514. (setq news (format "%c" new))
  12515. (if have
  12516. (if remove
  12517. (replace-match "" t t nil 1)
  12518. (replace-match news t t nil 2))
  12519. (if remove
  12520. (user-error "No priority cookie found in line")
  12521. (let ((case-fold-search nil))
  12522. (looking-at org-todo-line-regexp))
  12523. (if (match-end 2)
  12524. (progn
  12525. (goto-char (match-end 2))
  12526. (insert " [#" news "]"))
  12527. (goto-char (match-beginning 3))
  12528. (insert "[#" news "] "))))
  12529. (org-preserve-lc (org-set-tags nil 'align)))
  12530. (if remove
  12531. (message "Priority removed")
  12532. (message "Priority of current item set to %s" news)))))
  12533. (defun org-show-priority ()
  12534. "Show the priority of the current item.
  12535. This priority is composed of the main priority given with the [#A] cookies,
  12536. and by additional input from the age of a schedules or deadline entry."
  12537. (interactive)
  12538. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12539. (org-get-at-bol 'priority)
  12540. (save-excursion
  12541. (save-match-data
  12542. (beginning-of-line)
  12543. (and (looking-at org-heading-regexp)
  12544. (org-get-priority (match-string 0))))))))
  12545. (message "Priority is %d" (if pri pri -1000))))
  12546. (defun org-get-priority (s)
  12547. "Find priority cookie and return priority."
  12548. (save-match-data
  12549. (if (functionp org-get-priority-function)
  12550. (funcall org-get-priority-function)
  12551. (if (not (string-match org-priority-regexp s))
  12552. (* 1000 (- org-lowest-priority org-default-priority))
  12553. (* 1000 (- org-lowest-priority
  12554. (string-to-char (match-string 2 s))))))))
  12555. ;;;; Tags
  12556. (defvar org-agenda-archives-mode)
  12557. (defvar org-map-continue-from nil
  12558. "Position from where mapping should continue.
  12559. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12560. (defvar org-scanner-tags nil
  12561. "The current tag list while the tags scanner is running.")
  12562. (defvar org-trust-scanner-tags nil
  12563. "Should `org-get-tags-at' use the tags for the scanner.
  12564. This is for internal dynamical scoping only.
  12565. When this is non-nil, the function `org-get-tags-at' will return the value
  12566. of `org-scanner-tags' instead of building the list by itself. This
  12567. can lead to large speed-ups when the tags scanner is used in a file with
  12568. many entries, and when the list of tags is retrieved, for example to
  12569. obtain a list of properties. Building the tags list for each entry in such
  12570. a file becomes an N^2 operation - but with this variable set, it scales
  12571. as N.")
  12572. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12573. "Sca headline tags with inheritance and produce output ACTION.
  12574. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12575. or `agenda' to produce an entry list for an agenda view. It can also be
  12576. a Lisp form or a function that should be called at each matched headline, in
  12577. this case the return value is a list of all return values from these calls.
  12578. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12579. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12580. only lines with a not-done TODO keyword are included in the output.
  12581. This should be the same variable that was scoped into
  12582. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12583. START-LEVEL can be a string with asterisks, reducing the scope to
  12584. headlines matching this string."
  12585. (require 'org-agenda)
  12586. (let* ((re (concat "^"
  12587. (if start-level
  12588. ;; Get the correct level to match
  12589. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12590. org-outline-regexp)
  12591. " *\\(\\<\\("
  12592. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12593. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12594. (props (list 'face 'default
  12595. 'done-face 'org-agenda-done
  12596. 'undone-face 'default
  12597. 'mouse-face 'highlight
  12598. 'org-not-done-regexp org-not-done-regexp
  12599. 'org-todo-regexp org-todo-regexp
  12600. 'org-complex-heading-regexp org-complex-heading-regexp
  12601. 'help-echo
  12602. (format "mouse-2 or RET jump to org file %s"
  12603. (abbreviate-file-name
  12604. (or (buffer-file-name (buffer-base-buffer))
  12605. (buffer-name (buffer-base-buffer)))))))
  12606. (org-map-continue-from nil)
  12607. lspos tags tags-list
  12608. (tags-alist (list (cons 0 org-file-tags)))
  12609. (llast 0) rtn rtn1 level category i txt
  12610. todo marker entry priority)
  12611. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12612. (setq action (list 'lambda nil action)))
  12613. (save-excursion
  12614. (goto-char (point-min))
  12615. (when (eq action 'sparse-tree)
  12616. (org-overview)
  12617. (org-remove-occur-highlights))
  12618. (while (let (case-fold-search)
  12619. (re-search-forward re nil t))
  12620. (setq org-map-continue-from nil)
  12621. (catch :skip
  12622. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12623. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12624. (goto-char (setq lspos (match-beginning 0)))
  12625. (setq level (org-reduced-level (org-outline-level))
  12626. category (org-get-category))
  12627. (setq i llast llast level)
  12628. ;; remove tag lists from same and sublevels
  12629. (while (>= i level)
  12630. (when (setq entry (assoc i tags-alist))
  12631. (setq tags-alist (delete entry tags-alist)))
  12632. (setq i (1- i)))
  12633. ;; add the next tags
  12634. (when tags
  12635. (setq tags (org-split-string tags ":")
  12636. tags-alist
  12637. (cons (cons level tags) tags-alist)))
  12638. ;; compile tags for current headline
  12639. (setq tags-list
  12640. (if org-use-tag-inheritance
  12641. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12642. tags)
  12643. org-scanner-tags tags-list)
  12644. (when org-use-tag-inheritance
  12645. (setcdr (car tags-alist)
  12646. (mapcar (lambda (x)
  12647. (setq x (copy-sequence x))
  12648. (org-add-prop-inherited x))
  12649. (cdar tags-alist))))
  12650. (when (and tags org-use-tag-inheritance
  12651. (or (not (eq t org-use-tag-inheritance))
  12652. org-tags-exclude-from-inheritance))
  12653. ;; selective inheritance, remove uninherited ones
  12654. (setcdr (car tags-alist)
  12655. (org-remove-uninherited-tags (cdar tags-alist))))
  12656. (when (and
  12657. ;; eval matcher only when the todo condition is OK
  12658. (and (or (not todo-only) (member todo org-not-done-keywords))
  12659. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12660. (eval matcher)))
  12661. ;; Call the skipper, but return t if it does not skip,
  12662. ;; so that the `and' form continues evaluating
  12663. (progn
  12664. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12665. t)
  12666. ;; Check if timestamps are deselecting this entry
  12667. (or (not todo-only)
  12668. (and (member todo org-not-done-keywords)
  12669. (or (not org-agenda-tags-todo-honor-ignore-options)
  12670. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12671. ;; select this headline
  12672. (cond
  12673. ((eq action 'sparse-tree)
  12674. (and org-highlight-sparse-tree-matches
  12675. (org-get-heading) (match-end 0)
  12676. (org-highlight-new-match
  12677. (match-beginning 1) (match-end 1)))
  12678. (org-show-context 'tags-tree))
  12679. ((eq action 'agenda)
  12680. (setq txt (org-agenda-format-item
  12681. ""
  12682. (concat
  12683. (if (eq org-tags-match-list-sublevels 'indented)
  12684. (make-string (1- level) ?.) "")
  12685. (org-get-heading))
  12686. level category
  12687. tags-list)
  12688. priority (org-get-priority txt))
  12689. (goto-char lspos)
  12690. (setq marker (org-agenda-new-marker))
  12691. (org-add-props txt props
  12692. 'org-marker marker 'org-hd-marker marker 'org-category category
  12693. 'todo-state todo
  12694. 'priority priority 'type "tagsmatch")
  12695. (push txt rtn))
  12696. ((functionp action)
  12697. (setq org-map-continue-from nil)
  12698. (save-excursion
  12699. (setq rtn1 (funcall action))
  12700. (push rtn1 rtn)))
  12701. (t (user-error "Invalid action")))
  12702. ;; if we are to skip sublevels, jump to end of subtree
  12703. (unless org-tags-match-list-sublevels
  12704. (org-end-of-subtree t)
  12705. (backward-char 1))))
  12706. ;; Get the correct position from where to continue
  12707. (if org-map-continue-from
  12708. (goto-char org-map-continue-from)
  12709. (and (= (point) lspos) (end-of-line 1)))))
  12710. (when (and (eq action 'sparse-tree)
  12711. (not org-sparse-tree-open-archived-trees))
  12712. (org-hide-archived-subtrees (point-min) (point-max)))
  12713. (nreverse rtn)))
  12714. (defun org-remove-uninherited-tags (tags)
  12715. "Remove all tags that are not inherited from the list TAGS."
  12716. (cond
  12717. ((eq org-use-tag-inheritance t)
  12718. (if org-tags-exclude-from-inheritance
  12719. (org-delete-all org-tags-exclude-from-inheritance tags)
  12720. tags))
  12721. ((not org-use-tag-inheritance) nil)
  12722. ((stringp org-use-tag-inheritance)
  12723. (delq nil (mapcar
  12724. (lambda (x)
  12725. (if (and (string-match org-use-tag-inheritance x)
  12726. (not (member x org-tags-exclude-from-inheritance)))
  12727. x nil))
  12728. tags)))
  12729. ((listp org-use-tag-inheritance)
  12730. (delq nil (mapcar
  12731. (lambda (x)
  12732. (if (member x org-use-tag-inheritance) x nil))
  12733. tags)))))
  12734. (defun org-match-sparse-tree (&optional todo-only match)
  12735. "Create a sparse tree according to tags string MATCH.
  12736. MATCH can contain positive and negative selection of tags, like
  12737. \"+WORK+URGENT-WITHBOSS\".
  12738. If optional argument TODO-ONLY is non-nil, only select lines that are
  12739. also TODO lines."
  12740. (interactive "P")
  12741. (org-agenda-prepare-buffers (list (current-buffer)))
  12742. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12743. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12744. (defvar org-cached-props nil)
  12745. (defun org-cached-entry-get (pom property)
  12746. (if (or (eq t org-use-property-inheritance)
  12747. (and (stringp org-use-property-inheritance)
  12748. (string-match org-use-property-inheritance property))
  12749. (and (listp org-use-property-inheritance)
  12750. (member property org-use-property-inheritance)))
  12751. ;; Caching is not possible, check it directly
  12752. (org-entry-get pom property 'inherit)
  12753. ;; Get all properties, so that we can do complicated checks easily
  12754. (cdr (assoc property (or org-cached-props
  12755. (setq org-cached-props
  12756. (org-entry-properties pom)))))))
  12757. (defun org-global-tags-completion-table (&optional files)
  12758. "Return the list of all tags in all agenda buffer/files.
  12759. Optional FILES argument is a list of files which can be used
  12760. instead of the agenda files."
  12761. (save-excursion
  12762. (org-uniquify
  12763. (delq nil
  12764. (apply 'append
  12765. (mapcar
  12766. (lambda (file)
  12767. (set-buffer (find-file-noselect file))
  12768. (append (org-get-buffer-tags)
  12769. (mapcar (lambda (x) (if (stringp (car-safe x))
  12770. (list (car-safe x)) nil))
  12771. org-tag-alist)))
  12772. (if (and files (car files))
  12773. files
  12774. (org-agenda-files))))))))
  12775. (defun org-make-tags-matcher (match)
  12776. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12777. The variable `todo-only' is scoped dynamically into this function.
  12778. It will be set to t if the matcher restricts matching to TODO entries,
  12779. otherwise will not be touched.
  12780. Returns a cons of the selection string MATCH and the constructed
  12781. lisp form implementing the matcher. The matcher is to be evaluated
  12782. at an Org entry, with point on the headline, and returns t if the
  12783. entry matches the selection string MATCH. The returned lisp form
  12784. references two variables with information about the entry, which
  12785. must be bound around the form's evaluation: todo, the TODO keyword
  12786. at the entry (or nil of none); and tags-list, the list of all tags
  12787. at the entry including inherited ones. Additionally, the category
  12788. of the entry (if any) must be specified as the text property
  12789. 'org-category on the headline.
  12790. See also `org-scan-tags'.
  12791. "
  12792. (declare (special todo-only))
  12793. (unless (boundp 'todo-only)
  12794. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12795. (unless match
  12796. ;; Get a new match request, with completion against the global
  12797. ;; tags table and the local tags in current buffer
  12798. (let ((org-last-tags-completion-table
  12799. (org-uniquify
  12800. (delq nil (append (org-get-buffer-tags)
  12801. (org-global-tags-completion-table))))))
  12802. (setq match (org-completing-read-no-i
  12803. "Match: " 'org-tags-completion-function nil nil nil
  12804. 'org-tags-history))))
  12805. ;; Parse the string and create a lisp form
  12806. (let ((match0 match)
  12807. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12808. minus tag mm
  12809. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12810. orterms term orlist re-p str-p level-p level-op time-p
  12811. prop-p pn pv po gv rest (start 0) (ss 0))
  12812. ;; Expand group tags
  12813. (setq match (org-tags-expand match))
  12814. ;; Check if there is a TODO part of this match, which would be the
  12815. ;; part after a "/". TO make sure that this slash is not part of
  12816. ;; a property value to be matched against, we also check that there
  12817. ;; is no " after that slash.
  12818. ;; First, find the last slash
  12819. (while (string-match "/+" match ss)
  12820. (setq start (match-beginning 0) ss (match-end 0)))
  12821. (if (and (string-match "/+" match start)
  12822. (not (save-match-data (string-match "\"" match start))))
  12823. ;; match contains also a todo-matching request
  12824. (progn
  12825. (setq tagsmatch (substring match 0 (match-beginning 0))
  12826. todomatch (substring match (match-end 0)))
  12827. (if (string-match "^!" todomatch)
  12828. (setq todo-only t todomatch (substring todomatch 1)))
  12829. (if (string-match "^\\s-*$" todomatch)
  12830. (setq todomatch nil)))
  12831. ;; only matching tags
  12832. (setq tagsmatch match todomatch nil))
  12833. ;; Make the tags matcher
  12834. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12835. (setq tagsmatcher t)
  12836. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12837. (while (setq term (pop orterms))
  12838. (while (and (equal (substring term -1) "\\") orterms)
  12839. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12840. (while (string-match re term)
  12841. (setq rest (substring term (match-end 0))
  12842. minus (and (match-end 1)
  12843. (equal (match-string 1 term) "-"))
  12844. tag (save-match-data (replace-regexp-in-string
  12845. "\\\\-" "-"
  12846. (match-string 2 term)))
  12847. re-p (equal (string-to-char tag) ?{)
  12848. level-p (match-end 4)
  12849. prop-p (match-end 5)
  12850. mm (cond
  12851. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12852. (level-p
  12853. (setq level-op (org-op-to-function (match-string 3 term)))
  12854. `(,level-op level ,(string-to-number
  12855. (match-string 4 term))))
  12856. (prop-p
  12857. (setq pn (match-string 5 term)
  12858. po (match-string 6 term)
  12859. pv (match-string 7 term)
  12860. re-p (equal (string-to-char pv) ?{)
  12861. str-p (equal (string-to-char pv) ?\")
  12862. time-p (save-match-data
  12863. (string-match "^\"[[<].*[]>]\"$" pv))
  12864. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12865. (if time-p (setq pv (org-matcher-time pv)))
  12866. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12867. (cond
  12868. ((equal pn "CATEGORY")
  12869. (setq gv '(get-text-property (point) 'org-category)))
  12870. ((equal pn "TODO")
  12871. (setq gv 'todo))
  12872. (t
  12873. (setq gv `(org-cached-entry-get nil ,pn))))
  12874. (if re-p
  12875. (if (eq po 'org<>)
  12876. `(not (string-match ,pv (or ,gv "")))
  12877. `(string-match ,pv (or ,gv "")))
  12878. (if str-p
  12879. `(,po (or ,gv "") ,pv)
  12880. `(,po (string-to-number (or ,gv ""))
  12881. ,(string-to-number pv) ))))
  12882. (t `(member ,tag tags-list)))
  12883. mm (if minus (list 'not mm) mm)
  12884. term rest)
  12885. (push mm tagsmatcher))
  12886. (push (if (> (length tagsmatcher) 1)
  12887. (cons 'and tagsmatcher)
  12888. (car tagsmatcher))
  12889. orlist)
  12890. (setq tagsmatcher nil))
  12891. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12892. (setq tagsmatcher
  12893. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12894. ;; Make the todo matcher
  12895. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12896. (setq todomatcher t)
  12897. (setq orterms (org-split-string todomatch "|") orlist nil)
  12898. (while (setq term (pop orterms))
  12899. (while (string-match re term)
  12900. (setq minus (and (match-end 1)
  12901. (equal (match-string 1 term) "-"))
  12902. kwd (match-string 2 term)
  12903. re-p (equal (string-to-char kwd) ?{)
  12904. term (substring term (match-end 0))
  12905. mm (if re-p
  12906. `(string-match ,(substring kwd 1 -1) todo)
  12907. (list 'equal 'todo kwd))
  12908. mm (if minus (list 'not mm) mm))
  12909. (push mm todomatcher))
  12910. (push (if (> (length todomatcher) 1)
  12911. (cons 'and todomatcher)
  12912. (car todomatcher))
  12913. orlist)
  12914. (setq todomatcher nil))
  12915. (setq todomatcher (if (> (length orlist) 1)
  12916. (cons 'or orlist) (car orlist))))
  12917. ;; Return the string and lisp forms of the matcher
  12918. (setq matcher (if todomatcher
  12919. (list 'and tagsmatcher todomatcher)
  12920. tagsmatcher))
  12921. (when todo-only
  12922. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12923. matcher)))
  12924. (cons match0 matcher)))
  12925. (defun org-tags-expand (match &optional single-as-list downcased)
  12926. "Expand group tags in MATCH.
  12927. This replaces every group tag in MATCH with a regexp tag search.
  12928. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12929. will be replaced like this:
  12930. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12931. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12932. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12933. Replacing by a regexp preserves the structure of the match.
  12934. E.g., this expansion
  12935. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12936. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12937. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12938. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12939. assumed to be a single group tag, and the function will return
  12940. the list of tags in this group.
  12941. When DOWNCASE is non-nil, expand downcased TAGS."
  12942. (if org-group-tags
  12943. (let* ((case-fold-search t)
  12944. (stable org-mode-syntax-table)
  12945. (tal (or org-tag-groups-alist-for-agenda
  12946. org-tag-groups-alist))
  12947. (tal (if downcased
  12948. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12949. (tml (mapcar 'car tal))
  12950. (rtnmatch match) rpl)
  12951. ;; @ and _ are allowed as word-components in tags
  12952. (modify-syntax-entry ?@ "w" stable)
  12953. (modify-syntax-entry ?_ "w" stable)
  12954. (while (and tml
  12955. (with-syntax-table stable
  12956. (string-match
  12957. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12958. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12959. (let* ((dir (match-string 1 rtnmatch))
  12960. (tag (match-string 2 rtnmatch))
  12961. (tag (if downcased (downcase tag) tag)))
  12962. (setq tml (delete tag tml))
  12963. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12964. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12965. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12966. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12967. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12968. (if single-as-list
  12969. (or (reverse rpl) (list rtnmatch))
  12970. rtnmatch))
  12971. (if single-as-list (list (if downcased (downcase match) match))
  12972. match)))
  12973. (defun org-op-to-function (op &optional stringp)
  12974. "Turn an operator into the appropriate function."
  12975. (setq op
  12976. (cond
  12977. ((equal op "<" ) '(< string< org-time<))
  12978. ((equal op ">" ) '(> org-string> org-time>))
  12979. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12980. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12981. ((member op '("=" "==")) '(= string= org-time=))
  12982. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12983. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12984. (defun org<> (a b) (not (= a b)))
  12985. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12986. (defun org-string>= (a b) (not (string< a b)))
  12987. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12988. (defun org-string<> (a b) (not (string= a b)))
  12989. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12990. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12991. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12992. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12993. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12994. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12995. (defun org-2ft (s)
  12996. "Convert S to a floating point time.
  12997. If S is already a number, just return it. If it is a string, parse
  12998. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12999. (cond
  13000. ((numberp s) s)
  13001. ((stringp s)
  13002. (condition-case nil
  13003. (float-time (apply 'encode-time (org-parse-time-string s)))
  13004. (error 0.)))
  13005. (t 0.)))
  13006. (defun org-time-today ()
  13007. "Time in seconds today at 0:00.
  13008. Returns the float number of seconds since the beginning of the
  13009. epoch to the beginning of today (00:00)."
  13010. (float-time (apply 'encode-time
  13011. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13012. (defun org-matcher-time (s)
  13013. "Interpret a time comparison value."
  13014. (save-match-data
  13015. (cond
  13016. ((string= s "<now>") (float-time))
  13017. ((string= s "<today>") (org-time-today))
  13018. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13019. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13020. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13021. (+ (org-time-today)
  13022. (* (string-to-number (match-string 1 s))
  13023. (cdr (assoc (match-string 2 s)
  13024. '(("d" . 86400.0) ("w" . 604800.0)
  13025. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13026. (t (org-2ft s)))))
  13027. (defun org-match-any-p (re list)
  13028. "Does re match any element of list?"
  13029. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13030. (delq nil list))
  13031. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13032. (defvar org-tags-overlay (make-overlay 1 1))
  13033. (org-detach-overlay org-tags-overlay)
  13034. (defun org-get-local-tags-at (&optional pos)
  13035. "Get a list of tags defined in the current headline."
  13036. (org-get-tags-at pos 'local))
  13037. (defun org-get-local-tags ()
  13038. "Get a list of tags defined in the current headline."
  13039. (org-get-tags-at nil 'local))
  13040. (defun org-get-tags-at (&optional pos local)
  13041. "Get a list of all headline tags applicable at POS.
  13042. POS defaults to point. If tags are inherited, the list contains
  13043. the targets in the same sequence as the headlines appear, i.e.
  13044. the tags of the current headline come last.
  13045. When LOCAL is non-nil, only return tags from the current headline,
  13046. ignore inherited ones."
  13047. (interactive)
  13048. (if (and org-trust-scanner-tags
  13049. (or (not pos) (equal pos (point)))
  13050. (not local))
  13051. org-scanner-tags
  13052. (let (tags ltags lastpos parent)
  13053. (save-excursion
  13054. (save-restriction
  13055. (widen)
  13056. (goto-char (or pos (point)))
  13057. (save-match-data
  13058. (catch 'done
  13059. (condition-case nil
  13060. (progn
  13061. (org-back-to-heading t)
  13062. (while (not (equal lastpos (point)))
  13063. (setq lastpos (point))
  13064. (when (looking-at
  13065. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13066. (setq ltags (org-split-string
  13067. (org-match-string-no-properties 1) ":"))
  13068. (when parent
  13069. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13070. (setq tags (append
  13071. (if parent
  13072. (org-remove-uninherited-tags ltags)
  13073. ltags)
  13074. tags)))
  13075. (or org-use-tag-inheritance (throw 'done t))
  13076. (if local (throw 'done t))
  13077. (or (org-up-heading-safe) (error nil))
  13078. (setq parent t)))
  13079. (error nil)))))
  13080. (if local
  13081. tags
  13082. (reverse (delete-dups
  13083. (reverse (append
  13084. (org-remove-uninherited-tags
  13085. org-file-tags) tags)))))))))
  13086. (defun org-add-prop-inherited (s)
  13087. (add-text-properties 0 (length s) '(inherited t) s)
  13088. s)
  13089. (defun org-toggle-tag (tag &optional onoff)
  13090. "Toggle the tag TAG for the current line.
  13091. If ONOFF is `on' or `off', don't toggle but set to this state."
  13092. (let (res current)
  13093. (save-excursion
  13094. (org-back-to-heading t)
  13095. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13096. (point-at-eol) t)
  13097. (progn
  13098. (setq current (match-string 1))
  13099. (replace-match ""))
  13100. (setq current ""))
  13101. (setq current (nreverse (org-split-string current ":")))
  13102. (cond
  13103. ((eq onoff 'on)
  13104. (setq res t)
  13105. (or (member tag current) (push tag current)))
  13106. ((eq onoff 'off)
  13107. (or (not (member tag current)) (setq current (delete tag current))))
  13108. (t (if (member tag current)
  13109. (setq current (delete tag current))
  13110. (setq res t)
  13111. (push tag current))))
  13112. (end-of-line 1)
  13113. (if current
  13114. (progn
  13115. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13116. (org-set-tags nil t))
  13117. (delete-horizontal-space))
  13118. (run-hooks 'org-after-tags-change-hook))
  13119. res))
  13120. (defun org-align-tags-here (to-col)
  13121. ;; Assumes that this is a headline
  13122. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13123. (beginning-of-line 1)
  13124. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13125. (< pos (match-beginning 2)))
  13126. (progn
  13127. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13128. (goto-char (match-beginning 1))
  13129. (insert " ")
  13130. (delete-region (point) (1+ (match-beginning 2)))
  13131. (setq ncol (max (current-column)
  13132. (1+ col)
  13133. (if (> to-col 0)
  13134. to-col
  13135. (- (abs to-col) tags-l))))
  13136. (setq p (point))
  13137. (insert (make-string (- ncol (current-column)) ?\ ))
  13138. (setq ncol (current-column))
  13139. (when indent-tabs-mode (tabify p (point-at-eol)))
  13140. (org-move-to-column (min ncol col) t))
  13141. (goto-char pos))))
  13142. (defun org-set-tags-command (&optional arg just-align)
  13143. "Call the set-tags command for the current entry."
  13144. (interactive "P")
  13145. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13146. (org-set-tags arg just-align)
  13147. (save-excursion
  13148. (unless (and (org-region-active-p)
  13149. org-loop-over-headlines-in-active-region)
  13150. (org-back-to-heading t))
  13151. (org-set-tags arg just-align))))
  13152. (defun org-set-tags-to (data)
  13153. "Set the tags of the current entry to DATA, replacing the current tags.
  13154. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13155. If DATA is nil or the empty string, any tags will be removed."
  13156. (interactive "sTags: ")
  13157. (setq data
  13158. (cond
  13159. ((eq data nil) "")
  13160. ((equal data "") "")
  13161. ((stringp data)
  13162. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13163. ":"))
  13164. ((listp data)
  13165. (concat ":" (mapconcat 'identity data ":") ":"))))
  13166. (when data
  13167. (save-excursion
  13168. (org-back-to-heading t)
  13169. (when (looking-at org-complex-heading-regexp)
  13170. (if (match-end 5)
  13171. (progn
  13172. (goto-char (match-beginning 5))
  13173. (insert data)
  13174. (delete-region (point) (point-at-eol))
  13175. (org-set-tags nil 'align))
  13176. (goto-char (point-at-eol))
  13177. (insert " " data)
  13178. (org-set-tags nil 'align)))
  13179. (beginning-of-line 1)
  13180. (if (looking-at ".*?\\([ \t]+\\)$")
  13181. (delete-region (match-beginning 1) (match-end 1))))))
  13182. (defun org-align-all-tags ()
  13183. "Align the tags i all headings."
  13184. (interactive)
  13185. (save-excursion
  13186. (or (ignore-errors (org-back-to-heading t))
  13187. (outline-next-heading))
  13188. (if (org-at-heading-p)
  13189. (org-set-tags t)
  13190. (message "No headings"))))
  13191. (defvar org-indent-indentation-per-level)
  13192. (defun org-set-tags (&optional arg just-align)
  13193. "Set the tags for the current headline.
  13194. With prefix ARG, realign all tags in headings in the current buffer."
  13195. (interactive "P")
  13196. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13197. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13198. 'region-start-level 'region))
  13199. org-loop-over-headlines-in-active-region)
  13200. (org-map-entries
  13201. ;; We don't use ARG and JUST-ALIGN here these args are not
  13202. ;; useful when looping over headlines
  13203. `(org-set-tags)
  13204. org-loop-over-headlines-in-active-region
  13205. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13206. (let* ((re org-outline-regexp-bol)
  13207. (current (unless arg (org-get-tags-string)))
  13208. (col (current-column))
  13209. (org-setting-tags t)
  13210. table current-tags inherited-tags ; computed below when needed
  13211. tags p0 c0 c1 rpl di tc level)
  13212. (if arg
  13213. (save-excursion
  13214. (goto-char (point-min))
  13215. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13216. (while (re-search-forward re nil t)
  13217. (org-set-tags nil t)
  13218. (end-of-line 1)))
  13219. (message "All tags realigned to column %d" org-tags-column))
  13220. (if just-align
  13221. (setq tags current)
  13222. ;; Get a new set of tags from the user
  13223. (save-excursion
  13224. (setq table (append org-tag-persistent-alist
  13225. (or org-tag-alist (org-get-buffer-tags))
  13226. (and
  13227. org-complete-tags-always-offer-all-agenda-tags
  13228. (org-global-tags-completion-table
  13229. (org-agenda-files))))
  13230. org-last-tags-completion-table table
  13231. current-tags (org-split-string current ":")
  13232. inherited-tags (nreverse
  13233. (nthcdr (length current-tags)
  13234. (nreverse (org-get-tags-at))))
  13235. tags
  13236. (if (or (eq t org-use-fast-tag-selection)
  13237. (and org-use-fast-tag-selection
  13238. (delq nil (mapcar 'cdr table))))
  13239. (org-fast-tag-selection
  13240. current-tags inherited-tags table
  13241. (if org-fast-tag-selection-include-todo
  13242. org-todo-key-alist))
  13243. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13244. (org-trim
  13245. (org-icompleting-read "Tags: "
  13246. 'org-tags-completion-function
  13247. nil nil current 'org-tags-history))))))
  13248. (while (string-match "[-+&]+" tags)
  13249. ;; No boolean logic, just a list
  13250. (setq tags (replace-match ":" t t tags))))
  13251. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13252. (if org-tags-sort-function
  13253. (setq tags (mapconcat 'identity
  13254. (sort (org-split-string
  13255. tags (org-re "[^[:alnum:]_@#%]+"))
  13256. org-tags-sort-function) ":")))
  13257. (if (string-match "\\`[\t ]*\\'" tags)
  13258. (setq tags "")
  13259. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13260. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13261. ;; Insert new tags at the correct column
  13262. (beginning-of-line 1)
  13263. (setq level (or (and (looking-at org-outline-regexp)
  13264. (- (match-end 0) (point) 1))
  13265. 1))
  13266. (cond
  13267. ((and (equal current "") (equal tags "")))
  13268. ((re-search-forward
  13269. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13270. (point-at-eol) t)
  13271. (if (equal tags "")
  13272. (setq rpl "")
  13273. (goto-char (match-beginning 0))
  13274. (setq c0 (current-column)
  13275. ;; compute offset for the case of org-indent-mode active
  13276. di (if org-indent-mode
  13277. (* (1- org-indent-indentation-per-level) (1- level))
  13278. 0)
  13279. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13280. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13281. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13282. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13283. (replace-match rpl t t)
  13284. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13285. tags)
  13286. (t (error "Tags alignment failed")))
  13287. (org-move-to-column col)
  13288. (unless just-align
  13289. (run-hooks 'org-after-tags-change-hook))))))
  13290. (defun org-change-tag-in-region (beg end tag off)
  13291. "Add or remove TAG for each entry in the region.
  13292. This works in the agenda, and also in an org-mode buffer."
  13293. (interactive
  13294. (list (region-beginning) (region-end)
  13295. (let ((org-last-tags-completion-table
  13296. (if (derived-mode-p 'org-mode)
  13297. (org-uniquify
  13298. (delq nil (append (org-get-buffer-tags)
  13299. (org-global-tags-completion-table))))
  13300. (org-global-tags-completion-table))))
  13301. (org-icompleting-read
  13302. "Tag: " 'org-tags-completion-function nil nil nil
  13303. 'org-tags-history))
  13304. (progn
  13305. (message "[s]et or [r]emove? ")
  13306. (equal (read-char-exclusive) ?r))))
  13307. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13308. (let ((agendap (equal major-mode 'org-agenda-mode))
  13309. l1 l2 m buf pos newhead (cnt 0))
  13310. (goto-char end)
  13311. (setq l2 (1- (org-current-line)))
  13312. (goto-char beg)
  13313. (setq l1 (org-current-line))
  13314. (loop for l from l1 to l2 do
  13315. (org-goto-line l)
  13316. (setq m (get-text-property (point) 'org-hd-marker))
  13317. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13318. (and agendap m))
  13319. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13320. pos (if agendap m (point)))
  13321. (with-current-buffer buf
  13322. (save-excursion
  13323. (save-restriction
  13324. (goto-char pos)
  13325. (setq cnt (1+ cnt))
  13326. (org-toggle-tag tag (if off 'off 'on))
  13327. (setq newhead (org-get-heading)))))
  13328. (and agendap (org-agenda-change-all-lines newhead m))))
  13329. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13330. (defun org-tags-completion-function (string predicate &optional flag)
  13331. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13332. (confirm (lambda (x) (stringp (car x)))))
  13333. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13334. (setq s1 (match-string 1 string)
  13335. s2 (match-string 2 string))
  13336. (setq s1 "" s2 string))
  13337. (cond
  13338. ((eq flag nil)
  13339. ;; try completion
  13340. (setq rtn (try-completion s2 ctable confirm))
  13341. (if (stringp rtn)
  13342. (setq rtn
  13343. (concat s1 s2 (substring rtn (length s2))
  13344. (if (and org-add-colon-after-tag-completion
  13345. (assoc rtn ctable))
  13346. ":" ""))))
  13347. rtn)
  13348. ((eq flag t)
  13349. ;; all-completions
  13350. (all-completions s2 ctable confirm))
  13351. ((eq flag 'lambda)
  13352. ;; exact match?
  13353. (assoc s2 ctable)))))
  13354. (defun org-fast-tag-insert (kwd tags face &optional end)
  13355. "Insert KDW, and the TAGS, the latter with face FACE.
  13356. Also insert END."
  13357. (insert (format "%-12s" (concat kwd ":"))
  13358. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13359. (or end "")))
  13360. (defun org-fast-tag-show-exit (flag)
  13361. (save-excursion
  13362. (org-goto-line 3)
  13363. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13364. (replace-match ""))
  13365. (when flag
  13366. (end-of-line 1)
  13367. (org-move-to-column (- (window-width) 19) t)
  13368. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13369. (defun org-set-current-tags-overlay (current prefix)
  13370. "Add an overlay to CURRENT tag with PREFIX."
  13371. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13372. (if (featurep 'xemacs)
  13373. (org-overlay-display org-tags-overlay (concat prefix s)
  13374. 'secondary-selection)
  13375. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13376. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13377. (defvar org-last-tag-selection-key nil)
  13378. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13379. "Fast tag selection with single keys.
  13380. CURRENT is the current list of tags in the headline, INHERITED is the
  13381. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13382. possibly with grouping information. TODO-TABLE is a similar table with
  13383. TODO keywords, should these have keys assigned to them.
  13384. If the keys are nil, a-z are automatically assigned.
  13385. Returns the new tags string, or nil to not change the current settings."
  13386. (let* ((fulltable (append table todo-table))
  13387. (maxlen (apply 'max (mapcar
  13388. (lambda (x)
  13389. (if (stringp (car x)) (string-width (car x)) 0))
  13390. fulltable)))
  13391. (buf (current-buffer))
  13392. (expert (eq org-fast-tag-selection-single-key 'expert))
  13393. (buffer-tags nil)
  13394. (fwidth (+ maxlen 3 1 3))
  13395. (ncol (/ (- (window-width) 4) fwidth))
  13396. (i-face 'org-done)
  13397. (c-face 'org-todo)
  13398. tg cnt e c char c1 c2 ntable tbl rtn
  13399. ov-start ov-end ov-prefix
  13400. (exit-after-next org-fast-tag-selection-single-key)
  13401. (done-keywords org-done-keywords)
  13402. groups ingroup)
  13403. (save-excursion
  13404. (beginning-of-line 1)
  13405. (if (looking-at
  13406. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13407. (setq ov-start (match-beginning 1)
  13408. ov-end (match-end 1)
  13409. ov-prefix "")
  13410. (setq ov-start (1- (point-at-eol))
  13411. ov-end (1+ ov-start))
  13412. (skip-chars-forward "^\n\r")
  13413. (setq ov-prefix
  13414. (concat
  13415. (buffer-substring (1- (point)) (point))
  13416. (if (> (current-column) org-tags-column)
  13417. " "
  13418. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13419. (move-overlay org-tags-overlay ov-start ov-end)
  13420. (save-window-excursion
  13421. (if expert
  13422. (set-buffer (get-buffer-create " *Org tags*"))
  13423. (delete-other-windows)
  13424. (split-window-vertically)
  13425. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13426. (erase-buffer)
  13427. (org-set-local 'org-done-keywords done-keywords)
  13428. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13429. (org-fast-tag-insert "Current" current c-face "\n\n")
  13430. (org-fast-tag-show-exit exit-after-next)
  13431. (org-set-current-tags-overlay current ov-prefix)
  13432. (setq tbl fulltable char ?a cnt 0)
  13433. (while (setq e (pop tbl))
  13434. (cond
  13435. ((equal (car e) :startgroup)
  13436. (push '() groups) (setq ingroup t)
  13437. (when (not (= cnt 0))
  13438. (setq cnt 0)
  13439. (insert "\n"))
  13440. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13441. ((equal (car e) :endgroup)
  13442. (setq ingroup nil cnt 0)
  13443. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13444. ((equal e '(:newline))
  13445. (when (not (= cnt 0))
  13446. (setq cnt 0)
  13447. (insert "\n")
  13448. (setq e (car tbl))
  13449. (while (equal (car tbl) '(:newline))
  13450. (insert "\n")
  13451. (setq tbl (cdr tbl)))))
  13452. ((equal e '(:grouptags)) nil)
  13453. (t
  13454. (setq tg (copy-sequence (car e)) c2 nil)
  13455. (if (cdr e)
  13456. (setq c (cdr e))
  13457. ;; automatically assign a character.
  13458. (setq c1 (string-to-char
  13459. (downcase (substring
  13460. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13461. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13462. (while (or (rassoc char ntable) (rassoc char table))
  13463. (setq char (1+ char)))
  13464. (setq c2 c1))
  13465. (setq c (or c2 char)))
  13466. (if ingroup (push tg (car groups)))
  13467. (setq tg (org-add-props tg nil 'face
  13468. (cond
  13469. ((not (assoc tg table))
  13470. (org-get-todo-face tg))
  13471. ((member tg current) c-face)
  13472. ((member tg inherited) i-face))))
  13473. (if (equal (caar tbl) :grouptags)
  13474. (org-add-props tg nil 'face 'org-tag-group))
  13475. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13476. (insert "[" c "] " tg (make-string
  13477. (- fwidth 4 (length tg)) ?\ ))
  13478. (push (cons tg c) ntable)
  13479. (when (= (setq cnt (1+ cnt)) ncol)
  13480. (insert "\n")
  13481. (if ingroup (insert " "))
  13482. (setq cnt 0)))))
  13483. (setq ntable (nreverse ntable))
  13484. (insert "\n")
  13485. (goto-char (point-min))
  13486. (if (not expert) (org-fit-window-to-buffer))
  13487. (setq rtn
  13488. (catch 'exit
  13489. (while t
  13490. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13491. (if (not groups) "no " "")
  13492. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13493. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13494. (setq org-last-tag-selection-key c)
  13495. (cond
  13496. ((= c ?\r) (throw 'exit t))
  13497. ((= c ?!)
  13498. (setq groups (not groups))
  13499. (goto-char (point-min))
  13500. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13501. ((= c ?\C-c)
  13502. (if (not expert)
  13503. (org-fast-tag-show-exit
  13504. (setq exit-after-next (not exit-after-next)))
  13505. (setq expert nil)
  13506. (delete-other-windows)
  13507. (set-window-buffer (split-window-vertically) " *Org tags*")
  13508. (org-switch-to-buffer-other-window " *Org tags*")
  13509. (org-fit-window-to-buffer)))
  13510. ((or (= c ?\C-g)
  13511. (and (= c ?q) (not (rassoc c ntable))))
  13512. (org-detach-overlay org-tags-overlay)
  13513. (setq quit-flag t))
  13514. ((= c ?\ )
  13515. (setq current nil)
  13516. (if exit-after-next (setq exit-after-next 'now)))
  13517. ((= c ?\t)
  13518. (condition-case nil
  13519. (setq tg (org-icompleting-read
  13520. "Tag: "
  13521. (or buffer-tags
  13522. (with-current-buffer buf
  13523. (org-get-buffer-tags)))))
  13524. (quit (setq tg "")))
  13525. (when (string-match "\\S-" tg)
  13526. (add-to-list 'buffer-tags (list tg))
  13527. (if (member tg current)
  13528. (setq current (delete tg current))
  13529. (push tg current)))
  13530. (if exit-after-next (setq exit-after-next 'now)))
  13531. ((setq e (rassoc c todo-table) tg (car e))
  13532. (with-current-buffer buf
  13533. (save-excursion (org-todo tg)))
  13534. (if exit-after-next (setq exit-after-next 'now)))
  13535. ((setq e (rassoc c ntable) tg (car e))
  13536. (if (member tg current)
  13537. (setq current (delete tg current))
  13538. (loop for g in groups do
  13539. (if (member tg g)
  13540. (mapc (lambda (x)
  13541. (setq current (delete x current)))
  13542. g)))
  13543. (push tg current))
  13544. (if exit-after-next (setq exit-after-next 'now))))
  13545. ;; Create a sorted list
  13546. (setq current
  13547. (sort current
  13548. (lambda (a b)
  13549. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13550. (if (eq exit-after-next 'now) (throw 'exit t))
  13551. (goto-char (point-min))
  13552. (beginning-of-line 2)
  13553. (delete-region (point) (point-at-eol))
  13554. (org-fast-tag-insert "Current" current c-face)
  13555. (org-set-current-tags-overlay current ov-prefix)
  13556. (while (re-search-forward
  13557. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13558. (setq tg (match-string 1))
  13559. (add-text-properties
  13560. (match-beginning 1) (match-end 1)
  13561. (list 'face
  13562. (cond
  13563. ((member tg current) c-face)
  13564. ((member tg inherited) i-face)
  13565. (t (get-text-property (match-beginning 1) 'face))))))
  13566. (goto-char (point-min)))))
  13567. (org-detach-overlay org-tags-overlay)
  13568. (if rtn
  13569. (mapconcat 'identity current ":")
  13570. nil))))
  13571. (defun org-get-tags-string ()
  13572. "Get the TAGS string in the current headline."
  13573. (unless (org-at-heading-p t)
  13574. (user-error "Not on a heading"))
  13575. (save-excursion
  13576. (beginning-of-line 1)
  13577. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13578. (org-match-string-no-properties 1)
  13579. "")))
  13580. (defun org-get-tags ()
  13581. "Get the list of tags specified in the current headline."
  13582. (org-split-string (org-get-tags-string) ":"))
  13583. (defun org-get-buffer-tags ()
  13584. "Get a table of all tags used in the buffer, for completion."
  13585. (let (tags)
  13586. (save-excursion
  13587. (goto-char (point-min))
  13588. (while (re-search-forward
  13589. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13590. (when (equal (char-after (point-at-bol 0)) ?*)
  13591. (mapc (lambda (x) (add-to-list 'tags x))
  13592. (org-split-string (org-match-string-no-properties 1) ":")))))
  13593. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13594. (mapcar 'list tags)))
  13595. ;;;; The mapping API
  13596. (defun org-map-entries (func &optional match scope &rest skip)
  13597. "Call FUNC at each headline selected by MATCH in SCOPE.
  13598. FUNC is a function or a lisp form. The function will be called without
  13599. arguments, with the cursor positioned at the beginning of the headline.
  13600. The return values of all calls to the function will be collected and
  13601. returned as a list.
  13602. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13603. does not need to preserve point. After evaluation, the cursor will be
  13604. moved to the end of the line (presumably of the headline of the
  13605. processed entry) and search continues from there. Under some
  13606. circumstances, this may not produce the wanted results. For example,
  13607. if you have removed (e.g. archived) the current (sub)tree it could
  13608. mean that the next entry will be skipped entirely. In such cases, you
  13609. can specify the position from where search should continue by making
  13610. FUNC set the variable `org-map-continue-from' to the desired buffer
  13611. position.
  13612. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13613. Only headlines that are matched by this query will be considered during
  13614. the iteration. When MATCH is nil or t, all headlines will be
  13615. visited by the iteration.
  13616. SCOPE determines the scope of this command. It can be any of:
  13617. nil The current buffer, respecting the restriction if any
  13618. tree The subtree started with the entry at point
  13619. region The entries within the active region, if any
  13620. region-start-level
  13621. The entries within the active region, but only those at
  13622. the same level than the first one.
  13623. file The current buffer, without restriction
  13624. file-with-archives
  13625. The current buffer, and any archives associated with it
  13626. agenda All agenda files
  13627. agenda-with-archives
  13628. All agenda files with any archive files associated with them
  13629. \(file1 file2 ...)
  13630. If this is a list, all files in the list will be scanned
  13631. The remaining args are treated as settings for the skipping facilities of
  13632. the scanner. The following items can be given here:
  13633. archive skip trees with the archive tag
  13634. comment skip trees with the COMMENT keyword
  13635. function or Emacs Lisp form:
  13636. will be used as value for `org-agenda-skip-function', so
  13637. whenever the function returns a position, FUNC will not be
  13638. called for that entry and search will continue from the
  13639. position returned
  13640. If your function needs to retrieve the tags including inherited tags
  13641. at the *current* entry, you can use the value of the variable
  13642. `org-scanner-tags' which will be much faster than getting the value
  13643. with `org-get-tags-at'. If your function gets properties with
  13644. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13645. to t around the call to `org-entry-properties' to get the same speedup.
  13646. Note that if your function moves around to retrieve tags and properties at
  13647. a *different* entry, you cannot use these techniques."
  13648. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13649. (not (org-region-active-p)))
  13650. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13651. (org-agenda-skip-archived-trees (memq 'archive skip))
  13652. (org-agenda-skip-comment-trees (memq 'comment skip))
  13653. (org-agenda-skip-function
  13654. (car (org-delete-all '(comment archive) skip)))
  13655. (org-tags-match-list-sublevels t)
  13656. (start-level (eq scope 'region-start-level))
  13657. matcher file res
  13658. org-todo-keywords-for-agenda
  13659. org-done-keywords-for-agenda
  13660. org-todo-keyword-alist-for-agenda
  13661. org-tag-alist-for-agenda
  13662. todo-only)
  13663. (cond
  13664. ((eq match t) (setq matcher t))
  13665. ((eq match nil) (setq matcher t))
  13666. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13667. (save-window-excursion
  13668. (save-restriction
  13669. (cond ((eq scope 'tree)
  13670. (org-back-to-heading t)
  13671. (org-narrow-to-subtree)
  13672. (setq scope nil))
  13673. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13674. (org-region-active-p))
  13675. ;; If needed, set start-level to a string like "2"
  13676. (when start-level
  13677. (save-excursion
  13678. (goto-char (region-beginning))
  13679. (unless (org-at-heading-p) (outline-next-heading))
  13680. (setq start-level (org-current-level))))
  13681. (narrow-to-region (region-beginning)
  13682. (save-excursion
  13683. (goto-char (region-end))
  13684. (unless (and (bolp) (org-at-heading-p))
  13685. (outline-next-heading))
  13686. (point)))
  13687. (setq scope nil)))
  13688. (if (not scope)
  13689. (progn
  13690. (org-agenda-prepare-buffers
  13691. (list (buffer-file-name (current-buffer))))
  13692. (setq res (org-scan-tags func matcher todo-only start-level)))
  13693. ;; Get the right scope
  13694. (cond
  13695. ((and scope (listp scope) (symbolp (car scope)))
  13696. (setq scope (eval scope)))
  13697. ((eq scope 'agenda)
  13698. (setq scope (org-agenda-files t)))
  13699. ((eq scope 'agenda-with-archives)
  13700. (setq scope (org-agenda-files t))
  13701. (setq scope (org-add-archive-files scope)))
  13702. ((eq scope 'file)
  13703. (setq scope (list (buffer-file-name))))
  13704. ((eq scope 'file-with-archives)
  13705. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13706. (org-agenda-prepare-buffers scope)
  13707. (while (setq file (pop scope))
  13708. (with-current-buffer (org-find-base-buffer-visiting file)
  13709. (save-excursion
  13710. (save-restriction
  13711. (widen)
  13712. (goto-char (point-min))
  13713. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13714. res)))
  13715. ;;;; Properties
  13716. ;;; Setting and retrieving properties
  13717. (defconst org-special-properties
  13718. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13719. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13720. "The special properties valid in Org-mode.
  13721. These are properties that are not defined in the property drawer,
  13722. but in some other way.")
  13723. (defconst org-default-properties
  13724. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13725. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13726. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13727. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13728. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13729. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13730. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13731. "Some properties that are used by Org-mode for various purposes.
  13732. Being in this list makes sure that they are offered for completion.")
  13733. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13734. "Regular expression matching the first line of a property drawer.")
  13735. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13736. "Regular expression matching the last line of a property drawer.")
  13737. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13738. "Regular expression matching the first line of a property drawer.")
  13739. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13740. "Regular expression matching the first line of a property drawer.")
  13741. (defconst org-property-drawer-re
  13742. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  13743. org-property-end-re "\\)\n?")
  13744. "Matches an entire property drawer.")
  13745. (defconst org-clock-drawer-re
  13746. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  13747. org-property-end-re "\\)\n?")
  13748. "Matches an entire clock drawer.")
  13749. (defun org-property-action ()
  13750. "Do an action on properties."
  13751. (interactive)
  13752. (let (c)
  13753. (org-at-property-p)
  13754. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13755. (setq c (read-char-exclusive))
  13756. (cond
  13757. ((equal c ?s)
  13758. (call-interactively 'org-set-property))
  13759. ((equal c ?d)
  13760. (call-interactively 'org-delete-property))
  13761. ((equal c ?D)
  13762. (call-interactively 'org-delete-property-globally))
  13763. ((equal c ?c)
  13764. (call-interactively 'org-compute-property-at-point))
  13765. (t (user-error "No such property action %c" c)))))
  13766. (defun org-inc-effort ()
  13767. "Increment the value of the effort property in the current entry."
  13768. (interactive)
  13769. (org-set-effort nil t))
  13770. (defvar org-clock-effort) ;; Defined in org-clock.el
  13771. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13772. (defun org-set-effort (&optional value increment)
  13773. "Set the effort property of the current entry.
  13774. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13775. 10th allowed value.
  13776. When INCREMENT is non-nil, set the property to the next allowed value."
  13777. (interactive "P")
  13778. (if (equal value 0) (setq value 10))
  13779. (let* ((completion-ignore-case t)
  13780. (prop org-effort-property)
  13781. (cur (org-entry-get nil prop))
  13782. (allowed (org-property-get-allowed-values nil prop 'table))
  13783. (existing (mapcar 'list (org-property-values prop)))
  13784. (heading (nth 4 (org-heading-components)))
  13785. rpl
  13786. (val (cond
  13787. ((stringp value) value)
  13788. ((and allowed (integerp value))
  13789. (or (car (nth (1- value) allowed))
  13790. (car (org-last allowed))))
  13791. ((and allowed increment)
  13792. (or (caadr (member (list cur) allowed))
  13793. (user-error "Allowed effort values are not set")))
  13794. (allowed
  13795. (message "Select 1-9,0, [RET%s]: %s"
  13796. (if cur (concat "=" cur) "")
  13797. (mapconcat 'car allowed " "))
  13798. (setq rpl (read-char-exclusive))
  13799. (if (equal rpl ?\r)
  13800. cur
  13801. (setq rpl (- rpl ?0))
  13802. (if (equal rpl 0) (setq rpl 10))
  13803. (if (and (> rpl 0) (<= rpl (length allowed)))
  13804. (car (nth (1- rpl) allowed))
  13805. (org-completing-read "Effort: " allowed nil))))
  13806. (t
  13807. (let (org-completion-use-ido org-completion-use-iswitchb)
  13808. (org-completing-read
  13809. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13810. (concat "[" cur "]") "")
  13811. ": ")
  13812. existing nil nil "" nil cur))))))
  13813. (unless (equal (org-entry-get nil prop) val)
  13814. (org-entry-put nil prop val))
  13815. (save-excursion
  13816. (org-back-to-heading t)
  13817. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13818. (when (string= heading org-clock-current-task)
  13819. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13820. (org-clock-update-mode-line))
  13821. (message "%s is now %s" prop val)))
  13822. (defun org-at-property-p ()
  13823. "Is cursor inside a property drawer?"
  13824. (save-excursion
  13825. (when (equal 'node-property (car (org-element-at-point)))
  13826. (beginning-of-line 1)
  13827. (looking-at org-property-re))))
  13828. (defun org-get-property-block (&optional beg end force)
  13829. "Return the (beg . end) range of the body of the property drawer.
  13830. BEG and END are the beginning and end of the current subtree, or of
  13831. the part before the first headline. If they are not given, they will
  13832. be found. If the drawer does not exist and FORCE is non-nil, create
  13833. the drawer."
  13834. (catch 'exit
  13835. (save-excursion
  13836. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13837. (progn (org-back-to-heading t) (point))))
  13838. (end (or end (and (not (outline-next-heading)) (point-max))
  13839. (point))))
  13840. (goto-char beg)
  13841. (if (re-search-forward org-property-start-re end t)
  13842. (setq beg (1+ (match-end 0)))
  13843. (if force
  13844. (save-excursion
  13845. (org-insert-property-drawer)
  13846. (setq end (progn (outline-next-heading) (point))))
  13847. (throw 'exit nil))
  13848. (goto-char beg)
  13849. (if (re-search-forward org-property-start-re end t)
  13850. (setq beg (1+ (match-end 0)))))
  13851. (if (re-search-forward org-property-end-re end t)
  13852. (setq end (match-beginning 0))
  13853. (or force (throw 'exit nil))
  13854. (goto-char beg)
  13855. (setq end beg)
  13856. (org-indent-line)
  13857. (insert ":END:\n"))
  13858. (cons beg end)))))
  13859. (defun org-entry-properties (&optional pom which specific)
  13860. "Get all properties of the entry at point-or-marker POM.
  13861. This includes the TODO keyword, the tags, time strings for deadline,
  13862. scheduled, and clocking, and any additional properties defined in the
  13863. entry. The return value is an alist, keys may occur multiple times
  13864. if the property key was used several times.
  13865. POM may also be nil, in which case the current entry is used.
  13866. If WHICH is nil or `all', get all properties. If WHICH is
  13867. `special' or `standard', only get that subclass. If WHICH
  13868. is a string only get exactly this property. SPECIFIC can be a string, the
  13869. specific property we are interested in. Specifying it can speed
  13870. things up because then unnecessary parsing is avoided."
  13871. (setq which (or which 'all))
  13872. (org-with-point-at pom
  13873. (let ((clockstr (substring org-clock-string 0 -1))
  13874. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13875. (case-fold-search nil)
  13876. beg end range props sum-props key key1 value string clocksum clocksumt)
  13877. (save-excursion
  13878. (when (condition-case nil
  13879. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13880. (error nil))
  13881. (setq beg (point))
  13882. (setq sum-props (get-text-property (point) 'org-summaries))
  13883. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13884. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13885. (outline-next-heading)
  13886. (setq end (point))
  13887. (when (memq which '(all special))
  13888. ;; Get the special properties, like TODO and tags
  13889. (goto-char beg)
  13890. (when (and (or (not specific) (string= specific "TODO"))
  13891. (looking-at org-todo-line-regexp) (match-end 2))
  13892. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13893. (when (and (or (not specific) (string= specific "PRIORITY"))
  13894. (looking-at org-priority-regexp))
  13895. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13896. (when (or (not specific) (string= specific "FILE"))
  13897. (push (cons "FILE" buffer-file-name) props))
  13898. (when (and (or (not specific) (string= specific "TAGS"))
  13899. (setq value (org-get-tags-string))
  13900. (string-match "\\S-" value))
  13901. (push (cons "TAGS" value) props))
  13902. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13903. (setq value (org-get-tags-at)))
  13904. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13905. ":"))
  13906. props))
  13907. (when (or (not specific) (string= specific "BLOCKED"))
  13908. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13909. (when (or (not specific)
  13910. (member specific
  13911. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13912. "TIMESTAMP" "TIMESTAMP_IA")))
  13913. (catch 'match
  13914. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13915. (setq key (if (match-end 1)
  13916. (substring (org-match-string-no-properties 1)
  13917. 0 -1))
  13918. string (if (equal key clockstr)
  13919. (org-trim
  13920. (buffer-substring-no-properties
  13921. (match-beginning 3) (goto-char
  13922. (point-at-eol))))
  13923. (substring (org-match-string-no-properties 3)
  13924. 1 -1)))
  13925. ;; Get the correct property name from the key. This is
  13926. ;; necessary if the user has configured time keywords.
  13927. (setq key1 (concat key ":"))
  13928. (cond
  13929. ((not key)
  13930. (setq key
  13931. (if (= (char-after (match-beginning 3)) ?\[)
  13932. "TIMESTAMP_IA" "TIMESTAMP")))
  13933. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13934. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13935. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13936. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13937. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13938. (progn
  13939. (push (cons key string) props)
  13940. ;; no need to search further if match is found
  13941. (throw 'match t))
  13942. (when (or (equal key "CLOCK") (not (assoc key props)))
  13943. (push (cons key string) props)))))))
  13944. (when (memq which '(all standard))
  13945. ;; Get the standard properties, like :PROP: ...
  13946. (setq range (org-get-property-block beg end))
  13947. (when range
  13948. (goto-char (car range))
  13949. (while (re-search-forward org-property-re
  13950. (cdr range) t)
  13951. (setq key (org-match-string-no-properties 2)
  13952. value (org-trim (or (org-match-string-no-properties 3) "")))
  13953. (unless (member key excluded)
  13954. (push (cons key (or value "")) props)))))
  13955. (if clocksum
  13956. (push (cons "CLOCKSUM"
  13957. (org-columns-number-to-string (/ (float clocksum) 60.)
  13958. 'add_times))
  13959. props))
  13960. (if clocksumt
  13961. (push (cons "CLOCKSUM_T"
  13962. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13963. 'add_times))
  13964. props))
  13965. (unless (assoc "CATEGORY" props)
  13966. (push (cons "CATEGORY" (org-get-category)) props))
  13967. (append sum-props (nreverse props)))))))
  13968. (defun org-entry-get (pom property &optional inherit literal-nil)
  13969. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13970. If INHERIT is non-nil and the entry does not have the property,
  13971. then also check higher levels of the hierarchy.
  13972. If INHERIT is the symbol `selective', use inheritance only if the setting
  13973. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13974. If the property is present but empty, the return value is the empty string.
  13975. If the property is not present at all, nil is returned.
  13976. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13977. do not interpret it as the list atom nil. This is used for inheritance
  13978. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13979. (org-with-point-at pom
  13980. (if (and inherit (if (eq inherit 'selective)
  13981. (org-property-inherit-p property)
  13982. t))
  13983. (org-entry-get-with-inheritance property literal-nil)
  13984. (if (member property org-special-properties)
  13985. ;; We need a special property. Use `org-entry-properties' to
  13986. ;; retrieve it, but specify the wanted property
  13987. (cdr (assoc property (org-entry-properties nil 'special property)))
  13988. (let ((range (org-get-property-block)))
  13989. (when (and range (not (eq (car range) (cdr range))))
  13990. (let* ((props (list (or (assoc property org-file-properties)
  13991. (assoc property org-global-properties)
  13992. (assoc property org-global-properties-fixed))))
  13993. (ap (lambda (key)
  13994. (when (re-search-forward
  13995. (org-re-property key) (cdr range) t)
  13996. (setq props
  13997. (org-update-property-plist
  13998. key
  13999. (if (match-end 3)
  14000. (org-match-string-no-properties 3) "")
  14001. props)))))
  14002. val)
  14003. (goto-char (car range))
  14004. (funcall ap property)
  14005. (goto-char (car range))
  14006. (while (funcall ap (concat property "+")))
  14007. (setq val (cdr (assoc property props)))
  14008. (when val (if literal-nil val (org-not-nil val))))))))))
  14009. (defun org-property-or-variable-value (var &optional inherit)
  14010. "Check if there is a property fixing the value of VAR.
  14011. If yes, return this value. If not, return the current value of the variable."
  14012. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14013. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14014. (read prop)
  14015. (symbol-value var))))
  14016. (defun org-entry-delete (pom property)
  14017. "Delete the property PROPERTY from entry at point-or-marker POM."
  14018. (unless (member property org-special-properties)
  14019. (org-with-point-at pom
  14020. (let ((range (org-get-property-block)))
  14021. (if (and range
  14022. (goto-char (car range))
  14023. (re-search-forward
  14024. (org-re-property property)
  14025. (cdr range) t))
  14026. (progn
  14027. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14028. (org-remove-empty-drawer-at (car range))
  14029. t)
  14030. nil)))))
  14031. ;; Multi-values properties are properties that contain multiple values
  14032. ;; These values are assumed to be single words, separated by whitespace.
  14033. (defun org-entry-add-to-multivalued-property (pom property value)
  14034. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14035. (let* ((old (org-entry-get pom property))
  14036. (values (and old (org-split-string old "[ \t]"))))
  14037. (setq value (org-entry-protect-space value))
  14038. (unless (member value values)
  14039. (setq values (append values (list value)))
  14040. (org-entry-put pom property
  14041. (mapconcat 'identity values " ")))))
  14042. (defun org-entry-remove-from-multivalued-property (pom property value)
  14043. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14044. (let* ((old (org-entry-get pom property))
  14045. (values (and old (org-split-string old "[ \t]"))))
  14046. (setq value (org-entry-protect-space value))
  14047. (when (member value values)
  14048. (setq values (delete value values))
  14049. (org-entry-put pom property
  14050. (mapconcat 'identity values " ")))))
  14051. (defun org-entry-member-in-multivalued-property (pom property value)
  14052. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14053. (let* ((old (org-entry-get pom property))
  14054. (values (and old (org-split-string old "[ \t]"))))
  14055. (setq value (org-entry-protect-space value))
  14056. (member value values)))
  14057. (defun org-entry-get-multivalued-property (pom property)
  14058. "Return a list of values in a multivalued property."
  14059. (let* ((value (org-entry-get pom property))
  14060. (values (and value (org-split-string value "[ \t]"))))
  14061. (mapcar 'org-entry-restore-space values)))
  14062. (defun org-entry-put-multivalued-property (pom property &rest values)
  14063. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14064. VALUES should be a list of strings. Spaces will be protected."
  14065. (org-entry-put pom property
  14066. (mapconcat 'org-entry-protect-space values " "))
  14067. (let* ((value (org-entry-get pom property))
  14068. (values (and value (org-split-string value "[ \t]"))))
  14069. (mapcar 'org-entry-restore-space values)))
  14070. (defun org-entry-protect-space (s)
  14071. "Protect spaces and newline in string S."
  14072. (while (string-match " " s)
  14073. (setq s (replace-match "%20" t t s)))
  14074. (while (string-match "\n" s)
  14075. (setq s (replace-match "%0A" t t s)))
  14076. s)
  14077. (defun org-entry-restore-space (s)
  14078. "Restore spaces and newline in string S."
  14079. (while (string-match "%20" s)
  14080. (setq s (replace-match " " t t s)))
  14081. (while (string-match "%0A" s)
  14082. (setq s (replace-match "\n" t t s)))
  14083. s)
  14084. (defvar org-entry-property-inherited-from (make-marker)
  14085. "Marker pointing to the entry from where a property was inherited.
  14086. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14087. location of the entry where the inheritance search matched. If there was
  14088. no match, the marker will point nowhere.
  14089. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14090. is set.")
  14091. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14092. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14093. The search will stop at the first ancestor which has the property defined.
  14094. If the value found is \"nil\", return nil to show that the property
  14095. should be considered as undefined (this is the meaning of nil here).
  14096. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14097. (move-marker org-entry-property-inherited-from nil)
  14098. (let (tmp)
  14099. (save-excursion
  14100. (save-restriction
  14101. (widen)
  14102. (catch 'ex
  14103. (while t
  14104. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14105. (or (ignore-errors (org-back-to-heading t))
  14106. (goto-char (point-min)))
  14107. (move-marker org-entry-property-inherited-from (point))
  14108. (throw 'ex tmp))
  14109. (or (ignore-errors (org-up-heading-safe))
  14110. (throw 'ex nil))))))
  14111. (setq tmp (or tmp
  14112. (cdr (assoc property org-file-properties))
  14113. (cdr (assoc property org-global-properties))
  14114. (cdr (assoc property org-global-properties-fixed))))
  14115. (if literal-nil tmp (org-not-nil tmp))))
  14116. (defvar org-property-changed-functions nil
  14117. "Hook called when the value of a property has changed.
  14118. Each hook function should accept two arguments, the name of the property
  14119. and the new value.")
  14120. (defun org-entry-put (pom property value)
  14121. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14122. (org-with-point-at pom
  14123. (org-back-to-heading t)
  14124. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14125. range)
  14126. (cond
  14127. ((equal property "TODO")
  14128. (when (and (stringp value) (string-match "\\S-" value)
  14129. (not (member value org-todo-keywords-1)))
  14130. (user-error "\"%s\" is not a valid TODO state" value))
  14131. (if (or (not value)
  14132. (not (string-match "\\S-" value)))
  14133. (setq value 'none))
  14134. (org-todo value)
  14135. (org-set-tags nil 'align))
  14136. ((equal property "PRIORITY")
  14137. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14138. (string-to-char value) ?\ ))
  14139. (org-set-tags nil 'align))
  14140. ((equal property "CLOCKSUM")
  14141. (if (not (re-search-forward
  14142. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14143. (error "Cannot find a clock log")
  14144. (goto-char (- (match-end 1) 2))
  14145. (cond
  14146. ((eq value 'earlier) (org-timestamp-down))
  14147. ((eq value 'later) (org-timestamp-up)))
  14148. (org-clock-sum-current-item)))
  14149. ((equal property "SCHEDULED")
  14150. (if (re-search-forward org-scheduled-time-regexp end t)
  14151. (cond
  14152. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14153. ((eq value 'later) (org-timestamp-change 1 'day))
  14154. (t (call-interactively 'org-schedule)))
  14155. (call-interactively 'org-schedule)))
  14156. ((equal property "DEADLINE")
  14157. (if (re-search-forward org-deadline-time-regexp end t)
  14158. (cond
  14159. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14160. ((eq value 'later) (org-timestamp-change 1 'day))
  14161. (t (call-interactively 'org-deadline)))
  14162. (call-interactively 'org-deadline)))
  14163. ((member property org-special-properties)
  14164. (error "The %s property can not yet be set with `org-entry-put'"
  14165. property))
  14166. (t ; a non-special property
  14167. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14168. (setq range (org-get-property-block beg end 'force))
  14169. (goto-char (car range))
  14170. (if (re-search-forward
  14171. (org-re-property property) (cdr range) t)
  14172. (progn
  14173. (delete-region (match-beginning 0) (match-end 0))
  14174. (goto-char (match-beginning 0)))
  14175. (goto-char (cdr range))
  14176. (insert "\n")
  14177. (backward-char 1)
  14178. (org-indent-line))
  14179. (insert ":" property ":")
  14180. (and value (insert " " value))
  14181. (org-indent-line)))))
  14182. (run-hook-with-args 'org-property-changed-functions property value)))
  14183. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14184. "Get all property keys in the current buffer.
  14185. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14186. like tags and TODO state.
  14187. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14188. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14189. and others.
  14190. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14191. formats in the current buffer."
  14192. (let (rtn range cfmt s p)
  14193. (save-excursion
  14194. (save-restriction
  14195. (widen)
  14196. (goto-char (point-min))
  14197. (while (re-search-forward org-property-start-re nil t)
  14198. (setq range (org-get-property-block))
  14199. (goto-char (car range))
  14200. (while (re-search-forward org-property-re
  14201. (cdr range) t)
  14202. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14203. (outline-next-heading))))
  14204. (when include-specials
  14205. (setq rtn (append org-special-properties rtn)))
  14206. (when include-defaults
  14207. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14208. (add-to-list 'rtn org-effort-property))
  14209. (when include-columns
  14210. (save-excursion
  14211. (save-restriction
  14212. (widen)
  14213. (goto-char (point-min))
  14214. (while (re-search-forward
  14215. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14216. nil t)
  14217. (setq cfmt (match-string 2) s 0)
  14218. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14219. cfmt s)
  14220. (setq s (match-end 0)
  14221. p (match-string 1 cfmt))
  14222. (unless (or (equal p "ITEM")
  14223. (member p org-special-properties))
  14224. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14225. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14226. (defun org-property-values (key)
  14227. "Return a list of all values of property KEY in the current buffer."
  14228. (save-excursion
  14229. (save-restriction
  14230. (widen)
  14231. (goto-char (point-min))
  14232. (let ((re (org-re-property key))
  14233. values)
  14234. (while (re-search-forward re nil t)
  14235. (add-to-list 'values (org-trim (match-string 3))))
  14236. (delete "" values)))))
  14237. (defun org-insert-property-drawer ()
  14238. "Insert a property drawer into the current entry."
  14239. (org-back-to-heading t)
  14240. (looking-at org-outline-regexp)
  14241. (let ((indent (if org-adapt-indentation
  14242. (- (match-end 0) (match-beginning 0))
  14243. 0))
  14244. (beg (point))
  14245. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14246. end hiddenp)
  14247. (outline-next-heading)
  14248. (setq end (point))
  14249. (goto-char beg)
  14250. (while (re-search-forward re end t))
  14251. (setq hiddenp (outline-invisible-p))
  14252. (end-of-line 1)
  14253. (and (equal (char-after) ?\n) (forward-char 1))
  14254. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14255. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14256. ;; just skip this line
  14257. (beginning-of-line 2)
  14258. ;; Drawer start, find the end
  14259. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14260. (beginning-of-line 1)))
  14261. (org-skip-over-state-notes)
  14262. (skip-chars-backward " \t\n\r")
  14263. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14264. (forward-char 1))
  14265. (goto-char (point-at-eol))
  14266. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14267. (beginning-of-line 0)
  14268. (org-indent-to-column indent)
  14269. (beginning-of-line 2)
  14270. (org-indent-to-column indent)
  14271. (beginning-of-line 0)
  14272. (if hiddenp
  14273. (save-excursion
  14274. (org-back-to-heading t)
  14275. (hide-entry))
  14276. (org-flag-drawer t))))
  14277. (defun org-insert-drawer (&optional arg drawer)
  14278. "Insert a drawer at point.
  14279. Optional argument DRAWER, when non-nil, is a string representing
  14280. drawer's name. Otherwise, the user is prompted for a name.
  14281. If a region is active, insert the drawer around that region
  14282. instead.
  14283. Point is left between drawer's boundaries."
  14284. (interactive "P")
  14285. (let* ((drawer (if arg "PROPERTIES"
  14286. (or drawer (read-from-minibuffer "Drawer: ")))))
  14287. (cond
  14288. ;; With C-u, fall back on `org-insert-property-drawer'
  14289. (arg (org-insert-property-drawer))
  14290. ;;
  14291. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14292. (user-error "Invalid drawer name"))
  14293. ;; With an active region, insert a drawer at point.
  14294. ((not (org-region-active-p))
  14295. (progn
  14296. (unless (bolp) (insert "\n"))
  14297. (insert (format ":%s:\n\n:END:\n" drawer))
  14298. (forward-line -2)))
  14299. ;; Otherwise, insert the drawer at point
  14300. (t
  14301. (let ((rbeg (region-beginning))
  14302. (rend (copy-marker (region-end))))
  14303. (unwind-protect
  14304. (progn
  14305. (goto-char rbeg)
  14306. (beginning-of-line)
  14307. (when (save-excursion
  14308. (re-search-forward org-outline-regexp-bol rend t))
  14309. (user-error "Drawers cannot contain headlines"))
  14310. ;; Position point at the beginning of the first
  14311. ;; non-blank line in region. Insert drawer's opening
  14312. ;; there, then indent it.
  14313. (org-skip-whitespace)
  14314. (beginning-of-line)
  14315. (insert ":" drawer ":\n")
  14316. (forward-line -1)
  14317. (indent-for-tab-command)
  14318. ;; Move point to the beginning of the first blank line
  14319. ;; after the last non-blank line in region. Insert
  14320. ;; drawer's closing, then indent it.
  14321. (goto-char rend)
  14322. (skip-chars-backward " \r\t\n")
  14323. (insert "\n:END:")
  14324. (deactivate-mark t)
  14325. (indent-for-tab-command)
  14326. (unless (eolp) (insert "\n")))
  14327. ;; Clear marker, whatever the outcome of insertion is.
  14328. (set-marker rend nil)))))))
  14329. (defvar org-property-set-functions-alist nil
  14330. "Property set function alist.
  14331. Each entry should have the following format:
  14332. (PROPERTY . READ-FUNCTION)
  14333. The read function will be called with the same argument as
  14334. `org-completing-read'.")
  14335. (defun org-set-property-function (property)
  14336. "Get the function that should be used to set PROPERTY.
  14337. This is computed according to `org-property-set-functions-alist'."
  14338. (or (cdr (assoc property org-property-set-functions-alist))
  14339. 'org-completing-read))
  14340. (defun org-read-property-value (property)
  14341. "Read PROPERTY value from user."
  14342. (let* ((completion-ignore-case t)
  14343. (allowed (org-property-get-allowed-values nil property 'table))
  14344. (cur (org-entry-get nil property))
  14345. (prompt (concat property " value"
  14346. (if (and cur (string-match "\\S-" cur))
  14347. (concat " [" cur "]") "") ": "))
  14348. (set-function (org-set-property-function property))
  14349. (val (if allowed
  14350. (funcall set-function prompt allowed nil
  14351. (not (get-text-property 0 'org-unrestricted
  14352. (caar allowed))))
  14353. (let (org-completion-use-ido org-completion-use-iswitchb)
  14354. (funcall set-function prompt
  14355. (mapcar 'list (org-property-values property))
  14356. nil nil "" nil cur)))))
  14357. (if (equal val "")
  14358. cur
  14359. val)))
  14360. (defvar org-last-set-property nil)
  14361. (defvar org-last-set-property-value nil)
  14362. (defun org-read-property-name ()
  14363. "Read a property name."
  14364. (let* ((completion-ignore-case t)
  14365. (keys (org-buffer-property-keys nil t t))
  14366. (default-prop (or (save-excursion
  14367. (save-match-data
  14368. (beginning-of-line)
  14369. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14370. (null (string= (match-string 1) "END"))
  14371. (match-string 1))))
  14372. org-last-set-property))
  14373. (property (org-icompleting-read
  14374. (concat "Property"
  14375. (if default-prop (concat " [" default-prop "]") "")
  14376. ": ")
  14377. (mapcar 'list keys)
  14378. nil nil nil nil
  14379. default-prop)))
  14380. (if (member property keys)
  14381. property
  14382. (or (cdr (assoc (downcase property)
  14383. (mapcar (lambda (x) (cons (downcase x) x))
  14384. keys)))
  14385. property))))
  14386. (defun org-set-property-and-value (use-last)
  14387. "Allow to set [PROPERTY]: [value] direction from prompt.
  14388. When use-default, don't even ask, just use the last
  14389. \"[PROPERTY]: [value]\" string from the history."
  14390. (interactive "P")
  14391. (let* ((completion-ignore-case t)
  14392. (pv (or (and use-last org-last-set-property-value)
  14393. (org-completing-read
  14394. "Enter a \"[Property]: [value]\" pair: "
  14395. nil nil nil nil nil
  14396. org-last-set-property-value)))
  14397. prop val)
  14398. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14399. (setq prop (match-string 1 pv)
  14400. val (match-string 2 pv))
  14401. (org-set-property prop val))))
  14402. (defun org-set-property (property value)
  14403. "In the current entry, set PROPERTY to VALUE.
  14404. When called interactively, this will prompt for a property name, offering
  14405. completion on existing and default properties. And then it will prompt
  14406. for a value, offering completion either on allowed values (via an inherited
  14407. xxx_ALL property) or on existing values in other instances of this property
  14408. in the current file."
  14409. (interactive (list nil nil))
  14410. (let* ((property (or property (org-read-property-name)))
  14411. (value (or value (org-read-property-value property)))
  14412. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14413. (setq org-last-set-property property)
  14414. (setq org-last-set-property-value (concat property ": " value))
  14415. ;; Possibly postprocess the inserted value:
  14416. (when fn (setq value (funcall fn value)))
  14417. (unless (equal (org-entry-get nil property) value)
  14418. (org-entry-put nil property value))))
  14419. (defun org-delete-property (property)
  14420. "In the current entry, delete PROPERTY."
  14421. (interactive
  14422. (let* ((completion-ignore-case t)
  14423. (prop (org-icompleting-read "Property: "
  14424. (org-entry-properties nil 'standard))))
  14425. (list prop)))
  14426. (message "Property %s %s" property
  14427. (if (org-entry-delete nil property)
  14428. "deleted"
  14429. "was not present in the entry")))
  14430. (defun org-delete-property-globally (property)
  14431. "Remove PROPERTY globally, from all entries."
  14432. (interactive
  14433. (let* ((completion-ignore-case t)
  14434. (prop (org-icompleting-read
  14435. "Globally remove property: "
  14436. (mapcar 'list (org-buffer-property-keys)))))
  14437. (list prop)))
  14438. (save-excursion
  14439. (save-restriction
  14440. (widen)
  14441. (goto-char (point-min))
  14442. (let ((cnt 0))
  14443. (while (re-search-forward
  14444. (org-re-property property)
  14445. nil t)
  14446. (setq cnt (1+ cnt))
  14447. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14448. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14449. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14450. (defun org-compute-property-at-point ()
  14451. "Compute the property at point.
  14452. This looks for an enclosing column format, extracts the operator and
  14453. then applies it to the property in the column format's scope."
  14454. (interactive)
  14455. (unless (org-at-property-p)
  14456. (user-error "Not at a property"))
  14457. (let ((prop (org-match-string-no-properties 2)))
  14458. (org-columns-get-format-and-top-level)
  14459. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14460. (user-error "No operator defined for property %s" prop))
  14461. (org-columns-compute prop)))
  14462. (defvar org-property-allowed-value-functions nil
  14463. "Hook for functions supplying allowed values for a specific property.
  14464. The functions must take a single argument, the name of the property, and
  14465. return a flat list of allowed values. If \":ETC\" is one of
  14466. the values, this means that these values are intended as defaults for
  14467. completion, but that other values should be allowed too.
  14468. The functions must return nil if they are not responsible for this
  14469. property.")
  14470. (defun org-property-get-allowed-values (pom property &optional table)
  14471. "Get allowed values for the property PROPERTY.
  14472. When TABLE is non-nil, return an alist that can directly be used for
  14473. completion."
  14474. (let (vals)
  14475. (cond
  14476. ((equal property "TODO")
  14477. (setq vals (org-with-point-at pom
  14478. (append org-todo-keywords-1 '("")))))
  14479. ((equal property "PRIORITY")
  14480. (let ((n org-lowest-priority))
  14481. (while (>= n org-highest-priority)
  14482. (push (char-to-string n) vals)
  14483. (setq n (1- n)))))
  14484. ((member property org-special-properties))
  14485. ((setq vals (run-hook-with-args-until-success
  14486. 'org-property-allowed-value-functions property)))
  14487. (t
  14488. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14489. (when (and vals (string-match "\\S-" vals))
  14490. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14491. (setq vals (mapcar (lambda (x)
  14492. (cond ((stringp x) x)
  14493. ((numberp x) (number-to-string x))
  14494. ((symbolp x) (symbol-name x))
  14495. (t "???")))
  14496. vals)))))
  14497. (when (member ":ETC" vals)
  14498. (setq vals (remove ":ETC" vals))
  14499. (org-add-props (car vals) '(org-unrestricted t)))
  14500. (if table (mapcar 'list vals) vals)))
  14501. (defun org-property-previous-allowed-value (&optional previous)
  14502. "Switch to the next allowed value for this property."
  14503. (interactive)
  14504. (org-property-next-allowed-value t))
  14505. (defun org-property-next-allowed-value (&optional previous)
  14506. "Switch to the next allowed value for this property."
  14507. (interactive)
  14508. (unless (org-at-property-p)
  14509. (user-error "Not at a property"))
  14510. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14511. (key (match-string 2))
  14512. (value (match-string 3))
  14513. (allowed (or (org-property-get-allowed-values (point) key)
  14514. (and (member value '("[ ]" "[-]" "[X]"))
  14515. '("[ ]" "[X]"))))
  14516. (heading (save-match-data (nth 4 (org-heading-components))))
  14517. nval)
  14518. (unless allowed
  14519. (user-error "Allowed values for this property have not been defined"))
  14520. (if previous (setq allowed (reverse allowed)))
  14521. (if (member value allowed)
  14522. (setq nval (car (cdr (member value allowed)))))
  14523. (setq nval (or nval (car allowed)))
  14524. (if (equal nval value)
  14525. (user-error "Only one allowed value for this property"))
  14526. (org-at-property-p)
  14527. (replace-match (concat " :" key ": " nval) t t)
  14528. (org-indent-line)
  14529. (beginning-of-line 1)
  14530. (skip-chars-forward " \t")
  14531. (when (equal prop org-effort-property)
  14532. (save-excursion
  14533. (org-back-to-heading t)
  14534. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14535. (when (string= org-clock-current-task heading)
  14536. (setq org-clock-effort nval)
  14537. (org-clock-update-mode-line)))
  14538. (run-hook-with-args 'org-property-changed-functions key nval)))
  14539. (defun org-find-olp (path &optional this-buffer)
  14540. "Return a marker pointing to the entry at outline path OLP.
  14541. If anything goes wrong, throw an error.
  14542. You can wrap this call to catch the error like this:
  14543. (condition-case msg
  14544. (org-mobile-locate-entry (match-string 4))
  14545. (error (nth 1 msg)))
  14546. The return value will then be either a string with the error message,
  14547. or a marker if everything is OK.
  14548. If THIS-BUFFER is set, the outline path does not contain a file,
  14549. only headings."
  14550. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14551. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14552. (level 1)
  14553. (lmin 1)
  14554. (lmax 1)
  14555. limit re end found pos heading cnt flevel)
  14556. (unless buffer (error "File not found :%s" file))
  14557. (with-current-buffer buffer
  14558. (save-excursion
  14559. (save-restriction
  14560. (widen)
  14561. (setq limit (point-max))
  14562. (goto-char (point-min))
  14563. (while (setq heading (pop path))
  14564. (setq re (format org-complex-heading-regexp-format
  14565. (regexp-quote heading)))
  14566. (setq cnt 0 pos (point))
  14567. (while (re-search-forward re end t)
  14568. (setq level (- (match-end 1) (match-beginning 1)))
  14569. (if (and (>= level lmin) (<= level lmax))
  14570. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14571. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14572. lmax heading))
  14573. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14574. lmax heading))
  14575. (goto-char found)
  14576. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14577. (setq end (save-excursion (org-end-of-subtree t t))))
  14578. (when (org-at-heading-p)
  14579. (point-marker)))))))
  14580. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14581. "Find node HEADING in BUFFER.
  14582. Return a marker to the heading if it was found, or nil if not.
  14583. If POS-ONLY is set, return just the position instead of a marker.
  14584. The heading text must match exact, but it may have a TODO keyword,
  14585. a priority cookie and tags in the standard locations."
  14586. (with-current-buffer (or buffer (current-buffer))
  14587. (save-excursion
  14588. (save-restriction
  14589. (widen)
  14590. (goto-char (point-min))
  14591. (let (case-fold-search)
  14592. (if (re-search-forward
  14593. (format org-complex-heading-regexp-format
  14594. (regexp-quote heading)) nil t)
  14595. (if pos-only
  14596. (match-beginning 0)
  14597. (move-marker (make-marker) (match-beginning 0)))))))))
  14598. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14599. "Find Org node headline HEADING in all .org files in directory DIR.
  14600. When the target headline is found, return a marker to this location."
  14601. (let ((files (directory-files (or dir default-directory)
  14602. nil "\\`[^.#].*\\.org\\'"))
  14603. file visiting m buffer)
  14604. (catch 'found
  14605. (while (setq file (pop files))
  14606. (message "trying %s" file)
  14607. (setq visiting (org-find-base-buffer-visiting file))
  14608. (setq buffer (or visiting (find-file-noselect file)))
  14609. (setq m (org-find-exact-headline-in-buffer
  14610. heading buffer))
  14611. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14612. (and m (throw 'found m))))))
  14613. (defun org-find-entry-with-id (ident)
  14614. "Locate the entry that contains the ID property with exact value IDENT.
  14615. IDENT can be a string, a symbol or a number, this function will search for
  14616. the string representation of it.
  14617. Return the position where this entry starts, or nil if there is no such entry."
  14618. (interactive "sID: ")
  14619. (let ((id (cond
  14620. ((stringp ident) ident)
  14621. ((symbol-name ident) (symbol-name ident))
  14622. ((numberp ident) (number-to-string ident))
  14623. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14624. (case-fold-search nil))
  14625. (save-excursion
  14626. (save-restriction
  14627. (widen)
  14628. (goto-char (point-min))
  14629. (when (re-search-forward
  14630. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14631. nil t)
  14632. (org-back-to-heading t)
  14633. (point))))))
  14634. ;;;; Timestamps
  14635. (defvar org-last-changed-timestamp nil)
  14636. (defvar org-last-inserted-timestamp nil
  14637. "The last time stamp inserted with `org-insert-time-stamp'.")
  14638. (defvar org-time-was-given) ; dynamically scoped parameter
  14639. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14640. (defvar org-ts-what) ; dynamically scoped parameter
  14641. (defun org-time-stamp (arg &optional inactive)
  14642. "Prompt for a date/time and insert a time stamp.
  14643. If the user specifies a time like HH:MM or if this command is
  14644. called with at least one prefix argument, the time stamp contains
  14645. the date and the time. Otherwise, only the date is be included.
  14646. All parts of a date not specified by the user is filled in from
  14647. the current date/time. So if you just press return without
  14648. typing anything, the time stamp will represent the current
  14649. date/time.
  14650. If there is already a timestamp at the cursor, it will be
  14651. modified.
  14652. With two universal prefix arguments, insert an active timestamp
  14653. with the current time without prompting the user.
  14654. When called from lisp, the timestamp is inactive if INACTIVE is
  14655. non-nil."
  14656. (interactive "P")
  14657. (let* ((ts nil)
  14658. (default-time
  14659. ;; Default time is either today, or, when entering a range,
  14660. ;; the range start.
  14661. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14662. (save-excursion
  14663. (re-search-backward
  14664. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14665. (- (point) 20) t)))
  14666. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14667. (current-time)))
  14668. (default-input (and ts (org-get-compact-tod ts)))
  14669. (repeater (save-excursion
  14670. (save-match-data
  14671. (beginning-of-line)
  14672. (when (re-search-forward
  14673. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14674. (save-excursion (progn (end-of-line) (point))) t)
  14675. (match-string 0)))))
  14676. org-time-was-given org-end-time-was-given time)
  14677. (cond
  14678. ((and (org-at-timestamp-p t)
  14679. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14680. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14681. (insert "--")
  14682. (setq time (let ((this-command this-command))
  14683. (org-read-date arg 'totime nil nil
  14684. default-time default-input inactive)))
  14685. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14686. ((org-at-timestamp-p t)
  14687. (setq time (let ((this-command this-command))
  14688. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14689. (when (org-at-timestamp-p t) ; just to get the match data
  14690. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14691. (replace-match "")
  14692. (setq org-last-changed-timestamp
  14693. (org-insert-time-stamp
  14694. time (or org-time-was-given arg)
  14695. inactive nil nil (list org-end-time-was-given)))
  14696. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14697. (setq org-last-changed-timestamp
  14698. (concat (substring org-last-inserted-timestamp 0 -1)
  14699. " " repeater ">"))))
  14700. (message "Timestamp updated"))
  14701. ((equal arg '(16))
  14702. (org-insert-time-stamp (current-time) t inactive))
  14703. (t
  14704. (setq time (let ((this-command this-command))
  14705. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14706. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14707. nil nil (list org-end-time-was-given))))))
  14708. ;; FIXME: can we use this for something else, like computing time differences?
  14709. (defun org-get-compact-tod (s)
  14710. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14711. (let* ((t1 (match-string 1 s))
  14712. (h1 (string-to-number (match-string 2 s)))
  14713. (m1 (string-to-number (match-string 3 s)))
  14714. (t2 (and (match-end 4) (match-string 5 s)))
  14715. (h2 (and t2 (string-to-number (match-string 6 s))))
  14716. (m2 (and t2 (string-to-number (match-string 7 s))))
  14717. dh dm)
  14718. (if (not t2)
  14719. t1
  14720. (setq dh (- h2 h1) dm (- m2 m1))
  14721. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14722. (concat t1 "+" (number-to-string dh)
  14723. (and (/= 0 dm) (format ":%02d" dm)))))))
  14724. (defun org-time-stamp-inactive (&optional arg)
  14725. "Insert an inactive time stamp.
  14726. An inactive time stamp is enclosed in square brackets instead of angle
  14727. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14728. does not link to the calendar and cannot be changed with the S-cursor keys.
  14729. So these are more for recording a certain time/date."
  14730. (interactive "P")
  14731. (org-time-stamp arg 'inactive))
  14732. (defvar org-date-ovl (make-overlay 1 1))
  14733. (overlay-put org-date-ovl 'face 'org-date-selected)
  14734. (org-detach-overlay org-date-ovl)
  14735. (defvar org-ans1) ; dynamically scoped parameter
  14736. (defvar org-ans2) ; dynamically scoped parameter
  14737. (defvar org-plain-time-of-day-regexp) ; defined below
  14738. (defvar org-overriding-default-time nil) ; dynamically scoped
  14739. (defvar org-read-date-overlay nil)
  14740. (defvar org-dcst nil) ; dynamically scoped
  14741. (defvar org-read-date-history nil)
  14742. (defvar org-read-date-final-answer nil)
  14743. (defvar org-read-date-analyze-futurep nil)
  14744. (defvar org-read-date-analyze-forced-year nil)
  14745. (defvar org-read-date-inactive)
  14746. (defvar org-read-date-minibuffer-local-map
  14747. (let* ((org-replace-disputed-keys nil)
  14748. (map (make-sparse-keymap)))
  14749. (set-keymap-parent map minibuffer-local-map)
  14750. (org-defkey map (kbd ".")
  14751. (lambda () (interactive)
  14752. ;; Are we at the beginning of the prompt?
  14753. (if (looking-back "^[^:]+: ")
  14754. (org-eval-in-calendar '(calendar-goto-today))
  14755. (insert "."))))
  14756. (org-defkey map (kbd "C-.")
  14757. (lambda () (interactive)
  14758. (org-eval-in-calendar '(calendar-goto-today))))
  14759. (org-defkey map [(meta shift left)]
  14760. (lambda () (interactive)
  14761. (org-eval-in-calendar '(calendar-backward-month 1))))
  14762. (org-defkey map [(meta shift right)]
  14763. (lambda () (interactive)
  14764. (org-eval-in-calendar '(calendar-forward-month 1))))
  14765. (org-defkey map [(meta shift up)]
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar '(calendar-backward-year 1))))
  14768. (org-defkey map [(meta shift down)]
  14769. (lambda () (interactive)
  14770. (org-eval-in-calendar '(calendar-forward-year 1))))
  14771. (org-defkey map [?\e (shift left)]
  14772. (lambda () (interactive)
  14773. (org-eval-in-calendar '(calendar-backward-month 1))))
  14774. (org-defkey map [?\e (shift right)]
  14775. (lambda () (interactive)
  14776. (org-eval-in-calendar '(calendar-forward-month 1))))
  14777. (org-defkey map [?\e (shift up)]
  14778. (lambda () (interactive)
  14779. (org-eval-in-calendar '(calendar-backward-year 1))))
  14780. (org-defkey map [?\e (shift down)]
  14781. (lambda () (interactive)
  14782. (org-eval-in-calendar '(calendar-forward-year 1))))
  14783. (org-defkey map [(shift up)]
  14784. (lambda () (interactive)
  14785. (org-eval-in-calendar '(calendar-backward-week 1))))
  14786. (org-defkey map [(shift down)]
  14787. (lambda () (interactive)
  14788. (org-eval-in-calendar '(calendar-forward-week 1))))
  14789. (org-defkey map [(shift left)]
  14790. (lambda () (interactive)
  14791. (org-eval-in-calendar '(calendar-backward-day 1))))
  14792. (org-defkey map [(shift right)]
  14793. (lambda () (interactive)
  14794. (org-eval-in-calendar '(calendar-forward-day 1))))
  14795. (org-defkey map "!"
  14796. (lambda () (interactive)
  14797. (org-eval-in-calendar '(diary-view-entries))
  14798. (message "")))
  14799. (org-defkey map ">"
  14800. (lambda () (interactive)
  14801. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14802. (org-defkey map "<"
  14803. (lambda () (interactive)
  14804. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14805. (org-defkey map "\C-v"
  14806. (lambda () (interactive)
  14807. (org-eval-in-calendar
  14808. '(calendar-scroll-left-three-months 1))))
  14809. (org-defkey map "\M-v"
  14810. (lambda () (interactive)
  14811. (org-eval-in-calendar
  14812. '(calendar-scroll-right-three-months 1))))
  14813. map)
  14814. "Keymap for minibuffer commands when using `org-read-date'.")
  14815. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14816. default-time default-input inactive)
  14817. "Read a date, possibly a time, and make things smooth for the user.
  14818. The prompt will suggest to enter an ISO date, but you can also enter anything
  14819. which will at least partially be understood by `parse-time-string'.
  14820. Unrecognized parts of the date will default to the current day, month, year,
  14821. hour and minute. If this command is called to replace a timestamp at point,
  14822. or to enter the second timestamp of a range, the default time is taken
  14823. from the existing stamp. Furthermore, the command prefers the future,
  14824. so if you are giving a date where the year is not given, and the day-month
  14825. combination is already past in the current year, it will assume you
  14826. mean next year. For details, see the manual. A few examples:
  14827. 3-2-5 --> 2003-02-05
  14828. feb 15 --> currentyear-02-15
  14829. 2/15 --> currentyear-02-15
  14830. sep 12 9 --> 2009-09-12
  14831. 12:45 --> today 12:45
  14832. 22 sept 0:34 --> currentyear-09-22 0:34
  14833. 12 --> currentyear-currentmonth-12
  14834. Fri --> nearest Friday after today
  14835. -Tue --> last Tuesday
  14836. etc.
  14837. Furthermore you can specify a relative date by giving, as the *first* thing
  14838. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14839. change in days weeks, months, years.
  14840. With a single plus or minus, the date is relative to today. With a double
  14841. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14842. +4d --> four days from today
  14843. +4 --> same as above
  14844. +2w --> two weeks from today
  14845. ++5 --> five days from default date
  14846. The function understands only English month and weekday abbreviations.
  14847. While prompting, a calendar is popped up - you can also select the
  14848. date with the mouse (button 1). The calendar shows a period of three
  14849. months. To scroll it to other months, use the keys `>' and `<'.
  14850. If you don't like the calendar, turn it off with
  14851. \(setq org-read-date-popup-calendar nil)
  14852. With optional argument TO-TIME, the date will immediately be converted
  14853. to an internal time.
  14854. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14855. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14856. still enter a time, and this function will inform the calling routine
  14857. about this change. The calling routine may then choose to change the
  14858. format used to insert the time stamp into the buffer to include the time.
  14859. With optional argument FROM-STRING, read from this string instead from
  14860. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14861. the time/date that is used for everything that is not specified by the
  14862. user."
  14863. (require 'parse-time)
  14864. (let* ((org-time-stamp-rounding-minutes
  14865. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14866. (org-dcst org-display-custom-times)
  14867. (ct (org-current-time))
  14868. (org-def (or org-overriding-default-time default-time ct))
  14869. (org-defdecode (decode-time org-def))
  14870. (dummy (progn
  14871. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14872. (setcar (nthcdr 2 org-defdecode) -1)
  14873. (setcar (nthcdr 1 org-defdecode) 59)
  14874. (setq org-def (apply 'encode-time org-defdecode)
  14875. org-defdecode (decode-time org-def)))))
  14876. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14877. (calendar-frame-setup nil)
  14878. (calendar-setup nil)
  14879. (calendar-move-hook nil)
  14880. (calendar-view-diary-initially-flag nil)
  14881. (calendar-view-holidays-initially-flag nil)
  14882. (timestr (format-time-string
  14883. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14884. (prompt (concat (if prompt (concat prompt " ") "")
  14885. (format "Date+time [%s]: " timestr)))
  14886. ans (org-ans0 "") org-ans1 org-ans2 final)
  14887. (cond
  14888. (from-string (setq ans from-string))
  14889. (org-read-date-popup-calendar
  14890. (save-excursion
  14891. (save-window-excursion
  14892. (calendar)
  14893. (org-eval-in-calendar '(setq cursor-type nil) t)
  14894. (unwind-protect
  14895. (progn
  14896. (calendar-forward-day (- (time-to-days org-def)
  14897. (calendar-absolute-from-gregorian
  14898. (calendar-current-date))))
  14899. (org-eval-in-calendar nil t)
  14900. (let* ((old-map (current-local-map))
  14901. (map (copy-keymap calendar-mode-map))
  14902. (minibuffer-local-map
  14903. (copy-keymap org-read-date-minibuffer-local-map)))
  14904. (org-defkey map (kbd "RET") 'org-calendar-select)
  14905. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14906. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14907. (unwind-protect
  14908. (progn
  14909. (use-local-map map)
  14910. (setq org-read-date-inactive inactive)
  14911. (add-hook 'post-command-hook 'org-read-date-display)
  14912. (setq org-ans0 (read-string prompt default-input
  14913. 'org-read-date-history nil))
  14914. ;; org-ans0: from prompt
  14915. ;; org-ans1: from mouse click
  14916. ;; org-ans2: from calendar motion
  14917. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14918. (remove-hook 'post-command-hook 'org-read-date-display)
  14919. (use-local-map old-map)
  14920. (when org-read-date-overlay
  14921. (delete-overlay org-read-date-overlay)
  14922. (setq org-read-date-overlay nil)))))
  14923. (bury-buffer "*Calendar*")))))
  14924. (t ; Naked prompt only
  14925. (unwind-protect
  14926. (setq ans (read-string prompt default-input
  14927. 'org-read-date-history timestr))
  14928. (when org-read-date-overlay
  14929. (delete-overlay org-read-date-overlay)
  14930. (setq org-read-date-overlay nil)))))
  14931. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14932. (when org-read-date-analyze-forced-year
  14933. (message "Year was forced into %s"
  14934. (if org-read-date-force-compatible-dates
  14935. "compatible range (1970-2037)"
  14936. "range representable on this machine"))
  14937. (ding))
  14938. ;; One round trip to get rid of 34th of August and stuff like that....
  14939. (setq final (decode-time (apply 'encode-time final)))
  14940. (setq org-read-date-final-answer ans)
  14941. (if to-time
  14942. (apply 'encode-time final)
  14943. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14944. (format "%04d-%02d-%02d %02d:%02d"
  14945. (nth 5 final) (nth 4 final) (nth 3 final)
  14946. (nth 2 final) (nth 1 final))
  14947. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14948. (defvar org-def)
  14949. (defvar org-defdecode)
  14950. (defvar org-with-time)
  14951. (defun org-read-date-display ()
  14952. "Display the current date prompt interpretation in the minibuffer."
  14953. (when org-read-date-display-live
  14954. (when org-read-date-overlay
  14955. (delete-overlay org-read-date-overlay))
  14956. (when (minibufferp (current-buffer))
  14957. (save-excursion
  14958. (end-of-line 1)
  14959. (while (not (equal (buffer-substring
  14960. (max (point-min) (- (point) 4)) (point))
  14961. " "))
  14962. (insert " ")))
  14963. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14964. " " (or org-ans1 org-ans2)))
  14965. (org-end-time-was-given nil)
  14966. (f (org-read-date-analyze ans org-def org-defdecode))
  14967. (fmts (if org-dcst
  14968. org-time-stamp-custom-formats
  14969. org-time-stamp-formats))
  14970. (fmt (if (or org-with-time
  14971. (and (boundp 'org-time-was-given) org-time-was-given))
  14972. (cdr fmts)
  14973. (car fmts)))
  14974. (txt (format-time-string fmt (apply 'encode-time f)))
  14975. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14976. (txt (concat "=> " txt)))
  14977. (when (and org-end-time-was-given
  14978. (string-match org-plain-time-of-day-regexp txt))
  14979. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14980. org-end-time-was-given
  14981. (substring txt (match-end 0)))))
  14982. (when org-read-date-analyze-futurep
  14983. (setq txt (concat txt " (=>F)")))
  14984. (setq org-read-date-overlay
  14985. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14986. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14987. (defun org-read-date-analyze (ans org-def org-defdecode)
  14988. "Analyze the combined answer of the date prompt."
  14989. ;; FIXME: cleanup and comment
  14990. (let ((nowdecode (decode-time (current-time)))
  14991. delta deltan deltaw deltadef year month day
  14992. hour minute second wday pm h2 m2 tl wday1
  14993. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14994. (setq org-read-date-analyze-futurep nil
  14995. org-read-date-analyze-forced-year nil)
  14996. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14997. (setq ans "+0"))
  14998. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14999. (setq ans (replace-match "" t t ans)
  15000. deltan (car delta)
  15001. deltaw (nth 1 delta)
  15002. deltadef (nth 2 delta)))
  15003. ;; Check if there is an iso week date in there. If yes, store the
  15004. ;; info and postpone interpreting it until the rest of the parsing
  15005. ;; is done.
  15006. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15007. (setq iso-year (if (match-end 1)
  15008. (org-small-year-to-year
  15009. (string-to-number (match-string 1 ans))))
  15010. iso-weekday (if (match-end 3)
  15011. (string-to-number (match-string 3 ans)))
  15012. iso-week (string-to-number (match-string 2 ans)))
  15013. (setq ans (replace-match "" t t ans)))
  15014. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15015. (when (string-match
  15016. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15017. (setq year (if (match-end 2)
  15018. (string-to-number (match-string 2 ans))
  15019. (progn (setq kill-year t)
  15020. (string-to-number (format-time-string "%Y"))))
  15021. month (string-to-number (match-string 3 ans))
  15022. day (string-to-number (match-string 4 ans)))
  15023. (if (< year 100) (setq year (+ 2000 year)))
  15024. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15025. t nil ans)))
  15026. ;; Help matching dotted european dates
  15027. (when (string-match
  15028. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15029. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15030. (setq kill-year t)
  15031. (string-to-number (format-time-string "%Y")))
  15032. day (string-to-number (match-string 1 ans))
  15033. month (string-to-number (match-string 2 ans))
  15034. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15035. t nil ans)))
  15036. ;; Help matching american dates, like 5/30 or 5/30/7
  15037. (when (string-match
  15038. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15039. (setq year (if (match-end 4)
  15040. (string-to-number (match-string 4 ans))
  15041. (progn (setq kill-year t)
  15042. (string-to-number (format-time-string "%Y"))))
  15043. month (string-to-number (match-string 1 ans))
  15044. day (string-to-number (match-string 2 ans)))
  15045. (if (< year 100) (setq year (+ 2000 year)))
  15046. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15047. t nil ans)))
  15048. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15049. ;; If there is a time with am/pm, and *no* time without it, we convert
  15050. ;; so that matching will be successful.
  15051. (loop for i from 1 to 2 do ; twice, for end time as well
  15052. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15053. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15054. (setq hour (string-to-number (match-string 1 ans))
  15055. minute (if (match-end 3)
  15056. (string-to-number (match-string 3 ans))
  15057. 0)
  15058. pm (equal ?p
  15059. (string-to-char (downcase (match-string 4 ans)))))
  15060. (if (and (= hour 12) (not pm))
  15061. (setq hour 0)
  15062. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15063. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15064. t t ans))))
  15065. ;; Check if a time range is given as a duration
  15066. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15067. (setq hour (string-to-number (match-string 1 ans))
  15068. h2 (+ hour (string-to-number (match-string 3 ans)))
  15069. minute (string-to-number (match-string 2 ans))
  15070. m2 (+ minute (if (match-end 5) (string-to-number
  15071. (match-string 5 ans))0)))
  15072. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15073. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15074. t t ans)))
  15075. ;; Check if there is a time range
  15076. (when (boundp 'org-end-time-was-given)
  15077. (setq org-time-was-given nil)
  15078. (when (and (string-match org-plain-time-of-day-regexp ans)
  15079. (match-end 8))
  15080. (setq org-end-time-was-given (match-string 8 ans))
  15081. (setq ans (concat (substring ans 0 (match-beginning 7))
  15082. (substring ans (match-end 7))))))
  15083. (setq tl (parse-time-string ans)
  15084. day (or (nth 3 tl) (nth 3 org-defdecode))
  15085. month (or (nth 4 tl)
  15086. (if (and org-read-date-prefer-future
  15087. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15088. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15089. (nth 4 org-defdecode)))
  15090. year (or (and (not kill-year) (nth 5 tl))
  15091. (if (and org-read-date-prefer-future
  15092. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15093. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15094. (nth 5 org-defdecode)))
  15095. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15096. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15097. second (or (nth 0 tl) 0)
  15098. wday (nth 6 tl))
  15099. (when (and (eq org-read-date-prefer-future 'time)
  15100. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15101. (equal day (nth 3 nowdecode))
  15102. (equal month (nth 4 nowdecode))
  15103. (equal year (nth 5 nowdecode))
  15104. (nth 2 tl)
  15105. (or (< (nth 2 tl) (nth 2 nowdecode))
  15106. (and (= (nth 2 tl) (nth 2 nowdecode))
  15107. (nth 1 tl)
  15108. (< (nth 1 tl) (nth 1 nowdecode)))))
  15109. (setq day (1+ day)
  15110. futurep t))
  15111. ;; Special date definitions below
  15112. (cond
  15113. (iso-week
  15114. ;; There was an iso week
  15115. (require 'cal-iso)
  15116. (setq futurep nil)
  15117. (setq year (or iso-year year)
  15118. day (or iso-weekday wday 1)
  15119. wday nil ; to make sure that the trigger below does not match
  15120. iso-date (calendar-gregorian-from-absolute
  15121. (calendar-absolute-from-iso
  15122. (list iso-week day year))))
  15123. ; FIXME: Should we also push ISO weeks into the future?
  15124. ; (when (and org-read-date-prefer-future
  15125. ; (not iso-year)
  15126. ; (< (calendar-absolute-from-gregorian iso-date)
  15127. ; (time-to-days (current-time))))
  15128. ; (setq year (1+ year)
  15129. ; iso-date (calendar-gregorian-from-absolute
  15130. ; (calendar-absolute-from-iso
  15131. ; (list iso-week day year)))))
  15132. (setq month (car iso-date)
  15133. year (nth 2 iso-date)
  15134. day (nth 1 iso-date)))
  15135. (deltan
  15136. (setq futurep nil)
  15137. (unless deltadef
  15138. (let ((now (decode-time (current-time))))
  15139. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15140. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15141. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15142. ((equal deltaw "m") (setq month (+ month deltan)))
  15143. ((equal deltaw "y") (setq year (+ year deltan)))))
  15144. ((and wday (not (nth 3 tl)))
  15145. ;; Weekday was given, but no day, so pick that day in the week
  15146. ;; on or after the derived date.
  15147. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15148. (unless (equal wday wday1)
  15149. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15150. (if (and (boundp 'org-time-was-given)
  15151. (nth 2 tl))
  15152. (setq org-time-was-given t))
  15153. (if (< year 100) (setq year (+ 2000 year)))
  15154. ;; Check of the date is representable
  15155. (if org-read-date-force-compatible-dates
  15156. (progn
  15157. (if (< year 1970)
  15158. (setq year 1970 org-read-date-analyze-forced-year t))
  15159. (if (> year 2037)
  15160. (setq year 2037 org-read-date-analyze-forced-year t)))
  15161. (condition-case nil
  15162. (ignore (encode-time second minute hour day month year))
  15163. (error
  15164. (setq year (nth 5 org-defdecode))
  15165. (setq org-read-date-analyze-forced-year t))))
  15166. (setq org-read-date-analyze-futurep futurep)
  15167. (list second minute hour day month year)))
  15168. (defvar parse-time-weekdays)
  15169. (defun org-read-date-get-relative (s today default)
  15170. "Check string S for special relative date string.
  15171. TODAY and DEFAULT are internal times, for today and for a default.
  15172. Return shift list (N what def-flag)
  15173. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15174. N is the number of WHATs to shift.
  15175. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15176. the DEFAULT date rather than TODAY."
  15177. (require 'parse-time)
  15178. (when (and
  15179. (string-match
  15180. (concat
  15181. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15182. "\\([0-9]+\\)?"
  15183. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15184. "\\([ \t]\\|$\\)") s)
  15185. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15186. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15187. (string-to-char (substring (match-string 1 s) -1))
  15188. ?+))
  15189. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15190. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15191. (what (if (match-end 3) (match-string 3 s) "d"))
  15192. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15193. (date (if rel default today))
  15194. (wday (nth 6 (decode-time date)))
  15195. delta)
  15196. (if wday1
  15197. (progn
  15198. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15199. (if (= delta 0) (setq delta 7))
  15200. (if (= dir ?-)
  15201. (progn
  15202. (setq delta (- delta 7))
  15203. (if (= delta 0) (setq delta -7))))
  15204. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15205. (list delta "d" rel))
  15206. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15207. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15208. "Turn a user-specified date into the internal representation.
  15209. The internal representation needed by the calendar is (month day year).
  15210. This is a wrapper to handle the brain-dead convention in calendar that
  15211. user function argument order change dependent on argument order."
  15212. (if (boundp 'calendar-date-style)
  15213. (cond
  15214. ((eq calendar-date-style 'american)
  15215. (list arg1 arg2 arg3))
  15216. ((eq calendar-date-style 'european)
  15217. (list arg2 arg1 arg3))
  15218. ((eq calendar-date-style 'iso)
  15219. (list arg2 arg3 arg1)))
  15220. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15221. (if (org-bound-and-true-p european-calendar-style)
  15222. (list arg2 arg1 arg3)
  15223. (list arg1 arg2 arg3)))))
  15224. (defun org-eval-in-calendar (form &optional keepdate)
  15225. "Eval FORM in the calendar window and return to current window.
  15226. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15227. otherwise stick to the current value of `org-ans2'."
  15228. (let ((sf (selected-frame))
  15229. (sw (selected-window)))
  15230. (select-window (get-buffer-window "*Calendar*" t))
  15231. (eval form)
  15232. (when (and (not keepdate) (calendar-cursor-to-date))
  15233. (let* ((date (calendar-cursor-to-date))
  15234. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15235. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15236. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15237. (select-window sw)
  15238. (org-select-frame-set-input-focus sf)))
  15239. (defun org-calendar-select ()
  15240. "Return to `org-read-date' with the date currently selected.
  15241. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15242. (interactive)
  15243. (when (calendar-cursor-to-date)
  15244. (let* ((date (calendar-cursor-to-date))
  15245. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15246. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15247. (if (active-minibuffer-window) (exit-minibuffer))))
  15248. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15249. "Insert a date stamp for the date given by the internal TIME.
  15250. WITH-HM means use the stamp format that includes the time of the day.
  15251. INACTIVE means use square brackets instead of angular ones, so that the
  15252. stamp will not contribute to the agenda.
  15253. PRE and POST are optional strings to be inserted before and after the
  15254. stamp.
  15255. The command returns the inserted time stamp."
  15256. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15257. stamp)
  15258. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15259. (insert-before-markers (or pre ""))
  15260. (when (listp extra)
  15261. (setq extra (car extra))
  15262. (if (and (stringp extra)
  15263. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15264. (setq extra (format "-%02d:%02d"
  15265. (string-to-number (match-string 1 extra))
  15266. (string-to-number (match-string 2 extra))))
  15267. (setq extra nil)))
  15268. (when extra
  15269. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15270. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15271. (insert-before-markers (or post ""))
  15272. (setq org-last-inserted-timestamp stamp)))
  15273. (defun org-toggle-time-stamp-overlays ()
  15274. "Toggle the use of custom time stamp formats."
  15275. (interactive)
  15276. (setq org-display-custom-times (not org-display-custom-times))
  15277. (unless org-display-custom-times
  15278. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15279. (while (setq p (next-single-property-change p 'display))
  15280. (if (and (get-text-property p 'display)
  15281. (eq (get-text-property p 'face) 'org-date))
  15282. (remove-text-properties
  15283. p (setq p (next-single-property-change p 'display))
  15284. '(display t))))
  15285. (set-buffer-modified-p bmp)))
  15286. (if (featurep 'xemacs)
  15287. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15288. (org-restart-font-lock)
  15289. (setq org-table-may-need-update t)
  15290. (if org-display-custom-times
  15291. (message "Time stamps are overlaid with custom format")
  15292. (message "Time stamp overlays removed")))
  15293. (defun org-display-custom-time (beg end)
  15294. "Overlay modified time stamp format over timestamp between BEG and END."
  15295. (let* ((ts (buffer-substring beg end))
  15296. t1 w1 with-hm tf time str w2 (off 0))
  15297. (save-match-data
  15298. (setq t1 (org-parse-time-string ts t))
  15299. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15300. (setq off (- (match-end 0) (match-beginning 0)))))
  15301. (setq end (- end off))
  15302. (setq w1 (- end beg)
  15303. with-hm (and (nth 1 t1) (nth 2 t1))
  15304. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15305. time (org-fix-decoded-time t1)
  15306. str (org-add-props
  15307. (format-time-string
  15308. (substring tf 1 -1) (apply 'encode-time time))
  15309. nil 'mouse-face 'highlight)
  15310. w2 (length str))
  15311. (if (not (= w2 w1))
  15312. (add-text-properties (1+ beg) (+ 2 beg)
  15313. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15314. (if (featurep 'xemacs)
  15315. (progn
  15316. (put-text-property beg end 'invisible t)
  15317. (put-text-property beg end 'end-glyph (make-glyph str)))
  15318. (put-text-property beg end 'display str))))
  15319. (defun org-translate-time (string)
  15320. "Translate all timestamps in STRING to custom format.
  15321. But do this only if the variable `org-display-custom-times' is set."
  15322. (when org-display-custom-times
  15323. (save-match-data
  15324. (let* ((start 0)
  15325. (re org-ts-regexp-both)
  15326. t1 with-hm inactive tf time str beg end)
  15327. (while (setq start (string-match re string start))
  15328. (setq beg (match-beginning 0)
  15329. end (match-end 0)
  15330. t1 (save-match-data
  15331. (org-parse-time-string (substring string beg end) t))
  15332. with-hm (and (nth 1 t1) (nth 2 t1))
  15333. inactive (equal (substring string beg (1+ beg)) "[")
  15334. tf (funcall (if with-hm 'cdr 'car)
  15335. org-time-stamp-custom-formats)
  15336. time (org-fix-decoded-time t1)
  15337. str (format-time-string
  15338. (concat
  15339. (if inactive "[" "<") (substring tf 1 -1)
  15340. (if inactive "]" ">"))
  15341. (apply 'encode-time time))
  15342. string (replace-match str t t string)
  15343. start (+ start (length str)))))))
  15344. string)
  15345. (defun org-fix-decoded-time (time)
  15346. "Set 0 instead of nil for the first 6 elements of time.
  15347. Don't touch the rest."
  15348. (let ((n 0))
  15349. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15350. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15351. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15352. "Difference between TIMESTAMP-STRING and now in days.
  15353. If SECONDS is non-nil, return the difference in seconds."
  15354. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15355. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15356. (funcall fdiff (current-time)))))
  15357. (defun org-deadline-close (timestamp-string &optional ndays)
  15358. "Is the time in TIMESTAMP-STRING close to the current date?"
  15359. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15360. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15361. (not (org-entry-is-done-p))))
  15362. (defun org-get-wdays (ts &optional delay zero-delay)
  15363. "Get the deadline lead time appropriate for timestring TS.
  15364. When DELAY is non-nil, get the delay time for scheduled items
  15365. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15366. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15367. don't try to find the delay cookie in the scheduled timestamp."
  15368. (let ((tv (if delay org-scheduled-delay-days
  15369. org-deadline-warning-days)))
  15370. (cond
  15371. ((or (and delay (< tv 0))
  15372. (and delay zero-delay (<= tv 0))
  15373. (and (not delay) (<= tv 0)))
  15374. ;; Enforce this value no matter what
  15375. (- tv))
  15376. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15377. ;; lead time is specified.
  15378. (floor (* (string-to-number (match-string 1 ts))
  15379. (cdr (assoc (match-string 2 ts)
  15380. '(("d" . 1) ("w" . 7)
  15381. ("m" . 30.4) ("y" . 365.25)
  15382. ("h" . 0.041667)))))))
  15383. ;; go for the default.
  15384. (t tv))))
  15385. (defun org-calendar-select-mouse (ev)
  15386. "Return to `org-read-date' with the date currently selected.
  15387. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15388. (interactive "e")
  15389. (mouse-set-point ev)
  15390. (when (calendar-cursor-to-date)
  15391. (let* ((date (calendar-cursor-to-date))
  15392. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15393. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15394. (if (active-minibuffer-window) (exit-minibuffer))))
  15395. (defun org-check-deadlines (ndays)
  15396. "Check if there are any deadlines due or past due.
  15397. A deadline is considered due if it happens within `org-deadline-warning-days'
  15398. days from today's date. If the deadline appears in an entry marked DONE,
  15399. it is not shown. The prefix arg NDAYS can be used to test that many
  15400. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15401. (interactive "P")
  15402. (let* ((org-warn-days
  15403. (cond
  15404. ((equal ndays '(4)) 100000)
  15405. (ndays (prefix-numeric-value ndays))
  15406. (t (abs org-deadline-warning-days))))
  15407. (case-fold-search nil)
  15408. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15409. (callback
  15410. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15411. (message "%d deadlines past-due or due within %d days"
  15412. (org-occur regexp nil callback)
  15413. org-warn-days)))
  15414. (defsubst org-re-timestamp (type)
  15415. "Return a regexp for timestamp TYPE.
  15416. Allowed values for TYPE are:
  15417. all: all timestamps
  15418. active: only active timestamps (<...>)
  15419. inactive: only inactive timestamps ([...])
  15420. scheduled: only scheduled timestamps
  15421. deadline: only deadline timestamps
  15422. closed: only closed time-stamps
  15423. When TYPE is nil, fall back on returning a regexp that matches
  15424. both scheduled and deadline timestamps."
  15425. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15426. ((eq type 'active) org-ts-regexp)
  15427. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15428. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15429. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15430. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15431. ((eq type 'scheduled-or-deadline)
  15432. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15433. (defun org-check-before-date (date)
  15434. "Check if there are deadlines or scheduled entries before DATE."
  15435. (interactive (list (org-read-date)))
  15436. (let ((case-fold-search nil)
  15437. (regexp (org-re-timestamp org-ts-type))
  15438. (callback
  15439. (lambda () (time-less-p
  15440. (org-time-string-to-time (match-string 1))
  15441. (org-time-string-to-time date)))))
  15442. (message "%d entries before %s"
  15443. (org-occur regexp nil callback) date)))
  15444. (defun org-check-after-date (date)
  15445. "Check if there are deadlines or scheduled entries after DATE."
  15446. (interactive (list (org-read-date)))
  15447. (let ((case-fold-search nil)
  15448. (regexp (org-re-timestamp org-ts-type))
  15449. (callback
  15450. (lambda () (not
  15451. (time-less-p
  15452. (org-time-string-to-time (match-string 1))
  15453. (org-time-string-to-time date))))))
  15454. (message "%d entries after %s"
  15455. (org-occur regexp nil callback) date)))
  15456. (defun org-check-dates-range (start-date end-date)
  15457. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15458. (interactive (list (org-read-date nil nil nil "Range starts")
  15459. (org-read-date nil nil nil "Range end")))
  15460. (let ((case-fold-search nil)
  15461. (regexp (org-re-timestamp org-ts-type))
  15462. (callback
  15463. (lambda ()
  15464. (let ((match (match-string 1)))
  15465. (and
  15466. (not (time-less-p
  15467. (org-time-string-to-time match)
  15468. (org-time-string-to-time start-date)))
  15469. (time-less-p
  15470. (org-time-string-to-time match)
  15471. (org-time-string-to-time end-date)))))))
  15472. (message "%d entries between %s and %s"
  15473. (org-occur regexp nil callback) start-date end-date)))
  15474. (defun org-evaluate-time-range (&optional to-buffer)
  15475. "Evaluate a time range by computing the difference between start and end.
  15476. Normally the result is just printed in the echo area, but with prefix arg
  15477. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15478. If the time range is actually in a table, the result is inserted into the
  15479. next column.
  15480. For time difference computation, a year is assumed to be exactly 365
  15481. days in order to avoid rounding problems."
  15482. (interactive "P")
  15483. (or
  15484. (org-clock-update-time-maybe)
  15485. (save-excursion
  15486. (unless (org-at-date-range-p t)
  15487. (goto-char (point-at-bol))
  15488. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15489. (if (not (org-at-date-range-p t))
  15490. (user-error "Not at a time-stamp range, and none found in current line")))
  15491. (let* ((ts1 (match-string 1))
  15492. (ts2 (match-string 2))
  15493. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15494. (match-end (match-end 0))
  15495. (time1 (org-time-string-to-time ts1))
  15496. (time2 (org-time-string-to-time ts2))
  15497. (t1 (org-float-time time1))
  15498. (t2 (org-float-time time2))
  15499. (diff (abs (- t2 t1)))
  15500. (negative (< (- t2 t1) 0))
  15501. ;; (ys (floor (* 365 24 60 60)))
  15502. (ds (* 24 60 60))
  15503. (hs (* 60 60))
  15504. (fy "%dy %dd %02d:%02d")
  15505. (fy1 "%dy %dd")
  15506. (fd "%dd %02d:%02d")
  15507. (fd1 "%dd")
  15508. (fh "%02d:%02d")
  15509. y d h m align)
  15510. (if havetime
  15511. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15512. y 0
  15513. d (floor (/ diff ds)) diff (mod diff ds)
  15514. h (floor (/ diff hs)) diff (mod diff hs)
  15515. m (floor (/ diff 60)))
  15516. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15517. y 0
  15518. d (floor (+ (/ diff ds) 0.5))
  15519. h 0 m 0))
  15520. (if (not to-buffer)
  15521. (message "%s" (org-make-tdiff-string y d h m))
  15522. (if (org-at-table-p)
  15523. (progn
  15524. (goto-char match-end)
  15525. (setq align t)
  15526. (and (looking-at " *|") (goto-char (match-end 0))))
  15527. (goto-char match-end))
  15528. (if (looking-at
  15529. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15530. (replace-match ""))
  15531. (if negative (insert " -"))
  15532. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15533. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15534. (insert " " (format fh h m))))
  15535. (if align (org-table-align))
  15536. (message "Time difference inserted")))))
  15537. (defun org-make-tdiff-string (y d h m)
  15538. (let ((fmt "")
  15539. (l nil))
  15540. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15541. l (push y l)))
  15542. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15543. l (push d l)))
  15544. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15545. l (push h l)))
  15546. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15547. l (push m l)))
  15548. (apply 'format fmt (nreverse l))))
  15549. (defun org-time-string-to-time (s &optional buffer pos)
  15550. "Convert a timestamp string into internal time."
  15551. (condition-case errdata
  15552. (apply 'encode-time (org-parse-time-string s))
  15553. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15554. s (if (not (and buffer pos))
  15555. ""
  15556. (format " at %d in buffer `%s'" pos buffer))
  15557. (cdr errdata)))))
  15558. (defun org-time-string-to-seconds (s)
  15559. "Convert a timestamp string to a number of seconds."
  15560. (org-float-time (org-time-string-to-time s)))
  15561. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15562. "Convert a time stamp to an absolute day number.
  15563. If there is a specifier for a cyclic time stamp, get the closest
  15564. date to DAYNR.
  15565. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15566. The variable `date' is bound by the calendar when this is called."
  15567. (cond
  15568. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15569. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15570. daynr
  15571. (+ daynr 1000)))
  15572. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15573. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15574. (time-to-days (current-time))) (match-string 0 s)
  15575. prefer show-all))
  15576. (t (time-to-days
  15577. (condition-case errdata
  15578. (apply 'encode-time (org-parse-time-string s))
  15579. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15580. s (if (not (and buffer pos))
  15581. ""
  15582. (format " at %d in buffer `%s'" pos buffer))
  15583. (cdr errdata))))))))
  15584. (defun org-days-to-iso-week (days)
  15585. "Return the iso week number."
  15586. (require 'cal-iso)
  15587. (car (calendar-iso-from-absolute days)))
  15588. (defun org-small-year-to-year (year)
  15589. "Convert 2-digit years into 4-digit years.
  15590. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15591. The year 2000 cannot be abbreviated. Any year larger than 99
  15592. is returned unchanged."
  15593. (if (< year 38)
  15594. (setq year (+ 2000 year))
  15595. (if (< year 100)
  15596. (setq year (+ 1900 year))))
  15597. year)
  15598. (defun org-time-from-absolute (d)
  15599. "Return the time corresponding to date D.
  15600. D may be an absolute day number, or a calendar-type list (month day year)."
  15601. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15602. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15603. (defun org-calendar-holiday ()
  15604. "List of holidays, for Diary display in Org-mode."
  15605. (require 'holidays)
  15606. (let ((hl (funcall
  15607. (if (fboundp 'calendar-check-holidays)
  15608. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15609. (if hl (mapconcat 'identity hl "; "))))
  15610. (defun org-diary-sexp-entry (sexp entry date)
  15611. "Process a SEXP diary ENTRY for DATE."
  15612. (require 'diary-lib)
  15613. (let ((result (if calendar-debug-sexp
  15614. (let ((stack-trace-on-error t))
  15615. (eval (car (read-from-string sexp))))
  15616. (condition-case nil
  15617. (eval (car (read-from-string sexp)))
  15618. (error
  15619. (beep)
  15620. (message "Bad sexp at line %d in %s: %s"
  15621. (org-current-line)
  15622. (buffer-file-name) sexp)
  15623. (sleep-for 2))))))
  15624. (cond ((stringp result) (split-string result "; "))
  15625. ((and (consp result)
  15626. (not (consp (cdr result)))
  15627. (stringp (cdr result))) (cdr result))
  15628. ((and (consp result)
  15629. (stringp (car result))) result)
  15630. (result entry))))
  15631. (defun org-diary-to-ical-string (frombuf)
  15632. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15633. This uses the icalendar.el library."
  15634. (let* ((tmpdir (if (featurep 'xemacs)
  15635. (temp-directory)
  15636. temporary-file-directory))
  15637. (tmpfile (make-temp-name
  15638. (expand-file-name "orgics" tmpdir)))
  15639. buf rtn b e)
  15640. (with-current-buffer frombuf
  15641. (icalendar-export-region (point-min) (point-max) tmpfile)
  15642. (setq buf (find-buffer-visiting tmpfile))
  15643. (set-buffer buf)
  15644. (goto-char (point-min))
  15645. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15646. (setq b (match-beginning 0)))
  15647. (goto-char (point-max))
  15648. (if (re-search-backward "^END:VEVENT" nil t)
  15649. (setq e (match-end 0)))
  15650. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15651. (kill-buffer buf)
  15652. (delete-file tmpfile)
  15653. rtn))
  15654. (defun org-closest-date (start current change prefer show-all)
  15655. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15656. When PREFER is `past', return a date that is either CURRENT or past.
  15657. When PREFER is `future', return a date that is either CURRENT or future.
  15658. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15659. ;; Make the proper lists from the dates
  15660. (catch 'exit
  15661. (let ((a1 '(("h" . hour)
  15662. ("d" . day)
  15663. ("w" . week)
  15664. ("m" . month)
  15665. ("y" . year)))
  15666. (shour (nth 2 (org-parse-time-string start)))
  15667. dn dw sday cday n1 n2 n0
  15668. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15669. (setq start (org-date-to-gregorian start)
  15670. current (org-date-to-gregorian
  15671. (if show-all
  15672. current
  15673. (time-to-days (current-time))))
  15674. sday (calendar-absolute-from-gregorian start)
  15675. cday (calendar-absolute-from-gregorian current))
  15676. (if (<= cday sday) (throw 'exit sday))
  15677. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15678. (setq dn (string-to-number (match-string 1 change))
  15679. dw (cdr (assoc (match-string 2 change) a1)))
  15680. (user-error "Invalid change specifier: %s" change))
  15681. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15682. (cond
  15683. ((eq dw 'hour)
  15684. (let ((missing-hours
  15685. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15686. dn)))
  15687. (setq n1 (if (zerop missing-hours) cday
  15688. (- cday (1+ (floor (/ missing-hours 24)))))
  15689. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15690. ((eq dw 'day)
  15691. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15692. n2 (+ n1 dn)))
  15693. ((eq dw 'year)
  15694. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15695. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15696. (setq date1 (list m d y1)
  15697. n1 (calendar-absolute-from-gregorian date1)
  15698. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15699. n2 (calendar-absolute-from-gregorian date2)))
  15700. ((eq dw 'month)
  15701. ;; approx number of month between the two dates
  15702. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15703. ;; How often does dn fit in there?
  15704. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15705. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15706. m (+ m nm)
  15707. ny (floor (/ m 12))
  15708. y (+ y ny)
  15709. m (- m (* ny 12)))
  15710. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15711. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15712. (setq m2 (+ m dn) y2 y)
  15713. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15714. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15715. (while (<= n2 cday)
  15716. (setq n1 n2 m m2 y y2)
  15717. (setq m2 (+ m dn) y2 y)
  15718. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15719. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15720. ;; Make sure n1 is the earlier date
  15721. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15722. (if show-all
  15723. (cond
  15724. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15725. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15726. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15727. (cond
  15728. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15729. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15730. (t (if (= cday n1) n1 n2)))))))
  15731. (defun org-date-to-gregorian (date)
  15732. "Turn any specification of DATE into a Gregorian date for the calendar."
  15733. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15734. ((and (listp date) (= (length date) 3)) date)
  15735. ((stringp date)
  15736. (setq date (org-parse-time-string date))
  15737. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15738. ((listp date)
  15739. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15740. (defun org-parse-time-string (s &optional nodefault)
  15741. "Parse the standard Org-mode time string.
  15742. This should be a lot faster than the normal `parse-time-string'.
  15743. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15744. hour and minute fields will be nil if not given."
  15745. (cond ((string-match org-ts-regexp0 s)
  15746. (list 0
  15747. (if (or (match-beginning 8) (not nodefault))
  15748. (string-to-number (or (match-string 8 s) "0")))
  15749. (if (or (match-beginning 7) (not nodefault))
  15750. (string-to-number (or (match-string 7 s) "0")))
  15751. (string-to-number (match-string 4 s))
  15752. (string-to-number (match-string 3 s))
  15753. (string-to-number (match-string 2 s))
  15754. nil nil nil))
  15755. ((string-match "^<[^>]+>$" s)
  15756. (decode-time (seconds-to-time (org-matcher-time s))))
  15757. (t (error "Not a standard Org-mode time string: %s" s))))
  15758. (defun org-timestamp-up (&optional arg)
  15759. "Increase the date item at the cursor by one.
  15760. If the cursor is on the year, change the year. If it is on the month,
  15761. the day or the time, change that.
  15762. With prefix ARG, change by that many units."
  15763. (interactive "p")
  15764. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15765. (defun org-timestamp-down (&optional arg)
  15766. "Decrease the date item at the cursor by one.
  15767. If the cursor is on the year, change the year. If it is on the month,
  15768. the day or the time, change that.
  15769. With prefix ARG, change by that many units."
  15770. (interactive "p")
  15771. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15772. (defun org-timestamp-up-day (&optional arg)
  15773. "Increase the date in the time stamp by one day.
  15774. With prefix ARG, change that many days."
  15775. (interactive "p")
  15776. (if (and (not (org-at-timestamp-p t))
  15777. (org-at-heading-p))
  15778. (org-todo 'up)
  15779. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15780. (defun org-timestamp-down-day (&optional arg)
  15781. "Decrease the date in the time stamp by one day.
  15782. With prefix ARG, change that many days."
  15783. (interactive "p")
  15784. (if (and (not (org-at-timestamp-p t))
  15785. (org-at-heading-p))
  15786. (org-todo 'down)
  15787. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15788. (defun org-at-timestamp-p (&optional inactive-ok)
  15789. "Determine if the cursor is in or at a timestamp."
  15790. (interactive)
  15791. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15792. (pos (point))
  15793. (ans (or (looking-at tsr)
  15794. (save-excursion
  15795. (skip-chars-backward "^[<\n\r\t")
  15796. (if (> (point) (point-min)) (backward-char 1))
  15797. (and (looking-at tsr)
  15798. (> (- (match-end 0) pos) -1))))))
  15799. (and ans
  15800. (boundp 'org-ts-what)
  15801. (setq org-ts-what
  15802. (cond
  15803. ((= pos (match-beginning 0)) 'bracket)
  15804. ;; Point is considered to be "on the bracket" whether
  15805. ;; it's really on it or right after it.
  15806. ((= pos (1- (match-end 0))) 'bracket)
  15807. ((= pos (match-end 0)) 'after)
  15808. ((org-pos-in-match-range pos 2) 'year)
  15809. ((org-pos-in-match-range pos 3) 'month)
  15810. ((org-pos-in-match-range pos 7) 'hour)
  15811. ((org-pos-in-match-range pos 8) 'minute)
  15812. ((or (org-pos-in-match-range pos 4)
  15813. (org-pos-in-match-range pos 5)) 'day)
  15814. ((and (> pos (or (match-end 8) (match-end 5)))
  15815. (< pos (match-end 0)))
  15816. (- pos (or (match-end 8) (match-end 5))))
  15817. (t 'day))))
  15818. ans))
  15819. (defun org-toggle-timestamp-type ()
  15820. "Toggle the type (<active> or [inactive]) of a time stamp."
  15821. (interactive)
  15822. (when (org-at-timestamp-p t)
  15823. (let ((beg (match-beginning 0)) (end (match-end 0))
  15824. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15825. (save-excursion
  15826. (goto-char beg)
  15827. (while (re-search-forward "[][<>]" end t)
  15828. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15829. t t)))
  15830. (message "Timestamp is now %sactive"
  15831. (if (equal (char-after beg) ?<) "" "in")))))
  15832. (defun org-at-clock-log-p nil
  15833. "Is the cursor on the clock log line?"
  15834. (save-excursion
  15835. (move-beginning-of-line 1)
  15836. (looking-at "^[ \t]*CLOCK:")))
  15837. (defvar org-clock-history) ; defined in org-clock.el
  15838. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15839. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15840. "Change the date in the time stamp at point.
  15841. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15842. `year', `minute', `second'. If WHAT is not given, the cursor position
  15843. in the timestamp determines what will be changed.
  15844. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15845. (let ((origin (point)) origin-cat
  15846. with-hm inactive
  15847. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15848. org-ts-what
  15849. extra rem
  15850. ts time time0 fixnext clrgx)
  15851. (if (not (org-at-timestamp-p t))
  15852. (user-error "Not at a timestamp"))
  15853. (if (and (not what) (eq org-ts-what 'bracket))
  15854. (org-toggle-timestamp-type)
  15855. ;; Point isn't on brackets. Remember the part of the time-stamp
  15856. ;; the point was in. Indeed, size of time-stamps may change,
  15857. ;; but point must be kept in the same category nonetheless.
  15858. (setq origin-cat org-ts-what)
  15859. (if (and (not what) (not (eq org-ts-what 'day))
  15860. org-display-custom-times
  15861. (get-text-property (point) 'display)
  15862. (not (get-text-property (1- (point)) 'display)))
  15863. (setq org-ts-what 'day))
  15864. (setq org-ts-what (or what org-ts-what)
  15865. inactive (= (char-after (match-beginning 0)) ?\[)
  15866. ts (match-string 0))
  15867. (replace-match "")
  15868. (when (string-match
  15869. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15870. ts)
  15871. (setq extra (match-string 1 ts))
  15872. (if suppress-tmp-delay
  15873. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15874. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15875. (setq with-hm t))
  15876. (setq time0 (org-parse-time-string ts))
  15877. (when (and updown
  15878. (eq org-ts-what 'minute)
  15879. (not current-prefix-arg))
  15880. ;; This looks like s-up and s-down. Change by one rounding step.
  15881. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15882. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15883. (setcar (cdr time0) (+ (nth 1 time0)
  15884. (if (> n 0) (- rem) (- dm rem))))))
  15885. (setq time
  15886. (encode-time (or (car time0) 0)
  15887. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15888. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15889. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15890. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15891. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15892. (nthcdr 6 time0)))
  15893. (when (and (member org-ts-what '(hour minute))
  15894. extra
  15895. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15896. (setq extra (org-modify-ts-extra
  15897. extra
  15898. (if (eq org-ts-what 'hour) 2 5)
  15899. n dm)))
  15900. (when (integerp org-ts-what)
  15901. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15902. (if (eq what 'calendar)
  15903. (let ((cal-date (org-get-date-from-calendar)))
  15904. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15905. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15906. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15907. (setcar time0 (or (car time0) 0))
  15908. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15909. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15910. (setq time (apply 'encode-time time0))))
  15911. ;; Insert the new time-stamp, and ensure point stays in the same
  15912. ;; category as before (i.e. not after the last position in that
  15913. ;; category).
  15914. (let ((pos (point)))
  15915. ;; Stay before inserted string. `save-excursion' is of no use.
  15916. (setq org-last-changed-timestamp
  15917. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15918. (goto-char pos))
  15919. (save-match-data
  15920. (looking-at org-ts-regexp3)
  15921. (goto-char (cond
  15922. ;; `day' category ends before `hour' if any, or at
  15923. ;; the end of the day name.
  15924. ((eq origin-cat 'day)
  15925. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15926. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15927. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15928. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15929. ;; `year' and `month' have both fixed size: point
  15930. ;; couldn't have moved into another part.
  15931. (t origin))))
  15932. ;; Update clock if on a CLOCK line.
  15933. (org-clock-update-time-maybe)
  15934. ;; Maybe adjust the closest clock in `org-clock-history'
  15935. (when org-clock-adjust-closest
  15936. (if (not (and (org-at-clock-log-p)
  15937. (< 1 (length (delq nil (mapcar 'marker-position
  15938. org-clock-history))))))
  15939. (message "No clock to adjust")
  15940. (cond ((save-excursion ; fix previous clock?
  15941. (re-search-backward org-ts-regexp0 nil t)
  15942. (org-looking-back (concat org-clock-string " \\[")))
  15943. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15944. ((save-excursion ; fix next clock?
  15945. (re-search-backward org-ts-regexp0 nil t)
  15946. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15947. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15948. (save-window-excursion
  15949. ;; Find closest clock to point, adjust the previous/next one in history
  15950. (let* ((p (save-excursion (org-back-to-heading t)))
  15951. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15952. (clfixnth
  15953. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15954. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15955. (if (not clfixpos)
  15956. (message "No clock to adjust")
  15957. (save-excursion
  15958. (org-goto-marker-or-bmk clfixpos)
  15959. (org-show-subtree)
  15960. (when (re-search-forward clrgx nil t)
  15961. (goto-char (match-beginning 1))
  15962. (let (org-clock-adjust-closest)
  15963. (org-timestamp-change n org-ts-what updown))
  15964. (message "Clock adjusted in %s for heading: %s"
  15965. (file-name-nondirectory (buffer-file-name))
  15966. (org-get-heading t t)))))))))
  15967. ;; Try to recenter the calendar window, if any.
  15968. (if (and org-calendar-follow-timestamp-change
  15969. (get-buffer-window "*Calendar*" t)
  15970. (memq org-ts-what '(day month year)))
  15971. (org-recenter-calendar (time-to-days time))))))
  15972. (defun org-modify-ts-extra (s pos n dm)
  15973. "Change the different parts of the lead-time and repeat fields in timestamp."
  15974. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15975. ng h m new rem)
  15976. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15977. (cond
  15978. ((or (org-pos-in-match-range pos 2)
  15979. (org-pos-in-match-range pos 3))
  15980. (setq m (string-to-number (match-string 3 s))
  15981. h (string-to-number (match-string 2 s)))
  15982. (if (org-pos-in-match-range pos 2)
  15983. (setq h (+ h n))
  15984. (setq n (* dm (org-no-warnings (signum n))))
  15985. (when (not (= 0 (setq rem (% m dm))))
  15986. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15987. (setq m (+ m n)))
  15988. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15989. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15990. (setq h (min 24 (max 0 h)))
  15991. (setq ng 1 new (format "-%02d:%02d" h m)))
  15992. ((org-pos-in-match-range pos 6)
  15993. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15994. ((org-pos-in-match-range pos 5)
  15995. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15996. ((org-pos-in-match-range pos 9)
  15997. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15998. ((org-pos-in-match-range pos 8)
  15999. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16000. (when ng
  16001. (setq s (concat
  16002. (substring s 0 (match-beginning ng))
  16003. new
  16004. (substring s (match-end ng))))))
  16005. s))
  16006. (defun org-recenter-calendar (date)
  16007. "If the calendar is visible, recenter it to DATE."
  16008. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16009. (when cwin
  16010. (let ((calendar-move-hook nil))
  16011. (with-selected-window cwin
  16012. (calendar-goto-date (if (listp date) date
  16013. (calendar-gregorian-from-absolute date))))))))
  16014. (defun org-goto-calendar (&optional arg)
  16015. "Go to the Emacs calendar at the current date.
  16016. If there is a time stamp in the current line, go to that date.
  16017. A prefix ARG can be used to force the current date."
  16018. (interactive "P")
  16019. (let ((tsr org-ts-regexp) diff
  16020. (calendar-move-hook nil)
  16021. (calendar-view-holidays-initially-flag nil)
  16022. (calendar-view-diary-initially-flag nil))
  16023. (if (or (org-at-timestamp-p)
  16024. (save-excursion
  16025. (beginning-of-line 1)
  16026. (looking-at (concat ".*" tsr))))
  16027. (let ((d1 (time-to-days (current-time)))
  16028. (d2 (time-to-days
  16029. (org-time-string-to-time (match-string 1)))))
  16030. (setq diff (- d2 d1))))
  16031. (calendar)
  16032. (calendar-goto-today)
  16033. (if (and diff (not arg)) (calendar-forward-day diff))))
  16034. (defun org-get-date-from-calendar ()
  16035. "Return a list (month day year) of date at point in calendar."
  16036. (with-current-buffer "*Calendar*"
  16037. (save-match-data
  16038. (calendar-cursor-to-date))))
  16039. (defun org-date-from-calendar ()
  16040. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16041. If there is already a time stamp at the cursor position, update it."
  16042. (interactive)
  16043. (if (org-at-timestamp-p t)
  16044. (org-timestamp-change 0 'calendar)
  16045. (let ((cal-date (org-get-date-from-calendar)))
  16046. (org-insert-time-stamp
  16047. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16048. (defcustom org-effort-durations
  16049. `(("h" . 60)
  16050. ("d" . ,(* 60 8))
  16051. ("w" . ,(* 60 8 5))
  16052. ("m" . ,(* 60 8 5 4))
  16053. ("y" . ,(* 60 8 5 40)))
  16054. "Conversion factor to minutes for an effort modifier.
  16055. Each entry has the form (MODIFIER . MINUTES).
  16056. In an effort string, a number followed by MODIFIER is multiplied
  16057. by the specified number of MINUTES to obtain an effort in
  16058. minutes.
  16059. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16060. effort string \"2hours\" is equivalent to 120 minutes."
  16061. :group 'org-agenda
  16062. :version "24.1"
  16063. :type '(alist :key-type (string :tag "Modifier")
  16064. :value-type (number :tag "Minutes")))
  16065. (defun org-minutes-to-clocksum-string (m)
  16066. "Format number of minutes as a clocksum string.
  16067. The format is determined by `org-time-clocksum-format',
  16068. `org-time-clocksum-use-fractional' and
  16069. `org-time-clocksum-fractional-format' and
  16070. `org-time-clocksum-use-effort-durations'."
  16071. (let ((clocksum "")
  16072. (m (round m)) ; Don't allow fractions of minutes
  16073. h d w mo y fmt n)
  16074. (setq h (if org-time-clocksum-use-effort-durations
  16075. (cdr (assoc "h" org-effort-durations)) 60)
  16076. d (if org-time-clocksum-use-effort-durations
  16077. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16078. w (if org-time-clocksum-use-effort-durations
  16079. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16080. mo (if org-time-clocksum-use-effort-durations
  16081. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16082. y (if org-time-clocksum-use-effort-durations
  16083. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16084. ;; fractional format
  16085. (if org-time-clocksum-use-fractional
  16086. (cond
  16087. ;; single format string
  16088. ((stringp org-time-clocksum-fractional-format)
  16089. (format org-time-clocksum-fractional-format (/ m (float h))))
  16090. ;; choice of fractional formats for different time units
  16091. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16092. (> (/ (truncate m) (* y d h)) 0))
  16093. (format fmt (/ m (* y d (float h)))))
  16094. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16095. (> (/ (truncate m) (* mo d h)) 0))
  16096. (format fmt (/ m (* mo d (float h)))))
  16097. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16098. (> (/ (truncate m) (* w d h)) 0))
  16099. (format fmt (/ m (* w d (float h)))))
  16100. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16101. (> (/ (truncate m) (* d h)) 0))
  16102. (format fmt (/ m (* d (float h)))))
  16103. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16104. (> (/ (truncate m) h) 0))
  16105. (format fmt (/ m (float h))))
  16106. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16107. (format fmt m))
  16108. ;; fall back to smallest time unit with a format
  16109. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16110. (format fmt (/ m (float h))))
  16111. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16112. (format fmt (/ m (* d (float h)))))
  16113. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16114. (format fmt (/ m (* w d (float h)))))
  16115. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16116. (format fmt (/ m (* mo d (float h)))))
  16117. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16118. (format fmt (/ m (* y d (float h))))))
  16119. ;; standard (non-fractional) format, with single format string
  16120. (if (stringp org-time-clocksum-format)
  16121. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16122. ;; separate formats components
  16123. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16124. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16125. (plist-get org-time-clocksum-format :require-years))
  16126. (setq clocksum (concat clocksum (format fmt n))
  16127. m (- m (* n y d h))))
  16128. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16129. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16130. (plist-get org-time-clocksum-format :require-months))
  16131. (setq clocksum (concat clocksum (format fmt n))
  16132. m (- m (* n mo d h))))
  16133. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16134. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16135. (plist-get org-time-clocksum-format :require-weeks))
  16136. (setq clocksum (concat clocksum (format fmt n))
  16137. m (- m (* n w d h))))
  16138. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16139. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16140. (plist-get org-time-clocksum-format :require-days))
  16141. (setq clocksum (concat clocksum (format fmt n))
  16142. m (- m (* n d h))))
  16143. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16144. (or (> (setq n (/ (truncate m) h)) 0)
  16145. (plist-get org-time-clocksum-format :require-hours))
  16146. (setq clocksum (concat clocksum (format fmt n))
  16147. m (- m (* n h))))
  16148. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16149. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16150. (setq clocksum (concat clocksum (format fmt m))))
  16151. ;; return formatted time duration
  16152. clocksum))))
  16153. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16154. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16155. "Org mode version 8.0")
  16156. (defun org-hours-to-clocksum-string (n)
  16157. (org-minutes-to-clocksum-string (* n 60)))
  16158. (defun org-hh:mm-string-to-minutes (s)
  16159. "Convert a string H:MM to a number of minutes.
  16160. If the string is just a number, interpret it as minutes.
  16161. In fact, the first hh:mm or number in the string will be taken,
  16162. there can be extra stuff in the string.
  16163. If no number is found, the return value is 0."
  16164. (cond
  16165. ((integerp s) s)
  16166. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16167. (+ (* (string-to-number (match-string 1 s)) 60)
  16168. (string-to-number (match-string 2 s))))
  16169. ((string-match "\\([0-9]+\\)" s)
  16170. (string-to-number (match-string 1 s)))
  16171. (t 0)))
  16172. (defcustom org-image-actual-width t
  16173. "Should we use the actual width of images when inlining them?
  16174. When set to `t', always use the image width.
  16175. When set to a number, use imagemagick (when available) to set
  16176. the image's width to this value.
  16177. When set to a number in a list, try to get the width from any
  16178. #+ATTR.* keyword if it matches a width specification like
  16179. #+ATTR_HTML: :width 300px
  16180. and fall back on that number if none is found.
  16181. When set to nil, try to get the width from an #+ATTR.* keyword
  16182. and fall back on the original width if none is found.
  16183. This requires Emacs >= 24.1, build with imagemagick support."
  16184. :group 'org-appearance
  16185. :version "24.4"
  16186. :package-version '(Org . "8.0")
  16187. :type '(choice
  16188. (const :tag "Use the image width" t)
  16189. (integer :tag "Use a number of pixels")
  16190. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16191. (const :tag "Use #+ATTR* or don't resize" nil)))
  16192. (defcustom org-agenda-inhibit-startup nil
  16193. "Inhibit startup when preparing agenda buffers.
  16194. When this variable is `t' (the default), the initialization of
  16195. the Org agenda buffers is inhibited: e.g. the visibility state
  16196. is not set, the tables are not re-aligned, etc."
  16197. :type 'boolean
  16198. :version "24.3"
  16199. :group 'org-agenda)
  16200. (defcustom org-agenda-ignore-drawer-properties nil
  16201. "Avoid updating text properties when building the agenda.
  16202. Properties are used to prepare buffers for effort estimates, appointments,
  16203. and subtree-local categories.
  16204. If you don't use these in the agenda, you can add them to this list and
  16205. agenda building will be a bit faster.
  16206. The value is a list, with zero or more of the symbols `effort', `appt',
  16207. or `category'."
  16208. :type '(set :greedy t
  16209. (const effort)
  16210. (const appt)
  16211. (const category))
  16212. :version "24.3"
  16213. :group 'org-agenda)
  16214. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16215. "Convert a duration string S to minutes.
  16216. A bare number is interpreted as minutes, modifiers can be set by
  16217. customizing `org-effort-durations' (which see).
  16218. Entries containing a colon are interpreted as H:MM by
  16219. `org-hh:mm-string-to-minutes'."
  16220. (let ((result 0)
  16221. (re (concat "\\([0-9.]+\\) *\\("
  16222. (regexp-opt (mapcar 'car org-effort-durations))
  16223. "\\)")))
  16224. (while (string-match re s)
  16225. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16226. (string-to-number (match-string 1 s))))
  16227. (setq s (replace-match "" nil t s)))
  16228. (setq result (floor result))
  16229. (incf result (org-hh:mm-string-to-minutes s))
  16230. (if output-to-string (number-to-string result) result)))
  16231. ;;;; Files
  16232. (defun org-save-all-org-buffers ()
  16233. "Save all Org-mode buffers without user confirmation."
  16234. (interactive)
  16235. (message "Saving all Org-mode buffers...")
  16236. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16237. (when (featurep 'org-id) (org-id-locations-save))
  16238. (message "Saving all Org-mode buffers... done"))
  16239. (defun org-revert-all-org-buffers ()
  16240. "Revert all Org-mode buffers.
  16241. Prompt for confirmation when there are unsaved changes.
  16242. Be sure you know what you are doing before letting this function
  16243. overwrite your changes.
  16244. This function is useful in a setup where one tracks org files
  16245. with a version control system, to revert on one machine after pulling
  16246. changes from another. I believe the procedure must be like this:
  16247. 1. M-x org-save-all-org-buffers
  16248. 2. Pull changes from the other machine, resolve conflicts
  16249. 3. M-x org-revert-all-org-buffers"
  16250. (interactive)
  16251. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16252. (user-error "Abort"))
  16253. (save-excursion
  16254. (save-window-excursion
  16255. (mapc
  16256. (lambda (b)
  16257. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16258. (with-current-buffer b buffer-file-name))
  16259. (org-pop-to-buffer-same-window b)
  16260. (revert-buffer t 'no-confirm)))
  16261. (buffer-list))
  16262. (when (and (featurep 'org-id) org-id-track-globally)
  16263. (org-id-locations-load)))))
  16264. ;;;; Agenda files
  16265. ;;;###autoload
  16266. (defun org-switchb (&optional arg)
  16267. "Switch between Org buffers.
  16268. With one prefix argument, restrict available buffers to files.
  16269. With two prefix arguments, restrict available buffers to agenda files.
  16270. Defaults to `iswitchb' for buffer name completion.
  16271. Set `org-completion-use-ido' to make it use ido instead."
  16272. (interactive "P")
  16273. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16274. ((equal arg '(16)) (org-buffer-list 'agenda))
  16275. (t (org-buffer-list))))
  16276. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16277. (org-completion-use-ido org-completion-use-ido))
  16278. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16279. (setq org-completion-use-iswitchb t))
  16280. (org-pop-to-buffer-same-window
  16281. (org-icompleting-read "Org buffer: "
  16282. (mapcar 'list (mapcar 'buffer-name blist))
  16283. nil t))))
  16284. ;;; Define some older names previously used for this functionality
  16285. ;;;###autoload
  16286. (defalias 'org-ido-switchb 'org-switchb)
  16287. ;;;###autoload
  16288. (defalias 'org-iswitchb 'org-switchb)
  16289. (defun org-buffer-list (&optional predicate exclude-tmp)
  16290. "Return a list of Org buffers.
  16291. PREDICATE can be `export', `files' or `agenda'.
  16292. export restrict the list to Export buffers.
  16293. files restrict the list to buffers visiting Org files.
  16294. agenda restrict the list to buffers visiting agenda files.
  16295. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16296. (let* ((bfn nil)
  16297. (agenda-files (and (eq predicate 'agenda)
  16298. (mapcar 'file-truename (org-agenda-files t))))
  16299. (filter
  16300. (cond
  16301. ((eq predicate 'files)
  16302. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16303. ((eq predicate 'export)
  16304. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16305. ((eq predicate 'agenda)
  16306. (lambda (b)
  16307. (with-current-buffer b
  16308. (and (derived-mode-p 'org-mode)
  16309. (setq bfn (buffer-file-name b))
  16310. (member (file-truename bfn) agenda-files)))))
  16311. (t (lambda (b) (with-current-buffer b
  16312. (or (derived-mode-p 'org-mode)
  16313. (string-match "\*Org .*Export"
  16314. (buffer-name b)))))))))
  16315. (delq nil
  16316. (mapcar
  16317. (lambda(b)
  16318. (if (and (funcall filter b)
  16319. (or (not exclude-tmp)
  16320. (not (string-match "tmp" (buffer-name b)))))
  16321. b
  16322. nil))
  16323. (buffer-list)))))
  16324. (defun org-agenda-files (&optional unrestricted archives)
  16325. "Get the list of agenda files.
  16326. Optional UNRESTRICTED means return the full list even if a restriction
  16327. is currently in place.
  16328. When ARCHIVES is t, include all archive files that are really being
  16329. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16330. `org-agenda-archives-mode' is t."
  16331. (let ((files
  16332. (cond
  16333. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16334. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16335. ((listp org-agenda-files) org-agenda-files)
  16336. (t (error "Invalid value of `org-agenda-files'")))))
  16337. (setq files (apply 'append
  16338. (mapcar (lambda (f)
  16339. (if (file-directory-p f)
  16340. (directory-files
  16341. f t org-agenda-file-regexp)
  16342. (list f)))
  16343. files)))
  16344. (when org-agenda-skip-unavailable-files
  16345. (setq files (delq nil
  16346. (mapcar (function
  16347. (lambda (file)
  16348. (and (file-readable-p file) file)))
  16349. files))))
  16350. (when (or (eq archives t)
  16351. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16352. (setq files (org-add-archive-files files)))
  16353. files))
  16354. (defun org-agenda-file-p (&optional file)
  16355. "Return non-nil, if FILE is an agenda file.
  16356. If FILE is omitted, use the file associated with the current
  16357. buffer."
  16358. (member (or file (buffer-file-name))
  16359. (org-agenda-files t)))
  16360. (defun org-edit-agenda-file-list ()
  16361. "Edit the list of agenda files.
  16362. Depending on setup, this either uses customize to edit the variable
  16363. `org-agenda-files', or it visits the file that is holding the list. In the
  16364. latter case, the buffer is set up in a way that saving it automatically kills
  16365. the buffer and restores the previous window configuration."
  16366. (interactive)
  16367. (if (stringp org-agenda-files)
  16368. (let ((cw (current-window-configuration)))
  16369. (find-file org-agenda-files)
  16370. (org-set-local 'org-window-configuration cw)
  16371. (org-add-hook 'after-save-hook
  16372. (lambda ()
  16373. (set-window-configuration
  16374. (prog1 org-window-configuration
  16375. (kill-buffer (current-buffer))))
  16376. (org-install-agenda-files-menu)
  16377. (message "New agenda file list installed"))
  16378. nil 'local)
  16379. (message "%s" (substitute-command-keys
  16380. "Edit list and finish with \\[save-buffer]")))
  16381. (customize-variable 'org-agenda-files)))
  16382. (defun org-store-new-agenda-file-list (list)
  16383. "Set new value for the agenda file list and save it correctly."
  16384. (if (stringp org-agenda-files)
  16385. (let ((fe (org-read-agenda-file-list t)) b u)
  16386. (while (setq b (find-buffer-visiting org-agenda-files))
  16387. (kill-buffer b))
  16388. (with-temp-file org-agenda-files
  16389. (insert
  16390. (mapconcat
  16391. (lambda (f) ;; Keep un-expanded entries.
  16392. (if (setq u (assoc f fe))
  16393. (cdr u)
  16394. f))
  16395. list "\n")
  16396. "\n")))
  16397. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16398. (org-insert-mode-line-in-empty-file nil))
  16399. (setq org-agenda-files list)
  16400. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16401. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16402. "Read the list of agenda files from a file.
  16403. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16404. filenames, used by `org-store-new-agenda-file-list' to write back
  16405. un-expanded file names."
  16406. (when (file-directory-p org-agenda-files)
  16407. (error "`org-agenda-files' cannot be a single directory"))
  16408. (when (stringp org-agenda-files)
  16409. (with-temp-buffer
  16410. (insert-file-contents org-agenda-files)
  16411. (mapcar
  16412. (lambda (f)
  16413. (let ((e (expand-file-name (substitute-in-file-name f)
  16414. org-directory)))
  16415. (if pair-with-expansion
  16416. (cons e f)
  16417. e)))
  16418. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16419. ;;;###autoload
  16420. (defun org-cycle-agenda-files ()
  16421. "Cycle through the files in `org-agenda-files'.
  16422. If the current buffer visits an agenda file, find the next one in the list.
  16423. If the current buffer does not, find the first agenda file."
  16424. (interactive)
  16425. (let* ((fs (org-agenda-files t))
  16426. (files (append fs (list (car fs))))
  16427. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16428. file)
  16429. (unless files (user-error "No agenda files"))
  16430. (catch 'exit
  16431. (while (setq file (pop files))
  16432. (if (equal (file-truename file) tcf)
  16433. (when (car files)
  16434. (find-file (car files))
  16435. (throw 'exit t))))
  16436. (find-file (car fs)))
  16437. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16438. (defun org-agenda-file-to-front (&optional to-end)
  16439. "Move/add the current file to the top of the agenda file list.
  16440. If the file is not present in the list, it is added to the front. If it is
  16441. present, it is moved there. With optional argument TO-END, add/move to the
  16442. end of the list."
  16443. (interactive "P")
  16444. (let ((org-agenda-skip-unavailable-files nil)
  16445. (file-alist (mapcar (lambda (x)
  16446. (cons (file-truename x) x))
  16447. (org-agenda-files t)))
  16448. (ctf (file-truename
  16449. (or buffer-file-name
  16450. (user-error "Please save the current buffer to a file"))))
  16451. x had)
  16452. (setq x (assoc ctf file-alist) had x)
  16453. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16454. (if to-end
  16455. (setq file-alist (append (delq x file-alist) (list x)))
  16456. (setq file-alist (cons x (delq x file-alist))))
  16457. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16458. (org-install-agenda-files-menu)
  16459. (message "File %s to %s of agenda file list"
  16460. (if had "moved" "added") (if to-end "end" "front"))))
  16461. (defun org-remove-file (&optional file)
  16462. "Remove current file from the list of files in variable `org-agenda-files'.
  16463. These are the files which are being checked for agenda entries.
  16464. Optional argument FILE means use this file instead of the current."
  16465. (interactive)
  16466. (let* ((org-agenda-skip-unavailable-files nil)
  16467. (file (or file buffer-file-name
  16468. (user-error "Current buffer does not visit a file")))
  16469. (true-file (file-truename file))
  16470. (afile (abbreviate-file-name file))
  16471. (files (delq nil (mapcar
  16472. (lambda (x)
  16473. (if (equal true-file
  16474. (file-truename x))
  16475. nil x))
  16476. (org-agenda-files t)))))
  16477. (if (not (= (length files) (length (org-agenda-files t))))
  16478. (progn
  16479. (org-store-new-agenda-file-list files)
  16480. (org-install-agenda-files-menu)
  16481. (message "Removed file: %s" afile))
  16482. (message "File was not in list: %s (not removed)" afile))))
  16483. (defun org-file-menu-entry (file)
  16484. (vector file (list 'find-file file) t))
  16485. (defun org-check-agenda-file (file)
  16486. "Make sure FILE exists. If not, ask user what to do."
  16487. (when (not (file-exists-p file))
  16488. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16489. (abbreviate-file-name file))
  16490. (let ((r (downcase (read-char-exclusive))))
  16491. (cond
  16492. ((equal r ?r)
  16493. (org-remove-file file)
  16494. (throw 'nextfile t))
  16495. (t (error "Abort"))))))
  16496. (defun org-get-agenda-file-buffer (file)
  16497. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16498. it to the list of buffers which might be released later."
  16499. (let ((buf (org-find-base-buffer-visiting file)))
  16500. (if buf
  16501. buf ; just return it
  16502. ;; Make a new buffer and remember it
  16503. (setq buf (find-file-noselect file))
  16504. (if buf (push buf org-agenda-new-buffers))
  16505. buf)))
  16506. (defun org-release-buffers (blist)
  16507. "Release all buffers in list, asking the user for confirmation when needed.
  16508. When a buffer is unmodified, it is just killed. When modified, it is saved
  16509. \(if the user agrees) and then killed."
  16510. (let (buf file)
  16511. (while (setq buf (pop blist))
  16512. (setq file (buffer-file-name buf))
  16513. (when (and (buffer-modified-p buf)
  16514. file
  16515. (y-or-n-p (format "Save file %s? " file)))
  16516. (with-current-buffer buf (save-buffer)))
  16517. (kill-buffer buf))))
  16518. (defun org-agenda-prepare-buffers (files)
  16519. "Create buffers for all agenda files, protect archived trees and comments."
  16520. (interactive)
  16521. (let ((pa '(:org-archived t))
  16522. (pc '(:org-comment t))
  16523. (pall '(:org-archived t :org-comment t))
  16524. (inhibit-read-only t)
  16525. (org-inhibit-startup org-agenda-inhibit-startup)
  16526. (rea (concat ":" org-archive-tag ":"))
  16527. file re)
  16528. (setq org-tag-alist-for-agenda nil
  16529. org-tag-groups-alist-for-agenda nil)
  16530. (save-excursion
  16531. (save-restriction
  16532. (while (setq file (pop files))
  16533. (catch 'nextfile
  16534. (if (bufferp file)
  16535. (set-buffer file)
  16536. (org-check-agenda-file file)
  16537. (set-buffer (org-get-agenda-file-buffer file)))
  16538. (widen)
  16539. (org-set-regexps-and-options-for-tags)
  16540. (goto-char (point-min))
  16541. (let ((case-fold-search t))
  16542. (when (search-forward "#+setupfile" nil t)
  16543. ;; Don't set all regexps and options systematically as
  16544. ;; this is only run for setting agenda tags from setup
  16545. ;; file
  16546. (org-set-regexps-and-options)))
  16547. (or (memq 'category org-agenda-ignore-drawer-properties)
  16548. (org-refresh-category-properties))
  16549. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16550. (org-refresh-properties org-effort-property 'org-effort))
  16551. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16552. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16553. (setq org-todo-keywords-for-agenda
  16554. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16555. (setq org-done-keywords-for-agenda
  16556. (append org-done-keywords-for-agenda org-done-keywords))
  16557. (setq org-todo-keyword-alist-for-agenda
  16558. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16559. (setq org-tag-alist-for-agenda
  16560. (org-uniquify
  16561. (append org-tag-alist-for-agenda
  16562. org-tag-alist
  16563. org-tag-persistent-alist)))
  16564. (if org-group-tags
  16565. (setq org-tag-groups-alist-for-agenda
  16566. (org-uniquify-alist
  16567. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16568. (org-with-silent-modifications
  16569. (save-excursion
  16570. (remove-text-properties (point-min) (point-max) pall)
  16571. (when org-agenda-skip-archived-trees
  16572. (goto-char (point-min))
  16573. (while (re-search-forward rea nil t)
  16574. (if (org-at-heading-p t)
  16575. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16576. (goto-char (point-min))
  16577. (setq re (format org-heading-keyword-regexp-format
  16578. org-comment-string))
  16579. (while (re-search-forward re nil t)
  16580. (add-text-properties
  16581. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16582. (setq org-todo-keywords-for-agenda
  16583. (org-uniquify org-todo-keywords-for-agenda))
  16584. (setq org-todo-keyword-alist-for-agenda
  16585. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16586. ;;;; CDLaTeX minor mode
  16587. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16588. "Keymap for the minor `org-cdlatex-mode'.")
  16589. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16590. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16591. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16592. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16593. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16594. (defvar org-cdlatex-texmathp-advice-is-done nil
  16595. "Flag remembering if we have applied the advice to texmathp already.")
  16596. (define-minor-mode org-cdlatex-mode
  16597. "Toggle the minor `org-cdlatex-mode'.
  16598. This mode supports entering LaTeX environment and math in LaTeX fragments
  16599. in Org-mode.
  16600. \\{org-cdlatex-mode-map}"
  16601. nil " OCDL" nil
  16602. (when org-cdlatex-mode
  16603. (require 'cdlatex)
  16604. (run-hooks 'cdlatex-mode-hook)
  16605. (cdlatex-compute-tables))
  16606. (unless org-cdlatex-texmathp-advice-is-done
  16607. (setq org-cdlatex-texmathp-advice-is-done t)
  16608. (defadvice texmathp (around org-math-always-on activate)
  16609. "Always return t in org-mode buffers.
  16610. This is because we want to insert math symbols without dollars even outside
  16611. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16612. an embedded LaTeX fragment, let texmathp do its job.
  16613. \\[org-cdlatex-mode-map]"
  16614. (interactive)
  16615. (let (p)
  16616. (cond
  16617. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16618. ((eq this-command 'cdlatex-math-symbol)
  16619. (setq ad-return-value t
  16620. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16621. (t
  16622. (let ((p (org-inside-LaTeX-fragment-p)))
  16623. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16624. (setq ad-return-value t
  16625. texmathp-why '("Org-mode embedded math" . 0))
  16626. (if p ad-do-it)))))))))
  16627. (defun turn-on-org-cdlatex ()
  16628. "Unconditionally turn on `org-cdlatex-mode'."
  16629. (org-cdlatex-mode 1))
  16630. (defun org-try-cdlatex-tab ()
  16631. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16632. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16633. - inside a LaTeX fragment, or
  16634. - after the first word in a line, where an abbreviation expansion could
  16635. insert a LaTeX environment."
  16636. (when org-cdlatex-mode
  16637. (cond
  16638. ;; Before any word on the line: No expansion possible.
  16639. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16640. ;; Just after first word on the line: Expand it. Make sure it
  16641. ;; cannot happen on headlines, though.
  16642. ((save-excursion
  16643. (skip-chars-backward "a-zA-Z0-9*")
  16644. (skip-chars-backward " \t")
  16645. (and (bolp) (not (org-at-heading-p))))
  16646. (cdlatex-tab) t)
  16647. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16648. (defun org-cdlatex-underscore-caret (&optional arg)
  16649. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16650. Revert to the normal definition outside of these fragments."
  16651. (interactive "P")
  16652. (if (org-inside-LaTeX-fragment-p)
  16653. (call-interactively 'cdlatex-sub-superscript)
  16654. (let (org-cdlatex-mode)
  16655. (call-interactively (key-binding (vector last-input-event))))))
  16656. (defun org-cdlatex-math-modify (&optional arg)
  16657. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16658. Revert to the normal definition outside of these fragments."
  16659. (interactive "P")
  16660. (if (org-inside-LaTeX-fragment-p)
  16661. (call-interactively 'cdlatex-math-modify)
  16662. (let (org-cdlatex-mode)
  16663. (call-interactively (key-binding (vector last-input-event))))))
  16664. ;;;; LaTeX fragments
  16665. (defvar org-latex-regexps
  16666. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16667. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16668. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16669. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16670. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16671. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16672. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16673. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16674. "Regular expressions for matching embedded LaTeX.")
  16675. (defun org-inside-LaTeX-fragment-p ()
  16676. "Test if point is inside a LaTeX fragment.
  16677. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16678. sequence appearing also before point.
  16679. Even though the matchers for math are configurable, this function assumes
  16680. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16681. delimiters are skipped when they have been removed by customization.
  16682. The return value is nil, or a cons cell with the delimiter and the
  16683. position of this delimiter.
  16684. This function does a reasonably good job, but can locally be fooled by
  16685. for example currency specifications. For example it will assume being in
  16686. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16687. fragments that are properly closed, but during editing, we have to live
  16688. with the uncertainty caused by missing closing delimiters. This function
  16689. looks only before point, not after."
  16690. (catch 'exit
  16691. (let ((pos (point))
  16692. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16693. (lim (progn
  16694. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16695. (point)))
  16696. dd-on str (start 0) m re)
  16697. (goto-char pos)
  16698. (when dodollar
  16699. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16700. re (nth 1 (assoc "$" org-latex-regexps)))
  16701. (while (string-match re str start)
  16702. (cond
  16703. ((= (match-end 0) (length str))
  16704. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16705. ((= (match-end 0) (- (length str) 5))
  16706. (throw 'exit nil))
  16707. (t (setq start (match-end 0))))))
  16708. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16709. (goto-char pos)
  16710. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16711. (and (match-beginning 2) (throw 'exit nil))
  16712. ;; count $$
  16713. (while (re-search-backward "\\$\\$" lim t)
  16714. (setq dd-on (not dd-on)))
  16715. (goto-char pos)
  16716. (if dd-on (cons "$$" m))))))
  16717. (defun org-inside-latex-macro-p ()
  16718. "Is point inside a LaTeX macro or its arguments?"
  16719. (save-match-data
  16720. (org-in-regexp
  16721. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16722. (defvar org-latex-fragment-image-overlays nil
  16723. "List of overlays carrying the images of latex fragments.")
  16724. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16725. (defun org-remove-latex-fragment-image-overlays ()
  16726. "Remove all overlays with LaTeX fragment images in current buffer."
  16727. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16728. (setq org-latex-fragment-image-overlays nil))
  16729. (defun org-preview-latex-fragment (&optional subtree)
  16730. "Preview the LaTeX fragment at point, or all locally or globally.
  16731. If the cursor is in a LaTeX fragment, create the image and overlay
  16732. it over the source code. If there is no fragment at point, display
  16733. all fragments in the current text, from one headline to the next. With
  16734. prefix SUBTREE, display all fragments in the current subtree. With a
  16735. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16736. the cursor is before the first headline,
  16737. display all fragments in the buffer.
  16738. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16739. (interactive "P")
  16740. (unless buffer-file-name
  16741. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16742. (when (display-graphic-p)
  16743. (org-remove-latex-fragment-image-overlays)
  16744. (save-excursion
  16745. (save-restriction
  16746. (let (beg end at msg)
  16747. (cond
  16748. ((or (equal subtree '(16))
  16749. (not (save-excursion
  16750. (re-search-backward org-outline-regexp-bol nil t))))
  16751. (setq beg (point-min) end (point-max)
  16752. msg "Creating images for buffer...%s"))
  16753. ((equal subtree '(4))
  16754. (org-back-to-heading)
  16755. (setq beg (point) end (org-end-of-subtree t)
  16756. msg "Creating images for subtree...%s"))
  16757. (t
  16758. (if (setq at (org-inside-LaTeX-fragment-p))
  16759. (goto-char (max (point-min) (- (cdr at) 2)))
  16760. (org-back-to-heading))
  16761. (setq beg (point) end (progn (outline-next-heading) (point))
  16762. msg (if at "Creating image...%s"
  16763. "Creating images for entry...%s"))))
  16764. (message msg "")
  16765. (narrow-to-region beg end)
  16766. (goto-char beg)
  16767. (org-format-latex
  16768. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16769. (file-name-nondirectory
  16770. buffer-file-name)))
  16771. default-directory 'overlays msg at 'forbuffer
  16772. org-latex-create-formula-image-program)
  16773. (message msg "done. Use `C-c C-c' to remove images."))))))
  16774. (defun org-format-latex (prefix &optional dir overlays msg at
  16775. forbuffer processing-type)
  16776. "Replace LaTeX fragments with links to an image, and produce images.
  16777. Some of the options can be changed using the variable
  16778. `org-format-latex-options'."
  16779. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16780. (let* ((prefixnodir (file-name-nondirectory prefix))
  16781. (absprefix (expand-file-name prefix dir))
  16782. (todir (file-name-directory absprefix))
  16783. (opt org-format-latex-options)
  16784. (optnew org-format-latex-options)
  16785. (matchers (plist-get opt :matchers))
  16786. (re-list org-latex-regexps)
  16787. (cnt 0) txt hash link beg end re e checkdir
  16788. string
  16789. m n block-type block linkfile movefile ov)
  16790. ;; Check the different regular expressions
  16791. (while (setq e (pop re-list))
  16792. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16793. block (if block-type "\n\n" ""))
  16794. (when (member m matchers)
  16795. (goto-char (point-min))
  16796. (while (re-search-forward re nil t)
  16797. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16798. (or (not overlays)
  16799. (not (eq (get-char-property (match-beginning n)
  16800. 'org-overlay-type)
  16801. 'org-latex-overlay))))
  16802. (cond
  16803. ((eq processing-type 'verbatim))
  16804. ((eq processing-type 'mathjax)
  16805. ;; Prepare for MathJax processing.
  16806. (setq string (match-string n))
  16807. (when (member m '("$" "$1"))
  16808. (save-excursion
  16809. (delete-region (match-beginning n) (match-end n))
  16810. (goto-char (match-beginning n))
  16811. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16812. ((or (eq processing-type 'dvipng)
  16813. (eq processing-type 'imagemagick))
  16814. ;; Process to an image.
  16815. (setq txt (match-string n)
  16816. beg (match-beginning n) end (match-end n)
  16817. cnt (1+ cnt))
  16818. (let ((face (face-at-point))
  16819. (fg (plist-get opt :foreground))
  16820. (bg (plist-get opt :background))
  16821. ;; Ensure full list is printed.
  16822. print-length print-level)
  16823. (when forbuffer
  16824. ;; Get the colors from the face at point.
  16825. (goto-char beg)
  16826. (when (eq fg 'auto)
  16827. (setq fg (face-attribute face :foreground nil 'default)))
  16828. (when (eq bg 'auto)
  16829. (setq bg (face-attribute face :background nil 'default)))
  16830. (setq optnew (copy-sequence opt))
  16831. (plist-put optnew :foreground fg)
  16832. (plist-put optnew :background bg))
  16833. (setq hash (sha1 (prin1-to-string
  16834. (list org-format-latex-header
  16835. org-latex-default-packages-alist
  16836. org-latex-packages-alist
  16837. org-format-latex-options
  16838. forbuffer txt fg bg)))
  16839. linkfile (format "%s_%s.png" prefix hash)
  16840. movefile (format "%s_%s.png" absprefix hash)))
  16841. (setq link (concat block "[[file:" linkfile "]]" block))
  16842. (if msg (message msg cnt))
  16843. (goto-char beg)
  16844. (unless checkdir ; Ensure the directory exists.
  16845. (setq checkdir t)
  16846. (or (file-directory-p todir) (make-directory todir t)))
  16847. (unless (file-exists-p movefile)
  16848. (org-create-formula-image
  16849. txt movefile optnew forbuffer processing-type))
  16850. (if overlays
  16851. (progn
  16852. (mapc (lambda (o)
  16853. (if (eq (overlay-get o 'org-overlay-type)
  16854. 'org-latex-overlay)
  16855. (delete-overlay o)))
  16856. (overlays-in beg end))
  16857. (setq ov (make-overlay beg end))
  16858. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16859. (if (featurep 'xemacs)
  16860. (progn
  16861. (overlay-put ov 'invisible t)
  16862. (overlay-put
  16863. ov 'end-glyph
  16864. (make-glyph (vector 'png :file movefile))))
  16865. (overlay-put
  16866. ov 'display
  16867. (list 'image :type 'png :file movefile :ascent 'center)))
  16868. (push ov org-latex-fragment-image-overlays)
  16869. (goto-char end))
  16870. (delete-region beg end)
  16871. (insert (org-add-props link
  16872. (list 'org-latex-src
  16873. (replace-regexp-in-string
  16874. "\"" "" txt)
  16875. 'org-latex-src-embed-type
  16876. (if block-type 'paragraph 'character))))))
  16877. ((eq processing-type 'mathml)
  16878. ;; Process to MathML
  16879. (unless (save-match-data (org-format-latex-mathml-available-p))
  16880. (user-error "LaTeX to MathML converter not configured"))
  16881. (setq txt (match-string n)
  16882. beg (match-beginning n) end (match-end n)
  16883. cnt (1+ cnt))
  16884. (if msg (message msg cnt))
  16885. (goto-char beg)
  16886. (delete-region beg end)
  16887. (insert (org-format-latex-as-mathml
  16888. txt block-type prefix dir)))
  16889. (t
  16890. (error "Unknown conversion type %s for LaTeX fragments"
  16891. processing-type)))))))))
  16892. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16893. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16894. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16895. sucessful, return the portion between \"<math...> </math>\"
  16896. elements otherwise return nil. When MATHML-FILE is specified,
  16897. write the results in to that file. When invoked as an
  16898. interactive command, prompt for LATEX-FRAG, with initial value
  16899. set to the current active region and echo the results for user
  16900. inspection."
  16901. (interactive (list (let ((frag (when (org-region-active-p)
  16902. (buffer-substring-no-properties
  16903. (region-beginning) (region-end)))))
  16904. (read-string "LaTeX Fragment: " frag nil frag))))
  16905. (unless latex-frag (error "Invalid LaTeX fragment"))
  16906. (let* ((tmp-in-file (file-relative-name
  16907. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16908. (ignore (write-region latex-frag nil tmp-in-file))
  16909. (tmp-out-file (file-relative-name
  16910. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16911. (cmd (format-spec
  16912. org-latex-to-mathml-convert-command
  16913. `((?j . ,(shell-quote-argument
  16914. (expand-file-name org-latex-to-mathml-jar-file)))
  16915. (?I . ,(shell-quote-argument tmp-in-file))
  16916. (?o . ,(shell-quote-argument tmp-out-file)))))
  16917. mathml shell-command-output)
  16918. (when (org-called-interactively-p 'any)
  16919. (unless (org-format-latex-mathml-available-p)
  16920. (user-error "LaTeX to MathML converter not configured")))
  16921. (message "Running %s" cmd)
  16922. (setq shell-command-output (shell-command-to-string cmd))
  16923. (setq mathml
  16924. (when (file-readable-p tmp-out-file)
  16925. (with-current-buffer (find-file-noselect tmp-out-file t)
  16926. (goto-char (point-min))
  16927. (when (re-search-forward
  16928. (concat
  16929. (regexp-quote
  16930. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16931. "\\(.\\|\n\\)*"
  16932. (regexp-quote "</math>")) nil t)
  16933. (prog1 (match-string 0) (kill-buffer))))))
  16934. (cond
  16935. (mathml
  16936. (setq mathml
  16937. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16938. (when mathml-file
  16939. (write-region mathml nil mathml-file))
  16940. (when (org-called-interactively-p 'any)
  16941. (message mathml)))
  16942. ((message "LaTeX to MathML conversion failed")
  16943. (message shell-command-output)))
  16944. (delete-file tmp-in-file)
  16945. (when (file-exists-p tmp-out-file)
  16946. (delete-file tmp-out-file))
  16947. mathml))
  16948. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16949. prefix &optional dir)
  16950. "Use `org-create-math-formula' but check local cache first."
  16951. (let* ((absprefix (expand-file-name prefix dir))
  16952. (print-length nil) (print-level nil)
  16953. (formula-id (concat
  16954. "formula-"
  16955. (sha1
  16956. (prin1-to-string
  16957. (list latex-frag
  16958. org-latex-to-mathml-convert-command)))))
  16959. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16960. (formula-cache-dir (file-name-directory formula-cache)))
  16961. (unless (file-directory-p formula-cache-dir)
  16962. (make-directory formula-cache-dir t))
  16963. (unless (file-exists-p formula-cache)
  16964. (org-create-math-formula latex-frag formula-cache))
  16965. (if (file-exists-p formula-cache)
  16966. ;; Successful conversion. Return the link to MathML file.
  16967. (org-add-props
  16968. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16969. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16970. 'org-latex-src-embed-type (if latex-frag-type
  16971. 'paragraph 'character)))
  16972. ;; Failed conversion. Return the LaTeX fragment verbatim
  16973. latex-frag)))
  16974. (defun org-create-formula-image (string tofile options buffer &optional type)
  16975. "Create an image from LaTeX source using dvipng or convert.
  16976. This function calls either `org-create-formula-image-with-dvipng'
  16977. or `org-create-formula-image-with-imagemagick' depending on the
  16978. value of `org-latex-create-formula-image-program' or on the value
  16979. of the optional TYPE variable.
  16980. Note: ultimately these two function should be combined as they
  16981. share a good deal of logic."
  16982. (org-check-external-command
  16983. "latex" "needed to convert LaTeX fragments to images")
  16984. (funcall
  16985. (case (or type org-latex-create-formula-image-program)
  16986. ('dvipng
  16987. (org-check-external-command
  16988. "dvipng" "needed to convert LaTeX fragments to images")
  16989. #'org-create-formula-image-with-dvipng)
  16990. ('imagemagick
  16991. (org-check-external-command
  16992. "convert" "you need to install imagemagick")
  16993. #'org-create-formula-image-with-imagemagick)
  16994. (t (error
  16995. "Invalid value of `org-latex-create-formula-image-program'")))
  16996. string tofile options buffer))
  16997. (declare-function org-export-get-backend "ox" (name))
  16998. (declare-function org-export--get-global-options "ox" (&optional backend))
  16999. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17000. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17001. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17002. (defun org-create-formula--latex-header ()
  17003. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17004. (let ((info (org-combine-plists (org-export--get-global-options
  17005. (org-export-get-backend 'latex))
  17006. (org-export--get-inbuffer-options
  17007. (org-export-get-backend 'latex)))))
  17008. (org-latex-guess-babel-language
  17009. (org-latex-guess-inputenc
  17010. (org-splice-latex-header
  17011. org-format-latex-header
  17012. org-latex-default-packages-alist
  17013. org-latex-packages-alist t
  17014. (plist-get info :latex-header)))
  17015. info)))
  17016. ;; This function borrows from Ganesh Swami's latex2png.el
  17017. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17018. "This calls dvipng."
  17019. (require 'ox-latex)
  17020. (let* ((tmpdir (if (featurep 'xemacs)
  17021. (temp-directory)
  17022. temporary-file-directory))
  17023. (texfilebase (make-temp-name
  17024. (expand-file-name "orgtex" tmpdir)))
  17025. (texfile (concat texfilebase ".tex"))
  17026. (dvifile (concat texfilebase ".dvi"))
  17027. (pngfile (concat texfilebase ".png"))
  17028. (fnh (if (featurep 'xemacs)
  17029. (font-height (face-font 'default))
  17030. (face-attribute 'default :height nil)))
  17031. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17032. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17033. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17034. "Black"))
  17035. (bg (or (plist-get options (if buffer :background :html-background))
  17036. "Transparent")))
  17037. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17038. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17039. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17040. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17041. (let ((latex-header (org-create-formula--latex-header)))
  17042. (with-temp-file texfile
  17043. (insert latex-header)
  17044. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17045. (let ((dir default-directory))
  17046. (condition-case nil
  17047. (progn
  17048. (cd tmpdir)
  17049. (call-process "latex" nil nil nil texfile))
  17050. (error nil))
  17051. (cd dir))
  17052. (if (not (file-exists-p dvifile))
  17053. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17054. (condition-case nil
  17055. (if (featurep 'xemacs)
  17056. (call-process "dvipng" nil nil nil
  17057. "-fg" fg "-bg" bg
  17058. "-T" "tight"
  17059. "-o" pngfile
  17060. dvifile)
  17061. (call-process "dvipng" nil nil nil
  17062. "-fg" fg "-bg" bg
  17063. "-D" dpi
  17064. ;;"-x" scale "-y" scale
  17065. "-T" "tight"
  17066. "-o" pngfile
  17067. dvifile))
  17068. (error nil))
  17069. (if (not (file-exists-p pngfile))
  17070. (if org-format-latex-signal-error
  17071. (error "Failed to create png file from %s" texfile)
  17072. (message "Failed to create png file from %s" texfile)
  17073. nil)
  17074. ;; Use the requested file name and clean up
  17075. (copy-file pngfile tofile 'replace)
  17076. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17077. (if (file-exists-p (concat texfilebase e))
  17078. (delete-file (concat texfilebase e))))
  17079. pngfile))))
  17080. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17081. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17082. "This calls convert, which is included into imagemagick."
  17083. (require 'ox-latex)
  17084. (let* ((tmpdir (if (featurep 'xemacs)
  17085. (temp-directory)
  17086. temporary-file-directory))
  17087. (texfilebase (make-temp-name
  17088. (expand-file-name "orgtex" tmpdir)))
  17089. (texfile (concat texfilebase ".tex"))
  17090. (pdffile (concat texfilebase ".pdf"))
  17091. (pngfile (concat texfilebase ".png"))
  17092. (fnh (if (featurep 'xemacs)
  17093. (font-height (face-font 'default))
  17094. (face-attribute 'default :height nil)))
  17095. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17096. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17097. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17098. "black"))
  17099. (bg (or (plist-get options (if buffer :background :html-background))
  17100. "white")))
  17101. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17102. (setq fg (org-latex-color-format fg)))
  17103. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17104. (setq bg (org-latex-color-format
  17105. (if (string= bg "Transparent") "white" bg))))
  17106. (let ((latex-header (org-create-formula--latex-header)))
  17107. (with-temp-file texfile
  17108. (insert latex-header)
  17109. (insert "\n\\begin{document}\n"
  17110. "\\definecolor{fg}{rgb}{" fg "}\n"
  17111. "\\definecolor{bg}{rgb}{" bg "}\n"
  17112. "\n\\pagecolor{bg}\n"
  17113. "\n{\\color{fg}\n"
  17114. string
  17115. "\n}\n"
  17116. "\n\\end{document}\n")))
  17117. (org-latex-compile texfile t)
  17118. (if (not (file-exists-p pdffile))
  17119. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17120. (condition-case nil
  17121. (if (featurep 'xemacs)
  17122. (call-process "convert" nil nil nil
  17123. "-density" "96"
  17124. "-trim"
  17125. "-antialias"
  17126. pdffile
  17127. "-quality" "100"
  17128. ;; "-sharpen" "0x1.0"
  17129. pngfile)
  17130. (call-process "convert" nil nil nil
  17131. "-density" dpi
  17132. "-trim"
  17133. "-antialias"
  17134. pdffile
  17135. "-quality" "100"
  17136. ;; "-sharpen" "0x1.0"
  17137. pngfile))
  17138. (error nil))
  17139. (if (not (file-exists-p pngfile))
  17140. (if org-format-latex-signal-error
  17141. (error "Failed to create png file from %s" texfile)
  17142. (message "Failed to create png file from %s" texfile)
  17143. nil)
  17144. ;; Use the requested file name and clean up
  17145. (copy-file pngfile tofile 'replace)
  17146. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17147. (if (file-exists-p (concat texfilebase e))
  17148. (delete-file (concat texfilebase e))))
  17149. pngfile))))
  17150. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17151. "Fill a LaTeX header template TPL.
  17152. In the template, the following place holders will be recognized:
  17153. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17154. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17155. [PACKAGES] \\usepackage statements for PKG
  17156. [NO-PACKAGES] do not include PKG
  17157. [EXTRA] the string EXTRA
  17158. [NO-EXTRA] do not include EXTRA
  17159. For backward compatibility, if both the positive and the negative place
  17160. holder is missing, the positive one (without the \"NO-\") will be
  17161. assumed to be present at the end of the template.
  17162. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17163. EXTRA is a string.
  17164. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17165. (let (rpl (end ""))
  17166. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17167. (setq rpl (if (or (match-end 1) (not def-pkg))
  17168. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17169. tpl (replace-match rpl t t tpl))
  17170. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17171. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17172. (setq rpl (if (or (match-end 1) (not pkg))
  17173. "" (org-latex-packages-to-string pkg snippets-p t))
  17174. tpl (replace-match rpl t t tpl))
  17175. (if pkg (setq end
  17176. (concat end "\n"
  17177. (org-latex-packages-to-string pkg snippets-p)))))
  17178. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17179. (setq rpl (if (or (match-end 1) (not extra))
  17180. "" (concat extra "\n"))
  17181. tpl (replace-match rpl t t tpl))
  17182. (if (and extra (string-match "\\S-" extra))
  17183. (setq end (concat end "\n" extra))))
  17184. (if (string-match "\\S-" end)
  17185. (concat tpl "\n" end)
  17186. tpl)))
  17187. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17188. "Turn an alist of packages into a string with the \\usepackage macros."
  17189. (setq pkg (mapconcat (lambda(p)
  17190. (cond
  17191. ((stringp p) p)
  17192. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17193. (format "%% Package %s omitted" (cadr p)))
  17194. ((equal "" (car p))
  17195. (format "\\usepackage{%s}" (cadr p)))
  17196. (t
  17197. (format "\\usepackage[%s]{%s}"
  17198. (car p) (cadr p)))))
  17199. pkg
  17200. "\n"))
  17201. (if newline (concat pkg "\n") pkg))
  17202. (defun org-dvipng-color (attr)
  17203. "Return a RGB color specification for dvipng."
  17204. (apply 'format "rgb %s %s %s"
  17205. (mapcar 'org-normalize-color
  17206. (if (featurep 'xemacs)
  17207. (color-rgb-components
  17208. (face-property 'default
  17209. (cond ((eq attr :foreground) 'foreground)
  17210. ((eq attr :background) 'background))))
  17211. (color-values (face-attribute 'default attr nil))))))
  17212. (defun org-dvipng-color-format (color-name)
  17213. "Convert COLOR-NAME to a RGB color value for dvipng."
  17214. (apply 'format "rgb %s %s %s"
  17215. (mapcar 'org-normalize-color
  17216. (color-values color-name))))
  17217. (defun org-latex-color (attr)
  17218. "Return a RGB color for the LaTeX color package."
  17219. (apply 'format "%s,%s,%s"
  17220. (mapcar 'org-normalize-color
  17221. (if (featurep 'xemacs)
  17222. (color-rgb-components
  17223. (face-property 'default
  17224. (cond ((eq attr :foreground) 'foreground)
  17225. ((eq attr :background) 'background))))
  17226. (color-values (face-attribute 'default attr nil))))))
  17227. (defun org-latex-color-format (color-name)
  17228. "Convert COLOR-NAME to a RGB color value."
  17229. (apply 'format "%s,%s,%s"
  17230. (mapcar 'org-normalize-color
  17231. (color-values color-name))))
  17232. (defun org-normalize-color (value)
  17233. "Return string to be used as color value for an RGB component."
  17234. (format "%g" (/ value 65535.0)))
  17235. ;; Image display
  17236. (defvar org-inline-image-overlays nil)
  17237. (make-variable-buffer-local 'org-inline-image-overlays)
  17238. (defun org-toggle-inline-images (&optional include-linked)
  17239. "Toggle the display of inline images.
  17240. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17241. (interactive "P")
  17242. (if org-inline-image-overlays
  17243. (progn
  17244. (org-remove-inline-images)
  17245. (message "Inline image display turned off"))
  17246. (org-display-inline-images include-linked)
  17247. (if (and (org-called-interactively-p)
  17248. org-inline-image-overlays)
  17249. (message "%d images displayed inline"
  17250. (length org-inline-image-overlays))
  17251. (message "No images to display inline"))))
  17252. (defun org-redisplay-inline-images ()
  17253. "Refresh the display of inline images."
  17254. (interactive)
  17255. (if (not org-inline-image-overlays)
  17256. (org-toggle-inline-images)
  17257. (org-toggle-inline-images)
  17258. (org-toggle-inline-images)))
  17259. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17260. "Display inline images.
  17261. An inline image is a link which follows either of these
  17262. conventions:
  17263. 1. Its path is a file with an extension matching return value
  17264. from `image-file-name-regexp' and it has no contents.
  17265. 2. Its description consists in a single link of the previous
  17266. type.
  17267. When optional argument INCLUDE-LINKED is non-nil, also links with
  17268. a text description part will be inlined. This can be nice for
  17269. a quick look at those images, but it does not reflect what
  17270. exported files will look like.
  17271. When optional argument REFRESH is non-nil, refresh existing
  17272. images between BEG and END. This will create new image displays
  17273. only if necessary. BEG and END default to the buffer
  17274. boundaries."
  17275. (interactive "P")
  17276. (when (display-graphic-p)
  17277. (unless refresh
  17278. (org-remove-inline-images)
  17279. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17280. (org-with-wide-buffer
  17281. (goto-char (or beg (point-min)))
  17282. (let ((case-fold-search t)
  17283. (file-extension-re (org-image-file-name-regexp)))
  17284. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17285. (let ((link (save-match-data (org-element-context))))
  17286. ;; Check if we're at an inline image.
  17287. (when (and (equal (org-element-property :type link) "file")
  17288. (or include-linked
  17289. (not (org-element-property :contents-begin link)))
  17290. (let ((parent (org-element-property :parent link)))
  17291. (or (not (eq (org-element-type parent) 'link))
  17292. (not (cdr (org-element-contents parent)))))
  17293. (org-string-match-p file-extension-re
  17294. (org-element-property :path link)))
  17295. (let ((file (expand-file-name (org-element-property :path link))))
  17296. (when (file-exists-p file)
  17297. (let ((width
  17298. ;; Apply `org-image-actual-width' specifications.
  17299. (cond
  17300. ((not (image-type-available-p 'imagemagick)) nil)
  17301. ((eq org-image-actual-width t) nil)
  17302. ((listp org-image-actual-width)
  17303. (or
  17304. ;; First try to find a width among
  17305. ;; attributes associated to the paragraph
  17306. ;; containing link.
  17307. (let ((paragraph
  17308. (let ((e link))
  17309. (while (and (setq e (org-element-property
  17310. :parent e))
  17311. (not (eq (org-element-type e)
  17312. 'paragraph))))
  17313. e)))
  17314. (when paragraph
  17315. (save-excursion
  17316. (goto-char (org-element-property :begin paragraph))
  17317. (when (save-match-data
  17318. (re-search-forward
  17319. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17320. (org-element-property
  17321. :post-affiliated paragraph)
  17322. t))
  17323. (string-to-number (match-string 1))))))
  17324. ;; Otherwise, fall-back to provided number.
  17325. (car org-image-actual-width)))
  17326. ((numberp org-image-actual-width)
  17327. org-image-actual-width)))
  17328. (old (get-char-property-and-overlay
  17329. (org-element-property :begin link)
  17330. 'org-image-overlay)))
  17331. (if (and (car-safe old) refresh)
  17332. (image-refresh (overlay-get (cdr old) 'display))
  17333. (let ((image (save-match-data
  17334. (create-image file
  17335. (and width 'imagemagick)
  17336. nil
  17337. :width width))))
  17338. (when image
  17339. (let* ((link
  17340. ;; If inline image is the description
  17341. ;; of another link, be sure to
  17342. ;; consider the latter as the one to
  17343. ;; apply the overlay on.
  17344. (let ((parent
  17345. (org-element-property :parent link)))
  17346. (if (eq (org-element-type parent) 'link)
  17347. parent
  17348. link)))
  17349. (ov (make-overlay
  17350. (org-element-property :begin link)
  17351. (progn
  17352. (goto-char
  17353. (org-element-property :end link))
  17354. (skip-chars-backward " \t")
  17355. (point)))))
  17356. (overlay-put ov 'display image)
  17357. (overlay-put ov 'face 'default)
  17358. (overlay-put ov 'org-image-overlay t)
  17359. (overlay-put
  17360. ov 'modification-hooks
  17361. (list 'org-display-inline-remove-overlay))
  17362. (push ov org-inline-image-overlays)))))))))))))))
  17363. (define-obsolete-function-alias
  17364. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17365. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17366. "Remove inline-display overlay if a corresponding region is modified."
  17367. (let ((inhibit-modification-hooks t))
  17368. (when (and ov after)
  17369. (delete ov org-inline-image-overlays)
  17370. (delete-overlay ov))))
  17371. (defun org-remove-inline-images ()
  17372. "Remove inline display of images."
  17373. (interactive)
  17374. (mapc 'delete-overlay org-inline-image-overlays)
  17375. (setq org-inline-image-overlays nil))
  17376. ;;;; Key bindings
  17377. ;; Outline functions from `outline-mode-prefix-map'
  17378. ;; that can be remapped in Org:
  17379. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17380. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17381. (define-key org-mode-map [remap outline-forward-same-level]
  17382. 'org-forward-heading-same-level)
  17383. (define-key org-mode-map [remap outline-backward-same-level]
  17384. 'org-backward-heading-same-level)
  17385. (define-key org-mode-map [remap show-branches]
  17386. 'org-kill-note-or-show-branches)
  17387. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17388. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17389. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17390. ;; Outline functions from `outline-mode-prefix-map' that can not
  17391. ;; be remapped in Org:
  17392. ;;
  17393. ;; - the column "key binding" shows whether the Outline function is still
  17394. ;; available in Org mode on the same key that it has been bound to in
  17395. ;; Outline mode:
  17396. ;; - "overridden": key used for a different functionality in Org mode
  17397. ;; - else: key still bound to the same Outline function in Org mode
  17398. ;;
  17399. ;; | Outline function | key binding | Org replacement |
  17400. ;; |------------------------------------+-------------+-----------------------|
  17401. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17402. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17403. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17404. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17405. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17406. ;; | `show-entry' | overridden | no replacement |
  17407. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17408. ;; | `show-branches' | `C-c C-k' | still same function |
  17409. ;; | `show-subtree' | overridden | visibility cycling |
  17410. ;; | `show-all' | overridden | no replacement |
  17411. ;; | `hide-subtree' | overridden | visibility cycling |
  17412. ;; | `hide-body' | overridden | no replacement |
  17413. ;; | `hide-entry' | overridden | visibility cycling |
  17414. ;; | `hide-leaves' | overridden | no replacement |
  17415. ;; | `hide-sublevels' | overridden | no replacement |
  17416. ;; | `hide-other' | overridden | no replacement |
  17417. ;; Make `C-c C-x' a prefix key
  17418. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17419. ;; TAB key with modifiers
  17420. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17421. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17422. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17423. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17424. ;; The following line is necessary under Suse GNU/Linux
  17425. (unless (featurep 'xemacs)
  17426. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17427. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17428. (define-key org-mode-map [backtab] 'org-shifttab)
  17429. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17430. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17431. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17432. ;; Cursor keys with modifiers
  17433. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17434. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17435. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17436. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17437. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17438. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17439. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17440. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17441. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17442. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17443. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17444. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17445. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17446. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17447. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17448. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17449. ;; Babel keys
  17450. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17451. (mapc (lambda (pair)
  17452. (define-key org-babel-map (car pair) (cdr pair)))
  17453. org-babel-key-bindings)
  17454. ;;; Extra keys for tty access.
  17455. ;; We only set them when really needed because otherwise the
  17456. ;; menus don't show the simple keys
  17457. (when (or org-use-extra-keys
  17458. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17459. (not window-system))
  17460. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17461. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17462. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17463. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17464. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17465. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17466. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17467. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17468. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17469. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17470. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17471. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17472. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17473. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17474. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17475. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17476. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17477. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17478. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17479. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17480. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17481. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17482. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17483. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17484. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17485. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17486. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17487. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17488. ;; All the other keys
  17489. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17490. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17491. (if (boundp 'narrow-map)
  17492. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17493. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17494. (if (boundp 'narrow-map)
  17495. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17496. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17497. (if (boundp 'narrow-map)
  17498. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17499. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17500. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17501. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17502. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17503. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17504. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17505. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17506. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17507. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17508. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17509. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17510. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17511. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17512. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17513. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17514. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17515. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17516. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17517. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17518. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17519. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17520. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17521. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17522. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17523. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17524. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17525. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17526. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17527. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17528. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17529. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17530. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17531. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17532. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17533. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17534. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17535. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17536. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17537. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17538. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17539. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17540. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17541. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17542. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17543. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17544. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17545. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17546. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17547. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17548. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17549. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17550. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17551. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17552. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17553. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17554. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17555. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17556. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17557. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17558. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17559. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17560. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17561. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17562. (org-defkey org-mode-map "\C-m" 'org-return)
  17563. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17564. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17565. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17566. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17567. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17568. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17569. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17570. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17571. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17572. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17573. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17574. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17575. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17576. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17577. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17578. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17579. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17580. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17581. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17582. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17583. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17584. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17585. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17586. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17587. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17588. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17589. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17590. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17591. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17592. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17593. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17594. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17595. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17596. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17597. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17598. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17599. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17600. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17601. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17602. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17603. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17604. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17605. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17606. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17607. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17608. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17609. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17610. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17611. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17612. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17613. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17614. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17615. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17616. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17617. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17618. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17619. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17620. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17621. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17622. (when (featurep 'xemacs)
  17623. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17624. (defconst org-speed-commands-default
  17625. '(
  17626. ("Outline Navigation")
  17627. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17628. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17629. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17630. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17631. ("F" . org-next-block)
  17632. ("B" . org-previous-block)
  17633. ("u" . (org-speed-move-safe 'outline-up-heading))
  17634. ("j" . org-goto)
  17635. ("g" . (org-refile t))
  17636. ("Outline Visibility")
  17637. ("c" . org-cycle)
  17638. ("C" . org-shifttab)
  17639. (" " . org-display-outline-path)
  17640. ("s" . org-narrow-to-subtree)
  17641. ("=" . org-columns)
  17642. ("Outline Structure Editing")
  17643. ("U" . org-shiftmetaup)
  17644. ("D" . org-shiftmetadown)
  17645. ("r" . org-metaright)
  17646. ("l" . org-metaleft)
  17647. ("R" . org-shiftmetaright)
  17648. ("L" . org-shiftmetaleft)
  17649. ("i" . (progn (forward-char 1) (call-interactively
  17650. 'org-insert-heading-respect-content)))
  17651. ("^" . org-sort)
  17652. ("w" . org-refile)
  17653. ("a" . org-archive-subtree-default-with-confirmation)
  17654. ("@" . org-mark-subtree)
  17655. ("#" . org-toggle-comment)
  17656. ("Clock Commands")
  17657. ("I" . org-clock-in)
  17658. ("O" . org-clock-out)
  17659. ("Meta Data Editing")
  17660. ("t" . org-todo)
  17661. ("," . (org-priority))
  17662. ("0" . (org-priority ?\ ))
  17663. ("1" . (org-priority ?A))
  17664. ("2" . (org-priority ?B))
  17665. ("3" . (org-priority ?C))
  17666. (":" . org-set-tags-command)
  17667. ("e" . org-set-effort)
  17668. ("E" . org-inc-effort)
  17669. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17670. (org-entry-put (point) "APPT_WARNTIME" m)))
  17671. ("Agenda Views etc")
  17672. ("v" . org-agenda)
  17673. ("/" . org-sparse-tree)
  17674. ("Misc")
  17675. ("o" . org-open-at-point)
  17676. ("?" . org-speed-command-help)
  17677. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17678. (">" . (org-agenda-remove-restriction-lock))
  17679. )
  17680. "The default speed commands.")
  17681. (defun org-print-speed-command (e)
  17682. (if (> (length (car e)) 1)
  17683. (progn
  17684. (princ "\n")
  17685. (princ (car e))
  17686. (princ "\n")
  17687. (princ (make-string (length (car e)) ?-))
  17688. (princ "\n"))
  17689. (princ (car e))
  17690. (princ " ")
  17691. (if (symbolp (cdr e))
  17692. (princ (symbol-name (cdr e)))
  17693. (prin1 (cdr e)))
  17694. (princ "\n")))
  17695. (defun org-speed-command-help ()
  17696. "Show the available speed commands."
  17697. (interactive)
  17698. (if (not org-use-speed-commands)
  17699. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17700. (with-output-to-temp-buffer "*Help*"
  17701. (princ "User-defined Speed commands\n===========================\n")
  17702. (mapc 'org-print-speed-command org-speed-commands-user)
  17703. (princ "\n")
  17704. (princ "Built-in Speed commands\n=======================\n")
  17705. (mapc 'org-print-speed-command org-speed-commands-default))
  17706. (with-current-buffer "*Help*"
  17707. (setq truncate-lines t))))
  17708. (defun org-speed-move-safe (cmd)
  17709. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17710. If not, return to the original position and throw an error."
  17711. (interactive)
  17712. (let ((pos (point)))
  17713. (call-interactively cmd)
  17714. (unless (and (bolp) (org-at-heading-p))
  17715. (goto-char pos)
  17716. (error "Boundary reached while executing %s" cmd))))
  17717. (defvar org-self-insert-command-undo-counter 0)
  17718. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17719. (defvar org-speed-command nil)
  17720. (define-obsolete-function-alias
  17721. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17722. (defun org-speed-command-activate (keys)
  17723. "Hook for activating single-letter speed commands.
  17724. `org-speed-commands-default' specifies a minimal command set.
  17725. Use `org-speed-commands-user' for further customization."
  17726. (when (or (and (bolp) (looking-at org-outline-regexp))
  17727. (and (functionp org-use-speed-commands)
  17728. (funcall org-use-speed-commands)))
  17729. (cdr (assoc keys (append org-speed-commands-user
  17730. org-speed-commands-default)))))
  17731. (define-obsolete-function-alias
  17732. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17733. (defun org-babel-speed-command-activate (keys)
  17734. "Hook for activating single-letter code block commands."
  17735. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17736. (cdr (assoc keys org-babel-key-bindings))))
  17737. (defcustom org-speed-command-hook
  17738. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17739. "Hook for activating speed commands at strategic locations.
  17740. Hook functions are called in sequence until a valid handler is
  17741. found.
  17742. Each hook takes a single argument, a user-pressed command key
  17743. which is also a `self-insert-command' from the global map.
  17744. Within the hook, examine the cursor position and the command key
  17745. and return nil or a valid handler as appropriate. Handler could
  17746. be one of an interactive command, a function, or a form.
  17747. Set `org-use-speed-commands' to non-nil value to enable this
  17748. hook. The default setting is `org-speed-command-activate'."
  17749. :group 'org-structure
  17750. :version "24.1"
  17751. :type 'hook)
  17752. (defun org-self-insert-command (N)
  17753. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17754. If the cursor is in a table looking at whitespace, the whitespace is
  17755. overwritten, and the table is not marked as requiring realignment."
  17756. (interactive "p")
  17757. (org-check-before-invisible-edit 'insert)
  17758. (cond
  17759. ((and org-use-speed-commands
  17760. (setq org-speed-command
  17761. (run-hook-with-args-until-success
  17762. 'org-speed-command-hook (this-command-keys))))
  17763. (cond
  17764. ((commandp org-speed-command)
  17765. (setq this-command org-speed-command)
  17766. (call-interactively org-speed-command))
  17767. ((functionp org-speed-command)
  17768. (funcall org-speed-command))
  17769. ((and org-speed-command (listp org-speed-command))
  17770. (eval org-speed-command))
  17771. (t (let (org-use-speed-commands)
  17772. (call-interactively 'org-self-insert-command)))))
  17773. ((and
  17774. (org-table-p)
  17775. (progn
  17776. ;; check if we blank the field, and if that triggers align
  17777. (and (featurep 'org-table) org-table-auto-blank-field
  17778. (member last-command
  17779. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17780. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17781. ;; got extra space, this field does not determine column width
  17782. (let (org-table-may-need-update) (org-table-blank-field))
  17783. ;; no extra space, this field may determine column width
  17784. (org-table-blank-field)))
  17785. t)
  17786. (eq N 1)
  17787. (looking-at "[^|\n]* |"))
  17788. (let (org-table-may-need-update)
  17789. (goto-char (1- (match-end 0)))
  17790. (backward-delete-char 1)
  17791. (goto-char (match-beginning 0))
  17792. (self-insert-command N)))
  17793. (t
  17794. (setq org-table-may-need-update t)
  17795. (self-insert-command N)
  17796. (org-fix-tags-on-the-fly)
  17797. (if org-self-insert-cluster-for-undo
  17798. (if (not (eq last-command 'org-self-insert-command))
  17799. (setq org-self-insert-command-undo-counter 1)
  17800. (if (>= org-self-insert-command-undo-counter 20)
  17801. (setq org-self-insert-command-undo-counter 1)
  17802. (and (> org-self-insert-command-undo-counter 0)
  17803. buffer-undo-list (listp buffer-undo-list)
  17804. (not (cadr buffer-undo-list)) ; remove nil entry
  17805. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17806. (setq org-self-insert-command-undo-counter
  17807. (1+ org-self-insert-command-undo-counter))))))))
  17808. (defun org-check-before-invisible-edit (kind)
  17809. "Check is editing if kind KIND would be dangerous with invisible text around.
  17810. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17811. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17812. (if (and org-catch-invisible-edits
  17813. (or (not (boundp 'visible-mode)) (not visible-mode))
  17814. (or (get-char-property (point) 'invisible)
  17815. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17816. ;; OK, we need to take a closer look
  17817. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17818. (invisible-before-point (if (bobp) nil (get-char-property
  17819. (1- (point)) 'invisible)))
  17820. (border-and-ok-direction
  17821. (or
  17822. ;; Check if we are acting predictably before invisible text
  17823. (and invisible-at-point (not invisible-before-point)
  17824. (memq kind '(insert delete-backward)))
  17825. ;; Check if we are acting predictably after invisible text
  17826. ;; This works not well, and I have turned it off. It seems
  17827. ;; better to always show and stop after invisible text.
  17828. ;; (and (not invisible-at-point) invisible-before-point
  17829. ;; (memq kind '(insert delete)))
  17830. )))
  17831. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17832. (memq invisible-before-point '(outline org-hide-block t)))
  17833. (if (eq org-catch-invisible-edits 'error)
  17834. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17835. (if (and org-custom-properties-overlays
  17836. (y-or-n-p "Display invisible properties in this buffer? "))
  17837. (org-toggle-custom-properties-visibility)
  17838. ;; Make the area visible
  17839. (save-excursion
  17840. (if invisible-before-point
  17841. (goto-char (previous-single-char-property-change
  17842. (point) 'invisible)))
  17843. (org-cycle))
  17844. (cond
  17845. ((eq org-catch-invisible-edits 'show)
  17846. ;; That's it, we do the edit after showing
  17847. (message
  17848. "Unfolding invisible region around point before editing")
  17849. (sit-for 1))
  17850. ((and (eq org-catch-invisible-edits 'smart)
  17851. border-and-ok-direction)
  17852. (message "Unfolding invisible region around point before editing"))
  17853. (t
  17854. ;; Don't do the edit, make the user repeat it in full visibility
  17855. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17856. (defun org-fix-tags-on-the-fly ()
  17857. (when (and (equal (char-after (point-at-bol)) ?*)
  17858. (org-at-heading-p))
  17859. (org-align-tags-here org-tags-column)))
  17860. (defun org-delete-backward-char (N)
  17861. "Like `delete-backward-char', insert whitespace at field end in tables.
  17862. When deleting backwards, in tables this function will insert whitespace in
  17863. front of the next \"|\" separator, to keep the table aligned. The table will
  17864. still be marked for re-alignment if the field did fill the entire column,
  17865. because, in this case the deletion might narrow the column."
  17866. (interactive "p")
  17867. (save-match-data
  17868. (org-check-before-invisible-edit 'delete-backward)
  17869. (if (and (org-table-p)
  17870. (eq N 1)
  17871. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17872. (looking-at ".*?|"))
  17873. (let ((pos (point))
  17874. (noalign (looking-at "[^|\n\r]* |"))
  17875. (c org-table-may-need-update))
  17876. (backward-delete-char N)
  17877. (if (not overwrite-mode)
  17878. (progn
  17879. (skip-chars-forward "^|")
  17880. (insert " ")
  17881. (goto-char (1- pos))))
  17882. ;; noalign: if there were two spaces at the end, this field
  17883. ;; does not determine the width of the column.
  17884. (if noalign (setq org-table-may-need-update c)))
  17885. (backward-delete-char N)
  17886. (org-fix-tags-on-the-fly))))
  17887. (defun org-delete-char (N)
  17888. "Like `delete-char', but insert whitespace at field end in tables.
  17889. When deleting characters, in tables this function will insert whitespace in
  17890. front of the next \"|\" separator, to keep the table aligned. The table will
  17891. still be marked for re-alignment if the field did fill the entire column,
  17892. because, in this case the deletion might narrow the column."
  17893. (interactive "p")
  17894. (save-match-data
  17895. (org-check-before-invisible-edit 'delete)
  17896. (if (and (org-table-p)
  17897. (not (bolp))
  17898. (not (= (char-after) ?|))
  17899. (eq N 1))
  17900. (if (looking-at ".*?|")
  17901. (let ((pos (point))
  17902. (noalign (looking-at "[^|\n\r]* |"))
  17903. (c org-table-may-need-update))
  17904. (replace-match
  17905. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17906. (goto-char pos)
  17907. ;; noalign: if there were two spaces at the end, this field
  17908. ;; does not determine the width of the column.
  17909. (if noalign (setq org-table-may-need-update c)))
  17910. (delete-char N))
  17911. (delete-char N)
  17912. (org-fix-tags-on-the-fly))))
  17913. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17914. (put 'org-self-insert-command 'delete-selection t)
  17915. (put 'orgtbl-self-insert-command 'delete-selection t)
  17916. (put 'org-delete-char 'delete-selection 'supersede)
  17917. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17918. (put 'org-yank 'delete-selection 'yank)
  17919. ;; Make `flyspell-mode' delay after some commands
  17920. (put 'org-self-insert-command 'flyspell-delayed t)
  17921. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17922. (put 'org-delete-char 'flyspell-delayed t)
  17923. (put 'org-delete-backward-char 'flyspell-delayed t)
  17924. ;; Make pabbrev-mode expand after org-mode commands
  17925. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17926. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17927. ;; How to do this: Measure non-white length of current string
  17928. ;; If equal to column width, we should realign.
  17929. (defun org-remap (map &rest commands)
  17930. "In MAP, remap the functions given in COMMANDS.
  17931. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17932. (let (new old)
  17933. (while commands
  17934. (setq old (pop commands) new (pop commands))
  17935. (if (fboundp 'command-remapping)
  17936. (org-defkey map (vector 'remap old) new)
  17937. (substitute-key-definition old new map global-map)))))
  17938. (defun org-transpose-words ()
  17939. "Transpose words for Org.
  17940. This uses the `org-mode-transpose-word-syntax-table' syntax
  17941. table, which interprets characters in `org-emphasis-alist' as
  17942. word constituants."
  17943. (interactive)
  17944. (with-syntax-table org-mode-transpose-word-syntax-table
  17945. (call-interactively 'transpose-words)))
  17946. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17947. (when (eq org-enable-table-editor 'optimized)
  17948. ;; If the user wants maximum table support, we need to hijack
  17949. ;; some standard editing functions
  17950. (org-remap org-mode-map
  17951. 'self-insert-command 'org-self-insert-command
  17952. 'delete-char 'org-delete-char
  17953. 'delete-backward-char 'org-delete-backward-char)
  17954. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17955. (defvar org-ctrl-c-ctrl-c-hook nil
  17956. "Hook for functions attaching themselves to `C-c C-c'.
  17957. This can be used to add additional functionality to the C-c C-c
  17958. key which executes context-dependent commands. This hook is run
  17959. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17960. run after the last test.
  17961. Each function will be called with no arguments. The function
  17962. must check if the context is appropriate for it to act. If yes,
  17963. it should do its thing and then return a non-nil value. If the
  17964. context is wrong, just do nothing and return nil.")
  17965. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17966. "Hook for functions attaching themselves to `C-c C-c'.
  17967. This can be used to add additional functionality to the C-c C-c
  17968. key which executes context-dependent commands. This hook is run
  17969. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17970. before the first test.
  17971. Each function will be called with no arguments. The function
  17972. must check if the context is appropriate for it to act. If yes,
  17973. it should do its thing and then return a non-nil value. If the
  17974. context is wrong, just do nothing and return nil.")
  17975. (defvar org-tab-first-hook nil
  17976. "Hook for functions to attach themselves to TAB.
  17977. See `org-ctrl-c-ctrl-c-hook' for more information.
  17978. This hook runs as the first action when TAB is pressed, even before
  17979. `org-cycle' messes around with the `outline-regexp' to cater for
  17980. inline tasks and plain list item folding.
  17981. If any function in this hook returns t, any other actions that
  17982. would have been caused by TAB (such as table field motion or visibility
  17983. cycling) will not occur.")
  17984. (defvar org-tab-after-check-for-table-hook nil
  17985. "Hook for functions to attach themselves to TAB.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.
  17987. This hook runs after it has been established that the cursor is not in a
  17988. table, but before checking if the cursor is in a headline or if global cycling
  17989. should be done.
  17990. If any function in this hook returns t, not other actions like visibility
  17991. cycling will be done.")
  17992. (defvar org-tab-after-check-for-cycling-hook nil
  17993. "Hook for functions to attach themselves to TAB.
  17994. See `org-ctrl-c-ctrl-c-hook' for more information.
  17995. This hook runs after it has been established that not table field motion and
  17996. not visibility should be done because of current context. This is probably
  17997. the place where a package like yasnippets can hook in.")
  17998. (defvar org-tab-before-tab-emulation-hook nil
  17999. "Hook for functions to attach themselves to TAB.
  18000. See `org-ctrl-c-ctrl-c-hook' for more information.
  18001. This hook runs after every other options for TAB have been exhausted, but
  18002. before indentation and \t insertion takes place.")
  18003. (defvar org-metaleft-hook nil
  18004. "Hook for functions attaching themselves to `M-left'.
  18005. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18006. (defvar org-metaright-hook nil
  18007. "Hook for functions attaching themselves to `M-right'.
  18008. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18009. (defvar org-metaup-hook nil
  18010. "Hook for functions attaching themselves to `M-up'.
  18011. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18012. (defvar org-metadown-hook nil
  18013. "Hook for functions attaching themselves to `M-down'.
  18014. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18015. (defvar org-shiftmetaleft-hook nil
  18016. "Hook for functions attaching themselves to `M-S-left'.
  18017. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18018. (defvar org-shiftmetaright-hook nil
  18019. "Hook for functions attaching themselves to `M-S-right'.
  18020. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18021. (defvar org-shiftmetaup-hook nil
  18022. "Hook for functions attaching themselves to `M-S-up'.
  18023. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18024. (defvar org-shiftmetadown-hook nil
  18025. "Hook for functions attaching themselves to `M-S-down'.
  18026. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18027. (defvar org-metareturn-hook nil
  18028. "Hook for functions attaching themselves to `M-RET'.
  18029. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18030. (defvar org-shiftup-hook nil
  18031. "Hook for functions attaching themselves to `S-up'.
  18032. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18033. (defvar org-shiftup-final-hook nil
  18034. "Hook for functions attaching themselves to `S-up'.
  18035. This one runs after all other options except shift-select have been excluded.
  18036. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18037. (defvar org-shiftdown-hook nil
  18038. "Hook for functions attaching themselves to `S-down'.
  18039. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18040. (defvar org-shiftdown-final-hook nil
  18041. "Hook for functions attaching themselves to `S-down'.
  18042. This one runs after all other options except shift-select have been excluded.
  18043. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18044. (defvar org-shiftleft-hook nil
  18045. "Hook for functions attaching themselves to `S-left'.
  18046. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18047. (defvar org-shiftleft-final-hook nil
  18048. "Hook for functions attaching themselves to `S-left'.
  18049. This one runs after all other options except shift-select have been excluded.
  18050. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18051. (defvar org-shiftright-hook nil
  18052. "Hook for functions attaching themselves to `S-right'.
  18053. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18054. (defvar org-shiftright-final-hook nil
  18055. "Hook for functions attaching themselves to `S-right'.
  18056. This one runs after all other options except shift-select have been excluded.
  18057. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18058. (defun org-modifier-cursor-error ()
  18059. "Throw an error, a modified cursor command was applied in wrong context."
  18060. (user-error "This command is active in special context like tables, headlines or items"))
  18061. (defun org-shiftselect-error ()
  18062. "Throw an error because Shift-Cursor command was applied in wrong context."
  18063. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18064. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18065. (user-error "This command works only in special context like headlines or timestamps")))
  18066. (defun org-call-for-shift-select (cmd)
  18067. (let ((this-command-keys-shift-translated t))
  18068. (call-interactively cmd)))
  18069. (defun org-shifttab (&optional arg)
  18070. "Global visibility cycling or move to previous table field.
  18071. Call `org-table-previous-field' within a table.
  18072. When ARG is nil, cycle globally through visibility states.
  18073. When ARG is a numeric prefix, show contents of this level."
  18074. (interactive "P")
  18075. (cond
  18076. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18077. ((integerp arg)
  18078. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18079. (message "Content view to level: %d" arg)
  18080. (org-content (prefix-numeric-value arg2))
  18081. (org-cycle-show-empty-lines t)
  18082. (setq org-cycle-global-status 'overview)))
  18083. (t (call-interactively 'org-global-cycle))))
  18084. (defun org-shiftmetaleft ()
  18085. "Promote subtree or delete table column.
  18086. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18087. `org-table-delete-column', depending on context. See the
  18088. individual commands for more information."
  18089. (interactive)
  18090. (cond
  18091. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18092. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18093. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18094. ((if (not (org-region-active-p)) (org-at-item-p)
  18095. (save-excursion (goto-char (region-beginning))
  18096. (org-at-item-p)))
  18097. (call-interactively 'org-outdent-item-tree))
  18098. (t (org-modifier-cursor-error))))
  18099. (defun org-shiftmetaright ()
  18100. "Demote subtree or insert table column.
  18101. Calls `org-demote-subtree', `org-indent-item-tree', or
  18102. `org-table-insert-column', depending on context. See the
  18103. individual commands for more information."
  18104. (interactive)
  18105. (cond
  18106. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18107. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18108. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18109. ((if (not (org-region-active-p)) (org-at-item-p)
  18110. (save-excursion (goto-char (region-beginning))
  18111. (org-at-item-p)))
  18112. (call-interactively 'org-indent-item-tree))
  18113. (t (org-modifier-cursor-error))))
  18114. (defun org-shiftmetaup (&optional arg)
  18115. "Move subtree up or kill table row.
  18116. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18117. `org-move-item-up' or `org-timestamp-up', depending on context.
  18118. See the individual commands for more information."
  18119. (interactive "P")
  18120. (cond
  18121. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18122. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18123. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18124. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18125. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18126. (call-interactively 'org-timestamp-up)))
  18127. (t (call-interactively 'org-drag-line-backward))))
  18128. (defun org-shiftmetadown (&optional arg)
  18129. "Move subtree down or insert table row.
  18130. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18131. `org-move-item-down' or `org-timestamp-up', depending on context.
  18132. See the individual commands for more information."
  18133. (interactive "P")
  18134. (cond
  18135. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18136. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18137. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18138. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18139. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18140. (call-interactively 'org-timestamp-down)))
  18141. (t (call-interactively 'org-drag-line-forward))))
  18142. (defsubst org-hidden-tree-error ()
  18143. (user-error
  18144. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18145. (defun org-metaleft (&optional arg)
  18146. "Promote heading or move table column to left.
  18147. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18148. With no specific context, calls the Emacs default `backward-word'.
  18149. See the individual commands for more information."
  18150. (interactive "P")
  18151. (cond
  18152. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18153. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18154. ((org-with-limited-levels
  18155. (or (org-at-heading-p)
  18156. (and (org-region-active-p)
  18157. (save-excursion
  18158. (goto-char (region-beginning))
  18159. (org-at-heading-p)))))
  18160. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18161. (call-interactively 'org-do-promote))
  18162. ;; At an inline task.
  18163. ((org-at-heading-p)
  18164. (call-interactively 'org-inlinetask-promote))
  18165. ((or (org-at-item-p)
  18166. (and (org-region-active-p)
  18167. (save-excursion
  18168. (goto-char (region-beginning))
  18169. (org-at-item-p))))
  18170. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18171. (call-interactively 'org-outdent-item))
  18172. (t (call-interactively 'backward-word))))
  18173. (defun org-metaright (&optional arg)
  18174. "Demote a subtree, a list item or move table column to right.
  18175. In front of a drawer or a block keyword, indent it correctly.
  18176. With no specific context, calls the Emacs default `forward-word'.
  18177. See the individual commands for more information."
  18178. (interactive "P")
  18179. (cond
  18180. ((run-hook-with-args-until-success 'org-metaright-hook))
  18181. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18182. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18183. ((org-at-block-p) (call-interactively 'org-indent-block))
  18184. ((org-with-limited-levels
  18185. (or (org-at-heading-p)
  18186. (and (org-region-active-p)
  18187. (save-excursion
  18188. (goto-char (region-beginning))
  18189. (org-at-heading-p)))))
  18190. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18191. (call-interactively 'org-do-demote))
  18192. ;; At an inline task.
  18193. ((org-at-heading-p)
  18194. (call-interactively 'org-inlinetask-demote))
  18195. ((or (org-at-item-p)
  18196. (and (org-region-active-p)
  18197. (save-excursion
  18198. (goto-char (region-beginning))
  18199. (org-at-item-p))))
  18200. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18201. (call-interactively 'org-indent-item))
  18202. (t (call-interactively 'forward-word))))
  18203. (defun org-check-for-hidden (what)
  18204. "Check if there are hidden headlines/items in the current visual line.
  18205. WHAT can be either `headlines' or `items'. If the current line is
  18206. an outline or item heading and it has a folded subtree below it,
  18207. this function returns t, nil otherwise."
  18208. (let ((re (cond
  18209. ((eq what 'headlines) org-outline-regexp-bol)
  18210. ((eq what 'items) (org-item-beginning-re))
  18211. (t (error "This should not happen"))))
  18212. beg end)
  18213. (save-excursion
  18214. (catch 'exit
  18215. (unless (org-region-active-p)
  18216. (setq beg (point-at-bol))
  18217. (beginning-of-line 2)
  18218. (while (and (not (eobp)) ;; this is like `next-line'
  18219. (get-char-property (1- (point)) 'invisible))
  18220. (beginning-of-line 2))
  18221. (setq end (point))
  18222. (goto-char beg)
  18223. (goto-char (point-at-eol))
  18224. (setq end (max end (point)))
  18225. (while (re-search-forward re end t)
  18226. (if (get-char-property (match-beginning 0) 'invisible)
  18227. (throw 'exit t))))
  18228. nil))))
  18229. (defun org-metaup (&optional arg)
  18230. "Move subtree up or move table row up.
  18231. Calls `org-move-subtree-up' or `org-table-move-row' or
  18232. `org-move-item-up', depending on context. See the individual commands
  18233. for more information."
  18234. (interactive "P")
  18235. (cond
  18236. ((run-hook-with-args-until-success 'org-metaup-hook))
  18237. ((org-region-active-p)
  18238. (let* ((a (min (region-beginning) (region-end)))
  18239. (b (1- (max (region-beginning) (region-end))))
  18240. (c (save-excursion (goto-char a)
  18241. (move-beginning-of-line 0)))
  18242. (d (save-excursion (goto-char a)
  18243. (move-end-of-line 0) (point))))
  18244. (transpose-regions a b c d)
  18245. (goto-char c)))
  18246. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18247. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18248. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18249. (t (org-drag-element-backward))))
  18250. (defun org-metadown (&optional arg)
  18251. "Move subtree down or move table row down.
  18252. Calls `org-move-subtree-down' or `org-table-move-row' or
  18253. `org-move-item-down', depending on context. See the individual
  18254. commands for more information."
  18255. (interactive "P")
  18256. (cond
  18257. ((run-hook-with-args-until-success 'org-metadown-hook))
  18258. ((org-region-active-p)
  18259. (let* ((a (min (region-beginning) (region-end)))
  18260. (b (max (region-beginning) (region-end)))
  18261. (c (save-excursion (goto-char b)
  18262. (move-beginning-of-line 1)))
  18263. (d (save-excursion (goto-char b)
  18264. (move-end-of-line 1) (1+ (point)))))
  18265. (transpose-regions a b c d)
  18266. (goto-char d)))
  18267. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18268. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18269. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18270. (t (org-drag-element-forward))))
  18271. (defun org-shiftup (&optional arg)
  18272. "Increase item in timestamp or increase priority of current headline.
  18273. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18274. depending on context. See the individual commands for more information."
  18275. (interactive "P")
  18276. (cond
  18277. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18278. ((and org-support-shift-select (org-region-active-p))
  18279. (org-call-for-shift-select 'previous-line))
  18280. ((org-at-timestamp-p t)
  18281. (call-interactively (if org-edit-timestamp-down-means-later
  18282. 'org-timestamp-down 'org-timestamp-up)))
  18283. ((and (not (eq org-support-shift-select 'always))
  18284. org-enable-priority-commands
  18285. (org-at-heading-p))
  18286. (call-interactively 'org-priority-up))
  18287. ((and (not org-support-shift-select) (org-at-item-p))
  18288. (call-interactively 'org-previous-item))
  18289. ((org-clocktable-try-shift 'up arg))
  18290. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18291. (org-support-shift-select
  18292. (org-call-for-shift-select 'previous-line))
  18293. (t (org-shiftselect-error))))
  18294. (defun org-shiftdown (&optional arg)
  18295. "Decrease item in timestamp or decrease priority of current headline.
  18296. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18297. depending on context. See the individual commands for more information."
  18298. (interactive "P")
  18299. (cond
  18300. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18301. ((and org-support-shift-select (org-region-active-p))
  18302. (org-call-for-shift-select 'next-line))
  18303. ((org-at-timestamp-p t)
  18304. (call-interactively (if org-edit-timestamp-down-means-later
  18305. 'org-timestamp-up 'org-timestamp-down)))
  18306. ((and (not (eq org-support-shift-select 'always))
  18307. org-enable-priority-commands
  18308. (org-at-heading-p))
  18309. (call-interactively 'org-priority-down))
  18310. ((and (not org-support-shift-select) (org-at-item-p))
  18311. (call-interactively 'org-next-item))
  18312. ((org-clocktable-try-shift 'down arg))
  18313. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18314. (org-support-shift-select
  18315. (org-call-for-shift-select 'next-line))
  18316. (t (org-shiftselect-error))))
  18317. (defun org-shiftright (&optional arg)
  18318. "Cycle the thing at point or in the current line, depending on context.
  18319. Depending on context, this does one of the following:
  18320. - switch a timestamp at point one day into the future
  18321. - on a headline, switch to the next TODO keyword.
  18322. - on an item, switch entire list to the next bullet type
  18323. - on a property line, switch to the next allowed value
  18324. - on a clocktable definition line, move time block into the future"
  18325. (interactive "P")
  18326. (cond
  18327. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18328. ((and org-support-shift-select (org-region-active-p))
  18329. (org-call-for-shift-select 'forward-char))
  18330. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18331. ((and (not (eq org-support-shift-select 'always))
  18332. (org-at-heading-p))
  18333. (let ((org-inhibit-logging
  18334. (not org-treat-S-cursor-todo-selection-as-state-change))
  18335. (org-inhibit-blocking
  18336. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18337. (org-call-with-arg 'org-todo 'right)))
  18338. ((or (and org-support-shift-select
  18339. (not (eq org-support-shift-select 'always))
  18340. (org-at-item-bullet-p))
  18341. (and (not org-support-shift-select) (org-at-item-p)))
  18342. (org-call-with-arg 'org-cycle-list-bullet nil))
  18343. ((and (not (eq org-support-shift-select 'always))
  18344. (org-at-property-p))
  18345. (call-interactively 'org-property-next-allowed-value))
  18346. ((org-clocktable-try-shift 'right arg))
  18347. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18348. (org-support-shift-select
  18349. (org-call-for-shift-select 'forward-char))
  18350. (t (org-shiftselect-error))))
  18351. (defun org-shiftleft (&optional arg)
  18352. "Cycle the thing at point or in the current line, depending on context.
  18353. Depending on context, this does one of the following:
  18354. - switch a timestamp at point one day into the past
  18355. - on a headline, switch to the previous TODO keyword.
  18356. - on an item, switch entire list to the previous bullet type
  18357. - on a property line, switch to the previous allowed value
  18358. - on a clocktable definition line, move time block into the past"
  18359. (interactive "P")
  18360. (cond
  18361. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18362. ((and org-support-shift-select (org-region-active-p))
  18363. (org-call-for-shift-select 'backward-char))
  18364. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18365. ((and (not (eq org-support-shift-select 'always))
  18366. (org-at-heading-p))
  18367. (let ((org-inhibit-logging
  18368. (not org-treat-S-cursor-todo-selection-as-state-change))
  18369. (org-inhibit-blocking
  18370. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18371. (org-call-with-arg 'org-todo 'left)))
  18372. ((or (and org-support-shift-select
  18373. (not (eq org-support-shift-select 'always))
  18374. (org-at-item-bullet-p))
  18375. (and (not org-support-shift-select) (org-at-item-p)))
  18376. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18377. ((and (not (eq org-support-shift-select 'always))
  18378. (org-at-property-p))
  18379. (call-interactively 'org-property-previous-allowed-value))
  18380. ((org-clocktable-try-shift 'left arg))
  18381. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18382. (org-support-shift-select
  18383. (org-call-for-shift-select 'backward-char))
  18384. (t (org-shiftselect-error))))
  18385. (defun org-shiftcontrolright ()
  18386. "Switch to next TODO set."
  18387. (interactive)
  18388. (cond
  18389. ((and org-support-shift-select (org-region-active-p))
  18390. (org-call-for-shift-select 'forward-word))
  18391. ((and (not (eq org-support-shift-select 'always))
  18392. (org-at-heading-p))
  18393. (org-call-with-arg 'org-todo 'nextset))
  18394. (org-support-shift-select
  18395. (org-call-for-shift-select 'forward-word))
  18396. (t (org-shiftselect-error))))
  18397. (defun org-shiftcontrolleft ()
  18398. "Switch to previous TODO set."
  18399. (interactive)
  18400. (cond
  18401. ((and org-support-shift-select (org-region-active-p))
  18402. (org-call-for-shift-select 'backward-word))
  18403. ((and (not (eq org-support-shift-select 'always))
  18404. (org-at-heading-p))
  18405. (org-call-with-arg 'org-todo 'previousset))
  18406. (org-support-shift-select
  18407. (org-call-for-shift-select 'backward-word))
  18408. (t (org-shiftselect-error))))
  18409. (defun org-shiftcontrolup (&optional n)
  18410. "Change timestamps synchronously up in CLOCK log lines.
  18411. Optional argument N tells to change by that many units."
  18412. (interactive "P")
  18413. (cond ((and (not org-support-shift-select)
  18414. (org-at-clock-log-p)
  18415. (org-at-timestamp-p t))
  18416. (org-clock-timestamps-up n))
  18417. (t (org-shiftselect-error))))
  18418. (defun org-shiftcontroldown (&optional n)
  18419. "Change timestamps synchronously down in CLOCK log lines.
  18420. Optional argument N tells to change by that many units."
  18421. (interactive "P")
  18422. (cond ((and (not org-support-shift-select)
  18423. (org-at-clock-log-p)
  18424. (org-at-timestamp-p t))
  18425. (org-clock-timestamps-down n))
  18426. (t (org-shiftselect-error))))
  18427. (defun org-ctrl-c-ret ()
  18428. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18429. (interactive)
  18430. (cond
  18431. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18432. (t (call-interactively 'org-insert-heading))))
  18433. (defun org-find-visible ()
  18434. (let ((s (point)))
  18435. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18436. (get-char-property s 'invisible)))
  18437. s))
  18438. (defun org-find-invisible ()
  18439. (let ((s (point)))
  18440. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18441. (not (get-char-property s 'invisible))))
  18442. s))
  18443. (defun org-copy-visible (beg end)
  18444. "Copy the visible parts of the region."
  18445. (interactive "r")
  18446. (let (snippets s)
  18447. (save-excursion
  18448. (save-restriction
  18449. (narrow-to-region beg end)
  18450. (setq s (goto-char (point-min)))
  18451. (while (not (= (point) (point-max)))
  18452. (goto-char (org-find-invisible))
  18453. (push (buffer-substring s (point)) snippets)
  18454. (setq s (goto-char (org-find-visible))))))
  18455. (kill-new (apply 'concat (nreverse snippets)))))
  18456. (defun org-copy-special ()
  18457. "Copy region in table or copy current subtree.
  18458. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18459. See the individual commands for more information."
  18460. (interactive)
  18461. (call-interactively
  18462. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18463. (defun org-cut-special ()
  18464. "Cut region in table or cut current subtree.
  18465. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18466. See the individual commands for more information."
  18467. (interactive)
  18468. (call-interactively
  18469. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18470. (defun org-paste-special (arg)
  18471. "Paste rectangular region into table, or past subtree relative to level.
  18472. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18473. See the individual commands for more information."
  18474. (interactive "P")
  18475. (if (org-at-table-p)
  18476. (org-table-paste-rectangle)
  18477. (org-paste-subtree arg)))
  18478. (defsubst org-in-fixed-width-region-p ()
  18479. "Is point in a fixed-width region?"
  18480. (save-match-data
  18481. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18482. (defun org-edit-special (&optional arg)
  18483. "Call a special editor for the element at point.
  18484. When at a table, call the formula editor with `org-table-edit-formulas'.
  18485. When in a source code block, call `org-edit-src-code'.
  18486. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18487. When at an #+INCLUDE keyword, visit the included file.
  18488. On a link, call `ffap' to visit the link at point.
  18489. Otherwise, return a user error."
  18490. (interactive "P")
  18491. (let ((element (org-element-at-point)))
  18492. (assert (not buffer-read-only) nil
  18493. "Buffer is read-only: %s" (buffer-name))
  18494. (case (org-element-type element)
  18495. (src-block
  18496. (if (not arg) (org-edit-src-code)
  18497. (let* ((info (org-babel-get-src-block-info))
  18498. (lang (nth 0 info))
  18499. (params (nth 2 info))
  18500. (session (cdr (assq :session params))))
  18501. (if (not session) (org-edit-src-code)
  18502. ;; At a src-block with a session and function called with
  18503. ;; an ARG: switch to the buffer related to the inferior
  18504. ;; process.
  18505. (switch-to-buffer
  18506. (funcall (intern (concat "org-babel-prep-session:" lang))
  18507. session params))))))
  18508. (keyword
  18509. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18510. (find-file
  18511. (org-remove-double-quotes
  18512. (car (org-split-string (org-element-property :value element)))))
  18513. (user-error "No special environment to edit here")))
  18514. (table
  18515. (if (eq (org-element-property :type element) 'table.el)
  18516. (org-edit-src-code)
  18517. (call-interactively 'org-table-edit-formulas)))
  18518. ;; Only Org tables contain `table-row' type elements.
  18519. (table-row (call-interactively 'org-table-edit-formulas))
  18520. ((example-block export-block) (org-edit-src-code))
  18521. (fixed-width (org-edit-fixed-width-region))
  18522. (otherwise
  18523. ;; No notable element at point. Though, we may be at a link,
  18524. ;; which is an object. Thus, scan deeper.
  18525. (if (eq (org-element-type (org-element-context element)) 'link)
  18526. (call-interactively 'ffap)
  18527. (user-error "No special environment to edit here"))))))
  18528. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18529. (defun org-ctrl-c-ctrl-c (&optional arg)
  18530. "Set tags in headline, or update according to changed information at point.
  18531. This command does many different things, depending on context:
  18532. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18533. this is what we do.
  18534. - If the cursor is on a statistics cookie, update it.
  18535. - If the cursor is in a headline, prompt for tags and insert them
  18536. into the current line, aligned to `org-tags-column'. When called
  18537. with prefix arg, realign all tags in the current buffer.
  18538. - If the cursor is in one of the special #+KEYWORD lines, this
  18539. triggers scanning the buffer for these lines and updating the
  18540. information.
  18541. - If the cursor is inside a table, realign the table. This command
  18542. works even if the automatic table editor has been turned off.
  18543. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18544. the entire table.
  18545. - If the cursor is at a footnote reference or definition, jump to
  18546. the corresponding definition or references, respectively.
  18547. - If the cursor is a the beginning of a dynamic block, update it.
  18548. - If the current buffer is a capture buffer, close note and file it.
  18549. - If the cursor is on a <<<target>>>, update radio targets and
  18550. corresponding links in this buffer.
  18551. - If the cursor is on a numbered item in a plain list, renumber the
  18552. ordered list.
  18553. - If the cursor is on a checkbox, toggle it.
  18554. - If the cursor is on a code block, evaluate it. The variable
  18555. `org-confirm-babel-evaluate' can be used to control prompting
  18556. before code block evaluation, by default every code block
  18557. evaluation requires confirmation. Code block evaluation can be
  18558. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18559. (interactive "P")
  18560. (cond
  18561. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18562. org-occur-highlights
  18563. org-latex-fragment-image-overlays)
  18564. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18565. (org-remove-occur-highlights)
  18566. (org-remove-latex-fragment-image-overlays)
  18567. (message "Temporary highlights/overlays removed from current buffer"))
  18568. ((and (local-variable-p 'org-finish-function (current-buffer))
  18569. (fboundp org-finish-function))
  18570. (funcall org-finish-function))
  18571. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18572. (t
  18573. (let* ((context (org-element-context)) (type (org-element-type context)))
  18574. ;; Test if point is within a blank line.
  18575. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18576. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18577. (user-error "C-c C-c can do nothing useful at this location"))
  18578. ;; When at a link, act according to the parent instead.
  18579. (when (eq type 'link)
  18580. (setq context (org-element-property :parent context))
  18581. (setq type (org-element-type context)))
  18582. ;; For convenience: at the first line of a paragraph on the
  18583. ;; same line as an item, apply function on that item instead.
  18584. (when (eq type 'paragraph)
  18585. (let ((parent (org-element-property :parent context)))
  18586. (when (and (eq (org-element-type parent) 'item)
  18587. (= (point-at-bol) (org-element-property :begin parent)))
  18588. (setq context parent type 'item))))
  18589. ;; Act according to type of element or object at point.
  18590. (case type
  18591. (clock (org-clock-update-time-maybe))
  18592. (dynamic-block
  18593. (save-excursion
  18594. (goto-char (org-element-property :post-affiliated context))
  18595. (org-update-dblock)))
  18596. (footnote-definition
  18597. (goto-char (org-element-property :post-affiliated context))
  18598. (call-interactively 'org-footnote-action))
  18599. (footnote-reference (call-interactively 'org-footnote-action))
  18600. ((headline inlinetask)
  18601. (save-excursion (goto-char (org-element-property :begin context))
  18602. (call-interactively 'org-set-tags)))
  18603. (item
  18604. ;; At an item: a double C-u set checkbox to "[-]"
  18605. ;; unconditionally, whereas a single one will toggle its
  18606. ;; presence. Without an universal argument, if the item
  18607. ;; has a checkbox, toggle it. Otherwise repair the list.
  18608. (let* ((box (org-element-property :checkbox context))
  18609. (struct (org-element-property :structure context))
  18610. (old-struct (copy-tree struct))
  18611. (parents (org-list-parents-alist struct))
  18612. (prevs (org-list-prevs-alist struct))
  18613. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18614. (org-list-set-checkbox
  18615. (org-element-property :begin context) struct
  18616. (cond ((equal arg '(16)) "[-]")
  18617. ((and (not box) (equal arg '(4))) "[ ]")
  18618. ((or (not box) (equal arg '(4))) nil)
  18619. ((eq box 'on) "[ ]")
  18620. (t "[X]")))
  18621. ;; Mimic `org-list-write-struct' but with grabbing
  18622. ;; a return value from `org-list-struct-fix-box'.
  18623. (org-list-struct-fix-ind struct parents 2)
  18624. (org-list-struct-fix-item-end struct)
  18625. (org-list-struct-fix-bul struct prevs)
  18626. (org-list-struct-fix-ind struct parents)
  18627. (let ((block-item
  18628. (org-list-struct-fix-box struct parents prevs orderedp)))
  18629. (if (and box (equal struct old-struct))
  18630. (if (equal arg '(16))
  18631. (message "Checkboxes already reset")
  18632. (user-error "Cannot toggle this checkbox: %s"
  18633. (if (eq box 'on)
  18634. "all subitems checked"
  18635. "unchecked subitems")))
  18636. (org-list-struct-apply-struct struct old-struct)
  18637. (org-update-checkbox-count-maybe))
  18638. (when block-item
  18639. (message "Checkboxes were removed due to empty box at line %d"
  18640. (org-current-line block-item))))))
  18641. (keyword
  18642. (let ((org-inhibit-startup-visibility-stuff t)
  18643. (org-startup-align-all-tables nil))
  18644. (when (boundp 'org-table-coordinate-overlays)
  18645. (mapc 'delete-overlay org-table-coordinate-overlays)
  18646. (setq org-table-coordinate-overlays nil))
  18647. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18648. (message "Local setup has been refreshed"))
  18649. (plain-list
  18650. ;; At a plain list, with a double C-u argument, set
  18651. ;; checkboxes of each item to "[-]", whereas a single one
  18652. ;; will toggle their presence according to the state of the
  18653. ;; first item in the list. Without an argument, repair the
  18654. ;; list.
  18655. (let* ((begin (org-element-property :contents-begin context))
  18656. (beginm (move-marker (make-marker) begin))
  18657. (struct (org-element-property :structure context))
  18658. (old-struct (copy-tree struct))
  18659. (first-box (save-excursion
  18660. (goto-char begin)
  18661. (looking-at org-list-full-item-re)
  18662. (match-string-no-properties 3)))
  18663. (new-box (cond ((equal arg '(16)) "[-]")
  18664. ((equal arg '(4)) (unless first-box "[ ]"))
  18665. ((equal first-box "[X]") "[ ]")
  18666. (t "[X]"))))
  18667. (cond
  18668. (arg
  18669. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18670. (org-list-get-all-items
  18671. begin struct (org-list-prevs-alist struct))))
  18672. ((and first-box (eq (point) begin))
  18673. ;; For convenience, when point is at bol on the first
  18674. ;; item of the list and no argument is provided, simply
  18675. ;; toggle checkbox of that item, if any.
  18676. (org-list-set-checkbox begin struct new-box)))
  18677. (org-list-write-struct
  18678. struct (org-list-parents-alist struct) old-struct)
  18679. (org-update-checkbox-count-maybe)
  18680. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18681. ((property-drawer node-property)
  18682. (call-interactively 'org-property-action))
  18683. ((radio-target target)
  18684. (call-interactively 'org-update-radio-target-regexp))
  18685. (statistics-cookie
  18686. (call-interactively 'org-update-statistics-cookies))
  18687. ((table table-cell table-row)
  18688. ;; At a table, recalculate every field and align it. Also
  18689. ;; send the table if necessary. If the table has
  18690. ;; a `table.el' type, just give up. At a table row or
  18691. ;; cell, maybe recalculate line but always align table.
  18692. (if (eq (org-element-property :type context) 'table.el)
  18693. (message "Use C-c ' to edit table.el tables")
  18694. (let ((org-enable-table-editor t))
  18695. (if (or (eq type 'table)
  18696. ;; Check if point is at a TBLFM line.
  18697. (and (eq type 'table-row)
  18698. (= (point) (org-element-property :end context))))
  18699. (save-excursion
  18700. (if (org-at-TBLFM-p)
  18701. (progn (require 'org-table)
  18702. (org-table-calc-current-TBLFM))
  18703. (goto-char (org-element-property :contents-begin context))
  18704. (org-call-with-arg 'org-table-recalculate (or arg t))
  18705. (orgtbl-send-table 'maybe)))
  18706. (org-table-maybe-eval-formula)
  18707. (cond (arg (call-interactively 'org-table-recalculate))
  18708. ((org-table-maybe-recalculate-line))
  18709. (t (org-table-align)))))))
  18710. (timestamp (org-timestamp-change 0 'day))
  18711. (otherwise
  18712. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18713. (user-error
  18714. "C-c C-c can do nothing useful at this location")))))))))
  18715. (defun org-mode-restart ()
  18716. "Restart Org-mode, to scan again for special lines.
  18717. Also updates the keyword regular expressions."
  18718. (interactive)
  18719. (org-mode)
  18720. (message "Org-mode restarted"))
  18721. (defun org-kill-note-or-show-branches ()
  18722. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18723. (interactive)
  18724. (if (not org-finish-function)
  18725. (progn
  18726. (hide-subtree)
  18727. (call-interactively 'show-branches))
  18728. (let ((org-note-abort t))
  18729. (funcall org-finish-function))))
  18730. (defun org-open-line (n)
  18731. "Insert a new row in tables, call `open-line' elsewhere.
  18732. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18733. (interactive "*p")
  18734. (cond
  18735. ((not org-special-ctrl-o)
  18736. (open-line n))
  18737. ((org-at-table-p)
  18738. (org-table-insert-row))
  18739. (t
  18740. (open-line n))))
  18741. (defun org-return (&optional indent)
  18742. "Goto next table row or insert a newline.
  18743. Calls `org-table-next-row' or `newline', depending on context.
  18744. See the individual commands for more information."
  18745. (interactive)
  18746. (let (org-ts-what)
  18747. (cond
  18748. ((or (bobp) (org-in-src-block-p))
  18749. (if indent (newline-and-indent) (newline)))
  18750. ((org-at-table-p)
  18751. (org-table-justify-field-maybe)
  18752. (call-interactively 'org-table-next-row))
  18753. ;; when `newline-and-indent' is called within a list, make sure
  18754. ;; text moved stays inside the item.
  18755. ((and (org-in-item-p) indent)
  18756. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18757. (progn
  18758. (save-match-data (newline))
  18759. (org-indent-line-to (length (match-string 0))))
  18760. (let ((ind (org-get-indentation)))
  18761. (newline)
  18762. (if (org-looking-back org-list-end-re)
  18763. (org-indent-line)
  18764. (org-indent-line-to ind)))))
  18765. ((and org-return-follows-link
  18766. (org-at-timestamp-p t)
  18767. (not (eq org-ts-what 'after)))
  18768. (org-follow-timestamp-link))
  18769. ((and org-return-follows-link
  18770. (let ((tprop (get-text-property (point) 'face)))
  18771. (or (eq tprop 'org-link)
  18772. (and (listp tprop) (memq 'org-link tprop)))))
  18773. (call-interactively 'org-open-at-point))
  18774. ((and (org-at-heading-p)
  18775. (looking-at
  18776. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18777. (org-show-entry)
  18778. (end-of-line 1)
  18779. (newline))
  18780. (t (if indent (newline-and-indent) (newline))))))
  18781. (defun org-return-indent ()
  18782. "Goto next table row or insert a newline and indent.
  18783. Calls `org-table-next-row' or `newline-and-indent', depending on
  18784. context. See the individual commands for more information."
  18785. (interactive)
  18786. (org-return t))
  18787. (defun org-ctrl-c-star ()
  18788. "Compute table, or change heading status of lines.
  18789. Calls `org-table-recalculate' or `org-toggle-heading',
  18790. depending on context."
  18791. (interactive)
  18792. (cond
  18793. ((org-at-table-p)
  18794. (call-interactively 'org-table-recalculate))
  18795. (t
  18796. ;; Convert all lines in region to list items
  18797. (call-interactively 'org-toggle-heading))))
  18798. (defun org-ctrl-c-minus ()
  18799. "Insert separator line in table or modify bullet status of line.
  18800. Also turns a plain line or a region of lines into list items.
  18801. Calls `org-table-insert-hline', `org-toggle-item', or
  18802. `org-cycle-list-bullet', depending on context."
  18803. (interactive)
  18804. (cond
  18805. ((org-at-table-p)
  18806. (call-interactively 'org-table-insert-hline))
  18807. ((org-region-active-p)
  18808. (call-interactively 'org-toggle-item))
  18809. ((org-in-item-p)
  18810. (call-interactively 'org-cycle-list-bullet))
  18811. (t
  18812. (call-interactively 'org-toggle-item))))
  18813. (defun org-toggle-item (arg)
  18814. "Convert headings or normal lines to items, items to normal lines.
  18815. If there is no active region, only the current line is considered.
  18816. If the first non blank line in the region is a headline, convert
  18817. all headlines to items, shifting text accordingly.
  18818. If it is an item, convert all items to normal lines.
  18819. If it is normal text, change region into a list of items.
  18820. With a prefix argument ARG, change the region in a single item."
  18821. (interactive "P")
  18822. (let ((shift-text
  18823. (function
  18824. ;; Shift text in current section to IND, from point to END.
  18825. ;; The function leaves point to END line.
  18826. (lambda (ind end)
  18827. (let ((min-i 1000) (end (copy-marker end)))
  18828. ;; First determine the minimum indentation (MIN-I) of
  18829. ;; the text.
  18830. (save-excursion
  18831. (catch 'exit
  18832. (while (< (point) end)
  18833. (let ((i (org-get-indentation)))
  18834. (cond
  18835. ;; Skip blank lines and inline tasks.
  18836. ((looking-at "^[ \t]*$"))
  18837. ((looking-at org-outline-regexp-bol))
  18838. ;; We can't find less than 0 indentation.
  18839. ((zerop i) (throw 'exit (setq min-i 0)))
  18840. ((< i min-i) (setq min-i i))))
  18841. (forward-line))))
  18842. ;; Then indent each line so that a line indented to
  18843. ;; MIN-I becomes indented to IND. Ignore blank lines
  18844. ;; and inline tasks in the process.
  18845. (let ((delta (- ind min-i)))
  18846. (while (< (point) end)
  18847. (unless (or (looking-at "^[ \t]*$")
  18848. (looking-at org-outline-regexp-bol))
  18849. (org-indent-line-to (+ (org-get-indentation) delta)))
  18850. (forward-line)))))))
  18851. (skip-blanks
  18852. (function
  18853. ;; Return beginning of first non-blank line, starting from
  18854. ;; line at POS.
  18855. (lambda (pos)
  18856. (save-excursion
  18857. (goto-char pos)
  18858. (skip-chars-forward " \r\t\n")
  18859. (point-at-bol)))))
  18860. beg end)
  18861. ;; Determine boundaries of changes.
  18862. (if (org-region-active-p)
  18863. (setq beg (funcall skip-blanks (region-beginning))
  18864. end (copy-marker (region-end)))
  18865. (setq beg (funcall skip-blanks (point-at-bol))
  18866. end (copy-marker (point-at-eol))))
  18867. ;; Depending on the starting line, choose an action on the text
  18868. ;; between BEG and END.
  18869. (org-with-limited-levels
  18870. (save-excursion
  18871. (goto-char beg)
  18872. (cond
  18873. ;; Case 1. Start at an item: de-itemize. Note that it only
  18874. ;; happens when a region is active: `org-ctrl-c-minus'
  18875. ;; would call `org-cycle-list-bullet' otherwise.
  18876. ((org-at-item-p)
  18877. (while (< (point) end)
  18878. (when (org-at-item-p)
  18879. (skip-chars-forward " \t")
  18880. (delete-region (point) (match-end 0)))
  18881. (forward-line)))
  18882. ;; Case 2. Start at an heading: convert to items.
  18883. ((org-at-heading-p)
  18884. (let* ((bul (org-list-bullet-string "-"))
  18885. (bul-len (length bul))
  18886. ;; Indentation of the first heading. It should be
  18887. ;; relative to the indentation of its parent, if any.
  18888. (start-ind (save-excursion
  18889. (cond
  18890. ((not org-adapt-indentation) 0)
  18891. ((not (outline-previous-heading)) 0)
  18892. (t (length (match-string 0))))))
  18893. ;; Level of first heading. Further headings will be
  18894. ;; compared to it to determine hierarchy in the list.
  18895. (ref-level (org-reduced-level (org-outline-level))))
  18896. (while (< (point) end)
  18897. (let* ((level (org-reduced-level (org-outline-level)))
  18898. (delta (max 0 (- level ref-level))))
  18899. ;; If current headline is less indented than the first
  18900. ;; one, set it as reference, in order to preserve
  18901. ;; subtrees.
  18902. (when (< level ref-level) (setq ref-level level))
  18903. (replace-match bul t t)
  18904. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18905. ;; Ensure all text down to END (or SECTION-END) belongs
  18906. ;; to the newly created item.
  18907. (let ((section-end (save-excursion
  18908. (or (outline-next-heading) (point)))))
  18909. (forward-line)
  18910. (funcall shift-text
  18911. (+ start-ind (* (1+ delta) bul-len))
  18912. (min end section-end)))))))
  18913. ;; Case 3. Normal line with ARG: make the first line of region
  18914. ;; an item, and shift indentation of others lines to
  18915. ;; set them as item's body.
  18916. (arg (let* ((bul (org-list-bullet-string "-"))
  18917. (bul-len (length bul))
  18918. (ref-ind (org-get-indentation)))
  18919. (skip-chars-forward " \t")
  18920. (insert bul)
  18921. (forward-line)
  18922. (while (< (point) end)
  18923. ;; Ensure that lines less indented than first one
  18924. ;; still get included in item body.
  18925. (funcall shift-text
  18926. (+ ref-ind bul-len)
  18927. (min end (save-excursion (or (outline-next-heading)
  18928. (point)))))
  18929. (forward-line))))
  18930. ;; Case 4. Normal line without ARG: turn each non-item line
  18931. ;; into an item.
  18932. (t
  18933. (while (< (point) end)
  18934. (unless (or (org-at-heading-p) (org-at-item-p))
  18935. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18936. (replace-match
  18937. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18938. (forward-line))))))))
  18939. (defun org-toggle-heading (&optional nstars)
  18940. "Convert headings to normal text, or items or text to headings.
  18941. If there is no active region, only convert the current line.
  18942. With a \\[universal-argument] prefix, convert the whole list at
  18943. point into heading.
  18944. In a region:
  18945. - If the first non blank line is a headline, remove the stars
  18946. from all headlines in the region.
  18947. - If it is a normal line, turn each and every normal line (i.e.,
  18948. not an heading or an item) in the region into headings. If you
  18949. want to convert only the first line of this region, use one
  18950. universal prefix argument.
  18951. - If it is a plain list item, turn all plain list items into headings.
  18952. When converting a line into a heading, the number of stars is chosen
  18953. such that the lines become children of the current entry. However,
  18954. when a numeric prefix argument is given, its value determines the
  18955. number of stars to add."
  18956. (interactive "P")
  18957. (let ((skip-blanks
  18958. (function
  18959. ;; Return beginning of first non-blank line, starting from
  18960. ;; line at POS.
  18961. (lambda (pos)
  18962. (save-excursion
  18963. (goto-char pos)
  18964. (while (org-at-comment-p) (forward-line))
  18965. (skip-chars-forward " \r\t\n")
  18966. (point-at-bol)))))
  18967. beg end toggled)
  18968. ;; Determine boundaries of changes. If a universal prefix has
  18969. ;; been given, put the list in a region. If region ends at a bol,
  18970. ;; do not consider the last line to be in the region.
  18971. (when (and current-prefix-arg (org-at-item-p))
  18972. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18973. (org-mark-element))
  18974. (if (org-region-active-p)
  18975. (setq beg (funcall skip-blanks (region-beginning))
  18976. end (copy-marker (save-excursion
  18977. (goto-char (region-end))
  18978. (if (bolp) (point) (point-at-eol)))))
  18979. (setq beg (funcall skip-blanks (point-at-bol))
  18980. end (copy-marker (point-at-eol))))
  18981. ;; Ensure inline tasks don't count as headings.
  18982. (org-with-limited-levels
  18983. (save-excursion
  18984. (goto-char beg)
  18985. (cond
  18986. ;; Case 1. Started at an heading: de-star headings.
  18987. ((org-at-heading-p)
  18988. (while (< (point) end)
  18989. (when (org-at-heading-p t)
  18990. (looking-at org-outline-regexp) (replace-match "")
  18991. (setq toggled t))
  18992. (forward-line)))
  18993. ;; Case 2. Started at an item: change items into headlines.
  18994. ;; One star will be added by `org-list-to-subtree'.
  18995. ((org-at-item-p)
  18996. (let* ((stars (make-string
  18997. ;; subtract the star that will be added again by
  18998. ;; `org-list-to-subtree'
  18999. (if (numberp nstars) (1- nstars)
  19000. (or (org-current-level) 0))
  19001. ?*))
  19002. (add-stars
  19003. (cond (nstars "") ; stars from prefix only
  19004. ((equal stars "") "") ; before first heading
  19005. (org-odd-levels-only "*") ; inside heading, odd
  19006. (t "")))) ; inside heading, oddeven
  19007. (while (< (point) end)
  19008. (when (org-at-item-p)
  19009. ;; Pay attention to cases when region ends before list.
  19010. (let* ((struct (org-list-struct))
  19011. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19012. (save-restriction
  19013. (narrow-to-region (point) list-end)
  19014. (insert
  19015. (org-list-to-subtree
  19016. (org-list-parse-list t)
  19017. '(:istart (concat stars add-stars (funcall get-stars depth))
  19018. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19019. (setq toggled t))
  19020. (forward-line))))
  19021. ;; Case 3. Started at normal text: make every line an heading,
  19022. ;; skipping headlines and items.
  19023. (t (let* ((stars
  19024. (make-string
  19025. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19026. (add-stars
  19027. (cond (nstars "") ; stars from prefix only
  19028. ((equal stars "") "*") ; before first heading
  19029. (org-odd-levels-only "**") ; inside heading, odd
  19030. (t "*"))) ; inside heading, oddeven
  19031. (rpl (concat stars add-stars " "))
  19032. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19033. (while (< (point) (if (equal nstars '(4)) lend end))
  19034. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19035. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19036. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19037. (forward-line)))))))
  19038. (unless toggled (message "Cannot toggle heading from here"))))
  19039. (defun org-meta-return (&optional arg)
  19040. "Insert a new heading or wrap a region in a table.
  19041. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19042. on context. See the individual commands for more information."
  19043. (interactive "P")
  19044. (org-check-before-invisible-edit 'insert)
  19045. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19046. (let* ((element (org-element-at-point))
  19047. (type (org-element-type element)))
  19048. (when (eq type 'table-row)
  19049. (setq element (org-element-property :parent element))
  19050. (setq type 'table))
  19051. (if (and (eq type 'table)
  19052. (eq (org-element-property :type element) 'org)
  19053. (>= (point) (org-element-property :contents-begin element))
  19054. (< (point) (org-element-property :contents-end element)))
  19055. (call-interactively 'org-table-wrap-region)
  19056. (call-interactively 'org-insert-heading)))))
  19057. ;;; Menu entries
  19058. (defsubst org-in-subtree-not-table-p ()
  19059. "Are we in a subtree and not in a table?"
  19060. (and (not (org-before-first-heading-p))
  19061. (not (org-at-table-p))))
  19062. ;; Define the Org-mode menus
  19063. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19064. '("Tbl"
  19065. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19066. ["Next Field" org-cycle (org-at-table-p)]
  19067. ["Previous Field" org-shifttab (org-at-table-p)]
  19068. ["Next Row" org-return (org-at-table-p)]
  19069. "--"
  19070. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19071. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19072. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19073. "--"
  19074. ("Column"
  19075. ["Move Column Left" org-metaleft (org-at-table-p)]
  19076. ["Move Column Right" org-metaright (org-at-table-p)]
  19077. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19078. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19079. ("Row"
  19080. ["Move Row Up" org-metaup (org-at-table-p)]
  19081. ["Move Row Down" org-metadown (org-at-table-p)]
  19082. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19083. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19084. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19085. "--"
  19086. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19087. ("Rectangle"
  19088. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19089. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19090. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19091. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19092. "--"
  19093. ("Calculate"
  19094. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19095. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19096. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19097. "--"
  19098. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19099. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19100. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19101. "--"
  19102. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19103. "--"
  19104. ["Sum Column/Rectangle" org-table-sum
  19105. (or (org-at-table-p) (org-region-active-p))]
  19106. ["Which Column?" org-table-current-column (org-at-table-p)])
  19107. ["Debug Formulas"
  19108. org-table-toggle-formula-debugger
  19109. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19110. ["Show Col/Row Numbers"
  19111. org-table-toggle-coordinate-overlays
  19112. :style toggle
  19113. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19114. "--"
  19115. ["Create" org-table-create (and (not (org-at-table-p))
  19116. org-enable-table-editor)]
  19117. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19118. ["Import from File" org-table-import (not (org-at-table-p))]
  19119. ["Export to File" org-table-export (org-at-table-p)]
  19120. "--"
  19121. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19122. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19123. '("Org"
  19124. ("Show/Hide"
  19125. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19126. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19127. ["Sparse Tree..." org-sparse-tree t]
  19128. ["Reveal Context" org-reveal t]
  19129. ["Show All" show-all t]
  19130. "--"
  19131. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19132. "--"
  19133. ["New Heading" org-insert-heading t]
  19134. ("Navigate Headings"
  19135. ["Up" outline-up-heading t]
  19136. ["Next" outline-next-visible-heading t]
  19137. ["Previous" outline-previous-visible-heading t]
  19138. ["Next Same Level" outline-forward-same-level t]
  19139. ["Previous Same Level" outline-backward-same-level t]
  19140. "--"
  19141. ["Jump" org-goto t])
  19142. ("Edit Structure"
  19143. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19144. "--"
  19145. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19146. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19147. "--"
  19148. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19149. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19150. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19151. "--"
  19152. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19153. "--"
  19154. ["Copy visible text" org-copy-visible t]
  19155. "--"
  19156. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19157. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19158. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19159. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19160. "--"
  19161. ["Sort Region/Children" org-sort t]
  19162. "--"
  19163. ["Convert to odd levels" org-convert-to-odd-levels t]
  19164. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19165. ("Editing"
  19166. ["Emphasis..." org-emphasize t]
  19167. ["Edit Source Example" org-edit-special t]
  19168. "--"
  19169. ["Footnote new/jump" org-footnote-action t]
  19170. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19171. ("Archive"
  19172. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19173. "--"
  19174. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19175. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19176. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19177. )
  19178. "--"
  19179. ("Hyperlinks"
  19180. ["Store Link (Global)" org-store-link t]
  19181. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19182. ["Insert Link" org-insert-link t]
  19183. ["Follow Link" org-open-at-point t]
  19184. "--"
  19185. ["Next link" org-next-link t]
  19186. ["Previous link" org-previous-link t]
  19187. "--"
  19188. ["Descriptive Links"
  19189. org-toggle-link-display
  19190. :style radio
  19191. :selected org-descriptive-links
  19192. ]
  19193. ["Literal Links"
  19194. org-toggle-link-display
  19195. :style radio
  19196. :selected (not org-descriptive-links)])
  19197. "--"
  19198. ("TODO Lists"
  19199. ["TODO/DONE/-" org-todo t]
  19200. ("Select keyword"
  19201. ["Next keyword" org-shiftright (org-at-heading-p)]
  19202. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19203. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19204. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19205. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19206. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19207. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19208. "--"
  19209. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19210. :selected org-enforce-todo-dependencies :style toggle :active t]
  19211. "Settings for tree at point"
  19212. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19213. :selected (org-entry-get nil "ORDERED")
  19214. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19215. ["Do Children parallel" org-toggle-ordered-property :style radio
  19216. :selected (not (org-entry-get nil "ORDERED"))
  19217. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19218. "--"
  19219. ["Set Priority" org-priority t]
  19220. ["Priority Up" org-shiftup t]
  19221. ["Priority Down" org-shiftdown t]
  19222. "--"
  19223. ["Get news from all feeds" org-feed-update-all t]
  19224. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19225. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19226. ("TAGS and Properties"
  19227. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19228. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19229. "--"
  19230. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19231. ["Column view of properties" org-columns t]
  19232. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19233. ("Dates and Scheduling"
  19234. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19235. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19236. ("Change Date"
  19237. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19238. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19239. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19240. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19241. ["Compute Time Range" org-evaluate-time-range t]
  19242. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19243. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19244. "--"
  19245. ["Custom time format" org-toggle-time-stamp-overlays
  19246. :style radio :selected org-display-custom-times]
  19247. "--"
  19248. ["Goto Calendar" org-goto-calendar t]
  19249. ["Date from Calendar" org-date-from-calendar t]
  19250. "--"
  19251. ["Start/Restart Timer" org-timer-start t]
  19252. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19253. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19254. ["Insert Timer String" org-timer t]
  19255. ["Insert Timer Item" org-timer-item t])
  19256. ("Logging work"
  19257. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19258. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19259. ["Clock out" org-clock-out t]
  19260. ["Clock cancel" org-clock-cancel t]
  19261. "--"
  19262. ["Mark as default task" org-clock-mark-default-task t]
  19263. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19264. ["Goto running clock" org-clock-goto t]
  19265. "--"
  19266. ["Display times" org-clock-display t]
  19267. ["Create clock table" org-clock-report t]
  19268. "--"
  19269. ["Record DONE time"
  19270. (progn (setq org-log-done (not org-log-done))
  19271. (message "Switching to %s will %s record a timestamp"
  19272. (car org-done-keywords)
  19273. (if org-log-done "automatically" "not")))
  19274. :style toggle :selected org-log-done])
  19275. "--"
  19276. ["Agenda Command..." org-agenda t]
  19277. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19278. ("File List for Agenda")
  19279. ("Special views current file"
  19280. ["TODO Tree" org-show-todo-tree t]
  19281. ["Check Deadlines" org-check-deadlines t]
  19282. ["Timeline" org-timeline t]
  19283. ["Tags/Property tree" org-match-sparse-tree t])
  19284. "--"
  19285. ["Export/Publish..." org-export-dispatch t]
  19286. ("LaTeX"
  19287. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19288. :selected org-cdlatex-mode]
  19289. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19290. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19291. ["Modify math symbol" org-cdlatex-math-modify
  19292. (org-inside-LaTeX-fragment-p)]
  19293. ["Insert citation" org-reftex-citation t]
  19294. "--"
  19295. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19296. "--"
  19297. ("MobileOrg"
  19298. ["Push Files and Views" org-mobile-push t]
  19299. ["Get Captured and Flagged" org-mobile-pull t]
  19300. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19301. "--"
  19302. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19303. "--"
  19304. ("Documentation"
  19305. ["Show Version" org-version t]
  19306. ["Info Documentation" org-info t])
  19307. ("Customize"
  19308. ["Browse Org Group" org-customize t]
  19309. "--"
  19310. ["Expand This Menu" org-create-customize-menu
  19311. (fboundp 'customize-menu-create)])
  19312. ["Send bug report" org-submit-bug-report t]
  19313. "--"
  19314. ("Refresh/Reload"
  19315. ["Refresh setup current buffer" org-mode-restart t]
  19316. ["Reload Org (after update)" org-reload t]
  19317. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19318. ))
  19319. (defun org-info (&optional node)
  19320. "Read documentation for Org-mode in the info system.
  19321. With optional NODE, go directly to that node."
  19322. (interactive)
  19323. (info (format "(org)%s" (or node ""))))
  19324. ;;;###autoload
  19325. (defun org-submit-bug-report ()
  19326. "Submit a bug report on Org-mode via mail.
  19327. Don't hesitate to report any problems or inaccurate documentation.
  19328. If you don't have setup sending mail from (X)Emacs, please copy the
  19329. output buffer into your mail program, as it gives us important
  19330. information about your Org-mode version and configuration."
  19331. (interactive)
  19332. (require 'reporter)
  19333. (org-load-modules-maybe)
  19334. (org-require-autoloaded-modules)
  19335. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19336. (reporter-submit-bug-report
  19337. "emacs-orgmode@gnu.org"
  19338. (org-version nil 'full)
  19339. (let (list)
  19340. (save-window-excursion
  19341. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19342. (delete-other-windows)
  19343. (erase-buffer)
  19344. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19345. We would like to add your full Org-mode and Outline configuration to the
  19346. bug report. This greatly simplifies the work of the maintainer and
  19347. other experts on the mailing list.
  19348. HOWEVER, some variables you have customized may contain private
  19349. information. The names of customers, colleagues, or friends, might
  19350. appear in the form of file names, tags, todo states, or search strings.
  19351. If you answer yes to the prompt, you might want to check and remove
  19352. such private information before sending the email.")
  19353. (add-text-properties (point-min) (point-max) '(face org-warning))
  19354. (when (yes-or-no-p "Include your Org-mode configuration ")
  19355. (mapatoms
  19356. (lambda (v)
  19357. (and (boundp v)
  19358. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19359. (or (and (symbol-value v)
  19360. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19361. (and
  19362. (get v 'custom-type) (get v 'standard-value)
  19363. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19364. (push v list)))))
  19365. (kill-buffer (get-buffer "*Warn about privacy*"))
  19366. list))
  19367. nil nil
  19368. "Remember to cover the basics, that is, what you expected to happen and
  19369. what in fact did happen. You don't know how to make a good report? See
  19370. http://orgmode.org/manual/Feedback.html#Feedback
  19371. Your bug report will be posted to the Org-mode mailing list.
  19372. ------------------------------------------------------------------------")
  19373. (save-excursion
  19374. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19375. (replace-match "\\1Bug: \\3 [\\2]")))))
  19376. (defun org-install-agenda-files-menu ()
  19377. (let ((bl (buffer-list)))
  19378. (save-excursion
  19379. (while bl
  19380. (set-buffer (pop bl))
  19381. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19382. (when (derived-mode-p 'org-mode)
  19383. (easy-menu-change
  19384. '("Org") "File List for Agenda"
  19385. (append
  19386. (list
  19387. ["Edit File List" (org-edit-agenda-file-list) t]
  19388. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19389. ["Remove Current File from List" org-remove-file t]
  19390. ["Cycle through agenda files" org-cycle-agenda-files t]
  19391. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19392. "--")
  19393. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19394. ;;;; Documentation
  19395. (defun org-require-autoloaded-modules ()
  19396. (interactive)
  19397. (mapc 'require
  19398. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19399. org-remember org-table org-timer)))
  19400. ;;;###autoload
  19401. (defun org-reload (&optional uncompiled)
  19402. "Reload all org lisp files.
  19403. With prefix arg UNCOMPILED, load the uncompiled versions."
  19404. (interactive "P")
  19405. (require 'loadhist)
  19406. (let* ((org-dir (org-find-library-dir "org"))
  19407. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19408. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19409. (remove-re (mapconcat 'identity
  19410. (mapcar (lambda (f) (concat "^" f "$"))
  19411. (list (if (featurep 'xemacs)
  19412. "org-colview"
  19413. "org-colview-xemacs")
  19414. "org" "org-loaddefs" "org-version"))
  19415. "\\|"))
  19416. (feats (delete-dups
  19417. (mapcar 'file-name-sans-extension
  19418. (mapcar 'file-name-nondirectory
  19419. (delq nil
  19420. (mapcar 'feature-file
  19421. features))))))
  19422. (lfeat (append
  19423. (sort
  19424. (setq feats
  19425. (delq nil (mapcar
  19426. (lambda (f)
  19427. (if (and (string-match feature-re f)
  19428. (not (string-match remove-re f)))
  19429. f nil))
  19430. feats)))
  19431. 'string-lessp)
  19432. (list "org-version" "org")))
  19433. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19434. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19435. load-uncore load-misses)
  19436. (setq load-misses
  19437. (delq 't
  19438. (mapcar (lambda (f)
  19439. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19440. (and (string= org-dir contrib-dir)
  19441. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19442. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19443. (add-to-list 'load-uncore f 'append)
  19444. 't)
  19445. f))
  19446. lfeat)))
  19447. (if load-uncore
  19448. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19449. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19450. (if load-misses
  19451. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19452. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19453. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19454. ;;;###autoload
  19455. (defun org-customize ()
  19456. "Call the customize function with org as argument."
  19457. (interactive)
  19458. (org-load-modules-maybe)
  19459. (org-require-autoloaded-modules)
  19460. (customize-browse 'org))
  19461. (defun org-create-customize-menu ()
  19462. "Create a full customization menu for Org-mode, insert it into the menu."
  19463. (interactive)
  19464. (org-load-modules-maybe)
  19465. (org-require-autoloaded-modules)
  19466. (if (fboundp 'customize-menu-create)
  19467. (progn
  19468. (easy-menu-change
  19469. '("Org") "Customize"
  19470. `(["Browse Org group" org-customize t]
  19471. "--"
  19472. ,(customize-menu-create 'org)
  19473. ["Set" Custom-set t]
  19474. ["Save" Custom-save t]
  19475. ["Reset to Current" Custom-reset-current t]
  19476. ["Reset to Saved" Custom-reset-saved t]
  19477. ["Reset to Standard Settings" Custom-reset-standard t]))
  19478. (message "\"Org\"-menu now contains full customization menu"))
  19479. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19480. ;;;; Miscellaneous stuff
  19481. ;;; Generally useful functions
  19482. (defun org-get-at-bol (property)
  19483. "Get text property PROPERTY at beginning of line."
  19484. (get-text-property (point-at-bol) property))
  19485. (defun org-find-text-property-in-string (prop s)
  19486. "Return the first non-nil value of property PROP in string S."
  19487. (or (get-text-property 0 prop s)
  19488. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19489. prop s)))
  19490. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19491. "Display the given MESSAGE as a warning."
  19492. (if (fboundp 'display-warning)
  19493. (display-warning 'org message
  19494. (if (featurep 'xemacs) 'warning :warning))
  19495. (let ((buf (get-buffer-create "*Org warnings*")))
  19496. (with-current-buffer buf
  19497. (goto-char (point-max))
  19498. (insert "Warning (Org): " message)
  19499. (unless (bolp)
  19500. (newline)))
  19501. (display-buffer buf)
  19502. (sit-for 0))))
  19503. (defun org-eval (form)
  19504. "Eval FORM and return result."
  19505. (condition-case error
  19506. (eval form)
  19507. (error (format "%%![Error: %s]" error))))
  19508. (defun org-in-clocktable-p ()
  19509. "Check if the cursor is in a clocktable."
  19510. (let ((pos (point)) start)
  19511. (save-excursion
  19512. (end-of-line 1)
  19513. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19514. (setq start (match-beginning 0))
  19515. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19516. (>= (match-end 0) pos)
  19517. start))))
  19518. (defun org-in-commented-line ()
  19519. "Is point in a line starting with `#'?"
  19520. (equal (char-after (point-at-bol)) ?#))
  19521. (defun org-in-indented-comment-line ()
  19522. "Is point in a line starting with `#' after some white space?"
  19523. (save-excursion
  19524. (save-match-data
  19525. (goto-char (point-at-bol))
  19526. (looking-at "[ \t]*#"))))
  19527. (defun org-in-verbatim-emphasis ()
  19528. (save-match-data
  19529. (and (org-in-regexp org-emph-re 2)
  19530. (>= (point) (match-beginning 3))
  19531. (<= (point) (match-end 4))
  19532. (member (match-string 3) '("=" "~")))))
  19533. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19534. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19535. (if (and marker (marker-buffer marker)
  19536. (buffer-live-p (marker-buffer marker)))
  19537. (progn
  19538. (org-pop-to-buffer-same-window (marker-buffer marker))
  19539. (if (or (> marker (point-max)) (< marker (point-min)))
  19540. (widen))
  19541. (goto-char marker)
  19542. (org-show-context 'org-goto))
  19543. (if bookmark
  19544. (bookmark-jump bookmark)
  19545. (error "Cannot find location"))))
  19546. (defun org-quote-csv-field (s)
  19547. "Quote field for inclusion in CSV material."
  19548. (if (string-match "[\",]" s)
  19549. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19550. s))
  19551. (defun org-force-self-insert (N)
  19552. "Needed to enforce self-insert under remapping."
  19553. (interactive "p")
  19554. (self-insert-command N))
  19555. (defun org-string-width (s)
  19556. "Compute width of string, ignoring invisible characters.
  19557. This ignores character with invisibility property `org-link', and also
  19558. characters with property `org-cwidth', because these will become invisible
  19559. upon the next fontification round."
  19560. (let (b l)
  19561. (when (or (eq t buffer-invisibility-spec)
  19562. (assq 'org-link buffer-invisibility-spec))
  19563. (while (setq b (text-property-any 0 (length s)
  19564. 'invisible 'org-link s))
  19565. (setq s (concat (substring s 0 b)
  19566. (substring s (or (next-single-property-change
  19567. b 'invisible s) (length s)))))))
  19568. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19569. (setq s (concat (substring s 0 b)
  19570. (substring s (or (next-single-property-change
  19571. b 'org-cwidth s) (length s))))))
  19572. (setq l (string-width s) b -1)
  19573. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19574. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19575. l))
  19576. (defun org-shorten-string (s maxlength)
  19577. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19578. If the string is shorter or has length MAXLENGTH, just return the
  19579. original string. If it is longer, the functions finds a space in the
  19580. string, breaks this string off at that locations and adds three dots
  19581. as ellipsis. Including the ellipsis, the string will not be longer
  19582. than MAXLENGTH. If finding a good breaking point in the string does
  19583. not work, the string is just chopped off in the middle of a word
  19584. if necessary."
  19585. (if (<= (length s) maxlength)
  19586. s
  19587. (let* ((n (max (- maxlength 4) 1))
  19588. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19589. (if (string-match re s)
  19590. (concat (match-string 1 s) "...")
  19591. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19592. (defun org-get-indentation (&optional line)
  19593. "Get the indentation of the current line, interpreting tabs.
  19594. When LINE is given, assume it represents a line and compute its indentation."
  19595. (if line
  19596. (if (string-match "^ *" (org-remove-tabs line))
  19597. (match-end 0))
  19598. (save-excursion
  19599. (beginning-of-line 1)
  19600. (skip-chars-forward " \t")
  19601. (current-column))))
  19602. (defun org-get-string-indentation (s)
  19603. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19604. (let ((n -1) (i 0) (w tab-width) c)
  19605. (catch 'exit
  19606. (while (< (setq n (1+ n)) (length s))
  19607. (setq c (aref s n))
  19608. (cond ((= c ?\ ) (setq i (1+ i)))
  19609. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19610. (t (throw 'exit t)))))
  19611. i))
  19612. (defun org-remove-tabs (s &optional width)
  19613. "Replace tabulators in S with spaces.
  19614. Assumes that s is a single line, starting in column 0."
  19615. (setq width (or width tab-width))
  19616. (while (string-match "\t" s)
  19617. (setq s (replace-match
  19618. (make-string
  19619. (- (* width (/ (+ (match-beginning 0) width) width))
  19620. (match-beginning 0)) ?\ )
  19621. t t s)))
  19622. s)
  19623. (defun org-fix-indentation (line ind)
  19624. "Fix indentation in LINE.
  19625. IND is a cons cell with target and minimum indentation.
  19626. If the current indentation in LINE is smaller than the minimum,
  19627. leave it alone. If it is larger than ind, set it to the target."
  19628. (let* ((l (org-remove-tabs line))
  19629. (i (org-get-indentation l))
  19630. (i1 (car ind)) (i2 (cdr ind)))
  19631. (if (>= i i2) (setq l (substring line i2)))
  19632. (if (> i1 0)
  19633. (concat (make-string i1 ?\ ) l)
  19634. l)))
  19635. (defun org-remove-indentation (code &optional n)
  19636. "Remove the maximum common indentation from the lines in CODE.
  19637. N may optionally be the number of spaces to remove."
  19638. (with-temp-buffer
  19639. (insert code)
  19640. (org-do-remove-indentation n)
  19641. (buffer-string)))
  19642. (defun org-do-remove-indentation (&optional n)
  19643. "Remove the maximum common indentation from the buffer."
  19644. (untabify (point-min) (point-max))
  19645. (let ((min 10000) re)
  19646. (if n
  19647. (setq min n)
  19648. (goto-char (point-min))
  19649. (while (re-search-forward "^ *[^ \n]" nil t)
  19650. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19651. (unless (or (= min 0) (= min 10000))
  19652. (setq re (format "^ \\{%d\\}" min))
  19653. (goto-char (point-min))
  19654. (while (re-search-forward re nil t)
  19655. (replace-match "")
  19656. (end-of-line 1))
  19657. min)))
  19658. (defun org-fill-template (template alist)
  19659. "Find each %key of ALIST in TEMPLATE and replace it."
  19660. (let ((case-fold-search nil)
  19661. entry key value)
  19662. (setq alist (sort (copy-sequence alist)
  19663. (lambda (a b) (< (length (car a)) (length (car b))))))
  19664. (while (setq entry (pop alist))
  19665. (setq template
  19666. (replace-regexp-in-string
  19667. (concat "%" (regexp-quote (car entry)))
  19668. (or (cdr entry) "") template t t)))
  19669. template))
  19670. (defun org-base-buffer (buffer)
  19671. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19672. (if (not buffer)
  19673. buffer
  19674. (or (buffer-base-buffer buffer)
  19675. buffer)))
  19676. (defun org-trim (s)
  19677. "Remove whitespace at beginning and end of string."
  19678. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19679. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19680. s)
  19681. (defun org-wrap (string &optional width lines)
  19682. "Wrap string to either a number of lines, or a width in characters.
  19683. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19684. that costs. If there is a word longer than WIDTH, the text is actually
  19685. wrapped to the length of that word.
  19686. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19687. many lines, whatever width that takes.
  19688. The return value is a list of lines, without newlines at the end."
  19689. (let* ((words (org-split-string string "[ \t\n]+"))
  19690. (maxword (apply 'max (mapcar 'org-string-width words)))
  19691. w ll)
  19692. (cond (width
  19693. (org-do-wrap words (max maxword width)))
  19694. (lines
  19695. (setq w maxword)
  19696. (setq ll (org-do-wrap words maxword))
  19697. (if (<= (length ll) lines)
  19698. ll
  19699. (setq ll words)
  19700. (while (> (length ll) lines)
  19701. (setq w (1+ w))
  19702. (setq ll (org-do-wrap words w)))
  19703. ll))
  19704. (t (error "Cannot wrap this")))))
  19705. (defun org-do-wrap (words width)
  19706. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19707. (let (lines line)
  19708. (while words
  19709. (setq line (pop words))
  19710. (while (and words (< (+ (length line) (length (car words))) width))
  19711. (setq line (concat line " " (pop words))))
  19712. (setq lines (push line lines)))
  19713. (nreverse lines)))
  19714. (defun org-split-string (string &optional separators)
  19715. "Splits STRING into substrings at SEPARATORS.
  19716. No empty strings are returned if there are matches at the beginning
  19717. and end of string."
  19718. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19719. (start 0)
  19720. notfirst
  19721. (list nil))
  19722. (while (and (string-match rexp string
  19723. (if (and notfirst
  19724. (= start (match-beginning 0))
  19725. (< start (length string)))
  19726. (1+ start) start))
  19727. (< (match-beginning 0) (length string)))
  19728. (setq notfirst t)
  19729. (or (eq (match-beginning 0) 0)
  19730. (and (eq (match-beginning 0) (match-end 0))
  19731. (eq (match-beginning 0) start))
  19732. (setq list
  19733. (cons (substring string start (match-beginning 0))
  19734. list)))
  19735. (setq start (match-end 0)))
  19736. (or (eq start (length string))
  19737. (setq list
  19738. (cons (substring string start)
  19739. list)))
  19740. (nreverse list)))
  19741. (defun org-quote-vert (s)
  19742. "Replace \"|\" with \"\\vert\"."
  19743. (while (string-match "|" s)
  19744. (setq s (replace-match "\\vert" t t s)))
  19745. s)
  19746. (defun org-uuidgen-p (s)
  19747. "Is S an ID created by UUIDGEN?"
  19748. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19749. (defun org-in-src-block-p (&optional inside)
  19750. "Whether point is in a code source block.
  19751. When INSIDE is non-nil, don't consider we are within a src block
  19752. when point is at #+BEGIN_SRC or #+END_SRC."
  19753. (let ((case-fold-search t) ov)
  19754. (or (and (setq ov (overlays-at (point)))
  19755. (memq 'org-block-background
  19756. (overlay-properties (car ov))))
  19757. (and (not inside)
  19758. (save-match-data
  19759. (save-excursion
  19760. (beginning-of-line)
  19761. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19762. (defun org-context ()
  19763. "Return a list of contexts of the current cursor position.
  19764. If several contexts apply, all are returned.
  19765. Each context entry is a list with a symbol naming the context, and
  19766. two positions indicating start and end of the context. Possible
  19767. contexts are:
  19768. :headline anywhere in a headline
  19769. :headline-stars on the leading stars in a headline
  19770. :todo-keyword on a TODO keyword (including DONE) in a headline
  19771. :tags on the TAGS in a headline
  19772. :priority on the priority cookie in a headline
  19773. :item on the first line of a plain list item
  19774. :item-bullet on the bullet/number of a plain list item
  19775. :checkbox on the checkbox in a plain list item
  19776. :table in an org-mode table
  19777. :table-special on a special filed in a table
  19778. :table-table in a table.el table
  19779. :clocktable in a clocktable
  19780. :src-block in a source block
  19781. :link on a hyperlink
  19782. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19783. :target on a <<target>>
  19784. :radio-target on a <<<radio-target>>>
  19785. :latex-fragment on a LaTeX fragment
  19786. :latex-preview on a LaTeX fragment with overlaid preview image
  19787. This function expects the position to be visible because it uses font-lock
  19788. faces as a help to recognize the following contexts: :table-special, :link,
  19789. and :keyword."
  19790. (let* ((f (get-text-property (point) 'face))
  19791. (faces (if (listp f) f (list f)))
  19792. (case-fold-search t)
  19793. (p (point)) clist o)
  19794. ;; First the large context
  19795. (cond
  19796. ((org-at-heading-p t)
  19797. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19798. (when (progn
  19799. (beginning-of-line 1)
  19800. (looking-at org-todo-line-tags-regexp))
  19801. (push (org-point-in-group p 1 :headline-stars) clist)
  19802. (push (org-point-in-group p 2 :todo-keyword) clist)
  19803. (push (org-point-in-group p 4 :tags) clist))
  19804. (goto-char p)
  19805. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19806. (if (looking-at "\\[#[A-Z0-9]\\]")
  19807. (push (org-point-in-group p 0 :priority) clist)))
  19808. ((org-at-item-p)
  19809. (push (org-point-in-group p 2 :item-bullet) clist)
  19810. (push (list :item (point-at-bol)
  19811. (save-excursion (org-end-of-item) (point)))
  19812. clist)
  19813. (and (org-at-item-checkbox-p)
  19814. (push (org-point-in-group p 0 :checkbox) clist)))
  19815. ((org-at-table-p)
  19816. (push (list :table (org-table-begin) (org-table-end)) clist)
  19817. (if (memq 'org-formula faces)
  19818. (push (list :table-special
  19819. (previous-single-property-change p 'face)
  19820. (next-single-property-change p 'face)) clist)))
  19821. ((org-at-table-p 'any)
  19822. (push (list :table-table) clist)))
  19823. (goto-char p)
  19824. (let ((case-fold-search t))
  19825. ;; New the "medium" contexts: clocktables, source blocks
  19826. (cond ((org-in-clocktable-p)
  19827. (push (list :clocktable
  19828. (and (or (looking-at "#\\+BEGIN: clocktable")
  19829. (search-backward "#+BEGIN: clocktable" nil t))
  19830. (match-beginning 0))
  19831. (and (re-search-forward "#\\+END:?" nil t)
  19832. (match-end 0))) clist))
  19833. ((org-in-src-block-p)
  19834. (push (list :src-block
  19835. (and (or (looking-at "#\\+BEGIN_SRC")
  19836. (search-backward "#+BEGIN_SRC" nil t))
  19837. (match-beginning 0))
  19838. (and (search-forward "#+END_SRC" nil t)
  19839. (match-beginning 0))) clist))))
  19840. (goto-char p)
  19841. ;; Now the small context
  19842. (cond
  19843. ((org-at-timestamp-p)
  19844. (push (org-point-in-group p 0 :timestamp) clist))
  19845. ((memq 'org-link faces)
  19846. (push (list :link
  19847. (previous-single-property-change p 'face)
  19848. (next-single-property-change p 'face)) clist))
  19849. ((memq 'org-special-keyword faces)
  19850. (push (list :keyword
  19851. (previous-single-property-change p 'face)
  19852. (next-single-property-change p 'face)) clist))
  19853. ((org-at-target-p)
  19854. (push (org-point-in-group p 0 :target) clist)
  19855. (goto-char (1- (match-beginning 0)))
  19856. (if (looking-at org-radio-target-regexp)
  19857. (push (org-point-in-group p 0 :radio-target) clist))
  19858. (goto-char p))
  19859. ((setq o (car (delq nil
  19860. (mapcar
  19861. (lambda (x)
  19862. (if (memq x org-latex-fragment-image-overlays) x))
  19863. (overlays-at (point))))))
  19864. (push (list :latex-fragment
  19865. (overlay-start o) (overlay-end o)) clist)
  19866. (push (list :latex-preview
  19867. (overlay-start o) (overlay-end o)) clist))
  19868. ((org-inside-LaTeX-fragment-p)
  19869. ;; FIXME: positions wrong.
  19870. (push (list :latex-fragment (point) (point)) clist)))
  19871. (setq clist (nreverse (delq nil clist)))
  19872. clist))
  19873. ;; FIXME: Compare with at-regexp-p Do we need both?
  19874. (defun org-in-regexp (re &optional nlines visually)
  19875. "Check if point is inside a match of regexp.
  19876. Normally only the current line is checked, but you can include NLINES extra
  19877. lines both before and after point into the search.
  19878. If VISUALLY is set, require that the cursor is not after the match but
  19879. really on, so that the block visually is on the match."
  19880. (catch 'exit
  19881. (let ((pos (point))
  19882. (eol (point-at-eol (+ 1 (or nlines 0))))
  19883. (inc (if visually 1 0)))
  19884. (save-excursion
  19885. (beginning-of-line (- 1 (or nlines 0)))
  19886. (while (re-search-forward re eol t)
  19887. (if (and (<= (match-beginning 0) pos)
  19888. (>= (+ inc (match-end 0)) pos))
  19889. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19890. (defun org-at-regexp-p (regexp)
  19891. "Is point inside a match of REGEXP in the current line?"
  19892. (catch 'exit
  19893. (save-excursion
  19894. (let ((pos (point)) (end (point-at-eol)))
  19895. (beginning-of-line 1)
  19896. (while (re-search-forward regexp end t)
  19897. (if (and (<= (match-beginning 0) pos)
  19898. (>= (match-end 0) pos))
  19899. (throw 'exit t)))
  19900. nil))))
  19901. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19902. "Non-nil when point is between matches of START-RE and END-RE.
  19903. Also return a non-nil value when point is on one of the matches.
  19904. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19905. buffer positions. Default values are the positions of headlines
  19906. surrounding the point.
  19907. The functions returns a cons cell whose car (resp. cdr) is the
  19908. position before START-RE (resp. after END-RE)."
  19909. (save-match-data
  19910. (let ((pos (point))
  19911. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19912. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19913. beg end)
  19914. (save-excursion
  19915. ;; Point is on a block when on START-RE or if START-RE can be
  19916. ;; found before it...
  19917. (and (or (org-at-regexp-p start-re)
  19918. (re-search-backward start-re limit-up t))
  19919. (setq beg (match-beginning 0))
  19920. ;; ... and END-RE after it...
  19921. (goto-char (match-end 0))
  19922. (re-search-forward end-re limit-down t)
  19923. (> (setq end (match-end 0)) pos)
  19924. ;; ... without another START-RE in-between.
  19925. (goto-char (match-beginning 0))
  19926. (not (re-search-backward start-re (1+ beg) t))
  19927. ;; Return value.
  19928. (cons beg end))))))
  19929. (defun org-in-block-p (names)
  19930. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19931. NAMES is a list of strings containing names of blocks.
  19932. Return first block name matched, or nil. Beware that in case of
  19933. nested blocks, the returned name may not belong to the closest
  19934. block from point."
  19935. (save-match-data
  19936. (catch 'exit
  19937. (let ((case-fold-search t)
  19938. (lim-up (save-excursion (outline-previous-heading)))
  19939. (lim-down (save-excursion (outline-next-heading))))
  19940. (mapc (lambda (name)
  19941. (let ((n (regexp-quote name)))
  19942. (when (org-between-regexps-p
  19943. (concat "^[ \t]*#\\+begin_" n)
  19944. (concat "^[ \t]*#\\+end_" n)
  19945. lim-up lim-down)
  19946. (throw 'exit n))))
  19947. names))
  19948. nil)))
  19949. (defun org-in-drawer-p ()
  19950. "Non-nil if point is within a drawer.
  19951. If point is within a drawer, return it, as parsed data."
  19952. (let ((element (save-match-data (org-element-at-point))))
  19953. (while (and element (not (memq (org-element-type element)
  19954. '(drawer property-drawer))))
  19955. (setq element (org-element-property :parent element)))
  19956. element))
  19957. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19958. "Call `multi-occur' with buffers for all agenda files."
  19959. (interactive "sOrg-files matching: \np")
  19960. (let* ((files (org-agenda-files))
  19961. (tnames (mapcar 'file-truename files))
  19962. (extra org-agenda-text-search-extra-files)
  19963. f)
  19964. (when (eq (car extra) 'agenda-archives)
  19965. (setq extra (cdr extra))
  19966. (setq files (org-add-archive-files files)))
  19967. (while (setq f (pop extra))
  19968. (unless (member (file-truename f) tnames)
  19969. (add-to-list 'files f 'append)
  19970. (add-to-list 'tnames (file-truename f) 'append)))
  19971. (multi-occur
  19972. (mapcar (lambda (x)
  19973. (with-current-buffer
  19974. (or (get-file-buffer x) (find-file-noselect x))
  19975. (widen)
  19976. (current-buffer)))
  19977. files)
  19978. regexp)))
  19979. (if (boundp 'occur-mode-find-occurrence-hook)
  19980. ;; Emacs 23
  19981. (add-hook 'occur-mode-find-occurrence-hook
  19982. (lambda ()
  19983. (when (derived-mode-p 'org-mode)
  19984. (org-reveal))))
  19985. ;; Emacs 22
  19986. (defadvice occur-mode-goto-occurrence
  19987. (after org-occur-reveal activate)
  19988. (and (derived-mode-p 'org-mode) (org-reveal)))
  19989. (defadvice occur-mode-goto-occurrence-other-window
  19990. (after org-occur-reveal activate)
  19991. (and (derived-mode-p 'org-mode) (org-reveal)))
  19992. (defadvice occur-mode-display-occurrence
  19993. (after org-occur-reveal activate)
  19994. (when (derived-mode-p 'org-mode)
  19995. (let ((pos (occur-mode-find-occurrence)))
  19996. (with-current-buffer (marker-buffer pos)
  19997. (save-excursion
  19998. (goto-char pos)
  19999. (org-reveal)))))))
  20000. (defun org-occur-link-in-agenda-files ()
  20001. "Create a link and search for it in the agendas.
  20002. The link is not stored in `org-stored-links', it is just created
  20003. for the search purpose."
  20004. (interactive)
  20005. (let ((link (condition-case nil
  20006. (org-store-link nil)
  20007. (error "Unable to create a link to here"))))
  20008. (org-occur-in-agenda-files (regexp-quote link))))
  20009. (defun org-reverse-string (string)
  20010. "Return the reverse of STRING."
  20011. (apply 'string (reverse (string-to-list string))))
  20012. (defsubst org-uniquify (list)
  20013. "Non-destructively remove duplicate elements from LIST."
  20014. (let ((res (copy-sequence list))) (delete-dups res)))
  20015. (defun org-uniquify-alist (alist)
  20016. "Merge elements of ALIST with the same key.
  20017. For example, in this alist:
  20018. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20019. => '((a 1 3) (b 2))
  20020. merge (a 1) and (a 3) into (a 1 3).
  20021. The function returns the new ALIST."
  20022. (let (rtn)
  20023. (mapc
  20024. (lambda (e)
  20025. (let (n)
  20026. (if (not (assoc (car e) rtn))
  20027. (push e rtn)
  20028. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20029. (setq rtn (assq-delete-all (car e) rtn))
  20030. (push n rtn))))
  20031. alist)
  20032. rtn))
  20033. (defun org-delete-all (elts list)
  20034. "Remove all elements in ELTS from LIST."
  20035. (while elts
  20036. (setq list (delete (pop elts) list)))
  20037. list)
  20038. (defun org-count (cl-item cl-seq)
  20039. "Count the number of occurrences of ITEM in SEQ.
  20040. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20041. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20042. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20043. (while (< cl-start cl-end)
  20044. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20045. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20046. (setq cl-start (1+ cl-start)))
  20047. cl-count))
  20048. (defun org-remove-if (predicate seq)
  20049. "Remove everything from SEQ that fulfills PREDICATE."
  20050. (let (res e)
  20051. (while seq
  20052. (setq e (pop seq))
  20053. (if (not (funcall predicate e)) (push e res)))
  20054. (nreverse res)))
  20055. (defun org-remove-if-not (predicate seq)
  20056. "Remove everything from SEQ that does not fulfill PREDICATE."
  20057. (let (res e)
  20058. (while seq
  20059. (setq e (pop seq))
  20060. (if (funcall predicate e) (push e res)))
  20061. (nreverse res)))
  20062. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20063. "Reduce two-argument FUNCTION across SEQ.
  20064. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20065. \":initial-value\" removed."
  20066. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20067. (cadr (memq :initial-value cl-keys)))
  20068. (cl-seq (pop cl-seq))
  20069. (t (funcall cl-func)))))
  20070. (while cl-seq
  20071. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20072. cl-accum))
  20073. (defun org-every (pred seq)
  20074. "Return true if PREDICATE is true of every element of SEQ.
  20075. Adapted from `every' in cl.el."
  20076. (catch 'org-every
  20077. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20078. t))
  20079. (defun org-some (pred seq)
  20080. "Return true if PREDICATE is true of any element of SEQ.
  20081. Adapted from `some' in cl.el."
  20082. (catch 'org-some
  20083. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20084. nil))
  20085. (defun org-back-over-empty-lines ()
  20086. "Move backwards over whitespace, to the beginning of the first empty line.
  20087. Returns the number of empty lines passed."
  20088. (let ((pos (point)))
  20089. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20090. (skip-chars-backward " \t\n\r")
  20091. (unless (eobp)
  20092. (forward-line -1)))
  20093. (beginning-of-line 2)
  20094. (goto-char (min (point) pos))
  20095. (count-lines (point) pos)))
  20096. (defun org-skip-whitespace ()
  20097. (skip-chars-forward " \t\n\r"))
  20098. (defun org-point-in-group (point group &optional context)
  20099. "Check if POINT is in match-group GROUP.
  20100. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20101. match. If the match group does not exist or point is not inside it,
  20102. return nil."
  20103. (and (match-beginning group)
  20104. (>= point (match-beginning group))
  20105. (<= point (match-end group))
  20106. (if context
  20107. (list context (match-beginning group) (match-end group))
  20108. t)))
  20109. (defun org-switch-to-buffer-other-window (&rest args)
  20110. "Switch to buffer in a second window on the current frame.
  20111. In particular, do not allow pop-up frames.
  20112. Returns the newly created buffer."
  20113. (org-no-popups
  20114. (apply 'switch-to-buffer-other-window args)))
  20115. (defun org-combine-plists (&rest plists)
  20116. "Create a single property list from all plists in PLISTS.
  20117. The process starts by copying the first list, and then setting properties
  20118. from the other lists. Settings in the last list are the most significant
  20119. ones and overrule settings in the other lists."
  20120. (let ((rtn (copy-sequence (pop plists)))
  20121. p v ls)
  20122. (while plists
  20123. (setq ls (pop plists))
  20124. (while ls
  20125. (setq p (pop ls) v (pop ls))
  20126. (setq rtn (plist-put rtn p v))))
  20127. rtn))
  20128. (defun org-replace-escapes (string table)
  20129. "Replace %-escapes in STRING with values in TABLE.
  20130. TABLE is an association list with keys like \"%a\" and string values.
  20131. The sequences in STRING may contain normal field width and padding information,
  20132. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20133. so values can contain further %-escapes if they are define later in TABLE."
  20134. (let ((tbl (copy-alist table))
  20135. (case-fold-search nil)
  20136. (pchg 0)
  20137. e re rpl)
  20138. (while (setq e (pop tbl))
  20139. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20140. (when (and (cdr e) (string-match re (cdr e)))
  20141. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20142. (safe "SREF"))
  20143. (add-text-properties 0 3 (list 'sref sref) safe)
  20144. (setcdr e (replace-match safe t t (cdr e)))))
  20145. (while (string-match re string)
  20146. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20147. (cdr e)))
  20148. (setq string (replace-match rpl t t string))))
  20149. (while (setq pchg (next-property-change pchg string))
  20150. (let ((sref (get-text-property pchg 'sref string)))
  20151. (when (and sref (string-match "SREF" string pchg))
  20152. (setq string (replace-match sref t t string)))))
  20153. string))
  20154. (defun org-sublist (list start end)
  20155. "Return a section of LIST, from START to END.
  20156. Counting starts at 1."
  20157. (let (rtn (c start))
  20158. (setq list (nthcdr (1- start) list))
  20159. (while (and list (<= c end))
  20160. (push (pop list) rtn)
  20161. (setq c (1+ c)))
  20162. (nreverse rtn)))
  20163. (defun org-find-base-buffer-visiting (file)
  20164. "Like `find-buffer-visiting' but always return the base buffer and
  20165. not an indirect buffer."
  20166. (let ((buf (or (get-file-buffer file)
  20167. (find-buffer-visiting file))))
  20168. (if buf
  20169. (or (buffer-base-buffer buf) buf)
  20170. nil)))
  20171. (defun org-image-file-name-regexp (&optional extensions)
  20172. "Return regexp matching the file names of images.
  20173. If EXTENSIONS is given, only match these."
  20174. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20175. (image-file-name-regexp)
  20176. (let ((image-file-name-extensions
  20177. (or extensions
  20178. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20179. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20180. (concat "\\."
  20181. (regexp-opt (nconc (mapcar 'upcase
  20182. image-file-name-extensions)
  20183. image-file-name-extensions)
  20184. t)
  20185. "\\'"))))
  20186. (defun org-file-image-p (file &optional extensions)
  20187. "Return non-nil if FILE is an image."
  20188. (save-match-data
  20189. (string-match (org-image-file-name-regexp extensions) file)))
  20190. (defun org-get-cursor-date (&optional with-time)
  20191. "Return the date at cursor in as a time.
  20192. This works in the calendar and in the agenda, anywhere else it just
  20193. returns the current time.
  20194. If WITH-TIME is non-nil, returns the time of the event at point (in
  20195. the agenda) or the current time of the day."
  20196. (let (date day defd tp tm hod mod)
  20197. (when with-time
  20198. (setq tp (get-text-property (point) 'time))
  20199. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20200. (setq hod (string-to-number (match-string 1 tp))
  20201. mod (string-to-number (match-string 2 tp))))
  20202. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20203. mod (nth 1 (decode-time (current-time))))))
  20204. (cond
  20205. ((eq major-mode 'calendar-mode)
  20206. (setq date (calendar-cursor-to-date)
  20207. defd (encode-time 0 (or mod 0) (or hod 0)
  20208. (nth 1 date) (nth 0 date) (nth 2 date))))
  20209. ((eq major-mode 'org-agenda-mode)
  20210. (setq day (get-text-property (point) 'day))
  20211. (if day
  20212. (setq date (calendar-gregorian-from-absolute day)
  20213. defd (encode-time 0 (or mod 0) (or hod 0)
  20214. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20215. (or defd (current-time))))
  20216. (defun org-mark-subtree (&optional up)
  20217. "Mark the current subtree.
  20218. This puts point at the start of the current subtree, and mark at
  20219. the end. If a numeric prefix UP is given, move up into the
  20220. hierarchy of headlines by UP levels before marking the subtree."
  20221. (interactive "P")
  20222. (org-with-limited-levels
  20223. (cond ((org-at-heading-p) (beginning-of-line))
  20224. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20225. (t (outline-previous-visible-heading 1))))
  20226. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20227. (if (org-called-interactively-p 'any)
  20228. (call-interactively 'org-mark-element)
  20229. (org-mark-element)))
  20230. ;;; Indentation
  20231. (defun org-indent-line ()
  20232. "Indent line depending on context."
  20233. (interactive)
  20234. (let* ((pos (point))
  20235. (itemp (org-at-item-p))
  20236. (case-fold-search t)
  20237. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20238. (inline-task-p (and (featurep 'org-inlinetask)
  20239. (org-inlinetask-in-task-p)))
  20240. (inline-re (and inline-task-p
  20241. (org-inlinetask-outline-regexp)))
  20242. column)
  20243. (if (and orgstruct-is-++ (eq pos (point)))
  20244. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20245. (indent-according-to-mode))
  20246. (beginning-of-line 1)
  20247. (cond
  20248. ;; Headings
  20249. ((looking-at org-outline-regexp) (setq column 0))
  20250. ;; Footnote definition
  20251. ((looking-at org-footnote-definition-re) (setq column 0))
  20252. ;; Literal examples
  20253. ((looking-at "[ \t]*:\\( \\|$\\)")
  20254. (setq column (org-get-indentation))) ; do nothing
  20255. ;; Lists
  20256. ((ignore-errors (goto-char (org-in-item-p)))
  20257. (setq column (if itemp
  20258. (org-get-indentation)
  20259. (org-list-item-body-column (point))))
  20260. (goto-char pos))
  20261. ;; Drawers
  20262. ((and (looking-at "[ \t]*:END:")
  20263. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20264. (save-excursion
  20265. (goto-char (1- (match-beginning 1)))
  20266. (setq column (current-column))))
  20267. ;; Special blocks
  20268. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20269. (save-excursion
  20270. (re-search-backward
  20271. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20272. (setq column (org-get-indentation (match-string 0))))
  20273. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20274. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20275. (save-excursion
  20276. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20277. (setq column
  20278. (cond ((equal (downcase (match-string 1)) "src")
  20279. ;; src blocks: let `org-edit-src-exit' handle them
  20280. (org-get-indentation))
  20281. ((equal (downcase (match-string 1)) "example")
  20282. (max (org-get-indentation)
  20283. (org-get-indentation (match-string 0))))
  20284. (t
  20285. (org-get-indentation (match-string 0))))))
  20286. ;; This line has nothing special, look at the previous relevant
  20287. ;; line to compute indentation
  20288. (t
  20289. (beginning-of-line 0)
  20290. (while (and (not (bobp))
  20291. (not (looking-at org-table-line-regexp))
  20292. (not (looking-at org-drawer-regexp))
  20293. ;; When point started in an inline task, do not move
  20294. ;; above task starting line.
  20295. (not (and inline-task-p (looking-at inline-re)))
  20296. ;; Skip drawers, blocks, empty lines, verbatim,
  20297. ;; comments, tables, footnotes definitions, lists,
  20298. ;; inline tasks.
  20299. (or (and (looking-at "[ \t]*:END:")
  20300. (re-search-backward org-drawer-regexp nil t))
  20301. (and (looking-at "[ \t]*#\\+end_")
  20302. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20303. (looking-at "[ \t]*[\n:#|]")
  20304. (looking-at org-footnote-definition-re)
  20305. (and (not inline-task-p)
  20306. (featurep 'org-inlinetask)
  20307. (org-inlinetask-in-task-p)
  20308. (or (org-inlinetask-goto-beginning) t))))
  20309. (beginning-of-line 0))
  20310. (cond
  20311. ;; There was a list item above.
  20312. ((ignore-errors (goto-char (org-in-item-p)))
  20313. (goto-char (org-list-get-top-point (org-list-struct)))
  20314. (setq column (org-get-indentation)))
  20315. ;; There was an heading above.
  20316. ((looking-at "\\*+[ \t]+")
  20317. (if (not org-adapt-indentation)
  20318. (setq column 0)
  20319. (goto-char (match-end 0))
  20320. (setq column (current-column))))
  20321. ;; A drawer had started and is unfinished
  20322. ((looking-at org-drawer-regexp)
  20323. (goto-char (1- (match-beginning 1)))
  20324. (setq column (current-column)))
  20325. ;; Else, nothing noticeable found: get indentation and go on.
  20326. (t (setq column (org-get-indentation))))))
  20327. ;; Now apply indentation and move cursor accordingly
  20328. (goto-char pos)
  20329. (if (<= (current-column) (current-indentation))
  20330. (org-indent-line-to column)
  20331. (save-excursion (org-indent-line-to column)))
  20332. ;; Special polishing for properties, see `org-property-format'
  20333. (setq column (current-column))
  20334. (beginning-of-line 1)
  20335. (if (looking-at org-property-re)
  20336. (replace-match (concat (match-string 4)
  20337. (format org-property-format
  20338. (match-string 1) (match-string 3)))
  20339. t t))
  20340. (org-move-to-column column))))
  20341. (defun org-indent-drawer ()
  20342. "Indent the drawer at point."
  20343. (interactive)
  20344. (let ((p (point))
  20345. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20346. (match-end 0)))
  20347. (folded
  20348. (save-excursion
  20349. (end-of-line)
  20350. (when (overlays-at (point))
  20351. (member 'invisible (overlay-properties
  20352. (car (overlays-at (point)))))))))
  20353. (when folded (org-cycle))
  20354. (indent-for-tab-command)
  20355. (while (and (move-beginning-of-line 2) (< (point) e))
  20356. (indent-for-tab-command))
  20357. (goto-char p)
  20358. (when folded (org-cycle)))
  20359. (message "Drawer at point indented"))
  20360. (defun org-indent-block ()
  20361. "Indent the block at point."
  20362. (interactive)
  20363. (let ((p (point))
  20364. (case-fold-search t)
  20365. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20366. (match-end 0)))
  20367. (folded
  20368. (save-excursion
  20369. (end-of-line)
  20370. (when (overlays-at (point))
  20371. (member 'invisible (overlay-properties
  20372. (car (overlays-at (point)))))))))
  20373. (when folded (org-cycle))
  20374. (indent-for-tab-command)
  20375. (while (and (move-beginning-of-line 2) (< (point) e))
  20376. (indent-for-tab-command))
  20377. (goto-char p)
  20378. (when folded (org-cycle)))
  20379. (message "Block at point indented"))
  20380. (defun org-indent-region (start end)
  20381. "Indent region."
  20382. (interactive "r")
  20383. (save-excursion
  20384. (let ((line-end (org-current-line end)))
  20385. (goto-char start)
  20386. (while (< (org-current-line) line-end)
  20387. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20388. (t (call-interactively 'org-indent-line)))
  20389. (move-beginning-of-line 2)))))
  20390. ;;; Filling
  20391. ;; We use our own fill-paragraph and auto-fill functions.
  20392. ;; `org-fill-paragraph' relies on adaptive filling and context
  20393. ;; checking. Appropriate `fill-prefix' is computed with
  20394. ;; `org-adaptive-fill-function'.
  20395. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20396. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20397. ;; `org-adaptive-fill-function' value. Internally,
  20398. ;; `org-comment-line-break-function' breaks the line.
  20399. ;; `org-setup-filling' installs filling and auto-filling related
  20400. ;; variables during `org-mode' initialization.
  20401. (defvar org-element-paragraph-separate) ; org-element.el
  20402. (defun org-setup-filling ()
  20403. (require 'org-element)
  20404. ;; Prevent auto-fill from inserting unwanted new items.
  20405. (when (boundp 'fill-nobreak-predicate)
  20406. (org-set-local
  20407. 'fill-nobreak-predicate
  20408. (org-uniquify
  20409. (append fill-nobreak-predicate
  20410. '(org-fill-line-break-nobreak-p
  20411. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20412. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20413. (org-set-local 'paragraph-start paragraph-ending)
  20414. (org-set-local 'paragraph-separate paragraph-ending))
  20415. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20416. (org-set-local 'auto-fill-inhibit-regexp nil)
  20417. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20418. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20419. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20420. (defun org-fill-line-break-nobreak-p ()
  20421. "Non-nil when a new line at point would create an Org line break."
  20422. (save-excursion
  20423. (skip-chars-backward "[ \t]")
  20424. (skip-chars-backward "\\\\")
  20425. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20426. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20427. "Non-nil when a new line at point would split a timestamp."
  20428. (and (org-at-timestamp-p t)
  20429. (not (looking-at org-ts-regexp-both))))
  20430. (declare-function message-in-body-p "message" ())
  20431. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20432. (defun org-adaptive-fill-function ()
  20433. "Compute a fill prefix for the current line.
  20434. Return fill prefix, as a string, or nil if current line isn't
  20435. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20436. matches in paragraphs or comments, use it."
  20437. (catch 'exit
  20438. (when (derived-mode-p 'message-mode)
  20439. (save-excursion
  20440. (beginning-of-line)
  20441. (cond ((or (not (message-in-body-p))
  20442. (looking-at orgtbl-line-start-regexp))
  20443. (throw 'exit nil))
  20444. ((looking-at message-cite-prefix-regexp)
  20445. (throw 'exit (match-string-no-properties 0)))
  20446. ((looking-at org-outline-regexp)
  20447. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20448. (org-with-wide-buffer
  20449. (let* ((p (line-beginning-position))
  20450. (element (save-excursion
  20451. (beginning-of-line)
  20452. (or (ignore-errors (org-element-at-point))
  20453. (user-error "An element cannot be parsed line %d"
  20454. (line-number-at-pos (point))))))
  20455. (type (org-element-type element))
  20456. (post-affiliated (org-element-property :post-affiliated element)))
  20457. (unless (and post-affiliated (< p post-affiliated))
  20458. (case type
  20459. (comment
  20460. (save-excursion
  20461. (beginning-of-line)
  20462. (looking-at "[ \t]*")
  20463. (concat (match-string 0) "# ")))
  20464. (footnote-definition "")
  20465. ((item plain-list)
  20466. (make-string (org-list-item-body-column
  20467. (or post-affiliated
  20468. (org-element-property :begin element)))
  20469. ? ))
  20470. (paragraph
  20471. ;; Fill prefix is usually the same as the current line,
  20472. ;; unless the paragraph is at the beginning of an item.
  20473. (let ((parent (org-element-property :parent element)))
  20474. (save-excursion
  20475. (beginning-of-line)
  20476. (cond ((eq (org-element-type parent) 'item)
  20477. (make-string (org-list-item-body-column
  20478. (org-element-property :begin parent))
  20479. ? ))
  20480. ((and adaptive-fill-regexp
  20481. ;; Locally disable
  20482. ;; `adaptive-fill-function' to let
  20483. ;; `fill-context-prefix' handle
  20484. ;; `adaptive-fill-regexp' variable.
  20485. (let (adaptive-fill-function)
  20486. (fill-context-prefix
  20487. post-affiliated
  20488. (org-element-property :end element)))))
  20489. ((looking-at "[ \t]+") (match-string 0))
  20490. (t "")))))
  20491. (comment-block
  20492. ;; Only fill contents if P is within block boundaries.
  20493. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20494. (forward-line)
  20495. (point)))
  20496. (cend (save-excursion
  20497. (goto-char (org-element-property :end element))
  20498. (skip-chars-backward " \r\t\n")
  20499. (line-beginning-position))))
  20500. (when (and (>= p cbeg) (< p cend))
  20501. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20502. (match-string 0)
  20503. ""))))))))))
  20504. (declare-function message-goto-body "message" ())
  20505. (defvar message-cite-prefix-regexp) ; From message.el
  20506. (defun org-fill-paragraph (&optional justify)
  20507. "Fill element at point, when applicable.
  20508. This function only applies to comment blocks, comments, example
  20509. blocks and paragraphs. Also, as a special case, re-align table
  20510. when point is at one.
  20511. If JUSTIFY is non-nil (interactively, with prefix argument),
  20512. justify as well. If `sentence-end-double-space' is non-nil, then
  20513. period followed by one space does not end a sentence, so don't
  20514. break a line there. The variable `fill-column' controls the
  20515. width for filling.
  20516. For convenience, when point is at a plain list, an item or
  20517. a footnote definition, try to fill the first paragraph within."
  20518. (interactive)
  20519. (if (and (derived-mode-p 'message-mode)
  20520. (or (not (message-in-body-p))
  20521. (save-excursion (move-beginning-of-line 1)
  20522. (looking-at message-cite-prefix-regexp))))
  20523. ;; First ensure filling is correct in message-mode.
  20524. (let ((fill-paragraph-function
  20525. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20526. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20527. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20528. (paragraph-separate
  20529. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20530. (fill-paragraph nil))
  20531. (with-syntax-table org-mode-transpose-word-syntax-table
  20532. ;; Move to end of line in order to get the first paragraph
  20533. ;; within a plain list or a footnote definition.
  20534. (let ((element (save-excursion
  20535. (end-of-line)
  20536. (or (ignore-errors (org-element-at-point))
  20537. (user-error "An element cannot be parsed line %d"
  20538. (line-number-at-pos (point)))))))
  20539. ;; First check if point is in a blank line at the beginning of
  20540. ;; the buffer. In that case, ignore filling.
  20541. (case (org-element-type element)
  20542. ;; Use major mode filling function is src blocks.
  20543. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20544. ;; Align Org tables, leave table.el tables as-is.
  20545. (table-row (org-table-align) t)
  20546. (table
  20547. (when (eq (org-element-property :type element) 'org)
  20548. (save-excursion
  20549. (goto-char (org-element-property :post-affiliated element))
  20550. (org-table-align)))
  20551. t)
  20552. (paragraph
  20553. ;; Paragraphs may contain `line-break' type objects.
  20554. (let ((beg (max (point-min)
  20555. (org-element-property :contents-begin element)))
  20556. (end (min (point-max)
  20557. (org-element-property :contents-end element))))
  20558. ;; Do nothing if point is at an affiliated keyword.
  20559. (if (< (line-end-position) beg) t
  20560. (when (derived-mode-p 'message-mode)
  20561. ;; In `message-mode', do not fill following citation
  20562. ;; in current paragraph nor text before message body.
  20563. (let ((body-start (save-excursion (message-goto-body))))
  20564. (when body-start (setq beg (max body-start beg))))
  20565. (when (save-excursion
  20566. (re-search-forward
  20567. (concat "^" message-cite-prefix-regexp) end t))
  20568. (setq end (match-beginning 0))))
  20569. ;; Fill paragraph, taking line breaks into account.
  20570. ;; For that, slice the paragraph using line breaks as
  20571. ;; separators, and fill the parts in reverse order to
  20572. ;; avoid messing with markers.
  20573. (save-excursion
  20574. (goto-char end)
  20575. (mapc
  20576. (lambda (pos)
  20577. (fill-region-as-paragraph pos (point) justify)
  20578. (goto-char pos))
  20579. ;; Find the list of ending positions for line breaks
  20580. ;; in the current paragraph. Add paragraph
  20581. ;; beginning to include first slice.
  20582. (nreverse
  20583. (cons beg
  20584. (org-element-map
  20585. (org-element--parse-objects
  20586. beg end nil (org-element-restriction 'paragraph))
  20587. 'line-break
  20588. (lambda (lb) (org-element-property :end lb)))))))
  20589. t)))
  20590. ;; Contents of `comment-block' type elements should be
  20591. ;; filled as plain text, but only if point is within block
  20592. ;; markers.
  20593. (comment-block
  20594. (let* ((case-fold-search t)
  20595. (beg (save-excursion
  20596. (goto-char (org-element-property :begin element))
  20597. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20598. (forward-line)
  20599. (point)))
  20600. (end (save-excursion
  20601. (goto-char (org-element-property :end element))
  20602. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20603. (line-beginning-position))))
  20604. (if (or (< (point) beg) (> (point) end)) t
  20605. (fill-region-as-paragraph
  20606. (save-excursion (end-of-line)
  20607. (re-search-backward "^[ \t]*$" beg 'move)
  20608. (line-beginning-position))
  20609. (save-excursion (beginning-of-line)
  20610. (re-search-forward "^[ \t]*$" end 'move)
  20611. (line-beginning-position))
  20612. justify))))
  20613. ;; Fill comments.
  20614. (comment
  20615. (let ((begin (org-element-property :post-affiliated element))
  20616. (end (org-element-property :end element)))
  20617. (when (and (>= (point) begin) (<= (point) end))
  20618. (let ((begin (save-excursion
  20619. (end-of-line)
  20620. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20621. (progn (forward-line) (point))
  20622. begin)))
  20623. (end (save-excursion
  20624. (end-of-line)
  20625. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20626. (1- (line-beginning-position))
  20627. (skip-chars-backward " \r\t\n")
  20628. (line-end-position)))))
  20629. ;; Do not fill comments when at a blank line.
  20630. (when (> end begin)
  20631. (let ((fill-prefix
  20632. (save-excursion
  20633. (beginning-of-line)
  20634. (looking-at "[ \t]*#")
  20635. (let ((comment-prefix (match-string 0)))
  20636. (goto-char (match-end 0))
  20637. (if (looking-at adaptive-fill-regexp)
  20638. (concat comment-prefix (match-string 0))
  20639. (concat comment-prefix " "))))))
  20640. (save-excursion
  20641. (fill-region-as-paragraph begin end justify))))))
  20642. t))
  20643. ;; Ignore every other element.
  20644. (otherwise t))))))
  20645. (defun org-auto-fill-function ()
  20646. "Auto-fill function."
  20647. ;; Check if auto-filling is meaningful.
  20648. (let ((fc (current-fill-column)))
  20649. (when (and fc (> (current-column) fc))
  20650. (let* ((fill-prefix (org-adaptive-fill-function))
  20651. ;; Enforce empty fill prefix, if required. Otherwise, it
  20652. ;; will be computed again.
  20653. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20654. (when fill-prefix (do-auto-fill))))))
  20655. (defun org-comment-line-break-function (&optional soft)
  20656. "Break line at point and indent, continuing comment if within one.
  20657. The inserted newline is marked hard if variable
  20658. `use-hard-newlines' is true, unless optional argument SOFT is
  20659. non-nil."
  20660. (if soft (insert-and-inherit ?\n) (newline 1))
  20661. (save-excursion (forward-char -1) (delete-horizontal-space))
  20662. (delete-horizontal-space)
  20663. (indent-to-left-margin)
  20664. (insert-before-markers-and-inherit fill-prefix))
  20665. ;;; Comments
  20666. ;; Org comments syntax is quite complex. It requires the entire line
  20667. ;; to be just a comment. Also, even with the right syntax at the
  20668. ;; beginning of line, some some elements (i.e. verse-block or
  20669. ;; example-block) don't accept comments. Usual Emacs comment commands
  20670. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20671. ;; Org still relies on `comment-dwim', but cannot trust
  20672. ;; `comment-only-p'. So, `comment-region-function' and
  20673. ;; `uncomment-region-function' both point
  20674. ;; to`org-comment-or-uncomment-region'. Eventually,
  20675. ;; `org-insert-comment' takes care of insertion of comments at the
  20676. ;; beginning of line.
  20677. ;; `org-setup-comments-handling' install comments related variables
  20678. ;; during `org-mode' initialization.
  20679. (defun org-setup-comments-handling ()
  20680. (interactive)
  20681. (org-set-local 'comment-use-syntax nil)
  20682. (org-set-local 'comment-start "# ")
  20683. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20684. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20685. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20686. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20687. (defun org-insert-comment ()
  20688. "Insert an empty comment above current line.
  20689. If the line is empty, insert comment at its beginning."
  20690. (beginning-of-line)
  20691. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20692. (org-indent-line)
  20693. (insert "# "))
  20694. (defvar comment-empty-lines) ; From newcomment.el.
  20695. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20696. "Comment or uncomment each non-blank line in the region.
  20697. Uncomment each non-blank line between BEG and END if it only
  20698. contains commented lines. Otherwise, comment them."
  20699. (save-restriction
  20700. ;; Restrict region
  20701. (narrow-to-region (save-excursion (goto-char beg)
  20702. (skip-chars-forward " \r\t\n" end)
  20703. (line-beginning-position))
  20704. (save-excursion (goto-char end)
  20705. (skip-chars-backward " \r\t\n" beg)
  20706. (line-end-position)))
  20707. (let ((uncommentp
  20708. ;; UNCOMMENTP is non-nil when every non blank line between
  20709. ;; BEG and END is a comment.
  20710. (save-excursion
  20711. (goto-char (point-min))
  20712. (while (and (not (eobp))
  20713. (let ((element (org-element-at-point)))
  20714. (and (eq (org-element-type element) 'comment)
  20715. (goto-char (min (point-max)
  20716. (org-element-property
  20717. :end element)))))))
  20718. (eobp))))
  20719. (if uncommentp
  20720. ;; Only blank lines and comments in region: uncomment it.
  20721. (save-excursion
  20722. (goto-char (point-min))
  20723. (while (not (eobp))
  20724. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20725. (replace-match "" nil nil nil 1))
  20726. (forward-line)))
  20727. ;; Comment each line in region.
  20728. (let ((min-indent (point-max)))
  20729. ;; First find the minimum indentation across all lines.
  20730. (save-excursion
  20731. (goto-char (point-min))
  20732. (while (and (not (eobp)) (not (zerop min-indent)))
  20733. (unless (looking-at "[ \t]*$")
  20734. (setq min-indent (min min-indent (current-indentation))))
  20735. (forward-line)))
  20736. ;; Then loop over all lines.
  20737. (save-excursion
  20738. (goto-char (point-min))
  20739. (while (not (eobp))
  20740. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20741. ;; Don't get fooled by invisible text (e.g. link path)
  20742. ;; when moving to column MIN-INDENT.
  20743. (let ((buffer-invisibility-spec nil))
  20744. (org-move-to-column min-indent t))
  20745. (insert comment-start))
  20746. (forward-line))))))))
  20747. ;;; Planning
  20748. ;; This section contains tools to operate on timestamp objects, as
  20749. ;; returned by, e.g. `org-element-context'.
  20750. (defun org-timestamp-has-time-p (timestamp)
  20751. "Non-nil when TIMESTAMP has a time specified."
  20752. (org-element-property :hour-start timestamp))
  20753. (defun org-timestamp-format (timestamp format &optional end utc)
  20754. "Format a TIMESTAMP element into a string.
  20755. FORMAT is a format specifier to be passed to
  20756. `format-time-string'.
  20757. When optional argument END is non-nil, use end of date-range or
  20758. time-range, if possible.
  20759. When optional argument UTC is non-nil, time will be expressed as
  20760. Universal Time."
  20761. (format-time-string
  20762. format
  20763. (apply 'encode-time
  20764. (cons 0
  20765. (mapcar
  20766. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20767. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20768. '(:minute-start :hour-start :day-start :month-start
  20769. :year-start)))))
  20770. utc))
  20771. (defun org-timestamp-split-range (timestamp &optional end)
  20772. "Extract a timestamp object from a date or time range.
  20773. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20774. the end of the range. Otherwise, extract its start.
  20775. Return a new timestamp object sharing the same parent as
  20776. TIMESTAMP."
  20777. (let ((type (org-element-property :type timestamp)))
  20778. (if (memq type '(active inactive diary)) timestamp
  20779. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20780. ;; Set new type.
  20781. (org-element-put-property
  20782. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20783. ;; Copy start properties over end properties if END is
  20784. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20785. (let ((p-alist '((:minute-start . :minute-end)
  20786. (:hour-start . :hour-end)
  20787. (:day-start . :day-end)
  20788. (:month-start . :month-end)
  20789. (:year-start . :year-end))))
  20790. (dolist (p-cell p-alist)
  20791. (org-element-put-property
  20792. split-ts
  20793. (funcall (if end 'car 'cdr) p-cell)
  20794. (org-element-property
  20795. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20796. ;; Eventually refresh `:raw-value'.
  20797. (org-element-put-property split-ts :raw-value nil)
  20798. (org-element-put-property
  20799. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20800. (defun org-timestamp-translate (timestamp &optional boundary)
  20801. "Apply `org-translate-time' on a TIMESTAMP object.
  20802. When optional argument BOUNDARY is non-nil, it is either the
  20803. symbol `start' or `end'. In this case, only translate the
  20804. starting or ending part of TIMESTAMP if it is a date or time
  20805. range. Otherwise, translate both parts."
  20806. (if (and (not boundary)
  20807. (memq (org-element-property :type timestamp)
  20808. '(active-range inactive-range)))
  20809. (concat
  20810. (org-translate-time
  20811. (org-element-property :raw-value
  20812. (org-timestamp-split-range timestamp)))
  20813. "--"
  20814. (org-translate-time
  20815. (org-element-property :raw-value
  20816. (org-timestamp-split-range timestamp t))))
  20817. (org-translate-time
  20818. (org-element-property
  20819. :raw-value
  20820. (if (not boundary) timestamp
  20821. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20822. ;;; Other stuff.
  20823. (defun org-toggle-fixed-width-section (arg)
  20824. "Toggle the fixed-width export.
  20825. If there is no active region, the QUOTE keyword at the current headline is
  20826. inserted or removed. When present, it causes the text between this headline
  20827. and the next to be exported as fixed-width text, and unmodified.
  20828. If there is an active region, this command adds or removes a colon as the
  20829. first character of this line. If the first character of a line is a colon,
  20830. this line is also exported in fixed-width font."
  20831. (interactive "P")
  20832. (let* ((cc 0)
  20833. (regionp (org-region-active-p))
  20834. (beg (if regionp (region-beginning) (point)))
  20835. (end (if regionp (region-end)))
  20836. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20837. (case-fold-search nil)
  20838. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20839. off)
  20840. (if regionp
  20841. (save-excursion
  20842. (goto-char beg)
  20843. (setq cc (current-column))
  20844. (beginning-of-line 1)
  20845. (setq off (looking-at re))
  20846. (while (> nlines 0)
  20847. (setq nlines (1- nlines))
  20848. (beginning-of-line 1)
  20849. (cond
  20850. (arg
  20851. (org-move-to-column cc t)
  20852. (insert ": \n")
  20853. (forward-line -1))
  20854. ((and off (looking-at re))
  20855. (replace-match "" t t nil 1))
  20856. ((not off) (org-move-to-column cc t) (insert ": ")))
  20857. (forward-line 1)))
  20858. (save-excursion
  20859. (org-back-to-heading)
  20860. (cond
  20861. ((looking-at (format org-heading-keyword-regexp-format
  20862. org-quote-string))
  20863. (goto-char (match-end 1))
  20864. (looking-at (concat " +" org-quote-string))
  20865. (replace-match "" t t)
  20866. (when (eolp) (insert " ")))
  20867. ((looking-at org-outline-regexp)
  20868. (goto-char (match-end 0))
  20869. (insert org-quote-string " ")))))))
  20870. (defun org-reftex-citation ()
  20871. "Use reftex-citation to insert a citation into the buffer.
  20872. This looks for a line like
  20873. #+BIBLIOGRAPHY: foo plain option:-d
  20874. and derives from it that foo.bib is the bibliography file relevant
  20875. for this document. It then installs the necessary environment for RefTeX
  20876. to work in this buffer and calls `reftex-citation' to insert a citation
  20877. into the buffer.
  20878. Export of such citations to both LaTeX and HTML is handled by the contributed
  20879. package ox-bibtex by Taru Karttunen."
  20880. (interactive)
  20881. (let ((reftex-docstruct-symbol 'rds)
  20882. (reftex-cite-format "\\cite{%l}")
  20883. rds bib)
  20884. (save-excursion
  20885. (save-restriction
  20886. (widen)
  20887. (let ((case-fold-search t)
  20888. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20889. (if (not (save-excursion
  20890. (or (re-search-forward re nil t)
  20891. (re-search-backward re nil t))))
  20892. (error "No bibliography defined in file")
  20893. (setq bib (concat (match-string 1) ".bib")
  20894. rds (list (list 'bib bib)))))))
  20895. (call-interactively 'reftex-citation)))
  20896. ;;;; Functions extending outline functionality
  20897. (defun org-beginning-of-line (&optional arg)
  20898. "Go to the beginning of the current line. If that is invisible, continue
  20899. to a visible line beginning. This makes the function of C-a more intuitive.
  20900. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20901. first attempt, and only move to after the tags when the cursor is already
  20902. beyond the end of the headline."
  20903. (interactive "P")
  20904. (let ((pos (point))
  20905. (special (if (consp org-special-ctrl-a/e)
  20906. (car org-special-ctrl-a/e)
  20907. org-special-ctrl-a/e))
  20908. deactivate-mark refpos)
  20909. (if (org-bound-and-true-p visual-line-mode)
  20910. (beginning-of-visual-line 1)
  20911. (beginning-of-line 1))
  20912. (if (and arg (fboundp 'move-beginning-of-line))
  20913. (call-interactively 'move-beginning-of-line)
  20914. (if (bobp)
  20915. nil
  20916. (backward-char 1)
  20917. (if (org-truely-invisible-p)
  20918. (while (and (not (bobp)) (org-truely-invisible-p))
  20919. (backward-char 1)
  20920. (beginning-of-line 1))
  20921. (forward-char 1))))
  20922. (when special
  20923. (cond
  20924. ((and (looking-at org-complex-heading-regexp)
  20925. (= (char-after (match-end 1)) ?\ ))
  20926. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20927. (point-at-eol)))
  20928. (goto-char
  20929. (if (eq special t)
  20930. (cond ((> pos refpos) refpos)
  20931. ((= pos (point)) refpos)
  20932. (t (point)))
  20933. (cond ((> pos (point)) (point))
  20934. ((not (eq last-command this-command)) (point))
  20935. (t refpos)))))
  20936. ((org-at-item-p)
  20937. ;; Being at an item and not looking at an the item means point
  20938. ;; was previously moved to beginning of a visual line, which
  20939. ;; doesn't contain the item. Therefore, do nothing special,
  20940. ;; just stay here.
  20941. (when (looking-at org-list-full-item-re)
  20942. ;; Set special position at first white space character after
  20943. ;; bullet, and check-box, if any.
  20944. (let ((after-bullet
  20945. (let ((box (match-end 3)))
  20946. (if (not box) (match-end 1)
  20947. (let ((after (char-after box)))
  20948. (if (and after (= after ? )) (1+ box) box))))))
  20949. ;; Special case: Move point to special position when
  20950. ;; currently after it or at beginning of line.
  20951. (if (eq special t)
  20952. (when (or (> pos after-bullet) (= (point) pos))
  20953. (goto-char after-bullet))
  20954. ;; Reversed case: Move point to special position when
  20955. ;; point was already at beginning of line and command is
  20956. ;; repeated.
  20957. (when (and (= (point) pos) (eq last-command this-command))
  20958. (goto-char after-bullet))))))))
  20959. (org-no-warnings
  20960. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20961. (setq disable-point-adjustment
  20962. (or (not (invisible-p (point)))
  20963. (not (invisible-p (max (point-min) (1- (point))))))))
  20964. (defun org-end-of-line (&optional arg)
  20965. "Go to the end of the line.
  20966. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20967. tags on the first attempt, and only move to after the tags when
  20968. the cursor is already beyond the end of the headline."
  20969. (interactive "P")
  20970. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20971. org-special-ctrl-a/e))
  20972. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20973. 'end-of-visual-line)
  20974. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20975. (t 'end-of-line)))
  20976. deactivate-mark)
  20977. (if (or (not special) arg) (call-interactively move-fun)
  20978. (let* ((element (save-excursion (beginning-of-line)
  20979. (org-element-at-point)))
  20980. (type (org-element-type element)))
  20981. (cond
  20982. ((memq type '(headline inlinetask))
  20983. (let ((pos (point)))
  20984. (beginning-of-line 1)
  20985. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20986. (if (eq special t)
  20987. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20988. (goto-char (match-beginning 1))
  20989. (goto-char (match-end 0)))
  20990. (if (or (< pos (match-end 0))
  20991. (not (eq this-command last-command)))
  20992. (goto-char (match-end 0))
  20993. (goto-char (match-beginning 1))))
  20994. (call-interactively move-fun))))
  20995. ((outline-invisible-p (line-end-position))
  20996. ;; If element is hidden, `move-end-of-line' would put point
  20997. ;; after it. Use `end-of-line' to stay on current line.
  20998. (call-interactively 'end-of-line))
  20999. (t (call-interactively move-fun)))))
  21000. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21001. (setq disable-point-adjustment
  21002. (or (not (invisible-p (point)))
  21003. (not (invisible-p (max (point-min) (1- (point))))))))
  21004. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21005. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21006. (defun org-backward-sentence (&optional arg)
  21007. "Go to beginning of sentence, or beginning of table field.
  21008. This will call `backward-sentence' or `org-table-beginning-of-field',
  21009. depending on context."
  21010. (interactive "P")
  21011. (cond
  21012. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21013. (t (call-interactively 'backward-sentence))))
  21014. (defun org-forward-sentence (&optional arg)
  21015. "Go to end of sentence, or end of table field.
  21016. This will call `forward-sentence' or `org-table-end-of-field',
  21017. depending on context."
  21018. (interactive "P")
  21019. (cond
  21020. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21021. (t (call-interactively 'forward-sentence))))
  21022. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21023. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21024. (defun org-kill-line (&optional arg)
  21025. "Kill line, to tags or end of line."
  21026. (interactive "P")
  21027. (cond
  21028. ((or (not org-special-ctrl-k)
  21029. (bolp)
  21030. (not (org-at-heading-p)))
  21031. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21032. org-ctrl-k-protect-subtree)
  21033. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21034. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21035. (user-error "C-k aborted as it would kill a hidden subtree")))
  21036. (call-interactively
  21037. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21038. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21039. (kill-region (point) (match-beginning 1))
  21040. (org-set-tags nil t))
  21041. (t (kill-region (point) (point-at-eol)))))
  21042. (define-key org-mode-map "\C-k" 'org-kill-line)
  21043. (defun org-yank (&optional arg)
  21044. "Yank. If the kill is a subtree, treat it specially.
  21045. This command will look at the current kill and check if is a single
  21046. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21047. cursor is at the beginning of a line or after the stars of a currently
  21048. empty headline, then the yank is handled specially. How exactly depends
  21049. on the value of the following variables, both set by default.
  21050. org-yank-folded-subtrees
  21051. When set, the subtree(s) will be folded after insertion, but only
  21052. if doing so would now swallow text after the yanked text.
  21053. org-yank-adjusted-subtrees
  21054. When set, the subtree will be promoted or demoted in order to
  21055. fit into the local outline tree structure, which means that the level
  21056. will be adjusted so that it becomes the smaller one of the two
  21057. *visible* surrounding headings.
  21058. Any prefix to this command will cause `yank' to be called directly with
  21059. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21060. will just
  21061. plainly yank the text as it is.
  21062. \[1] The test checks if the first non-white line is a heading
  21063. and if there are no other headings with fewer stars."
  21064. (interactive "P")
  21065. (org-yank-generic 'yank arg))
  21066. (defun org-yank-generic (command arg)
  21067. "Perform some yank-like command.
  21068. This function implements the behavior described in the `org-yank'
  21069. documentation. However, it has been generalized to work for any
  21070. interactive command with similar behavior."
  21071. ;; pretend to be command COMMAND
  21072. (setq this-command command)
  21073. (if arg
  21074. (call-interactively command)
  21075. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21076. (and (org-kill-is-subtree-p)
  21077. (or (bolp)
  21078. (and (looking-at "[ \t]*$")
  21079. (string-match
  21080. "\\`\\*+\\'"
  21081. (buffer-substring (point-at-bol) (point)))))))
  21082. swallowp)
  21083. (cond
  21084. ((and subtreep org-yank-folded-subtrees)
  21085. (let ((beg (point))
  21086. end)
  21087. (if (and subtreep org-yank-adjusted-subtrees)
  21088. (org-paste-subtree nil nil 'for-yank)
  21089. (call-interactively command))
  21090. (setq end (point))
  21091. (goto-char beg)
  21092. (when (and (bolp) subtreep
  21093. (not (setq swallowp
  21094. (org-yank-folding-would-swallow-text beg end))))
  21095. (org-with-limited-levels
  21096. (or (looking-at org-outline-regexp)
  21097. (re-search-forward org-outline-regexp-bol end t))
  21098. (while (and (< (point) end) (looking-at org-outline-regexp))
  21099. (hide-subtree)
  21100. (org-cycle-show-empty-lines 'folded)
  21101. (condition-case nil
  21102. (outline-forward-same-level 1)
  21103. (error (goto-char end))))))
  21104. (when swallowp
  21105. (message
  21106. "Inserted text not folded because that would swallow text"))
  21107. (goto-char end)
  21108. (skip-chars-forward " \t\n\r")
  21109. (beginning-of-line 1)
  21110. (push-mark beg 'nomsg)))
  21111. ((and subtreep org-yank-adjusted-subtrees)
  21112. (let ((beg (point-at-bol)))
  21113. (org-paste-subtree nil nil 'for-yank)
  21114. (push-mark beg 'nomsg)))
  21115. (t
  21116. (call-interactively command))))))
  21117. (defun org-yank-folding-would-swallow-text (beg end)
  21118. "Would hide-subtree at BEG swallow any text after END?"
  21119. (let (level)
  21120. (org-with-limited-levels
  21121. (save-excursion
  21122. (goto-char beg)
  21123. (when (or (looking-at org-outline-regexp)
  21124. (re-search-forward org-outline-regexp-bol end t))
  21125. (setq level (org-outline-level)))
  21126. (goto-char end)
  21127. (skip-chars-forward " \t\r\n\v\f")
  21128. (if (or (eobp)
  21129. (and (bolp) (looking-at org-outline-regexp)
  21130. (<= (org-outline-level) level)))
  21131. nil ; Nothing would be swallowed
  21132. t))))) ; something would swallow
  21133. (define-key org-mode-map "\C-y" 'org-yank)
  21134. (defun org-truely-invisible-p ()
  21135. "Check if point is at a character currently not visible.
  21136. This version does not only check the character property, but also
  21137. `visible-mode'."
  21138. ;; Early versions of noutline don't have `outline-invisible-p'.
  21139. (if (org-bound-and-true-p visible-mode)
  21140. nil
  21141. (outline-invisible-p)))
  21142. (defun org-invisible-p2 ()
  21143. "Check if point is at a character currently not visible."
  21144. (save-excursion
  21145. (if (and (eolp) (not (bobp))) (backward-char 1))
  21146. ;; Early versions of noutline don't have `outline-invisible-p'.
  21147. (outline-invisible-p)))
  21148. (defun org-back-to-heading (&optional invisible-ok)
  21149. "Call `outline-back-to-heading', but provide a better error message."
  21150. (condition-case nil
  21151. (outline-back-to-heading invisible-ok)
  21152. (error (error "Before first headline at position %d in buffer %s"
  21153. (point) (current-buffer)))))
  21154. (defun org-before-first-heading-p ()
  21155. "Before first heading?"
  21156. (save-excursion
  21157. (end-of-line)
  21158. (null (re-search-backward org-outline-regexp-bol nil t))))
  21159. (defun org-at-heading-p (&optional ignored)
  21160. (outline-on-heading-p t))
  21161. ;; Compatibility alias with Org versions < 7.8.03
  21162. (defalias 'org-on-heading-p 'org-at-heading-p)
  21163. (defun org-at-comment-p nil
  21164. "Is cursor in a line starting with a # character?"
  21165. (save-excursion
  21166. (beginning-of-line)
  21167. (looking-at "^#")))
  21168. (defun org-at-drawer-p nil
  21169. "Is cursor at a drawer keyword?"
  21170. (save-excursion
  21171. (move-beginning-of-line 1)
  21172. (looking-at org-drawer-regexp)))
  21173. (defun org-at-block-p nil
  21174. "Is cursor at a block keyword?"
  21175. (save-excursion
  21176. (move-beginning-of-line 1)
  21177. (looking-at org-block-regexp)))
  21178. (defun org-point-at-end-of-empty-headline ()
  21179. "If point is at the end of an empty headline, return t, else nil.
  21180. If the heading only contains a TODO keyword, it is still still considered
  21181. empty."
  21182. (and (looking-at "[ \t]*$")
  21183. (when org-todo-line-regexp
  21184. (save-excursion
  21185. (beginning-of-line 1)
  21186. (let ((case-fold-search nil))
  21187. (looking-at org-todo-line-regexp)
  21188. (string= (match-string 3) ""))))))
  21189. (defun org-at-heading-or-item-p ()
  21190. (or (org-at-heading-p) (org-at-item-p)))
  21191. (defun org-at-target-p ()
  21192. (or (org-in-regexp org-radio-target-regexp)
  21193. (org-in-regexp org-target-regexp)))
  21194. ;; Compatibility alias with Org versions < 7.8.03
  21195. (defalias 'org-on-target-p 'org-at-target-p)
  21196. (defun org-up-heading-all (arg)
  21197. "Move to the heading line of which the present line is a subheading.
  21198. This function considers both visible and invisible heading lines.
  21199. With argument, move up ARG levels."
  21200. (if (fboundp 'outline-up-heading-all)
  21201. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21202. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21203. (defun org-up-heading-safe ()
  21204. "Move to the heading line of which the present line is a subheading.
  21205. This version will not throw an error. It will return the level of the
  21206. headline found, or nil if no higher level is found.
  21207. Also, this function will be a lot faster than `outline-up-heading',
  21208. because it relies on stars being the outline starters. This can really
  21209. make a significant difference in outlines with very many siblings."
  21210. (let (start-level re)
  21211. (org-back-to-heading t)
  21212. (setq start-level (funcall outline-level))
  21213. (if (equal start-level 1)
  21214. nil
  21215. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21216. (if (re-search-backward re nil t)
  21217. (funcall outline-level)))))
  21218. (defun org-first-sibling-p ()
  21219. "Is this heading the first child of its parents?"
  21220. (interactive)
  21221. (let ((re org-outline-regexp-bol)
  21222. level l)
  21223. (unless (org-at-heading-p t)
  21224. (user-error "Not at a heading"))
  21225. (setq level (funcall outline-level))
  21226. (save-excursion
  21227. (if (not (re-search-backward re nil t))
  21228. t
  21229. (setq l (funcall outline-level))
  21230. (< l level)))))
  21231. (defun org-goto-sibling (&optional previous)
  21232. "Goto the next sibling, even if it is invisible.
  21233. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21234. when a sibling was found. When none is found, return nil and don't
  21235. move point."
  21236. (let ((fun (if previous 're-search-backward 're-search-forward))
  21237. (pos (point))
  21238. (re org-outline-regexp-bol)
  21239. level l)
  21240. (when (condition-case nil (org-back-to-heading t) (error nil))
  21241. (setq level (funcall outline-level))
  21242. (catch 'exit
  21243. (or previous (forward-char 1))
  21244. (while (funcall fun re nil t)
  21245. (setq l (funcall outline-level))
  21246. (when (< l level) (goto-char pos) (throw 'exit nil))
  21247. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21248. (goto-char pos)
  21249. nil))))
  21250. (defun org-show-siblings ()
  21251. "Show all siblings of the current headline."
  21252. (save-excursion
  21253. (while (org-goto-sibling) (org-flag-heading nil)))
  21254. (save-excursion
  21255. (while (org-goto-sibling 'previous)
  21256. (org-flag-heading nil))))
  21257. (defun org-goto-first-child ()
  21258. "Goto the first child, even if it is invisible.
  21259. Return t when a child was found. Otherwise don't move point and
  21260. return nil."
  21261. (let (level (pos (point)) (re org-outline-regexp-bol))
  21262. (when (condition-case nil (org-back-to-heading t) (error nil))
  21263. (setq level (outline-level))
  21264. (forward-char 1)
  21265. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21266. (progn (goto-char (match-beginning 0)) t)
  21267. (goto-char pos) nil))))
  21268. (defun org-show-hidden-entry ()
  21269. "Show an entry where even the heading is hidden."
  21270. (save-excursion
  21271. (org-show-entry)))
  21272. (defun org-flag-heading (flag &optional entry)
  21273. "Flag the current heading. FLAG non-nil means make invisible.
  21274. When ENTRY is non-nil, show the entire entry."
  21275. (save-excursion
  21276. (org-back-to-heading t)
  21277. ;; Check if we should show the entire entry
  21278. (if entry
  21279. (progn
  21280. (org-show-entry)
  21281. (save-excursion
  21282. (and (outline-next-heading)
  21283. (org-flag-heading nil))))
  21284. (outline-flag-region (max (point-min) (1- (point)))
  21285. (save-excursion (outline-end-of-heading) (point))
  21286. flag))))
  21287. (defun org-get-next-sibling ()
  21288. "Move to next heading of the same level, and return point.
  21289. If there is no such heading, return nil.
  21290. This is like outline-next-sibling, but invisible headings are ok."
  21291. (let ((level (funcall outline-level)))
  21292. (outline-next-heading)
  21293. (while (and (not (eobp)) (> (funcall outline-level) level))
  21294. (outline-next-heading))
  21295. (if (or (eobp) (< (funcall outline-level) level))
  21296. nil
  21297. (point))))
  21298. (defun org-get-last-sibling ()
  21299. "Move to previous heading of the same level, and return point.
  21300. If there is no such heading, return nil."
  21301. (let ((opoint (point))
  21302. (level (funcall outline-level)))
  21303. (outline-previous-heading)
  21304. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21305. (while (and (> (funcall outline-level) level)
  21306. (not (bobp)))
  21307. (outline-previous-heading))
  21308. (if (< (funcall outline-level) level)
  21309. nil
  21310. (point)))))
  21311. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21312. "Goto to the end of a subtree."
  21313. ;; This contains an exact copy of the original function, but it uses
  21314. ;; `org-back-to-heading', to make it work also in invisible
  21315. ;; trees. And is uses an invisible-ok argument.
  21316. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21317. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21318. ;; with many children and grandchildren etc, this can be much faster
  21319. ;; than the outline version.
  21320. (org-back-to-heading invisible-ok)
  21321. (let ((first t)
  21322. (level (funcall outline-level)))
  21323. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21324. ;; A true heading (not a plain list item), in Org-mode
  21325. ;; This means we can easily find the end by looking
  21326. ;; only for the right number of stars. Using a regexp to do
  21327. ;; this is so much faster than using a Lisp loop.
  21328. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21329. (forward-char 1)
  21330. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21331. ;; something else, do it the slow way
  21332. (while (and (not (eobp))
  21333. (or first (> (funcall outline-level) level)))
  21334. (setq first nil)
  21335. (outline-next-heading)))
  21336. (unless to-heading
  21337. (if (memq (preceding-char) '(?\n ?\^M))
  21338. (progn
  21339. ;; Go to end of line before heading
  21340. (forward-char -1)
  21341. (if (memq (preceding-char) '(?\n ?\^M))
  21342. ;; leave blank line before heading
  21343. (forward-char -1))))))
  21344. (point))
  21345. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21346. "Use Org version in org-mode, for dramatic speed-up."
  21347. (if (derived-mode-p 'org-mode)
  21348. (progn
  21349. (org-end-of-subtree nil t)
  21350. (unless (eobp) (backward-char 1)))
  21351. ad-do-it))
  21352. (defun org-end-of-meta-data-and-drawers ()
  21353. "Jump to the first text after meta data and drawers in the current entry.
  21354. This will move over empty lines, lines with planning time stamps,
  21355. clocking lines, and drawers."
  21356. (org-back-to-heading t)
  21357. (let ((end (save-excursion (outline-next-heading) (point)))
  21358. (re (concat "\\(" org-drawer-regexp "\\)"
  21359. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21360. (forward-line 1)
  21361. (while (re-search-forward re end t)
  21362. (if (not (match-end 1))
  21363. ;; empty or planning line
  21364. (forward-line 1)
  21365. ;; a drawer, find the end
  21366. (re-search-forward "^[ \t]*:END:" end 'move)
  21367. (forward-line 1)))
  21368. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21369. (point)))
  21370. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21371. "Move forward to the ARG'th subheading at same level as this one.
  21372. Stop at the first and last subheadings of a superior heading.
  21373. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21374. non-nil it will also look at invisible ones."
  21375. (interactive "p")
  21376. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21377. (if (and arg (< arg 0))
  21378. (goto-char (point-min))
  21379. (outline-next-heading))
  21380. (org-at-heading-p)
  21381. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21382. (f (if (and arg (< arg 0))
  21383. 're-search-backward
  21384. 're-search-forward))
  21385. (count (if arg (abs arg) 1))
  21386. (result (point)))
  21387. (while (and (prog1 (> count 0)
  21388. (forward-char (if (and arg (< arg 0)) -1 1)))
  21389. (funcall f org-outline-regexp-bol nil 'move))
  21390. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21391. (cond ((< l level) (setq count 0))
  21392. ((and (= l level)
  21393. (or invisible-ok
  21394. (progn
  21395. (goto-char (line-beginning-position))
  21396. (not (outline-invisible-p)))))
  21397. (setq count (1- count))
  21398. (when (eq l level)
  21399. (setq result (point)))))))
  21400. (goto-char result))
  21401. (beginning-of-line 1)))
  21402. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21403. "Move backward to the ARG'th subheading at same level as this one.
  21404. Stop at the first and last subheadings of a superior heading."
  21405. (interactive "p")
  21406. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21407. (defun org-next-block (arg &optional backward block-regexp)
  21408. "Jump to the next block.
  21409. With a prefix argument ARG, jump forward ARG many source blocks.
  21410. When BACKWARD is non-nil, jump to the previous block.
  21411. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21412. (interactive "p")
  21413. (let ((re (or block-regexp org-block-regexp))
  21414. (re-search-fn (or (and backward 're-search-backward)
  21415. 're-search-forward)))
  21416. (if (looking-at re) (forward-char 1))
  21417. (condition-case nil
  21418. (funcall re-search-fn re nil nil arg)
  21419. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21420. (goto-char (match-beginning 0)) (org-show-context)))
  21421. (defun org-previous-block (arg &optional block-regexp)
  21422. "Jump to the previous block.
  21423. With a prefix argument ARG, jump backward ARG many source blocks.
  21424. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21425. (interactive "p")
  21426. (org-next-block arg t block-regexp))
  21427. (defun org-forward-paragraph ()
  21428. "Move forward to beginning of next paragraph or equivalent.
  21429. The function moves point to the beginning of the next visible
  21430. structural element, which can be a paragraph, a table, a list
  21431. item, etc. It also provides some special moves for convenience:
  21432. - On an affiliated keyword, jump to the beginning of the
  21433. relative element.
  21434. - On an item or a footnote definition, move to the second
  21435. element inside, if any.
  21436. - On a table or a property drawer, jump after it.
  21437. - On a verse or source block, stop after blank lines."
  21438. (interactive)
  21439. (when (eobp) (user-error "Cannot move further down"))
  21440. (let* ((deactivate-mark nil)
  21441. (element (org-element-at-point))
  21442. (type (org-element-type element))
  21443. (post-affiliated (org-element-property :post-affiliated element))
  21444. (contents-begin (org-element-property :contents-begin element))
  21445. (contents-end (org-element-property :contents-end element))
  21446. (end (let ((end (org-element-property :end element)) (parent element))
  21447. (while (and (setq parent (org-element-property :parent parent))
  21448. (= (org-element-property :contents-end parent) end))
  21449. (setq end (org-element-property :end parent)))
  21450. end)))
  21451. (cond ((not element)
  21452. (skip-chars-forward " \r\t\n")
  21453. (or (eobp) (beginning-of-line)))
  21454. ;; On affiliated keywords, move to element's beginning.
  21455. ((and post-affiliated (< (point) post-affiliated))
  21456. (goto-char post-affiliated))
  21457. ;; At a table row, move to the end of the table. Similarly,
  21458. ;; at a node property, move to the end of the property
  21459. ;; drawer.
  21460. ((memq type '(node-property table-row))
  21461. (goto-char (org-element-property
  21462. :end (org-element-property :parent element))))
  21463. ((memq type '(property-drawer table)) (goto-char end))
  21464. ;; Consider blank lines as separators in verse and source
  21465. ;; blocks to ease editing.
  21466. ((memq type '(src-block verse-block))
  21467. (when (eq type 'src-block)
  21468. (setq contents-end
  21469. (save-excursion (goto-char end)
  21470. (skip-chars-backward " \r\t\n")
  21471. (line-beginning-position))))
  21472. (beginning-of-line)
  21473. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21474. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21475. (goto-char end)
  21476. (skip-chars-forward " \r\t\n")
  21477. (if (= (point) contents-end) (goto-char end)
  21478. (beginning-of-line))))
  21479. ;; With no contents, just skip element.
  21480. ((not contents-begin) (goto-char end))
  21481. ;; If contents are invisible, skip the element altogether.
  21482. ((outline-invisible-p (line-end-position))
  21483. (case type
  21484. (headline
  21485. (org-with-limited-levels (outline-next-visible-heading 1)))
  21486. ;; At a plain list, make sure we move to the next item
  21487. ;; instead of skipping the whole list.
  21488. (plain-list (forward-char)
  21489. (org-forward-paragraph))
  21490. (otherwise (goto-char end))))
  21491. ((>= (point) contents-end) (goto-char end))
  21492. ((>= (point) contents-begin)
  21493. ;; This can only happen on paragraphs and plain lists.
  21494. (case type
  21495. (paragraph (goto-char end))
  21496. ;; At a plain list, try to move to second element in
  21497. ;; first item, if possible.
  21498. (plain-list (end-of-line)
  21499. (org-forward-paragraph))))
  21500. ;; When contents start on the middle of a line (e.g. in
  21501. ;; items and footnote definitions), try to reach first
  21502. ;; element starting after current line.
  21503. ((> (line-end-position) contents-begin)
  21504. (end-of-line)
  21505. (org-forward-paragraph))
  21506. (t (goto-char contents-begin)))))
  21507. (defun org-backward-paragraph ()
  21508. "Move backward to start of previous paragraph or equivalent.
  21509. The function moves point to the beginning of the current
  21510. structural element, which can be a paragraph, a table, a list
  21511. item, etc., or to the beginning of the previous visible one if
  21512. point is already there. It also provides some special moves for
  21513. convenience:
  21514. - On an affiliated keyword, jump to the first one.
  21515. - On a table or a property drawer, move to its beginning.
  21516. - On a verse or source block, stop before blank lines."
  21517. (interactive)
  21518. (when (bobp) (user-error "Cannot move further up"))
  21519. (let* ((deactivate-mark nil)
  21520. (element (org-element-at-point))
  21521. (type (org-element-type element))
  21522. (contents-begin (org-element-property :contents-begin element))
  21523. (contents-end (org-element-property :contents-end element))
  21524. (post-affiliated (org-element-property :post-affiliated element))
  21525. (begin (org-element-property :begin element)))
  21526. (cond
  21527. ((not element) (goto-char (point-min)))
  21528. ((= (point) begin)
  21529. (backward-char)
  21530. (org-backward-paragraph))
  21531. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21532. ((memq type '(node-property table-row))
  21533. (goto-char (org-element-property
  21534. :post-affiliated (org-element-property :parent element))))
  21535. ((memq type '(property-drawer table)) (goto-char begin))
  21536. ((memq type '(src-block verse-block))
  21537. (when (eq type 'src-block)
  21538. (setq contents-begin
  21539. (save-excursion (goto-char begin) (forward-line) (point))))
  21540. (if (= (point) contents-begin) (goto-char post-affiliated)
  21541. ;; Inside a verse block, see blank lines as paragraph
  21542. ;; separators.
  21543. (let ((origin (point)))
  21544. (skip-chars-backward " \r\t\n" contents-begin)
  21545. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21546. (skip-chars-forward " \r\t\n" origin)
  21547. (if (= (point) origin) (goto-char contents-begin)
  21548. (beginning-of-line))))))
  21549. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21550. ((eq type 'paragraph)
  21551. (goto-char contents-begin)
  21552. ;; When at first paragraph in an item or a footnote definition,
  21553. ;; move directly to beginning of line.
  21554. (let ((parent-contents
  21555. (org-element-property
  21556. :contents-begin (org-element-property :parent element))))
  21557. (when (and parent-contents (= parent-contents contents-begin))
  21558. (beginning-of-line))))
  21559. ;; At the end of a greater element, move to the beginning of the
  21560. ;; last element within.
  21561. ((>= (point) contents-end)
  21562. (goto-char (1- contents-end))
  21563. (org-backward-paragraph))
  21564. (t (goto-char (or post-affiliated begin))))
  21565. ;; Ensure we never leave point invisible.
  21566. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21567. (defun org-forward-element ()
  21568. "Move forward by one element.
  21569. Move to the next element at the same level, when possible."
  21570. (interactive)
  21571. (cond ((eobp) (user-error "Cannot move further down"))
  21572. ((org-with-limited-levels (org-at-heading-p))
  21573. (let ((origin (point)))
  21574. (goto-char (org-end-of-subtree nil t))
  21575. (unless (org-with-limited-levels (org-at-heading-p))
  21576. (goto-char origin)
  21577. (user-error "Cannot move further down"))))
  21578. (t
  21579. (let* ((elem (org-element-at-point))
  21580. (end (org-element-property :end elem))
  21581. (parent (org-element-property :parent elem)))
  21582. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21583. (goto-char (org-element-property :end parent)))
  21584. ((integer-or-marker-p end) (goto-char end))
  21585. (t (message "No element at point")))))))
  21586. (defun org-backward-element ()
  21587. "Move backward by one element.
  21588. Move to the previous element at the same level, when possible."
  21589. (interactive)
  21590. (cond ((bobp) (user-error "Cannot move further up"))
  21591. ((org-with-limited-levels (org-at-heading-p))
  21592. ;; At a headline, move to the previous one, if any, or stay
  21593. ;; here.
  21594. (let ((origin (point)))
  21595. (org-with-limited-levels (org-backward-heading-same-level 1))
  21596. ;; When current headline has no sibling above, move to its
  21597. ;; parent.
  21598. (when (= (point) origin)
  21599. (or (org-with-limited-levels (org-up-heading-safe))
  21600. (progn (goto-char origin)
  21601. (user-error "Cannot move further up"))))))
  21602. (t
  21603. (let* ((trail (org-element-at-point 'keep-trail))
  21604. (elem (car trail))
  21605. (prev-elem (nth 1 trail))
  21606. (beg (org-element-property :begin elem)))
  21607. (cond
  21608. ;; Move to beginning of current element if point isn't
  21609. ;; there already.
  21610. ((null beg) (message "No element at point"))
  21611. ((/= (point) beg) (goto-char beg))
  21612. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21613. ((org-before-first-heading-p) (goto-char (point-min)))
  21614. (t (org-back-to-heading)))))))
  21615. (defun org-up-element ()
  21616. "Move to upper element."
  21617. (interactive)
  21618. (if (org-with-limited-levels (org-at-heading-p))
  21619. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21620. (let* ((elem (org-element-at-point))
  21621. (parent (org-element-property :parent elem)))
  21622. (if parent (goto-char (org-element-property :begin parent))
  21623. (if (org-with-limited-levels (org-before-first-heading-p))
  21624. (user-error "No surrounding element")
  21625. (org-with-limited-levels (org-back-to-heading)))))))
  21626. (defvar org-element-greater-elements)
  21627. (defun org-down-element ()
  21628. "Move to inner element."
  21629. (interactive)
  21630. (let ((element (org-element-at-point)))
  21631. (cond
  21632. ((memq (org-element-type element) '(plain-list table))
  21633. (goto-char (org-element-property :contents-begin element))
  21634. (forward-char))
  21635. ((memq (org-element-type element) org-element-greater-elements)
  21636. ;; If contents are hidden, first disclose them.
  21637. (when (outline-invisible-p (line-end-position)) (org-cycle))
  21638. (goto-char (or (org-element-property :contents-begin element)
  21639. (user-error "No content for this element"))))
  21640. (t (user-error "No inner element")))))
  21641. (defun org-drag-element-backward ()
  21642. "Move backward element at point."
  21643. (interactive)
  21644. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21645. (let* ((trail (org-element-at-point 'keep-trail))
  21646. (elem (car trail))
  21647. (prev-elem (nth 1 trail)))
  21648. ;; Error out if no previous element or previous element is
  21649. ;; a parent of the current one.
  21650. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21651. (user-error "Cannot drag element backward")
  21652. (let ((pos (point)))
  21653. (org-element-swap-A-B prev-elem elem)
  21654. (goto-char (+ (org-element-property :begin prev-elem)
  21655. (- pos (org-element-property :begin elem)))))))))
  21656. (defun org-drag-element-forward ()
  21657. "Move forward element at point."
  21658. (interactive)
  21659. (let* ((pos (point))
  21660. (elem (org-element-at-point)))
  21661. (when (= (point-max) (org-element-property :end elem))
  21662. (user-error "Cannot drag element forward"))
  21663. (goto-char (org-element-property :end elem))
  21664. (let ((next-elem (org-element-at-point)))
  21665. (when (or (org-element-nested-p elem next-elem)
  21666. (and (eq (org-element-type next-elem) 'headline)
  21667. (not (eq (org-element-type elem) 'headline))))
  21668. (goto-char pos)
  21669. (user-error "Cannot drag element forward"))
  21670. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21671. ;; body's length (without final blanks) and by the length of
  21672. ;; blanks between ELEM and NEXT-ELEM.
  21673. (let ((size-next (- (save-excursion
  21674. (goto-char (org-element-property :end next-elem))
  21675. (skip-chars-backward " \r\t\n")
  21676. (forward-line)
  21677. ;; Small correction if buffer doesn't end
  21678. ;; with a newline character.
  21679. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21680. (org-element-property :begin next-elem)))
  21681. (size-blank (- (org-element-property :end elem)
  21682. (save-excursion
  21683. (goto-char (org-element-property :end elem))
  21684. (skip-chars-backward " \r\t\n")
  21685. (forward-line)
  21686. (point)))))
  21687. (org-element-swap-A-B elem next-elem)
  21688. (goto-char (+ pos size-next size-blank))))))
  21689. (defun org-drag-line-forward (arg)
  21690. "Drag the line at point ARG lines forward."
  21691. (interactive "p")
  21692. (dotimes (n (abs arg))
  21693. (let ((c (current-column)))
  21694. (if (< 0 arg)
  21695. (progn
  21696. (beginning-of-line 2)
  21697. (transpose-lines 1)
  21698. (beginning-of-line 0))
  21699. (transpose-lines 1)
  21700. (beginning-of-line -1))
  21701. (org-move-to-column c))))
  21702. (defun org-drag-line-backward (arg)
  21703. "Drag the line at point ARG lines backward."
  21704. (interactive "p")
  21705. (org-drag-line-forward (- arg)))
  21706. (defun org-mark-element ()
  21707. "Put point at beginning of this element, mark at end.
  21708. Interactively, if this command is repeated or (in Transient Mark
  21709. mode) if the mark is active, it marks the next element after the
  21710. ones already marked."
  21711. (interactive)
  21712. (let (deactivate-mark)
  21713. (if (and (org-called-interactively-p 'any)
  21714. (or (and (eq last-command this-command) (mark t))
  21715. (and transient-mark-mode mark-active)))
  21716. (set-mark
  21717. (save-excursion
  21718. (goto-char (mark))
  21719. (goto-char (org-element-property :end (org-element-at-point)))))
  21720. (let ((element (org-element-at-point)))
  21721. (end-of-line)
  21722. (push-mark (org-element-property :end element) t t)
  21723. (goto-char (org-element-property :begin element))))))
  21724. (defun org-narrow-to-element ()
  21725. "Narrow buffer to current element."
  21726. (interactive)
  21727. (let ((elem (org-element-at-point)))
  21728. (cond
  21729. ((eq (car elem) 'headline)
  21730. (narrow-to-region
  21731. (org-element-property :begin elem)
  21732. (org-element-property :end elem)))
  21733. ((memq (car elem) org-element-greater-elements)
  21734. (narrow-to-region
  21735. (org-element-property :contents-begin elem)
  21736. (org-element-property :contents-end elem)))
  21737. (t
  21738. (narrow-to-region
  21739. (org-element-property :begin elem)
  21740. (org-element-property :end elem))))))
  21741. (defun org-transpose-element ()
  21742. "Transpose current and previous elements, keeping blank lines between.
  21743. Point is moved after both elements."
  21744. (interactive)
  21745. (org-skip-whitespace)
  21746. (let ((end (org-element-property :end (org-element-at-point))))
  21747. (org-drag-element-backward)
  21748. (goto-char end)))
  21749. (defun org-unindent-buffer ()
  21750. "Un-indent the visible part of the buffer.
  21751. Relative indentation (between items, inside blocks, etc.) isn't
  21752. modified."
  21753. (interactive)
  21754. (unless (eq major-mode 'org-mode)
  21755. (user-error "Cannot un-indent a buffer not in Org mode"))
  21756. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21757. unindent-tree ; For byte-compiler.
  21758. (unindent-tree
  21759. (function
  21760. (lambda (contents)
  21761. (mapc
  21762. (lambda (element)
  21763. (if (memq (org-element-type element) '(headline section))
  21764. (funcall unindent-tree (org-element-contents element))
  21765. (save-excursion
  21766. (save-restriction
  21767. (narrow-to-region
  21768. (org-element-property :begin element)
  21769. (org-element-property :end element))
  21770. (org-do-remove-indentation)))))
  21771. (reverse contents))))))
  21772. (funcall unindent-tree (org-element-contents parse-tree))))
  21773. (defun org-show-subtree ()
  21774. "Show everything after this heading at deeper levels."
  21775. (interactive)
  21776. (outline-flag-region
  21777. (point)
  21778. (save-excursion
  21779. (org-end-of-subtree t t))
  21780. nil))
  21781. (defun org-show-entry ()
  21782. "Show the body directly following this heading.
  21783. Show the heading too, if it is currently invisible."
  21784. (interactive)
  21785. (save-excursion
  21786. (condition-case nil
  21787. (progn
  21788. (org-back-to-heading t)
  21789. (outline-flag-region
  21790. (max (point-min) (1- (point)))
  21791. (save-excursion
  21792. (if (re-search-forward
  21793. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21794. (match-beginning 1)
  21795. (point-max)))
  21796. nil)
  21797. (org-cycle-hide-drawers 'children))
  21798. (error nil))))
  21799. (defun org-make-options-regexp (kwds &optional extra)
  21800. "Make a regular expression for keyword lines."
  21801. (concat
  21802. "^#\\+\\("
  21803. (mapconcat 'regexp-quote kwds "\\|")
  21804. (if extra (concat "\\|" extra))
  21805. "\\):[ \t]*\\(.*\\)"))
  21806. ;; Make isearch reveal the necessary context
  21807. (defun org-isearch-end ()
  21808. "Reveal context after isearch exits."
  21809. (when isearch-success ; only if search was successful
  21810. (if (featurep 'xemacs)
  21811. ;; Under XEmacs, the hook is run in the correct place,
  21812. ;; we directly show the context.
  21813. (org-show-context 'isearch)
  21814. ;; In Emacs the hook runs *before* restoring the overlays.
  21815. ;; So we have to use a one-time post-command-hook to do this.
  21816. ;; (Emacs 22 has a special variable, see function `org-mode')
  21817. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21818. isearch-mode-end-hook-quit)
  21819. ;; Only when the isearch was not quitted.
  21820. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21821. 'append 'local)))
  21822. (org-fix-ellipsis-at-bol)))
  21823. (defun org-isearch-post-command ()
  21824. "Remove self from hook, and show context."
  21825. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21826. (org-show-context 'isearch))
  21827. ;;;; Integration with and fixes for other packages
  21828. ;;; Imenu support
  21829. (defvar org-imenu-markers nil
  21830. "All markers currently used by Imenu.")
  21831. (make-variable-buffer-local 'org-imenu-markers)
  21832. (defun org-imenu-new-marker (&optional pos)
  21833. "Return a new marker for use by Imenu, and remember the marker."
  21834. (let ((m (make-marker)))
  21835. (move-marker m (or pos (point)))
  21836. (push m org-imenu-markers)
  21837. m))
  21838. (defun org-imenu-get-tree ()
  21839. "Produce the index for Imenu."
  21840. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21841. (setq org-imenu-markers nil)
  21842. (let* ((n org-imenu-depth)
  21843. (re (concat "^" (org-get-limited-outline-regexp)))
  21844. (subs (make-vector (1+ n) nil))
  21845. (last-level 0)
  21846. m level head0 head)
  21847. (save-excursion
  21848. (save-restriction
  21849. (widen)
  21850. (goto-char (point-max))
  21851. (while (re-search-backward re nil t)
  21852. (setq level (org-reduced-level (funcall outline-level)))
  21853. (when (and (<= level n)
  21854. (looking-at org-complex-heading-regexp)
  21855. (setq head0 (org-match-string-no-properties 4)))
  21856. (setq head (org-link-display-format head0)
  21857. m (org-imenu-new-marker))
  21858. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21859. (if (>= level last-level)
  21860. (push (cons head m) (aref subs level))
  21861. (push (cons head (aref subs (1+ level))) (aref subs level))
  21862. (loop for i from (1+ level) to n do (aset subs i nil)))
  21863. (setq last-level level)))))
  21864. (aref subs 1)))
  21865. (eval-after-load "imenu"
  21866. '(progn
  21867. (add-hook 'imenu-after-jump-hook
  21868. (lambda ()
  21869. (if (derived-mode-p 'org-mode)
  21870. (org-show-context 'org-goto))))))
  21871. (defun org-link-display-format (link)
  21872. "Replace a link with its the description.
  21873. If there is no description, use the link target."
  21874. (save-match-data
  21875. (if (string-match org-bracket-link-analytic-regexp link)
  21876. (replace-match (if (match-end 5)
  21877. (match-string 5 link)
  21878. (concat (match-string 1 link)
  21879. (match-string 3 link)))
  21880. nil t link)
  21881. link)))
  21882. (defun org-toggle-link-display ()
  21883. "Toggle the literal or descriptive display of links."
  21884. (interactive)
  21885. (if org-descriptive-links
  21886. (progn (org-remove-from-invisibility-spec '(org-link))
  21887. (org-restart-font-lock)
  21888. (setq org-descriptive-links nil))
  21889. (progn (add-to-invisibility-spec '(org-link))
  21890. (org-restart-font-lock)
  21891. (setq org-descriptive-links t))))
  21892. ;; Speedbar support
  21893. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21894. "Overlay marking the agenda restriction line in speedbar.")
  21895. (overlay-put org-speedbar-restriction-lock-overlay
  21896. 'face 'org-agenda-restriction-lock)
  21897. (overlay-put org-speedbar-restriction-lock-overlay
  21898. 'help-echo "Agendas are currently limited to this item.")
  21899. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21900. (defun org-speedbar-set-agenda-restriction ()
  21901. "Restrict future agenda commands to the location at point in speedbar.
  21902. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21903. (interactive)
  21904. (require 'org-agenda)
  21905. (let (p m tp np dir txt)
  21906. (cond
  21907. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21908. 'org-imenu t))
  21909. (setq m (get-text-property p 'org-imenu-marker))
  21910. (with-current-buffer (marker-buffer m)
  21911. (goto-char m)
  21912. (org-agenda-set-restriction-lock 'subtree)))
  21913. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21914. 'speedbar-function 'speedbar-find-file))
  21915. (setq tp (previous-single-property-change
  21916. (1+ p) 'speedbar-function)
  21917. np (next-single-property-change
  21918. tp 'speedbar-function)
  21919. dir (speedbar-line-directory)
  21920. txt (buffer-substring-no-properties (or tp (point-min))
  21921. (or np (point-max))))
  21922. (with-current-buffer (find-file-noselect
  21923. (let ((default-directory dir))
  21924. (expand-file-name txt)))
  21925. (unless (derived-mode-p 'org-mode)
  21926. (user-error "Cannot restrict to non-Org-mode file"))
  21927. (org-agenda-set-restriction-lock 'file)))
  21928. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21929. (move-overlay org-speedbar-restriction-lock-overlay
  21930. (point-at-bol) (point-at-eol))
  21931. (setq current-prefix-arg nil)
  21932. (org-agenda-maybe-redo)))
  21933. (defvar speedbar-file-key-map)
  21934. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21935. (eval-after-load "speedbar"
  21936. '(progn
  21937. (speedbar-add-supported-extension ".org")
  21938. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21939. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21940. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21941. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21942. (add-hook 'speedbar-visiting-tag-hook
  21943. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21944. ;;; Fixes and Hacks for problems with other packages
  21945. ;; Make flyspell not check words in links, to not mess up our keymap
  21946. (defvar org-element-affiliated-keywords) ; From org-element.el
  21947. (defvar org-element-block-name-alist) ; From org-element.el
  21948. (defun org-mode-flyspell-verify ()
  21949. "Don't let flyspell put overlays at active buttons, or on
  21950. {todo,all-time,additional-option-like}-keywords."
  21951. (require 'org-element) ; For `org-element-affiliated-keywords'
  21952. (let ((pos (max (1- (point)) (point-min)))
  21953. (word (thing-at-point 'word)))
  21954. (and (not (get-text-property pos 'keymap))
  21955. (not (get-text-property pos 'org-no-flyspell))
  21956. (not (member word org-todo-keywords-1))
  21957. (not (member word org-all-time-keywords))
  21958. (not (member word org-options-keywords))
  21959. (not (member word (mapcar 'car org-startup-options)))
  21960. (not (member-ignore-case word org-element-affiliated-keywords))
  21961. (not (member-ignore-case word (org-get-export-keywords)))
  21962. (not (member-ignore-case
  21963. word (mapcar 'car org-element-block-name-alist)))
  21964. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21965. (not (org-in-src-block-p)))))
  21966. (defun org-remove-flyspell-overlays-in (beg end)
  21967. "Remove flyspell overlays in region."
  21968. (and (org-bound-and-true-p flyspell-mode)
  21969. (fboundp 'flyspell-delete-region-overlays)
  21970. (flyspell-delete-region-overlays beg end))
  21971. (add-text-properties beg end '(org-no-flyspell t)))
  21972. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21973. (eval-after-load "bookmark"
  21974. '(if (boundp 'bookmark-after-jump-hook)
  21975. ;; We can use the hook
  21976. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21977. ;; Hook not available, use advice
  21978. (defadvice bookmark-jump (after org-make-visible activate)
  21979. "Make the position visible."
  21980. (org-bookmark-jump-unhide))))
  21981. ;; Make sure saveplace shows the location if it was hidden
  21982. (eval-after-load "saveplace"
  21983. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21984. "Make the position visible."
  21985. (org-bookmark-jump-unhide)))
  21986. ;; Make sure ecb shows the location if it was hidden
  21987. (eval-after-load "ecb"
  21988. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21989. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21990. (if (derived-mode-p 'org-mode)
  21991. (org-show-context))))
  21992. (defun org-bookmark-jump-unhide ()
  21993. "Unhide the current position, to show the bookmark location."
  21994. (and (derived-mode-p 'org-mode)
  21995. (or (outline-invisible-p)
  21996. (save-excursion (goto-char (max (point-min) (1- (point))))
  21997. (outline-invisible-p)))
  21998. (org-show-context 'bookmark-jump)))
  21999. ;; Make session.el ignore our circular variable
  22000. (defvar session-globals-exclude)
  22001. (eval-after-load "session"
  22002. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22003. ;;;; Finish up
  22004. (provide 'org)
  22005. (run-hooks 'org-load-hook)
  22006. ;;; org.el ends here