org.el 909 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012
  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-bound 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. (version1 (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. version1)))
  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. For example, to activate speed commands when the point is on any
  906. star at the beginning of the headline, you can do this:
  907. (setq org-use-speed-commands
  908. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  909. :group 'org-structure
  910. :type '(choice
  911. (const :tag "Never" nil)
  912. (const :tag "At beginning of headline stars" t)
  913. (function)))
  914. (defcustom org-speed-commands-user nil
  915. "Alist of additional speed commands.
  916. This list will be checked before `org-speed-commands-default'
  917. when the variable `org-use-speed-commands' is non-nil
  918. and when the cursor is at the beginning of a headline.
  919. The car if each entry is a string with a single letter, which must
  920. be assigned to `self-insert-command' in the global map.
  921. The cdr is either a command to be called interactively, a function
  922. to be called, or a form to be evaluated.
  923. An entry that is just a list with a single string will be interpreted
  924. as a descriptive headline that will be added when listing the speed
  925. commands in the Help buffer using the `?' speed command."
  926. :group 'org-structure
  927. :type '(repeat :value ("k" . ignore)
  928. (choice :value ("k" . ignore)
  929. (list :tag "Descriptive Headline" (string :tag "Headline"))
  930. (cons :tag "Letter and Command"
  931. (string :tag "Command letter")
  932. (choice
  933. (function)
  934. (sexp))))))
  935. (defcustom org-bookmark-names-plist
  936. '(:last-capture "org-capture-last-stored"
  937. :last-refile "org-refile-last-stored"
  938. :last-capture-marker "org-capture-last-stored-marker")
  939. "Names for bookmarks automatically set by some Org commands.
  940. This can provide strings as names for a number of bookmakrs Org sets
  941. automatically. The following keys are currently implemented:
  942. :last-capture
  943. :last-capture-marker
  944. :last-refile
  945. When a key does not show up in the property list, the corresponding bookmark
  946. is not set."
  947. :group 'org-structure
  948. :type 'plist)
  949. (defgroup org-cycle nil
  950. "Options concerning visibility cycling in Org-mode."
  951. :tag "Org Cycle"
  952. :group 'org-structure)
  953. (defcustom org-cycle-skip-children-state-if-no-children t
  954. "Non-nil means skip CHILDREN state in entries that don't have any."
  955. :group 'org-cycle
  956. :type 'boolean)
  957. (defcustom org-cycle-max-level nil
  958. "Maximum level which should still be subject to visibility cycling.
  959. Levels higher than this will, for cycling, be treated as text, not a headline.
  960. When `org-odd-levels-only' is set, a value of N in this variable actually
  961. means 2N-1 stars as the limiting headline.
  962. When nil, cycle all levels.
  963. Note that the limiting level of cycling is also influenced by
  964. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  965. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  966. than its value."
  967. :group 'org-cycle
  968. :type '(choice
  969. (const :tag "No limit" nil)
  970. (integer :tag "Maximum level")))
  971. (defcustom org-hide-block-startup nil
  972. "Non-nil means entering Org-mode will fold all blocks.
  973. This can also be set in on a per-file basis with
  974. #+STARTUP: hideblocks
  975. #+STARTUP: showblocks"
  976. :group 'org-startup
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-global-at-bob nil
  980. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  981. This makes it possible to do global cycling without having to use S-TAB or
  982. \\[universal-argument] TAB. For this special case to work, the first line
  983. of the buffer must not be a headline -- it may be empty or some other text.
  984. When used in this way, `org-cycle-hook' is disabled temporarily to make
  985. sure the cursor stays at the beginning of the buffer. When this option is
  986. nil, don't do anything special at the beginning of the buffer."
  987. :group 'org-cycle
  988. :type 'boolean)
  989. (defcustom org-cycle-level-after-item/entry-creation t
  990. "Non-nil means cycle entry level or item indentation in new empty entries.
  991. When the cursor is at the end of an empty headline, i.e., with only stars
  992. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  993. and then all possible ancestor states, before returning to the original state.
  994. This makes data entry extremely fast: M-RET to create a new headline,
  995. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  996. When the cursor is at the end of an empty plain list item, one TAB will
  997. make it a subitem, two or more tabs will back up to make this an item
  998. higher up in the item hierarchy."
  999. :group 'org-cycle
  1000. :type 'boolean)
  1001. (defcustom org-cycle-emulate-tab t
  1002. "Where should `org-cycle' emulate TAB.
  1003. nil Never
  1004. white Only in completely white lines
  1005. whitestart Only at the beginning of lines, before the first non-white char
  1006. t Everywhere except in headlines
  1007. exc-hl-bol Everywhere except at the start of a headline
  1008. If TAB is used in a place where it does not emulate TAB, the current subtree
  1009. visibility is cycled."
  1010. :group 'org-cycle
  1011. :type '(choice (const :tag "Never" nil)
  1012. (const :tag "Only in completely white lines" white)
  1013. (const :tag "Before first char in a line" whitestart)
  1014. (const :tag "Everywhere except in headlines" t)
  1015. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1016. (defcustom org-cycle-separator-lines 2
  1017. "Number of empty lines needed to keep an empty line between collapsed trees.
  1018. If you leave an empty line between the end of a subtree and the following
  1019. headline, this empty line is hidden when the subtree is folded.
  1020. Org-mode will leave (exactly) one empty line visible if the number of
  1021. empty lines is equal or larger to the number given in this variable.
  1022. So the default 2 means at least 2 empty lines after the end of a subtree
  1023. are needed to produce free space between a collapsed subtree and the
  1024. following headline.
  1025. If the number is negative, and the number of empty lines is at least -N,
  1026. all empty lines are shown.
  1027. Special case: when 0, never leave empty lines in collapsed view."
  1028. :group 'org-cycle
  1029. :type 'integer)
  1030. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1031. (defcustom org-pre-cycle-hook nil
  1032. "Hook that is run before visibility cycling is happening.
  1033. The function(s) in this hook must accept a single argument which indicates
  1034. the new state that will be set right after running this hook. The
  1035. argument is a symbol. Before a global state change, it can have the values
  1036. `overview', `content', or `all'. Before a local state change, it can have
  1037. the values `folded', `children', or `subtree'."
  1038. :group 'org-cycle
  1039. :type 'hook)
  1040. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1041. org-cycle-hide-drawers
  1042. org-cycle-hide-inline-tasks
  1043. org-cycle-show-empty-lines
  1044. org-optimize-window-after-visibility-change)
  1045. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1046. The function(s) in this hook must accept a single argument which indicates
  1047. the new state that was set by the most recent `org-cycle' command. The
  1048. argument is a symbol. After a global state change, it can have the values
  1049. `overview', `contents', or `all'. After a local state change, it can have
  1050. the values `folded', `children', or `subtree'."
  1051. :group 'org-cycle
  1052. :type 'hook)
  1053. (defgroup org-edit-structure nil
  1054. "Options concerning structure editing in Org-mode."
  1055. :tag "Org Edit Structure"
  1056. :group 'org-structure)
  1057. (defcustom org-odd-levels-only nil
  1058. "Non-nil means skip even levels and only use odd levels for the outline.
  1059. This has the effect that two stars are being added/taken away in
  1060. promotion/demotion commands. It also influences how levels are
  1061. handled by the exporters.
  1062. Changing it requires restart of `font-lock-mode' to become effective
  1063. for fontification also in regions already fontified.
  1064. You may also set this on a per-file basis by adding one of the following
  1065. lines to the buffer:
  1066. #+STARTUP: odd
  1067. #+STARTUP: oddeven"
  1068. :group 'org-edit-structure
  1069. :group 'org-appearance
  1070. :type 'boolean)
  1071. (defcustom org-adapt-indentation t
  1072. "Non-nil means adapt indentation to outline node level.
  1073. When this variable is set, Org assumes that you write outlines by
  1074. indenting text in each node to align with the headline (after the stars).
  1075. The following issues are influenced by this variable:
  1076. - When this is set and the *entire* text in an entry is indented, the
  1077. indentation is increased by one space in a demotion command, and
  1078. decreased by one in a promotion command. If any line in the entry
  1079. body starts with text at column 0, indentation is not changed at all.
  1080. - Property drawers and planning information is inserted indented when
  1081. this variable s set. When nil, they will not be indented.
  1082. - TAB indents a line relative to context. The lines below a headline
  1083. will be indented when this variable is set.
  1084. Note that this is all about true indentation, by adding and removing
  1085. space characters. See also `org-indent.el' which does level-dependent
  1086. indentation in a virtual way, i.e. at display time in Emacs."
  1087. :group 'org-edit-structure
  1088. :type 'boolean)
  1089. (defcustom org-special-ctrl-a/e nil
  1090. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1091. When t, `C-a' will bring back the cursor to the beginning of the
  1092. headline text, i.e. after the stars and after a possible TODO
  1093. keyword. In an item, this will be the position after bullet and
  1094. check-box, if any. When the cursor is already at that position,
  1095. another `C-a' will bring it to the beginning of the line.
  1096. `C-e' will jump to the end of the headline, ignoring the presence
  1097. of tags in the headline. A second `C-e' will then jump to the
  1098. true end of the line, after any tags. This also means that, when
  1099. this variable is non-nil, `C-e' also will never jump beyond the
  1100. end of the heading of a folded section, i.e. not after the
  1101. ellipses.
  1102. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1103. normally, going to the true line boundary first. Only a directly
  1104. following, identical keypress will bring the cursor to the
  1105. special positions.
  1106. This may also be a cons cell where the behavior for `C-a' and
  1107. `C-e' is set separately."
  1108. :group 'org-edit-structure
  1109. :type '(choice
  1110. (const :tag "off" nil)
  1111. (const :tag "on: after stars/bullet and before tags first" t)
  1112. (const :tag "reversed: true line boundary first" reversed)
  1113. (cons :tag "Set C-a and C-e separately"
  1114. (choice :tag "Special C-a"
  1115. (const :tag "off" nil)
  1116. (const :tag "on: after stars/bullet first" t)
  1117. (const :tag "reversed: before stars/bullet first" reversed))
  1118. (choice :tag "Special C-e"
  1119. (const :tag "off" nil)
  1120. (const :tag "on: before tags first" t)
  1121. (const :tag "reversed: after tags first" reversed)))))
  1122. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1123. (defcustom org-special-ctrl-k nil
  1124. "Non-nil means `C-k' will behave specially in headlines.
  1125. When nil, `C-k' will call the default `kill-line' command.
  1126. When t, the following will happen while the cursor is in the headline:
  1127. - When the cursor is at the beginning of a headline, kill the entire
  1128. line and possible the folded subtree below the line.
  1129. - When in the middle of the headline text, kill the headline up to the tags.
  1130. - When after the headline text, kill the tags."
  1131. :group 'org-edit-structure
  1132. :type 'boolean)
  1133. (defcustom org-ctrl-k-protect-subtree nil
  1134. "Non-nil means, do not delete a hidden subtree with C-k.
  1135. When set to the symbol `error', simply throw an error when C-k is
  1136. used to kill (part-of) a headline that has hidden text behind it.
  1137. Any other non-nil value will result in a query to the user, if it is
  1138. OK to kill that hidden subtree. When nil, kill without remorse."
  1139. :group 'org-edit-structure
  1140. :version "24.1"
  1141. :type '(choice
  1142. (const :tag "Do not protect hidden subtrees" nil)
  1143. (const :tag "Protect hidden subtrees with a security query" t)
  1144. (const :tag "Never kill a hidden subtree with C-k" error)))
  1145. (defcustom org-special-ctrl-o t
  1146. "Non-nil means, make `C-o' insert a row in tables."
  1147. :group 'org-edit-structure
  1148. :type 'boolean)
  1149. (defcustom org-catch-invisible-edits nil
  1150. "Check if in invisible region before inserting or deleting a character.
  1151. Valid values are:
  1152. nil Do not check, so just do invisible edits.
  1153. error Throw an error and do nothing.
  1154. show Make point visible, and do the requested edit.
  1155. show-and-error Make point visible, then throw an error and abort the edit.
  1156. smart Make point visible, and do insertion/deletion if it is
  1157. adjacent to visible text and the change feels predictable.
  1158. Never delete a previously invisible character or add in the
  1159. middle or right after an invisible region. Basically, this
  1160. allows insertion and backward-delete right before ellipses.
  1161. FIXME: maybe in this case we should not even show?"
  1162. :group 'org-edit-structure
  1163. :version "24.1"
  1164. :type '(choice
  1165. (const :tag "Do not check" nil)
  1166. (const :tag "Throw error when trying to edit" error)
  1167. (const :tag "Unhide, but do not do the edit" show-and-error)
  1168. (const :tag "Show invisible part and do the edit" show)
  1169. (const :tag "Be smart and do the right thing" smart)))
  1170. (defcustom org-yank-folded-subtrees t
  1171. "Non-nil means when yanking subtrees, fold them.
  1172. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1173. it starts with a heading and all other headings in it are either children
  1174. or siblings, then fold all the subtrees. However, do this only if no
  1175. text after the yank would be swallowed into a folded tree by this action."
  1176. :group 'org-edit-structure
  1177. :type 'boolean)
  1178. (defcustom org-yank-adjusted-subtrees nil
  1179. "Non-nil means when yanking subtrees, adjust the level.
  1180. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1181. this function for details."
  1182. :group 'org-edit-structure
  1183. :type 'boolean)
  1184. (defcustom org-M-RET-may-split-line '((default . t))
  1185. "Non-nil means M-RET will split the line at the cursor position.
  1186. When nil, it will go to the end of the line before making a
  1187. new line.
  1188. You may also set this option in a different way for different
  1189. contexts. Valid contexts are:
  1190. headline when creating a new headline
  1191. item when creating a new item
  1192. table in a table field
  1193. default the value to be used for all contexts not explicitly
  1194. customized"
  1195. :group 'org-structure
  1196. :group 'org-table
  1197. :type '(choice
  1198. (const :tag "Always" t)
  1199. (const :tag "Never" nil)
  1200. (repeat :greedy t :tag "Individual contexts"
  1201. (cons
  1202. (choice :tag "Context"
  1203. (const headline)
  1204. (const item)
  1205. (const table)
  1206. (const default))
  1207. (boolean)))))
  1208. (defcustom org-insert-heading-respect-content nil
  1209. "Non-nil means insert new headings after the current subtree.
  1210. When nil, the new heading is created directly after the current line.
  1211. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1212. this variable on for the duration of the command."
  1213. :group 'org-structure
  1214. :type 'boolean)
  1215. (defcustom org-blank-before-new-entry '((heading . auto)
  1216. (plain-list-item . auto))
  1217. "Should `org-insert-heading' leave a blank line before new heading/item?
  1218. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1219. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1220. which case Org will look at the surrounding headings/items and try to
  1221. make an intelligent decision whether to insert a blank line or not.
  1222. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1223. the setting here is ignored and no empty line is inserted to avoid breaking
  1224. the list structure."
  1225. :group 'org-edit-structure
  1226. :type '(list
  1227. (cons (const heading)
  1228. (choice (const :tag "Never" nil)
  1229. (const :tag "Always" t)
  1230. (const :tag "Auto" auto)))
  1231. (cons (const plain-list-item)
  1232. (choice (const :tag "Never" nil)
  1233. (const :tag "Always" t)
  1234. (const :tag "Auto" auto)))))
  1235. (defcustom org-insert-heading-hook nil
  1236. "Hook being run after inserting a new heading."
  1237. :group 'org-edit-structure
  1238. :type 'hook)
  1239. (defcustom org-enable-fixed-width-editor t
  1240. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1241. This currently only means they are never auto-wrapped.
  1242. When nil, such lines will be treated like ordinary lines.
  1243. See also the QUOTE keyword."
  1244. :group 'org-edit-structure
  1245. :type 'boolean)
  1246. (defcustom org-goto-auto-isearch t
  1247. "Non-nil means typing characters in `org-goto' starts incremental search.
  1248. When nil, you can use these keybindings to navigate the buffer:
  1249. q Quit the org-goto interface
  1250. n Go to the next visible heading
  1251. p Go to the previous visible heading
  1252. f Go one heading forward on same level
  1253. b Go one heading backward on same level
  1254. u Go one heading up"
  1255. :group 'org-edit-structure
  1256. :type 'boolean)
  1257. (defgroup org-sparse-trees nil
  1258. "Options concerning sparse trees in Org-mode."
  1259. :tag "Org Sparse Trees"
  1260. :group 'org-structure)
  1261. (defcustom org-highlight-sparse-tree-matches t
  1262. "Non-nil means highlight all matches that define a sparse tree.
  1263. The highlights will automatically disappear the next time the buffer is
  1264. changed by an edit command."
  1265. :group 'org-sparse-trees
  1266. :type 'boolean)
  1267. (defcustom org-remove-highlights-with-change t
  1268. "Non-nil means any change to the buffer will remove temporary highlights.
  1269. Such highlights are created by `org-occur' and `org-clock-display'.
  1270. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1271. The highlights created by `org-preview-latex-fragment' always need
  1272. `C-c C-c' to be removed."
  1273. :group 'org-sparse-trees
  1274. :group 'org-time
  1275. :type 'boolean)
  1276. (defcustom org-occur-hook '(org-first-headline-recenter)
  1277. "Hook that is run after `org-occur' has constructed a sparse tree.
  1278. This can be used to recenter the window to show as much of the structure
  1279. as possible."
  1280. :group 'org-sparse-trees
  1281. :type 'hook)
  1282. (defgroup org-imenu-and-speedbar nil
  1283. "Options concerning imenu and speedbar in Org-mode."
  1284. :tag "Org Imenu and Speedbar"
  1285. :group 'org-structure)
  1286. (defcustom org-imenu-depth 2
  1287. "The maximum level for Imenu access to Org-mode headlines.
  1288. This also applied for speedbar access."
  1289. :group 'org-imenu-and-speedbar
  1290. :type 'integer)
  1291. (defgroup org-table nil
  1292. "Options concerning tables in Org-mode."
  1293. :tag "Org Table"
  1294. :group 'org)
  1295. (defcustom org-enable-table-editor 'optimized
  1296. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1297. When nil, such lines will be treated like ordinary lines.
  1298. When equal to the symbol `optimized', the table editor will be optimized to
  1299. do the following:
  1300. - Automatic overwrite mode in front of whitespace in table fields.
  1301. This makes the structure of the table stay in tact as long as the edited
  1302. field does not exceed the column width.
  1303. - Minimize the number of realigns. Normally, the table is aligned each time
  1304. TAB or RET are pressed to move to another field. With optimization this
  1305. happens only if changes to a field might have changed the column width.
  1306. Optimization requires replacing the functions `self-insert-command',
  1307. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1308. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1309. very good at guessing when a re-align will be necessary, but you can always
  1310. force one with \\[org-ctrl-c-ctrl-c].
  1311. If you would like to use the optimized version in Org-mode, but the
  1312. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1313. This variable can be used to turn on and off the table editor during a session,
  1314. but in order to toggle optimization, a restart is required.
  1315. See also the variable `org-table-auto-blank-field'."
  1316. :group 'org-table
  1317. :type '(choice
  1318. (const :tag "off" nil)
  1319. (const :tag "on" t)
  1320. (const :tag "on, optimized" optimized)))
  1321. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1322. (version<= emacs-version "24.1"))
  1323. "Non-nil means cluster self-insert commands for undo when possible.
  1324. If this is set, then, like in the Emacs command loop, 20 consecutive
  1325. characters will be undone together.
  1326. This is configurable, because there is some impact on typing performance."
  1327. :group 'org-table
  1328. :type 'boolean)
  1329. (defcustom org-table-tab-recognizes-table.el t
  1330. "Non-nil means TAB will automatically notice a table.el table.
  1331. When it sees such a table, it moves point into it and - if necessary -
  1332. calls `table-recognize-table'."
  1333. :group 'org-table-editing
  1334. :type 'boolean)
  1335. (defgroup org-link nil
  1336. "Options concerning links in Org-mode."
  1337. :tag "Org Link"
  1338. :group 'org)
  1339. (defvar org-link-abbrev-alist-local nil
  1340. "Buffer-local version of `org-link-abbrev-alist', which see.
  1341. The value of this is taken from the #+LINK lines.")
  1342. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1343. (defcustom org-link-abbrev-alist nil
  1344. "Alist of link abbreviations.
  1345. The car of each element is a string, to be replaced at the start of a link.
  1346. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1347. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1348. [[linkkey:tag][description]]
  1349. The 'linkkey' must be a word word, starting with a letter, followed
  1350. by letters, numbers, '-' or '_'.
  1351. If REPLACE is a string, the tag will simply be appended to create the link.
  1352. If the string contains \"%s\", the tag will be inserted there. If the string
  1353. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1354. at that point (see the function `url-hexify-string'). If the string contains
  1355. the specifier \"%(my-function)\", then the custom function `my-function' will
  1356. be invoked: this function takes the tag as its only argument and must return
  1357. a string.
  1358. REPLACE may also be a function that will be called with the tag as the
  1359. only argument to create the link, which should be returned as a string.
  1360. See the manual for examples."
  1361. :group 'org-link
  1362. :type '(repeat
  1363. (cons
  1364. (string :tag "Protocol")
  1365. (choice
  1366. (string :tag "Format")
  1367. (function)))))
  1368. (defcustom org-descriptive-links t
  1369. "Non-nil means Org will display descriptive links.
  1370. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1371. \"Org Website\", hiding the link itself and just displaying its
  1372. description. When set to `nil', Org will display the full links
  1373. literally.
  1374. You can interactively set the value of this variable by calling
  1375. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1376. :group 'org-link
  1377. :type 'boolean)
  1378. (defcustom org-link-file-path-type 'adaptive
  1379. "How the path name in file links should be stored.
  1380. Valid values are:
  1381. relative Relative to the current directory, i.e. the directory of the file
  1382. into which the link is being inserted.
  1383. absolute Absolute path, if possible with ~ for home directory.
  1384. noabbrev Absolute path, no abbreviation of home directory.
  1385. adaptive Use relative path for files in the current directory and sub-
  1386. directories of it. For other files, use an absolute path."
  1387. :group 'org-link
  1388. :type '(choice
  1389. (const relative)
  1390. (const absolute)
  1391. (const noabbrev)
  1392. (const adaptive)))
  1393. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1394. "Types of links that should be activated in Org-mode files.
  1395. This is a list of symbols, each leading to the activation of a certain link
  1396. type. In principle, it does not hurt to turn on most link types - there may
  1397. be a small gain when turning off unused link types. The types are:
  1398. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1399. angle Links in angular brackets that may contain whitespace like
  1400. <bbdb:Carsten Dominik>.
  1401. plain Plain links in normal text, no whitespace, like http://google.com.
  1402. radio Text that is matched by a radio target, see manual for details.
  1403. tag Tag settings in a headline (link to tag search).
  1404. date Time stamps (link to calendar).
  1405. footnote Footnote labels.
  1406. Changing this variable requires a restart of Emacs to become effective."
  1407. :group 'org-link
  1408. :type '(set :greedy t
  1409. (const :tag "Double bracket links" bracket)
  1410. (const :tag "Angular bracket links" angle)
  1411. (const :tag "Plain text links" plain)
  1412. (const :tag "Radio target matches" radio)
  1413. (const :tag "Tags" tag)
  1414. (const :tag "Timestamps" date)
  1415. (const :tag "Footnotes" footnote)))
  1416. (defcustom org-make-link-description-function nil
  1417. "Function to use for generating link descriptions from links.
  1418. When nil, the link location will be used. This function must take
  1419. two parameters: the first one is the link, the second one is the
  1420. description generated by `org-insert-link'. The function should
  1421. return the description to use."
  1422. :group 'org-link
  1423. :type '(choice (const nil) (function)))
  1424. (defgroup org-link-store nil
  1425. "Options concerning storing links in Org-mode."
  1426. :tag "Org Store Link"
  1427. :group 'org-link)
  1428. (defcustom org-url-hexify-p t
  1429. "When non-nil, hexify URL when creating a link."
  1430. :type 'boolean
  1431. :version "24.3"
  1432. :group 'org-link-store)
  1433. (defcustom org-email-link-description-format "Email %c: %.30s"
  1434. "Format of the description part of a link to an email or usenet message.
  1435. The following %-escapes will be replaced by corresponding information:
  1436. %F full \"From\" field
  1437. %f name, taken from \"From\" field, address if no name
  1438. %T full \"To\" field
  1439. %t first name in \"To\" field, address if no name
  1440. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1441. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1442. %s subject
  1443. %d date
  1444. %m message-id.
  1445. You may use normal field width specification between the % and the letter.
  1446. This is for example useful to limit the length of the subject.
  1447. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1448. :group 'org-link-store
  1449. :type 'string)
  1450. (defcustom org-from-is-user-regexp
  1451. (let (r1 r2)
  1452. (when (and user-mail-address (not (string= user-mail-address "")))
  1453. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1454. (when (and user-full-name (not (string= user-full-name "")))
  1455. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1456. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1457. "Regexp matched against the \"From:\" header of an email or usenet message.
  1458. It should match if the message is from the user him/herself."
  1459. :group 'org-link-store
  1460. :type 'regexp)
  1461. (defcustom org-context-in-file-links t
  1462. "Non-nil means file links from `org-store-link' contain context.
  1463. A search string will be added to the file name with :: as separator and
  1464. used to find the context when the link is activated by the command
  1465. `org-open-at-point'. When this option is t, the entire active region
  1466. will be placed in the search string of the file link. If set to a
  1467. positive integer, only the first n lines of context will be stored.
  1468. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1469. negates this setting for the duration of the command."
  1470. :group 'org-link-store
  1471. :type '(choice boolean integer))
  1472. (defcustom org-keep-stored-link-after-insertion nil
  1473. "Non-nil means keep link in list for entire session.
  1474. The command `org-store-link' adds a link pointing to the current
  1475. location to an internal list. These links accumulate during a session.
  1476. The command `org-insert-link' can be used to insert links into any
  1477. Org-mode file (offering completion for all stored links). When this
  1478. option is nil, every link which has been inserted once using \\[org-insert-link]
  1479. will be removed from the list, to make completing the unused links
  1480. more efficient."
  1481. :group 'org-link-store
  1482. :type 'boolean)
  1483. (defgroup org-link-follow nil
  1484. "Options concerning following links in Org-mode."
  1485. :tag "Org Follow Link"
  1486. :group 'org-link)
  1487. (defcustom org-link-translation-function nil
  1488. "Function to translate links with different syntax to Org syntax.
  1489. This can be used to translate links created for example by the Planner
  1490. or emacs-wiki packages to Org syntax.
  1491. The function must accept two parameters, a TYPE containing the link
  1492. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1493. which is everything after the link protocol. It should return a cons
  1494. with possibly modified values of type and path.
  1495. Org contains a function for this, so if you set this variable to
  1496. `org-translate-link-from-planner', you should be able follow many
  1497. links created by planner."
  1498. :group 'org-link-follow
  1499. :type '(choice (const nil) (function)))
  1500. (defcustom org-follow-link-hook nil
  1501. "Hook that is run after a link has been followed."
  1502. :group 'org-link-follow
  1503. :type 'hook)
  1504. (defcustom org-tab-follows-link nil
  1505. "Non-nil means on links TAB will follow the link.
  1506. Needs to be set before org.el is loaded.
  1507. This really should not be used, it does not make sense, and the
  1508. implementation is bad."
  1509. :group 'org-link-follow
  1510. :type 'boolean)
  1511. (defcustom org-return-follows-link nil
  1512. "Non-nil means on links RET will follow the link.
  1513. In tables, the special behavior of RET has precedence."
  1514. :group 'org-link-follow
  1515. :type 'boolean)
  1516. (defcustom org-mouse-1-follows-link
  1517. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1518. "Non-nil means mouse-1 on a link will follow the link.
  1519. A longer mouse click will still set point. Does not work on XEmacs.
  1520. Needs to be set before org.el is loaded."
  1521. :group 'org-link-follow
  1522. :type 'boolean)
  1523. (defcustom org-mark-ring-length 4
  1524. "Number of different positions to be recorded in the ring.
  1525. Changing this requires a restart of Emacs to work correctly."
  1526. :group 'org-link-follow
  1527. :type 'integer)
  1528. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1529. "Non-nil means internal links in Org files must exactly match a headline.
  1530. When nil, the link search tries to match a phrase with all words
  1531. in the search text."
  1532. :group 'org-link-follow
  1533. :version "24.1"
  1534. :type '(choice
  1535. (const :tag "Use fuzzy text search" nil)
  1536. (const :tag "Match only exact headline" t)
  1537. (const :tag "Match exact headline or query to create it"
  1538. query-to-create)))
  1539. (defcustom org-link-frame-setup
  1540. '((vm . vm-visit-folder-other-frame)
  1541. (vm-imap . vm-visit-imap-folder-other-frame)
  1542. (gnus . org-gnus-no-new-news)
  1543. (file . find-file-other-window)
  1544. (wl . wl-other-frame))
  1545. "Setup the frame configuration for following links.
  1546. When following a link with Emacs, it may often be useful to display
  1547. this link in another window or frame. This variable can be used to
  1548. set this up for the different types of links.
  1549. For VM, use any of
  1550. `vm-visit-folder'
  1551. `vm-visit-folder-other-window'
  1552. `vm-visit-folder-other-frame'
  1553. For Gnus, use any of
  1554. `gnus'
  1555. `gnus-other-frame'
  1556. `org-gnus-no-new-news'
  1557. For FILE, use any of
  1558. `find-file'
  1559. `find-file-other-window'
  1560. `find-file-other-frame'
  1561. For Wanderlust use any of
  1562. `wl'
  1563. `wl-other-frame'
  1564. For the calendar, use the variable `calendar-setup'.
  1565. For BBDB, it is currently only possible to display the matches in
  1566. another window."
  1567. :group 'org-link-follow
  1568. :type '(list
  1569. (cons (const vm)
  1570. (choice
  1571. (const vm-visit-folder)
  1572. (const vm-visit-folder-other-window)
  1573. (const vm-visit-folder-other-frame)))
  1574. (cons (const vm-imap)
  1575. (choice
  1576. (const vm-visit-imap-folder)
  1577. (const vm-visit-imap-folder-other-window)
  1578. (const vm-visit-imap-folder-other-frame)))
  1579. (cons (const gnus)
  1580. (choice
  1581. (const gnus)
  1582. (const gnus-other-frame)
  1583. (const org-gnus-no-new-news)))
  1584. (cons (const file)
  1585. (choice
  1586. (const find-file)
  1587. (const find-file-other-window)
  1588. (const find-file-other-frame)))
  1589. (cons (const wl)
  1590. (choice
  1591. (const wl)
  1592. (const wl-other-frame)))))
  1593. (defcustom org-display-internal-link-with-indirect-buffer nil
  1594. "Non-nil means use indirect buffer to display infile links.
  1595. Activating internal links (from one location in a file to another location
  1596. in the same file) normally just jumps to the location. When the link is
  1597. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1598. is displayed in
  1599. another window. When this option is set, the other window actually displays
  1600. an indirect buffer clone of the current buffer, to avoid any visibility
  1601. changes to the current buffer."
  1602. :group 'org-link-follow
  1603. :type 'boolean)
  1604. (defcustom org-open-non-existing-files nil
  1605. "Non-nil means `org-open-file' will open non-existing files.
  1606. When nil, an error will be generated.
  1607. This variable applies only to external applications because they
  1608. might choke on non-existing files. If the link is to a file that
  1609. will be opened in Emacs, the variable is ignored."
  1610. :group 'org-link-follow
  1611. :type 'boolean)
  1612. (defcustom org-open-directory-means-index-dot-org nil
  1613. "Non-nil means a link to a directory really means to index.org.
  1614. When nil, following a directory link will run dired or open a finder/explorer
  1615. window on that directory."
  1616. :group 'org-link-follow
  1617. :type 'boolean)
  1618. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1619. "Function and arguments to call for following mailto links.
  1620. This is a list with the first element being a Lisp function, and the
  1621. remaining elements being arguments to the function. In string arguments,
  1622. %a will be replaced by the address, and %s will be replaced by the subject
  1623. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1624. :group 'org-link-follow
  1625. :type '(choice
  1626. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1627. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1628. (const :tag "message-mail" (message-mail "%a" "%s"))
  1629. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1630. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1631. "Non-nil means ask for confirmation before executing shell links.
  1632. Shell links can be dangerous: just think about a link
  1633. [[shell:rm -rf ~/*][Google Search]]
  1634. This link would show up in your Org-mode document as \"Google Search\",
  1635. but really it would remove your entire home directory.
  1636. Therefore we advise against setting this variable to nil.
  1637. Just change it to `y-or-n-p' if you want to confirm with a
  1638. single keystroke rather than having to type \"yes\"."
  1639. :group 'org-link-follow
  1640. :type '(choice
  1641. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1642. (const :tag "with y-or-n (faster)" y-or-n-p)
  1643. (const :tag "no confirmation (dangerous)" nil)))
  1644. (put 'org-confirm-shell-link-function
  1645. 'safe-local-variable
  1646. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1647. (defcustom org-confirm-shell-link-not-regexp ""
  1648. "A regexp to skip confirmation for shell links."
  1649. :group 'org-link-follow
  1650. :version "24.1"
  1651. :type 'regexp)
  1652. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1653. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1654. Elisp links can be dangerous: just think about a link
  1655. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1656. This link would show up in your Org-mode document as \"Google Search\",
  1657. but really it would remove your entire home directory.
  1658. Therefore we advise against setting this variable to nil.
  1659. Just change it to `y-or-n-p' if you want to confirm with a
  1660. single keystroke rather than having to type \"yes\"."
  1661. :group 'org-link-follow
  1662. :type '(choice
  1663. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1664. (const :tag "with y-or-n (faster)" y-or-n-p)
  1665. (const :tag "no confirmation (dangerous)" nil)))
  1666. (put 'org-confirm-shell-link-function
  1667. 'safe-local-variable
  1668. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1669. (defcustom org-confirm-elisp-link-not-regexp ""
  1670. "A regexp to skip confirmation for Elisp links."
  1671. :group 'org-link-follow
  1672. :version "24.1"
  1673. :type 'regexp)
  1674. (defconst org-file-apps-defaults-gnu
  1675. '((remote . emacs)
  1676. (system . mailcap)
  1677. (t . mailcap))
  1678. "Default file applications on a UNIX or GNU/Linux system.
  1679. See `org-file-apps'.")
  1680. (defconst org-file-apps-defaults-macosx
  1681. '((remote . emacs)
  1682. (t . "open %s")
  1683. (system . "open %s")
  1684. ("ps.gz" . "gv %s")
  1685. ("eps.gz" . "gv %s")
  1686. ("dvi" . "xdvi %s")
  1687. ("fig" . "xfig %s"))
  1688. "Default file applications on a MacOS X system.
  1689. The system \"open\" is known as a default, but we use X11 applications
  1690. for some files for which the OS does not have a good default.
  1691. See `org-file-apps'.")
  1692. (defconst org-file-apps-defaults-windowsnt
  1693. (list
  1694. '(remote . emacs)
  1695. (cons t
  1696. (list (if (featurep 'xemacs)
  1697. 'mswindows-shell-execute
  1698. 'w32-shell-execute)
  1699. "open" 'file))
  1700. (cons 'system
  1701. (list (if (featurep 'xemacs)
  1702. 'mswindows-shell-execute
  1703. 'w32-shell-execute)
  1704. "open" 'file)))
  1705. "Default file applications on a Windows NT system.
  1706. The system \"open\" is used for most files.
  1707. See `org-file-apps'.")
  1708. (defcustom org-file-apps
  1709. '((auto-mode . emacs)
  1710. ("\\.mm\\'" . default)
  1711. ("\\.x?html?\\'" . default)
  1712. ("\\.pdf\\'" . default))
  1713. "External applications for opening `file:path' items in a document.
  1714. Org-mode uses system defaults for different file types, but
  1715. you can use this variable to set the application for a given file
  1716. extension. The entries in this list are cons cells where the car identifies
  1717. files and the cdr the corresponding command. Possible values for the
  1718. file identifier are
  1719. \"string\" A string as a file identifier can be interpreted in different
  1720. ways, depending on its contents:
  1721. - Alphanumeric characters only:
  1722. Match links with this file extension.
  1723. Example: (\"pdf\" . \"evince %s\")
  1724. to open PDFs with evince.
  1725. - Regular expression: Match links where the
  1726. filename matches the regexp. If you want to
  1727. use groups here, use shy groups.
  1728. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1729. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1730. to open *.html and *.xhtml with firefox.
  1731. - Regular expression which contains (non-shy) groups:
  1732. Match links where the whole link, including \"::\", and
  1733. anything after that, matches the regexp.
  1734. In a custom command string, %1, %2, etc. are replaced with
  1735. the parts of the link that were matched by the groups.
  1736. For backwards compatibility, if a command string is given
  1737. that does not use any of the group matches, this case is
  1738. handled identically to the second one (i.e. match against
  1739. file name only).
  1740. In a custom lisp form, you can access the group matches with
  1741. (match-string n link).
  1742. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1743. to open [[file:document.pdf::5]] with evince at page 5.
  1744. `directory' Matches a directory
  1745. `remote' Matches a remote file, accessible through tramp or efs.
  1746. Remote files most likely should be visited through Emacs
  1747. because external applications cannot handle such paths.
  1748. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1749. so all files Emacs knows how to handle. Using this with
  1750. command `emacs' will open most files in Emacs. Beware that this
  1751. will also open html files inside Emacs, unless you add
  1752. (\"html\" . default) to the list as well.
  1753. t Default for files not matched by any of the other options.
  1754. `system' The system command to open files, like `open' on Windows
  1755. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1756. that will be selected if you call `C-c C-o' with a double
  1757. \\[universal-argument] \\[universal-argument] prefix.
  1758. Possible values for the command are:
  1759. `emacs' The file will be visited by the current Emacs process.
  1760. `default' Use the default application for this file type, which is the
  1761. association for t in the list, most likely in the system-specific
  1762. part.
  1763. This can be used to overrule an unwanted setting in the
  1764. system-specific variable.
  1765. `system' Use the system command for opening files, like \"open\".
  1766. This command is specified by the entry whose car is `system'.
  1767. Most likely, the system-specific version of this variable
  1768. does define this command, but you can overrule/replace it
  1769. here.
  1770. string A command to be executed by a shell; %s will be replaced
  1771. by the path to the file.
  1772. sexp A Lisp form which will be evaluated. The file path will
  1773. be available in the Lisp variable `file'.
  1774. For more examples, see the system specific constants
  1775. `org-file-apps-defaults-macosx'
  1776. `org-file-apps-defaults-windowsnt'
  1777. `org-file-apps-defaults-gnu'."
  1778. :group 'org-link-follow
  1779. :type '(repeat
  1780. (cons (choice :value ""
  1781. (string :tag "Extension")
  1782. (const :tag "System command to open files" system)
  1783. (const :tag "Default for unrecognized files" t)
  1784. (const :tag "Remote file" remote)
  1785. (const :tag "Links to a directory" directory)
  1786. (const :tag "Any files that have Emacs modes"
  1787. auto-mode))
  1788. (choice :value ""
  1789. (const :tag "Visit with Emacs" emacs)
  1790. (const :tag "Use default" default)
  1791. (const :tag "Use the system command" system)
  1792. (string :tag "Command")
  1793. (sexp :tag "Lisp form")))))
  1794. (defcustom org-doi-server-url "http://dx.doi.org/"
  1795. "The URL of the DOI server."
  1796. :type 'string
  1797. :version "24.3"
  1798. :group 'org-link-follow)
  1799. (defgroup org-refile nil
  1800. "Options concerning refiling entries in Org-mode."
  1801. :tag "Org Refile"
  1802. :group 'org)
  1803. (defcustom org-directory "~/org"
  1804. "Directory with org files.
  1805. This is just a default location to look for Org files. There is no need
  1806. at all to put your files into this directory. It is only used in the
  1807. following situations:
  1808. 1. When a capture template specifies a target file that is not an
  1809. absolute path. The path will then be interpreted relative to
  1810. `org-directory'
  1811. 2. When a capture note is filed away in an interactive way (when exiting the
  1812. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1813. with `org-directory' as the default path."
  1814. :group 'org-refile
  1815. :group 'org-capture
  1816. :type 'directory)
  1817. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1818. "Default target for storing notes.
  1819. Used as a fall back file for org-capture.el, for templates that
  1820. do not specify a target file."
  1821. :group 'org-refile
  1822. :group 'org-capture
  1823. :type '(choice
  1824. (const :tag "Default from remember-data-file" nil)
  1825. file))
  1826. (defcustom org-goto-interface 'outline
  1827. "The default interface to be used for `org-goto'.
  1828. Allowed values are:
  1829. outline The interface shows an outline of the relevant file
  1830. and the correct heading is found by moving through
  1831. the outline or by searching with incremental search.
  1832. outline-path-completion Headlines in the current buffer are offered via
  1833. completion. This is the interface also used by
  1834. the refile command."
  1835. :group 'org-refile
  1836. :type '(choice
  1837. (const :tag "Outline" outline)
  1838. (const :tag "Outline-path-completion" outline-path-completion)))
  1839. (defcustom org-goto-max-level 5
  1840. "Maximum target level when running `org-goto' with refile interface."
  1841. :group 'org-refile
  1842. :type 'integer)
  1843. (defcustom org-reverse-note-order nil
  1844. "Non-nil means store new notes at the beginning of a file or entry.
  1845. When nil, new notes will be filed to the end of a file or entry.
  1846. This can also be a list with cons cells of regular expressions that
  1847. are matched against file names, and values."
  1848. :group 'org-capture
  1849. :group 'org-refile
  1850. :type '(choice
  1851. (const :tag "Reverse always" t)
  1852. (const :tag "Reverse never" nil)
  1853. (repeat :tag "By file name regexp"
  1854. (cons regexp boolean))))
  1855. (defcustom org-log-refile nil
  1856. "Information to record when a task is refiled.
  1857. Possible values are:
  1858. nil Don't add anything
  1859. time Add a time stamp to the task
  1860. note Prompt for a note and add it with template `org-log-note-headings'
  1861. This option can also be set with on a per-file-basis with
  1862. #+STARTUP: nologrefile
  1863. #+STARTUP: logrefile
  1864. #+STARTUP: lognoterefile
  1865. You can have local logging settings for a subtree by setting the LOGGING
  1866. property to one or more of these keywords.
  1867. When bulk-refiling from the agenda, the value `note' is forbidden and
  1868. will temporarily be changed to `time'."
  1869. :group 'org-refile
  1870. :group 'org-progress
  1871. :version "24.1"
  1872. :type '(choice
  1873. (const :tag "No logging" nil)
  1874. (const :tag "Record timestamp" time)
  1875. (const :tag "Record timestamp with note." note)))
  1876. (defcustom org-refile-targets nil
  1877. "Targets for refiling entries with \\[org-refile].
  1878. This is a list of cons cells. Each cell contains:
  1879. - a specification of the files to be considered, either a list of files,
  1880. or a symbol whose function or variable value will be used to retrieve
  1881. a file name or a list of file names. If you use `org-agenda-files' for
  1882. that, all agenda files will be scanned for targets. Nil means consider
  1883. headings in the current buffer.
  1884. - A specification of how to find candidate refile targets. This may be
  1885. any of:
  1886. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1887. This tag has to be present in all target headlines, inheritance will
  1888. not be considered.
  1889. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1890. todo keyword.
  1891. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1892. headlines that are refiling targets.
  1893. - a cons cell (:level . N). Any headline of level N is considered a target.
  1894. Note that, when `org-odd-levels-only' is set, level corresponds to
  1895. order in hierarchy, not to the number of stars.
  1896. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1897. Note that, when `org-odd-levels-only' is set, level corresponds to
  1898. order in hierarchy, not to the number of stars.
  1899. Each element of this list generates a set of possible targets.
  1900. The union of these sets is presented (with completion) to
  1901. the user by `org-refile'.
  1902. You can set the variable `org-refile-target-verify-function' to a function
  1903. to verify each headline found by the simple criteria above.
  1904. When this variable is nil, all top-level headlines in the current buffer
  1905. are used, equivalent to the value `((nil . (:level . 1))'."
  1906. :group 'org-refile
  1907. :type '(repeat
  1908. (cons
  1909. (choice :value org-agenda-files
  1910. (const :tag "All agenda files" org-agenda-files)
  1911. (const :tag "Current buffer" nil)
  1912. (function) (variable) (file))
  1913. (choice :tag "Identify target headline by"
  1914. (cons :tag "Specific tag" (const :value :tag) (string))
  1915. (cons :tag "TODO keyword" (const :value :todo) (string))
  1916. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1917. (cons :tag "Level number" (const :value :level) (integer))
  1918. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1919. (defcustom org-refile-target-verify-function nil
  1920. "Function to verify if the headline at point should be a refile target.
  1921. The function will be called without arguments, with point at the
  1922. beginning of the headline. It should return t and leave point
  1923. where it is if the headline is a valid target for refiling.
  1924. If the target should not be selected, the function must return nil.
  1925. In addition to this, it may move point to a place from where the search
  1926. should be continued. For example, the function may decide that the entire
  1927. subtree of the current entry should be excluded and move point to the end
  1928. of the subtree."
  1929. :group 'org-refile
  1930. :type '(choice
  1931. (const nil)
  1932. (function)))
  1933. (defcustom org-refile-use-cache nil
  1934. "Non-nil means cache refile targets to speed up the process.
  1935. The cache for a particular file will be updated automatically when
  1936. the buffer has been killed, or when any of the marker used for flagging
  1937. refile targets no longer points at a live buffer.
  1938. If you have added new entries to a buffer that might themselves be targets,
  1939. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1940. find that easier, `C-u C-u C-u C-c C-w'."
  1941. :group 'org-refile
  1942. :version "24.1"
  1943. :type 'boolean)
  1944. (defcustom org-refile-use-outline-path nil
  1945. "Non-nil means provide refile targets as paths.
  1946. So a level 3 headline will be available as level1/level2/level3.
  1947. When the value is `file', also include the file name (without directory)
  1948. into the path. In this case, you can also stop the completion after
  1949. the file name, to get entries inserted as top level in the file.
  1950. When `full-file-path', include the full file path."
  1951. :group 'org-refile
  1952. :type '(choice
  1953. (const :tag "Not" nil)
  1954. (const :tag "Yes" t)
  1955. (const :tag "Start with file name" file)
  1956. (const :tag "Start with full file path" full-file-path)))
  1957. (defcustom org-outline-path-complete-in-steps t
  1958. "Non-nil means complete the outline path in hierarchical steps.
  1959. When Org-mode uses the refile interface to select an outline path
  1960. \(see variable `org-refile-use-outline-path'), the completion of
  1961. the path can be done is a single go, or if can be done in steps down
  1962. the headline hierarchy. Going in steps is probably the best if you
  1963. do not use a special completion package like `ido' or `icicles'.
  1964. However, when using these packages, going in one step can be very
  1965. fast, while still showing the whole path to the entry."
  1966. :group 'org-refile
  1967. :type 'boolean)
  1968. (defcustom org-refile-allow-creating-parent-nodes nil
  1969. "Non-nil means allow to create new nodes as refile targets.
  1970. New nodes are then created by adding \"/new node name\" to the completion
  1971. of an existing node. When the value of this variable is `confirm',
  1972. new node creation must be confirmed by the user (recommended)
  1973. When nil, the completion must match an existing entry.
  1974. Note that, if the new heading is not seen by the criteria
  1975. listed in `org-refile-targets', multiple instances of the same
  1976. heading would be created by trying again to file under the new
  1977. heading."
  1978. :group 'org-refile
  1979. :type '(choice
  1980. (const :tag "Never" nil)
  1981. (const :tag "Always" t)
  1982. (const :tag "Prompt for confirmation" confirm)))
  1983. (defcustom org-refile-active-region-within-subtree nil
  1984. "Non-nil means also refile active region within a subtree.
  1985. By default `org-refile' doesn't allow refiling regions if they
  1986. don't contain a set of subtrees, but it might be convenient to
  1987. do so sometimes: in that case, the first line of the region is
  1988. converted to a headline before refiling."
  1989. :group 'org-refile
  1990. :version "24.1"
  1991. :type 'boolean)
  1992. (defgroup org-todo nil
  1993. "Options concerning TODO items in Org-mode."
  1994. :tag "Org TODO"
  1995. :group 'org)
  1996. (defgroup org-progress nil
  1997. "Options concerning Progress logging in Org-mode."
  1998. :tag "Org Progress"
  1999. :group 'org-time)
  2000. (defvar org-todo-interpretation-widgets
  2001. '((:tag "Sequence (cycling hits every state)" sequence)
  2002. (:tag "Type (cycling directly to DONE)" type))
  2003. "The available interpretation symbols for customizing `org-todo-keywords'.
  2004. Interested libraries should add to this list.")
  2005. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2006. "List of TODO entry keyword sequences and their interpretation.
  2007. \\<org-mode-map>This is a list of sequences.
  2008. Each sequence starts with a symbol, either `sequence' or `type',
  2009. indicating if the keywords should be interpreted as a sequence of
  2010. action steps, or as different types of TODO items. The first
  2011. keywords are states requiring action - these states will select a headline
  2012. for inclusion into the global TODO list Org-mode produces. If one of
  2013. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2014. signify that no further action is necessary. If \"|\" is not found,
  2015. the last keyword is treated as the only DONE state of the sequence.
  2016. The command \\[org-todo] cycles an entry through these states, and one
  2017. additional state where no keyword is present. For details about this
  2018. cycling, see the manual.
  2019. TODO keywords and interpretation can also be set on a per-file basis with
  2020. the special #+SEQ_TODO and #+TYP_TODO lines.
  2021. Each keyword can optionally specify a character for fast state selection
  2022. \(in combination with the variable `org-use-fast-todo-selection')
  2023. and specifiers for state change logging, using the same syntax that
  2024. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2025. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2026. indicates to record a time stamp each time this state is selected.
  2027. Each keyword may also specify if a timestamp or a note should be
  2028. recorded when entering or leaving the state, by adding additional
  2029. characters in the parenthesis after the keyword. This looks like this:
  2030. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2031. record only the time of the state change. With X and Y being either
  2032. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2033. Y when leaving the state if and only if the *target* state does not
  2034. define X. You may omit any of the fast-selection key or X or /Y,
  2035. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2036. For backward compatibility, this variable may also be just a list
  2037. of keywords. In this case the interpretation (sequence or type) will be
  2038. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2039. :group 'org-todo
  2040. :group 'org-keywords
  2041. :type '(choice
  2042. (repeat :tag "Old syntax, just keywords"
  2043. (string :tag "Keyword"))
  2044. (repeat :tag "New syntax"
  2045. (cons
  2046. (choice
  2047. :tag "Interpretation"
  2048. ;;Quick and dirty way to see
  2049. ;;`org-todo-interpretations'. This takes the
  2050. ;;place of item arguments
  2051. :convert-widget
  2052. (lambda (widget)
  2053. (widget-put widget
  2054. :args (mapcar
  2055. #'(lambda (x)
  2056. (widget-convert
  2057. (cons 'const x)))
  2058. org-todo-interpretation-widgets))
  2059. widget))
  2060. (repeat
  2061. (string :tag "Keyword"))))))
  2062. (defvar org-todo-keywords-1 nil
  2063. "All TODO and DONE keywords active in a buffer.")
  2064. (make-variable-buffer-local 'org-todo-keywords-1)
  2065. (defvar org-todo-keywords-for-agenda nil)
  2066. (defvar org-done-keywords-for-agenda nil)
  2067. (defvar org-todo-keyword-alist-for-agenda nil)
  2068. (defvar org-tag-alist-for-agenda nil
  2069. "Alist of all tags from all agenda files.")
  2070. (defvar org-tag-groups-alist-for-agenda nil
  2071. "Alist of all groups tags from all current agenda files.")
  2072. (defvar org-tag-groups-alist nil)
  2073. (make-variable-buffer-local 'org-tag-groups-alist)
  2074. (defvar org-agenda-contributing-files nil)
  2075. (defvar org-not-done-keywords nil)
  2076. (make-variable-buffer-local 'org-not-done-keywords)
  2077. (defvar org-done-keywords nil)
  2078. (make-variable-buffer-local 'org-done-keywords)
  2079. (defvar org-todo-heads nil)
  2080. (make-variable-buffer-local 'org-todo-heads)
  2081. (defvar org-todo-sets nil)
  2082. (make-variable-buffer-local 'org-todo-sets)
  2083. (defvar org-todo-log-states nil)
  2084. (make-variable-buffer-local 'org-todo-log-states)
  2085. (defvar org-todo-kwd-alist nil)
  2086. (make-variable-buffer-local 'org-todo-kwd-alist)
  2087. (defvar org-todo-key-alist nil)
  2088. (make-variable-buffer-local 'org-todo-key-alist)
  2089. (defvar org-todo-key-trigger nil)
  2090. (make-variable-buffer-local 'org-todo-key-trigger)
  2091. (defcustom org-todo-interpretation 'sequence
  2092. "Controls how TODO keywords are interpreted.
  2093. This variable is in principle obsolete and is only used for
  2094. backward compatibility, if the interpretation of todo keywords is
  2095. not given already in `org-todo-keywords'. See that variable for
  2096. more information."
  2097. :group 'org-todo
  2098. :group 'org-keywords
  2099. :type '(choice (const sequence)
  2100. (const type)))
  2101. (defcustom org-use-fast-todo-selection t
  2102. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2103. This variable describes if and under what circumstances the cycling
  2104. mechanism for TODO keywords will be replaced by a single-key, direct
  2105. selection scheme.
  2106. When nil, fast selection is never used.
  2107. When the symbol `prefix', it will be used when `org-todo' is called
  2108. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2109. `C-u t' in an agenda buffer.
  2110. When t, fast selection is used by default. In this case, the prefix
  2111. argument forces cycling instead.
  2112. In all cases, the special interface is only used if access keys have
  2113. actually been assigned by the user, i.e. if keywords in the configuration
  2114. are followed by a letter in parenthesis, like TODO(t)."
  2115. :group 'org-todo
  2116. :type '(choice
  2117. (const :tag "Never" nil)
  2118. (const :tag "By default" t)
  2119. (const :tag "Only with C-u C-c C-t" prefix)))
  2120. (defcustom org-provide-todo-statistics t
  2121. "Non-nil means update todo statistics after insert and toggle.
  2122. ALL-HEADLINES means update todo statistics by including headlines
  2123. with no TODO keyword as well, counting them as not done.
  2124. A list of TODO keywords means the same, but skip keywords that are
  2125. not in this list.
  2126. When this is set, todo statistics is updated in the parent of the
  2127. current entry each time a todo state is changed."
  2128. :group 'org-todo
  2129. :type '(choice
  2130. (const :tag "Yes, only for TODO entries" t)
  2131. (const :tag "Yes, including all entries" 'all-headlines)
  2132. (repeat :tag "Yes, for TODOs in this list"
  2133. (string :tag "TODO keyword"))
  2134. (other :tag "No TODO statistics" nil)))
  2135. (defcustom org-hierarchical-todo-statistics t
  2136. "Non-nil means TODO statistics covers just direct children.
  2137. When nil, all entries in the subtree are considered.
  2138. This has only an effect if `org-provide-todo-statistics' is set.
  2139. To set this to nil for only a single subtree, use a COOKIE_DATA
  2140. property and include the word \"recursive\" into the value."
  2141. :group 'org-todo
  2142. :type 'boolean)
  2143. (defcustom org-after-todo-state-change-hook nil
  2144. "Hook which is run after the state of a TODO item was changed.
  2145. The new state (a string with a TODO keyword, or nil) is available in the
  2146. Lisp variable `org-state'."
  2147. :group 'org-todo
  2148. :type 'hook)
  2149. (defvar org-blocker-hook nil
  2150. "Hook for functions that are allowed to block a state change.
  2151. Functions in this hook should not modify the buffer.
  2152. Each function gets as its single argument a property list,
  2153. see `org-trigger-hook' for more information about this list.
  2154. If any of the functions in this hook returns nil, the state change
  2155. is blocked.")
  2156. (defvar org-trigger-hook nil
  2157. "Hook for functions that are triggered by a state change.
  2158. Each function gets as its single argument a property list with at
  2159. least the following elements:
  2160. (:type type-of-change :position pos-at-entry-start
  2161. :from old-state :to new-state)
  2162. Depending on the type, more properties may be present.
  2163. This mechanism is currently implemented for:
  2164. TODO state changes
  2165. ------------------
  2166. :type todo-state-change
  2167. :from previous state (keyword as a string), or nil, or a symbol
  2168. 'todo' or 'done', to indicate the general type of state.
  2169. :to new state, like in :from")
  2170. (defcustom org-enforce-todo-dependencies nil
  2171. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2172. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2173. be blocked if any prior sibling is not yet done.
  2174. Finally, if the parent is blocked because of ordered siblings of its own,
  2175. the child will also be blocked."
  2176. :set (lambda (var val)
  2177. (set var val)
  2178. (if val
  2179. (add-hook 'org-blocker-hook
  2180. 'org-block-todo-from-children-or-siblings-or-parent)
  2181. (remove-hook 'org-blocker-hook
  2182. 'org-block-todo-from-children-or-siblings-or-parent)))
  2183. :group 'org-todo
  2184. :type 'boolean)
  2185. (defcustom org-enforce-todo-checkbox-dependencies nil
  2186. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2187. When this is nil, checkboxes have no influence on switching TODO states.
  2188. When non-nil, you first need to check off all check boxes before the TODO
  2189. entry can be switched to DONE.
  2190. This variable needs to be set before org.el is loaded, and you need to
  2191. restart Emacs after a change to make the change effective. The only way
  2192. to change is while Emacs is running is through the customize interface."
  2193. :set (lambda (var val)
  2194. (set var val)
  2195. (if val
  2196. (add-hook 'org-blocker-hook
  2197. 'org-block-todo-from-checkboxes)
  2198. (remove-hook 'org-blocker-hook
  2199. 'org-block-todo-from-checkboxes)))
  2200. :group 'org-todo
  2201. :type 'boolean)
  2202. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2203. "Non-nil means inserting a TODO heading is treated as state change.
  2204. So when the command \\[org-insert-todo-heading] is used, state change
  2205. logging will apply if appropriate. When nil, the new TODO item will
  2206. be inserted directly, and no logging will take place."
  2207. :group 'org-todo
  2208. :type 'boolean)
  2209. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2210. "Non-nil means switching TODO states with S-cursor counts as state change.
  2211. This is the default behavior. However, setting this to nil allows a
  2212. convenient way to select a TODO state and bypass any logging associated
  2213. with that."
  2214. :group 'org-todo
  2215. :type 'boolean)
  2216. (defcustom org-todo-state-tags-triggers nil
  2217. "Tag changes that should be triggered by TODO state changes.
  2218. This is a list. Each entry is
  2219. (state-change (tag . flag) .......)
  2220. State-change can be a string with a state, and empty string to indicate the
  2221. state that has no TODO keyword, or it can be one of the symbols `todo'
  2222. or `done', meaning any not-done or done state, respectively."
  2223. :group 'org-todo
  2224. :group 'org-tags
  2225. :type '(repeat
  2226. (cons (choice :tag "When changing to"
  2227. (const :tag "Not-done state" todo)
  2228. (const :tag "Done state" done)
  2229. (string :tag "State"))
  2230. (repeat
  2231. (cons :tag "Tag action"
  2232. (string :tag "Tag")
  2233. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2234. (defcustom org-log-done nil
  2235. "Information to record when a task moves to the DONE state.
  2236. Possible values are:
  2237. nil Don't add anything, just change the keyword
  2238. time Add a time stamp to the task
  2239. note Prompt for a note and add it with template `org-log-note-headings'
  2240. This option can also be set with on a per-file-basis with
  2241. #+STARTUP: nologdone
  2242. #+STARTUP: logdone
  2243. #+STARTUP: lognotedone
  2244. You can have local logging settings for a subtree by setting the LOGGING
  2245. property to one or more of these keywords."
  2246. :group 'org-todo
  2247. :group 'org-progress
  2248. :type '(choice
  2249. (const :tag "No logging" nil)
  2250. (const :tag "Record CLOSED timestamp" time)
  2251. (const :tag "Record CLOSED timestamp with note." note)))
  2252. ;; Normalize old uses of org-log-done.
  2253. (cond
  2254. ((eq org-log-done t) (setq org-log-done 'time))
  2255. ((and (listp org-log-done) (memq 'done org-log-done))
  2256. (setq org-log-done 'note)))
  2257. (defcustom org-log-reschedule nil
  2258. "Information to record when the scheduling date of a tasks is modified.
  2259. Possible values are:
  2260. nil Don't add anything, just change the date
  2261. time Add a time stamp to the task
  2262. note Prompt for a note and add it with template `org-log-note-headings'
  2263. This option can also be set with on a per-file-basis with
  2264. #+STARTUP: nologreschedule
  2265. #+STARTUP: logreschedule
  2266. #+STARTUP: lognotereschedule"
  2267. :group 'org-todo
  2268. :group 'org-progress
  2269. :type '(choice
  2270. (const :tag "No logging" nil)
  2271. (const :tag "Record timestamp" time)
  2272. (const :tag "Record timestamp with note." note)))
  2273. (defcustom org-log-redeadline nil
  2274. "Information to record when the deadline date of a tasks is modified.
  2275. Possible values are:
  2276. nil Don't add anything, just change the date
  2277. time Add a time stamp to the task
  2278. note Prompt for a note and add it with template `org-log-note-headings'
  2279. This option can also be set with on a per-file-basis with
  2280. #+STARTUP: nologredeadline
  2281. #+STARTUP: logredeadline
  2282. #+STARTUP: lognoteredeadline
  2283. You can have local logging settings for a subtree by setting the LOGGING
  2284. property to one or more of these keywords."
  2285. :group 'org-todo
  2286. :group 'org-progress
  2287. :type '(choice
  2288. (const :tag "No logging" nil)
  2289. (const :tag "Record timestamp" time)
  2290. (const :tag "Record timestamp with note." note)))
  2291. (defcustom org-log-note-clock-out nil
  2292. "Non-nil means record a note when clocking out of an item.
  2293. This can also be configured on a per-file basis by adding one of
  2294. the following lines anywhere in the buffer:
  2295. #+STARTUP: lognoteclock-out
  2296. #+STARTUP: nolognoteclock-out"
  2297. :group 'org-todo
  2298. :group 'org-progress
  2299. :type 'boolean)
  2300. (defcustom org-log-done-with-time t
  2301. "Non-nil means the CLOSED time stamp will contain date and time.
  2302. When nil, only the date will be recorded."
  2303. :group 'org-progress
  2304. :type 'boolean)
  2305. (defcustom org-log-note-headings
  2306. '((done . "CLOSING NOTE %t")
  2307. (state . "State %-12s from %-12S %t")
  2308. (note . "Note taken on %t")
  2309. (reschedule . "Rescheduled from %S on %t")
  2310. (delschedule . "Not scheduled, was %S on %t")
  2311. (redeadline . "New deadline from %S on %t")
  2312. (deldeadline . "Removed deadline, was %S on %t")
  2313. (refile . "Refiled on %t")
  2314. (clock-out . ""))
  2315. "Headings for notes added to entries.
  2316. The value is an alist, with the car being a symbol indicating the note
  2317. context, and the cdr is the heading to be used. The heading may also be the
  2318. empty string.
  2319. %t in the heading will be replaced by a time stamp.
  2320. %T will be an active time stamp instead the default inactive one
  2321. %d will be replaced by a short-format time stamp.
  2322. %D will be replaced by an active short-format time stamp.
  2323. %s will be replaced by the new TODO state, in double quotes.
  2324. %S will be replaced by the old TODO state, in double quotes.
  2325. %u will be replaced by the user name.
  2326. %U will be replaced by the full user name.
  2327. In fact, it is not a good idea to change the `state' entry, because
  2328. agenda log mode depends on the format of these entries."
  2329. :group 'org-todo
  2330. :group 'org-progress
  2331. :type '(list :greedy t
  2332. (cons (const :tag "Heading when closing an item" done) string)
  2333. (cons (const :tag
  2334. "Heading when changing todo state (todo sequence only)"
  2335. state) string)
  2336. (cons (const :tag "Heading when just taking a note" note) string)
  2337. (cons (const :tag "Heading when clocking out" clock-out) string)
  2338. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2339. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2340. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2341. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2342. (cons (const :tag "Heading when refiling" refile) string)))
  2343. (unless (assq 'note org-log-note-headings)
  2344. (push '(note . "%t") org-log-note-headings))
  2345. (defcustom org-log-into-drawer nil
  2346. "Non-nil means insert state change notes and time stamps into a drawer.
  2347. When nil, state changes notes will be inserted after the headline and
  2348. any scheduling and clock lines, but not inside a drawer.
  2349. The value of this variable should be the name of the drawer to use.
  2350. LOGBOOK is proposed as the default drawer for this purpose, you can
  2351. also set this to a string to define the drawer of your choice.
  2352. A value of t is also allowed, representing \"LOGBOOK\".
  2353. A value of t or nil can also be set with on a per-file-basis with
  2354. #+STARTUP: logdrawer
  2355. #+STARTUP: nologdrawer
  2356. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2357. will be ignored.
  2358. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2359. a subtree."
  2360. :group 'org-todo
  2361. :group 'org-progress
  2362. :type '(choice
  2363. (const :tag "Not into a drawer" nil)
  2364. (const :tag "LOGBOOK" t)
  2365. (string :tag "Other")))
  2366. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2367. (defun org-log-into-drawer ()
  2368. "Return the value of `org-log-into-drawer', but let properties overrule.
  2369. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2370. used instead of the default value."
  2371. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2372. (cond
  2373. ((not p) org-log-into-drawer)
  2374. ((equal p "nil") nil)
  2375. ((equal p "t") "LOGBOOK")
  2376. (t p))))
  2377. (defcustom org-log-state-notes-insert-after-drawers nil
  2378. "Non-nil means insert state change notes after any drawers in entry.
  2379. Only the drawers that *immediately* follow the headline and the
  2380. deadline/scheduled line are skipped.
  2381. When nil, insert notes right after the heading and perhaps the line
  2382. with deadline/scheduling if present.
  2383. This variable will have no effect if `org-log-into-drawer' is
  2384. set."
  2385. :group 'org-todo
  2386. :group 'org-progress
  2387. :type 'boolean)
  2388. (defcustom org-log-states-order-reversed t
  2389. "Non-nil means the latest state note will be directly after heading.
  2390. When nil, the state change notes will be ordered according to time.
  2391. This option can also be set with on a per-file-basis with
  2392. #+STARTUP: logstatesreversed
  2393. #+STARTUP: nologstatesreversed"
  2394. :group 'org-todo
  2395. :group 'org-progress
  2396. :type 'boolean)
  2397. (defcustom org-todo-repeat-to-state nil
  2398. "The TODO state to which a repeater should return the repeating task.
  2399. By default this is the first task in a TODO sequence, or the previous state
  2400. in a TODO_TYP set. But you can specify another task here.
  2401. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2402. :group 'org-todo
  2403. :version "24.1"
  2404. :type '(choice (const :tag "Head of sequence" nil)
  2405. (string :tag "Specific state")))
  2406. (defcustom org-log-repeat 'time
  2407. "Non-nil means record moving through the DONE state when triggering repeat.
  2408. An auto-repeating task is immediately switched back to TODO when
  2409. marked DONE. If you are not logging state changes (by adding \"@\"
  2410. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2411. record a closing note, there will be no record of the task moving
  2412. through DONE. This variable forces taking a note anyway.
  2413. nil Don't force a record
  2414. time Record a time stamp
  2415. note Prompt for a note and add it with template `org-log-note-headings'
  2416. This option can also be set with on a per-file-basis with
  2417. #+STARTUP: nologrepeat
  2418. #+STARTUP: logrepeat
  2419. #+STARTUP: lognoterepeat
  2420. You can have local logging settings for a subtree by setting the LOGGING
  2421. property to one or more of these keywords."
  2422. :group 'org-todo
  2423. :group 'org-progress
  2424. :type '(choice
  2425. (const :tag "Don't force a record" nil)
  2426. (const :tag "Force recording the DONE state" time)
  2427. (const :tag "Force recording a note with the DONE state" note)))
  2428. (defgroup org-priorities nil
  2429. "Priorities in Org-mode."
  2430. :tag "Org Priorities"
  2431. :group 'org-todo)
  2432. (defcustom org-enable-priority-commands t
  2433. "Non-nil means priority commands are active.
  2434. When nil, these commands will be disabled, so that you never accidentally
  2435. set a priority."
  2436. :group 'org-priorities
  2437. :type 'boolean)
  2438. (defcustom org-highest-priority ?A
  2439. "The highest priority of TODO items. A character like ?A, ?B etc.
  2440. Must have a smaller ASCII number than `org-lowest-priority'."
  2441. :group 'org-priorities
  2442. :type 'character)
  2443. (defcustom org-lowest-priority ?C
  2444. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2445. Must have a larger ASCII number than `org-highest-priority'."
  2446. :group 'org-priorities
  2447. :type 'character)
  2448. (defcustom org-default-priority ?B
  2449. "The default priority of TODO items.
  2450. This is the priority an item gets if no explicit priority is given.
  2451. When starting to cycle on an empty priority the first step in the cycle
  2452. depends on `org-priority-start-cycle-with-default'. The resulting first
  2453. step priority must not exceed the range from `org-highest-priority' to
  2454. `org-lowest-priority' which means that `org-default-priority' has to be
  2455. in this range exclusive or inclusive the range boundaries. Else the
  2456. first step refuses to set the default and the second will fall back
  2457. to (depending on the command used) the highest or lowest priority."
  2458. :group 'org-priorities
  2459. :type 'character)
  2460. (defcustom org-priority-start-cycle-with-default t
  2461. "Non-nil means start with default priority when starting to cycle.
  2462. When this is nil, the first step in the cycle will be (depending on the
  2463. command used) one higher or lower than the default priority.
  2464. See also `org-default-priority'."
  2465. :group 'org-priorities
  2466. :type 'boolean)
  2467. (defcustom org-get-priority-function nil
  2468. "Function to extract the priority from a string.
  2469. The string is normally the headline. If this is nil Org computes the
  2470. priority from the priority cookie like [#A] in the headline. It returns
  2471. an integer, increasing by 1000 for each priority level.
  2472. The user can set a different function here, which should take a string
  2473. as an argument and return the numeric priority."
  2474. :group 'org-priorities
  2475. :version "24.1"
  2476. :type '(choice
  2477. (const nil)
  2478. (function)))
  2479. (defgroup org-time nil
  2480. "Options concerning time stamps and deadlines in Org-mode."
  2481. :tag "Org Time"
  2482. :group 'org)
  2483. (defcustom org-insert-labeled-timestamps-at-point nil
  2484. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2485. When nil, these labeled time stamps are forces into the second line of an
  2486. entry, just after the headline. When scheduling from the global TODO list,
  2487. the time stamp will always be forced into the second line."
  2488. :group 'org-time
  2489. :type 'boolean)
  2490. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2491. "Formats for `format-time-string' which are used for time stamps.
  2492. It is not recommended to change this constant.")
  2493. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2494. "Number of minutes to round time stamps to.
  2495. These are two values, the first applies when first creating a time stamp.
  2496. The second applies when changing it with the commands `S-up' and `S-down'.
  2497. When changing the time stamp, this means that it will change in steps
  2498. of N minutes, as given by the second value.
  2499. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2500. numbers should be factors of 60, so for example 5, 10, 15.
  2501. When this is larger than 1, you can still force an exact time stamp by using
  2502. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2503. and by using a prefix arg to `S-up/down' to specify the exact number
  2504. of minutes to shift."
  2505. :group 'org-time
  2506. :get #'(lambda (var) ; Make sure both elements are there
  2507. (if (integerp (default-value var))
  2508. (list (default-value var) 5)
  2509. (default-value var)))
  2510. :type '(list
  2511. (integer :tag "when inserting times")
  2512. (integer :tag "when modifying times")))
  2513. ;; Normalize old customizations of this variable.
  2514. (when (integerp org-time-stamp-rounding-minutes)
  2515. (setq org-time-stamp-rounding-minutes
  2516. (list org-time-stamp-rounding-minutes
  2517. org-time-stamp-rounding-minutes)))
  2518. (defcustom org-display-custom-times nil
  2519. "Non-nil means overlay custom formats over all time stamps.
  2520. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2521. To turn this on on a per-file basis, insert anywhere in the file:
  2522. #+STARTUP: customtime"
  2523. :group 'org-time
  2524. :set 'set-default
  2525. :type 'sexp)
  2526. (make-variable-buffer-local 'org-display-custom-times)
  2527. (defcustom org-time-stamp-custom-formats
  2528. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2529. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2530. These are overlaid over the default ISO format if the variable
  2531. `org-display-custom-times' is set. Time like %H:%M should be at the
  2532. end of the second format. The custom formats are also honored by export
  2533. commands, if custom time display is turned on at the time of export."
  2534. :group 'org-time
  2535. :type 'sexp)
  2536. (defun org-time-stamp-format (&optional long inactive)
  2537. "Get the right format for a time string."
  2538. (let ((f (if long (cdr org-time-stamp-formats)
  2539. (car org-time-stamp-formats))))
  2540. (if inactive
  2541. (concat "[" (substring f 1 -1) "]")
  2542. f)))
  2543. (defcustom org-time-clocksum-format
  2544. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2545. "The format string used when creating CLOCKSUM lines.
  2546. This is also used when Org mode generates a time duration.
  2547. The value can be a single format string containing two
  2548. %-sequences, which will be filled with the number of hours and
  2549. minutes in that order.
  2550. Alternatively, the value can be a plist associating any of the
  2551. keys :years, :months, :weeks, :days, :hours or :minutes with
  2552. format strings. The time duration is formatted using only the
  2553. time components that are needed and concatenating the results.
  2554. If a time unit in absent, it falls back to the next smallest
  2555. unit.
  2556. The keys :require-years, :require-months, :require-days,
  2557. :require-weeks, :require-hours, :require-minutes are also
  2558. meaningful. A non-nil value for these keys indicates that the
  2559. corresponding time component should always be included, even if
  2560. its value is 0.
  2561. For example,
  2562. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2563. :require-minutes t)
  2564. means durations longer than a day will be expressed in days,
  2565. hours and minutes, and durations less than a day will always be
  2566. expressed in hours and minutes (even for durations less than an
  2567. hour).
  2568. The value
  2569. \(:days \"%dd\" :minutes \"%dm\")
  2570. means durations longer than a day will be expressed in days and
  2571. minutes, and durations less than a day will be expressed entirely
  2572. in minutes (even for durations longer than an hour)."
  2573. :group 'org-time
  2574. :group 'org-clock
  2575. :version "24.4"
  2576. :package-version '(Org . "8.0")
  2577. :type '(choice (string :tag "Format string")
  2578. (set :tag "Plist"
  2579. (group :inline t (const :tag "Years" :years)
  2580. (string :tag "Format string"))
  2581. (group :inline t
  2582. (const :tag "Always show years" :require-years)
  2583. (const t))
  2584. (group :inline t (const :tag "Months" :months)
  2585. (string :tag "Format string"))
  2586. (group :inline t
  2587. (const :tag "Always show months" :require-months)
  2588. (const t))
  2589. (group :inline t (const :tag "Weeks" :weeks)
  2590. (string :tag "Format string"))
  2591. (group :inline t
  2592. (const :tag "Always show weeks" :require-weeks)
  2593. (const t))
  2594. (group :inline t (const :tag "Days" :days)
  2595. (string :tag "Format string"))
  2596. (group :inline t
  2597. (const :tag "Always show days" :require-days)
  2598. (const t))
  2599. (group :inline t (const :tag "Hours" :hours)
  2600. (string :tag "Format string"))
  2601. (group :inline t
  2602. (const :tag "Always show hours" :require-hours)
  2603. (const t))
  2604. (group :inline t (const :tag "Minutes" :minutes)
  2605. (string :tag "Format string"))
  2606. (group :inline t
  2607. (const :tag "Always show minutes" :require-minutes)
  2608. (const t)))))
  2609. (defcustom org-time-clocksum-use-fractional nil
  2610. "When non-nil, \\[org-clock-display] uses fractional times.
  2611. See `org-time-clocksum-format' for more on time clock formats."
  2612. :group 'org-time
  2613. :group 'org-clock
  2614. :version "24.3"
  2615. :type 'boolean)
  2616. (defcustom org-time-clocksum-use-effort-durations nil
  2617. "When non-nil, \\[org-clock-display] uses effort durations.
  2618. E.g. by default, one day is considered to be a 8 hours effort,
  2619. so a task that has been clocked for 16 hours will be displayed
  2620. as during 2 days in the clock display or in the clocktable.
  2621. See `org-effort-durations' on how to set effort durations
  2622. and `org-time-clocksum-format' for more on time clock formats."
  2623. :group 'org-time
  2624. :group 'org-clock
  2625. :version "24.4"
  2626. :package-version '(Org . "8.0")
  2627. :type 'boolean)
  2628. (defcustom org-time-clocksum-fractional-format "%.2f"
  2629. "The format string used when creating CLOCKSUM lines,
  2630. or when Org mode generates a time duration, if
  2631. `org-time-clocksum-use-fractional' is enabled.
  2632. The value can be a single format string containing one
  2633. %-sequence, which will be filled with the number of hours as
  2634. a float.
  2635. Alternatively, the value can be a plist associating any of the
  2636. keys :years, :months, :weeks, :days, :hours or :minutes with
  2637. a format string. The time duration is formatted using the
  2638. largest time unit which gives a non-zero integer part. If all
  2639. specified formats have zero integer part, the smallest time unit
  2640. is used."
  2641. :group 'org-time
  2642. :type '(choice (string :tag "Format string")
  2643. (set (group :inline t (const :tag "Years" :years)
  2644. (string :tag "Format string"))
  2645. (group :inline t (const :tag "Months" :months)
  2646. (string :tag "Format string"))
  2647. (group :inline t (const :tag "Weeks" :weeks)
  2648. (string :tag "Format string"))
  2649. (group :inline t (const :tag "Days" :days)
  2650. (string :tag "Format string"))
  2651. (group :inline t (const :tag "Hours" :hours)
  2652. (string :tag "Format string"))
  2653. (group :inline t (const :tag "Minutes" :minutes)
  2654. (string :tag "Format string")))))
  2655. (defcustom org-deadline-warning-days 14
  2656. "Number of days before expiration during which a deadline becomes active.
  2657. This variable governs the display in sparse trees and in the agenda.
  2658. When 0 or negative, it means use this number (the absolute value of it)
  2659. even if a deadline has a different individual lead time specified.
  2660. Custom commands can set this variable in the options section."
  2661. :group 'org-time
  2662. :group 'org-agenda-daily/weekly
  2663. :type 'integer)
  2664. (defcustom org-scheduled-delay-days 0
  2665. "Number of days before a scheduled item becomes active.
  2666. This variable governs the display in sparse trees and in the agenda.
  2667. The default value (i.e. 0) means: don't delay scheduled item.
  2668. When negative, it means use this number (the absolute value of it)
  2669. even if a scheduled item has a different individual delay time
  2670. specified.
  2671. Custom commands can set this variable in the options section."
  2672. :group 'org-time
  2673. :group 'org-agenda-daily/weekly
  2674. :version "24.4"
  2675. :package-version '(Org . "8.0")
  2676. :type 'integer)
  2677. (defcustom org-read-date-prefer-future t
  2678. "Non-nil means assume future for incomplete date input from user.
  2679. This affects the following situations:
  2680. 1. The user gives a month but not a year.
  2681. For example, if it is April and you enter \"feb 2\", this will be read
  2682. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2683. 2. The user gives a day, but no month.
  2684. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2685. read this as the third of *next* month. However, if you enter \"17\",
  2686. it will be considered as *this* month.
  2687. If you set this variable to the symbol `time', then also the following
  2688. will work:
  2689. 3. If the user gives a time.
  2690. If the time is before now, it will be interpreted as tomorrow.
  2691. Currently none of this works for ISO week specifications.
  2692. When this option is nil, the current day, month and year will always be
  2693. used as defaults.
  2694. See also `org-agenda-jump-prefer-future'."
  2695. :group 'org-time
  2696. :type '(choice
  2697. (const :tag "Never" nil)
  2698. (const :tag "Check month and day" t)
  2699. (const :tag "Check month, day, and time" time)))
  2700. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2701. "Should the agenda jump command prefer the future for incomplete dates?
  2702. The default is to do the same as configured in `org-read-date-prefer-future'.
  2703. But you can also set a deviating value here.
  2704. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2705. :group 'org-agenda
  2706. :group 'org-time
  2707. :version "24.1"
  2708. :type '(choice
  2709. (const :tag "Use org-read-date-prefer-future"
  2710. org-read-date-prefer-future)
  2711. (const :tag "Never" nil)
  2712. (const :tag "Always" t)))
  2713. (defcustom org-read-date-force-compatible-dates t
  2714. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2715. Depending on the system Emacs is running on, certain dates cannot
  2716. be represented with the type used internally to represent time.
  2717. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2718. correctly. Some systems allow for earlier dates, some for later,
  2719. some for both. One way to find out it to insert any date into an
  2720. Org buffer, putting the cursor on the year and hitting S-up and
  2721. S-down to test the range.
  2722. When this variable is set to t, the date/time prompt will not let
  2723. you specify dates outside the 1970-2037 range, so it is certain that
  2724. these dates will work in whatever version of Emacs you are
  2725. running, and also that you can move a file from one Emacs implementation
  2726. to another. WHenever Org is forcing the year for you, it will display
  2727. a message and beep.
  2728. When this variable is nil, Org will check if the date is
  2729. representable in the specific Emacs implementation you are using.
  2730. If not, it will force a year, usually the current year, and beep
  2731. to remind you. Currently this setting is not recommended because
  2732. the likelihood that you will open your Org files in an Emacs that
  2733. has limited date range is not negligible.
  2734. A workaround for this problem is to use diary sexp dates for time
  2735. stamps outside of this range."
  2736. :group 'org-time
  2737. :version "24.1"
  2738. :type 'boolean)
  2739. (defcustom org-read-date-display-live t
  2740. "Non-nil means display current interpretation of date prompt live.
  2741. This display will be in an overlay, in the minibuffer."
  2742. :group 'org-time
  2743. :type 'boolean)
  2744. (defcustom org-read-date-popup-calendar t
  2745. "Non-nil means pop up a calendar when prompting for a date.
  2746. In the calendar, the date can be selected with mouse-1. However, the
  2747. minibuffer will also be active, and you can simply enter the date as well.
  2748. When nil, only the minibuffer will be available."
  2749. :group 'org-time
  2750. :type 'boolean)
  2751. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2752. 'org-read-date-popup-calendar)
  2753. (make-obsolete-variable
  2754. 'org-read-date-minibuffer-setup-hook
  2755. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2756. (defcustom org-read-date-minibuffer-setup-hook nil
  2757. "Hook to be used to set up keys for the date/time interface.
  2758. Add key definitions to `minibuffer-local-map', which will be a
  2759. temporary copy.
  2760. WARNING: This option is obsolete, you should use
  2761. `org-read-date-minibuffer-local-map' to set up keys."
  2762. :group 'org-time
  2763. :type 'hook)
  2764. (defcustom org-extend-today-until 0
  2765. "The hour when your day really ends. Must be an integer.
  2766. This has influence for the following applications:
  2767. - When switching the agenda to \"today\". It it is still earlier than
  2768. the time given here, the day recognized as TODAY is actually yesterday.
  2769. - When a date is read from the user and it is still before the time given
  2770. here, the current date and time will be assumed to be yesterday, 23:59.
  2771. Also, timestamps inserted in capture templates follow this rule.
  2772. IMPORTANT: This is a feature whose implementation is and likely will
  2773. remain incomplete. Really, it is only here because past midnight seems to
  2774. be the favorite working time of John Wiegley :-)"
  2775. :group 'org-time
  2776. :type 'integer)
  2777. (defcustom org-use-effective-time nil
  2778. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2779. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2780. \"effective time\" of any timestamps between midnight and 8am will be
  2781. 23:59 of the previous day."
  2782. :group 'org-time
  2783. :version "24.1"
  2784. :type 'boolean)
  2785. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2786. "When non-nil, use the last clock out time for `org-todo'.
  2787. Note that this option has precedence over the combined use of
  2788. `org-use-effective-time' and `org-extend-today-until'."
  2789. :group 'org-time
  2790. :version "24.4"
  2791. :package-version '(Org . "8.0")
  2792. :type 'boolean)
  2793. (defcustom org-edit-timestamp-down-means-later nil
  2794. "Non-nil means S-down will increase the time in a time stamp.
  2795. When nil, S-up will increase."
  2796. :group 'org-time
  2797. :type 'boolean)
  2798. (defcustom org-calendar-follow-timestamp-change t
  2799. "Non-nil means make the calendar window follow timestamp changes.
  2800. When a timestamp is modified and the calendar window is visible, it will be
  2801. moved to the new date."
  2802. :group 'org-time
  2803. :type 'boolean)
  2804. (defgroup org-tags nil
  2805. "Options concerning tags in Org-mode."
  2806. :tag "Org Tags"
  2807. :group 'org)
  2808. (defcustom org-tag-alist nil
  2809. "List of tags allowed in Org-mode files.
  2810. When this list is nil, Org-mode will base TAG input on what is already in the
  2811. buffer.
  2812. The value of this variable is an alist, the car of each entry must be a
  2813. keyword as a string, the cdr may be a character that is used to select
  2814. that tag through the fast-tag-selection interface.
  2815. See the manual for details."
  2816. :group 'org-tags
  2817. :type '(repeat
  2818. (choice
  2819. (cons (string :tag "Tag name")
  2820. (character :tag "Access char"))
  2821. (list :tag "Start radio group"
  2822. (const :startgroup)
  2823. (option (string :tag "Group description")))
  2824. (list :tag "Group tags delimiter"
  2825. (const :grouptags))
  2826. (list :tag "End radio group"
  2827. (const :endgroup)
  2828. (option (string :tag "Group description")))
  2829. (const :tag "New line" (:newline)))))
  2830. (defcustom org-tag-persistent-alist nil
  2831. "List of tags that will always appear in all Org-mode files.
  2832. This is in addition to any in buffer settings or customizations
  2833. of `org-tag-alist'.
  2834. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2835. The value of this variable is an alist, the car of each entry must be a
  2836. keyword as a string, the cdr may be a character that is used to select
  2837. that tag through the fast-tag-selection interface.
  2838. See the manual for details.
  2839. To disable these tags on a per-file basis, insert anywhere in the file:
  2840. #+STARTUP: noptag"
  2841. :group 'org-tags
  2842. :type '(repeat
  2843. (choice
  2844. (cons (string :tag "Tag name")
  2845. (character :tag "Access char"))
  2846. (const :tag "Start radio group" (:startgroup))
  2847. (const :tag "Group tags delimiter" (:grouptags))
  2848. (const :tag "End radio group" (:endgroup))
  2849. (const :tag "New line" (:newline)))))
  2850. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2851. "If non-nil, always offer completion for all tags of all agenda files.
  2852. Instead of customizing this variable directly, you might want to
  2853. set it locally for capture buffers, because there no list of
  2854. tags in that file can be created dynamically (there are none).
  2855. (add-hook 'org-capture-mode-hook
  2856. (lambda ()
  2857. (set (make-local-variable
  2858. 'org-complete-tags-always-offer-all-agenda-tags)
  2859. t)))"
  2860. :group 'org-tags
  2861. :version "24.1"
  2862. :type 'boolean)
  2863. (defvar org-file-tags nil
  2864. "List of tags that can be inherited by all entries in the file.
  2865. The tags will be inherited if the variable `org-use-tag-inheritance'
  2866. says they should be.
  2867. This variable is populated from #+FILETAGS lines.")
  2868. (defcustom org-use-fast-tag-selection 'auto
  2869. "Non-nil means use fast tag selection scheme.
  2870. This is a special interface to select and deselect tags with single keys.
  2871. When nil, fast selection is never used.
  2872. When the symbol `auto', fast selection is used if and only if selection
  2873. characters for tags have been configured, either through the variable
  2874. `org-tag-alist' or through a #+TAGS line in the buffer.
  2875. When t, fast selection is always used and selection keys are assigned
  2876. automatically if necessary."
  2877. :group 'org-tags
  2878. :type '(choice
  2879. (const :tag "Always" t)
  2880. (const :tag "Never" nil)
  2881. (const :tag "When selection characters are configured" auto)))
  2882. (defcustom org-fast-tag-selection-single-key nil
  2883. "Non-nil means fast tag selection exits after first change.
  2884. When nil, you have to press RET to exit it.
  2885. During fast tag selection, you can toggle this flag with `C-c'.
  2886. This variable can also have the value `expert'. In this case, the window
  2887. displaying the tags menu is not even shown, until you press C-c again."
  2888. :group 'org-tags
  2889. :type '(choice
  2890. (const :tag "No" nil)
  2891. (const :tag "Yes" t)
  2892. (const :tag "Expert" expert)))
  2893. (defvar org-fast-tag-selection-include-todo nil
  2894. "Non-nil means fast tags selection interface will also offer TODO states.
  2895. This is an undocumented feature, you should not rely on it.")
  2896. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2897. "The column to which tags should be indented in a headline.
  2898. If this number is positive, it specifies the column. If it is negative,
  2899. it means that the tags should be flushright to that column. For example,
  2900. -80 works well for a normal 80 character screen.
  2901. When 0, place tags directly after headline text, with only one space in
  2902. between."
  2903. :group 'org-tags
  2904. :type 'integer)
  2905. (defcustom org-auto-align-tags t
  2906. "Non-nil keeps tags aligned when modifying headlines.
  2907. Some operations (i.e. demoting) change the length of a headline and
  2908. therefore shift the tags around. With this option turned on, after
  2909. each such operation the tags are again aligned to `org-tags-column'."
  2910. :group 'org-tags
  2911. :type 'boolean)
  2912. (defcustom org-use-tag-inheritance t
  2913. "Non-nil means tags in levels apply also for sublevels.
  2914. When nil, only the tags directly given in a specific line apply there.
  2915. This may also be a list of tags that should be inherited, or a regexp that
  2916. matches tags that should be inherited. Additional control is possible
  2917. with the variable `org-tags-exclude-from-inheritance' which gives an
  2918. explicit list of tags to be excluded from inheritance, even if the value of
  2919. `org-use-tag-inheritance' would select it for inheritance.
  2920. If this option is t, a match early-on in a tree can lead to a large
  2921. number of matches in the subtree when constructing the agenda or creating
  2922. a sparse tree. If you only want to see the first match in a tree during
  2923. a search, check out the variable `org-tags-match-list-sublevels'."
  2924. :group 'org-tags
  2925. :type '(choice
  2926. (const :tag "Not" nil)
  2927. (const :tag "Always" t)
  2928. (repeat :tag "Specific tags" (string :tag "Tag"))
  2929. (regexp :tag "Tags matched by regexp")))
  2930. (defcustom org-tags-exclude-from-inheritance nil
  2931. "List of tags that should never be inherited.
  2932. This is a way to exclude a few tags from inheritance. For way to do
  2933. the opposite, to actively allow inheritance for selected tags,
  2934. see the variable `org-use-tag-inheritance'."
  2935. :group 'org-tags
  2936. :type '(repeat (string :tag "Tag")))
  2937. (defun org-tag-inherit-p (tag)
  2938. "Check if TAG is one that should be inherited."
  2939. (cond
  2940. ((member tag org-tags-exclude-from-inheritance) nil)
  2941. ((eq org-use-tag-inheritance t) t)
  2942. ((not org-use-tag-inheritance) nil)
  2943. ((stringp org-use-tag-inheritance)
  2944. (string-match org-use-tag-inheritance tag))
  2945. ((listp org-use-tag-inheritance)
  2946. (member tag org-use-tag-inheritance))
  2947. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2948. (defcustom org-tags-match-list-sublevels t
  2949. "Non-nil means list also sublevels of headlines matching a search.
  2950. This variable applies to tags/property searches, and also to stuck
  2951. projects because this search is based on a tags match as well.
  2952. When set to the symbol `indented', sublevels are indented with
  2953. leading dots.
  2954. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2955. the sublevels of a headline matching a tag search often also match
  2956. the same search. Listing all of them can create very long lists.
  2957. Setting this variable to nil causes subtrees of a match to be skipped.
  2958. This variable is semi-obsolete and probably should always be true. It
  2959. is better to limit inheritance to certain tags using the variables
  2960. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2961. :group 'org-tags
  2962. :type '(choice
  2963. (const :tag "No, don't list them" nil)
  2964. (const :tag "Yes, do list them" t)
  2965. (const :tag "List them, indented with leading dots" indented)))
  2966. (defcustom org-tags-sort-function nil
  2967. "When set, tags are sorted using this function as a comparator."
  2968. :group 'org-tags
  2969. :type '(choice
  2970. (const :tag "No sorting" nil)
  2971. (const :tag "Alphabetical" string<)
  2972. (const :tag "Reverse alphabetical" string>)
  2973. (function :tag "Custom function" nil)))
  2974. (defvar org-tags-history nil
  2975. "History of minibuffer reads for tags.")
  2976. (defvar org-last-tags-completion-table nil
  2977. "The last used completion table for tags.")
  2978. (defvar org-after-tags-change-hook nil
  2979. "Hook that is run after the tags in a line have changed.")
  2980. (defgroup org-properties nil
  2981. "Options concerning properties in Org-mode."
  2982. :tag "Org Properties"
  2983. :group 'org)
  2984. (defcustom org-property-format "%-10s %s"
  2985. "How property key/value pairs should be formatted by `indent-line'.
  2986. When `indent-line' hits a property definition, it will format the line
  2987. according to this format, mainly to make sure that the values are
  2988. lined-up with respect to each other."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-properties-postprocess-alist nil
  2992. "Alist of properties and functions to adjust inserted values.
  2993. Elements of this alist must be of the form
  2994. ([string] [function])
  2995. where [string] must be a property name and [function] must be a
  2996. lambda expression: this lambda expression must take one argument,
  2997. the value to adjust, and return the new value as a string.
  2998. For example, this element will allow the property \"Remaining\"
  2999. to be updated wrt the relation between the \"Effort\" property
  3000. and the clock summary:
  3001. ((\"Remaining\" (lambda(value)
  3002. (let ((clocksum (org-clock-sum-current-item))
  3003. (effort (org-duration-string-to-minutes
  3004. (org-entry-get (point) \"Effort\"))))
  3005. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3006. :group 'org-properties
  3007. :version "24.1"
  3008. :type '(alist :key-type (string :tag "Property")
  3009. :value-type (function :tag "Function")))
  3010. (defcustom org-use-property-inheritance nil
  3011. "Non-nil means properties apply also for sublevels.
  3012. This setting is chiefly used during property searches. Turning it on can
  3013. cause significant overhead when doing a search, which is why it is not
  3014. on by default.
  3015. When nil, only the properties directly given in the current entry count.
  3016. When t, every property is inherited. The value may also be a list of
  3017. properties that should have inheritance, or a regular expression matching
  3018. properties that should be inherited.
  3019. However, note that some special properties use inheritance under special
  3020. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3021. and the properties ending in \"_ALL\" when they are used as descriptor
  3022. for valid values of a property.
  3023. Note for programmers:
  3024. When querying an entry with `org-entry-get', you can control if inheritance
  3025. should be used. By default, `org-entry-get' looks only at the local
  3026. properties. You can request inheritance by setting the inherit argument
  3027. to t (to force inheritance) or to `selective' (to respect the setting
  3028. in this variable)."
  3029. :group 'org-properties
  3030. :type '(choice
  3031. (const :tag "Not" nil)
  3032. (const :tag "Always" t)
  3033. (repeat :tag "Specific properties" (string :tag "Property"))
  3034. (regexp :tag "Properties matched by regexp")))
  3035. (defun org-property-inherit-p (property)
  3036. "Check if PROPERTY is one that should be inherited."
  3037. (cond
  3038. ((eq org-use-property-inheritance t) t)
  3039. ((not org-use-property-inheritance) nil)
  3040. ((stringp org-use-property-inheritance)
  3041. (string-match org-use-property-inheritance property))
  3042. ((listp org-use-property-inheritance)
  3043. (member property org-use-property-inheritance))
  3044. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3045. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3046. "The default column format, if no other format has been defined.
  3047. This variable can be set on the per-file basis by inserting a line
  3048. #+COLUMNS: %25ITEM ....."
  3049. :group 'org-properties
  3050. :type 'string)
  3051. (defcustom org-columns-ellipses ".."
  3052. "The ellipses to be used when a field in column view is truncated.
  3053. When this is the empty string, as many characters as possible are shown,
  3054. but then there will be no visual indication that the field has been truncated.
  3055. When this is a string of length N, the last N characters of a truncated
  3056. field are replaced by this string. If the column is narrower than the
  3057. ellipses string, only part of the ellipses string will be shown."
  3058. :group 'org-properties
  3059. :type 'string)
  3060. (defcustom org-columns-modify-value-for-display-function nil
  3061. "Function that modifies values for display in column view.
  3062. For example, it can be used to cut out a certain part from a time stamp.
  3063. The function must take 2 arguments:
  3064. column-title The title of the column (*not* the property name)
  3065. value The value that should be modified.
  3066. The function should return the value that should be displayed,
  3067. or nil if the normal value should be used."
  3068. :group 'org-properties
  3069. :type '(choice (const nil) (function)))
  3070. (defcustom org-effort-property "Effort"
  3071. "The property that is being used to keep track of effort estimates.
  3072. Effort estimates given in this property need to have the format H:MM."
  3073. :group 'org-properties
  3074. :group 'org-progress
  3075. :type '(string :tag "Property"))
  3076. (defconst org-global-properties-fixed
  3077. '(("VISIBILITY_ALL" . "folded children content all")
  3078. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3079. "List of property/value pairs that can be inherited by any entry.
  3080. These are fixed values, for the preset properties. The user variable
  3081. that can be used to add to this list is `org-global-properties'.
  3082. The entries in this list are cons cells where the car is a property
  3083. name and cdr is a string with the value. If the value represents
  3084. multiple items like an \"_ALL\" property, separate the items by
  3085. spaces.")
  3086. (defcustom org-global-properties nil
  3087. "List of property/value pairs that can be inherited by any entry.
  3088. This list will be combined with the constant `org-global-properties-fixed'.
  3089. The entries in this list are cons cells where the car is a property
  3090. name and cdr is a string with the value.
  3091. You can set buffer-local values for the same purpose in the variable
  3092. `org-file-properties' this by adding lines like
  3093. #+PROPERTY: NAME VALUE"
  3094. :group 'org-properties
  3095. :type '(repeat
  3096. (cons (string :tag "Property")
  3097. (string :tag "Value"))))
  3098. (defvar org-file-properties nil
  3099. "List of property/value pairs that can be inherited by any entry.
  3100. Valid for the current buffer.
  3101. This variable is populated from #+PROPERTY lines.")
  3102. (make-variable-buffer-local 'org-file-properties)
  3103. (defgroup org-agenda nil
  3104. "Options concerning agenda views in Org-mode."
  3105. :tag "Org Agenda"
  3106. :group 'org)
  3107. (defvar org-category nil
  3108. "Variable used by org files to set a category for agenda display.
  3109. Such files should use a file variable to set it, for example
  3110. # -*- mode: org; org-category: \"ELisp\"
  3111. or contain a special line
  3112. #+CATEGORY: ELisp
  3113. If the file does not specify a category, then file's base name
  3114. is used instead.")
  3115. (make-variable-buffer-local 'org-category)
  3116. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3117. (defcustom org-agenda-files nil
  3118. "The files to be used for agenda display.
  3119. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3120. \\[org-remove-file]. You can also use customize to edit the list.
  3121. If an entry is a directory, all files in that directory that are matched by
  3122. `org-agenda-file-regexp' will be part of the file list.
  3123. If the value of the variable is not a list but a single file name, then
  3124. the list of agenda files is actually stored and maintained in that file, one
  3125. agenda file per line. In this file paths can be given relative to
  3126. `org-directory'. Tilde expansion and environment variable substitution
  3127. are also made."
  3128. :group 'org-agenda
  3129. :type '(choice
  3130. (repeat :tag "List of files and directories" file)
  3131. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3132. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3133. "Regular expression to match files for `org-agenda-files'.
  3134. If any element in the list in that variable contains a directory instead
  3135. of a normal file, all files in that directory that are matched by this
  3136. regular expression will be included."
  3137. :group 'org-agenda
  3138. :type 'regexp)
  3139. (defcustom org-agenda-text-search-extra-files nil
  3140. "List of extra files to be searched by text search commands.
  3141. These files will be searched in addition to the agenda files by the
  3142. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3143. Note that these files will only be searched for text search commands,
  3144. not for the other agenda views like todo lists, tag searches or the weekly
  3145. agenda. This variable is intended to list notes and possibly archive files
  3146. that should also be searched by these two commands.
  3147. In fact, if the first element in the list is the symbol `agenda-archives',
  3148. then all archive files of all agenda files will be added to the search
  3149. scope."
  3150. :group 'org-agenda
  3151. :type '(set :greedy t
  3152. (const :tag "Agenda Archives" agenda-archives)
  3153. (repeat :inline t (file))))
  3154. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3155. 'org-agenda-text-search-extra-files)
  3156. (defcustom org-agenda-skip-unavailable-files nil
  3157. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3158. A nil value means to remove them, after a query, from the list."
  3159. :group 'org-agenda
  3160. :type 'boolean)
  3161. (defcustom org-calendar-to-agenda-key [?c]
  3162. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3163. The command `org-calendar-goto-agenda' will be bound to this key. The
  3164. default is the character `c' because then `c' can be used to switch back and
  3165. forth between agenda and calendar."
  3166. :group 'org-agenda
  3167. :type 'sexp)
  3168. (defcustom org-calendar-insert-diary-entry-key [?i]
  3169. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3170. This option is irrelevant until `org-agenda-diary-file' has been configured
  3171. to point to an Org-mode file. When that is the case, the command
  3172. `org-agenda-diary-entry' will be bound to the key given here, by default
  3173. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3174. if you want to continue doing this, you need to change this to a different
  3175. key."
  3176. :group 'org-agenda
  3177. :type 'sexp)
  3178. (defcustom org-agenda-diary-file 'diary-file
  3179. "File to which to add new entries with the `i' key in agenda and calendar.
  3180. When this is the symbol `diary-file', the functionality in the Emacs
  3181. calendar will be used to add entries to the `diary-file'. But when this
  3182. points to a file, `org-agenda-diary-entry' will be used instead."
  3183. :group 'org-agenda
  3184. :type '(choice
  3185. (const :tag "The standard Emacs diary file" diary-file)
  3186. (file :tag "Special Org file diary entries")))
  3187. (eval-after-load "calendar"
  3188. '(progn
  3189. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3190. 'org-calendar-goto-agenda)
  3191. (add-hook 'calendar-mode-hook
  3192. (lambda ()
  3193. (unless (eq org-agenda-diary-file 'diary-file)
  3194. (define-key calendar-mode-map
  3195. org-calendar-insert-diary-entry-key
  3196. 'org-agenda-diary-entry))))))
  3197. (defgroup org-latex nil
  3198. "Options for embedding LaTeX code into Org-mode."
  3199. :tag "Org LaTeX"
  3200. :group 'org)
  3201. (defcustom org-format-latex-options
  3202. '(:foreground default :background default :scale 1.0
  3203. :html-foreground "Black" :html-background "Transparent"
  3204. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3205. "Options for creating images from LaTeX fragments.
  3206. This is a property list with the following properties:
  3207. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3208. `default' means use the foreground of the default face.
  3209. `auto' means use the foreground from the text face.
  3210. :background the background color, or \"Transparent\".
  3211. `default' means use the background of the default face.
  3212. `auto' means use the background from the text face.
  3213. :scale a scaling factor for the size of the images, to get more pixels
  3214. :html-foreground, :html-background, :html-scale
  3215. the same numbers for HTML export.
  3216. :matchers a list indicating which matchers should be used to
  3217. find LaTeX fragments. Valid members of this list are:
  3218. \"begin\" find environments
  3219. \"$1\" find single characters surrounded by $.$
  3220. \"$\" find math expressions surrounded by $...$
  3221. \"$$\" find math expressions surrounded by $$....$$
  3222. \"\\(\" find math expressions surrounded by \\(...\\)
  3223. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3224. :group 'org-latex
  3225. :type 'plist)
  3226. (defcustom org-format-latex-signal-error t
  3227. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3228. When nil, just push out a message."
  3229. :group 'org-latex
  3230. :version "24.1"
  3231. :type 'boolean)
  3232. (defcustom org-latex-to-mathml-jar-file nil
  3233. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3234. Use this to specify additional executable file say a jar file.
  3235. When using MathToWeb as the converter, specify the full-path to
  3236. your mathtoweb.jar file."
  3237. :group 'org-latex
  3238. :version "24.1"
  3239. :type '(choice
  3240. (const :tag "None" nil)
  3241. (file :tag "JAR file" :must-match t)))
  3242. (defcustom org-latex-to-mathml-convert-command nil
  3243. "Command to convert LaTeX fragments to MathML.
  3244. Replace format-specifiers in the command as noted below and use
  3245. `shell-command' to convert LaTeX to MathML.
  3246. %j: Executable file in fully expanded form as specified by
  3247. `org-latex-to-mathml-jar-file'.
  3248. %I: Input LaTeX file in fully expanded form
  3249. %o: Output MathML file
  3250. This command is used by `org-create-math-formula'.
  3251. When using MathToWeb as the converter, set this to
  3252. \"java -jar %j -unicode -force -df %o %I\"."
  3253. :group 'org-latex
  3254. :version "24.1"
  3255. :type '(choice
  3256. (const :tag "None" nil)
  3257. (string :tag "\nShell command")))
  3258. (defcustom org-latex-create-formula-image-program 'dvipng
  3259. "Program to convert LaTeX fragments with.
  3260. dvipng Process the LaTeX fragments to dvi file, then convert
  3261. dvi files to png files using dvipng.
  3262. This will also include processing of non-math environments.
  3263. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3264. to convert pdf files to png files"
  3265. :group 'org-latex
  3266. :version "24.1"
  3267. :type '(choice
  3268. (const :tag "dvipng" dvipng)
  3269. (const :tag "imagemagick" imagemagick)))
  3270. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3271. "Path to store latex preview images.
  3272. A relative path here creates many directories relative to the
  3273. processed org files paths. An absolute path puts all preview
  3274. images at the same place."
  3275. :group 'org-latex
  3276. :version "24.3"
  3277. :type 'string)
  3278. (defun org-format-latex-mathml-available-p ()
  3279. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3280. (save-match-data
  3281. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3282. org-latex-to-mathml-convert-command)
  3283. (let ((executable (car (split-string
  3284. org-latex-to-mathml-convert-command))))
  3285. (when (executable-find executable)
  3286. (if (string-match
  3287. "%j" org-latex-to-mathml-convert-command)
  3288. (file-readable-p org-latex-to-mathml-jar-file)
  3289. t))))))
  3290. (defcustom org-format-latex-header "\\documentclass{article}
  3291. \\usepackage[usenames]{color}
  3292. \[PACKAGES]
  3293. \[DEFAULT-PACKAGES]
  3294. \\pagestyle{empty} % do not remove
  3295. % The settings below are copied from fullpage.sty
  3296. \\setlength{\\textwidth}{\\paperwidth}
  3297. \\addtolength{\\textwidth}{-3cm}
  3298. \\setlength{\\oddsidemargin}{1.5cm}
  3299. \\addtolength{\\oddsidemargin}{-2.54cm}
  3300. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3301. \\setlength{\\textheight}{\\paperheight}
  3302. \\addtolength{\\textheight}{-\\headheight}
  3303. \\addtolength{\\textheight}{-\\headsep}
  3304. \\addtolength{\\textheight}{-\\footskip}
  3305. \\addtolength{\\textheight}{-3cm}
  3306. \\setlength{\\topmargin}{1.5cm}
  3307. \\addtolength{\\topmargin}{-2.54cm}"
  3308. "The document header used for processing LaTeX fragments.
  3309. It is imperative that this header make sure that no page number
  3310. appears on the page. The package defined in the variables
  3311. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3312. will either replace the placeholder \"[PACKAGES]\" in this
  3313. header, or they will be appended."
  3314. :group 'org-latex
  3315. :type 'string)
  3316. (defun org-set-packages-alist (var val)
  3317. "Set the packages alist and make sure it has 3 elements per entry."
  3318. (set var (mapcar (lambda (x)
  3319. (if (and (consp x) (= (length x) 2))
  3320. (list (car x) (nth 1 x) t)
  3321. x))
  3322. val)))
  3323. (defun org-get-packages-alist (var)
  3324. "Get the packages alist and make sure it has 3 elements per entry."
  3325. (mapcar (lambda (x)
  3326. (if (and (consp x) (= (length x) 2))
  3327. (list (car x) (nth 1 x) t)
  3328. x))
  3329. (default-value var)))
  3330. (defcustom org-latex-default-packages-alist
  3331. '(("AUTO" "inputenc" t)
  3332. ("T1" "fontenc" t)
  3333. ("" "fixltx2e" nil)
  3334. ("" "graphicx" t)
  3335. ("" "longtable" nil)
  3336. ("" "float" nil)
  3337. ("" "wrapfig" nil)
  3338. ("" "rotating" nil)
  3339. ("normalem" "ulem" t)
  3340. ("" "amsmath" t)
  3341. ("" "textcomp" t)
  3342. ("" "marvosym" t)
  3343. ("" "wasysym" t)
  3344. ("" "amssymb" t)
  3345. ("" "hyperref" nil)
  3346. "\\tolerance=1000")
  3347. "Alist of default packages to be inserted in the header.
  3348. Change this only if one of the packages here causes an
  3349. incompatibility with another package you are using.
  3350. The packages in this list are needed by one part or another of
  3351. Org mode to function properly:
  3352. - inputenc, fontenc: for basic font and character selection
  3353. - fixltx2e: Important patches of LaTeX itself
  3354. - graphicx: for including images
  3355. - longtable: For multipage tables
  3356. - float, wrapfig: for figure placement
  3357. - rotating: for sideways figures and tables
  3358. - ulem: for underline and strike-through
  3359. - amsmath: for subscript and superscript and math environments
  3360. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3361. for interpreting the entities in `org-entities'. You can skip
  3362. some of these packages if you don't use any of their symbols.
  3363. - hyperref: for cross references
  3364. Therefore you should not modify this variable unless you know
  3365. what you are doing. The one reason to change it anyway is that
  3366. you might be loading some other package that conflicts with one
  3367. of the default packages. Each cell is of the format
  3368. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3369. the package also needs to be included when compiling LaTeX
  3370. snippets into images for inclusion into non-LaTeX output."
  3371. :group 'org-latex
  3372. :group 'org-export-latex
  3373. :set 'org-set-packages-alist
  3374. :get 'org-get-packages-alist
  3375. :version "24.1"
  3376. :type '(repeat
  3377. (choice
  3378. (list :tag "options/package pair"
  3379. (string :tag "options")
  3380. (string :tag "package")
  3381. (boolean :tag "Snippet"))
  3382. (string :tag "A line of LaTeX"))))
  3383. (defcustom org-latex-packages-alist nil
  3384. "Alist of packages to be inserted in every LaTeX header.
  3385. These will be inserted after `org-latex-default-packages-alist'.
  3386. Each cell is of the format:
  3387. \(\"options\" \"package\" snippet-flag)
  3388. SNIPPET-FLAG, when t, indicates that this package is also needed
  3389. when turning LaTeX snippets into images for inclusion into
  3390. non-LaTeX output.
  3391. Make sure that you only list packages here which:
  3392. - you want in every file
  3393. - do not conflict with the setup in `org-format-latex-header'.
  3394. - do not conflict with the default packages in
  3395. `org-latex-default-packages-alist'."
  3396. :group 'org-latex
  3397. :group 'org-export-latex
  3398. :set 'org-set-packages-alist
  3399. :get 'org-get-packages-alist
  3400. :type '(repeat
  3401. (choice
  3402. (list :tag "options/package pair"
  3403. (string :tag "options")
  3404. (string :tag "package")
  3405. (boolean :tag "Snippet"))
  3406. (string :tag "A line of LaTeX"))))
  3407. (defgroup org-appearance nil
  3408. "Settings for Org-mode appearance."
  3409. :tag "Org Appearance"
  3410. :group 'org)
  3411. (defcustom org-level-color-stars-only nil
  3412. "Non-nil means fontify only the stars in each headline.
  3413. When nil, the entire headline is fontified.
  3414. Changing it requires restart of `font-lock-mode' to become effective
  3415. also in regions already fontified."
  3416. :group 'org-appearance
  3417. :type 'boolean)
  3418. (defcustom org-hide-leading-stars nil
  3419. "Non-nil means hide the first N-1 stars in a headline.
  3420. This works by using the face `org-hide' for these stars. This
  3421. face is white for a light background, and black for a dark
  3422. background. You may have to customize the face `org-hide' to
  3423. make this work.
  3424. Changing it requires restart of `font-lock-mode' to become effective
  3425. also in regions already fontified.
  3426. You may also set this on a per-file basis by adding one of the following
  3427. lines to the buffer:
  3428. #+STARTUP: hidestars
  3429. #+STARTUP: showstars"
  3430. :group 'org-appearance
  3431. :type 'boolean)
  3432. (defcustom org-hidden-keywords nil
  3433. "List of symbols corresponding to keywords to be hidden the org buffer.
  3434. For example, a value '(title) for this list will make the document's title
  3435. appear in the buffer without the initial #+TITLE: keyword."
  3436. :group 'org-appearance
  3437. :version "24.1"
  3438. :type '(set (const :tag "#+AUTHOR" author)
  3439. (const :tag "#+DATE" date)
  3440. (const :tag "#+EMAIL" email)
  3441. (const :tag "#+TITLE" title)))
  3442. (defcustom org-custom-properties nil
  3443. "List of properties (as strings) with a special meaning.
  3444. The default use of these custom properties is to let the user
  3445. hide them with `org-toggle-custom-properties-visibility'."
  3446. :group 'org-properties
  3447. :group 'org-appearance
  3448. :version "24.3"
  3449. :type '(repeat (string :tag "Property Name")))
  3450. (defcustom org-fontify-done-headline nil
  3451. "Non-nil means change the face of a headline if it is marked DONE.
  3452. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3453. When this is non-nil, the headline after the keyword is set to the
  3454. `org-headline-done' as an additional indication."
  3455. :group 'org-appearance
  3456. :type 'boolean)
  3457. (defcustom org-fontify-emphasized-text t
  3458. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3459. Changing this variable requires a restart of Emacs to take effect."
  3460. :group 'org-appearance
  3461. :type 'boolean)
  3462. (defcustom org-fontify-whole-heading-line nil
  3463. "Non-nil means fontify the whole line for headings.
  3464. This is useful when setting a background color for the
  3465. org-level-* faces."
  3466. :group 'org-appearance
  3467. :type 'boolean)
  3468. (defcustom org-highlight-latex-and-related nil
  3469. "Non-nil means highlight LaTeX related syntax in the buffer.
  3470. When non nil, the value should be a list containing any of the
  3471. following symbols:
  3472. `latex' Highlight LaTeX snippets and environments.
  3473. `script' Highlight subscript and superscript.
  3474. `entities' Highlight entities."
  3475. :group 'org-appearance
  3476. :version "24.4"
  3477. :package-version '(Org . "8.0")
  3478. :type '(choice
  3479. (const :tag "No highlighting" nil)
  3480. (set :greedy t :tag "Highlight"
  3481. (const :tag "LaTeX snippets and environments" latex)
  3482. (const :tag "Subscript and superscript" script)
  3483. (const :tag "Entities" entities))))
  3484. (defcustom org-hide-emphasis-markers nil
  3485. "Non-nil mean font-lock should hide the emphasis marker characters."
  3486. :group 'org-appearance
  3487. :type 'boolean)
  3488. (defcustom org-pretty-entities nil
  3489. "Non-nil means show entities as UTF8 characters.
  3490. When nil, the \\name form remains in the buffer."
  3491. :group 'org-appearance
  3492. :version "24.1"
  3493. :type 'boolean)
  3494. (defcustom org-pretty-entities-include-sub-superscripts t
  3495. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3496. :group 'org-appearance
  3497. :version "24.1"
  3498. :type 'boolean)
  3499. (defvar org-emph-re nil
  3500. "Regular expression for matching emphasis.
  3501. After a match, the match groups contain these elements:
  3502. 0 The match of the full regular expression, including the characters
  3503. before and after the proper match
  3504. 1 The character before the proper match, or empty at beginning of line
  3505. 2 The proper match, including the leading and trailing markers
  3506. 3 The leading marker like * or /, indicating the type of highlighting
  3507. 4 The text between the emphasis markers, not including the markers
  3508. 5 The character after the match, empty at the end of a line")
  3509. (defvar org-verbatim-re nil
  3510. "Regular expression for matching verbatim text.")
  3511. (defvar org-emphasis-regexp-components) ; defined just below
  3512. (defvar org-emphasis-alist) ; defined just below
  3513. (defun org-set-emph-re (var val)
  3514. "Set variable and compute the emphasis regular expression."
  3515. (set var val)
  3516. (when (and (boundp 'org-emphasis-alist)
  3517. (boundp 'org-emphasis-regexp-components)
  3518. org-emphasis-alist org-emphasis-regexp-components)
  3519. (let* ((e org-emphasis-regexp-components)
  3520. (pre (car e))
  3521. (post (nth 1 e))
  3522. (border (nth 2 e))
  3523. (body (nth 3 e))
  3524. (nl (nth 4 e))
  3525. (body1 (concat body "*?"))
  3526. (markers (mapconcat 'car org-emphasis-alist ""))
  3527. (vmarkers (mapconcat
  3528. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3529. org-emphasis-alist "")))
  3530. ;; make sure special characters appear at the right position in the class
  3531. (if (string-match "\\^" markers)
  3532. (setq markers (concat (replace-match "" t t markers) "^")))
  3533. (if (string-match "-" markers)
  3534. (setq markers (concat (replace-match "" t t markers) "-")))
  3535. (if (string-match "\\^" vmarkers)
  3536. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3537. (if (string-match "-" vmarkers)
  3538. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3539. (if (> nl 0)
  3540. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3541. (int-to-string nl) "\\}")))
  3542. ;; Make the regexp
  3543. (setq org-emph-re
  3544. (concat "\\([" pre "]\\|^\\)"
  3545. "\\("
  3546. "\\([" markers "]\\)"
  3547. "\\("
  3548. "[^" border "]\\|"
  3549. "[^" border "]"
  3550. body1
  3551. "[^" border "]"
  3552. "\\)"
  3553. "\\3\\)"
  3554. "\\([" post "]\\|$\\)"))
  3555. (setq org-verbatim-re
  3556. (concat "\\([" pre "]\\|^\\)"
  3557. "\\("
  3558. "\\([" vmarkers "]\\)"
  3559. "\\("
  3560. "[^" border "]\\|"
  3561. "[^" border "]"
  3562. body1
  3563. "[^" border "]"
  3564. "\\)"
  3565. "\\3\\)"
  3566. "\\([" post "]\\|$\\)")))))
  3567. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3568. ;; set this option proved cumbersome. See this message/thread:
  3569. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3570. (defvar org-emphasis-regexp-components
  3571. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3572. "Components used to build the regular expression for emphasis.
  3573. This is a list with five entries. Terminology: In an emphasis string
  3574. like \" *strong word* \", we call the initial space PREMATCH, the final
  3575. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3576. and \"trong wor\" is the body. The different components in this variable
  3577. specify what is allowed/forbidden in each part:
  3578. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3579. post Chars allowed as postmatch. End of line will be allowed too.
  3580. border The chars *forbidden* as border characters.
  3581. body-regexp A regexp like \".\" to match a body character. Don't use
  3582. non-shy groups here, and don't allow newline here.
  3583. newline The maximum number of newlines allowed in an emphasis exp.
  3584. You need to reload Org or to restart Emacs after customizing this.")
  3585. (defcustom org-emphasis-alist
  3586. `(("*" bold)
  3587. ("/" italic)
  3588. ("_" underline)
  3589. ("=" org-code verbatim)
  3590. ("~" org-verbatim verbatim)
  3591. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3592. "Alist of characters and faces to emphasize text.
  3593. Text starting and ending with a special character will be emphasized,
  3594. for example *bold*, _underlined_ and /italic/. This variable sets the
  3595. marker characters and the face to be used by font-lock for highlighting
  3596. in Org-mode Emacs buffers.
  3597. You need to reload Org or to restart Emacs after customizing this."
  3598. :group 'org-appearance
  3599. :set 'org-set-emph-re
  3600. :version "24.4"
  3601. :package-version '(Org . "8.0")
  3602. :type '(repeat
  3603. (list
  3604. (string :tag "Marker character")
  3605. (choice
  3606. (face :tag "Font-lock-face")
  3607. (plist :tag "Face property list"))
  3608. (option (const verbatim)))))
  3609. (defvar org-protecting-blocks
  3610. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3611. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3612. This is needed for font-lock setup.")
  3613. ;;; Miscellaneous options
  3614. (defgroup org-completion nil
  3615. "Completion in Org-mode."
  3616. :tag "Org Completion"
  3617. :group 'org)
  3618. (defcustom org-completion-use-ido nil
  3619. "Non-nil means use ido completion wherever possible.
  3620. Note that `ido-mode' must be active for this variable to be relevant.
  3621. If you decide to turn this variable on, you might well want to turn off
  3622. `org-outline-path-complete-in-steps'.
  3623. See also `org-completion-use-iswitchb'."
  3624. :group 'org-completion
  3625. :type 'boolean)
  3626. (defcustom org-completion-use-iswitchb nil
  3627. "Non-nil means use iswitchb completion wherever possible.
  3628. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3629. If you decide to turn this variable on, you might well want to turn off
  3630. `org-outline-path-complete-in-steps'.
  3631. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3632. :group 'org-completion
  3633. :type 'boolean)
  3634. (defcustom org-completion-fallback-command 'hippie-expand
  3635. "The expansion command called by \\[pcomplete] in normal context.
  3636. Normal means, no org-mode-specific context."
  3637. :group 'org-completion
  3638. :type 'function)
  3639. ;;; Functions and variables from their packages
  3640. ;; Declared here to avoid compiler warnings
  3641. ;; XEmacs only
  3642. (defvar outline-mode-menu-heading)
  3643. (defvar outline-mode-menu-show)
  3644. (defvar outline-mode-menu-hide)
  3645. (defvar zmacs-regions) ; XEmacs regions
  3646. ;; Emacs only
  3647. (defvar mark-active)
  3648. ;; Various packages
  3649. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3650. (declare-function calendar-forward-day "cal-move" (arg))
  3651. (declare-function calendar-goto-date "cal-move" (date))
  3652. (declare-function calendar-goto-today "cal-move" ())
  3653. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3654. (defvar calc-embedded-close-formula)
  3655. (defvar calc-embedded-open-formula)
  3656. (declare-function cdlatex-tab "ext:cdlatex" ())
  3657. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3658. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3659. (defvar font-lock-unfontify-region-function)
  3660. (declare-function iswitchb-read-buffer "iswitchb"
  3661. (prompt &optional default require-match start matches-set))
  3662. (defvar iswitchb-temp-buflist)
  3663. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3664. (defvar org-agenda-tags-todo-honor-ignore-options)
  3665. (declare-function org-agenda-skip "org-agenda" ())
  3666. (declare-function
  3667. org-agenda-format-item "org-agenda"
  3668. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3669. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3670. (declare-function org-agenda-change-all-lines "org-agenda"
  3671. (newhead hdmarker &optional fixface just-this))
  3672. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3673. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3674. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3675. (beg end))
  3676. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3677. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3678. "org-agenda" (&optional end))
  3679. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3680. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3681. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3682. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3683. (declare-function org-indent-mode "org-indent" (&optional arg))
  3684. (declare-function parse-time-string "parse-time" (string))
  3685. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3686. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3687. (defvar remember-data-file)
  3688. (defvar texmathp-why)
  3689. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3690. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3691. (defvar org-latex-regexps)
  3692. ;;; Autoload and prepare some org modules
  3693. ;; Some table stuff that needs to be defined here, because it is used
  3694. ;; by the functions setting up org-mode or checking for table context.
  3695. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3696. "Detect an org-type or table-type table.")
  3697. (defconst org-table-line-regexp "^[ \t]*|"
  3698. "Detect an org-type table line.")
  3699. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3700. "Detect an org-type table line.")
  3701. (defconst org-table-hline-regexp "^[ \t]*|-"
  3702. "Detect an org-type table hline.")
  3703. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3704. "Detect a table-type table hline.")
  3705. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3706. "Detect the first line outside a table when searching from within it.
  3707. This works for both table types.")
  3708. ;; Autoload the functions in org-table.el that are needed by functions here.
  3709. (eval-and-compile
  3710. (org-autoload "org-table"
  3711. '(org-table-begin org-table-blank-field org-table-end)))
  3712. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3713. "Detect a #+TBLFM line.")
  3714. ;;;###autoload
  3715. (defun turn-on-orgtbl ()
  3716. "Unconditionally turn on `orgtbl-mode'."
  3717. (require 'org-table)
  3718. (orgtbl-mode 1))
  3719. (defun org-at-table-p (&optional table-type)
  3720. "Return t if the cursor is inside an org-type table.
  3721. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3722. (if org-enable-table-editor
  3723. (save-excursion
  3724. (beginning-of-line 1)
  3725. (looking-at (if table-type org-table-any-line-regexp
  3726. org-table-line-regexp)))
  3727. nil))
  3728. (defsubst org-table-p () (org-at-table-p))
  3729. (defun org-at-table.el-p ()
  3730. "Return t if and only if we are at a table.el table."
  3731. (and (org-at-table-p 'any)
  3732. (save-excursion
  3733. (goto-char (org-table-begin 'any))
  3734. (looking-at org-table1-hline-regexp))))
  3735. (defun org-table-recognize-table.el ()
  3736. "If there is a table.el table nearby, recognize it and move into it."
  3737. (if org-table-tab-recognizes-table.el
  3738. (if (org-at-table.el-p)
  3739. (progn
  3740. (beginning-of-line 1)
  3741. (if (looking-at org-table-dataline-regexp)
  3742. nil
  3743. (if (looking-at org-table1-hline-regexp)
  3744. (progn
  3745. (beginning-of-line 2)
  3746. (if (looking-at org-table-any-border-regexp)
  3747. (beginning-of-line -1)))))
  3748. (if (re-search-forward "|" (org-table-end t) t)
  3749. (progn
  3750. (require 'table)
  3751. (if (table--at-cell-p (point))
  3752. t
  3753. (message "recognizing table.el table...")
  3754. (table-recognize-table)
  3755. (message "recognizing table.el table...done")))
  3756. (error "This should not happen"))
  3757. t)
  3758. nil)
  3759. nil))
  3760. (defun org-at-table-hline-p ()
  3761. "Return t if the cursor is inside a hline in a table."
  3762. (if org-enable-table-editor
  3763. (save-excursion
  3764. (beginning-of-line 1)
  3765. (looking-at org-table-hline-regexp))
  3766. nil))
  3767. (defun org-table-map-tables (function &optional quietly)
  3768. "Apply FUNCTION to the start of all tables in the buffer."
  3769. (save-excursion
  3770. (save-restriction
  3771. (widen)
  3772. (goto-char (point-min))
  3773. (while (re-search-forward org-table-any-line-regexp nil t)
  3774. (unless quietly
  3775. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3776. (beginning-of-line 1)
  3777. (when (and (looking-at org-table-line-regexp)
  3778. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3779. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3780. (save-excursion (funcall function))
  3781. (or (looking-at org-table-line-regexp)
  3782. (forward-char 1)))
  3783. (re-search-forward org-table-any-border-regexp nil 1))))
  3784. (unless quietly (message "Mapping tables: done")))
  3785. ;; Declare and autoload functions from org-agenda.el
  3786. (eval-and-compile
  3787. (org-autoload "org-agenda"
  3788. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3789. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3790. (declare-function org-clock-update-mode-line "org-clock" ())
  3791. (declare-function org-resolve-clocks "org-clock"
  3792. (&optional also-non-dangling-p prompt last-valid))
  3793. (defun org-at-TBLFM-p (&optional pos)
  3794. "Return t when point (or POS) is in #+TBLFM line."
  3795. (save-excursion
  3796. (let ((pos pos)))
  3797. (goto-char (or pos (point)))
  3798. (beginning-of-line 1)
  3799. (looking-at org-TBLFM-regexp)))
  3800. (defvar org-clock-start-time)
  3801. (defvar org-clock-marker (make-marker)
  3802. "Marker recording the last clock-in.")
  3803. (defvar org-clock-hd-marker (make-marker)
  3804. "Marker recording the last clock-in, but the headline position.")
  3805. (defvar org-clock-heading ""
  3806. "The heading of the current clock entry.")
  3807. (defun org-clock-is-active ()
  3808. "Return the buffer where the clock is currently running.
  3809. Return nil if no clock is running."
  3810. (marker-buffer org-clock-marker))
  3811. (eval-and-compile
  3812. (org-autoload "org-clock" '(org-clock-remove-overlays
  3813. org-clock-update-time-maybe
  3814. org-clocktable-shift)))
  3815. (defun org-check-running-clock ()
  3816. "Check if the current buffer contains the running clock.
  3817. If yes, offer to stop it and to save the buffer with the changes."
  3818. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3819. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3820. (buffer-name))))
  3821. (org-clock-out)
  3822. (when (y-or-n-p "Save changed buffer?")
  3823. (save-buffer))))
  3824. (defun org-clocktable-try-shift (dir n)
  3825. "Check if this line starts a clock table, if yes, shift the time block."
  3826. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3827. (org-clocktable-shift dir n)))
  3828. ;;;###autoload
  3829. (defun org-clock-persistence-insinuate ()
  3830. "Set up hooks for clock persistence."
  3831. (require 'org-clock)
  3832. (add-hook 'org-mode-hook 'org-clock-load)
  3833. (add-hook 'kill-emacs-hook 'org-clock-save))
  3834. ;; Define the variable already here, to make sure we have it.
  3835. (defvar org-indent-mode nil
  3836. "Non-nil if Org-Indent mode is enabled.
  3837. Use the command `org-indent-mode' to change this variable.")
  3838. ;; Autoload archiving code
  3839. ;; The stuff that is needed for cycling and tags has to be defined here.
  3840. (defgroup org-archive nil
  3841. "Options concerning archiving in Org-mode."
  3842. :tag "Org Archive"
  3843. :group 'org-structure)
  3844. (defcustom org-archive-location "%s_archive::"
  3845. "The location where subtrees should be archived.
  3846. The value of this variable is a string, consisting of two parts,
  3847. separated by a double-colon. The first part is a filename and
  3848. the second part is a headline.
  3849. When the filename is omitted, archiving happens in the same file.
  3850. %s in the filename will be replaced by the current file
  3851. name (without the directory part). Archiving to a different file
  3852. is useful to keep archived entries from contributing to the
  3853. Org-mode Agenda.
  3854. The archived entries will be filed as subtrees of the specified
  3855. headline. When the headline is omitted, the subtrees are simply
  3856. filed away at the end of the file, as top-level entries. Also in
  3857. the heading you can use %s to represent the file name, this can be
  3858. useful when using the same archive for a number of different files.
  3859. Here are a few examples:
  3860. \"%s_archive::\"
  3861. If the current file is Projects.org, archive in file
  3862. Projects.org_archive, as top-level trees. This is the default.
  3863. \"::* Archived Tasks\"
  3864. Archive in the current file, under the top-level headline
  3865. \"* Archived Tasks\".
  3866. \"~/org/archive.org::\"
  3867. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3868. \"~/org/archive.org::* From %s\"
  3869. Archive in file ~/org/archive.org (absolute path), under headlines
  3870. \"From FILENAME\" where file name is the current file name.
  3871. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3872. The \"datetree/\" string is special, signifying to archive
  3873. items to the datetree. Items are placed in either the CLOSED
  3874. date of the item, or the current date if there is no CLOSED date.
  3875. The heading will be a subentry to the current date. There doesn't
  3876. need to be a heading, but there always needs to be a slash after
  3877. datetree. For example, to store archived items directly in the
  3878. datetree, use \"~/org/datetree.org::datetree/\".
  3879. \"basement::** Finished Tasks\"
  3880. Archive in file ./basement (relative path), as level 3 trees
  3881. below the level 2 heading \"** Finished Tasks\".
  3882. You may set this option on a per-file basis by adding to the buffer a
  3883. line like
  3884. #+ARCHIVE: basement::** Finished Tasks
  3885. You may also define it locally for a subtree by setting an ARCHIVE property
  3886. in the entry. If such a property is found in an entry, or anywhere up
  3887. the hierarchy, it will be used."
  3888. :group 'org-archive
  3889. :type 'string)
  3890. (defcustom org-archive-tag "ARCHIVE"
  3891. "The tag that marks a subtree as archived.
  3892. An archived subtree does not open during visibility cycling, and does
  3893. not contribute to the agenda listings.
  3894. After changing this, font-lock must be restarted in the relevant buffers to
  3895. get the proper fontification."
  3896. :group 'org-archive
  3897. :group 'org-keywords
  3898. :type 'string)
  3899. (defcustom org-agenda-skip-archived-trees t
  3900. "Non-nil means the agenda will skip any items located in archived trees.
  3901. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3902. variable is no longer recommended, you should leave it at the value t.
  3903. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3904. :group 'org-archive
  3905. :group 'org-agenda-skip
  3906. :type 'boolean)
  3907. (defcustom org-columns-skip-archived-trees t
  3908. "Non-nil means ignore archived trees when creating column view."
  3909. :group 'org-archive
  3910. :group 'org-properties
  3911. :type 'boolean)
  3912. (defcustom org-cycle-open-archived-trees nil
  3913. "Non-nil means `org-cycle' will open archived trees.
  3914. An archived tree is a tree marked with the tag ARCHIVE.
  3915. When nil, archived trees will stay folded. You can still open them with
  3916. normal outline commands like `show-all', but not with the cycling commands."
  3917. :group 'org-archive
  3918. :group 'org-cycle
  3919. :type 'boolean)
  3920. (defcustom org-sparse-tree-open-archived-trees nil
  3921. "Non-nil means sparse tree construction shows matches in archived trees.
  3922. When nil, matches in these trees are highlighted, but the trees are kept in
  3923. collapsed state."
  3924. :group 'org-archive
  3925. :group 'org-sparse-trees
  3926. :type 'boolean)
  3927. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3928. "The default date type when building a sparse tree.
  3929. When this is nil, a date is a scheduled or a deadline timestamp.
  3930. Otherwise, these types are allowed:
  3931. all: all timestamps
  3932. active: only active timestamps (<...>)
  3933. inactive: only inactive timestamps (<...)
  3934. scheduled: only scheduled timestamps
  3935. deadline: only deadline timestamps"
  3936. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3937. (const :tag "All timestamps" all)
  3938. (const :tag "Only active timestamps" active)
  3939. (const :tag "Only inactive timestamps" inactive)
  3940. (const :tag "Only scheduled timestamps" scheduled)
  3941. (const :tag "Only deadline timestamps" deadline)
  3942. (const :tag "Only closed timestamps" closed))
  3943. :version "24.3"
  3944. :group 'org-sparse-trees)
  3945. (defun org-cycle-hide-archived-subtrees (state)
  3946. "Re-hide all archived subtrees after a visibility state change."
  3947. (when (and (not org-cycle-open-archived-trees)
  3948. (not (memq state '(overview folded))))
  3949. (save-excursion
  3950. (let* ((globalp (memq state '(contents all)))
  3951. (beg (if globalp (point-min) (point)))
  3952. (end (if globalp (point-max) (org-end-of-subtree t))))
  3953. (org-hide-archived-subtrees beg end)
  3954. (goto-char beg)
  3955. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3956. (message "%s" (substitute-command-keys
  3957. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3958. (defun org-force-cycle-archived ()
  3959. "Cycle subtree even if it is archived."
  3960. (interactive)
  3961. (setq this-command 'org-cycle)
  3962. (let ((org-cycle-open-archived-trees t))
  3963. (call-interactively 'org-cycle)))
  3964. (defun org-hide-archived-subtrees (beg end)
  3965. "Re-hide all archived subtrees after a visibility state change."
  3966. (save-excursion
  3967. (let* ((re (concat ":" org-archive-tag ":")))
  3968. (goto-char beg)
  3969. (while (re-search-forward re end t)
  3970. (when (org-at-heading-p)
  3971. (org-flag-subtree t)
  3972. (org-end-of-subtree t))))))
  3973. (declare-function outline-end-of-heading "outline" ())
  3974. (declare-function outline-flag-region "outline" (from to flag))
  3975. (defun org-flag-subtree (flag)
  3976. (save-excursion
  3977. (org-back-to-heading t)
  3978. (outline-end-of-heading)
  3979. (outline-flag-region (point)
  3980. (progn (org-end-of-subtree t) (point))
  3981. flag)))
  3982. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3983. (eval-and-compile
  3984. (org-autoload "org-archive"
  3985. '(org-add-archive-files)))
  3986. ;; Autoload Column View Code
  3987. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3988. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3989. (declare-function org-columns-compute "org-colview" (property))
  3990. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3991. '(org-columns-number-to-string
  3992. org-columns-get-format-and-top-level
  3993. org-columns-compute
  3994. org-columns-remove-overlays))
  3995. ;; Autoload ID code
  3996. (declare-function org-id-store-link "org-id")
  3997. (declare-function org-id-locations-load "org-id")
  3998. (declare-function org-id-locations-save "org-id")
  3999. (defvar org-id-track-globally)
  4000. (org-autoload "org-id"
  4001. '(org-id-new
  4002. org-id-copy
  4003. org-id-get-with-outline-path-completion
  4004. org-id-get-with-outline-drilling))
  4005. ;;; Variables for pre-computed regular expressions, all buffer local
  4006. (defvar org-todo-regexp nil
  4007. "Matches any of the TODO state keywords.")
  4008. (make-variable-buffer-local 'org-todo-regexp)
  4009. (defvar org-not-done-regexp nil
  4010. "Matches any of the TODO state keywords except the last one.")
  4011. (make-variable-buffer-local 'org-not-done-regexp)
  4012. (defvar org-not-done-heading-regexp nil
  4013. "Matches a TODO headline that is not done.")
  4014. (make-variable-buffer-local 'org-not-done-regexp)
  4015. (defvar org-todo-line-regexp nil
  4016. "Matches a headline and puts TODO state into group 2 if present.")
  4017. (make-variable-buffer-local 'org-todo-line-regexp)
  4018. (defvar org-complex-heading-regexp nil
  4019. "Matches a headline and puts everything into groups:
  4020. group 1: the stars
  4021. group 2: The todo keyword, maybe
  4022. group 3: Priority cookie
  4023. group 4: True headline
  4024. group 5: Tags")
  4025. (make-variable-buffer-local 'org-complex-heading-regexp)
  4026. (defvar org-complex-heading-regexp-format nil
  4027. "Printf format to make regexp to match an exact headline.
  4028. This regexp will match the headline of any node which has the
  4029. exact headline text that is put into the format, but may have any
  4030. TODO state, priority and tags.")
  4031. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4032. (defvar org-todo-line-tags-regexp nil
  4033. "Matches a headline and puts TODO state into group 2 if present.
  4034. Also put tags into group 4 if tags are present.")
  4035. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4036. (defvar org-ds-keyword-length 12
  4037. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4038. (make-variable-buffer-local 'org-ds-keyword-length)
  4039. (defvar org-deadline-regexp nil
  4040. "Matches the DEADLINE keyword.")
  4041. (make-variable-buffer-local 'org-deadline-regexp)
  4042. (defvar org-deadline-time-regexp nil
  4043. "Matches the DEADLINE keyword together with a time stamp.")
  4044. (make-variable-buffer-local 'org-deadline-time-regexp)
  4045. (defvar org-deadline-time-hour-regexp nil
  4046. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4047. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4048. (defvar org-deadline-line-regexp nil
  4049. "Matches the DEADLINE keyword and the rest of the line.")
  4050. (make-variable-buffer-local 'org-deadline-line-regexp)
  4051. (defvar org-scheduled-regexp nil
  4052. "Matches the SCHEDULED keyword.")
  4053. (make-variable-buffer-local 'org-scheduled-regexp)
  4054. (defvar org-scheduled-time-regexp nil
  4055. "Matches the SCHEDULED keyword together with a time stamp.")
  4056. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4057. (defvar org-scheduled-time-hour-regexp nil
  4058. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4059. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4060. (defvar org-closed-time-regexp nil
  4061. "Matches the CLOSED keyword together with a time stamp.")
  4062. (make-variable-buffer-local 'org-closed-time-regexp)
  4063. (defvar org-keyword-time-regexp nil
  4064. "Matches any of the 4 keywords, together with the time stamp.")
  4065. (make-variable-buffer-local 'org-keyword-time-regexp)
  4066. (defvar org-keyword-time-not-clock-regexp nil
  4067. "Matches any of the 3 keywords, together with the time stamp.")
  4068. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4069. (defvar org-maybe-keyword-time-regexp nil
  4070. "Matches a timestamp, possibly preceded by a keyword.")
  4071. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4072. (defvar org-all-time-keywords nil
  4073. "List of time keywords.")
  4074. (make-variable-buffer-local 'org-all-time-keywords)
  4075. (defconst org-plain-time-of-day-regexp
  4076. (concat
  4077. "\\(\\<[012]?[0-9]"
  4078. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4079. "\\(--?"
  4080. "\\(\\<[012]?[0-9]"
  4081. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4082. "\\)?")
  4083. "Regular expression to match a plain time or time range.
  4084. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4085. groups carry important information:
  4086. 0 the full match
  4087. 1 the first time, range or not
  4088. 8 the second time, if it is a range.")
  4089. (defconst org-plain-time-extension-regexp
  4090. (concat
  4091. "\\(\\<[012]?[0-9]"
  4092. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4093. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4094. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4095. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4096. groups carry important information:
  4097. 0 the full match
  4098. 7 hours of duration
  4099. 9 minutes of duration")
  4100. (defconst org-stamp-time-of-day-regexp
  4101. (concat
  4102. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4103. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4104. "\\(--?"
  4105. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4106. "Regular expression to match a timestamp time or time range.
  4107. After a match, the following groups carry important information:
  4108. 0 the full match
  4109. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4110. 2 the first time, range or not
  4111. 4 the second time, if it is a range.")
  4112. (defconst org-startup-options
  4113. '(("fold" org-startup-folded t)
  4114. ("overview" org-startup-folded t)
  4115. ("nofold" org-startup-folded nil)
  4116. ("showall" org-startup-folded nil)
  4117. ("showeverything" org-startup-folded showeverything)
  4118. ("content" org-startup-folded content)
  4119. ("indent" org-startup-indented t)
  4120. ("noindent" org-startup-indented nil)
  4121. ("hidestars" org-hide-leading-stars t)
  4122. ("showstars" org-hide-leading-stars nil)
  4123. ("odd" org-odd-levels-only t)
  4124. ("oddeven" org-odd-levels-only nil)
  4125. ("align" org-startup-align-all-tables t)
  4126. ("noalign" org-startup-align-all-tables nil)
  4127. ("inlineimages" org-startup-with-inline-images t)
  4128. ("noinlineimages" org-startup-with-inline-images nil)
  4129. ("latexpreview" org-startup-with-latex-preview t)
  4130. ("nolatexpreview" org-startup-with-latex-preview nil)
  4131. ("customtime" org-display-custom-times t)
  4132. ("logdone" org-log-done time)
  4133. ("lognotedone" org-log-done note)
  4134. ("nologdone" org-log-done nil)
  4135. ("lognoteclock-out" org-log-note-clock-out t)
  4136. ("nolognoteclock-out" org-log-note-clock-out nil)
  4137. ("logrepeat" org-log-repeat state)
  4138. ("lognoterepeat" org-log-repeat note)
  4139. ("logdrawer" org-log-into-drawer t)
  4140. ("nologdrawer" org-log-into-drawer nil)
  4141. ("logstatesreversed" org-log-states-order-reversed t)
  4142. ("nologstatesreversed" org-log-states-order-reversed nil)
  4143. ("nologrepeat" org-log-repeat nil)
  4144. ("logreschedule" org-log-reschedule time)
  4145. ("lognotereschedule" org-log-reschedule note)
  4146. ("nologreschedule" org-log-reschedule nil)
  4147. ("logredeadline" org-log-redeadline time)
  4148. ("lognoteredeadline" org-log-redeadline note)
  4149. ("nologredeadline" org-log-redeadline nil)
  4150. ("logrefile" org-log-refile time)
  4151. ("lognoterefile" org-log-refile note)
  4152. ("nologrefile" org-log-refile nil)
  4153. ("fninline" org-footnote-define-inline t)
  4154. ("nofninline" org-footnote-define-inline nil)
  4155. ("fnlocal" org-footnote-section nil)
  4156. ("fnauto" org-footnote-auto-label t)
  4157. ("fnprompt" org-footnote-auto-label nil)
  4158. ("fnconfirm" org-footnote-auto-label confirm)
  4159. ("fnplain" org-footnote-auto-label plain)
  4160. ("fnadjust" org-footnote-auto-adjust t)
  4161. ("nofnadjust" org-footnote-auto-adjust nil)
  4162. ("constcgs" constants-unit-system cgs)
  4163. ("constSI" constants-unit-system SI)
  4164. ("noptag" org-tag-persistent-alist nil)
  4165. ("hideblocks" org-hide-block-startup t)
  4166. ("nohideblocks" org-hide-block-startup nil)
  4167. ("beamer" org-startup-with-beamer-mode t)
  4168. ("entitiespretty" org-pretty-entities t)
  4169. ("entitiesplain" org-pretty-entities nil))
  4170. "Variable associated with STARTUP options for org-mode.
  4171. Each element is a list of three items: the startup options (as written
  4172. in the #+STARTUP line), the corresponding variable, and the value to set
  4173. this variable to if the option is found. An optional forth element PUSH
  4174. means to push this value onto the list in the variable.")
  4175. (defun org-update-property-plist (key val props)
  4176. "Update PROPS with KEY and VAL."
  4177. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4178. (key (if appending (substring key 0 (- (length key) 1)) key))
  4179. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4180. (previous (cdr (assoc key props))))
  4181. (if appending
  4182. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4183. (cons (cons key val) remainder))))
  4184. (defconst org-block-regexp
  4185. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4186. "Regular expression for hiding blocks.")
  4187. (defconst org-heading-keyword-regexp-format
  4188. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4189. "Printf format for a regexp matching a headline with some keyword.
  4190. This regexp will match the headline of any node which has the
  4191. exact keyword that is put into the format. The keyword isn't in
  4192. any group by default, but the stars and the body are.")
  4193. (defconst org-heading-keyword-maybe-regexp-format
  4194. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4195. "Printf format for a regexp matching a headline, possibly with some keyword.
  4196. This regexp can match any headline with the specified keyword, or
  4197. without a keyword. The keyword isn't in any group by default,
  4198. but the stars and the body are.")
  4199. (defcustom org-group-tags t
  4200. "When non-nil (the default), use group tags.
  4201. This can be turned on/off through `org-toggle-tags-groups'."
  4202. :group 'org-tags
  4203. :group 'org-startup
  4204. :type 'boolean)
  4205. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4206. (defun org-toggle-tags-groups ()
  4207. "Toggle support for group tags.
  4208. Support for group tags is controlled by the option
  4209. `org-group-tags', which is non-nil by default."
  4210. (interactive)
  4211. (setq org-group-tags (not org-group-tags))
  4212. (cond ((and (derived-mode-p 'org-agenda-mode)
  4213. org-group-tags)
  4214. (org-agenda-redo))
  4215. ((derived-mode-p 'org-mode)
  4216. (let ((org-inhibit-startup t)) (org-mode))))
  4217. (message "Groups tags support has been turned %s"
  4218. (if org-group-tags "on" "off")))
  4219. (defun org-set-regexps-and-options-for-tags ()
  4220. "Precompute variables used for tags."
  4221. (when (derived-mode-p 'org-mode)
  4222. (org-set-local 'org-file-tags nil)
  4223. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4224. (splitre "[ \t]+")
  4225. (start 0)
  4226. tags ftags key value)
  4227. (save-excursion
  4228. (save-restriction
  4229. (widen)
  4230. (goto-char (point-min))
  4231. (while (re-search-forward re nil t)
  4232. (setq key (upcase (org-match-string-no-properties 1))
  4233. value (org-match-string-no-properties 2))
  4234. (if (stringp value) (setq value (org-trim value)))
  4235. (cond
  4236. ((equal key "TAGS")
  4237. (setq tags (append tags (if tags '("\\n") nil)
  4238. (org-split-string value splitre))))
  4239. ((equal key "FILETAGS")
  4240. (when (string-match "\\S-" value)
  4241. (setq ftags
  4242. (append
  4243. ftags
  4244. (apply 'append
  4245. (mapcar (lambda (x) (org-split-string x ":"))
  4246. (org-split-string value)))))))))))
  4247. ;; Process the file tags.
  4248. (and ftags (org-set-local 'org-file-tags
  4249. (mapcar 'org-add-prop-inherited ftags)))
  4250. (org-set-local 'org-tag-groups-alist nil)
  4251. ;; Process the tags.
  4252. (when (and (not tags) org-tag-alist)
  4253. (setq tags
  4254. (mapcar
  4255. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4256. ((eq (car tg) :endgroup) "}")
  4257. ((eq (car tg) :grouptags) ":")
  4258. ((eq (car tg) :newline) "\n")
  4259. (t (concat (car tg)
  4260. (if (characterp (cdr tg))
  4261. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4262. org-tag-alist)))
  4263. (let (e tgs g)
  4264. (while (setq e (pop tags))
  4265. (cond
  4266. ((equal e "{")
  4267. (progn (push '(:startgroup) tgs)
  4268. (when (equal (nth 1 tags) ":")
  4269. (push (list (replace-regexp-in-string
  4270. "(.+)$" "" (nth 0 tags)))
  4271. org-tag-groups-alist)
  4272. (setq g 0))))
  4273. ((equal e ":") (push '(:grouptags) tgs))
  4274. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4275. ((equal e "\\n") (push '(:newline) tgs))
  4276. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4277. (push (cons (match-string 1 e)
  4278. (string-to-char (match-string 2 e))) tgs)
  4279. (if (and g (> g 0))
  4280. (setcar org-tag-groups-alist
  4281. (append (car org-tag-groups-alist)
  4282. (list (match-string 1 e)))))
  4283. (if g (setq g (1+ g))))
  4284. (t (push (list e) tgs)
  4285. (if (and g (> g 0))
  4286. (setcar org-tag-groups-alist
  4287. (append (car org-tag-groups-alist) (list e))))
  4288. (if g (setq g (1+ g))))))
  4289. (org-set-local 'org-tag-alist nil)
  4290. (while (setq e (pop tgs))
  4291. (or (and (stringp (car e))
  4292. (assoc (car e) org-tag-alist))
  4293. (push e org-tag-alist)))
  4294. ;; Return a list with tag variables
  4295. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4296. (defvar org-ota nil)
  4297. (defun org-set-regexps-and-options ()
  4298. "Precompute regular expressions used in the current buffer."
  4299. (when (derived-mode-p 'org-mode)
  4300. (org-set-local 'org-todo-kwd-alist nil)
  4301. (org-set-local 'org-todo-key-alist nil)
  4302. (org-set-local 'org-todo-key-trigger nil)
  4303. (org-set-local 'org-todo-keywords-1 nil)
  4304. (org-set-local 'org-done-keywords nil)
  4305. (org-set-local 'org-todo-heads nil)
  4306. (org-set-local 'org-todo-sets nil)
  4307. (org-set-local 'org-todo-log-states nil)
  4308. (org-set-local 'org-file-properties nil)
  4309. (let ((re (org-make-options-regexp
  4310. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4311. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4312. "SETUPFILE" "OPTIONS")
  4313. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4314. (splitre "[ \t]+")
  4315. (scripts org-use-sub-superscripts)
  4316. kwds kws0 kwsa key log value cat arch const links hw dws
  4317. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4318. (start 0))
  4319. (save-excursion
  4320. (save-restriction
  4321. (widen)
  4322. (goto-char (point-min))
  4323. (while
  4324. (or (and
  4325. ext-setup-or-nil
  4326. (not org-ota)
  4327. (let (ret)
  4328. (with-temp-buffer
  4329. (insert ext-setup-or-nil)
  4330. (let ((major-mode 'org-mode) org-ota)
  4331. (setq ret (save-match-data
  4332. (org-set-regexps-and-options-for-tags)))))
  4333. ;; Append setupfile tags to existing tags
  4334. (setq org-ota t)
  4335. (setq org-file-tags
  4336. (delq nil (append org-file-tags (nth 0 ret)))
  4337. org-tag-alist
  4338. (delq nil (append org-tag-alist (nth 1 ret)))
  4339. org-tag-groups-alist
  4340. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4341. (and ext-setup-or-nil
  4342. (string-match re ext-setup-or-nil start)
  4343. (setq start (match-end 0)))
  4344. (and (setq ext-setup-or-nil nil start 0)
  4345. (re-search-forward re nil t)))
  4346. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4347. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4348. (if (stringp value) (setq value (org-trim value)))
  4349. (cond
  4350. ((equal key "CATEGORY")
  4351. (setq cat value))
  4352. ((member key '("SEQ_TODO" "TODO"))
  4353. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4354. ((equal key "TYP_TODO")
  4355. (push (cons 'type (org-split-string value splitre)) kwds))
  4356. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4357. ;; general TODO-like setup
  4358. (push (cons (intern (downcase (match-string 1 key)))
  4359. (org-split-string value splitre)) kwds))
  4360. ((equal key "COLUMNS")
  4361. (org-set-local 'org-columns-default-format value))
  4362. ((equal key "LINK")
  4363. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4364. (push (cons (match-string 1 value)
  4365. (org-trim (match-string 2 value)))
  4366. links)))
  4367. ((equal key "PRIORITIES")
  4368. (setq prio (org-split-string value " +")))
  4369. ((equal key "PROPERTY")
  4370. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4371. (setq props (org-update-property-plist (match-string 1 value)
  4372. (match-string 2 value)
  4373. props))))
  4374. ((equal key "CONSTANTS")
  4375. (org-table-set-constants))
  4376. ((equal key "STARTUP")
  4377. (let ((opts (org-split-string value splitre))
  4378. l var val)
  4379. (while (setq l (pop opts))
  4380. (when (setq l (assoc l org-startup-options))
  4381. (setq var (nth 1 l) val (nth 2 l))
  4382. (if (not (nth 3 l))
  4383. (set (make-local-variable var) val)
  4384. (if (not (listp (symbol-value var)))
  4385. (set (make-local-variable var) nil))
  4386. (set (make-local-variable var) (symbol-value var))
  4387. (add-to-list var val))))))
  4388. ((equal key "ARCHIVE")
  4389. (setq arch value)
  4390. (remove-text-properties 0 (length arch)
  4391. '(face t fontified t) arch))
  4392. ((equal key "OPTIONS")
  4393. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4394. (setq scripts (read (match-string 2 value)))))
  4395. ((and (equal key "SETUPFILE")
  4396. ;; Prevent checking in Gnus messages
  4397. (not buffer-read-only))
  4398. (setq setup-contents (org-file-contents
  4399. (expand-file-name
  4400. (org-remove-double-quotes value))
  4401. 'noerror))
  4402. (if (not ext-setup-or-nil)
  4403. (setq ext-setup-or-nil setup-contents start 0)
  4404. (setq ext-setup-or-nil
  4405. (concat (substring ext-setup-or-nil 0 start)
  4406. "\n" setup-contents "\n"
  4407. (substring ext-setup-or-nil start)))))))
  4408. ;; search for property blocks
  4409. (goto-char (point-min))
  4410. (while (re-search-forward org-block-regexp nil t)
  4411. (when (equal "PROPERTY" (upcase (match-string 1)))
  4412. (setq value (replace-regexp-in-string
  4413. "[\n\r]" " " (match-string 4)))
  4414. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4415. (setq props (org-update-property-plist (match-string 1 value)
  4416. (match-string 2 value)
  4417. props)))))))
  4418. (org-set-local 'org-use-sub-superscripts scripts)
  4419. (when cat
  4420. (org-set-local 'org-category (intern cat))
  4421. (push (cons "CATEGORY" cat) props))
  4422. (when prio
  4423. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4424. (setq prio (mapcar 'string-to-char prio))
  4425. (org-set-local 'org-highest-priority (nth 0 prio))
  4426. (org-set-local 'org-lowest-priority (nth 1 prio))
  4427. (org-set-local 'org-default-priority (nth 2 prio)))
  4428. (and props (org-set-local 'org-file-properties (nreverse props)))
  4429. (and arch (org-set-local 'org-archive-location arch))
  4430. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4431. ;; Process the TODO keywords
  4432. (unless kwds
  4433. ;; Use the global values as if they had been given locally.
  4434. (setq kwds (default-value 'org-todo-keywords))
  4435. (if (stringp (car kwds))
  4436. (setq kwds (list (cons org-todo-interpretation
  4437. (default-value 'org-todo-keywords)))))
  4438. (setq kwds (reverse kwds)))
  4439. (setq kwds (nreverse kwds))
  4440. (let (inter kws kw)
  4441. (while (setq kws (pop kwds))
  4442. (let ((kws (or
  4443. (run-hook-with-args-until-success
  4444. 'org-todo-setup-filter-hook kws)
  4445. kws)))
  4446. (setq inter (pop kws) sep (member "|" kws)
  4447. kws0 (delete "|" (copy-sequence kws))
  4448. kwsa nil
  4449. kws1 (mapcar
  4450. (lambda (x)
  4451. ;; 1 2
  4452. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4453. (progn
  4454. (setq kw (match-string 1 x)
  4455. key (and (match-end 2) (match-string 2 x))
  4456. log (org-extract-log-state-settings x))
  4457. (push (cons kw (and key (string-to-char key))) kwsa)
  4458. (and log (push log org-todo-log-states))
  4459. kw)
  4460. (error "Invalid TODO keyword %s" x)))
  4461. kws0)
  4462. kwsa (if kwsa (append '((:startgroup))
  4463. (nreverse kwsa)
  4464. '((:endgroup))))
  4465. hw (car kws1)
  4466. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4467. tail (list inter hw (car dws) (org-last dws))))
  4468. (add-to-list 'org-todo-heads hw 'append)
  4469. (push kws1 org-todo-sets)
  4470. (setq org-done-keywords (append org-done-keywords dws nil))
  4471. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4472. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4473. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4474. (setq org-todo-sets (nreverse org-todo-sets)
  4475. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4476. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4477. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4478. ;; Compute the regular expressions and other local variables.
  4479. ;; Using `org-outline-regexp-bol' would complicate them much,
  4480. ;; because of the fixed white space at the end of that string.
  4481. (if (not org-done-keywords)
  4482. (setq org-done-keywords (and org-todo-keywords-1
  4483. (list (org-last org-todo-keywords-1)))))
  4484. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4485. (length org-scheduled-string)
  4486. (length org-clock-string)
  4487. (length org-closed-string)))
  4488. org-not-done-keywords
  4489. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4490. org-todo-regexp
  4491. (concat "\\("
  4492. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4493. "\\)")
  4494. org-not-done-regexp
  4495. (concat "\\("
  4496. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4497. "\\)")
  4498. org-not-done-heading-regexp
  4499. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4500. org-todo-line-regexp
  4501. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4502. org-complex-heading-regexp
  4503. (concat "^\\(\\*+\\)"
  4504. "\\(?: +" org-todo-regexp "\\)?"
  4505. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4506. "\\(?: +\\(.*?\\)\\)??"
  4507. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4508. "[ \t]*$")
  4509. org-complex-heading-regexp-format
  4510. (concat "^\\(\\*+\\)"
  4511. "\\(?: +" org-todo-regexp "\\)?"
  4512. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4513. "\\(?: +"
  4514. ;; Stats cookies can be stuck to body.
  4515. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4516. "\\(%s\\)"
  4517. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4518. "\\)"
  4519. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4520. "[ \t]*$")
  4521. org-todo-line-tags-regexp
  4522. (concat "^\\(\\*+\\)"
  4523. "\\(?: +" org-todo-regexp "\\)?"
  4524. "\\(?: +\\(.*?\\)\\)??"
  4525. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4526. "[ \t]*$")
  4527. org-deadline-regexp (concat "\\<" org-deadline-string)
  4528. org-deadline-time-regexp
  4529. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4530. org-deadline-time-hour-regexp
  4531. (concat "\\<" org-deadline-string
  4532. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4533. org-deadline-line-regexp
  4534. (concat "\\<\\(" org-deadline-string "\\).*")
  4535. org-scheduled-regexp
  4536. (concat "\\<" org-scheduled-string)
  4537. org-scheduled-time-regexp
  4538. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4539. org-scheduled-time-hour-regexp
  4540. (concat "\\<" org-scheduled-string
  4541. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4542. org-closed-time-regexp
  4543. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4544. org-keyword-time-regexp
  4545. (concat "\\<\\(" org-scheduled-string
  4546. "\\|" org-deadline-string
  4547. "\\|" org-closed-string
  4548. "\\|" org-clock-string "\\)"
  4549. " *[[<]\\([^]>]+\\)[]>]")
  4550. org-keyword-time-not-clock-regexp
  4551. (concat "\\<\\(" org-scheduled-string
  4552. "\\|" org-deadline-string
  4553. "\\|" org-closed-string
  4554. "\\)"
  4555. " *[[<]\\([^]>]+\\)[]>]")
  4556. org-maybe-keyword-time-regexp
  4557. (concat "\\(\\<\\(" org-scheduled-string
  4558. "\\|" org-deadline-string
  4559. "\\|" org-closed-string
  4560. "\\|" org-clock-string "\\)\\)?"
  4561. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4562. org-all-time-keywords
  4563. (mapcar (lambda (w) (substring w 0 -1))
  4564. (list org-scheduled-string org-deadline-string
  4565. org-clock-string org-closed-string)))
  4566. (setq org-ota nil)
  4567. (org-compute-latex-and-related-regexp))))
  4568. (defun org-file-contents (file &optional noerror)
  4569. "Return the contents of FILE, as a string."
  4570. (if (or (not file)
  4571. (not (file-readable-p file)))
  4572. (if noerror
  4573. (message "Cannot read file \"%s\"" file)
  4574. (error "Cannot read file \"%s\"" file))
  4575. (with-temp-buffer
  4576. (insert-file-contents file)
  4577. (buffer-string))))
  4578. (defun org-extract-log-state-settings (x)
  4579. "Extract the log state setting from a TODO keyword string.
  4580. This will extract info from a string like \"WAIT(w@/!)\"."
  4581. (let (kw key log1 log2)
  4582. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4583. (setq kw (match-string 1 x)
  4584. key (and (match-end 2) (match-string 2 x))
  4585. log1 (and (match-end 3) (match-string 3 x))
  4586. log2 (and (match-end 4) (match-string 4 x)))
  4587. (and (or log1 log2)
  4588. (list kw
  4589. (and log1 (if (equal log1 "!") 'time 'note))
  4590. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4591. (defun org-remove-keyword-keys (list)
  4592. "Remove a pair of parenthesis at the end of each string in LIST."
  4593. (mapcar (lambda (x)
  4594. (if (string-match "(.*)$" x)
  4595. (substring x 0 (match-beginning 0))
  4596. x))
  4597. list))
  4598. (defun org-assign-fast-keys (alist)
  4599. "Assign fast keys to a keyword-key alist.
  4600. Respect keys that are already there."
  4601. (let (new e (alt ?0))
  4602. (while (setq e (pop alist))
  4603. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4604. (cdr e)) ;; Key already assigned.
  4605. (push e new)
  4606. (let ((clist (string-to-list (downcase (car e))))
  4607. (used (append new alist)))
  4608. (when (= (car clist) ?@)
  4609. (pop clist))
  4610. (while (and clist (rassoc (car clist) used))
  4611. (pop clist))
  4612. (unless clist
  4613. (while (rassoc alt used)
  4614. (incf alt)))
  4615. (push (cons (car e) (or (car clist) alt)) new))))
  4616. (nreverse new)))
  4617. ;;; Some variables used in various places
  4618. (defvar org-window-configuration nil
  4619. "Used in various places to store a window configuration.")
  4620. (defvar org-selected-window nil
  4621. "Used in various places to store a window configuration.")
  4622. (defvar org-finish-function nil
  4623. "Function to be called when `C-c C-c' is used.
  4624. This is for getting out of special buffers like capture.")
  4625. ;; FIXME: Occasionally check by commenting these, to make sure
  4626. ;; no other functions uses these, forgetting to let-bind them.
  4627. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4628. (defvar org-last-state)
  4629. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4630. ;; Defined somewhere in this file, but used before definition.
  4631. (defvar org-entities) ;; defined in org-entities.el
  4632. (defvar org-struct-menu)
  4633. (defvar org-org-menu)
  4634. (defvar org-tbl-menu)
  4635. ;;;; Define the Org-mode
  4636. ;; We use a before-change function to check if a table might need
  4637. ;; an update.
  4638. (defvar org-table-may-need-update t
  4639. "Indicates that a table might need an update.
  4640. This variable is set by `org-before-change-function'.
  4641. `org-table-align' sets it back to nil.")
  4642. (defun org-before-change-function (beg end)
  4643. "Every change indicates that a table might need an update."
  4644. (setq org-table-may-need-update t))
  4645. (defvar org-mode-map)
  4646. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4647. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4648. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4649. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4650. (defvar org-table-buffer-is-an nil)
  4651. (defvar bidi-paragraph-direction)
  4652. (defvar buffer-face-mode-face)
  4653. (require 'outline)
  4654. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4655. (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"))
  4656. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4657. ;; Other stuff we need.
  4658. (require 'time-date)
  4659. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4660. (require 'easymenu)
  4661. (require 'overlay)
  4662. ;; (require 'org-macs) moved higher up in the file before it is first used
  4663. (require 'org-entities)
  4664. ;; (require 'org-compat) moved higher up in the file before it is first used
  4665. (require 'org-faces)
  4666. (require 'org-list)
  4667. (require 'org-pcomplete)
  4668. (require 'org-src)
  4669. (require 'org-footnote)
  4670. (require 'org-macro)
  4671. ;; babel
  4672. (require 'ob)
  4673. ;;;###autoload
  4674. (define-derived-mode org-mode outline-mode "Org"
  4675. "Outline-based notes management and organizer, alias
  4676. \"Carsten's outline-mode for keeping track of everything.\"
  4677. Org-mode develops organizational tasks around a NOTES file which
  4678. contains information about projects as plain text. Org-mode is
  4679. implemented on top of outline-mode, which is ideal to keep the content
  4680. of large files well structured. It supports ToDo items, deadlines and
  4681. time stamps, which magically appear in the diary listing of the Emacs
  4682. calendar. Tables are easily created with a built-in table editor.
  4683. Plain text URL-like links connect to websites, emails (VM), Usenet
  4684. messages (Gnus), BBDB entries, and any files related to the project.
  4685. For printing and sharing of notes, an Org-mode file (or a part of it)
  4686. can be exported as a structured ASCII or HTML file.
  4687. The following commands are available:
  4688. \\{org-mode-map}"
  4689. ;; Get rid of Outline menus, they are not needed
  4690. ;; Need to do this here because define-derived-mode sets up
  4691. ;; the keymap so late. Still, it is a waste to call this each time
  4692. ;; we switch another buffer into org-mode.
  4693. (if (featurep 'xemacs)
  4694. (when (boundp 'outline-mode-menu-heading)
  4695. ;; Assume this is Greg's port, it uses easymenu
  4696. (easy-menu-remove outline-mode-menu-heading)
  4697. (easy-menu-remove outline-mode-menu-show)
  4698. (easy-menu-remove outline-mode-menu-hide))
  4699. (define-key org-mode-map [menu-bar headings] 'undefined)
  4700. (define-key org-mode-map [menu-bar hide] 'undefined)
  4701. (define-key org-mode-map [menu-bar show] 'undefined))
  4702. (org-load-modules-maybe)
  4703. (easy-menu-add org-org-menu)
  4704. (easy-menu-add org-tbl-menu)
  4705. (org-install-agenda-files-menu)
  4706. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4707. (add-to-invisibility-spec '(org-cwidth))
  4708. (add-to-invisibility-spec '(org-hide-block . t))
  4709. (when (featurep 'xemacs)
  4710. (org-set-local 'line-move-ignore-invisible t))
  4711. (org-set-local 'outline-regexp org-outline-regexp)
  4712. (org-set-local 'outline-level 'org-outline-level)
  4713. (setq bidi-paragraph-direction 'left-to-right)
  4714. (when (and org-ellipsis
  4715. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4716. (fboundp 'make-glyph-code))
  4717. (unless org-display-table
  4718. (setq org-display-table (make-display-table)))
  4719. (set-display-table-slot
  4720. org-display-table 4
  4721. (vconcat (mapcar
  4722. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4723. org-ellipsis)))
  4724. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4725. (setq buffer-display-table org-display-table))
  4726. (org-set-regexps-and-options-for-tags)
  4727. (org-set-regexps-and-options)
  4728. (org-set-font-lock-defaults)
  4729. (when (and org-tag-faces (not org-tags-special-faces-re))
  4730. ;; tag faces set outside customize.... force initialization.
  4731. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4732. ;; Calc embedded
  4733. (org-set-local 'calc-embedded-open-mode "# ")
  4734. ;; Modify a few syntax entries
  4735. (modify-syntax-entry ?@ "w")
  4736. (modify-syntax-entry ?\" "\"")
  4737. (if org-startup-truncated (setq truncate-lines t))
  4738. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4739. (org-set-local 'font-lock-unfontify-region-function
  4740. 'org-unfontify-region)
  4741. ;; Activate before-change-function
  4742. (org-set-local 'org-table-may-need-update t)
  4743. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4744. 'local)
  4745. ;; Check for running clock before killing a buffer
  4746. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4747. ;; Initialize macros templates.
  4748. (org-macro-initialize-templates)
  4749. ;; Initialize radio targets.
  4750. (org-update-radio-target-regexp)
  4751. ;; Indentation.
  4752. (org-set-local 'indent-line-function 'org-indent-line)
  4753. (org-set-local 'indent-region-function 'org-indent-region)
  4754. ;; Filling and auto-filling.
  4755. (org-setup-filling)
  4756. ;; Comments.
  4757. (org-setup-comments-handling)
  4758. ;; Initialize cache.
  4759. (org-element-cache-reset)
  4760. ;; Beginning/end of defun
  4761. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4762. (org-set-local 'end-of-defun-function 'org-forward-element)
  4763. ;; Next error for sparse trees
  4764. (org-set-local 'next-error-function 'org-occur-next-match)
  4765. ;; Make sure dependence stuff works reliably, even for users who set it
  4766. ;; too late :-(
  4767. (if org-enforce-todo-dependencies
  4768. (add-hook 'org-blocker-hook
  4769. 'org-block-todo-from-children-or-siblings-or-parent)
  4770. (remove-hook 'org-blocker-hook
  4771. 'org-block-todo-from-children-or-siblings-or-parent))
  4772. (if org-enforce-todo-checkbox-dependencies
  4773. (add-hook 'org-blocker-hook
  4774. 'org-block-todo-from-checkboxes)
  4775. (remove-hook 'org-blocker-hook
  4776. 'org-block-todo-from-checkboxes))
  4777. ;; Align options lines
  4778. (org-set-local
  4779. 'align-mode-rules-list
  4780. '((org-in-buffer-settings
  4781. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4782. (modes . '(org-mode)))))
  4783. ;; Imenu
  4784. (org-set-local 'imenu-create-index-function
  4785. 'org-imenu-get-tree)
  4786. ;; Make isearch reveal context
  4787. (if (or (featurep 'xemacs)
  4788. (not (boundp 'outline-isearch-open-invisible-function)))
  4789. ;; Emacs 21 and XEmacs make use of the hook
  4790. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4791. ;; Emacs 22 deals with this through a special variable
  4792. (org-set-local 'outline-isearch-open-invisible-function
  4793. (lambda (&rest ignore) (org-show-context 'isearch)))
  4794. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4795. ;; Setup the pcomplete hooks
  4796. (set (make-local-variable 'pcomplete-command-completion-function)
  4797. 'org-pcomplete-initial)
  4798. (set (make-local-variable 'pcomplete-command-name-function)
  4799. 'org-command-at-point)
  4800. (set (make-local-variable 'pcomplete-default-completion-function)
  4801. 'ignore)
  4802. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4803. 'org-parse-arguments)
  4804. (set (make-local-variable 'pcomplete-termination-string) "")
  4805. (when (>= emacs-major-version 23)
  4806. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4807. ;; If empty file that did not turn on org-mode automatically, make it to.
  4808. (if (and org-insert-mode-line-in-empty-file
  4809. (org-called-interactively-p 'any)
  4810. (= (point-min) (point-max)))
  4811. (insert "# -*- mode: org -*-\n\n"))
  4812. (unless org-inhibit-startup
  4813. (org-unmodified
  4814. (and org-startup-with-beamer-mode (org-beamer-mode))
  4815. (when org-startup-align-all-tables
  4816. (org-table-map-tables 'org-table-align 'quietly))
  4817. (when org-startup-with-inline-images
  4818. (org-display-inline-images))
  4819. (when org-startup-with-latex-preview
  4820. (org-preview-latex-fragment))
  4821. (unless org-inhibit-startup-visibility-stuff
  4822. (org-set-startup-visibility))))
  4823. ;; Try to set org-hide correctly
  4824. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4825. ;; Update `customize-package-emacs-version-alist'
  4826. (add-to-list 'customize-package-emacs-version-alist
  4827. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4828. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4829. ("8.0" . "24.4")))
  4830. (defvar org-mode-transpose-word-syntax-table
  4831. (let ((st (make-syntax-table)))
  4832. (mapc (lambda(c) (modify-syntax-entry
  4833. (string-to-char (car c)) "w p" st))
  4834. org-emphasis-alist)
  4835. st))
  4836. (when (fboundp 'abbrev-table-put)
  4837. (abbrev-table-put org-mode-abbrev-table
  4838. :parents (list text-mode-abbrev-table)))
  4839. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4840. (defsubst org-fix-ellipsis-at-bol ()
  4841. (save-excursion (goto-char (window-start)) (recenter 0)))
  4842. (defun org-find-invisible-foreground ()
  4843. (let ((candidates (remove
  4844. "unspecified-bg"
  4845. (nconc
  4846. (list (face-background 'default)
  4847. (face-background 'org-default))
  4848. (mapcar
  4849. (lambda (alist)
  4850. (when (boundp alist)
  4851. (cdr (assoc 'background-color (symbol-value alist)))))
  4852. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4853. (list (face-foreground 'org-hide))))))
  4854. (car (remove nil candidates))))
  4855. (defun org-current-time (&optional rounding-minutes past)
  4856. "Current time, possibly rounded to ROUNDING-MINUTES.
  4857. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4858. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4859. the rounding returns a past time."
  4860. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4861. (car org-time-stamp-rounding-minutes)))
  4862. (time (decode-time)) res)
  4863. (if (< r 1)
  4864. (current-time)
  4865. (setq res
  4866. (apply 'encode-time
  4867. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4868. (nthcdr 2 time))))
  4869. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4870. (seconds-to-time (- (org-float-time res) (* r 60)))
  4871. res))))
  4872. (defun org-today ()
  4873. "Return today date, considering `org-extend-today-until'."
  4874. (time-to-days
  4875. (time-subtract (current-time)
  4876. (list 0 (* 3600 org-extend-today-until) 0))))
  4877. ;;;; Font-Lock stuff, including the activators
  4878. (defvar org-mouse-map (make-sparse-keymap))
  4879. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4880. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4881. (when org-mouse-1-follows-link
  4882. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4883. (when org-tab-follows-link
  4884. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4885. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4886. (require 'font-lock)
  4887. (defconst org-non-link-chars "]\t\n\r<>")
  4888. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4889. "shell" "elisp" "doi" "message"))
  4890. (defvar org-link-types-re nil
  4891. "Matches a link that has a url-like prefix like \"http:\"")
  4892. (defvar org-link-re-with-space nil
  4893. "Matches a link with spaces, optional angular brackets around it.")
  4894. (defvar org-link-re-with-space2 nil
  4895. "Matches a link with spaces, optional angular brackets around it.")
  4896. (defvar org-link-re-with-space3 nil
  4897. "Matches a link with spaces, only for internal part in bracket links.")
  4898. (defvar org-angle-link-re nil
  4899. "Matches link with angular brackets, spaces are allowed.")
  4900. (defvar org-plain-link-re nil
  4901. "Matches plain link, without spaces.")
  4902. (defvar org-bracket-link-regexp nil
  4903. "Matches a link in double brackets.")
  4904. (defvar org-bracket-link-analytic-regexp nil
  4905. "Regular expression used to analyze links.
  4906. Here is what the match groups contain after a match:
  4907. 1: http:
  4908. 2: http
  4909. 3: path
  4910. 4: [desc]
  4911. 5: desc")
  4912. (defvar org-bracket-link-analytic-regexp++ nil
  4913. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4914. (defvar org-any-link-re nil
  4915. "Regular expression matching any link.")
  4916. (defconst org-match-sexp-depth 3
  4917. "Number of stacked braces for sub/superscript matching.")
  4918. (defun org-create-multibrace-regexp (left right n)
  4919. "Create a regular expression which will match a balanced sexp.
  4920. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4921. as single character strings.
  4922. The regexp returned will match the entire expression including the
  4923. delimiters. It will also define a single group which contains the
  4924. match except for the outermost delimiters. The maximum depth of
  4925. stacked delimiters is N. Escaping delimiters is not possible."
  4926. (let* ((nothing (concat "[^" left right "]*?"))
  4927. (or "\\|")
  4928. (re nothing)
  4929. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4930. (while (> n 1)
  4931. (setq n (1- n)
  4932. re (concat re or next)
  4933. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4934. (concat left "\\(" re "\\)" right)))
  4935. (defvar org-match-substring-regexp
  4936. (concat
  4937. "\\(\\S-\\)\\([_^]\\)\\("
  4938. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4939. "\\|"
  4940. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4941. "\\|"
  4942. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4943. "The regular expression matching a sub- or superscript.")
  4944. (defvar org-match-substring-with-braces-regexp
  4945. (concat
  4946. "\\(\\S-\\)\\([_^]\\)\\("
  4947. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4948. "\\)")
  4949. "The regular expression matching a sub- or superscript, forcing braces.")
  4950. (defun org-make-link-regexps ()
  4951. "Update the link regular expressions.
  4952. This should be called after the variable `org-link-types' has changed."
  4953. (setq org-link-types-re
  4954. (concat
  4955. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4956. org-link-re-with-space
  4957. (concat
  4958. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4959. "\\([^" org-non-link-chars " ]"
  4960. "[^" org-non-link-chars "]*"
  4961. "[^" org-non-link-chars " ]\\)>?")
  4962. org-link-re-with-space2
  4963. (concat
  4964. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4965. "\\([^" org-non-link-chars " ]"
  4966. "[^\t\n\r]*"
  4967. "[^" org-non-link-chars " ]\\)>?")
  4968. org-link-re-with-space3
  4969. (concat
  4970. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4971. "\\([^" org-non-link-chars " ]"
  4972. "[^\t\n\r]*\\)")
  4973. org-angle-link-re
  4974. (concat
  4975. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4976. "\\([^" org-non-link-chars " ]"
  4977. "[^" org-non-link-chars "]*"
  4978. "\\)>")
  4979. org-plain-link-re
  4980. (concat
  4981. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4982. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4983. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4984. org-bracket-link-regexp
  4985. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4986. org-bracket-link-analytic-regexp
  4987. (concat
  4988. "\\[\\["
  4989. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4990. "\\([^]]+\\)"
  4991. "\\]"
  4992. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4993. "\\]")
  4994. org-bracket-link-analytic-regexp++
  4995. (concat
  4996. "\\[\\["
  4997. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4998. "\\([^]]+\\)"
  4999. "\\]"
  5000. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5001. "\\]")
  5002. org-any-link-re
  5003. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5004. org-angle-link-re "\\)\\|\\("
  5005. org-plain-link-re "\\)")))
  5006. (org-make-link-regexps)
  5007. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5008. "Regular expression for fast time stamp matching.")
  5009. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5010. "Regular expression for fast time stamp matching.")
  5011. (defconst org-ts-regexp0
  5012. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5013. "Regular expression matching time strings for analysis.
  5014. This one does not require the space after the date, so it can be used
  5015. on a string that terminates immediately after the date.")
  5016. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5017. "Regular expression matching time strings for analysis.")
  5018. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5019. "Regular expression matching time stamps, with groups.")
  5020. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5021. "Regular expression matching time stamps (also [..]), with groups.")
  5022. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5023. "Regular expression matching a time stamp range.")
  5024. (defconst org-tr-regexp-both
  5025. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5026. "Regular expression matching a time stamp range.")
  5027. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5028. org-ts-regexp "\\)?")
  5029. "Regular expression matching a time stamp or time stamp range.")
  5030. (defconst org-tsr-regexp-both
  5031. (concat org-ts-regexp-both "\\(--?-?"
  5032. org-ts-regexp-both "\\)?")
  5033. "Regular expression matching a time stamp or time stamp range.
  5034. The time stamps may be either active or inactive.")
  5035. (defvar org-emph-face nil)
  5036. (defun org-do-emphasis-faces (limit)
  5037. "Run through the buffer and add overlays to emphasized strings."
  5038. (let (rtn a)
  5039. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5040. (if (not (= (char-after (match-beginning 3))
  5041. (char-after (match-beginning 4))))
  5042. (progn
  5043. (setq rtn t)
  5044. (setq a (assoc (match-string 3) org-emphasis-alist))
  5045. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5046. 'face
  5047. (nth 1 a))
  5048. (and (nth 2 a)
  5049. (org-remove-flyspell-overlays-in
  5050. (match-beginning 0) (match-end 0)))
  5051. (add-text-properties (match-beginning 2) (match-end 2)
  5052. '(font-lock-multiline t org-emphasis t))
  5053. (when org-hide-emphasis-markers
  5054. (add-text-properties (match-end 4) (match-beginning 5)
  5055. '(invisible org-link))
  5056. (add-text-properties (match-beginning 3) (match-end 3)
  5057. '(invisible org-link)))))
  5058. (backward-char 1))
  5059. rtn))
  5060. (defun org-emphasize (&optional char)
  5061. "Insert or change an emphasis, i.e. a font like bold or italic.
  5062. If there is an active region, change that region to a new emphasis.
  5063. If there is no region, just insert the marker characters and position
  5064. the cursor between them.
  5065. CHAR should be the marker character. If it is a space, it means to
  5066. remove the emphasis of the selected region.
  5067. If CHAR is not given (for example in an interactive call) it will be
  5068. prompted for."
  5069. (interactive)
  5070. (let ((erc org-emphasis-regexp-components)
  5071. (prompt "")
  5072. (string "") beg end move c s)
  5073. (if (org-region-active-p)
  5074. (setq beg (region-beginning) end (region-end)
  5075. string (buffer-substring beg end))
  5076. (setq move t))
  5077. (unless char
  5078. (message "Emphasis marker or tag: [%s]"
  5079. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5080. (setq char (read-char-exclusive)))
  5081. (if (equal char ?\ )
  5082. (setq s "" move nil)
  5083. (unless (assoc (char-to-string char) org-emphasis-alist)
  5084. (user-error "No such emphasis marker: \"%c\"" char))
  5085. (setq s (char-to-string char)))
  5086. (while (and (> (length string) 1)
  5087. (equal (substring string 0 1) (substring string -1))
  5088. (assoc (substring string 0 1) org-emphasis-alist))
  5089. (setq string (substring string 1 -1)))
  5090. (setq string (concat s string s))
  5091. (if beg (delete-region beg end))
  5092. (unless (or (bolp)
  5093. (string-match (concat "[" (nth 0 erc) "\n]")
  5094. (char-to-string (char-before (point)))))
  5095. (insert " "))
  5096. (unless (or (eobp)
  5097. (string-match (concat "[" (nth 1 erc) "\n]")
  5098. (char-to-string (char-after (point)))))
  5099. (insert " ") (backward-char 1))
  5100. (insert string)
  5101. (and move (backward-char 1))))
  5102. (defconst org-nonsticky-props
  5103. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5104. (defsubst org-rear-nonsticky-at (pos)
  5105. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5106. (defun org-activate-plain-links (limit)
  5107. "Run through the buffer and add overlays to links."
  5108. (let (f hl)
  5109. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5110. (not (org-in-src-block-p)))
  5111. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5112. (setq f (get-text-property (match-beginning 0) 'face))
  5113. (setq hl (org-match-string-no-properties 0))
  5114. (if (or (eq f 'org-tag)
  5115. (and (listp f) (memq 'org-tag f)))
  5116. nil
  5117. (add-text-properties (match-beginning 0) (match-end 0)
  5118. (list 'mouse-face 'highlight
  5119. 'face 'org-link
  5120. 'htmlize-link `(:uri ,hl)
  5121. 'keymap org-mouse-map))
  5122. (org-rear-nonsticky-at (match-end 0)))
  5123. t)))
  5124. (defun org-activate-code (limit)
  5125. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5126. (progn
  5127. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5128. (remove-text-properties (match-beginning 0) (match-end 0)
  5129. '(display t invisible t intangible t))
  5130. t)))
  5131. (defcustom org-src-fontify-natively nil
  5132. "When non-nil, fontify code in code blocks."
  5133. :type 'boolean
  5134. :version "24.1"
  5135. :group 'org-appearance
  5136. :group 'org-babel)
  5137. (defcustom org-allow-promoting-top-level-subtree nil
  5138. "When non-nil, allow promoting a top level subtree.
  5139. The leading star of the top level headline will be replaced
  5140. by a #."
  5141. :type 'boolean
  5142. :version "24.1"
  5143. :group 'org-appearance)
  5144. (defun org-fontify-meta-lines-and-blocks (limit)
  5145. (condition-case nil
  5146. (org-fontify-meta-lines-and-blocks-1 limit)
  5147. (error (message "org-mode fontification error"))))
  5148. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5149. "Fontify #+ lines and blocks."
  5150. (let ((case-fold-search t))
  5151. (if (re-search-forward
  5152. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5153. limit t)
  5154. (let ((beg (match-beginning 0))
  5155. (block-start (match-end 0))
  5156. (block-end nil)
  5157. (lang (match-string 7))
  5158. (beg1 (line-beginning-position 2))
  5159. (dc1 (downcase (match-string 2)))
  5160. (dc3 (downcase (match-string 3)))
  5161. end end1 quoting block-type ovl)
  5162. (cond
  5163. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5164. ;; a single line of backend-specific content
  5165. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5166. (remove-text-properties (match-beginning 0) (match-end 0)
  5167. '(display t invisible t intangible t))
  5168. (add-text-properties (match-beginning 1) (match-end 3)
  5169. '(font-lock-fontified t face org-meta-line))
  5170. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5171. '(font-lock-fontified t face org-block))
  5172. ; for backend-specific code
  5173. t)
  5174. ((and (match-end 4) (equal dc3 "+begin"))
  5175. ;; Truly a block
  5176. (setq block-type (downcase (match-string 5))
  5177. quoting (member block-type org-protecting-blocks))
  5178. (when (re-search-forward
  5179. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5180. nil t) ;; on purpose, we look further than LIMIT
  5181. (setq end (min (point-max) (match-end 0))
  5182. end1 (min (point-max) (1- (match-beginning 0))))
  5183. (setq block-end (match-beginning 0))
  5184. (when quoting
  5185. (remove-text-properties beg end
  5186. '(display t invisible t intangible t)))
  5187. (add-text-properties
  5188. beg end
  5189. '(font-lock-fontified t font-lock-multiline t))
  5190. (add-text-properties beg beg1 '(face org-meta-line))
  5191. (add-text-properties end1 (min (point-max) (1+ end))
  5192. '(face org-meta-line)) ; for end_src
  5193. (cond
  5194. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5195. (org-src-font-lock-fontify-block lang block-start block-end)
  5196. ;; remove old background overlays
  5197. (mapc (lambda (ov)
  5198. (if (eq (overlay-get ov 'face) 'org-block-background)
  5199. (delete-overlay ov)))
  5200. (overlays-at (/ (+ beg1 block-end) 2)))
  5201. ;; add a background overlay
  5202. (setq ovl (make-overlay beg1 block-end))
  5203. (overlay-put ovl 'face 'org-block-background)
  5204. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5205. (quoting
  5206. (add-text-properties beg1 (min (point-max) (1+ end1))
  5207. '(face org-block))) ; end of source block
  5208. ((not org-fontify-quote-and-verse-blocks))
  5209. ((string= block-type "quote")
  5210. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5211. ((string= block-type "verse")
  5212. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5213. (add-text-properties beg beg1 '(face org-block-begin-line))
  5214. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5215. '(face org-block-end-line))
  5216. t))
  5217. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5218. (add-text-properties
  5219. beg (match-end 3)
  5220. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5221. '(font-lock-fontified t invisible t)
  5222. '(font-lock-fontified t face org-document-info-keyword)))
  5223. (add-text-properties
  5224. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5225. (if (string-equal dc1 "+title:")
  5226. '(font-lock-fontified t face org-document-title)
  5227. '(font-lock-fontified t face org-document-info))))
  5228. ((or (equal dc1 "+results")
  5229. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5230. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5231. "+call:" "+header:" "+headers:" "+name:"))
  5232. (and (match-end 4) (equal dc3 "+attr")))
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face org-meta-line))
  5236. t)
  5237. ((member dc3 '(" " ""))
  5238. (add-text-properties
  5239. beg (match-end 0)
  5240. '(font-lock-fontified t face font-lock-comment-face)))
  5241. ((not (member (char-after beg) '(?\ ?\t)))
  5242. ;; just any other in-buffer setting, but not indented
  5243. (add-text-properties
  5244. beg (match-end 0)
  5245. '(font-lock-fontified t face org-meta-line))
  5246. t)
  5247. (t nil))))))
  5248. (defun org-activate-angle-links (limit)
  5249. "Run through the buffer and add overlays to links."
  5250. (if (and (re-search-forward org-angle-link-re limit t)
  5251. (not (org-in-src-block-p)))
  5252. (progn
  5253. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5254. (add-text-properties (match-beginning 0) (match-end 0)
  5255. (list 'mouse-face 'highlight
  5256. 'keymap org-mouse-map))
  5257. (org-rear-nonsticky-at (match-end 0))
  5258. t)))
  5259. (defun org-activate-footnote-links (limit)
  5260. "Run through the buffer and add overlays to footnotes."
  5261. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5262. (when fn
  5263. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5264. (org-remove-flyspell-overlays-in beg end)
  5265. (add-text-properties beg end
  5266. (list 'mouse-face 'highlight
  5267. 'keymap org-mouse-map
  5268. 'help-echo
  5269. (if (= (point-at-bol) beg)
  5270. "Footnote definition"
  5271. "Footnote reference")
  5272. 'font-lock-fontified t
  5273. 'font-lock-multiline t
  5274. 'face 'org-footnote))))))
  5275. (defun org-activate-bracket-links (limit)
  5276. "Run through the buffer and add overlays to bracketed links."
  5277. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5278. (not (org-in-src-block-p)))
  5279. (let* ((hl (org-match-string-no-properties 1))
  5280. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5281. (ip (org-maybe-intangible
  5282. (list 'invisible 'org-link
  5283. 'keymap org-mouse-map 'mouse-face 'highlight
  5284. 'font-lock-multiline t 'help-echo help
  5285. 'htmlize-link `(:uri ,hl))))
  5286. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5287. 'font-lock-multiline t 'help-echo help
  5288. 'htmlize-link `(:uri ,hl))))
  5289. ;; We need to remove the invisible property here. Table narrowing
  5290. ;; may have made some of this invisible.
  5291. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5292. (remove-text-properties (match-beginning 0) (match-end 0)
  5293. '(invisible nil))
  5294. (if (match-end 3)
  5295. (progn
  5296. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5297. (org-rear-nonsticky-at (match-beginning 3))
  5298. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5299. (org-rear-nonsticky-at (match-end 3))
  5300. (add-text-properties (match-end 3) (match-end 0) ip)
  5301. (org-rear-nonsticky-at (match-end 0)))
  5302. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5303. (org-rear-nonsticky-at (match-beginning 1))
  5304. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5305. (org-rear-nonsticky-at (match-end 1))
  5306. (add-text-properties (match-end 1) (match-end 0) ip)
  5307. (org-rear-nonsticky-at (match-end 0)))
  5308. t)))
  5309. (defun org-activate-dates (limit)
  5310. "Run through the buffer and add overlays to dates."
  5311. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5312. (not (equal (char-before (match-beginning 0)) 91)))
  5313. (progn
  5314. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5315. (add-text-properties (match-beginning 0) (match-end 0)
  5316. (list 'mouse-face 'highlight
  5317. 'keymap org-mouse-map))
  5318. (org-rear-nonsticky-at (match-end 0))
  5319. (when org-display-custom-times
  5320. (if (match-end 3)
  5321. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5322. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5323. t)))
  5324. (defvar org-target-link-regexp nil
  5325. "Regular expression matching radio targets in plain text.")
  5326. (make-variable-buffer-local 'org-target-link-regexp)
  5327. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5328. "Regular expression matching a link target.")
  5329. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5330. "Regular expression matching a radio target.")
  5331. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5332. "Regular expression matching any target.")
  5333. (defun org-activate-target-links (limit)
  5334. "Run through the buffer and add overlays to target matches."
  5335. (when org-target-link-regexp
  5336. (let ((case-fold-search t))
  5337. (if (re-search-forward org-target-link-regexp limit t)
  5338. (progn
  5339. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5340. (add-text-properties (match-beginning 0) (match-end 0)
  5341. (list 'mouse-face 'highlight
  5342. 'keymap org-mouse-map
  5343. 'help-echo "Radio target link"
  5344. 'org-linked-text t))
  5345. (org-rear-nonsticky-at (match-end 0))
  5346. t)))))
  5347. (defun org-update-radio-target-regexp ()
  5348. "Find all radio targets in this file and update the regular expression."
  5349. (interactive)
  5350. (when (memq 'radio org-activate-links)
  5351. (setq org-target-link-regexp
  5352. (org-make-target-link-regexp (org-all-targets 'radio)))
  5353. (org-restart-font-lock)))
  5354. (defun org-hide-wide-columns (limit)
  5355. (let (s e)
  5356. (setq s (text-property-any (point) (or limit (point-max))
  5357. 'org-cwidth t))
  5358. (when s
  5359. (setq e (next-single-property-change s 'org-cwidth))
  5360. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5361. (goto-char e)
  5362. t)))
  5363. (defvar org-latex-and-related-regexp nil
  5364. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5365. (defvar org-match-substring-regexp)
  5366. (defvar org-match-substring-with-braces-regexp)
  5367. (defun org-compute-latex-and-related-regexp ()
  5368. "Compute regular expression for LaTeX, entities and sub/superscript.
  5369. Result depends on variable `org-highlight-latex-and-related'."
  5370. (org-set-local
  5371. 'org-latex-and-related-regexp
  5372. (let* ((re-sub
  5373. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5374. ((eq org-use-sub-superscripts '{})
  5375. (list org-match-substring-with-braces-regexp))
  5376. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5377. (re-latex
  5378. (when (memq 'latex org-highlight-latex-and-related)
  5379. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5380. (delq nil
  5381. (mapcar (lambda (x)
  5382. (and (member (car x) matchers) (nth 1 x)))
  5383. org-latex-regexps)))))
  5384. (re-entities
  5385. (when (memq 'entities org-highlight-latex-and-related)
  5386. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5387. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5388. (defun org-do-latex-and-related (limit)
  5389. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5390. LIMIT bounds the search for syntax to highlight. Stop at first
  5391. highlighted object, if any. Return t if some highlighting was
  5392. done, nil otherwise."
  5393. (when (org-string-nw-p org-latex-and-related-regexp)
  5394. (catch 'found
  5395. (while (re-search-forward org-latex-and-related-regexp limit t)
  5396. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5397. 'face))
  5398. '(org-code org-verbatim underline))
  5399. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5400. '(?_ ?^))
  5401. 1
  5402. 0)))
  5403. (font-lock-prepend-text-property
  5404. (+ offset (match-beginning 0)) (match-end 0)
  5405. 'face 'org-latex-and-related)
  5406. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5407. '(font-lock-multiline t)))
  5408. (throw 'found t)))
  5409. nil)))
  5410. (defun org-restart-font-lock ()
  5411. "Restart `font-lock-mode', to force refontification."
  5412. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5413. (font-lock-mode -1)
  5414. (font-lock-mode 1)))
  5415. (defun org-all-targets (&optional radio)
  5416. "Return a list of all targets in this file.
  5417. When optional argument RADIO is non-nil, only find radio
  5418. targets."
  5419. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5420. (save-excursion
  5421. (goto-char (point-min))
  5422. (while (re-search-forward re nil t)
  5423. ;; Make sure point is really within the object.
  5424. (backward-char)
  5425. (let ((obj (org-element-context)))
  5426. (when (memq (org-element-type obj) '(radio-target target))
  5427. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5428. rtn)))
  5429. (defun org-make-target-link-regexp (targets)
  5430. "Make regular expression matching all strings in TARGETS.
  5431. The regular expression finds the targets also if there is a line break
  5432. between words."
  5433. (and targets
  5434. (concat
  5435. "\\<\\("
  5436. (mapconcat
  5437. (lambda (x)
  5438. (setq x (regexp-quote x))
  5439. (while (string-match " +" x)
  5440. (setq x (replace-match "\\s-+" t t x)))
  5441. x)
  5442. targets
  5443. "\\|")
  5444. "\\)\\>")))
  5445. (defun org-activate-tags (limit)
  5446. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5447. (progn
  5448. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5449. (add-text-properties (match-beginning 1) (match-end 1)
  5450. (list 'mouse-face 'highlight
  5451. 'keymap org-mouse-map))
  5452. (org-rear-nonsticky-at (match-end 1))
  5453. t)))
  5454. (defun org-outline-level ()
  5455. "Compute the outline level of the heading at point.
  5456. If this is called at a normal headline, the level is the number of stars.
  5457. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5458. (save-excursion
  5459. (if (not (condition-case nil
  5460. (org-back-to-heading t)
  5461. (error nil)))
  5462. 0
  5463. (looking-at org-outline-regexp)
  5464. (1- (- (match-end 0) (match-beginning 0))))))
  5465. (defvar org-font-lock-keywords nil)
  5466. (defsubst org-re-property (property &optional literal)
  5467. "Return a regexp matching a PROPERTY line.
  5468. Match group 3 will be set to the value if it exists."
  5469. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5470. (if literal property (regexp-quote property))
  5471. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5472. (defconst org-property-re
  5473. (org-re-property ".*?" 'literal)
  5474. "Regular expression matching a property line.
  5475. There are four matching groups:
  5476. 1: :PROPKEY: including the leading and trailing colon,
  5477. 2: PROPKEY without the leading and trailing colon,
  5478. 3: PROPVAL without leading or trailing spaces,
  5479. 4: the indentation of the current line,
  5480. 5: trailing whitespace.")
  5481. (defvar org-font-lock-hook nil
  5482. "Functions to be called for special font lock stuff.")
  5483. (defvar org-font-lock-set-keywords-hook nil
  5484. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5485. This is called after `org-font-lock-extra-keywords' is defined, but before
  5486. it is installed to be used by font lock. This can be useful if something
  5487. needs to be inserted at a specific position in the font-lock sequence.")
  5488. (defun org-font-lock-hook (limit)
  5489. "Run `org-font-lock-hook' within LIMIT."
  5490. (run-hook-with-args 'org-font-lock-hook limit))
  5491. (defun org-set-font-lock-defaults ()
  5492. "Set font lock defaults for the current buffer."
  5493. (let* ((em org-fontify-emphasized-text)
  5494. (lk org-activate-links)
  5495. (org-font-lock-extra-keywords
  5496. (list
  5497. ;; Call the hook
  5498. '(org-font-lock-hook)
  5499. ;; Headlines
  5500. `(,(if org-fontify-whole-heading-line
  5501. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5502. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5503. (1 (org-get-level-face 1))
  5504. (2 (org-get-level-face 2))
  5505. (3 (org-get-level-face 3)))
  5506. ;; Table lines
  5507. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5508. (1 'org-table t))
  5509. ;; Table internals
  5510. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5511. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5512. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5513. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5514. ;; Drawers
  5515. (list org-drawer-regexp '(0 'org-special-keyword t))
  5516. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5517. ;; Properties
  5518. (list org-property-re
  5519. '(1 'org-special-keyword t)
  5520. '(3 'org-property-value t))
  5521. ;; Links
  5522. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5523. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5524. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5525. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5526. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5527. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5528. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5529. ;; Targets.
  5530. (list org-any-target-regexp '(0 'org-target t))
  5531. ;; Diary sexps.
  5532. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5533. ;; Macro
  5534. '("{{{.+}}}" (0 'org-macro t))
  5535. '(org-hide-wide-columns (0 nil append))
  5536. ;; TODO keyword
  5537. (list (format org-heading-keyword-regexp-format
  5538. org-todo-regexp)
  5539. '(2 (org-get-todo-face 2) t))
  5540. ;; DONE
  5541. (if org-fontify-done-headline
  5542. (list (format org-heading-keyword-regexp-format
  5543. (concat
  5544. "\\(?:"
  5545. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5546. "\\)"))
  5547. '(2 'org-headline-done t))
  5548. nil)
  5549. ;; Priorities
  5550. '(org-font-lock-add-priority-faces)
  5551. ;; Tags
  5552. '(org-font-lock-add-tag-faces)
  5553. ;; Tags groups
  5554. (if (and org-group-tags org-tag-groups-alist)
  5555. (list (concat org-outline-regexp-bol ".+\\(:"
  5556. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5557. ":\\).*$")
  5558. '(1 'org-tag-group prepend)))
  5559. ;; Special keywords
  5560. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5561. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5562. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5563. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5564. ;; Emphasis
  5565. (if em
  5566. (if (featurep 'xemacs)
  5567. '(org-do-emphasis-faces (0 nil append))
  5568. '(org-do-emphasis-faces)))
  5569. ;; Checkboxes
  5570. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5571. 1 'org-checkbox prepend)
  5572. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5573. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5574. (0 (org-get-checkbox-statistics-face) t)))
  5575. ;; Description list items
  5576. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5577. 1 'org-list-dt prepend)
  5578. ;; ARCHIVEd headings
  5579. (list (concat
  5580. org-outline-regexp-bol
  5581. "\\(.*:" org-archive-tag ":.*\\)")
  5582. '(1 'org-archived prepend))
  5583. ;; Specials
  5584. '(org-do-latex-and-related)
  5585. '(org-fontify-entities)
  5586. '(org-raise-scripts)
  5587. ;; Code
  5588. '(org-activate-code (1 'org-code t))
  5589. ;; COMMENT
  5590. (list (format org-heading-keyword-regexp-format
  5591. (concat "\\("
  5592. org-comment-string "\\|" org-quote-string
  5593. "\\)"))
  5594. '(2 'org-special-keyword t))
  5595. ;; Blocks and meta lines
  5596. '(org-fontify-meta-lines-and-blocks))))
  5597. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5598. (run-hooks 'org-font-lock-set-keywords-hook)
  5599. ;; Now set the full font-lock-keywords
  5600. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5601. (org-set-local 'font-lock-defaults
  5602. '(org-font-lock-keywords t nil nil backward-paragraph))
  5603. (kill-local-variable 'font-lock-keywords) nil))
  5604. (defun org-toggle-pretty-entities ()
  5605. "Toggle the composition display of entities as UTF8 characters."
  5606. (interactive)
  5607. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5608. (org-restart-font-lock)
  5609. (if org-pretty-entities
  5610. (message "Entities are now displayed as UTF8 characters")
  5611. (save-restriction
  5612. (widen)
  5613. (org-decompose-region (point-min) (point-max))
  5614. (message "Entities are now displayed as plain text"))))
  5615. (defvar org-custom-properties-overlays nil
  5616. "List of overlays used for custom properties.")
  5617. (make-variable-buffer-local 'org-custom-properties-overlays)
  5618. (defun org-toggle-custom-properties-visibility ()
  5619. "Display or hide properties in `org-custom-properties'."
  5620. (interactive)
  5621. (if org-custom-properties-overlays
  5622. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5623. (setq org-custom-properties-overlays nil))
  5624. (unless (not org-custom-properties)
  5625. (save-excursion
  5626. (save-restriction
  5627. (widen)
  5628. (goto-char (point-min))
  5629. (while (re-search-forward org-property-re nil t)
  5630. (mapc (lambda(p)
  5631. (when (equal p (substring (match-string 1) 1 -1))
  5632. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5633. (overlay-put o 'invisible t)
  5634. (overlay-put o 'org-custom-property t)
  5635. (push o org-custom-properties-overlays))))
  5636. org-custom-properties)))))))
  5637. (defun org-fontify-entities (limit)
  5638. "Find an entity to fontify."
  5639. (let (ee)
  5640. (when org-pretty-entities
  5641. (catch 'match
  5642. (while (re-search-forward
  5643. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5644. limit t)
  5645. (if (and (not (org-in-indented-comment-line))
  5646. (setq ee (org-entity-get (match-string 1)))
  5647. (= (length (nth 6 ee)) 1))
  5648. (let*
  5649. ((end (if (equal (match-string 2) "{}")
  5650. (match-end 2)
  5651. (match-end 1))))
  5652. (add-text-properties
  5653. (match-beginning 0) end
  5654. (list 'font-lock-fontified t))
  5655. (compose-region (match-beginning 0) end
  5656. (nth 6 ee) nil)
  5657. (backward-char 1)
  5658. (throw 'match t))))
  5659. nil))))
  5660. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5661. "Fontify string S like in Org-mode."
  5662. (with-temp-buffer
  5663. (insert s)
  5664. (let ((org-odd-levels-only odd-levels))
  5665. (org-mode)
  5666. (font-lock-fontify-buffer)
  5667. (buffer-string))))
  5668. (defvar org-m nil)
  5669. (defvar org-l nil)
  5670. (defvar org-f nil)
  5671. (defun org-get-level-face (n)
  5672. "Get the right face for match N in font-lock matching of headlines."
  5673. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5674. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5675. (if org-cycle-level-faces
  5676. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5677. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5678. (cond
  5679. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5680. ((eq n 2) org-f)
  5681. (t (if org-level-color-stars-only nil org-f))))
  5682. (defun org-get-todo-face (kwd)
  5683. "Get the right face for a TODO keyword KWD.
  5684. If KWD is a number, get the corresponding match group."
  5685. (if (numberp kwd) (setq kwd (match-string kwd)))
  5686. (or (org-face-from-face-or-color
  5687. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5688. (and (member kwd org-done-keywords) 'org-done)
  5689. 'org-todo))
  5690. (defun org-face-from-face-or-color (context inherit face-or-color)
  5691. "Create a face list that inherits INHERIT, but sets the foreground color.
  5692. When FACE-OR-COLOR is not a string, just return it."
  5693. (if (stringp face-or-color)
  5694. (list :inherit inherit
  5695. (cdr (assoc context org-faces-easy-properties))
  5696. face-or-color)
  5697. face-or-color))
  5698. (defun org-font-lock-add-tag-faces (limit)
  5699. "Add the special tag faces."
  5700. (when (and org-tag-faces org-tags-special-faces-re)
  5701. (while (re-search-forward org-tags-special-faces-re limit t)
  5702. (add-text-properties (match-beginning 1) (match-end 1)
  5703. (list 'face (org-get-tag-face 1)
  5704. 'font-lock-fontified t))
  5705. (backward-char 1))))
  5706. (defun org-font-lock-add-priority-faces (limit)
  5707. "Add the special priority faces."
  5708. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5709. (when (save-match-data (org-at-heading-p))
  5710. (add-text-properties
  5711. (match-beginning 0) (match-end 0)
  5712. (list 'face (or (org-face-from-face-or-color
  5713. 'priority 'org-priority
  5714. (cdr (assoc (char-after (match-beginning 1))
  5715. org-priority-faces)))
  5716. 'org-priority)
  5717. 'font-lock-fontified t)))))
  5718. (defun org-get-tag-face (kwd)
  5719. "Get the right face for a TODO keyword KWD.
  5720. If KWD is a number, get the corresponding match group."
  5721. (if (numberp kwd) (setq kwd (match-string kwd)))
  5722. (or (org-face-from-face-or-color
  5723. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5724. 'org-tag))
  5725. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5726. "Remove fontification and activation overlays from links."
  5727. (font-lock-default-unfontify-region beg end)
  5728. (let* ((buffer-undo-list t)
  5729. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5730. (inhibit-modification-hooks t)
  5731. deactivate-mark buffer-file-name buffer-file-truename)
  5732. (org-decompose-region beg end)
  5733. (remove-text-properties beg end
  5734. '(mouse-face t keymap t org-linked-text t
  5735. invisible t intangible t
  5736. org-no-flyspell t org-emphasis t))
  5737. (org-remove-font-lock-display-properties beg end)))
  5738. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5739. ((raise 0.3) (height 0.7))
  5740. ((raise -0.5))
  5741. ((raise 0.5)))
  5742. "Display properties for showing superscripts and subscripts.")
  5743. (defun org-remove-font-lock-display-properties (beg end)
  5744. "Remove specific display properties that have been added by font lock.
  5745. The will remove the raise properties that are used to show superscripts
  5746. and subscripts."
  5747. (let (next prop)
  5748. (while (< beg end)
  5749. (setq next (next-single-property-change beg 'display nil end)
  5750. prop (get-text-property beg 'display))
  5751. (if (member prop org-script-display)
  5752. (put-text-property beg next 'display nil))
  5753. (setq beg next))))
  5754. (defun org-raise-scripts (limit)
  5755. "Add raise properties to sub/superscripts."
  5756. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5757. (if (re-search-forward
  5758. (if (eq org-use-sub-superscripts t)
  5759. org-match-substring-regexp
  5760. org-match-substring-with-braces-regexp)
  5761. limit t)
  5762. (let* ((pos (point)) table-p comment-p
  5763. (mpos (match-beginning 3))
  5764. (emph-p (get-text-property mpos 'org-emphasis))
  5765. (link-p (get-text-property mpos 'mouse-face))
  5766. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5767. (goto-char (point-at-bol))
  5768. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5769. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5770. (goto-char pos)
  5771. ;; Handle a_b^c
  5772. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5773. (if (or comment-p emph-p link-p keyw-p)
  5774. t
  5775. (put-text-property (match-beginning 3) (match-end 0)
  5776. 'display
  5777. (if (equal (char-after (match-beginning 2)) ?^)
  5778. (nth (if table-p 3 1) org-script-display)
  5779. (nth (if table-p 2 0) org-script-display)))
  5780. (add-text-properties (match-beginning 2) (match-end 2)
  5781. (list 'invisible t
  5782. 'org-dwidth t 'org-dwidth-n 1))
  5783. (if (and (eq (char-after (match-beginning 3)) ?{)
  5784. (eq (char-before (match-end 3)) ?}))
  5785. (progn
  5786. (add-text-properties
  5787. (match-beginning 3) (1+ (match-beginning 3))
  5788. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5789. (add-text-properties
  5790. (1- (match-end 3)) (match-end 3)
  5791. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5792. t)))))
  5793. ;;;; Visibility cycling, including org-goto and indirect buffer
  5794. ;;; Cycling
  5795. (defvar org-cycle-global-status nil)
  5796. (make-variable-buffer-local 'org-cycle-global-status)
  5797. (put 'org-cycle-global-status 'org-state t)
  5798. (defvar org-cycle-subtree-status nil)
  5799. (make-variable-buffer-local 'org-cycle-subtree-status)
  5800. (put 'org-cycle-subtree-status 'org-state t)
  5801. (defvar org-inlinetask-min-level)
  5802. (defun org-unlogged-message (&rest args)
  5803. "Display a message, but avoid logging it in the *Messages* buffer."
  5804. (let ((message-log-max nil))
  5805. (apply 'message args)))
  5806. ;;;###autoload
  5807. (defun org-cycle (&optional arg)
  5808. "TAB-action and visibility cycling for Org-mode.
  5809. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5810. is outline visibility cycling, but it also invokes other actions
  5811. in special contexts.
  5812. - When this function is called with a prefix argument, rotate the entire
  5813. buffer through 3 states (global cycling)
  5814. 1. OVERVIEW: Show only top-level headlines.
  5815. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5816. 3. SHOW ALL: Show everything.
  5817. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5818. determined by the variable `org-startup-folded', and by any VISIBILITY
  5819. properties in the buffer.
  5820. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5821. including any drawers.
  5822. - When inside a table, re-align the table and move to the next field.
  5823. - When point is at the beginning of a headline, rotate the subtree started
  5824. by this line through 3 different states (local cycling)
  5825. 1. FOLDED: Only the main headline is shown.
  5826. 2. CHILDREN: The main headline and the direct children are shown.
  5827. From this state, you can move to one of the children
  5828. and zoom in further.
  5829. 3. SUBTREE: Show the entire subtree, including body text.
  5830. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5831. - When point is at the beginning of an empty headline and the variable
  5832. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5833. of the headline by demoting and promoting it to likely levels. This
  5834. speeds up creation document structure by pressing TAB once or several
  5835. times right after creating a new headline.
  5836. - When there is a numeric prefix, go up to a heading with level ARG, do
  5837. a `show-subtree' and return to the previous cursor position. If ARG
  5838. is negative, go up that many levels.
  5839. - When point is not at the beginning of a headline, execute the global
  5840. binding for TAB, which is re-indenting the line. See the option
  5841. `org-cycle-emulate-tab' for details.
  5842. - Special case: if point is at the beginning of the buffer and there is
  5843. no headline in line 1, this function will act as if called with prefix arg
  5844. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5845. But only if also the variable `org-cycle-global-at-bob' is t."
  5846. (interactive "P")
  5847. (org-load-modules-maybe)
  5848. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5849. (and org-cycle-level-after-item/entry-creation
  5850. (or (org-cycle-level)
  5851. (org-cycle-item-indentation))))
  5852. (let* ((limit-level
  5853. (or org-cycle-max-level
  5854. (and (boundp 'org-inlinetask-min-level)
  5855. org-inlinetask-min-level
  5856. (1- org-inlinetask-min-level))))
  5857. (nstars (and limit-level
  5858. (if org-odd-levels-only
  5859. (and limit-level (1- (* limit-level 2)))
  5860. limit-level)))
  5861. (org-outline-regexp
  5862. (if (not (derived-mode-p 'org-mode))
  5863. outline-regexp
  5864. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5865. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5866. (not (looking-at org-outline-regexp))))
  5867. (org-cycle-hook
  5868. (if bob-special
  5869. (delq 'org-optimize-window-after-visibility-change
  5870. (copy-sequence org-cycle-hook))
  5871. org-cycle-hook))
  5872. (pos (point)))
  5873. (if (or bob-special (equal arg '(4)))
  5874. ;; special case: use global cycling
  5875. (setq arg t))
  5876. (cond
  5877. ((equal arg '(16))
  5878. (setq last-command 'dummy)
  5879. (org-set-startup-visibility)
  5880. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5881. ((equal arg '(64))
  5882. (show-all)
  5883. (org-unlogged-message "Entire buffer visible, including drawers"))
  5884. ;; Table: enter it or move to the next field.
  5885. ((org-at-table-p 'any)
  5886. (if (org-at-table.el-p)
  5887. (message "Use C-c ' to edit table.el tables")
  5888. (if arg (org-table-edit-field t)
  5889. (org-table-justify-field-maybe)
  5890. (call-interactively 'org-table-next-field))))
  5891. ((run-hook-with-args-until-success
  5892. 'org-tab-after-check-for-table-hook))
  5893. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5894. ((eq arg t) (org-cycle-internal-global))
  5895. ;; Drawers: delegate to `org-flag-drawer'.
  5896. ((save-excursion
  5897. (beginning-of-line 1)
  5898. (looking-at org-drawer-regexp))
  5899. (org-flag-drawer ; toggle block visibility
  5900. (not (get-char-property (match-end 0) 'invisible))))
  5901. ;; Show-subtree, ARG levels up from here.
  5902. ((integerp arg)
  5903. (save-excursion
  5904. (org-back-to-heading)
  5905. (outline-up-heading (if (< arg 0) (- arg)
  5906. (- (funcall outline-level) arg)))
  5907. (org-show-subtree)))
  5908. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5909. ((and (featurep 'org-inlinetask)
  5910. (org-inlinetask-at-task-p)
  5911. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5912. (org-inlinetask-toggle-visibility))
  5913. ((org-try-cdlatex-tab))
  5914. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5915. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5916. (save-excursion (beginning-of-line 1)
  5917. (looking-at org-outline-regexp)))
  5918. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5919. (org-cycle-internal-local))
  5920. ;; From there: TAB emulation and template completion.
  5921. (buffer-read-only (org-back-to-heading))
  5922. ((run-hook-with-args-until-success
  5923. 'org-tab-after-check-for-cycling-hook))
  5924. ((org-try-structure-completion))
  5925. ((run-hook-with-args-until-success
  5926. 'org-tab-before-tab-emulation-hook))
  5927. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5928. (or (not (bolp))
  5929. (not (looking-at org-outline-regexp))))
  5930. (call-interactively (global-key-binding "\t")))
  5931. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5932. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5933. (or (and (eq org-cycle-emulate-tab 'white)
  5934. (= (match-end 0) (point-at-eol)))
  5935. (and (eq org-cycle-emulate-tab 'whitestart)
  5936. (>= (match-end 0) pos))))
  5937. t
  5938. (eq org-cycle-emulate-tab t))
  5939. (call-interactively (global-key-binding "\t")))
  5940. (t (save-excursion
  5941. (org-back-to-heading)
  5942. (org-cycle)))))))
  5943. (defun org-cycle-internal-global ()
  5944. "Do the global cycling action."
  5945. ;; Hack to avoid display of messages for .org attachments in Gnus
  5946. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5947. (cond
  5948. ((and (eq last-command this-command)
  5949. (eq org-cycle-global-status 'overview))
  5950. ;; We just created the overview - now do table of contents
  5951. ;; This can be slow in very large buffers, so indicate action
  5952. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5953. (unless ga (org-unlogged-message "CONTENTS..."))
  5954. (org-content)
  5955. (unless ga (org-unlogged-message "CONTENTS...done"))
  5956. (setq org-cycle-global-status 'contents)
  5957. (run-hook-with-args 'org-cycle-hook 'contents))
  5958. ((and (eq last-command this-command)
  5959. (eq org-cycle-global-status 'contents))
  5960. ;; We just showed the table of contents - now show everything
  5961. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5962. (show-all)
  5963. (unless ga (org-unlogged-message "SHOW ALL"))
  5964. (setq org-cycle-global-status 'all)
  5965. (run-hook-with-args 'org-cycle-hook 'all))
  5966. (t
  5967. ;; Default action: go to overview
  5968. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5969. (org-overview)
  5970. (unless ga (org-unlogged-message "OVERVIEW"))
  5971. (setq org-cycle-global-status 'overview)
  5972. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5973. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  5974. (defun org-cycle-internal-local ()
  5975. "Do the local cycling action."
  5976. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5977. ;; First, determine end of headline (EOH), end of subtree or item
  5978. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5979. (save-excursion
  5980. (if (org-at-item-p)
  5981. (progn
  5982. (beginning-of-line)
  5983. (setq struct (org-list-struct))
  5984. (setq eoh (point-at-eol))
  5985. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5986. (setq has-children (org-list-has-child-p (point) struct)))
  5987. (org-back-to-heading)
  5988. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5989. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5990. (setq has-children
  5991. (or (save-excursion
  5992. (let ((level (funcall outline-level)))
  5993. (outline-next-heading)
  5994. (and (org-at-heading-p t)
  5995. (> (funcall outline-level) level))))
  5996. (save-excursion
  5997. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5998. ;; Determine end invisible part of buffer (EOL)
  5999. (beginning-of-line 2)
  6000. ;; XEmacs doesn't have `next-single-char-property-change'
  6001. (if (featurep 'xemacs)
  6002. (while (and (not (eobp)) ;; this is like `next-line'
  6003. (get-char-property (1- (point)) 'invisible))
  6004. (beginning-of-line 2))
  6005. (while (and (not (eobp)) ;; this is like `next-line'
  6006. (get-char-property (1- (point)) 'invisible))
  6007. (goto-char (next-single-char-property-change (point) 'invisible))
  6008. (and (eolp) (beginning-of-line 2))))
  6009. (setq eol (point)))
  6010. ;; Find out what to do next and set `this-command'
  6011. (cond
  6012. ((= eos eoh)
  6013. ;; Nothing is hidden behind this heading
  6014. (unless (org-before-first-heading-p)
  6015. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6016. (org-unlogged-message "EMPTY ENTRY")
  6017. (setq org-cycle-subtree-status nil)
  6018. (save-excursion
  6019. (goto-char eos)
  6020. (outline-next-heading)
  6021. (if (outline-invisible-p) (org-flag-heading nil))))
  6022. ((and (or (>= eol eos)
  6023. (not (string-match "\\S-" (buffer-substring eol eos))))
  6024. (or has-children
  6025. (not (setq children-skipped
  6026. org-cycle-skip-children-state-if-no-children))))
  6027. ;; Entire subtree is hidden in one line: children view
  6028. (unless (org-before-first-heading-p)
  6029. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6030. (if (org-at-item-p)
  6031. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6032. (org-show-entry)
  6033. (org-with-limited-levels (show-children))
  6034. ;; FIXME: This slows down the func way too much.
  6035. ;; How keep drawers hidden in subtree anyway?
  6036. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6037. ;; (org-cycle-hide-drawers 'subtree))
  6038. ;; Fold every list in subtree to top-level items.
  6039. (when (eq org-cycle-include-plain-lists 'integrate)
  6040. (save-excursion
  6041. (org-back-to-heading)
  6042. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6043. (beginning-of-line 1)
  6044. (let* ((struct (org-list-struct))
  6045. (prevs (org-list-prevs-alist struct))
  6046. (end (org-list-get-bottom-point struct)))
  6047. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6048. (org-list-get-all-items (point) struct prevs))
  6049. (goto-char (if (< end eos) end eos)))))))
  6050. (org-unlogged-message "CHILDREN")
  6051. (save-excursion
  6052. (goto-char eos)
  6053. (outline-next-heading)
  6054. (if (outline-invisible-p) (org-flag-heading nil)))
  6055. (setq org-cycle-subtree-status 'children)
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-cycle-hook 'children)))
  6058. ((or children-skipped
  6059. (and (eq last-command this-command)
  6060. (eq org-cycle-subtree-status 'children)))
  6061. ;; We just showed the children, or no children are there,
  6062. ;; now show everything.
  6063. (unless (org-before-first-heading-p)
  6064. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6065. (outline-flag-region eoh eos nil)
  6066. (org-unlogged-message
  6067. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6068. (setq org-cycle-subtree-status 'subtree)
  6069. (unless (org-before-first-heading-p)
  6070. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6071. (t
  6072. ;; Default action: hide the subtree.
  6073. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6074. (outline-flag-region eoh eos t)
  6075. (org-unlogged-message "FOLDED")
  6076. (setq org-cycle-subtree-status 'folded)
  6077. (unless (org-before-first-heading-p)
  6078. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6079. ;;;###autoload
  6080. (defun org-global-cycle (&optional arg)
  6081. "Cycle the global visibility. For details see `org-cycle'.
  6082. With \\[universal-argument] prefix arg, switch to startup visibility.
  6083. With a numeric prefix, show all headlines up to that level."
  6084. (interactive "P")
  6085. (let ((org-cycle-include-plain-lists
  6086. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6087. (cond
  6088. ((integerp arg)
  6089. (show-all)
  6090. (hide-sublevels arg)
  6091. (setq org-cycle-global-status 'contents))
  6092. ((equal arg '(4))
  6093. (org-set-startup-visibility)
  6094. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6095. (t
  6096. (org-cycle '(4))))))
  6097. (defun org-set-startup-visibility ()
  6098. "Set the visibility required by startup options and properties."
  6099. (cond
  6100. ((eq org-startup-folded t)
  6101. (org-cycle '(4)))
  6102. ((eq org-startup-folded 'content)
  6103. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6104. (org-cycle '(4)) (org-cycle '(4)))))
  6105. (unless (eq org-startup-folded 'showeverything)
  6106. (if org-hide-block-startup (org-hide-block-all))
  6107. (org-set-visibility-according-to-property 'no-cleanup)
  6108. (org-cycle-hide-archived-subtrees 'all)
  6109. (org-cycle-hide-drawers 'all)
  6110. (org-cycle-show-empty-lines t)))
  6111. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6112. "Switch subtree visibilities according to :VISIBILITY: property."
  6113. (interactive)
  6114. (let (org-show-entry-below state)
  6115. (save-excursion
  6116. (goto-char (point-min))
  6117. (while (re-search-forward
  6118. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6119. nil t)
  6120. (setq state (match-string 1))
  6121. (save-excursion
  6122. (org-back-to-heading t)
  6123. (hide-subtree)
  6124. (org-reveal)
  6125. (cond
  6126. ((equal state '("fold" "folded"))
  6127. (hide-subtree))
  6128. ((equal state "children")
  6129. (org-show-hidden-entry)
  6130. (show-children))
  6131. ((equal state "content")
  6132. (save-excursion
  6133. (save-restriction
  6134. (org-narrow-to-subtree)
  6135. (org-content))))
  6136. ((member state '("all" "showall"))
  6137. (show-subtree)))))
  6138. (unless no-cleanup
  6139. (org-cycle-hide-archived-subtrees 'all)
  6140. (org-cycle-hide-drawers 'all)
  6141. (org-cycle-show-empty-lines 'all)))))
  6142. ;; This function uses outline-regexp instead of the more fundamental
  6143. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6144. ;; buffers, where outline-regexp is needed.
  6145. (defun org-overview ()
  6146. "Switch to overview mode, showing only top-level headlines.
  6147. Really, this shows all headlines with level equal or greater than the level
  6148. of the first headline in the buffer. This is important, because if the
  6149. first headline is not level one, then (hide-sublevels 1) gives confusing
  6150. results."
  6151. (interactive)
  6152. (let ((pos (point))
  6153. (level (save-excursion
  6154. (goto-char (point-min))
  6155. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6156. (progn
  6157. (goto-char (match-beginning 0))
  6158. (funcall outline-level))))))
  6159. (and level (hide-sublevels level))
  6160. (recenter '(4))
  6161. (goto-char pos)))
  6162. (defun org-content (&optional arg)
  6163. "Show all headlines in the buffer, like a table of contents.
  6164. With numerical argument N, show content up to level N."
  6165. (interactive "P")
  6166. (save-excursion
  6167. ;; Visit all headings and show their offspring
  6168. (and (integerp arg) (org-overview))
  6169. (goto-char (point-max))
  6170. (catch 'exit
  6171. (while (and (progn (condition-case nil
  6172. (outline-previous-visible-heading 1)
  6173. (error (goto-char (point-min))))
  6174. t)
  6175. (looking-at org-outline-regexp))
  6176. (if (integerp arg)
  6177. (show-children (1- arg))
  6178. (show-branches))
  6179. (if (bobp) (throw 'exit nil))))))
  6180. (defun org-optimize-window-after-visibility-change (state)
  6181. "Adjust the window after a change in outline visibility.
  6182. This function is the default value of the hook `org-cycle-hook'."
  6183. (when (get-buffer-window (current-buffer))
  6184. (cond
  6185. ((eq state 'content) nil)
  6186. ((eq state 'all) nil)
  6187. ((eq state 'folded) nil)
  6188. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6189. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6190. (defun org-remove-empty-overlays-at (pos)
  6191. "Remove outline overlays that do not contain non-white stuff."
  6192. (mapc
  6193. (lambda (o)
  6194. (and (eq 'outline (overlay-get o 'invisible))
  6195. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6196. (overlay-end o))))
  6197. (delete-overlay o)))
  6198. (overlays-at pos)))
  6199. (defun org-clean-visibility-after-subtree-move ()
  6200. "Fix visibility issues after moving a subtree."
  6201. ;; First, find a reasonable region to look at:
  6202. ;; Start two siblings above, end three below
  6203. (let* ((beg (save-excursion
  6204. (and (org-get-last-sibling)
  6205. (org-get-last-sibling))
  6206. (point)))
  6207. (end (save-excursion
  6208. (and (org-get-next-sibling)
  6209. (org-get-next-sibling)
  6210. (org-get-next-sibling))
  6211. (if (org-at-heading-p)
  6212. (point-at-eol)
  6213. (point))))
  6214. (level (looking-at "\\*+"))
  6215. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6216. (save-excursion
  6217. (save-restriction
  6218. (narrow-to-region beg end)
  6219. (when re
  6220. ;; Properly fold already folded siblings
  6221. (goto-char (point-min))
  6222. (while (re-search-forward re nil t)
  6223. (if (and (not (outline-invisible-p))
  6224. (save-excursion
  6225. (goto-char (point-at-eol)) (outline-invisible-p)))
  6226. (hide-entry))))
  6227. (org-cycle-show-empty-lines 'overview)
  6228. (org-cycle-hide-drawers 'overview)))))
  6229. (defun org-cycle-show-empty-lines (state)
  6230. "Show empty lines above all visible headlines.
  6231. The region to be covered depends on STATE when called through
  6232. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6233. entire buffer covered. Note that an empty line is only shown if there
  6234. are at least `org-cycle-separator-lines' empty lines before the headline."
  6235. (when (not (= org-cycle-separator-lines 0))
  6236. (save-excursion
  6237. (let* ((n (abs org-cycle-separator-lines))
  6238. (re (cond
  6239. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6240. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6241. (t (let ((ns (number-to-string (- n 2))))
  6242. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6243. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6244. beg end b e)
  6245. (cond
  6246. ((memq state '(overview contents t))
  6247. (setq beg (point-min) end (point-max)))
  6248. ((memq state '(children folded))
  6249. (setq beg (point) end (progn (org-end-of-subtree t t)
  6250. (beginning-of-line 2)
  6251. (point)))))
  6252. (when beg
  6253. (goto-char beg)
  6254. (while (re-search-forward re end t)
  6255. (unless (get-char-property (match-end 1) 'invisible)
  6256. (setq e (match-end 1))
  6257. (if (< org-cycle-separator-lines 0)
  6258. (setq b (save-excursion
  6259. (goto-char (match-beginning 0))
  6260. (org-back-over-empty-lines)
  6261. (if (save-excursion
  6262. (goto-char (max (point-min) (1- (point))))
  6263. (org-at-heading-p))
  6264. (1- (point))
  6265. (point))))
  6266. (setq b (match-beginning 1)))
  6267. (outline-flag-region b e nil)))))))
  6268. ;; Never hide empty lines at the end of the file.
  6269. (save-excursion
  6270. (goto-char (point-max))
  6271. (outline-previous-heading)
  6272. (outline-end-of-heading)
  6273. (if (and (looking-at "[ \t\n]+")
  6274. (= (match-end 0) (point-max)))
  6275. (outline-flag-region (point) (match-end 0) nil))))
  6276. (defun org-show-empty-lines-in-parent ()
  6277. "Move to the parent and re-show empty lines before visible headlines."
  6278. (save-excursion
  6279. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6280. (org-cycle-show-empty-lines context))))
  6281. (defun org-files-list ()
  6282. "Return `org-agenda-files' list, plus all open org-mode files.
  6283. This is useful for operations that need to scan all of a user's
  6284. open and agenda-wise Org files."
  6285. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6286. (dolist (buf (buffer-list))
  6287. (with-current-buffer buf
  6288. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6289. (let ((file (expand-file-name (buffer-file-name))))
  6290. (unless (member file files)
  6291. (push file files))))))
  6292. files))
  6293. (defsubst org-entry-beginning-position ()
  6294. "Return the beginning position of the current entry."
  6295. (save-excursion (outline-back-to-heading t) (point)))
  6296. (defsubst org-entry-end-position ()
  6297. "Return the end position of the current entry."
  6298. (save-excursion (outline-next-heading) (point)))
  6299. (defun org-cycle-hide-drawers (state &optional exceptions)
  6300. "Re-hide all drawers after a visibility state change.
  6301. When non-nil, optional argument EXCEPTIONS is a list of strings
  6302. specifying which drawers should not be hidden."
  6303. (when (and (derived-mode-p 'org-mode)
  6304. (not (memq state '(overview folded contents))))
  6305. (save-excursion
  6306. (let* ((globalp (memq state '(contents all)))
  6307. (beg (if globalp (point-min) (point)))
  6308. (end (if globalp (point-max)
  6309. (if (eq state 'children)
  6310. (save-excursion (outline-next-heading) (point))
  6311. (org-end-of-subtree t)))))
  6312. (goto-char beg)
  6313. (while (re-search-forward org-drawer-regexp end t)
  6314. (unless (member-ignore-case (match-string 1) exceptions)
  6315. (org-flag-drawer t)))))))
  6316. (defun org-cycle-hide-inline-tasks (state)
  6317. "Re-hide inline tasks when switching to 'contents or 'children
  6318. visibility state."
  6319. (case state
  6320. (contents
  6321. (when (org-bound-and-true-p org-inlinetask-min-level)
  6322. (hide-sublevels (1- org-inlinetask-min-level))))
  6323. (children
  6324. (when (featurep 'org-inlinetask)
  6325. (save-excursion
  6326. (while (and (outline-next-heading)
  6327. (org-inlinetask-at-task-p))
  6328. (org-inlinetask-toggle-visibility)
  6329. (org-inlinetask-goto-end)))))))
  6330. (defun org-flag-drawer (flag &optional element)
  6331. "When FLAG is non-nil, hide the drawer we are at.
  6332. Otherwise make it visible. When optional argument ELEMENT is
  6333. a parsed drawer, as returned by `org-element-at-point', hide or
  6334. show that drawer instead."
  6335. (let ((drawer (or element (org-element-at-point))))
  6336. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6337. (save-excursion
  6338. (goto-char (org-element-property :post-affiliated drawer))
  6339. (outline-flag-region
  6340. (line-end-position)
  6341. (progn (goto-char (org-element-property :end drawer))
  6342. (skip-chars-backward " \r\t\n")
  6343. (line-end-position))
  6344. flag)))))
  6345. (defun org-subtree-end-visible-p ()
  6346. "Is the end of the current subtree visible?"
  6347. (pos-visible-in-window-p
  6348. (save-excursion (org-end-of-subtree t) (point))))
  6349. (defun org-first-headline-recenter (&optional N)
  6350. "Move cursor to the first headline and recenter the headline.
  6351. Optional argument N means put the headline into the Nth line of the window."
  6352. (goto-char (point-min))
  6353. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6354. (beginning-of-line)
  6355. (recenter (prefix-numeric-value N))))
  6356. ;;; Saving and restoring visibility
  6357. (defun org-outline-overlay-data (&optional use-markers)
  6358. "Return a list of the locations of all outline overlays.
  6359. These are overlays with the `invisible' property value `outline'.
  6360. The return value is a list of cons cells, with start and stop
  6361. positions for each overlay.
  6362. If USE-MARKERS is set, return the positions as markers."
  6363. (let (beg end)
  6364. (save-excursion
  6365. (save-restriction
  6366. (widen)
  6367. (delq nil
  6368. (mapcar (lambda (o)
  6369. (when (eq (overlay-get o 'invisible) 'outline)
  6370. (setq beg (overlay-start o)
  6371. end (overlay-end o))
  6372. (and beg end (> end beg)
  6373. (if use-markers
  6374. (cons (move-marker (make-marker) beg)
  6375. (move-marker (make-marker) end))
  6376. (cons beg end)))))
  6377. (overlays-in (point-min) (point-max))))))))
  6378. (defun org-set-outline-overlay-data (data)
  6379. "Create visibility overlays for all positions in DATA.
  6380. DATA should have been made by `org-outline-overlay-data'."
  6381. (let (o)
  6382. (save-excursion
  6383. (save-restriction
  6384. (widen)
  6385. (show-all)
  6386. (mapc (lambda (c)
  6387. (outline-flag-region (car c) (cdr c) t))
  6388. data)))))
  6389. ;;; Folding of blocks
  6390. (defvar org-hide-block-overlays nil
  6391. "Overlays hiding blocks.")
  6392. (make-variable-buffer-local 'org-hide-block-overlays)
  6393. (defun org-block-map (function &optional start end)
  6394. "Call FUNCTION at the head of all source blocks in the current buffer.
  6395. Optional arguments START and END can be used to limit the range."
  6396. (let ((start (or start (point-min)))
  6397. (end (or end (point-max))))
  6398. (save-excursion
  6399. (goto-char start)
  6400. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6401. (save-excursion
  6402. (save-match-data
  6403. (goto-char (match-beginning 0))
  6404. (funcall function)))))))
  6405. (defun org-hide-block-toggle-all ()
  6406. "Toggle the visibility of all blocks in the current buffer."
  6407. (org-block-map #'org-hide-block-toggle))
  6408. (defun org-hide-block-all ()
  6409. "Fold all blocks in the current buffer."
  6410. (interactive)
  6411. (org-show-block-all)
  6412. (org-block-map #'org-hide-block-toggle-maybe))
  6413. (defun org-show-block-all ()
  6414. "Unfold all blocks in the current buffer."
  6415. (interactive)
  6416. (mapc 'delete-overlay org-hide-block-overlays)
  6417. (setq org-hide-block-overlays nil))
  6418. (defun org-hide-block-toggle-maybe ()
  6419. "Toggle visibility of block at point."
  6420. (interactive)
  6421. (let ((case-fold-search t))
  6422. (if (save-excursion
  6423. (beginning-of-line 1)
  6424. (looking-at org-block-regexp))
  6425. (progn (org-hide-block-toggle)
  6426. t) ;; to signal that we took action
  6427. nil))) ;; to signal that we did not
  6428. (defun org-hide-block-toggle (&optional force)
  6429. "Toggle the visibility of the current block."
  6430. (interactive)
  6431. (save-excursion
  6432. (beginning-of-line)
  6433. (if (re-search-forward org-block-regexp nil t)
  6434. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6435. (end (match-end 0)) ;; end of entire body
  6436. ov)
  6437. (if (memq t (mapcar (lambda (overlay)
  6438. (eq (overlay-get overlay 'invisible)
  6439. 'org-hide-block))
  6440. (overlays-at start)))
  6441. (if (or (not force) (eq force 'off))
  6442. (mapc (lambda (ov)
  6443. (when (member ov org-hide-block-overlays)
  6444. (setq org-hide-block-overlays
  6445. (delq ov org-hide-block-overlays)))
  6446. (when (eq (overlay-get ov 'invisible)
  6447. 'org-hide-block)
  6448. (delete-overlay ov)))
  6449. (overlays-at start)))
  6450. (setq ov (make-overlay start end))
  6451. (overlay-put ov 'invisible 'org-hide-block)
  6452. ;; make the block accessible to isearch
  6453. (overlay-put
  6454. ov 'isearch-open-invisible
  6455. (lambda (ov)
  6456. (when (member ov org-hide-block-overlays)
  6457. (setq org-hide-block-overlays
  6458. (delq ov org-hide-block-overlays)))
  6459. (when (eq (overlay-get ov 'invisible)
  6460. 'org-hide-block)
  6461. (delete-overlay ov))))
  6462. (push ov org-hide-block-overlays)))
  6463. (user-error "Not looking at a source block"))))
  6464. ;; org-tab-after-check-for-cycling-hook
  6465. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6466. ;; Remove overlays when changing major mode
  6467. (add-hook 'org-mode-hook
  6468. (lambda () (org-add-hook 'change-major-mode-hook
  6469. 'org-show-block-all 'append 'local)))
  6470. ;;; Org-goto
  6471. (defvar org-goto-window-configuration nil)
  6472. (defvar org-goto-marker nil)
  6473. (defvar org-goto-map)
  6474. (defun org-goto-map ()
  6475. "Set the keymap `org-goto'."
  6476. (setq org-goto-map
  6477. (let ((map (make-sparse-keymap)))
  6478. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6479. mouse-drag-region universal-argument org-occur))
  6480. cmd)
  6481. (while (setq cmd (pop cmds))
  6482. (substitute-key-definition cmd cmd map global-map)))
  6483. (suppress-keymap map)
  6484. (org-defkey map "\C-m" 'org-goto-ret)
  6485. (org-defkey map [(return)] 'org-goto-ret)
  6486. (org-defkey map [(left)] 'org-goto-left)
  6487. (org-defkey map [(right)] 'org-goto-right)
  6488. (org-defkey map [(control ?g)] 'org-goto-quit)
  6489. (org-defkey map "\C-i" 'org-cycle)
  6490. (org-defkey map [(tab)] 'org-cycle)
  6491. (org-defkey map [(down)] 'outline-next-visible-heading)
  6492. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6493. (if org-goto-auto-isearch
  6494. (if (fboundp 'define-key-after)
  6495. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6496. nil)
  6497. (org-defkey map "q" 'org-goto-quit)
  6498. (org-defkey map "n" 'outline-next-visible-heading)
  6499. (org-defkey map "p" 'outline-previous-visible-heading)
  6500. (org-defkey map "f" 'outline-forward-same-level)
  6501. (org-defkey map "b" 'outline-backward-same-level)
  6502. (org-defkey map "u" 'outline-up-heading))
  6503. (org-defkey map "/" 'org-occur)
  6504. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6505. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6506. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6507. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6508. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6509. map)))
  6510. (defconst org-goto-help
  6511. "Browse buffer copy, to find location or copy text.%s
  6512. RET=jump to location C-g=quit and return to previous location
  6513. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6514. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6515. (defun org-goto (&optional alternative-interface)
  6516. "Look up a different location in the current file, keeping current visibility.
  6517. When you want look-up or go to a different location in a
  6518. document, the fastest way is often to fold the entire buffer and
  6519. then dive into the tree. This method has the disadvantage, that
  6520. the previous location will be folded, which may not be what you
  6521. want.
  6522. This command works around this by showing a copy of the current
  6523. buffer in an indirect buffer, in overview mode. You can dive
  6524. into the tree in that copy, use org-occur and incremental search
  6525. to find a location. When pressing RET or `Q', the command
  6526. returns to the original buffer in which the visibility is still
  6527. unchanged. After RET it will also jump to the location selected
  6528. in the indirect buffer and expose the headline hierarchy above.
  6529. With a prefix argument, use the alternative interface: e.g. if
  6530. `org-goto-interface' is 'outline use 'outline-path-completion."
  6531. (interactive "P")
  6532. (org-goto-map)
  6533. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6534. (org-refile-use-outline-path t)
  6535. (org-refile-target-verify-function nil)
  6536. (interface
  6537. (if (not alternative-interface)
  6538. org-goto-interface
  6539. (if (eq org-goto-interface 'outline)
  6540. 'outline-path-completion
  6541. 'outline)))
  6542. (org-goto-start-pos (point))
  6543. (selected-point
  6544. (if (eq interface 'outline)
  6545. (car (org-get-location (current-buffer) org-goto-help))
  6546. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6547. (org-refile-check-position pa)
  6548. (nth 3 pa)))))
  6549. (if selected-point
  6550. (progn
  6551. (org-mark-ring-push org-goto-start-pos)
  6552. (goto-char selected-point)
  6553. (if (or (outline-invisible-p) (org-invisible-p2))
  6554. (org-show-context 'org-goto)))
  6555. (message "Quit"))))
  6556. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6557. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6558. (defvar org-goto-local-auto-isearch-map) ; defined below
  6559. (defun org-get-location (buf help)
  6560. "Let the user select a location in the Org-mode buffer BUF.
  6561. This function uses a recursive edit. It returns the selected position
  6562. or nil."
  6563. (org-no-popups
  6564. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6565. (isearch-hide-immediately nil)
  6566. (isearch-search-fun-function
  6567. (lambda () 'org-goto-local-search-headings))
  6568. (org-goto-selected-point org-goto-exit-command))
  6569. (save-excursion
  6570. (save-window-excursion
  6571. (delete-other-windows)
  6572. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6573. (org-pop-to-buffer-same-window
  6574. (condition-case nil
  6575. (make-indirect-buffer (current-buffer) "*org-goto*")
  6576. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6577. (with-output-to-temp-buffer "*Org Help*"
  6578. (princ (format help (if org-goto-auto-isearch
  6579. " Just type for auto-isearch."
  6580. " n/p/f/b/u to navigate, q to quit."))))
  6581. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6582. (setq buffer-read-only nil)
  6583. (let ((org-startup-truncated t)
  6584. (org-startup-folded nil)
  6585. (org-startup-align-all-tables nil))
  6586. (org-mode)
  6587. (org-overview))
  6588. (setq buffer-read-only t)
  6589. (if (and (boundp 'org-goto-start-pos)
  6590. (integer-or-marker-p org-goto-start-pos))
  6591. (let ((org-show-hierarchy-above t)
  6592. (org-show-siblings t)
  6593. (org-show-following-heading t))
  6594. (goto-char org-goto-start-pos)
  6595. (and (outline-invisible-p) (org-show-context)))
  6596. (goto-char (point-min)))
  6597. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6598. (message "Select location and press RET")
  6599. (use-local-map org-goto-map)
  6600. (recursive-edit)))
  6601. (kill-buffer "*org-goto*")
  6602. (cons org-goto-selected-point org-goto-exit-command))))
  6603. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6604. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6605. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6606. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6607. (defun org-goto-local-search-headings (string bound noerror)
  6608. "Search and make sure that any matches are in headlines."
  6609. (catch 'return
  6610. (while (if isearch-forward
  6611. (search-forward string bound noerror)
  6612. (search-backward string bound noerror))
  6613. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6614. (and (member :headline context)
  6615. (not (member :tags context))))
  6616. (throw 'return (point))))))
  6617. (defun org-goto-local-auto-isearch ()
  6618. "Start isearch."
  6619. (interactive)
  6620. (goto-char (point-min))
  6621. (let ((keys (this-command-keys)))
  6622. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6623. (isearch-mode t)
  6624. (isearch-process-search-char (string-to-char keys)))))
  6625. (defun org-goto-ret (&optional arg)
  6626. "Finish `org-goto' by going to the new location."
  6627. (interactive "P")
  6628. (setq org-goto-selected-point (point)
  6629. org-goto-exit-command 'return)
  6630. (throw 'exit nil))
  6631. (defun org-goto-left ()
  6632. "Finish `org-goto' by going to the new location."
  6633. (interactive)
  6634. (if (org-at-heading-p)
  6635. (progn
  6636. (beginning-of-line 1)
  6637. (setq org-goto-selected-point (point)
  6638. org-goto-exit-command 'left)
  6639. (throw 'exit nil))
  6640. (user-error "Not on a heading")))
  6641. (defun org-goto-right ()
  6642. "Finish `org-goto' by going to the new location."
  6643. (interactive)
  6644. (if (org-at-heading-p)
  6645. (progn
  6646. (setq org-goto-selected-point (point)
  6647. org-goto-exit-command 'right)
  6648. (throw 'exit nil))
  6649. (user-error "Not on a heading")))
  6650. (defun org-goto-quit ()
  6651. "Finish `org-goto' without cursor motion."
  6652. (interactive)
  6653. (setq org-goto-selected-point nil)
  6654. (setq org-goto-exit-command 'quit)
  6655. (throw 'exit nil))
  6656. ;;; Indirect buffer display of subtrees
  6657. (defvar org-indirect-dedicated-frame nil
  6658. "This is the frame being used for indirect tree display.")
  6659. (defvar org-last-indirect-buffer nil)
  6660. (defun org-tree-to-indirect-buffer (&optional arg)
  6661. "Create indirect buffer and narrow it to current subtree.
  6662. With a numerical prefix ARG, go up to this level and then take that tree.
  6663. If ARG is negative, go up that many levels.
  6664. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6665. indirect buffer previously made with this command, to avoid proliferation of
  6666. indirect buffers. However, when you call the command with a \
  6667. \\[universal-argument] prefix, or
  6668. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6669. is kept so that you can work with several indirect buffers at the same time.
  6670. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6671. \\[universal-argument] prefix also
  6672. requests that a new frame be made for the new buffer, so that the dedicated
  6673. frame is not changed."
  6674. (interactive "P")
  6675. (let ((cbuf (current-buffer))
  6676. (cwin (selected-window))
  6677. (pos (point))
  6678. beg end level heading ibuf)
  6679. (save-excursion
  6680. (org-back-to-heading t)
  6681. (when (numberp arg)
  6682. (setq level (org-outline-level))
  6683. (if (< arg 0) (setq arg (+ level arg)))
  6684. (while (> (setq level (org-outline-level)) arg)
  6685. (org-up-heading-safe)))
  6686. (setq beg (point)
  6687. heading (org-get-heading))
  6688. (org-end-of-subtree t t)
  6689. (if (org-at-heading-p) (backward-char 1))
  6690. (setq end (point)))
  6691. (if (and (buffer-live-p org-last-indirect-buffer)
  6692. (not (eq org-indirect-buffer-display 'new-frame))
  6693. (not arg))
  6694. (kill-buffer org-last-indirect-buffer))
  6695. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6696. org-last-indirect-buffer ibuf)
  6697. (cond
  6698. ((or (eq org-indirect-buffer-display 'new-frame)
  6699. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6700. (select-frame (make-frame))
  6701. (delete-other-windows)
  6702. (org-pop-to-buffer-same-window ibuf)
  6703. (org-set-frame-title heading))
  6704. ((eq org-indirect-buffer-display 'dedicated-frame)
  6705. (raise-frame
  6706. (select-frame (or (and org-indirect-dedicated-frame
  6707. (frame-live-p org-indirect-dedicated-frame)
  6708. org-indirect-dedicated-frame)
  6709. (setq org-indirect-dedicated-frame (make-frame)))))
  6710. (delete-other-windows)
  6711. (org-pop-to-buffer-same-window ibuf)
  6712. (org-set-frame-title (concat "Indirect: " heading)))
  6713. ((eq org-indirect-buffer-display 'current-window)
  6714. (org-pop-to-buffer-same-window ibuf))
  6715. ((eq org-indirect-buffer-display 'other-window)
  6716. (pop-to-buffer ibuf))
  6717. (t (error "Invalid value")))
  6718. (if (featurep 'xemacs)
  6719. (save-excursion (org-mode) (turn-on-font-lock)))
  6720. (narrow-to-region beg end)
  6721. (show-all)
  6722. (goto-char pos)
  6723. (run-hook-with-args 'org-cycle-hook 'all)
  6724. (and (window-live-p cwin) (select-window cwin))))
  6725. (defun org-get-indirect-buffer (&optional buffer heading)
  6726. (setq buffer (or buffer (current-buffer)))
  6727. (let ((n 1) (base (buffer-name buffer)) bname)
  6728. (while (buffer-live-p
  6729. (get-buffer
  6730. (setq bname
  6731. (concat base "-"
  6732. (if heading (concat heading "-" (number-to-string n))
  6733. (number-to-string n))))))
  6734. (setq n (1+ n)))
  6735. (condition-case nil
  6736. (make-indirect-buffer buffer bname 'clone)
  6737. (error (make-indirect-buffer buffer bname)))))
  6738. (defun org-set-frame-title (title)
  6739. "Set the title of the current frame to the string TITLE."
  6740. ;; FIXME: how to name a single frame in XEmacs???
  6741. (unless (featurep 'xemacs)
  6742. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6743. ;;;; Structure editing
  6744. ;;; Inserting headlines
  6745. (defun org-previous-line-empty-p (&optional next)
  6746. "Is the previous line a blank line?
  6747. When NEXT is non-nil, check the next line instead."
  6748. (save-excursion
  6749. (and (not (bobp))
  6750. (or (beginning-of-line (if next 2 0)) t)
  6751. (save-match-data
  6752. (looking-at "[ \t]*$")))))
  6753. (defun org-insert-heading (&optional arg invisible-ok)
  6754. "Insert a new heading or item with same depth at point.
  6755. If point is in a plain list and ARG is nil, create a new list item.
  6756. With one universal prefix argument, insert a heading even in lists.
  6757. With two universal prefix arguments, insert the heading at the end
  6758. of the parent subtree.
  6759. If point is at the beginning of a headline, insert a sibling before
  6760. the current headline. If point is not at the beginning, split the line
  6761. and create a new headline with the text in the current line after point
  6762. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6763. If point is at the beginning of a normal line, turn this line into
  6764. a heading.
  6765. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6766. This is important for non-interactive uses of the command."
  6767. (interactive "P")
  6768. (if (org-called-interactively-p 'any) (org-reveal))
  6769. (let ((itemp (org-in-item-p))
  6770. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6771. (respect-content (or org-insert-heading-respect-content
  6772. (equal arg '(16))))
  6773. (initial-content "")
  6774. (adjust-empty-lines t))
  6775. (cond
  6776. ((or (= (buffer-size) 0)
  6777. (and (not (save-excursion
  6778. (and (ignore-errors (org-back-to-heading invisible-ok))
  6779. (org-at-heading-p))))
  6780. (or arg (not itemp))))
  6781. ;; At beginning of buffer or so high up that only a heading
  6782. ;; makes sense.
  6783. (insert
  6784. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6785. (if (org-in-src-block-p) ",* " "* "))
  6786. (run-hooks 'org-insert-heading-hook))
  6787. ((and itemp (not (equal arg '(4))))
  6788. ;; Insert an item
  6789. (org-insert-item))
  6790. (t
  6791. ;; Insert a heading
  6792. (save-restriction
  6793. (widen)
  6794. (let* ((level nil)
  6795. (on-heading (org-at-heading-p))
  6796. (empty-line-p (if on-heading
  6797. (org-previous-line-empty-p)
  6798. ;; We will decide later
  6799. nil))
  6800. ;; Get a level string to fall back on
  6801. (fix-level
  6802. (save-excursion
  6803. (org-back-to-heading t)
  6804. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6805. (looking-at org-outline-regexp)
  6806. (make-string (1- (length (match-string 0))) ?*)))
  6807. (stars
  6808. (save-excursion
  6809. (condition-case nil
  6810. (progn
  6811. (org-back-to-heading invisible-ok)
  6812. (when (and (not on-heading)
  6813. (featurep 'org-inlinetask)
  6814. (integerp org-inlinetask-min-level)
  6815. (>= (length (match-string 0))
  6816. org-inlinetask-min-level))
  6817. ;; Find a heading level before the inline task
  6818. (while (and (setq level (org-up-heading-safe))
  6819. (>= level org-inlinetask-min-level)))
  6820. (if (org-at-heading-p)
  6821. (org-back-to-heading invisible-ok)
  6822. (error "This should not happen")))
  6823. (unless (and (save-excursion
  6824. (save-match-data
  6825. (org-backward-heading-same-level
  6826. 1 invisible-ok))
  6827. (= (point) (match-beginning 0)))
  6828. (not (org-previous-line-empty-p t)))
  6829. (setq empty-line-p (or empty-line-p
  6830. (org-previous-line-empty-p))))
  6831. (match-string 0))
  6832. (error (or fix-level "* ")))))
  6833. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6834. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6835. pos hide-previous previous-pos)
  6836. ;; If we insert after content, move there and clean up whitespace
  6837. (when respect-content
  6838. (org-end-of-subtree nil t)
  6839. (skip-chars-backward " \r\n")
  6840. (and (looking-at "[ \t]+") (replace-match ""))
  6841. (unless (eobp) (forward-char 1))
  6842. (when (looking-at "^\\*")
  6843. (unless (bobp) (backward-char 1))
  6844. (insert "\n")))
  6845. ;; If we are splitting, grab the text that should be moved to the new headline
  6846. (when may-split
  6847. (if (org-on-heading-p)
  6848. ;; This is a heading, we split intelligently (keeping tags)
  6849. (let ((pos (point)))
  6850. (goto-char (point-at-bol))
  6851. (unless (looking-at org-complex-heading-regexp)
  6852. (error "This should not happen"))
  6853. (when (and (match-beginning 4)
  6854. (> pos (match-beginning 4))
  6855. (< pos (match-end 4)))
  6856. (setq initial-content (buffer-substring pos (match-end 4)))
  6857. (goto-char pos)
  6858. (delete-region (point) (match-end 4))
  6859. (if (looking-at "[ \t]*$")
  6860. (replace-match "")
  6861. (insert (make-string (length initial-content) ?\ )))
  6862. (setq initial-content (org-trim initial-content)))
  6863. (goto-char pos))
  6864. ;; a normal line
  6865. (unless (bolp)
  6866. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6867. (delete-region (point) (point-at-eol))
  6868. (setq initial-content (org-trim initial-content)))))
  6869. ;; If we are at the beginning of the line, insert before it. Else after
  6870. (cond
  6871. ((and (bolp) (looking-at "[ \t]*$")))
  6872. ((and (bolp) (not (looking-at "[ \t]*$")))
  6873. (open-line 1))
  6874. (t
  6875. (goto-char (point-at-eol))
  6876. (insert "\n")))
  6877. ;; Insert the new heading
  6878. (insert stars)
  6879. (just-one-space)
  6880. (insert initial-content)
  6881. (when adjust-empty-lines
  6882. (if (or (not blank)
  6883. (and blank (not (org-previous-line-empty-p))))
  6884. (org-N-empty-lines-before-current (if blank 1 0))))
  6885. (run-hooks 'org-insert-heading-hook)))))))
  6886. (defun org-N-empty-lines-before-current (N)
  6887. "Make the number of empty lines before current exactly N.
  6888. So this will delete or add empty lines."
  6889. (save-excursion
  6890. (goto-char (point-at-bol))
  6891. (if (looking-back "\\s-+" nil 'greedy)
  6892. (replace-match ""))
  6893. (or (bobp) (insert "\n"))
  6894. (while (> N 0)
  6895. (insert "\n")
  6896. (setq N (1- N)))))
  6897. (defun org-get-heading (&optional no-tags no-todo)
  6898. "Return the heading of the current entry, without the stars.
  6899. When NO-TAGS is non-nil, don't include tags.
  6900. When NO-TODO is non-nil, don't include TODO keywords."
  6901. (save-excursion
  6902. (org-back-to-heading t)
  6903. (cond
  6904. ((and no-tags no-todo)
  6905. (looking-at org-complex-heading-regexp)
  6906. (match-string 4))
  6907. (no-tags
  6908. (looking-at (concat org-outline-regexp
  6909. "\\(.*?\\)"
  6910. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6911. (match-string 1))
  6912. (no-todo
  6913. (looking-at org-todo-line-regexp)
  6914. (match-string 3))
  6915. (t (looking-at org-heading-regexp)
  6916. (match-string 2)))))
  6917. (defvar orgstruct-mode) ; defined below
  6918. (defun org-heading-components ()
  6919. "Return the components of the current heading.
  6920. This is a list with the following elements:
  6921. - the level as an integer
  6922. - the reduced level, different if `org-odd-levels-only' is set.
  6923. - the TODO keyword, or nil
  6924. - the priority character, like ?A, or nil if no priority is given
  6925. - the headline text itself, or the tags string if no headline text
  6926. - the tags string, or nil."
  6927. (save-excursion
  6928. (org-back-to-heading t)
  6929. (if (let (case-fold-search)
  6930. (looking-at
  6931. (if orgstruct-mode
  6932. org-heading-regexp
  6933. org-complex-heading-regexp)))
  6934. (if orgstruct-mode
  6935. (list (length (match-string 1))
  6936. (org-reduced-level (length (match-string 1)))
  6937. nil
  6938. nil
  6939. (match-string 2)
  6940. nil)
  6941. (list (length (match-string 1))
  6942. (org-reduced-level (length (match-string 1)))
  6943. (org-match-string-no-properties 2)
  6944. (and (match-end 3) (aref (match-string 3) 2))
  6945. (org-match-string-no-properties 4)
  6946. (org-match-string-no-properties 5))))))
  6947. (defun org-get-entry ()
  6948. "Get the entry text, after heading, entire subtree."
  6949. (save-excursion
  6950. (org-back-to-heading t)
  6951. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6952. (defun org-insert-heading-after-current ()
  6953. "Insert a new heading with same level as current, after current subtree."
  6954. (interactive)
  6955. (org-back-to-heading)
  6956. (org-insert-heading)
  6957. (org-move-subtree-down)
  6958. (end-of-line 1))
  6959. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6960. "Insert heading with `org-insert-heading-respect-content' set to t."
  6961. (interactive "P")
  6962. (let ((org-insert-heading-respect-content t))
  6963. (org-insert-heading '(4) invisible-ok)))
  6964. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6965. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6966. (interactive "P")
  6967. (let ((org-insert-heading-respect-content t))
  6968. (org-insert-todo-heading force-state '(4))))
  6969. (defun org-insert-todo-heading (arg &optional force-heading)
  6970. "Insert a new heading with the same level and TODO state as current heading.
  6971. If the heading has no TODO state, or if the state is DONE, use the first
  6972. state (TODO by default). Also one prefix arg, force first state. With two
  6973. prefix args, force inserting at the end of the parent subtree."
  6974. (interactive "P")
  6975. (when (or force-heading (not (org-insert-item 'checkbox)))
  6976. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6977. force-heading))
  6978. (save-excursion
  6979. (org-back-to-heading)
  6980. (outline-previous-heading)
  6981. (looking-at org-todo-line-regexp))
  6982. (let*
  6983. ((new-mark-x
  6984. (if (or arg
  6985. (not (match-beginning 2))
  6986. (member (match-string 2) org-done-keywords))
  6987. (car org-todo-keywords-1)
  6988. (match-string 2)))
  6989. (new-mark
  6990. (or
  6991. (run-hook-with-args-until-success
  6992. 'org-todo-get-default-hook new-mark-x nil)
  6993. new-mark-x)))
  6994. (beginning-of-line 1)
  6995. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6996. (if org-treat-insert-todo-heading-as-state-change
  6997. (org-todo new-mark)
  6998. (insert new-mark " "))))
  6999. (when org-provide-todo-statistics
  7000. (org-update-parent-todo-statistics))))
  7001. (defun org-insert-subheading (arg)
  7002. "Insert a new subheading and demote it.
  7003. Works for outline headings and for plain lists alike."
  7004. (interactive "P")
  7005. (org-insert-heading arg)
  7006. (cond
  7007. ((org-at-heading-p) (org-do-demote))
  7008. ((org-at-item-p) (org-indent-item))))
  7009. (defun org-insert-todo-subheading (arg)
  7010. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7011. Works for outline headings and for plain lists alike."
  7012. (interactive "P")
  7013. (org-insert-todo-heading arg)
  7014. (cond
  7015. ((org-at-heading-p) (org-do-demote))
  7016. ((org-at-item-p) (org-indent-item))))
  7017. ;;; Promotion and Demotion
  7018. (defvar org-after-demote-entry-hook nil
  7019. "Hook run after an entry has been demoted.
  7020. The cursor will be at the beginning of the entry.
  7021. When a subtree is being demoted, the hook will be called for each node.")
  7022. (defvar org-after-promote-entry-hook nil
  7023. "Hook run after an entry has been promoted.
  7024. The cursor will be at the beginning of the entry.
  7025. When a subtree is being promoted, the hook will be called for each node.")
  7026. (defun org-promote-subtree ()
  7027. "Promote the entire subtree.
  7028. See also `org-promote'."
  7029. (interactive)
  7030. (save-excursion
  7031. (org-with-limited-levels (org-map-tree 'org-promote)))
  7032. (org-fix-position-after-promote))
  7033. (defun org-demote-subtree ()
  7034. "Demote the entire subtree. See `org-demote'.
  7035. See also `org-promote'."
  7036. (interactive)
  7037. (save-excursion
  7038. (org-with-limited-levels (org-map-tree 'org-demote)))
  7039. (org-fix-position-after-promote))
  7040. (defun org-do-promote ()
  7041. "Promote the current heading higher up the tree.
  7042. If the region is active in `transient-mark-mode', promote all headings
  7043. in the region."
  7044. (interactive)
  7045. (save-excursion
  7046. (if (org-region-active-p)
  7047. (org-map-region 'org-promote (region-beginning) (region-end))
  7048. (org-promote)))
  7049. (org-fix-position-after-promote))
  7050. (defun org-do-demote ()
  7051. "Demote the current heading lower down the tree.
  7052. If the region is active in `transient-mark-mode', demote all headings
  7053. in the region."
  7054. (interactive)
  7055. (save-excursion
  7056. (if (org-region-active-p)
  7057. (org-map-region 'org-demote (region-beginning) (region-end))
  7058. (org-demote)))
  7059. (org-fix-position-after-promote))
  7060. (defun org-fix-position-after-promote ()
  7061. "Make sure that after pro/demotion cursor position is right."
  7062. (let ((pos (point)))
  7063. (when (save-excursion
  7064. (beginning-of-line 1)
  7065. (looking-at org-todo-line-regexp)
  7066. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7067. (cond ((eobp) (insert " "))
  7068. ((eolp) (insert " "))
  7069. ((equal (char-after) ?\ ) (forward-char 1))))))
  7070. (defun org-current-level ()
  7071. "Return the level of the current entry, or nil if before the first headline.
  7072. The level is the number of stars at the beginning of the headline."
  7073. (save-excursion
  7074. (org-with-limited-levels
  7075. (if (ignore-errors (org-back-to-heading t))
  7076. (funcall outline-level)))))
  7077. (defun org-get-previous-line-level ()
  7078. "Return the outline depth of the last headline before the current line.
  7079. Returns 0 for the first headline in the buffer, and nil if before the
  7080. first headline."
  7081. (let ((current-level (org-current-level))
  7082. (prev-level (when (> (line-number-at-pos) 1)
  7083. (save-excursion
  7084. (beginning-of-line 0)
  7085. (org-current-level)))))
  7086. (cond ((null current-level) nil) ; Before first headline
  7087. ((null prev-level) 0) ; At first headline
  7088. (prev-level))))
  7089. (defun org-reduced-level (l)
  7090. "Compute the effective level of a heading.
  7091. This takes into account the setting of `org-odd-levels-only'."
  7092. (cond
  7093. ((zerop l) 0)
  7094. (org-odd-levels-only (1+ (floor (/ l 2))))
  7095. (t l)))
  7096. (defun org-level-increment ()
  7097. "Return the number of stars that will be added or removed at a
  7098. time to headlines when structure editing, based on the value of
  7099. `org-odd-levels-only'."
  7100. (if org-odd-levels-only 2 1))
  7101. (defun org-get-valid-level (level &optional change)
  7102. "Rectify a level change under the influence of `org-odd-levels-only'
  7103. LEVEL is a current level, CHANGE is by how much the level should be
  7104. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7105. even level numbers will become the next higher odd number."
  7106. (if org-odd-levels-only
  7107. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7108. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7109. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7110. (max 1 (+ level (or change 0)))))
  7111. (if (boundp 'define-obsolete-function-alias)
  7112. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7113. (define-obsolete-function-alias 'org-get-legal-level
  7114. 'org-get-valid-level)
  7115. (define-obsolete-function-alias 'org-get-legal-level
  7116. 'org-get-valid-level "23.1")))
  7117. (defun org-promote ()
  7118. "Promote the current heading higher up the tree.
  7119. If the region is active in `transient-mark-mode', promote all headings
  7120. in the region."
  7121. (org-back-to-heading t)
  7122. (let* ((level (save-match-data (funcall outline-level)))
  7123. (after-change-functions (remove 'flyspell-after-change-function
  7124. after-change-functions))
  7125. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7126. (diff (abs (- level (length up-head) -1))))
  7127. (cond ((and (= level 1) org-called-with-limited-levels
  7128. org-allow-promoting-top-level-subtree)
  7129. (replace-match "# " nil t))
  7130. ((= level 1)
  7131. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7132. (t (replace-match up-head nil t)))
  7133. ;; Fixup tag positioning
  7134. (unless (= level 1)
  7135. (and org-auto-align-tags (org-set-tags nil t))
  7136. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7137. (run-hooks 'org-after-promote-entry-hook)))
  7138. (defun org-demote ()
  7139. "Demote the current heading lower down the tree.
  7140. If the region is active in `transient-mark-mode', demote all headings
  7141. in the region."
  7142. (org-back-to-heading t)
  7143. (let* ((level (save-match-data (funcall outline-level)))
  7144. (after-change-functions (remove 'flyspell-after-change-function
  7145. after-change-functions))
  7146. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7147. (diff (abs (- level (length down-head) -1))))
  7148. (replace-match down-head nil t)
  7149. ;; Fixup tag positioning
  7150. (and org-auto-align-tags (org-set-tags nil t))
  7151. (if org-adapt-indentation (org-fixup-indentation diff))
  7152. (run-hooks 'org-after-demote-entry-hook)))
  7153. (defun org-cycle-level ()
  7154. "Cycle the level of an empty headline through possible states.
  7155. This goes first to child, then to parent, level, then up the hierarchy.
  7156. After top level, it switches back to sibling level."
  7157. (interactive)
  7158. (let ((org-adapt-indentation nil))
  7159. (when (org-point-at-end-of-empty-headline)
  7160. (setq this-command 'org-cycle-level) ; Only needed for caching
  7161. (let ((cur-level (org-current-level))
  7162. (prev-level (org-get-previous-line-level)))
  7163. (cond
  7164. ;; If first headline in file, promote to top-level.
  7165. ((= prev-level 0)
  7166. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7167. do (org-do-promote)))
  7168. ;; If same level as prev, demote one.
  7169. ((= prev-level cur-level)
  7170. (org-do-demote))
  7171. ;; If parent is top-level, promote to top level if not already.
  7172. ((= prev-level 1)
  7173. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7174. do (org-do-promote)))
  7175. ;; If top-level, return to prev-level.
  7176. ((= cur-level 1)
  7177. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7178. do (org-do-demote)))
  7179. ;; If less than prev-level, promote one.
  7180. ((< cur-level prev-level)
  7181. (org-do-promote))
  7182. ;; If deeper than prev-level, promote until higher than
  7183. ;; prev-level.
  7184. ((> cur-level prev-level)
  7185. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7186. do (org-do-promote))))
  7187. t))))
  7188. (defun org-map-tree (fun)
  7189. "Call FUN for every heading underneath the current one."
  7190. (org-back-to-heading)
  7191. (let ((level (funcall outline-level)))
  7192. (save-excursion
  7193. (funcall fun)
  7194. (while (and (progn
  7195. (outline-next-heading)
  7196. (> (funcall outline-level) level))
  7197. (not (eobp)))
  7198. (funcall fun)))))
  7199. (defun org-map-region (fun beg end)
  7200. "Call FUN for every heading between BEG and END."
  7201. (let ((org-ignore-region t))
  7202. (save-excursion
  7203. (setq end (copy-marker end))
  7204. (goto-char beg)
  7205. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7206. (< (point) end))
  7207. (funcall fun))
  7208. (while (and (progn
  7209. (outline-next-heading)
  7210. (< (point) end))
  7211. (not (eobp)))
  7212. (funcall fun)))))
  7213. (defvar org-property-end-re) ; silence byte-compiler
  7214. (defun org-fixup-indentation (diff)
  7215. "Change the indentation in the current entry by DIFF.
  7216. However, if any line in the current entry has no indentation, or if it
  7217. would end up with no indentation after the change, nothing at all is done."
  7218. (save-excursion
  7219. (let ((end (save-excursion (outline-next-heading)
  7220. (point-marker)))
  7221. (prohibit (if (> diff 0)
  7222. "^\\S-"
  7223. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7224. col)
  7225. (unless (save-excursion (end-of-line 1)
  7226. (re-search-forward prohibit end t))
  7227. (while (and (< (point) end)
  7228. (re-search-forward "^[ \t]+" end t))
  7229. (goto-char (match-end 0))
  7230. (setq col (current-column))
  7231. (if (< diff 0) (replace-match ""))
  7232. (org-indent-to-column (+ diff col))))
  7233. (move-marker end nil))))
  7234. (defun org-convert-to-odd-levels ()
  7235. "Convert an org-mode file with all levels allowed to one with odd levels.
  7236. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7237. level 5 etc."
  7238. (interactive)
  7239. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7240. (let ((outline-level 'org-outline-level)
  7241. (org-odd-levels-only nil) n)
  7242. (save-excursion
  7243. (goto-char (point-min))
  7244. (while (re-search-forward "^\\*\\*+ " nil t)
  7245. (setq n (- (length (match-string 0)) 2))
  7246. (while (>= (setq n (1- n)) 0)
  7247. (org-demote))
  7248. (end-of-line 1))))))
  7249. (defun org-convert-to-oddeven-levels ()
  7250. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7251. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7252. file contains a section with an even level, conversion would
  7253. destroy the structure of the file. An error is signaled in this
  7254. case."
  7255. (interactive)
  7256. (goto-char (point-min))
  7257. ;; First check if there are no even levels
  7258. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7259. (org-show-context t)
  7260. (error "Not all levels are odd in this file. Conversion not possible"))
  7261. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7262. (let ((outline-regexp org-outline-regexp)
  7263. (outline-level 'org-outline-level)
  7264. (org-odd-levels-only nil) n)
  7265. (save-excursion
  7266. (goto-char (point-min))
  7267. (while (re-search-forward "^\\*\\*+ " nil t)
  7268. (setq n (/ (1- (length (match-string 0))) 2))
  7269. (while (>= (setq n (1- n)) 0)
  7270. (org-promote))
  7271. (end-of-line 1))))))
  7272. (defun org-tr-level (n)
  7273. "Make N odd if required."
  7274. (if org-odd-levels-only (1+ (/ n 2)) n))
  7275. ;;; Vertical tree motion, cutting and pasting of subtrees
  7276. (defun org-move-subtree-up (&optional arg)
  7277. "Move the current subtree up past ARG headlines of the same level."
  7278. (interactive "p")
  7279. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7280. (defun org-move-subtree-down (&optional arg)
  7281. "Move the current subtree down past ARG headlines of the same level."
  7282. (interactive "p")
  7283. (setq arg (prefix-numeric-value arg))
  7284. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7285. 'org-get-last-sibling))
  7286. (ins-point (make-marker))
  7287. (cnt (abs arg))
  7288. (col (current-column))
  7289. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7290. ;; Select the tree
  7291. (org-back-to-heading)
  7292. (setq beg0 (point))
  7293. (save-excursion
  7294. (setq ne-beg (org-back-over-empty-lines))
  7295. (setq beg (point)))
  7296. (save-match-data
  7297. (save-excursion (outline-end-of-heading)
  7298. (setq folded (outline-invisible-p)))
  7299. (outline-end-of-subtree))
  7300. (outline-next-heading)
  7301. (setq ne-end (org-back-over-empty-lines))
  7302. (setq end (point))
  7303. (goto-char beg0)
  7304. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7305. ;; include less whitespace
  7306. (save-excursion
  7307. (goto-char beg)
  7308. (forward-line (- ne-beg ne-end))
  7309. (setq beg (point))))
  7310. ;; Find insertion point, with error handling
  7311. (while (> cnt 0)
  7312. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7313. (progn (goto-char beg0)
  7314. (user-error "Cannot move past superior level or buffer limit")))
  7315. (setq cnt (1- cnt)))
  7316. (if (> arg 0)
  7317. ;; Moving forward - still need to move over subtree
  7318. (progn (org-end-of-subtree t t)
  7319. (save-excursion
  7320. (org-back-over-empty-lines)
  7321. (or (bolp) (newline)))))
  7322. (setq ne-ins (org-back-over-empty-lines))
  7323. (move-marker ins-point (point))
  7324. (setq txt (buffer-substring beg end))
  7325. (org-save-markers-in-region beg end)
  7326. (delete-region beg end)
  7327. (org-remove-empty-overlays-at beg)
  7328. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7329. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7330. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7331. (let ((bbb (point)))
  7332. (insert-before-markers txt)
  7333. (org-reinstall-markers-in-region bbb)
  7334. (move-marker ins-point bbb))
  7335. (or (bolp) (insert "\n"))
  7336. (setq ins-end (point))
  7337. (goto-char ins-point)
  7338. (org-skip-whitespace)
  7339. (when (and (< arg 0)
  7340. (org-first-sibling-p)
  7341. (> ne-ins ne-beg))
  7342. ;; Move whitespace back to beginning
  7343. (save-excursion
  7344. (goto-char ins-end)
  7345. (let ((kill-whole-line t))
  7346. (kill-line (- ne-ins ne-beg)) (point)))
  7347. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7348. (move-marker ins-point nil)
  7349. (if folded
  7350. (hide-subtree)
  7351. (org-show-entry)
  7352. (show-children)
  7353. (org-cycle-hide-drawers 'children))
  7354. (org-clean-visibility-after-subtree-move)
  7355. ;; move back to the initial column we were at
  7356. (move-to-column col)))
  7357. (defvar org-subtree-clip ""
  7358. "Clipboard for cut and paste of subtrees.
  7359. This is actually only a copy of the kill, because we use the normal kill
  7360. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7361. (defvar org-subtree-clip-folded nil
  7362. "Was the last copied subtree folded?
  7363. This is used to fold the tree back after pasting.")
  7364. (defun org-cut-subtree (&optional n)
  7365. "Cut the current subtree into the clipboard.
  7366. With prefix arg N, cut this many sequential subtrees.
  7367. This is a short-hand for marking the subtree and then cutting it."
  7368. (interactive "p")
  7369. (org-copy-subtree n 'cut))
  7370. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7371. "Copy the current subtree it in the clipboard.
  7372. With prefix arg N, copy this many sequential subtrees.
  7373. This is a short-hand for marking the subtree and then copying it.
  7374. If CUT is non-nil, actually cut the subtree.
  7375. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7376. of some markers in the region, even if CUT is non-nil. This is
  7377. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7378. (interactive "p")
  7379. (let (beg end folded (beg0 (point)))
  7380. (if (org-called-interactively-p 'any)
  7381. (org-back-to-heading nil) ; take what looks like a subtree
  7382. (org-back-to-heading t)) ; take what is really there
  7383. (setq beg (point))
  7384. (skip-chars-forward " \t\r\n")
  7385. (save-match-data
  7386. (if nosubtrees
  7387. (outline-next-heading)
  7388. (save-excursion (outline-end-of-heading)
  7389. (setq folded (outline-invisible-p)))
  7390. (condition-case nil
  7391. (org-forward-heading-same-level (1- n) t)
  7392. (error nil))
  7393. (org-end-of-subtree t t)))
  7394. (setq end (point))
  7395. (goto-char beg0)
  7396. (when (> end beg)
  7397. (setq org-subtree-clip-folded folded)
  7398. (when (or cut force-store-markers)
  7399. (org-save-markers-in-region beg end))
  7400. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7401. (setq org-subtree-clip (current-kill 0))
  7402. (message "%s: Subtree(s) with %d characters"
  7403. (if cut "Cut" "Copied")
  7404. (length org-subtree-clip)))))
  7405. (defun org-paste-subtree (&optional level tree for-yank)
  7406. "Paste the clipboard as a subtree, with modification of headline level.
  7407. The entire subtree is promoted or demoted in order to match a new headline
  7408. level.
  7409. If the cursor is at the beginning of a headline, the same level as
  7410. that headline is used to paste the tree.
  7411. If not, the new level is derived from the *visible* headings
  7412. before and after the insertion point, and taken to be the inferior headline
  7413. level of the two. So if the previous visible heading is level 3 and the
  7414. next is level 4 (or vice versa), level 4 will be used for insertion.
  7415. This makes sure that the subtree remains an independent subtree and does
  7416. not swallow low level entries.
  7417. You can also force a different level, either by using a numeric prefix
  7418. argument, or by inserting the heading marker by hand. For example, if the
  7419. cursor is after \"*****\", then the tree will be shifted to level 5.
  7420. If optional TREE is given, use this text instead of the kill ring.
  7421. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7422. move back over whitespace before inserting, and move point to the end of
  7423. the inserted text when done."
  7424. (interactive "P")
  7425. (setq tree (or tree (and kill-ring (current-kill 0))))
  7426. (unless (org-kill-is-subtree-p tree)
  7427. (user-error "%s"
  7428. (substitute-command-keys
  7429. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7430. (org-with-limited-levels
  7431. (let* ((visp (not (outline-invisible-p)))
  7432. (txt tree)
  7433. (^re_ "\\(\\*+\\)[ \t]*")
  7434. (old-level (if (string-match org-outline-regexp-bol txt)
  7435. (- (match-end 0) (match-beginning 0) 1)
  7436. -1))
  7437. (force-level (cond (level (prefix-numeric-value level))
  7438. ((and (looking-at "[ \t]*$")
  7439. (string-match
  7440. "^\\*+$" (buffer-substring
  7441. (point-at-bol) (point))))
  7442. (- (match-end 1) (match-beginning 1)))
  7443. ((and (bolp)
  7444. (looking-at org-outline-regexp))
  7445. (- (match-end 0) (point) 1))))
  7446. (previous-level (save-excursion
  7447. (condition-case nil
  7448. (progn
  7449. (outline-previous-visible-heading 1)
  7450. (if (looking-at ^re_)
  7451. (- (match-end 0) (match-beginning 0) 1)
  7452. 1))
  7453. (error 1))))
  7454. (next-level (save-excursion
  7455. (condition-case nil
  7456. (progn
  7457. (or (looking-at org-outline-regexp)
  7458. (outline-next-visible-heading 1))
  7459. (if (looking-at ^re_)
  7460. (- (match-end 0) (match-beginning 0) 1)
  7461. 1))
  7462. (error 1))))
  7463. (new-level (or force-level (max previous-level next-level)))
  7464. (shift (if (or (= old-level -1)
  7465. (= new-level -1)
  7466. (= old-level new-level))
  7467. 0
  7468. (- new-level old-level)))
  7469. (delta (if (> shift 0) -1 1))
  7470. (func (if (> shift 0) 'org-demote 'org-promote))
  7471. (org-odd-levels-only nil)
  7472. beg end newend)
  7473. ;; Remove the forced level indicator
  7474. (if force-level
  7475. (delete-region (point-at-bol) (point)))
  7476. ;; Paste
  7477. (beginning-of-line (if (bolp) 1 2))
  7478. (setq beg (point))
  7479. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7480. (insert-before-markers txt)
  7481. (unless (string-match "\n\\'" txt) (insert "\n"))
  7482. (setq newend (point))
  7483. (org-reinstall-markers-in-region beg)
  7484. (setq end (point))
  7485. (goto-char beg)
  7486. (skip-chars-forward " \t\n\r")
  7487. (setq beg (point))
  7488. (if (and (outline-invisible-p) visp)
  7489. (save-excursion (outline-show-heading)))
  7490. ;; Shift if necessary
  7491. (unless (= shift 0)
  7492. (save-restriction
  7493. (narrow-to-region beg end)
  7494. (while (not (= shift 0))
  7495. (org-map-region func (point-min) (point-max))
  7496. (setq shift (+ delta shift)))
  7497. (goto-char (point-min))
  7498. (setq newend (point-max))))
  7499. (when (or (org-called-interactively-p 'interactive) for-yank)
  7500. (message "Clipboard pasted as level %d subtree" new-level))
  7501. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7502. kill-ring
  7503. (eq org-subtree-clip (current-kill 0))
  7504. org-subtree-clip-folded)
  7505. ;; The tree was folded before it was killed/copied
  7506. (hide-subtree))
  7507. (and for-yank (goto-char newend)))))
  7508. (defun org-kill-is-subtree-p (&optional txt)
  7509. "Check if the current kill is an outline subtree, or a set of trees.
  7510. Returns nil if kill does not start with a headline, or if the first
  7511. headline level is not the largest headline level in the tree.
  7512. So this will actually accept several entries of equal levels as well,
  7513. which is OK for `org-paste-subtree'.
  7514. If optional TXT is given, check this string instead of the current kill."
  7515. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7516. (re (org-get-limited-outline-regexp))
  7517. (^re (concat "^" re))
  7518. (start-level (and kill
  7519. (string-match
  7520. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7521. kill)
  7522. (- (match-end 2) (match-beginning 2) 1)))
  7523. (start (1+ (or (match-beginning 2) -1))))
  7524. (if (not start-level)
  7525. (progn
  7526. nil) ;; does not even start with a heading
  7527. (catch 'exit
  7528. (while (setq start (string-match ^re kill (1+ start)))
  7529. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7530. (throw 'exit nil)))
  7531. t))))
  7532. (defvar org-markers-to-move nil
  7533. "Markers that should be moved with a cut-and-paste operation.
  7534. Those markers are stored together with their positions relative to
  7535. the start of the region.")
  7536. (defun org-save-markers-in-region (beg end)
  7537. "Check markers in region.
  7538. If these markers are between BEG and END, record their position relative
  7539. to BEG, so that after moving the block of text, we can put the markers back
  7540. into place.
  7541. This function gets called just before an entry or tree gets cut from the
  7542. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7543. called immediately, to move the markers with the entries."
  7544. (setq org-markers-to-move nil)
  7545. (when (featurep 'org-clock)
  7546. (org-clock-save-markers-for-cut-and-paste beg end))
  7547. (when (featurep 'org-agenda)
  7548. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7549. (defun org-check-and-save-marker (marker beg end)
  7550. "Check if MARKER is between BEG and END.
  7551. If yes, remember the marker and the distance to BEG."
  7552. (when (and (marker-buffer marker)
  7553. (equal (marker-buffer marker) (current-buffer)))
  7554. (if (and (>= marker beg) (< marker end))
  7555. (push (cons marker (- marker beg)) org-markers-to-move))))
  7556. (defun org-reinstall-markers-in-region (beg)
  7557. "Move all remembered markers to their position relative to BEG."
  7558. (mapc (lambda (x)
  7559. (move-marker (car x) (+ beg (cdr x))))
  7560. org-markers-to-move)
  7561. (setq org-markers-to-move nil))
  7562. (defun org-narrow-to-subtree ()
  7563. "Narrow buffer to the current subtree."
  7564. (interactive)
  7565. (save-excursion
  7566. (save-match-data
  7567. (org-with-limited-levels
  7568. (narrow-to-region
  7569. (progn (org-back-to-heading t) (point))
  7570. (progn (org-end-of-subtree t t)
  7571. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7572. (point)))))))
  7573. (defun org-narrow-to-block ()
  7574. "Narrow buffer to the current block."
  7575. (interactive)
  7576. (let* ((case-fold-search t)
  7577. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7578. "^[ \t]*#\\+end_.*")))
  7579. (if blockp
  7580. (narrow-to-region (car blockp) (cdr blockp))
  7581. (user-error "Not in a block"))))
  7582. (eval-when-compile
  7583. (defvar org-property-drawer-re))
  7584. (defvar org-property-start-re) ;; defined below
  7585. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7586. "Clone the task (subtree) at point N times.
  7587. The clones will be inserted as siblings.
  7588. In interactive use, the user will be prompted for the number of
  7589. clones to be produced. If the entry has a timestamp, the user
  7590. will also be prompted for a time shift, which may be a repeater
  7591. as used in time stamps, for example `+3d'. To disable this,
  7592. you can call the function with a universal prefix argument.
  7593. When a valid repeater is given and the entry contains any time
  7594. stamps, the clones will become a sequence in time, with time
  7595. stamps in the subtree shifted for each clone produced. If SHIFT
  7596. is nil or the empty string, time stamps will be left alone. The
  7597. ID property of the original subtree is removed.
  7598. If the original subtree did contain time stamps with a repeater,
  7599. the following will happen:
  7600. - the repeater will be removed in each clone
  7601. - an additional clone will be produced, with the current, unshifted
  7602. date(s) in the entry.
  7603. - the original entry will be placed *after* all the clones, with
  7604. repeater intact.
  7605. - the start days in the repeater in the original entry will be shifted
  7606. to past the last clone.
  7607. In this way you can spell out a number of instances of a repeating task,
  7608. and still retain the repeater to cover future instances of the task."
  7609. (interactive "nNumber of clones to produce: ")
  7610. (let ((shift
  7611. (or shift
  7612. (if (and (not (equal current-prefix-arg '(4)))
  7613. (save-excursion
  7614. (re-search-forward org-ts-regexp-both
  7615. (save-excursion
  7616. (org-end-of-subtree t)
  7617. (point)) t)))
  7618. (read-from-minibuffer
  7619. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7620. ""))) ;; No time shift
  7621. (n-no-remove -1)
  7622. (drawer-re org-drawer-regexp)
  7623. beg end template task idprop
  7624. shift-n shift-what doshift nmin nmax)
  7625. (if (not (and (integerp n) (> n 0)))
  7626. (error "Invalid number of replications %s" n))
  7627. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7628. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7629. shift)))
  7630. (error "Invalid shift specification %s" shift))
  7631. (when doshift
  7632. (setq shift-n (string-to-number (match-string 1 shift))
  7633. shift-what (cdr (assoc (match-string 2 shift)
  7634. '(("d" . day) ("w" . week)
  7635. ("m" . month) ("y" . year))))))
  7636. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7637. (setq nmin 1 nmax n)
  7638. (org-back-to-heading t)
  7639. (setq beg (point))
  7640. (setq idprop (org-entry-get nil "ID"))
  7641. (org-end-of-subtree t t)
  7642. (or (bolp) (insert "\n"))
  7643. (setq end (point))
  7644. (setq template (buffer-substring beg end))
  7645. (when (and doshift
  7646. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7647. (delete-region beg end)
  7648. (setq end beg)
  7649. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7650. (goto-char end)
  7651. (loop for n from nmin to nmax do
  7652. ;; prepare clone
  7653. (with-temp-buffer
  7654. (insert template)
  7655. (org-mode)
  7656. (goto-char (point-min))
  7657. (org-show-subtree)
  7658. (and idprop (if org-clone-delete-id
  7659. (org-entry-delete nil "ID")
  7660. (org-id-get-create t)))
  7661. (unless (= n 0)
  7662. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7663. (kill-whole-line))
  7664. (goto-char (point-min))
  7665. (while (re-search-forward drawer-re nil t)
  7666. (org-remove-empty-drawer-at (point))))
  7667. (goto-char (point-min))
  7668. (when doshift
  7669. (while (re-search-forward org-ts-regexp-both nil t)
  7670. (org-timestamp-change (* n shift-n) shift-what))
  7671. (unless (= n n-no-remove)
  7672. (goto-char (point-min))
  7673. (while (re-search-forward org-ts-regexp nil t)
  7674. (save-excursion
  7675. (goto-char (match-beginning 0))
  7676. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7677. (delete-region (match-beginning 1) (match-end 1)))))))
  7678. (setq task (buffer-string)))
  7679. (insert task))
  7680. (goto-char beg)))
  7681. ;;; Outline Sorting
  7682. (defun org-sort (with-case)
  7683. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7684. Optional argument WITH-CASE means sort case-sensitively."
  7685. (interactive "P")
  7686. (cond
  7687. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7688. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7689. (t
  7690. (org-call-with-arg 'org-sort-entries with-case))))
  7691. (defun org-sort-remove-invisible (s)
  7692. "Remove invisible links from string S."
  7693. (remove-text-properties 0 (length s) org-rm-props s)
  7694. (while (string-match org-bracket-link-regexp s)
  7695. (setq s (replace-match (if (match-end 2)
  7696. (match-string 3 s)
  7697. (match-string 1 s)) t t s)))
  7698. (let ((st (format " %s " s)))
  7699. (while (string-match org-emph-re st)
  7700. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7701. (setq s (substring st 1 -1)))
  7702. s)
  7703. (defvar org-priority-regexp) ; defined later in the file
  7704. (defvar org-after-sorting-entries-or-items-hook nil
  7705. "Hook that is run after a bunch of entries or items have been sorted.
  7706. When children are sorted, the cursor is in the parent line when this
  7707. hook gets called. When a region or a plain list is sorted, the cursor
  7708. will be in the first entry of the sorted region/list.")
  7709. (defun org-sort-entries
  7710. (&optional with-case sorting-type getkey-func compare-func property)
  7711. "Sort entries on a certain level of an outline tree.
  7712. If there is an active region, the entries in the region are sorted.
  7713. Else, if the cursor is before the first entry, sort the top-level items.
  7714. Else, the children of the entry at point are sorted.
  7715. Sorting can be alphabetically, numerically, by date/time as given by
  7716. a time stamp, by a property, by priority order, or by a custom function.
  7717. The command prompts for the sorting type unless it has been given to the
  7718. function through the SORTING-TYPE argument, which needs to be a character,
  7719. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7720. precise meaning of each character:
  7721. n Numerically, by converting the beginning of the entry/item to a number.
  7722. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7723. o By order of TODO keywords.
  7724. t By date/time, either the first active time stamp in the entry, or, if
  7725. none exist, by the first inactive one.
  7726. s By the scheduled date/time.
  7727. d By deadline date/time.
  7728. c By creation time, which is assumed to be the first inactive time stamp
  7729. at the beginning of a line.
  7730. p By priority according to the cookie.
  7731. r By the value of a property.
  7732. Capital letters will reverse the sort order.
  7733. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7734. called with point at the beginning of the record. It must return either
  7735. a string or a number that should serve as the sorting key for that record.
  7736. Comparing entries ignores case by default. However, with an optional argument
  7737. WITH-CASE, the sorting considers case as well.
  7738. Sorting is done against the visible part of the headlines, it ignores hidden
  7739. links.
  7740. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7741. (interactive "P")
  7742. (let ((case-func (if with-case 'identity 'downcase))
  7743. (cmstr
  7744. ;; The clock marker is lost when using `sort-subr', let's
  7745. ;; store the clocking string.
  7746. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7747. (save-excursion
  7748. (goto-char org-clock-marker)
  7749. (looking-back "^.*") (match-string-no-properties 0))))
  7750. start beg end stars re re2
  7751. txt what tmp)
  7752. ;; Find beginning and end of region to sort
  7753. (cond
  7754. ((org-region-active-p)
  7755. ;; we will sort the region
  7756. (setq end (region-end)
  7757. what "region")
  7758. (goto-char (region-beginning))
  7759. (if (not (org-at-heading-p)) (outline-next-heading))
  7760. (setq start (point)))
  7761. ((or (org-at-heading-p)
  7762. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7763. ;; we will sort the children of the current headline
  7764. (org-back-to-heading)
  7765. (setq start (point)
  7766. end (progn (org-end-of-subtree t t)
  7767. (or (bolp) (insert "\n"))
  7768. (org-back-over-empty-lines)
  7769. (point))
  7770. what "children")
  7771. (goto-char start)
  7772. (show-subtree)
  7773. (outline-next-heading))
  7774. (t
  7775. ;; we will sort the top-level entries in this file
  7776. (goto-char (point-min))
  7777. (or (org-at-heading-p) (outline-next-heading))
  7778. (setq start (point))
  7779. (goto-char (point-max))
  7780. (beginning-of-line 1)
  7781. (when (looking-at ".*?\\S-")
  7782. ;; File ends in a non-white line
  7783. (end-of-line 1)
  7784. (insert "\n"))
  7785. (setq end (point-max))
  7786. (setq what "top-level")
  7787. (goto-char start)
  7788. (show-all)))
  7789. (setq beg (point))
  7790. (if (>= beg end) (user-error "Nothing to sort"))
  7791. (looking-at "\\(\\*+\\)")
  7792. (setq stars (match-string 1)
  7793. re (concat "^" (regexp-quote stars) " +")
  7794. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7795. txt (buffer-substring beg end))
  7796. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7797. (if (and (not (equal stars "*")) (string-match re2 txt))
  7798. (user-error "Region to sort contains a level above the first entry"))
  7799. (unless sorting-type
  7800. (message
  7801. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7802. [t]ime [s]cheduled [d]eadline [c]reated
  7803. A/N/P/R/O/F/T/S/D/C means reversed:"
  7804. what)
  7805. (setq sorting-type (read-char-exclusive))
  7806. (unless getkey-func
  7807. (and (= (downcase sorting-type) ?f)
  7808. (setq getkey-func
  7809. (org-icompleting-read "Sort using function: "
  7810. obarray 'fboundp t nil nil))
  7811. (setq getkey-func (intern getkey-func))))
  7812. (and (= (downcase sorting-type) ?r)
  7813. (not property)
  7814. (setq property
  7815. (org-icompleting-read "Property: "
  7816. (mapcar 'list (org-buffer-property-keys t))
  7817. nil t))))
  7818. (message "Sorting entries...")
  7819. (save-restriction
  7820. (narrow-to-region start end)
  7821. (let ((dcst (downcase sorting-type))
  7822. (case-fold-search nil)
  7823. (now (current-time)))
  7824. (sort-subr
  7825. (/= dcst sorting-type)
  7826. ;; This function moves to the beginning character of the "record" to
  7827. ;; be sorted.
  7828. (lambda nil
  7829. (if (re-search-forward re nil t)
  7830. (goto-char (match-beginning 0))
  7831. (goto-char (point-max))))
  7832. ;; This function moves to the last character of the "record" being
  7833. ;; sorted.
  7834. (lambda nil
  7835. (save-match-data
  7836. (condition-case nil
  7837. (outline-forward-same-level 1)
  7838. (error
  7839. (goto-char (point-max))))))
  7840. ;; This function returns the value that gets sorted against.
  7841. (lambda nil
  7842. (cond
  7843. ((= dcst ?n)
  7844. (if (looking-at org-complex-heading-regexp)
  7845. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7846. nil))
  7847. ((= dcst ?a)
  7848. (if (looking-at org-complex-heading-regexp)
  7849. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7850. nil))
  7851. ((= dcst ?t)
  7852. (let ((end (save-excursion (outline-next-heading) (point))))
  7853. (if (or (re-search-forward org-ts-regexp end t)
  7854. (re-search-forward org-ts-regexp-both end t))
  7855. (org-time-string-to-seconds (match-string 0))
  7856. (org-float-time now))))
  7857. ((= dcst ?c)
  7858. (let ((end (save-excursion (outline-next-heading) (point))))
  7859. (if (re-search-forward
  7860. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7861. end t)
  7862. (org-time-string-to-seconds (match-string 0))
  7863. (org-float-time now))))
  7864. ((= dcst ?s)
  7865. (let ((end (save-excursion (outline-next-heading) (point))))
  7866. (if (re-search-forward org-scheduled-time-regexp end t)
  7867. (org-time-string-to-seconds (match-string 1))
  7868. (org-float-time now))))
  7869. ((= dcst ?d)
  7870. (let ((end (save-excursion (outline-next-heading) (point))))
  7871. (if (re-search-forward org-deadline-time-regexp end t)
  7872. (org-time-string-to-seconds (match-string 1))
  7873. (org-float-time now))))
  7874. ((= dcst ?p)
  7875. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7876. (string-to-char (match-string 2))
  7877. org-default-priority))
  7878. ((= dcst ?r)
  7879. (or (org-entry-get nil property) ""))
  7880. ((= dcst ?o)
  7881. (if (looking-at org-complex-heading-regexp)
  7882. (- 9999 (length (member (match-string 2)
  7883. org-todo-keywords-1)))))
  7884. ((= dcst ?f)
  7885. (if getkey-func
  7886. (progn
  7887. (setq tmp (funcall getkey-func))
  7888. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7889. tmp)
  7890. (error "Invalid key function `%s'" getkey-func)))
  7891. (t (error "Invalid sorting type `%c'" sorting-type))))
  7892. nil
  7893. (cond
  7894. ((= dcst ?a) 'string<)
  7895. ((= dcst ?f) compare-func)
  7896. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7897. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7898. ;; Reset the clock marker if needed
  7899. (when cmstr
  7900. (save-excursion
  7901. (goto-char start)
  7902. (search-forward cmstr nil t)
  7903. (move-marker org-clock-marker (point))))
  7904. (message "Sorting entries...done")))
  7905. (defun org-do-sort (table what &optional with-case sorting-type)
  7906. "Sort TABLE of WHAT according to SORTING-TYPE.
  7907. The user will be prompted for the SORTING-TYPE if the call to this
  7908. function does not specify it. WHAT is only for the prompt, to indicate
  7909. what is being sorted. The sorting key will be extracted from
  7910. the car of the elements of the table.
  7911. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7912. (unless sorting-type
  7913. (message
  7914. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7915. what)
  7916. (setq sorting-type (read-char-exclusive)))
  7917. (let ((dcst (downcase sorting-type))
  7918. extractfun comparefun)
  7919. ;; Define the appropriate functions
  7920. (cond
  7921. ((= dcst ?n)
  7922. (setq extractfun 'string-to-number
  7923. comparefun (if (= dcst sorting-type) '< '>)))
  7924. ((= dcst ?a)
  7925. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7926. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7927. comparefun (if (= dcst sorting-type)
  7928. 'string<
  7929. (lambda (a b) (and (not (string< a b))
  7930. (not (string= a b)))))))
  7931. ((= dcst ?t)
  7932. (setq extractfun
  7933. (lambda (x)
  7934. (if (or (string-match org-ts-regexp x)
  7935. (string-match org-ts-regexp-both x))
  7936. (org-float-time
  7937. (org-time-string-to-time (match-string 0 x)))
  7938. 0))
  7939. comparefun (if (= dcst sorting-type) '< '>)))
  7940. (t (error "Invalid sorting type `%c'" sorting-type)))
  7941. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7942. table)
  7943. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7944. ;;; The orgstruct minor mode
  7945. ;; Define a minor mode which can be used in other modes in order to
  7946. ;; integrate the org-mode structure editing commands.
  7947. ;; This is really a hack, because the org-mode structure commands use
  7948. ;; keys which normally belong to the major mode. Here is how it
  7949. ;; works: The minor mode defines all the keys necessary to operate the
  7950. ;; structure commands, but wraps the commands into a function which
  7951. ;; tests if the cursor is currently at a headline or a plain list
  7952. ;; item. If that is the case, the structure command is used,
  7953. ;; temporarily setting many Org-mode variables like regular
  7954. ;; expressions for filling etc. However, when any of those keys is
  7955. ;; used at a different location, function uses `key-binding' to look
  7956. ;; up if the key has an associated command in another currently active
  7957. ;; keymap (minor modes, major mode, global), and executes that
  7958. ;; command. There might be problems if any of the keys is otherwise
  7959. ;; used as a prefix key.
  7960. (defcustom orgstruct-heading-prefix-regexp nil
  7961. "Regexp that matches the custom prefix of Org headlines in
  7962. orgstruct(++)-mode."
  7963. :group 'org
  7964. :version "24.4"
  7965. :package-version '(Org . "8.0")
  7966. :type 'string)
  7967. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7968. (defcustom orgstruct-setup-hook nil
  7969. "Hook run after orgstruct-mode-map is filled."
  7970. :group 'org
  7971. :version "24.4"
  7972. :package-version '(Org . "8.0")
  7973. :type 'hook)
  7974. (defvar orgstruct-initialized nil)
  7975. (defvar org-local-vars nil
  7976. "List of local variables, for use by `orgstruct-mode'.")
  7977. ;;;###autoload
  7978. (define-minor-mode orgstruct-mode
  7979. "Toggle the minor mode `orgstruct-mode'.
  7980. This mode is for using Org-mode structure commands in other
  7981. modes. The following keys behave as if Org-mode were active, if
  7982. the cursor is on a headline, or on a plain list item (both as
  7983. defined by Org-mode)."
  7984. nil " OrgStruct" (make-sparse-keymap)
  7985. (funcall (if orgstruct-mode
  7986. 'add-to-invisibility-spec
  7987. 'remove-from-invisibility-spec)
  7988. '(outline . t))
  7989. (when orgstruct-mode
  7990. (org-load-modules-maybe)
  7991. (unless orgstruct-initialized
  7992. (orgstruct-setup)
  7993. (setq orgstruct-initialized t))))
  7994. ;;;###autoload
  7995. (defun turn-on-orgstruct ()
  7996. "Unconditionally turn on `orgstruct-mode'."
  7997. (orgstruct-mode 1))
  7998. (defvar org-fb-vars nil)
  7999. (make-variable-buffer-local 'org-fb-vars)
  8000. (defun orgstruct++-mode (&optional arg)
  8001. "Toggle `orgstruct-mode', the enhanced version of it.
  8002. In addition to setting orgstruct-mode, this also exports all
  8003. indentation and autofilling variables from org-mode into the
  8004. buffer. It will also recognize item context in multiline items."
  8005. (interactive "P")
  8006. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8007. (if (< arg 1)
  8008. (progn (orgstruct-mode -1)
  8009. (mapc (lambda(v)
  8010. (org-set-local (car v)
  8011. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8012. org-fb-vars))
  8013. (orgstruct-mode 1)
  8014. (setq org-fb-vars nil)
  8015. (unless org-local-vars
  8016. (setq org-local-vars (org-get-local-variables)))
  8017. (let (var val)
  8018. (mapc
  8019. (lambda (x)
  8020. (when (string-match
  8021. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8022. (symbol-name (car x)))
  8023. (setq var (car x) val (nth 1 x))
  8024. (push (list var `(quote ,(eval var))) org-fb-vars)
  8025. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8026. org-local-vars)
  8027. (org-set-local 'orgstruct-is-++ t))))
  8028. (defvar orgstruct-is-++ nil
  8029. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8030. (make-variable-buffer-local 'orgstruct-is-++)
  8031. ;;;###autoload
  8032. (defun turn-on-orgstruct++ ()
  8033. "Unconditionally turn on `orgstruct++-mode'."
  8034. (orgstruct++-mode 1))
  8035. (defun orgstruct-error ()
  8036. "Error when there is no default binding for a structure key."
  8037. (interactive)
  8038. (funcall (if (fboundp 'user-error)
  8039. 'user-error
  8040. 'error)
  8041. "This key has no function outside structure elements"))
  8042. (defun orgstruct-setup ()
  8043. "Setup orgstruct keymap."
  8044. (dolist (cell '((org-demote . t)
  8045. (org-metaleft . t)
  8046. (org-metaright . t)
  8047. (org-promote . t)
  8048. (org-shiftmetaleft . t)
  8049. (org-shiftmetaright . t)
  8050. org-backward-element
  8051. org-backward-heading-same-level
  8052. org-ctrl-c-ret
  8053. org-ctrl-c-minus
  8054. org-ctrl-c-star
  8055. org-cycle
  8056. org-forward-heading-same-level
  8057. org-insert-heading
  8058. org-insert-heading-respect-content
  8059. org-kill-note-or-show-branches
  8060. org-mark-subtree
  8061. org-meta-return
  8062. org-metadown
  8063. org-metaup
  8064. org-narrow-to-subtree
  8065. org-promote-subtree
  8066. org-reveal
  8067. org-shiftdown
  8068. org-shiftleft
  8069. org-shiftmetadown
  8070. org-shiftmetaup
  8071. org-shiftright
  8072. org-shifttab
  8073. org-shifttab
  8074. org-shiftup
  8075. org-show-subtree
  8076. org-sort
  8077. org-up-element
  8078. outline-demote
  8079. outline-next-visible-heading
  8080. outline-previous-visible-heading
  8081. outline-promote
  8082. outline-up-heading
  8083. show-children))
  8084. (let ((f (or (car-safe cell) cell))
  8085. (disable-when-heading-prefix (cdr-safe cell)))
  8086. (when (fboundp f)
  8087. (let ((new-bindings))
  8088. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8089. (where-is-internal f outline-mode-map)))
  8090. (push binding new-bindings)
  8091. ;; TODO use local-function-key-map
  8092. (dolist (rep '(("<tab>" . "TAB")
  8093. ("<return>" . "RET")
  8094. ("<escape>" . "ESC")
  8095. ("<delete>" . "DEL")))
  8096. (setq binding (read-kbd-macro
  8097. (let ((case-fold-search))
  8098. (replace-regexp-in-string
  8099. (regexp-quote (cdr rep))
  8100. (car rep)
  8101. (key-description binding)))))
  8102. (pushnew binding new-bindings :test 'equal)))
  8103. (dolist (binding new-bindings)
  8104. (let ((key (lookup-key orgstruct-mode-map binding)))
  8105. (when (or (not key) (numberp key))
  8106. (condition-case nil
  8107. (org-defkey orgstruct-mode-map
  8108. binding
  8109. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8110. (error nil)))))))))
  8111. (run-hooks 'orgstruct-setup-hook))
  8112. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8113. "Create a function for binding in the structure minor mode.
  8114. FUN is the command to call inside a table. KEY is the key that
  8115. should be checked in for a command to execute outside of tables.
  8116. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8117. if `orgstruct-heading-prefix-regexp' is non-nil."
  8118. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8119. (let ((nname name)
  8120. (i 0))
  8121. (while (fboundp (intern nname))
  8122. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8123. (setq name (intern nname)))
  8124. (eval
  8125. (let ((bindings '((org-heading-regexp
  8126. (concat "^"
  8127. orgstruct-heading-prefix-regexp
  8128. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8129. (org-outline-regexp
  8130. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8131. (org-outline-regexp-bol
  8132. (concat "^" org-outline-regexp))
  8133. (outline-regexp org-outline-regexp)
  8134. (outline-heading-end-regexp "\n")
  8135. (outline-level 'org-outline-level)
  8136. (outline-heading-alist))))
  8137. `(defun ,name (arg)
  8138. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8139. "Outside of structure, run the binding of `"
  8140. (key-description key) "'."
  8141. (when disable-when-heading-prefix
  8142. (concat
  8143. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8144. "back to the default binding due to limitations of Org's implementation of\n"
  8145. "`" (symbol-name fun) "'.")))
  8146. (interactive "p")
  8147. (let* ((disable
  8148. ,(when disable-when-heading-prefix
  8149. '(and orgstruct-heading-prefix-regexp
  8150. (not (string= orgstruct-heading-prefix-regexp "")))))
  8151. (fallback
  8152. (or disable
  8153. (not
  8154. (let* ,bindings
  8155. (org-context-p 'headline 'item
  8156. ,(when (memq fun
  8157. '(org-insert-heading
  8158. org-insert-heading-respect-content
  8159. org-meta-return))
  8160. '(when orgstruct-is-++
  8161. 'item-body))))))))
  8162. (if fallback
  8163. (let* ((orgstruct-mode)
  8164. (binding
  8165. (loop with key = ,key
  8166. for rep in
  8167. '(nil
  8168. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8169. ("<\\([^>]*\\)return>" . "\\1RET")
  8170. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8171. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8172. do
  8173. (when rep
  8174. (setq key (read-kbd-macro
  8175. (let ((case-fold-search))
  8176. (replace-regexp-in-string
  8177. (car rep)
  8178. (cdr rep)
  8179. (key-description key))))))
  8180. thereis (key-binding key))))
  8181. (if (keymapp binding)
  8182. (set-temporary-overlay-map binding)
  8183. (let ((func (or binding
  8184. (unless disable
  8185. 'orgstruct-error))))
  8186. (when func
  8187. (call-interactively func)))))
  8188. (org-run-like-in-org-mode
  8189. (lambda ()
  8190. (interactive)
  8191. (let* ,bindings
  8192. (call-interactively ',fun)))))))))
  8193. name))
  8194. (defun org-contextualize-keys (alist contexts)
  8195. "Return valid elements in ALIST depending on CONTEXTS.
  8196. `org-agenda-custom-commands' or `org-capture-templates' are the
  8197. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8198. or `org-capture-templates-contexts' are the associated contexts
  8199. definitions."
  8200. (let ((contexts
  8201. ;; normalize contexts
  8202. (mapcar
  8203. (lambda(c) (cond ((listp (cadr c))
  8204. (list (car c) (car c) (cadr c)))
  8205. ((string= "" (cadr c))
  8206. (list (car c) (car c) (caddr c)))
  8207. (t c))) contexts))
  8208. (a alist) c r s)
  8209. ;; loop over all commands or templates
  8210. (while (setq c (pop a))
  8211. (let (vrules repl)
  8212. (cond
  8213. ((not (assoc (car c) contexts))
  8214. (push c r))
  8215. ((and (assoc (car c) contexts)
  8216. (setq vrules (org-contextualize-validate-key
  8217. (car c) contexts)))
  8218. (mapc (lambda (vr)
  8219. (when (not (equal (car vr) (cadr vr)))
  8220. (setq repl vr))) vrules)
  8221. (if (not repl) (push c r)
  8222. (push (cadr repl) s)
  8223. (push
  8224. (cons (car c)
  8225. (cdr (or (assoc (cadr repl) alist)
  8226. (error "Undefined key `%s' as contextual replacement for `%s'"
  8227. (cadr repl) (car c)))))
  8228. r))))))
  8229. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8230. (delq
  8231. nil
  8232. (delete-dups
  8233. (mapcar (lambda (x)
  8234. (let ((tpl (car x)))
  8235. (when (not (delq
  8236. nil
  8237. (mapcar (lambda(y)
  8238. (equal y tpl)) s))) x)))
  8239. (reverse r))))))
  8240. (defun org-contextualize-validate-key (key contexts)
  8241. "Check CONTEXTS for agenda or capture KEY."
  8242. (let (r rr res)
  8243. (while (setq r (pop contexts))
  8244. (mapc
  8245. (lambda (rr)
  8246. (when
  8247. (and (equal key (car r))
  8248. (if (functionp rr) (funcall rr)
  8249. (or (and (eq (car rr) 'in-file)
  8250. (buffer-file-name)
  8251. (string-match (cdr rr) (buffer-file-name)))
  8252. (and (eq (car rr) 'in-mode)
  8253. (string-match (cdr rr) (symbol-name major-mode)))
  8254. (and (eq (car rr) 'in-buffer)
  8255. (string-match (cdr rr) (buffer-name)))
  8256. (when (and (eq (car rr) 'not-in-file)
  8257. (buffer-file-name))
  8258. (not (string-match (cdr rr) (buffer-file-name))))
  8259. (when (eq (car rr) 'not-in-mode)
  8260. (not (string-match (cdr rr) (symbol-name major-mode))))
  8261. (when (eq (car rr) 'not-in-buffer)
  8262. (not (string-match (cdr rr) (buffer-name)))))))
  8263. (push r res)))
  8264. (car (last r))))
  8265. (delete-dups (delq nil res))))
  8266. (defun org-context-p (&rest contexts)
  8267. "Check if local context is any of CONTEXTS.
  8268. Possible values in the list of contexts are `table', `headline', and `item'."
  8269. (let ((pos (point)))
  8270. (goto-char (point-at-bol))
  8271. (prog1 (or (and (memq 'table contexts)
  8272. (looking-at "[ \t]*|"))
  8273. (and (memq 'headline contexts)
  8274. (looking-at org-outline-regexp))
  8275. (and (memq 'item contexts)
  8276. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8277. (and (memq 'item-body contexts)
  8278. (org-in-item-p)))
  8279. (goto-char pos))))
  8280. (defun org-get-local-variables ()
  8281. "Return a list of all local variables in an Org mode buffer."
  8282. (let (varlist)
  8283. (with-current-buffer (get-buffer-create "*Org tmp*")
  8284. (erase-buffer)
  8285. (org-mode)
  8286. (setq varlist (buffer-local-variables)))
  8287. (kill-buffer "*Org tmp*")
  8288. (delq nil
  8289. (mapcar
  8290. (lambda (x)
  8291. (setq x
  8292. (if (symbolp x)
  8293. (list x)
  8294. (list (car x) (cdr x))))
  8295. (if (and (not (get (car x) 'org-state))
  8296. (string-match
  8297. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8298. (symbol-name (car x))))
  8299. x nil))
  8300. varlist))))
  8301. (defun org-clone-local-variables (from-buffer &optional regexp)
  8302. "Clone local variables from FROM-BUFFER.
  8303. Optional argument REGEXP selects variables to clone."
  8304. (mapc
  8305. (lambda (pair)
  8306. (and (symbolp (car pair))
  8307. (or (null regexp)
  8308. (string-match regexp (symbol-name (car pair))))
  8309. (set (make-local-variable (car pair))
  8310. (cdr pair))))
  8311. (buffer-local-variables from-buffer)))
  8312. ;;;###autoload
  8313. (defun org-run-like-in-org-mode (cmd)
  8314. "Run a command, pretending that the current buffer is in Org-mode.
  8315. This will temporarily bind local variables that are typically bound in
  8316. Org-mode to the values they have in Org-mode, and then interactively
  8317. call CMD."
  8318. (org-load-modules-maybe)
  8319. (unless org-local-vars
  8320. (setq org-local-vars (org-get-local-variables)))
  8321. (let (binds)
  8322. (dolist (var org-local-vars)
  8323. (when (or (not (boundp (car var)))
  8324. (eq (symbol-value (car var))
  8325. (default-value (car var))))
  8326. (push (list (car var) `(quote ,(cadr var))) binds)))
  8327. (eval `(let ,binds
  8328. (call-interactively (quote ,cmd))))))
  8329. ;;;; Archiving
  8330. (defun org-get-category (&optional pos force-refresh)
  8331. "Get the category applying to position POS."
  8332. (save-match-data
  8333. (if force-refresh (org-refresh-category-properties))
  8334. (let ((pos (or pos (point))))
  8335. (or (get-text-property pos 'org-category)
  8336. (progn (org-refresh-category-properties)
  8337. (get-text-property pos 'org-category))))))
  8338. (defun org-refresh-category-properties ()
  8339. "Refresh category text properties in the buffer."
  8340. (let ((case-fold-search t)
  8341. (inhibit-read-only t)
  8342. (def-cat (cond
  8343. ((null org-category)
  8344. (if buffer-file-name
  8345. (file-name-sans-extension
  8346. (file-name-nondirectory buffer-file-name))
  8347. "???"))
  8348. ((symbolp org-category) (symbol-name org-category))
  8349. (t org-category)))
  8350. beg end cat pos optionp)
  8351. (org-with-silent-modifications
  8352. (save-excursion
  8353. (save-restriction
  8354. (widen)
  8355. (goto-char (point-min))
  8356. (put-text-property (point) (point-max) 'org-category def-cat)
  8357. (while (re-search-forward
  8358. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8359. (setq pos (match-end 0)
  8360. optionp (equal (char-after (match-beginning 0)) ?#)
  8361. cat (org-trim (match-string 2)))
  8362. (if optionp
  8363. (setq beg (point-at-bol) end (point-max))
  8364. (org-back-to-heading t)
  8365. (setq beg (point) end (org-end-of-subtree t t)))
  8366. (put-text-property beg end 'org-category cat)
  8367. (put-text-property beg end 'org-category-position beg)
  8368. (goto-char pos)))))))
  8369. (defun org-refresh-properties (dprop tprop)
  8370. "Refresh buffer text properties.
  8371. DPROP is the drawer property and TPROP is the corresponding text
  8372. property to set."
  8373. (let ((case-fold-search t)
  8374. (inhibit-read-only t) p)
  8375. (org-with-silent-modifications
  8376. (save-excursion
  8377. (save-restriction
  8378. (widen)
  8379. (goto-char (point-min))
  8380. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8381. (setq p (org-match-string-no-properties 1))
  8382. (save-excursion
  8383. (org-back-to-heading t)
  8384. (put-text-property
  8385. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8386. ;;;; Link Stuff
  8387. ;;; Link abbreviations
  8388. (defun org-link-expand-abbrev (link)
  8389. "Apply replacements as defined in `org-link-abbrev-alist'."
  8390. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8391. (let* ((key (match-string 1 link))
  8392. (as (or (assoc key org-link-abbrev-alist-local)
  8393. (assoc key org-link-abbrev-alist)))
  8394. (tag (and (match-end 2) (match-string 3 link)))
  8395. rpl)
  8396. (if (not as)
  8397. link
  8398. (setq rpl (cdr as))
  8399. (cond
  8400. ((symbolp rpl) (funcall rpl tag))
  8401. ((string-match "%(\\([^)]+\\))" rpl)
  8402. (replace-match
  8403. (save-match-data
  8404. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8405. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8406. ((string-match "%h" rpl)
  8407. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8408. (t (concat rpl tag)))))
  8409. link))
  8410. ;;; Storing and inserting links
  8411. (defvar org-insert-link-history nil
  8412. "Minibuffer history for links inserted with `org-insert-link'.")
  8413. (defvar org-stored-links nil
  8414. "Contains the links stored with `org-store-link'.")
  8415. (defvar org-store-link-plist nil
  8416. "Plist with info about the most recently link created with `org-store-link'.")
  8417. (defvar org-link-protocols nil
  8418. "Link protocols added to Org-mode using `org-add-link-type'.")
  8419. (defvar org-store-link-functions nil
  8420. "List of functions that are called to create and store a link.
  8421. Each function will be called in turn until one returns a non-nil
  8422. value. Each function should check if it is responsible for creating
  8423. this link (for example by looking at the major mode).
  8424. If not, it must exit and return nil.
  8425. If yes, it should return a non-nil value after a calling
  8426. `org-store-link-props' with a list of properties and values.
  8427. Special properties are:
  8428. :type The link prefix, like \"http\". This must be given.
  8429. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8430. This is obligatory as well.
  8431. :description Optional default description for the second pair
  8432. of brackets in an Org-mode link. The user can still change
  8433. this when inserting this link into an Org-mode buffer.
  8434. In addition to these, any additional properties can be specified
  8435. and then used in capture templates.")
  8436. (defun org-add-link-type (type &optional follow export)
  8437. "Add TYPE to the list of `org-link-types'.
  8438. Re-compute all regular expressions depending on `org-link-types'
  8439. FOLLOW and EXPORT are two functions.
  8440. FOLLOW should take the link path as the single argument and do whatever
  8441. is necessary to follow the link, for example find a file or display
  8442. a mail message.
  8443. EXPORT should format the link path for export to one of the export formats.
  8444. It should be a function accepting three arguments:
  8445. path the path of the link, the text after the prefix (like \"http:\")
  8446. desc the description of the link, if any, or a description added by
  8447. org-export-normalize-links if there is none
  8448. format the export format, a symbol like `html' or `latex' or `ascii'..
  8449. The function may use the FORMAT information to return different values
  8450. depending on the format. The return value will be put literally into
  8451. the exported file. If the return value is nil, this means Org should
  8452. do what it normally does with links which do not have EXPORT defined.
  8453. Org-mode has a built-in default for exporting links. If you are happy with
  8454. this default, there is no need to define an export function for the link
  8455. type. For a simple example of an export function, see `org-bbdb.el'."
  8456. (add-to-list 'org-link-types type t)
  8457. (org-make-link-regexps)
  8458. (if (assoc type org-link-protocols)
  8459. (setcdr (assoc type org-link-protocols) (list follow export))
  8460. (push (list type follow export) org-link-protocols)))
  8461. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8462. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8463. ;;;###autoload
  8464. (defun org-store-link (arg)
  8465. "\\<org-mode-map>Store an org-link to the current location.
  8466. This link is added to `org-stored-links' and can later be inserted
  8467. into an org-buffer with \\[org-insert-link].
  8468. For some link types, a prefix arg is interpreted.
  8469. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8470. For file links, arg negates `org-context-in-file-links'.
  8471. A double prefix arg force skipping storing functions that are not
  8472. part of Org's core.
  8473. A triple prefix arg force storing a link for each line in the
  8474. active region."
  8475. (interactive "P")
  8476. (org-load-modules-maybe)
  8477. (if (and (equal arg '(64)) (org-region-active-p))
  8478. (save-excursion
  8479. (let ((end (region-end)))
  8480. (goto-char (region-beginning))
  8481. (set-mark (point))
  8482. (while (< (point-at-eol) end)
  8483. (move-end-of-line 1) (activate-mark)
  8484. (let (current-prefix-arg)
  8485. (call-interactively 'org-store-link))
  8486. (move-beginning-of-line 2)
  8487. (set-mark (point)))))
  8488. (org-with-limited-levels
  8489. (setq org-store-link-plist nil)
  8490. (let (link cpltxt desc description search
  8491. txt custom-id agenda-link sfuns sfunsn)
  8492. (cond
  8493. ;; Store a link using an external link type
  8494. ((and (not (equal arg '(16)))
  8495. (setq sfuns
  8496. (delq
  8497. nil (mapcar (lambda (f)
  8498. (let (fs) (if (funcall f) (push f fs))))
  8499. org-store-link-functions))
  8500. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8501. (or (and (cdr sfuns)
  8502. (funcall (intern
  8503. (completing-read
  8504. "Which function for creating the link? "
  8505. sfunsn t (car sfunsn)))))
  8506. (funcall (caar sfuns)))
  8507. (setq link (plist-get org-store-link-plist :link)
  8508. desc (or (plist-get org-store-link-plist
  8509. :description) link))))
  8510. ;; Store a link from a source code buffer
  8511. ((org-src-edit-buffer-p)
  8512. (let (label gc)
  8513. (while (or (not label)
  8514. (save-excursion
  8515. (save-restriction
  8516. (widen)
  8517. (goto-char (point-min))
  8518. (re-search-forward
  8519. (regexp-quote (format org-coderef-label-format label))
  8520. nil t))))
  8521. (when label (message "Label exists already") (sit-for 2))
  8522. (setq label (read-string "Code line label: " label)))
  8523. (end-of-line 1)
  8524. (setq link (format org-coderef-label-format label))
  8525. (setq gc (- 79 (length link)))
  8526. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8527. (insert link)
  8528. (setq link (concat "(" label ")") desc nil)))
  8529. ;; We are in the agenda, link to referenced location
  8530. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8531. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8532. (get-text-property (point) 'org-marker))))
  8533. (when m
  8534. (org-with-point-at m
  8535. (setq agenda-link
  8536. (if (org-called-interactively-p 'any)
  8537. (call-interactively 'org-store-link)
  8538. (org-store-link nil)))))))
  8539. ((eq major-mode 'calendar-mode)
  8540. (let ((cd (calendar-cursor-to-date)))
  8541. (setq link
  8542. (format-time-string
  8543. (car org-time-stamp-formats)
  8544. (apply 'encode-time
  8545. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8546. nil nil nil))))
  8547. (org-store-link-props :type "calendar" :date cd)))
  8548. ((eq major-mode 'help-mode)
  8549. (setq link (concat "help:" (save-excursion
  8550. (goto-char (point-min))
  8551. (looking-at "^[^ ]+")
  8552. (match-string 0))))
  8553. (org-store-link-props :type "help"))
  8554. ((eq major-mode 'w3-mode)
  8555. (setq cpltxt (if (and (buffer-name)
  8556. (not (string-match "Untitled" (buffer-name))))
  8557. (buffer-name)
  8558. (url-view-url t))
  8559. link (url-view-url t))
  8560. (org-store-link-props :type "w3" :url (url-view-url t)))
  8561. ((eq major-mode 'image-mode)
  8562. (setq cpltxt (concat "file:"
  8563. (abbreviate-file-name buffer-file-name))
  8564. link cpltxt)
  8565. (org-store-link-props :type "image" :file buffer-file-name))
  8566. ;; In dired, store a link to the file of the current line
  8567. ((eq major-mode 'dired-mode)
  8568. (let ((file (dired-get-filename nil t)))
  8569. (setq file (if file
  8570. (abbreviate-file-name
  8571. (expand-file-name (dired-get-filename nil t)))
  8572. ;; otherwise, no file so use current directory.
  8573. default-directory))
  8574. (setq cpltxt (concat "file:" file)
  8575. link cpltxt)))
  8576. ((setq search (run-hook-with-args-until-success
  8577. 'org-create-file-search-functions))
  8578. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8579. "::" search))
  8580. (setq cpltxt (or description link)))
  8581. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8582. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8583. (cond
  8584. ;; Store a link using the target at point
  8585. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8586. (setq cpltxt
  8587. (concat "file:"
  8588. (abbreviate-file-name
  8589. (buffer-file-name (buffer-base-buffer)))
  8590. "::" (match-string 1))
  8591. link cpltxt))
  8592. ((and (featurep 'org-id)
  8593. (or (eq org-id-link-to-org-use-id t)
  8594. (and (org-called-interactively-p 'any)
  8595. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8596. (and (eq org-id-link-to-org-use-id
  8597. 'create-if-interactive-and-no-custom-id)
  8598. (not custom-id))))
  8599. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8600. ;; Store a link using the ID at point
  8601. (setq link (condition-case nil
  8602. (prog1 (org-id-store-link)
  8603. (setq desc (plist-get org-store-link-plist
  8604. :description)))
  8605. (error
  8606. ;; Probably before first headline, link only to file
  8607. (concat "file:"
  8608. (abbreviate-file-name
  8609. (buffer-file-name (buffer-base-buffer))))))))
  8610. (t
  8611. ;; Just link to current headline
  8612. (setq cpltxt (concat "file:"
  8613. (abbreviate-file-name
  8614. (buffer-file-name (buffer-base-buffer)))))
  8615. ;; Add a context search string
  8616. (when (org-xor org-context-in-file-links arg)
  8617. (let* ((ee (org-element-at-point))
  8618. (et (org-element-type ee))
  8619. (ev (plist-get (cadr ee) :value))
  8620. (ek (plist-get (cadr ee) :key))
  8621. (eok (and (stringp ek) (string-match "name" ek))))
  8622. (setq txt (cond
  8623. ((org-at-heading-p) nil)
  8624. ((and (eq et 'keyword) eok) ev)
  8625. ((org-region-active-p)
  8626. (buffer-substring (region-beginning) (region-end)))))
  8627. (when (or (null txt) (string-match "\\S-" txt))
  8628. (setq cpltxt
  8629. (concat cpltxt "::"
  8630. (condition-case nil
  8631. (org-make-org-heading-search-string txt)
  8632. (error "")))
  8633. desc (or (and (eq et 'keyword) eok ev)
  8634. (nth 4 (ignore-errors (org-heading-components)))
  8635. "NONE")))))
  8636. (if (string-match "::\\'" cpltxt)
  8637. (setq cpltxt (substring cpltxt 0 -2)))
  8638. (setq link cpltxt))))
  8639. ((buffer-file-name (buffer-base-buffer))
  8640. ;; Just link to this file here.
  8641. (setq cpltxt (concat "file:"
  8642. (abbreviate-file-name
  8643. (buffer-file-name (buffer-base-buffer)))))
  8644. ;; Add a context string.
  8645. (when (org-xor org-context-in-file-links arg)
  8646. (setq txt (if (org-region-active-p)
  8647. (buffer-substring (region-beginning) (region-end))
  8648. (buffer-substring (point-at-bol) (point-at-eol))))
  8649. ;; Only use search option if there is some text.
  8650. (when (string-match "\\S-" txt)
  8651. (setq cpltxt
  8652. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8653. desc "NONE")))
  8654. (setq link cpltxt))
  8655. ((org-called-interactively-p 'interactive)
  8656. (user-error "No method for storing a link from this buffer"))
  8657. (t (setq link nil)))
  8658. ;; We're done setting link and desc, clean up
  8659. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8660. (setq link (or link cpltxt)
  8661. desc (or desc cpltxt))
  8662. (cond ((equal desc "NONE") (setq desc nil))
  8663. ((string-match org-bracket-link-analytic-regexp desc)
  8664. (let ((d0 (match-string 3 desc))
  8665. (p0 (match-string 5 desc)))
  8666. (setq desc
  8667. (replace-regexp-in-string
  8668. org-bracket-link-regexp
  8669. (concat (or p0 d0)
  8670. (if (equal (length (match-string 0 desc))
  8671. (length desc)) "*" "")) desc)))))
  8672. ;; Return the link
  8673. (if (not (and (or (org-called-interactively-p 'any)
  8674. executing-kbd-macro) link))
  8675. (or agenda-link (and link (org-make-link-string link desc)))
  8676. (push (list link desc) org-stored-links)
  8677. (message "Stored: %s" (or desc link))
  8678. (when custom-id
  8679. (setq link (concat "file:" (abbreviate-file-name
  8680. (buffer-file-name)) "::#" custom-id))
  8681. (push (list link desc) org-stored-links)))))))
  8682. (defun org-store-link-props (&rest plist)
  8683. "Store link properties, extract names and addresses."
  8684. (let (x adr)
  8685. (when (setq x (plist-get plist :from))
  8686. (setq adr (mail-extract-address-components x))
  8687. (setq plist (plist-put plist :fromname (car adr)))
  8688. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8689. (when (setq x (plist-get plist :to))
  8690. (setq adr (mail-extract-address-components x))
  8691. (setq plist (plist-put plist :toname (car adr)))
  8692. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8693. (let ((from (plist-get plist :from))
  8694. (to (plist-get plist :to)))
  8695. (when (and from to org-from-is-user-regexp)
  8696. (setq plist
  8697. (plist-put plist :fromto
  8698. (if (string-match org-from-is-user-regexp from)
  8699. (concat "to %t")
  8700. (concat "from %f"))))))
  8701. (setq org-store-link-plist plist))
  8702. (defun org-add-link-props (&rest plist)
  8703. "Add these properties to the link property list."
  8704. (let (key value)
  8705. (while plist
  8706. (setq key (pop plist) value (pop plist))
  8707. (setq org-store-link-plist
  8708. (plist-put org-store-link-plist key value)))))
  8709. (defun org-email-link-description (&optional fmt)
  8710. "Return the description part of an email link.
  8711. This takes information from `org-store-link-plist' and formats it
  8712. according to FMT (default from `org-email-link-description-format')."
  8713. (setq fmt (or fmt org-email-link-description-format))
  8714. (let* ((p org-store-link-plist)
  8715. (to (plist-get p :toaddress))
  8716. (from (plist-get p :fromaddress))
  8717. (table
  8718. (list
  8719. (cons "%c" (plist-get p :fromto))
  8720. (cons "%F" (plist-get p :from))
  8721. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8722. (cons "%T" (plist-get p :to))
  8723. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8724. (cons "%s" (plist-get p :subject))
  8725. (cons "%d" (plist-get p :date))
  8726. (cons "%m" (plist-get p :message-id)))))
  8727. (when (string-match "%c" fmt)
  8728. ;; Check if the user wrote this message
  8729. (if (and org-from-is-user-regexp from to
  8730. (save-match-data (string-match org-from-is-user-regexp from)))
  8731. (setq fmt (replace-match "to %t" t t fmt))
  8732. (setq fmt (replace-match "from %f" t t fmt))))
  8733. (org-replace-escapes fmt table)))
  8734. (defun org-make-org-heading-search-string (&optional string)
  8735. "Make search string for the current headline or STRING."
  8736. (let ((s (or string
  8737. (and (derived-mode-p 'org-mode)
  8738. (save-excursion
  8739. (org-back-to-heading t)
  8740. (org-element-property :raw-value (org-element-at-point))))))
  8741. (lines org-context-in-file-links))
  8742. (or string (setq s (concat "*" s))) ; Add * for headlines
  8743. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8744. (when (and string (integerp lines) (> lines 0))
  8745. (let ((slines (org-split-string s "\n")))
  8746. (when (< lines (length slines))
  8747. (setq s (mapconcat
  8748. 'identity
  8749. (reverse (nthcdr (- (length slines) lines)
  8750. (reverse slines))) "\n")))))
  8751. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8752. (defun org-make-link-string (link &optional description)
  8753. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8754. (unless (string-match "\\S-" link)
  8755. (error "Empty link"))
  8756. (when (and description
  8757. (stringp description)
  8758. (not (string-match "\\S-" description)))
  8759. (setq description nil))
  8760. (when (stringp description)
  8761. ;; Remove brackets from the description, they are fatal.
  8762. (while (string-match "\\[" description)
  8763. (setq description (replace-match "{" t t description)))
  8764. (while (string-match "\\]" description)
  8765. (setq description (replace-match "}" t t description))))
  8766. (when (equal link description)
  8767. ;; No description needed, it is identical
  8768. (setq description nil))
  8769. (when (and (not description)
  8770. (not (string-match (org-image-file-name-regexp) link))
  8771. (not (equal link (org-link-escape link))))
  8772. (setq description (org-extract-attributes link)))
  8773. (setq link
  8774. (cond ((string-match (org-image-file-name-regexp) link) link)
  8775. ((string-match org-link-types-re link)
  8776. (concat (match-string 1 link)
  8777. (org-link-escape (substring link (match-end 1)))))
  8778. (t (org-link-escape link))))
  8779. (concat "[[" link "]"
  8780. (if description (concat "[" description "]") "")
  8781. "]"))
  8782. (defconst org-link-escape-chars
  8783. ;;%20 %2B %3B %3D %5B %5D
  8784. '(?\ ?\+ ?\; ?\= ?\[ ?\])
  8785. "List of characters that should be escaped in link.
  8786. This is the list that is used for internal purposes.")
  8787. (defconst org-link-escape-chars-browser
  8788. ;;%20 %22
  8789. '(?\ ?\")
  8790. "List of escapes for characters that are problematic in links.
  8791. This is the list that is used before handing over to the browser.")
  8792. (defun org-link-escape (text &optional table merge)
  8793. "Return percent escaped representation of TEXT.
  8794. TEXT is a string with the text to escape.
  8795. Optional argument TABLE is a list with characters that should be
  8796. escaped. When nil, `org-link-escape-chars' is used.
  8797. If optional argument MERGE is set, merge TABLE into
  8798. `org-link-escape-chars'."
  8799. (cond
  8800. ((and table merge)
  8801. (mapc (lambda (defchr)
  8802. (unless (member defchr table)
  8803. (setq table (cons defchr table)))) org-link-escape-chars))
  8804. ((null table)
  8805. (setq table org-link-escape-chars)))
  8806. (mapconcat
  8807. (lambda (char)
  8808. (if (or (member char table)
  8809. (and (or (< char 32) (= char ?\%) (> char 126))
  8810. org-url-hexify-p))
  8811. (mapconcat (lambda (sequence-element)
  8812. (format "%%%.2X" sequence-element))
  8813. (or (encode-coding-char char 'utf-8)
  8814. (error "Unable to percent escape character: %s"
  8815. (char-to-string char))) "")
  8816. (char-to-string char))) text ""))
  8817. (defun org-link-escape-browser (text)
  8818. (if (org-string-match-p
  8819. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8820. text)
  8821. (org-link-escape text org-link-escape-chars-browser)
  8822. text))
  8823. (defun org-link-unescape (str)
  8824. "Unhex hexified Unicode strings as returned from the JavaScript function
  8825. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8826. (unless (and (null str) (string= "" str))
  8827. (let ((pos 0) (case-fold-search t) unhexed)
  8828. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8829. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8830. (setq str (replace-match unhexed t t str))
  8831. (setq pos (+ pos (length unhexed))))))
  8832. str)
  8833. (defun org-link-unescape-compound (hex)
  8834. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8835. Note: this function also decodes single byte encodings like
  8836. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8837. (save-match-data
  8838. (let* ((bytes (cdr (split-string hex "%")))
  8839. (ret "")
  8840. (eat 0)
  8841. (sum 0))
  8842. (while bytes
  8843. (let* ((val (string-to-number (pop bytes) 16))
  8844. (shift-xor
  8845. (if (= 0 eat)
  8846. (cond
  8847. ((>= val 252) (cons 6 252))
  8848. ((>= val 248) (cons 5 248))
  8849. ((>= val 240) (cons 4 240))
  8850. ((>= val 224) (cons 3 224))
  8851. ((>= val 192) (cons 2 192))
  8852. (t (cons 0 0)))
  8853. (cons 6 128))))
  8854. (if (>= val 192) (setq eat (car shift-xor)))
  8855. (setq val (logxor val (cdr shift-xor)))
  8856. (setq sum (+ (lsh sum (car shift-xor)) val))
  8857. (if (> eat 0) (setq eat (- eat 1)))
  8858. (cond
  8859. ((= 0 eat) ;multi byte
  8860. (setq ret (concat ret (org-char-to-string sum)))
  8861. (setq sum 0))
  8862. ((not bytes) ; single byte(s)
  8863. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8864. )) ;; end (while bytes
  8865. ret )))
  8866. (defun org-link-unescape-single-byte-sequence (hex)
  8867. "Unhexify hex-encoded single byte character sequences."
  8868. (mapconcat (lambda (byte)
  8869. (char-to-string (string-to-number byte 16)))
  8870. (cdr (split-string hex "%")) ""))
  8871. (defun org-xor (a b)
  8872. "Exclusive or."
  8873. (if a (not b) b))
  8874. (defun org-fixup-message-id-for-http (s)
  8875. "Replace special characters in a message id, so it can be used in an http query."
  8876. (when (string-match "%" s)
  8877. (setq s (mapconcat (lambda (c)
  8878. (if (eq c ?%)
  8879. "%25"
  8880. (char-to-string c)))
  8881. s "")))
  8882. (while (string-match "<" s)
  8883. (setq s (replace-match "%3C" t t s)))
  8884. (while (string-match ">" s)
  8885. (setq s (replace-match "%3E" t t s)))
  8886. (while (string-match "@" s)
  8887. (setq s (replace-match "%40" t t s)))
  8888. s)
  8889. (defun org-link-prettify (link)
  8890. "Return a human-readable representation of LINK.
  8891. The car of LINK must be a raw link the cdr of LINK must be either
  8892. a link description or nil."
  8893. (let ((desc (or (cadr link) "<no description>")))
  8894. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8895. "<" (car link) ">")))
  8896. ;;;###autoload
  8897. (defun org-insert-link-global ()
  8898. "Insert a link like Org-mode does.
  8899. This command can be called in any mode to insert a link in Org-mode syntax."
  8900. (interactive)
  8901. (org-load-modules-maybe)
  8902. (org-run-like-in-org-mode 'org-insert-link))
  8903. (defun org-insert-all-links (&optional keep)
  8904. "Insert all links in `org-stored-links'."
  8905. (interactive "P")
  8906. (let ((links (copy-sequence org-stored-links)) l)
  8907. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8908. (insert "- ")
  8909. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8910. (insert "\n"))))
  8911. (defun org-link-fontify-links-to-this-file ()
  8912. "Fontify links to the current file in `org-stored-links'."
  8913. (let ((f (buffer-file-name)) a b)
  8914. (setq a (mapcar (lambda(l)
  8915. (let ((ll (car l)))
  8916. (when (and (string-match "^file:\\(.+\\)::" ll)
  8917. (equal f (expand-file-name (match-string 1 ll))))
  8918. ll)))
  8919. org-stored-links))
  8920. (when (featurep 'org-id)
  8921. (setq b (mapcar (lambda(l)
  8922. (let ((ll (car l)))
  8923. (when (and (string-match "^id:\\(.+\\)$" ll)
  8924. (equal f (expand-file-name
  8925. (or (org-id-find-id-file
  8926. (match-string 1 ll)) ""))))
  8927. ll)))
  8928. org-stored-links)))
  8929. (mapcar (lambda(l)
  8930. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8931. (delq nil (append a b)))))
  8932. (defvar org-link-links-in-this-file nil)
  8933. (defun org-insert-link (&optional complete-file link-location default-description)
  8934. "Insert a link. At the prompt, enter the link.
  8935. Completion can be used to insert any of the link protocol prefixes like
  8936. http or ftp in use.
  8937. The history can be used to select a link previously stored with
  8938. `org-store-link'. When the empty string is entered (i.e. if you just
  8939. press RET at the prompt), the link defaults to the most recently
  8940. stored link. As SPC triggers completion in the minibuffer, you need to
  8941. use M-SPC or C-q SPC to force the insertion of a space character.
  8942. You will also be prompted for a description, and if one is given, it will
  8943. be displayed in the buffer instead of the link.
  8944. If there is already a link at point, this command will allow you to edit link
  8945. and description parts.
  8946. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8947. be selected using completion. The path to the file will be relative to the
  8948. current directory if the file is in the current directory or a subdirectory.
  8949. Otherwise, the link will be the absolute path as completed in the minibuffer
  8950. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8951. option `org-link-file-path-type'.
  8952. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8953. the current directory or below.
  8954. With three \\[universal-argument] prefixes, negate the meaning of
  8955. `org-keep-stored-link-after-insertion'.
  8956. If `org-make-link-description-function' is non-nil, this function will be
  8957. called with the link target, and the result will be the default
  8958. link description.
  8959. If the LINK-LOCATION parameter is non-nil, this value will be
  8960. used as the link location instead of reading one interactively.
  8961. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8962. be used as the default description."
  8963. (interactive "P")
  8964. (let* ((wcf (current-window-configuration))
  8965. (origbuf (current-buffer))
  8966. (region (if (org-region-active-p)
  8967. (buffer-substring (region-beginning) (region-end))))
  8968. (remove (and region (list (region-beginning) (region-end))))
  8969. (desc region)
  8970. tmphist ; byte-compile incorrectly complains about this
  8971. (link link-location)
  8972. (abbrevs org-link-abbrev-alist-local)
  8973. entry file all-prefixes auto-desc)
  8974. (cond
  8975. (link-location) ; specified by arg, just use it.
  8976. ((org-in-regexp org-bracket-link-regexp 1)
  8977. ;; We do have a link at point, and we are going to edit it.
  8978. (setq remove (list (match-beginning 0) (match-end 0)))
  8979. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8980. (setq link (read-string "Link: "
  8981. (org-link-unescape
  8982. (org-match-string-no-properties 1)))))
  8983. ((or (org-in-regexp org-angle-link-re)
  8984. (org-in-regexp org-plain-link-re))
  8985. ;; Convert to bracket link
  8986. (setq remove (list (match-beginning 0) (match-end 0))
  8987. link (read-string "Link: "
  8988. (org-remove-angle-brackets (match-string 0)))))
  8989. ((member complete-file '((4) (16)))
  8990. ;; Completing read for file names.
  8991. (setq link (org-file-complete-link complete-file)))
  8992. (t
  8993. ;; Read link, with completion for stored links.
  8994. (org-link-fontify-links-to-this-file)
  8995. (org-switch-to-buffer-other-window "*Org Links*")
  8996. (with-current-buffer "*Org Links*"
  8997. (erase-buffer)
  8998. (insert "Insert a link.
  8999. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9000. (when org-stored-links
  9001. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9002. (insert (mapconcat 'org-link-prettify
  9003. (reverse org-stored-links) "\n")))
  9004. (goto-char (point-min)))
  9005. (let ((cw (selected-window)))
  9006. (select-window (get-buffer-window "*Org Links*" 'visible))
  9007. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9008. (unless (pos-visible-in-window-p (point-max))
  9009. (org-fit-window-to-buffer))
  9010. (and (window-live-p cw) (select-window cw)))
  9011. ;; Fake a link history, containing the stored links.
  9012. (setq tmphist (append (mapcar 'car org-stored-links)
  9013. org-insert-link-history))
  9014. (setq all-prefixes (append (mapcar 'car abbrevs)
  9015. (mapcar 'car org-link-abbrev-alist)
  9016. org-link-types))
  9017. (unwind-protect
  9018. (progn
  9019. (setq link
  9020. (org-completing-read
  9021. "Link: "
  9022. (append
  9023. (mapcar (lambda (x) (concat x ":"))
  9024. all-prefixes)
  9025. (mapcar 'car org-stored-links))
  9026. nil nil nil
  9027. 'tmphist
  9028. (caar org-stored-links)))
  9029. (if (not (string-match "\\S-" link))
  9030. (user-error "No link selected"))
  9031. (mapc (lambda(l)
  9032. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9033. org-stored-links)
  9034. (if (or (member link all-prefixes)
  9035. (and (equal ":" (substring link -1))
  9036. (member (substring link 0 -1) all-prefixes)
  9037. (setq link (substring link 0 -1))))
  9038. (setq link (with-current-buffer origbuf
  9039. (org-link-try-special-completion link)))))
  9040. (set-window-configuration wcf)
  9041. (kill-buffer "*Org Links*"))
  9042. (setq entry (assoc link org-stored-links))
  9043. (or entry (push link org-insert-link-history))
  9044. (setq desc (or desc (nth 1 entry)))))
  9045. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9046. (not org-keep-stored-link-after-insertion))
  9047. (setq org-stored-links (delq (assoc link org-stored-links)
  9048. org-stored-links)))
  9049. (if (and (string-match org-plain-link-re link)
  9050. (not (string-match org-ts-regexp link)))
  9051. ;; URL-like link, normalize the use of angular brackets.
  9052. (setq link (org-remove-angle-brackets link)))
  9053. ;; Check if we are linking to the current file with a search
  9054. ;; option If yes, simplify the link by using only the search
  9055. ;; option.
  9056. (when (and buffer-file-name
  9057. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9058. (let* ((path (match-string 1 link))
  9059. (case-fold-search nil)
  9060. (search (match-string 2 link)))
  9061. (save-match-data
  9062. (if (equal (file-truename buffer-file-name) (file-truename path))
  9063. ;; We are linking to this same file, with a search option
  9064. (setq link search)))))
  9065. ;; Check if we can/should use a relative path. If yes, simplify the link
  9066. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9067. (let* ((type (match-string 1 link))
  9068. (path (match-string 2 link))
  9069. (origpath path)
  9070. (case-fold-search nil))
  9071. (cond
  9072. ((or (eq org-link-file-path-type 'absolute)
  9073. (equal complete-file '(16)))
  9074. (setq path (abbreviate-file-name (expand-file-name path))))
  9075. ((eq org-link-file-path-type 'noabbrev)
  9076. (setq path (expand-file-name path)))
  9077. ((eq org-link-file-path-type 'relative)
  9078. (setq path (file-relative-name path)))
  9079. (t
  9080. (save-match-data
  9081. (if (string-match (concat "^" (regexp-quote
  9082. (expand-file-name
  9083. (file-name-as-directory
  9084. default-directory))))
  9085. (expand-file-name path))
  9086. ;; We are linking a file with relative path name.
  9087. (setq path (substring (expand-file-name path)
  9088. (match-end 0)))
  9089. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9090. (setq link (concat type path))
  9091. (if (equal desc origpath)
  9092. (setq desc path))))
  9093. (if org-make-link-description-function
  9094. (setq desc
  9095. (or (condition-case nil
  9096. (funcall org-make-link-description-function link desc)
  9097. (error (progn (message "Can't get link description from `%s'"
  9098. (symbol-name org-make-link-description-function))
  9099. (sit-for 2) nil)))
  9100. (read-string "Description: " default-description)))
  9101. (if default-description (setq desc default-description)
  9102. (setq desc (or (and auto-desc desc)
  9103. (read-string "Description: " desc)))))
  9104. (unless (string-match "\\S-" desc) (setq desc nil))
  9105. (if remove (apply 'delete-region remove))
  9106. (insert (org-make-link-string link desc))))
  9107. (defun org-link-try-special-completion (type)
  9108. "If there is completion support for link type TYPE, offer it."
  9109. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9110. (if (functionp fun)
  9111. (funcall fun)
  9112. (read-string "Link (no completion support): " (concat type ":")))))
  9113. (defun org-file-complete-link (&optional arg)
  9114. "Create a file link using completion."
  9115. (let (file link)
  9116. (setq file (org-iread-file-name "File: "))
  9117. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9118. (pwd1 (file-name-as-directory (abbreviate-file-name
  9119. (expand-file-name ".")))))
  9120. (cond
  9121. ((equal arg '(16))
  9122. (setq link (concat
  9123. "file:"
  9124. (abbreviate-file-name (expand-file-name file)))))
  9125. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9126. (setq link (concat "file:" (match-string 1 file))))
  9127. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9128. (expand-file-name file))
  9129. (setq link (concat
  9130. "file:" (match-string 1 (expand-file-name file)))))
  9131. (t (setq link (concat "file:" file)))))
  9132. link))
  9133. (defun org-iread-file-name (&rest args)
  9134. "Read-file-name using `ido-mode' speedup if available.
  9135. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9136. See `read-file-name' for a description of parameters."
  9137. (org-without-partial-completion
  9138. (if (and org-completion-use-ido
  9139. (fboundp 'ido-read-file-name)
  9140. (boundp 'ido-mode) ido-mode
  9141. (listp (second args)))
  9142. (let ((ido-enter-matching-directory nil))
  9143. (apply 'ido-read-file-name args))
  9144. (apply 'read-file-name args))))
  9145. (defun org-completing-read (&rest args)
  9146. "Completing-read with SPACE being a normal character."
  9147. (let ((enable-recursive-minibuffers t)
  9148. (minibuffer-local-completion-map
  9149. (copy-keymap minibuffer-local-completion-map)))
  9150. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9151. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9152. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9153. (apply 'org-icompleting-read args)))
  9154. (defun org-completing-read-no-i (&rest args)
  9155. (let (org-completion-use-ido org-completion-use-iswitchb)
  9156. (apply 'org-completing-read args)))
  9157. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9158. "Use iswitch as a completing-read replacement to choose from choices.
  9159. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9160. from."
  9161. (let* ((iswitchb-use-virtual-buffers nil)
  9162. (iswitchb-make-buflist-hook
  9163. (lambda ()
  9164. (setq iswitchb-temp-buflist choices))))
  9165. (iswitchb-read-buffer prompt)))
  9166. (defun org-icompleting-read (&rest args)
  9167. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9168. (org-without-partial-completion
  9169. (if (and org-completion-use-ido
  9170. (fboundp 'ido-completing-read)
  9171. (boundp 'ido-mode) ido-mode
  9172. (listp (second args)))
  9173. (let ((ido-enter-matching-directory nil))
  9174. (apply 'ido-completing-read (concat (car args))
  9175. (if (consp (car (nth 1 args)))
  9176. (mapcar 'car (nth 1 args))
  9177. (nth 1 args))
  9178. (cddr args)))
  9179. (if (and org-completion-use-iswitchb
  9180. (boundp 'iswitchb-mode) iswitchb-mode
  9181. (listp (second args)))
  9182. (apply 'org-iswitchb-completing-read (concat (car args))
  9183. (if (consp (car (nth 1 args)))
  9184. (mapcar 'car (nth 1 args))
  9185. (nth 1 args))
  9186. (cddr args))
  9187. (apply 'completing-read args)))))
  9188. (defun org-extract-attributes (s)
  9189. "Extract the attributes cookie from a string and set as text property."
  9190. (let (a attr (start 0) key value)
  9191. (save-match-data
  9192. (when (string-match "{{\\([^}]+\\)}}$" s)
  9193. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9194. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9195. (setq key (match-string 1 a) value (match-string 2 a)
  9196. start (match-end 0)
  9197. attr (plist-put attr (intern key) value))))
  9198. (org-add-props s nil 'org-attr attr))
  9199. s))
  9200. ;;; Opening/following a link
  9201. (defvar org-link-search-failed nil)
  9202. (defvar org-open-link-functions nil
  9203. "Hook for functions finding a plain text link.
  9204. These functions must take a single argument, the link content.
  9205. They will be called for links that look like [[link text][description]]
  9206. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9207. like a filename (e.g. \"./blue.png\").
  9208. These functions will be called *before* Org attempts to resolve the
  9209. link by doing text searches in the current buffer - so if you want a
  9210. link \"[[target]]\" to still find \"<<target>>\", your function should
  9211. handle this as a special case.
  9212. When the function does handle the link, it must return a non-nil value.
  9213. If it decides that it is not responsible for this link, it must return
  9214. nil to indicate that that Org-mode can continue with other options
  9215. like exact and fuzzy text search.")
  9216. (defun org-next-link (&optional search-backward)
  9217. "Move forward to the next link.
  9218. If the link is in hidden text, expose it."
  9219. (interactive "P")
  9220. (when (and org-link-search-failed (eq this-command last-command))
  9221. (goto-char (point-min))
  9222. (message "Link search wrapped back to beginning of buffer"))
  9223. (setq org-link-search-failed nil)
  9224. (let* ((pos (point))
  9225. (ct (org-context))
  9226. (a (assoc :link ct))
  9227. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9228. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9229. ((looking-at org-any-link-re)
  9230. ;; Don't stay stuck at link without an org-link face
  9231. (forward-char (if search-backward -1 1))))
  9232. (if (funcall srch-fun org-any-link-re nil t)
  9233. (progn
  9234. (goto-char (match-beginning 0))
  9235. (if (outline-invisible-p) (org-show-context)))
  9236. (goto-char pos)
  9237. (setq org-link-search-failed t)
  9238. (message "No further link found"))))
  9239. (defun org-previous-link ()
  9240. "Move backward to the previous link.
  9241. If the link is in hidden text, expose it."
  9242. (interactive)
  9243. (funcall 'org-next-link t))
  9244. (defun org-translate-link (s)
  9245. "Translate a link string if a translation function has been defined."
  9246. (if (and org-link-translation-function
  9247. (fboundp org-link-translation-function)
  9248. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9249. (progn
  9250. (setq s (funcall org-link-translation-function
  9251. (match-string 1 s) (match-string 2 s)))
  9252. (concat (car s) ":" (cdr s)))
  9253. s))
  9254. (defun org-translate-link-from-planner (type path)
  9255. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9256. This is still an experimental function, your mileage may vary."
  9257. (cond
  9258. ((member type '("http" "https" "news" "ftp"))
  9259. ;; standard Internet links are the same.
  9260. nil)
  9261. ((and (equal type "irc") (string-match "^//" path))
  9262. ;; Planner has two / at the beginning of an irc link, we have 1.
  9263. ;; We should have zero, actually....
  9264. (setq path (substring path 1)))
  9265. ((and (equal type "lisp") (string-match "^/" path))
  9266. ;; Planner has a slash, we do not.
  9267. (setq type "elisp" path (substring path 1)))
  9268. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9269. ;; A typical message link. Planner has the id after the final slash,
  9270. ;; we separate it with a hash mark
  9271. (setq path (concat (match-string 1 path) "#"
  9272. (org-remove-angle-brackets (match-string 2 path))))))
  9273. (cons type path))
  9274. (defun org-find-file-at-mouse (ev)
  9275. "Open file link or URL at mouse."
  9276. (interactive "e")
  9277. (mouse-set-point ev)
  9278. (org-open-at-point 'in-emacs))
  9279. (defun org-open-at-mouse (ev)
  9280. "Open file link or URL at mouse.
  9281. See the docstring of `org-open-file' for details."
  9282. (interactive "e")
  9283. (mouse-set-point ev)
  9284. (if (eq major-mode 'org-agenda-mode)
  9285. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9286. (org-open-at-point))
  9287. (defvar org-window-config-before-follow-link nil
  9288. "The window configuration before following a link.
  9289. This is saved in case the need arises to restore it.")
  9290. (defvar org-open-link-marker (make-marker)
  9291. "Marker pointing to the location where `org-open-at-point; was called.")
  9292. ;;;###autoload
  9293. (defun org-open-at-point-global ()
  9294. "Follow a link like Org-mode does.
  9295. This command can be called in any mode to follow a link that has
  9296. Org-mode syntax."
  9297. (interactive)
  9298. (org-run-like-in-org-mode 'org-open-at-point))
  9299. ;;;###autoload
  9300. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9301. "Open a link in the string S, as if it was in Org-mode."
  9302. (interactive "sLink: \nP")
  9303. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9304. (with-temp-buffer
  9305. (let ((org-inhibit-startup (not reference-buffer)))
  9306. (org-mode)
  9307. (insert s)
  9308. (goto-char (point-min))
  9309. (when reference-buffer
  9310. (setq org-link-abbrev-alist-local
  9311. (with-current-buffer reference-buffer
  9312. org-link-abbrev-alist-local)))
  9313. (org-open-at-point arg reference-buffer)))))
  9314. (defvar org-open-at-point-functions nil
  9315. "Hook that is run when following a link at point.
  9316. Functions in this hook must return t if they identify and follow
  9317. a link at point. If they don't find anything interesting at point,
  9318. they must return nil.")
  9319. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9320. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9321. (defun org-open-at-point (&optional arg reference-buffer)
  9322. "Open link at or after point.
  9323. If there is no link at point, this function will search forward up to
  9324. the end of the current line.
  9325. Normally, files will be opened by an appropriate application. If the
  9326. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9327. With a double prefix argument, try to open outside of Emacs, in the
  9328. application the system uses for this file type."
  9329. (interactive "P")
  9330. ;; if in a code block, then open the block's results
  9331. (unless (call-interactively #'org-babel-open-src-block-result)
  9332. (org-load-modules-maybe)
  9333. (move-marker org-open-link-marker (point))
  9334. (setq org-window-config-before-follow-link (current-window-configuration))
  9335. (org-remove-occur-highlights nil nil t)
  9336. (cond
  9337. ((and (org-at-heading-p)
  9338. (not (org-at-timestamp-p t))
  9339. (not (org-in-regexp
  9340. (concat org-plain-link-re "\\|"
  9341. org-bracket-link-regexp "\\|"
  9342. org-angle-link-re "\\|"
  9343. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9344. (not (get-text-property (point) 'org-linked-text)))
  9345. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9346. (lk0 (car lkall))
  9347. (lk (if (stringp lk0) (list lk0) lk0))
  9348. (lkend (cdr lkall)))
  9349. (mapcar (lambda(l)
  9350. (search-forward l nil lkend)
  9351. (goto-char (match-beginning 0))
  9352. (org-open-at-point))
  9353. lk))
  9354. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9355. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9356. ((and (org-at-timestamp-p t)
  9357. (not (org-in-regexp org-bracket-link-regexp)))
  9358. (org-follow-timestamp-link))
  9359. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9360. (not (org-in-regexp org-any-link-re)))
  9361. (org-footnote-action))
  9362. (t
  9363. (let (type path link line search (pos (point)))
  9364. (catch 'match
  9365. (save-excursion
  9366. (skip-chars-forward "^]\n\r")
  9367. (when (org-in-regexp org-bracket-link-regexp 1)
  9368. (setq link (org-extract-attributes
  9369. (org-link-unescape (org-match-string-no-properties 1))))
  9370. (while (string-match " *\n *" link)
  9371. (setq link (replace-match " " t t link)))
  9372. (setq link (org-link-expand-abbrev link))
  9373. (cond
  9374. ((or (file-name-absolute-p link)
  9375. (string-match "^\\.\\.?/" link))
  9376. (setq type "file" path link))
  9377. ((string-match org-link-re-with-space3 link)
  9378. (setq type (match-string 1 link) path (match-string 2 link)))
  9379. ((string-match "^help:+\\(.+\\)" link)
  9380. (setq type "help" path (match-string 1 link)))
  9381. (t (setq type "thisfile" path link)))
  9382. (throw 'match t)))
  9383. (when (get-text-property (point) 'org-linked-text)
  9384. (setq type "thisfile"
  9385. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9386. (1+ (point)) (point))
  9387. path (buffer-substring
  9388. (or (previous-single-property-change pos 'org-linked-text)
  9389. (point-min))
  9390. (or (next-single-property-change pos 'org-linked-text)
  9391. (point-max)))
  9392. ;; Ensure we will search for a <<<radio>>> link, not
  9393. ;; a simple reference like <<ref>>
  9394. path (concat "<" path))
  9395. (throw 'match t))
  9396. (save-excursion
  9397. (when (or (org-in-regexp org-angle-link-re)
  9398. (let ((match (org-in-regexp org-plain-link-re)))
  9399. ;; Check a plain link is not within a bracket link
  9400. (and match
  9401. (save-excursion
  9402. (progn
  9403. (goto-char (car match))
  9404. (not (org-in-regexp org-bracket-link-regexp))))))
  9405. (let ((line_ending (save-excursion (end-of-line) (point))))
  9406. ;; We are in a line before a plain or bracket link
  9407. (or (re-search-forward org-plain-link-re line_ending t)
  9408. (re-search-forward org-bracket-link-regexp line_ending t))))
  9409. (setq type (match-string 1)
  9410. path (org-link-unescape (match-string 2)))
  9411. (throw 'match t)))
  9412. (save-excursion
  9413. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9414. (setq type "tags"
  9415. path (match-string 1))
  9416. (while (string-match ":" path)
  9417. (setq path (replace-match "+" t t path)))
  9418. (throw 'match t)))
  9419. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9420. (setq type "tree-match"
  9421. path (match-string 1))
  9422. (throw 'match t)))
  9423. (unless path
  9424. (user-error "No link found"))
  9425. ;; switch back to reference buffer
  9426. ;; needed when if called in a temporary buffer through
  9427. ;; org-open-link-from-string
  9428. (with-current-buffer (or reference-buffer (current-buffer))
  9429. ;; Remove any trailing spaces in path
  9430. (if (string-match " +\\'" path)
  9431. (setq path (replace-match "" t t path)))
  9432. (if (and org-link-translation-function
  9433. (fboundp org-link-translation-function))
  9434. ;; Check if we need to translate the link
  9435. (let ((tmp (funcall org-link-translation-function type path)))
  9436. (setq type (car tmp) path (cdr tmp))))
  9437. (cond
  9438. ((assoc type org-link-protocols)
  9439. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9440. ((equal type "help")
  9441. (let ((f-or-v (intern path)))
  9442. (cond ((fboundp f-or-v)
  9443. (describe-function f-or-v))
  9444. ((boundp f-or-v)
  9445. (describe-variable f-or-v))
  9446. (t (error "Not a known function or variable")))))
  9447. ((equal type "mailto")
  9448. (let ((cmd (car org-link-mailto-program))
  9449. (args (cdr org-link-mailto-program)) args1
  9450. (address path) (subject "") a)
  9451. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9452. (setq address (match-string 1 path)
  9453. subject (org-link-escape (match-string 2 path))))
  9454. (while args
  9455. (cond
  9456. ((not (stringp (car args))) (push (pop args) args1))
  9457. (t (setq a (pop args))
  9458. (if (string-match "%a" a)
  9459. (setq a (replace-match address t t a)))
  9460. (if (string-match "%s" a)
  9461. (setq a (replace-match subject t t a)))
  9462. (push a args1))))
  9463. (apply cmd (nreverse args1))))
  9464. ((member type '("http" "https" "ftp" "news"))
  9465. (browse-url (concat type ":" (org-link-escape-browser path))))
  9466. ((string= type "doi")
  9467. (browse-url (concat org-doi-server-url
  9468. (org-link-escape-browser path))))
  9469. ((member type '("message"))
  9470. (browse-url (concat type ":" path)))
  9471. ((string= type "tags")
  9472. (org-tags-view arg path))
  9473. ((string= type "tree-match")
  9474. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9475. ((string= type "file")
  9476. (if (string-match "::\\([0-9]+\\)\\'" path)
  9477. (setq line (string-to-number (match-string 1 path))
  9478. path (substring path 0 (match-beginning 0)))
  9479. (if (string-match "::\\(.+\\)\\'" path)
  9480. (setq search (match-string 1 path)
  9481. path (substring path 0 (match-beginning 0)))))
  9482. (if (string-match "[*?{]" (file-name-nondirectory path))
  9483. (dired path)
  9484. (org-open-file path arg line search)))
  9485. ((string= type "shell")
  9486. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9487. (cmd path))
  9488. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9489. (string-match org-confirm-shell-link-not-regexp cmd))
  9490. (not org-confirm-shell-link-function)
  9491. (funcall org-confirm-shell-link-function
  9492. (format "Execute \"%s\" in shell? "
  9493. (org-add-props cmd nil
  9494. 'face 'org-warning))))
  9495. (progn
  9496. (message "Executing %s" cmd)
  9497. (shell-command cmd buf)
  9498. (if (featurep 'midnight)
  9499. (setq clean-buffer-list-kill-buffer-names
  9500. (cons buf clean-buffer-list-kill-buffer-names))))
  9501. (error "Abort"))))
  9502. ((string= type "elisp")
  9503. (let ((cmd path))
  9504. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9505. (string-match org-confirm-elisp-link-not-regexp cmd))
  9506. (not org-confirm-elisp-link-function)
  9507. (funcall org-confirm-elisp-link-function
  9508. (format "Execute \"%s\" as elisp? "
  9509. (org-add-props cmd nil
  9510. 'face 'org-warning))))
  9511. (message "%s => %s" cmd
  9512. (if (equal (string-to-char cmd) ?\()
  9513. (eval (read cmd))
  9514. (call-interactively (read cmd))))
  9515. (error "Abort"))))
  9516. ((and (string= type "thisfile")
  9517. (or (run-hook-with-args-until-success
  9518. 'org-open-link-functions path)
  9519. (and link
  9520. (string-match "^id:" link)
  9521. (or (featurep 'org-id) (require 'org-id))
  9522. (progn
  9523. (funcall (nth 1 (assoc "id" org-link-protocols))
  9524. (substring path 3))
  9525. t)))))
  9526. ((string= type "thisfile")
  9527. (if arg
  9528. (switch-to-buffer-other-window
  9529. (org-get-buffer-for-internal-link (current-buffer)))
  9530. (org-mark-ring-push))
  9531. (let ((cmd `(org-link-search
  9532. ,path
  9533. ,(cond ((equal arg '(4)) ''occur)
  9534. ((equal arg '(16)) ''org-occur))
  9535. ,pos)))
  9536. (condition-case nil (let ((org-link-search-inhibit-query t))
  9537. (eval cmd))
  9538. (error (progn (widen) (eval cmd))))))
  9539. (t (browse-url-at-point)))))))
  9540. (move-marker org-open-link-marker nil)
  9541. (run-hook-with-args 'org-follow-link-hook)))
  9542. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9543. "Offer links in the current entry and return the selected link.
  9544. If there is only one link, return it.
  9545. If NTH is an integer, return the NTH link found.
  9546. If ZERO is a string, check also this string for a link, and if
  9547. there is one, return it."
  9548. (with-current-buffer buffer
  9549. (save-excursion
  9550. (save-restriction
  9551. (widen)
  9552. (goto-char marker)
  9553. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9554. "\\(" org-angle-link-re "\\)\\|"
  9555. "\\(" org-plain-link-re "\\)"))
  9556. (cnt ?0)
  9557. (in-emacs (if (integerp nth) nil nth))
  9558. have-zero end links link c)
  9559. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9560. (push (match-string 0 zero) links)
  9561. (setq cnt (1- cnt) have-zero t))
  9562. (save-excursion
  9563. (org-back-to-heading t)
  9564. (setq end (save-excursion (outline-next-heading) (point)))
  9565. (while (re-search-forward re end t)
  9566. (push (match-string 0) links))
  9567. (setq links (org-uniquify (reverse links))))
  9568. (cond
  9569. ((null links)
  9570. (message "No links"))
  9571. ((equal (length links) 1)
  9572. (setq link (car links)))
  9573. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9574. (setq link (nth (if have-zero nth (1- nth)) links)))
  9575. (t ; we have to select a link
  9576. (save-excursion
  9577. (save-window-excursion
  9578. (delete-other-windows)
  9579. (with-output-to-temp-buffer "*Select Link*"
  9580. (mapc (lambda (l)
  9581. (if (not (string-match org-bracket-link-regexp l))
  9582. (princ (format "[%c] %s\n" (incf cnt)
  9583. (org-remove-angle-brackets l)))
  9584. (if (match-end 3)
  9585. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9586. (match-string 3 l) (match-string 1 l)))
  9587. (princ (format "[%c] %s\n" (incf cnt)
  9588. (match-string 1 l))))))
  9589. links))
  9590. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9591. (message "Select link to open, RET to open all:")
  9592. (setq c (read-char-exclusive))
  9593. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9594. (when (equal c ?q) (error "Abort"))
  9595. (if (equal c ?\C-m)
  9596. (setq link links)
  9597. (setq nth (- c ?0))
  9598. (if have-zero (setq nth (1+ nth)))
  9599. (unless (and (integerp nth) (>= (length links) nth))
  9600. (user-error "Invalid link selection"))
  9601. (setq link (nth (1- nth) links)))))
  9602. (cons link end))))))
  9603. ;; Add special file links that specify the way of opening
  9604. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9605. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9606. (defun org-open-file-with-system (path)
  9607. "Open file at PATH using the system way of opening it."
  9608. (org-open-file path 'system))
  9609. (defun org-open-file-with-emacs (path)
  9610. "Open file at PATH in Emacs."
  9611. (org-open-file path 'emacs))
  9612. ;;; File search
  9613. (defvar org-create-file-search-functions nil
  9614. "List of functions to construct the right search string for a file link.
  9615. These functions are called in turn with point at the location to
  9616. which the link should point.
  9617. A function in the hook should first test if it would like to
  9618. handle this file type, for example by checking the `major-mode'
  9619. or the file extension. If it decides not to handle this file, it
  9620. should just return nil to give other functions a chance. If it
  9621. does handle the file, it must return the search string to be used
  9622. when following the link. The search string will be part of the
  9623. file link, given after a double colon, and `org-open-at-point'
  9624. will automatically search for it. If special measures must be
  9625. taken to make the search successful, another function should be
  9626. added to the companion hook `org-execute-file-search-functions',
  9627. which see.
  9628. A function in this hook may also use `setq' to set the variable
  9629. `description' to provide a suggestion for the descriptive text to
  9630. be used for this link when it gets inserted into an Org-mode
  9631. buffer with \\[org-insert-link].")
  9632. (defvar org-execute-file-search-functions nil
  9633. "List of functions to execute a file search triggered by a link.
  9634. Functions added to this hook must accept a single argument, the
  9635. search string that was part of the file link, the part after the
  9636. double colon. The function must first check if it would like to
  9637. handle this search, for example by checking the `major-mode' or
  9638. the file extension. If it decides not to handle this search, it
  9639. should just return nil to give other functions a chance. If it
  9640. does handle the search, it must return a non-nil value to keep
  9641. other functions from trying.
  9642. Each function can access the current prefix argument through the
  9643. variable `current-prefix-arg'. Note that a single prefix is used
  9644. to force opening a link in Emacs, so it may be good to only use a
  9645. numeric or double prefix to guide the search function.
  9646. In case this is needed, a function in this hook can also restore
  9647. the window configuration before `org-open-at-point' was called using:
  9648. (set-window-configuration org-window-config-before-follow-link)")
  9649. (defun org-link-search (s &optional type avoid-pos stealth)
  9650. "Search for a link search option.
  9651. If S is surrounded by forward slashes, it is interpreted as a
  9652. regular expression. In org-mode files, this will create an `org-occur'
  9653. sparse tree. In ordinary files, `occur' will be used to list matches.
  9654. If the current buffer is in `dired-mode', grep will be used to search
  9655. in all files. If AVOID-POS is given, ignore matches near that position.
  9656. When optional argument STEALTH is non-nil, do not modify
  9657. visibility around point, thus ignoring
  9658. `org-show-hierarchy-above', `org-show-following-heading' and
  9659. `org-show-siblings' variables."
  9660. (let ((case-fold-search t)
  9661. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9662. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9663. (append '(("") (" ") ("\t") ("\n"))
  9664. org-emphasis-alist)
  9665. "\\|") "\\)"))
  9666. (pos (point))
  9667. (pre nil) (post nil)
  9668. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9669. (cond
  9670. ;; First check if there are any special search functions
  9671. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9672. ;; Now try the builtin stuff
  9673. ((and (equal (string-to-char s0) ?#)
  9674. (> (length s0) 1)
  9675. (save-excursion
  9676. (goto-char (point-min))
  9677. (and
  9678. (re-search-forward
  9679. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9680. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9681. (setq type 'dedicated
  9682. pos (match-beginning 0))))
  9683. ;; There is an exact target for this
  9684. (goto-char pos)
  9685. (org-back-to-heading t)))
  9686. ((save-excursion
  9687. (goto-char (point-min))
  9688. (and
  9689. (re-search-forward
  9690. (concat "<<" (regexp-quote s0) ">>") nil t)
  9691. (setq type 'dedicated
  9692. pos (match-beginning 0))))
  9693. ;; There is an exact target for this
  9694. (goto-char pos))
  9695. ((save-excursion
  9696. (goto-char (point-min))
  9697. (and
  9698. (re-search-forward
  9699. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9700. (setq type 'dedicated pos (match-beginning 0))))
  9701. ;; Found an element with a matching #+name affiliated keyword.
  9702. (goto-char pos))
  9703. ((and (string-match "^(\\(.*\\))$" s0)
  9704. (save-excursion
  9705. (goto-char (point-min))
  9706. (and
  9707. (re-search-forward
  9708. (concat "[^[]" (regexp-quote
  9709. (format org-coderef-label-format
  9710. (match-string 1 s0))))
  9711. nil t)
  9712. (setq type 'dedicated
  9713. pos (1+ (match-beginning 0))))))
  9714. ;; There is a coderef target for this
  9715. (goto-char pos))
  9716. ((string-match "^/\\(.*\\)/$" s)
  9717. ;; A regular expression
  9718. (cond
  9719. ((derived-mode-p 'org-mode)
  9720. (org-occur (match-string 1 s)))
  9721. (t (org-do-occur (match-string 1 s)))))
  9722. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9723. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9724. (goto-char (point-min))
  9725. (cond
  9726. ((let (case-fold-search)
  9727. (re-search-forward (format org-complex-heading-regexp-format
  9728. (regexp-quote s))
  9729. nil t))
  9730. ;; OK, found a match
  9731. (setq type 'dedicated)
  9732. (goto-char (match-beginning 0)))
  9733. ((and (not org-link-search-inhibit-query)
  9734. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9735. (y-or-n-p "No match - create this as a new heading? "))
  9736. (goto-char (point-max))
  9737. (or (bolp) (newline))
  9738. (insert "* " s "\n")
  9739. (beginning-of-line 0))
  9740. (t
  9741. (goto-char pos)
  9742. (error "No match"))))
  9743. (t
  9744. ;; A normal search string
  9745. (when (equal (string-to-char s) ?*)
  9746. ;; Anchor on headlines, post may include tags.
  9747. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9748. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9749. s (substring s 1)))
  9750. (remove-text-properties
  9751. 0 (length s)
  9752. '(face nil mouse-face nil keymap nil fontified nil) s)
  9753. ;; Make a series of regular expressions to find a match
  9754. (setq words (org-split-string s "[ \n\r\t]+")
  9755. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9756. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9757. "\\)" markers)
  9758. re2a_ (concat "\\(" (mapconcat 'downcase words
  9759. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9760. re2a (concat "[ \t\r\n]" re2a_)
  9761. re4_ (concat "\\(" (mapconcat 'downcase words
  9762. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9763. re4 (concat "[^a-zA-Z_]" re4_)
  9764. re1 (concat pre re2 post)
  9765. re3 (concat pre (if pre re4_ re4) post)
  9766. re5 (concat pre ".*" re4)
  9767. re2 (concat pre re2)
  9768. re2a (concat pre (if pre re2a_ re2a))
  9769. re4 (concat pre (if pre re4_ re4))
  9770. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9771. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9772. re5 "\\)"))
  9773. (cond
  9774. ((eq type 'org-occur) (org-occur reall))
  9775. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9776. (t (goto-char (point-min))
  9777. (setq type 'fuzzy)
  9778. (if (or (and (org-search-not-self 1 re0 nil t)
  9779. (setq type 'dedicated))
  9780. (org-search-not-self 1 re1 nil t)
  9781. (org-search-not-self 1 re2 nil t)
  9782. (org-search-not-self 1 re2a nil t)
  9783. (org-search-not-self 1 re3 nil t)
  9784. (org-search-not-self 1 re4 nil t)
  9785. (org-search-not-self 1 re5 nil t))
  9786. (goto-char (match-beginning 1))
  9787. (goto-char pos)
  9788. (error "No match"))))))
  9789. (and (derived-mode-p 'org-mode)
  9790. (not stealth)
  9791. (org-show-context 'link-search))
  9792. type))
  9793. (defun org-search-not-self (group &rest args)
  9794. "Execute `re-search-forward', but only accept matches that do not
  9795. enclose the position of `org-open-link-marker'."
  9796. (let ((m org-open-link-marker))
  9797. (catch 'exit
  9798. (while (apply 're-search-forward args)
  9799. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9800. (goto-char (match-end group))
  9801. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9802. (> (match-beginning 0) (marker-position m))
  9803. (< (match-end 0) (marker-position m)))
  9804. (save-match-data
  9805. (or (not (org-in-regexp
  9806. org-bracket-link-analytic-regexp 1))
  9807. (not (match-end 4)) ; no description
  9808. (and (<= (match-beginning 4) (point))
  9809. (>= (match-end 4) (point))))))
  9810. (throw 'exit (point))))))))
  9811. (defun org-get-buffer-for-internal-link (buffer)
  9812. "Return a buffer to be used for displaying the link target of internal links."
  9813. (cond
  9814. ((not org-display-internal-link-with-indirect-buffer)
  9815. buffer)
  9816. ((string-match "(Clone)$" (buffer-name buffer))
  9817. (message "Buffer is already a clone, not making another one")
  9818. ;; we also do not modify visibility in this case
  9819. buffer)
  9820. (t ; make a new indirect buffer for displaying the link
  9821. (let* ((bn (buffer-name buffer))
  9822. (ibn (concat bn "(Clone)"))
  9823. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9824. (with-current-buffer ib (org-overview))
  9825. ib))))
  9826. (defun org-do-occur (regexp &optional cleanup)
  9827. "Call the Emacs command `occur'.
  9828. If CLEANUP is non-nil, remove the printout of the regular expression
  9829. in the *Occur* buffer. This is useful if the regex is long and not useful
  9830. to read."
  9831. (occur regexp)
  9832. (when cleanup
  9833. (let ((cwin (selected-window)) win beg end)
  9834. (when (setq win (get-buffer-window "*Occur*"))
  9835. (select-window win))
  9836. (goto-char (point-min))
  9837. (when (re-search-forward "match[a-z]+" nil t)
  9838. (setq beg (match-end 0))
  9839. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9840. (setq end (1- (match-beginning 0)))))
  9841. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9842. (goto-char (point-min))
  9843. (select-window cwin))))
  9844. ;;; The mark ring for links jumps
  9845. (defvar org-mark-ring nil
  9846. "Mark ring for positions before jumps in Org-mode.")
  9847. (defvar org-mark-ring-last-goto nil
  9848. "Last position in the mark ring used to go back.")
  9849. ;; Fill and close the ring
  9850. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9851. (loop for i from 1 to org-mark-ring-length do
  9852. (push (make-marker) org-mark-ring))
  9853. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9854. org-mark-ring)
  9855. (defun org-mark-ring-push (&optional pos buffer)
  9856. "Put the current position or POS into the mark ring and rotate it."
  9857. (interactive)
  9858. (setq pos (or pos (point)))
  9859. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9860. (move-marker (car org-mark-ring)
  9861. (or pos (point))
  9862. (or buffer (current-buffer)))
  9863. (message "%s"
  9864. (substitute-command-keys
  9865. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9866. (defun org-mark-ring-goto (&optional n)
  9867. "Jump to the previous position in the mark ring.
  9868. With prefix arg N, jump back that many stored positions. When
  9869. called several times in succession, walk through the entire ring.
  9870. Org-mode commands jumping to a different position in the current file,
  9871. or to another Org-mode file, automatically push the old position
  9872. onto the ring."
  9873. (interactive "p")
  9874. (let (p m)
  9875. (if (eq last-command this-command)
  9876. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9877. (setq p org-mark-ring))
  9878. (setq org-mark-ring-last-goto p)
  9879. (setq m (car p))
  9880. (org-pop-to-buffer-same-window (marker-buffer m))
  9881. (goto-char m)
  9882. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9883. (defun org-remove-angle-brackets (s)
  9884. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9885. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9886. s)
  9887. (defun org-add-angle-brackets (s)
  9888. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9889. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9890. s)
  9891. (defun org-remove-double-quotes (s)
  9892. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9893. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9894. s)
  9895. ;;; Following specific links
  9896. (defun org-follow-timestamp-link ()
  9897. "Open an agenda view for the time-stamp date/range at point."
  9898. (cond
  9899. ((org-at-date-range-p t)
  9900. (let ((org-agenda-start-on-weekday)
  9901. (t1 (match-string 1))
  9902. (t2 (match-string 2)) tt1 tt2)
  9903. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9904. tt2 (time-to-days (org-time-string-to-time t2)))
  9905. (let ((org-agenda-buffer-tmp-name
  9906. (format "*Org Agenda(a:%s)"
  9907. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9908. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9909. ((org-at-timestamp-p t)
  9910. (let ((org-agenda-buffer-tmp-name
  9911. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9912. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9913. (substring (match-string 1) 0 10)))
  9914. 1)))
  9915. (t (error "This should not happen"))))
  9916. ;;; Following file links
  9917. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9918. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9919. (declare-function mailcap-mime-info
  9920. "mailcap" (string &optional request no-decode))
  9921. (defvar org-wait nil)
  9922. (defun org-open-file (path &optional in-emacs line search)
  9923. "Open the file at PATH.
  9924. First, this expands any special file name abbreviations. Then the
  9925. configuration variable `org-file-apps' is checked if it contains an
  9926. entry for this file type, and if yes, the corresponding command is launched.
  9927. If no application is found, Emacs simply visits the file.
  9928. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9929. With a double \\[universal-argument] \\[universal-argument] \
  9930. prefix arg, Org tries to avoid opening in Emacs
  9931. and to use an external application to visit the file.
  9932. Optional LINE specifies a line to go to, optional SEARCH a string
  9933. to search for. If LINE or SEARCH is given, the file will be
  9934. opened in Emacs, unless an entry from org-file-apps that makes
  9935. use of groups in a regexp matches.
  9936. If you want to change the way frames are used when following a
  9937. link, please customize `org-link-frame-setup'.
  9938. If the file does not exist, an error is thrown."
  9939. (let* ((file (if (equal path "")
  9940. buffer-file-name
  9941. (substitute-in-file-name (expand-file-name path))))
  9942. (file-apps (append org-file-apps (org-default-apps)))
  9943. (apps (org-remove-if
  9944. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9945. (apps-dlink (org-remove-if-not
  9946. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9947. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9948. (dirp (if remp nil (file-directory-p file)))
  9949. (file (if (and dirp org-open-directory-means-index-dot-org)
  9950. (concat (file-name-as-directory file) "index.org")
  9951. file))
  9952. (a-m-a-p (assq 'auto-mode apps))
  9953. (dfile (downcase file))
  9954. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9955. (link (cond ((and (eq line nil)
  9956. (eq search nil))
  9957. file)
  9958. (line
  9959. (concat file "::" (number-to-string line)))
  9960. (search
  9961. (concat file "::" search))))
  9962. (dlink (downcase link))
  9963. (old-buffer (current-buffer))
  9964. (old-pos (point))
  9965. (old-mode major-mode)
  9966. ext cmd link-match-data)
  9967. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9968. (setq ext (match-string 1 dfile))
  9969. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9970. (setq ext (match-string 1 dfile))))
  9971. (cond
  9972. ((member in-emacs '((16) system))
  9973. (setq cmd (cdr (assoc 'system apps))))
  9974. (in-emacs (setq cmd 'emacs))
  9975. (t
  9976. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9977. (and dirp (cdr (assoc 'directory apps)))
  9978. ; first, try matching against apps-dlink
  9979. ; if we get a match here, store the match data for later
  9980. (let ((match (assoc-default dlink apps-dlink
  9981. 'string-match)))
  9982. (if match
  9983. (progn (setq link-match-data (match-data))
  9984. match)
  9985. (progn (setq in-emacs (or in-emacs line search))
  9986. nil))) ; if we have no match in apps-dlink,
  9987. ; always open the file in emacs if line or search
  9988. ; is given (for backwards compatibility)
  9989. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9990. 'string-match)
  9991. (cdr (assoc ext apps))
  9992. (cdr (assoc t apps))))))
  9993. (when (eq cmd 'system)
  9994. (setq cmd (cdr (assoc 'system apps))))
  9995. (when (eq cmd 'default)
  9996. (setq cmd (cdr (assoc t apps))))
  9997. (when (eq cmd 'mailcap)
  9998. (require 'mailcap)
  9999. (mailcap-parse-mailcaps)
  10000. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10001. (command (mailcap-mime-info mime-type)))
  10002. (if (stringp command)
  10003. (setq cmd command)
  10004. (setq cmd 'emacs))))
  10005. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10006. (not (file-exists-p file))
  10007. (not org-open-non-existing-files))
  10008. (user-error "No such file: %s" file))
  10009. (cond
  10010. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10011. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10012. (while (string-match "['\"]%s['\"]" cmd)
  10013. (setq cmd (replace-match "%s" t t cmd)))
  10014. (while (string-match "%s" cmd)
  10015. (setq cmd (replace-match
  10016. (save-match-data
  10017. (shell-quote-argument
  10018. (convert-standard-filename file)))
  10019. t t cmd)))
  10020. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10021. (save-match-data
  10022. (let ((match-index 1)
  10023. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10024. (set-match-data link-match-data)
  10025. (while (<= match-index number-of-groups)
  10026. (let ((regex (concat "%" (number-to-string match-index)))
  10027. (replace-with (match-string match-index dlink)))
  10028. (while (string-match regex cmd)
  10029. (setq cmd (replace-match replace-with t t cmd))))
  10030. (setq match-index (+ match-index 1)))))
  10031. (save-window-excursion
  10032. (message "Running %s...done" cmd)
  10033. (start-process-shell-command cmd nil cmd)
  10034. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10035. ((or (stringp cmd)
  10036. (eq cmd 'emacs))
  10037. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10038. (widen)
  10039. (if line (progn (org-goto-line line)
  10040. (if (derived-mode-p 'org-mode)
  10041. (org-reveal)))
  10042. (if search (org-link-search search))))
  10043. ((consp cmd)
  10044. (let ((file (convert-standard-filename file)))
  10045. (save-match-data
  10046. (set-match-data link-match-data)
  10047. (eval cmd))))
  10048. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10049. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10050. (or (not (equal old-buffer (current-buffer)))
  10051. (not (equal old-pos (point))))
  10052. (org-mark-ring-push old-pos old-buffer))))
  10053. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10054. "This function returns non-nil if `entry' uses a regular
  10055. expression which should be matched against the whole link by
  10056. org-open-file.
  10057. It assumes that is the case when the entry uses a regular
  10058. expression which has at least one grouping construct and the
  10059. action is either a lisp form or a command string containing
  10060. '%1', i.e. using at least one subexpression match as a
  10061. parameter."
  10062. (let ((selector (car entry))
  10063. (action (cdr entry)))
  10064. (if (stringp selector)
  10065. (and (> (regexp-opt-depth selector) 0)
  10066. (or (and (stringp action)
  10067. (string-match "%[0-9]" action))
  10068. (consp action)))
  10069. nil)))
  10070. (defun org-default-apps ()
  10071. "Return the default applications for this operating system."
  10072. (cond
  10073. ((eq system-type 'darwin)
  10074. org-file-apps-defaults-macosx)
  10075. ((eq system-type 'windows-nt)
  10076. org-file-apps-defaults-windowsnt)
  10077. (t org-file-apps-defaults-gnu)))
  10078. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10079. "Convert extensions to regular expressions in the cars of LIST.
  10080. Also, weed out any non-string entries, because the return value is used
  10081. only for regexp matching.
  10082. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10083. point to the symbol `emacs', indicating that the file should
  10084. be opened in Emacs."
  10085. (append
  10086. (delq nil
  10087. (mapcar (lambda (x)
  10088. (if (not (stringp (car x)))
  10089. nil
  10090. (if (string-match "\\W" (car x))
  10091. x
  10092. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10093. list))
  10094. (if add-auto-mode
  10095. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10096. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10097. (defun org-file-remote-p (file)
  10098. "Test whether FILE specifies a location on a remote system.
  10099. Return non-nil if the location is indeed remote.
  10100. For example, the filename \"/user@host:/foo\" specifies a location
  10101. on the system \"/user@host:\"."
  10102. (cond ((fboundp 'file-remote-p)
  10103. (file-remote-p file))
  10104. ((fboundp 'tramp-handle-file-remote-p)
  10105. (tramp-handle-file-remote-p file))
  10106. ((and (boundp 'ange-ftp-name-format)
  10107. (string-match (car ange-ftp-name-format) file))
  10108. t)))
  10109. ;;;; Refiling
  10110. (defun org-get-org-file ()
  10111. "Read a filename, with default directory `org-directory'."
  10112. (let ((default (or org-default-notes-file remember-data-file)))
  10113. (read-file-name (format "File name [%s]: " default)
  10114. (file-name-as-directory org-directory)
  10115. default)))
  10116. (defun org-notes-order-reversed-p ()
  10117. "Check if the current file should receive notes in reversed order."
  10118. (cond
  10119. ((not org-reverse-note-order) nil)
  10120. ((eq t org-reverse-note-order) t)
  10121. ((not (listp org-reverse-note-order)) nil)
  10122. (t (catch 'exit
  10123. (let ((all org-reverse-note-order)
  10124. entry)
  10125. (while (setq entry (pop all))
  10126. (if (string-match (car entry) buffer-file-name)
  10127. (throw 'exit (cdr entry))))
  10128. nil)))))
  10129. (defvar org-refile-target-table nil
  10130. "The list of refile targets, created by `org-refile'.")
  10131. (defvar org-agenda-new-buffers nil
  10132. "Buffers created to visit agenda files.")
  10133. (defvar org-refile-cache nil
  10134. "Cache for refile targets.")
  10135. (defvar org-refile-markers nil
  10136. "All the markers used for caching refile locations.")
  10137. (defun org-refile-marker (pos)
  10138. "Get a new refile marker, but only if caching is in use."
  10139. (if (not org-refile-use-cache)
  10140. pos
  10141. (let ((m (make-marker)))
  10142. (move-marker m pos)
  10143. (push m org-refile-markers)
  10144. m)))
  10145. (defun org-refile-cache-clear ()
  10146. "Clear the refile cache and disable all the markers."
  10147. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10148. (setq org-refile-markers nil)
  10149. (setq org-refile-cache nil)
  10150. (message "Refile cache has been cleared"))
  10151. (defun org-refile-cache-check-set (set)
  10152. "Check if all the markers in the cache still have live buffers."
  10153. (let (marker)
  10154. (catch 'exit
  10155. (while (and set (setq marker (nth 3 (pop set))))
  10156. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10157. (when (and marker (null (marker-buffer marker)))
  10158. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10159. (sit-for 3)
  10160. (throw 'exit nil)))
  10161. t)))
  10162. (defun org-refile-cache-put (set &rest identifiers)
  10163. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10164. (let* ((key (sha1 (prin1-to-string identifiers)))
  10165. (entry (assoc key org-refile-cache)))
  10166. (if entry
  10167. (setcdr entry set)
  10168. (push (cons key set) org-refile-cache))))
  10169. (defun org-refile-cache-get (&rest identifiers)
  10170. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10171. (cond
  10172. ((not org-refile-cache) nil)
  10173. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10174. (t
  10175. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10176. org-refile-cache))))
  10177. (and set (org-refile-cache-check-set set) set)))))
  10178. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10179. "Produce a table with refile targets."
  10180. (let ((case-fold-search nil)
  10181. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10182. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10183. targets tgs txt re files f desc descre fast-path-p level pos0)
  10184. (message "Getting targets...")
  10185. (with-current-buffer (or default-buffer (current-buffer))
  10186. (while (setq entry (pop entries))
  10187. (setq files (car entry) desc (cdr entry))
  10188. (setq fast-path-p nil)
  10189. (cond
  10190. ((null files) (setq files (list (current-buffer))))
  10191. ((eq files 'org-agenda-files)
  10192. (setq files (org-agenda-files 'unrestricted)))
  10193. ((and (symbolp files) (fboundp files))
  10194. (setq files (funcall files)))
  10195. ((and (symbolp files) (boundp files))
  10196. (setq files (symbol-value files))))
  10197. (if (stringp files) (setq files (list files)))
  10198. (cond
  10199. ((eq (car desc) :tag)
  10200. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10201. ((eq (car desc) :todo)
  10202. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10203. ((eq (car desc) :regexp)
  10204. (setq descre (cdr desc)))
  10205. ((eq (car desc) :level)
  10206. (setq descre (concat "^\\*\\{" (number-to-string
  10207. (if org-odd-levels-only
  10208. (1- (* 2 (cdr desc)))
  10209. (cdr desc)))
  10210. "\\}[ \t]")))
  10211. ((eq (car desc) :maxlevel)
  10212. (setq fast-path-p t)
  10213. (setq descre (concat "^\\*\\{1," (number-to-string
  10214. (if org-odd-levels-only
  10215. (1- (* 2 (cdr desc)))
  10216. (cdr desc)))
  10217. "\\}[ \t]")))
  10218. (t (error "Bad refiling target description %s" desc)))
  10219. (while (setq f (pop files))
  10220. (with-current-buffer
  10221. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10222. (or
  10223. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10224. (progn
  10225. (if (bufferp f) (setq f (buffer-file-name
  10226. (buffer-base-buffer f))))
  10227. (setq f (and f (expand-file-name f)))
  10228. (if (eq org-refile-use-outline-path 'file)
  10229. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10230. (save-excursion
  10231. (save-restriction
  10232. (widen)
  10233. (goto-char (point-min))
  10234. (while (re-search-forward descre nil t)
  10235. (goto-char (setq pos0 (point-at-bol)))
  10236. (catch 'next
  10237. (when org-refile-target-verify-function
  10238. (save-match-data
  10239. (or (funcall org-refile-target-verify-function)
  10240. (throw 'next t))))
  10241. (when (and (looking-at org-complex-heading-regexp)
  10242. (not (member (match-string 4) excluded-entries))
  10243. (match-string 4))
  10244. (setq level (org-reduced-level
  10245. (- (match-end 1) (match-beginning 1)))
  10246. txt (org-link-display-format (match-string 4))
  10247. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10248. re (format org-complex-heading-regexp-format
  10249. (regexp-quote (match-string 4))))
  10250. (when org-refile-use-outline-path
  10251. (setq txt (mapconcat
  10252. 'org-protect-slash
  10253. (append
  10254. (if (eq org-refile-use-outline-path
  10255. 'file)
  10256. (list (file-name-nondirectory
  10257. (buffer-file-name
  10258. (buffer-base-buffer))))
  10259. (if (eq org-refile-use-outline-path
  10260. 'full-file-path)
  10261. (list (buffer-file-name
  10262. (buffer-base-buffer)))))
  10263. (org-get-outline-path fast-path-p
  10264. level txt)
  10265. (list txt))
  10266. "/")))
  10267. (push (list txt f re (org-refile-marker (point)))
  10268. tgs)))
  10269. (when (= (point) pos0)
  10270. ;; verification function has not moved point
  10271. (goto-char (point-at-eol))))))))
  10272. (when org-refile-use-cache
  10273. (org-refile-cache-put tgs (buffer-file-name) descre))
  10274. (setq targets (append tgs targets))))))
  10275. (message "Getting targets...done")
  10276. (nreverse targets)))
  10277. (defun org-protect-slash (s)
  10278. (while (string-match "/" s)
  10279. (setq s (replace-match "\\" t t s)))
  10280. s)
  10281. (defvar org-olpa (make-vector 20 nil))
  10282. (defun org-get-outline-path (&optional fastp level heading)
  10283. "Return the outline path to the current entry, as a list.
  10284. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10285. routine which makes outline path derivations for an entire file,
  10286. avoiding backtracing. Refile target collection makes use of that."
  10287. (if fastp
  10288. (progn
  10289. (if (> level 19)
  10290. (error "Outline path failure, more than 19 levels"))
  10291. (loop for i from level upto 19 do
  10292. (aset org-olpa i nil))
  10293. (prog1
  10294. (delq nil (append org-olpa nil))
  10295. (aset org-olpa level heading)))
  10296. (let (rtn case-fold-search)
  10297. (save-excursion
  10298. (save-restriction
  10299. (widen)
  10300. (while (org-up-heading-safe)
  10301. (when (looking-at org-complex-heading-regexp)
  10302. (push (org-trim
  10303. (replace-regexp-in-string
  10304. ;; Remove statistical/checkboxes cookies
  10305. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10306. (org-match-string-no-properties 4)))
  10307. rtn)))
  10308. rtn)))))
  10309. (defun org-format-outline-path (path &optional width prefix separator)
  10310. "Format the outline path PATH for display.
  10311. WIDTH is the maximum number of characters that is available.
  10312. PREFIX is a prefix to be included in the returned string,
  10313. such as the file name.
  10314. SEPARATOR is inserted between the different parts of the path,
  10315. the default is \"/\"."
  10316. (setq width (or width 79))
  10317. (if prefix (setq width (- width (length prefix))))
  10318. (if (not path)
  10319. (or prefix "")
  10320. (let* ((nsteps (length path))
  10321. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10322. (maxwidth (if (<= total-width width)
  10323. 10000 ;; everything fits
  10324. ;; we need to shorten the level headings
  10325. (/ (- width nsteps) nsteps)))
  10326. (org-odd-levels-only nil)
  10327. (n 0)
  10328. (total (1+ (length prefix))))
  10329. (setq maxwidth (max maxwidth 10))
  10330. (concat prefix
  10331. (if prefix (or separator "/"))
  10332. (mapconcat
  10333. (lambda (h)
  10334. (setq n (1+ n))
  10335. (if (and (= n nsteps) (< maxwidth 10000))
  10336. (setq maxwidth (- total-width total)))
  10337. (if (< (length h) maxwidth)
  10338. (progn (setq total (+ total (length h) 1)) h)
  10339. (setq h (substring h 0 (- maxwidth 2))
  10340. total (+ total maxwidth 1))
  10341. (if (string-match "[ \t]+\\'" h)
  10342. (setq h (substring h 0 (match-beginning 0))))
  10343. (setq h (concat h "..")))
  10344. (org-add-props h nil 'face
  10345. (nth (% (1- n) org-n-level-faces)
  10346. org-level-faces))
  10347. h)
  10348. path (or separator "/"))))))
  10349. (defun org-display-outline-path (&optional file current separator just-return-string)
  10350. "Display the current outline path in the echo area.
  10351. If FILE is non-nil, prepend the output with the file name.
  10352. If CURRENT is non-nil, append the current heading to the output.
  10353. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10354. the different parts of the path and defaults to \"/\".
  10355. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10356. (interactive "P")
  10357. (let* (case-fold-search
  10358. (bfn (buffer-file-name (buffer-base-buffer)))
  10359. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10360. res)
  10361. (if current (setq path (append path
  10362. (save-excursion
  10363. (org-back-to-heading t)
  10364. (if (looking-at org-complex-heading-regexp)
  10365. (list (match-string 4)))))))
  10366. (setq res
  10367. (org-format-outline-path
  10368. path
  10369. (1- (frame-width))
  10370. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10371. separator))
  10372. (if just-return-string
  10373. (org-no-properties res)
  10374. (org-unlogged-message "%s" res))))
  10375. (defvar org-refile-history nil
  10376. "History for refiling operations.")
  10377. (defvar org-after-refile-insert-hook nil
  10378. "Hook run after `org-refile' has inserted its stuff at the new location.
  10379. Note that this is still *before* the stuff will be removed from
  10380. the *old* location.")
  10381. (defvar org-capture-last-stored-marker)
  10382. (defvar org-refile-keep nil
  10383. "Non-nil means `org-refile' will copy instead of refile.")
  10384. (defun org-copy ()
  10385. "Like `org-refile', but copy."
  10386. (interactive)
  10387. (let ((org-refile-keep t))
  10388. (funcall 'org-refile nil nil nil "Copy")))
  10389. (defun org-refile (&optional goto default-buffer rfloc msg)
  10390. "Move the entry or entries at point to another heading.
  10391. The list of target headings is compiled using the information in
  10392. `org-refile-targets', which see.
  10393. At the target location, the entry is filed as a subitem of the target
  10394. heading. Depending on `org-reverse-note-order', the new subitem will
  10395. either be the first or the last subitem.
  10396. If there is an active region, all entries in that region will be moved.
  10397. However, the region must fulfill the requirement that the first heading
  10398. is the first one sets the top-level of the moved text - at most siblings
  10399. below it are allowed.
  10400. With prefix arg GOTO, the command will only visit the target location
  10401. and not actually move anything.
  10402. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10403. go to the location where the last refiling operation has put the subtree.
  10404. With a numeric prefix argument of `2', refile to the running clock.
  10405. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10406. being set to `t' and copy to the target location, don't move it.
  10407. Beware that keeping refiled entries may result in duplicated ID
  10408. properties.
  10409. RFLOC can be a refile location obtained in a different way.
  10410. MSG is a string to replace \"Refile\" in the default prompt with
  10411. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10412. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10413. If you are using target caching (see `org-refile-use-cache'),
  10414. you have to clear the target cache in order to find new targets.
  10415. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10416. prefix argument (`C-u C-u C-u C-c C-w')."
  10417. (interactive "P")
  10418. (if (member goto '(0 (64)))
  10419. (org-refile-cache-clear)
  10420. (let* ((actionmsg (or msg "Refile"))
  10421. (cbuf (current-buffer))
  10422. (regionp (org-region-active-p))
  10423. (region-start (and regionp (region-beginning)))
  10424. (region-end (and regionp (region-end)))
  10425. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10426. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10427. pos it nbuf file re level reversed)
  10428. (setq last-command nil)
  10429. (when regionp
  10430. (goto-char region-start)
  10431. (or (bolp) (goto-char (point-at-bol)))
  10432. (setq region-start (point))
  10433. (unless (or (org-kill-is-subtree-p
  10434. (buffer-substring region-start region-end))
  10435. (prog1 org-refile-active-region-within-subtree
  10436. (let ((s (point-at-eol)))
  10437. (org-toggle-heading)
  10438. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10439. (user-error "The region is not a (sequence of) subtree(s)")))
  10440. (if (equal goto '(16))
  10441. (org-refile-goto-last-stored)
  10442. (when (or
  10443. (and (equal goto 2)
  10444. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10445. (prog1
  10446. (setq it (list (or org-clock-heading "running clock")
  10447. (buffer-file-name
  10448. (marker-buffer org-clock-hd-marker))
  10449. ""
  10450. (marker-position org-clock-hd-marker)))
  10451. (setq goto nil)))
  10452. (setq it (or rfloc
  10453. (let (heading-text)
  10454. (save-excursion
  10455. (unless goto
  10456. (org-back-to-heading t)
  10457. (setq heading-text
  10458. (nth 4 (org-heading-components))))
  10459. (org-refile-get-location
  10460. (cond (goto "Goto")
  10461. (regionp (concat actionmsg " region to"))
  10462. (t (concat actionmsg " subtree \""
  10463. heading-text "\" to")))
  10464. default-buffer
  10465. (and (not (equal '(4) goto))
  10466. org-refile-allow-creating-parent-nodes)
  10467. goto))))))
  10468. (setq file (nth 1 it)
  10469. re (nth 2 it)
  10470. pos (nth 3 it))
  10471. (if (and (not goto)
  10472. pos
  10473. (equal (buffer-file-name) file)
  10474. (if regionp
  10475. (and (>= pos region-start)
  10476. (<= pos region-end))
  10477. (and (>= pos (point))
  10478. (< pos (save-excursion
  10479. (org-end-of-subtree t t))))))
  10480. (error "Cannot refile to position inside the tree or region"))
  10481. (setq nbuf (or (find-buffer-visiting file)
  10482. (find-file-noselect file)))
  10483. (if (and goto (not (equal goto 3)))
  10484. (progn
  10485. (org-pop-to-buffer-same-window nbuf)
  10486. (goto-char pos)
  10487. (org-show-context 'org-goto))
  10488. (if regionp
  10489. (progn
  10490. (org-kill-new (buffer-substring region-start region-end))
  10491. (org-save-markers-in-region region-start region-end))
  10492. (org-copy-subtree 1 nil t))
  10493. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10494. (find-file-noselect file)))
  10495. (setq reversed (org-notes-order-reversed-p))
  10496. (save-excursion
  10497. (save-restriction
  10498. (widen)
  10499. (if pos
  10500. (progn
  10501. (goto-char pos)
  10502. (looking-at org-outline-regexp)
  10503. (setq level (org-get-valid-level (funcall outline-level) 1))
  10504. (goto-char
  10505. (if reversed
  10506. (or (outline-next-heading) (point-max))
  10507. (or (save-excursion (org-get-next-sibling))
  10508. (org-end-of-subtree t t)
  10509. (point-max)))))
  10510. (setq level 1)
  10511. (if (not reversed)
  10512. (goto-char (point-max))
  10513. (goto-char (point-min))
  10514. (or (outline-next-heading) (goto-char (point-max)))))
  10515. (if (not (bolp)) (newline))
  10516. (org-paste-subtree level)
  10517. (when org-log-refile
  10518. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10519. (unless (eq org-log-refile 'note)
  10520. (save-excursion (org-add-log-note))))
  10521. (and org-auto-align-tags
  10522. (let ((org-loop-over-headlines-in-active-region nil))
  10523. (org-set-tags nil t)))
  10524. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10525. :last-refile)))
  10526. (when bookmark-name
  10527. (with-demoted-errors
  10528. (bookmark-set bookmark-name))))
  10529. ;; If we are refiling for capture, make sure that the
  10530. ;; last-capture pointers point here
  10531. (when (org-bound-and-true-p org-refile-for-capture)
  10532. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10533. :last-capture-marker)))
  10534. (when bookmark-name
  10535. (with-demoted-errors
  10536. (bookmark-set bookmark-name))))
  10537. (move-marker org-capture-last-stored-marker (point)))
  10538. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10539. (run-hooks 'org-after-refile-insert-hook))))
  10540. (unless org-refile-keep
  10541. (if regionp
  10542. (delete-region (point) (+ (point) (- region-end region-start)))
  10543. (delete-region
  10544. (and (org-back-to-heading t) (point))
  10545. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10546. (when (featurep 'org-inlinetask)
  10547. (org-inlinetask-remove-END-maybe))
  10548. (setq org-markers-to-move nil)
  10549. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10550. (defun org-refile-goto-last-stored ()
  10551. "Go to the location where the last refile was stored."
  10552. (interactive)
  10553. (bookmark-jump "org-refile-last-stored")
  10554. (message "This is the location of the last refile"))
  10555. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10556. no-exclude)
  10557. "Prompt the user for a refile location, using PROMPT.
  10558. PROMPT should not be suffixed with a colon and a space, because
  10559. this function appends the default value from
  10560. `org-refile-history' automatically, if that is not empty.
  10561. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10562. this is used for the GOTO interface."
  10563. (let ((org-refile-targets org-refile-targets)
  10564. (org-refile-use-outline-path org-refile-use-outline-path)
  10565. excluded-entries)
  10566. (when (and (derived-mode-p 'org-mode)
  10567. (not org-refile-use-cache)
  10568. (not no-exclude))
  10569. (org-map-tree
  10570. (lambda()
  10571. (setq excluded-entries
  10572. (append excluded-entries (list (org-get-heading t t)))))))
  10573. (setq org-refile-target-table
  10574. (org-refile-get-targets default-buffer excluded-entries)))
  10575. (unless org-refile-target-table
  10576. (user-error "No refile targets"))
  10577. (let* ((cbuf (current-buffer))
  10578. (partial-completion-mode nil)
  10579. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10580. (cfunc (if (and org-refile-use-outline-path
  10581. org-outline-path-complete-in-steps)
  10582. 'org-olpath-completing-read
  10583. 'org-icompleting-read))
  10584. (extra (if org-refile-use-outline-path "/" ""))
  10585. (cbnex (concat (buffer-name) extra))
  10586. (filename (and cfn (expand-file-name cfn)))
  10587. (tbl (mapcar
  10588. (lambda (x)
  10589. (if (and (not (member org-refile-use-outline-path
  10590. '(file full-file-path)))
  10591. (not (equal filename (nth 1 x))))
  10592. (cons (concat (car x) extra " ("
  10593. (file-name-nondirectory (nth 1 x)) ")")
  10594. (cdr x))
  10595. (cons (concat (car x) extra) (cdr x))))
  10596. org-refile-target-table))
  10597. (completion-ignore-case t)
  10598. cdef
  10599. (prompt (concat prompt
  10600. (or (and (car org-refile-history)
  10601. (concat " (default " (car org-refile-history) ")"))
  10602. (and (assoc cbnex tbl) (setq cdef cbnex)
  10603. (concat " (default " cbnex ")"))) ": "))
  10604. pa answ parent-target child parent old-hist)
  10605. (setq old-hist org-refile-history)
  10606. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10607. nil 'org-refile-history (or cdef (car org-refile-history))))
  10608. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10609. (org-refile-check-position pa)
  10610. (if pa
  10611. (progn
  10612. (when (or (not org-refile-history)
  10613. (not (eq old-hist org-refile-history))
  10614. (not (equal (car pa) (car org-refile-history))))
  10615. (setq org-refile-history
  10616. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10617. org-refile-history
  10618. (cdr org-refile-history))))
  10619. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10620. (pop org-refile-history)))
  10621. pa)
  10622. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10623. (progn
  10624. (setq parent (match-string 1 answ)
  10625. child (match-string 2 answ))
  10626. (setq parent-target (or (assoc parent tbl)
  10627. (assoc (concat parent "/") tbl)))
  10628. (when (and parent-target
  10629. (or (eq new-nodes t)
  10630. (and (eq new-nodes 'confirm)
  10631. (y-or-n-p (format "Create new node \"%s\"? "
  10632. child)))))
  10633. (org-refile-new-child parent-target child)))
  10634. (user-error "Invalid target location")))))
  10635. (declare-function org-string-nw-p "org-macs" (s))
  10636. (defun org-refile-check-position (refile-pointer)
  10637. "Check if the refile pointer matches the headline to which it points."
  10638. (let* ((file (nth 1 refile-pointer))
  10639. (re (nth 2 refile-pointer))
  10640. (pos (nth 3 refile-pointer))
  10641. buffer)
  10642. (if (and (not (markerp pos)) (not file))
  10643. (user-error "Please save the buffer to a file before refiling")
  10644. (when (org-string-nw-p re)
  10645. (setq buffer (if (markerp pos)
  10646. (marker-buffer pos)
  10647. (or (find-buffer-visiting file)
  10648. (find-file-noselect file))))
  10649. (with-current-buffer buffer
  10650. (save-excursion
  10651. (save-restriction
  10652. (widen)
  10653. (goto-char pos)
  10654. (beginning-of-line 1)
  10655. (unless (org-looking-at-p re)
  10656. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10657. (defun org-refile-new-child (parent-target child)
  10658. "Use refile target PARENT-TARGET to add new CHILD below it."
  10659. (unless parent-target
  10660. (error "Cannot find parent for new node"))
  10661. (let ((file (nth 1 parent-target))
  10662. (pos (nth 3 parent-target))
  10663. level)
  10664. (with-current-buffer (or (find-buffer-visiting file)
  10665. (find-file-noselect file))
  10666. (save-excursion
  10667. (save-restriction
  10668. (widen)
  10669. (if pos
  10670. (goto-char pos)
  10671. (goto-char (point-max))
  10672. (if (not (bolp)) (newline)))
  10673. (when (looking-at org-outline-regexp)
  10674. (setq level (funcall outline-level))
  10675. (org-end-of-subtree t t))
  10676. (org-back-over-empty-lines)
  10677. (insert "\n" (make-string
  10678. (if pos (org-get-valid-level level 1) 1) ?*)
  10679. " " child "\n")
  10680. (beginning-of-line 0)
  10681. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10682. (defun org-olpath-completing-read (prompt collection &rest args)
  10683. "Read an outline path like a file name."
  10684. (let ((thetable collection)
  10685. (org-completion-use-ido nil) ; does not work with ido.
  10686. (org-completion-use-iswitchb nil)) ; or iswitchb
  10687. (apply
  10688. 'org-icompleting-read prompt
  10689. (lambda (string predicate &optional flag)
  10690. (let (rtn r f (l (length string)))
  10691. (cond
  10692. ((eq flag nil)
  10693. ;; try completion
  10694. (try-completion string thetable))
  10695. ((eq flag t)
  10696. ;; all-completions
  10697. (setq rtn (all-completions string thetable predicate))
  10698. (mapcar
  10699. (lambda (x)
  10700. (setq r (substring x l))
  10701. (if (string-match " ([^)]*)$" x)
  10702. (setq f (match-string 0 x))
  10703. (setq f ""))
  10704. (if (string-match "/" r)
  10705. (concat string (substring r 0 (match-end 0)) f)
  10706. x))
  10707. rtn))
  10708. ((eq flag 'lambda)
  10709. ;; exact match?
  10710. (assoc string thetable)))))
  10711. args)))
  10712. ;;;; Dynamic blocks
  10713. (defun org-find-dblock (name)
  10714. "Find the first dynamic block with name NAME in the buffer.
  10715. If not found, stay at current position and return nil."
  10716. (let ((case-fold-search t) pos)
  10717. (save-excursion
  10718. (goto-char (point-min))
  10719. (setq pos (and (re-search-forward
  10720. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10721. (match-beginning 0))))
  10722. (if pos (goto-char pos))
  10723. pos))
  10724. (defconst org-dblock-start-re
  10725. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10726. "Matches the start line of a dynamic block, with parameters.")
  10727. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10728. "Matches the end of a dynamic block.")
  10729. (defun org-create-dblock (plist)
  10730. "Create a dynamic block section, with parameters taken from PLIST.
  10731. PLIST must contain a :name entry which is used as name of the block."
  10732. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10733. (end-of-line 1)
  10734. (newline))
  10735. (let ((col (current-column))
  10736. (name (plist-get plist :name)))
  10737. (insert "#+BEGIN: " name)
  10738. (while plist
  10739. (if (eq (car plist) :name)
  10740. (setq plist (cddr plist))
  10741. (insert " " (prin1-to-string (pop plist)))))
  10742. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10743. (beginning-of-line -2)))
  10744. (defun org-prepare-dblock ()
  10745. "Prepare dynamic block for refresh.
  10746. This empties the block, puts the cursor at the insert position and returns
  10747. the property list including an extra property :name with the block name."
  10748. (unless (looking-at org-dblock-start-re)
  10749. (user-error "Not at a dynamic block"))
  10750. (let* ((begdel (1+ (match-end 0)))
  10751. (name (org-no-properties (match-string 1)))
  10752. (params (append (list :name name)
  10753. (read (concat "(" (match-string 3) ")")))))
  10754. (save-excursion
  10755. (beginning-of-line 1)
  10756. (skip-chars-forward " \t")
  10757. (setq params (plist-put params :indentation-column (current-column))))
  10758. (unless (re-search-forward org-dblock-end-re nil t)
  10759. (error "Dynamic block not terminated"))
  10760. (setq params
  10761. (append params
  10762. (list :content (buffer-substring
  10763. begdel (match-beginning 0)))))
  10764. (delete-region begdel (match-beginning 0))
  10765. (goto-char begdel)
  10766. (open-line 1)
  10767. params))
  10768. (defun org-map-dblocks (&optional command)
  10769. "Apply COMMAND to all dynamic blocks in the current buffer.
  10770. If COMMAND is not given, use `org-update-dblock'."
  10771. (let ((cmd (or command 'org-update-dblock)))
  10772. (save-excursion
  10773. (goto-char (point-min))
  10774. (while (re-search-forward org-dblock-start-re nil t)
  10775. (goto-char (match-beginning 0))
  10776. (save-excursion
  10777. (condition-case nil
  10778. (funcall cmd)
  10779. (error (message "Error during update of dynamic block"))))
  10780. (unless (re-search-forward org-dblock-end-re nil t)
  10781. (error "Dynamic block not terminated"))))))
  10782. (defun org-dblock-update (&optional arg)
  10783. "User command for updating dynamic blocks.
  10784. Update the dynamic block at point. With prefix ARG, update all dynamic
  10785. blocks in the buffer."
  10786. (interactive "P")
  10787. (if arg
  10788. (org-update-all-dblocks)
  10789. (or (looking-at org-dblock-start-re)
  10790. (org-beginning-of-dblock))
  10791. (org-update-dblock)))
  10792. (defun org-update-dblock ()
  10793. "Update the dynamic block at point.
  10794. This means to empty the block, parse for parameters and then call
  10795. the correct writing function."
  10796. (interactive)
  10797. (save-window-excursion
  10798. (let* ((pos (point))
  10799. (line (org-current-line))
  10800. (params (org-prepare-dblock))
  10801. (name (plist-get params :name))
  10802. (indent (plist-get params :indentation-column))
  10803. (cmd (intern (concat "org-dblock-write:" name))))
  10804. (message "Updating dynamic block `%s' at line %d..." name line)
  10805. (funcall cmd params)
  10806. (message "Updating dynamic block `%s' at line %d...done" name line)
  10807. (goto-char pos)
  10808. (when (and indent (> indent 0))
  10809. (setq indent (make-string indent ?\ ))
  10810. (save-excursion
  10811. (org-beginning-of-dblock)
  10812. (forward-line 1)
  10813. (while (not (looking-at org-dblock-end-re))
  10814. (insert indent)
  10815. (beginning-of-line 2))
  10816. (when (looking-at org-dblock-end-re)
  10817. (and (looking-at "[ \t]+")
  10818. (replace-match ""))
  10819. (insert indent)))))))
  10820. (defun org-beginning-of-dblock ()
  10821. "Find the beginning of the dynamic block at point.
  10822. Error if there is no such block at point."
  10823. (let ((pos (point))
  10824. beg)
  10825. (end-of-line 1)
  10826. (if (and (re-search-backward org-dblock-start-re nil t)
  10827. (setq beg (match-beginning 0))
  10828. (re-search-forward org-dblock-end-re nil t)
  10829. (> (match-end 0) pos))
  10830. (goto-char beg)
  10831. (goto-char pos)
  10832. (error "Not in a dynamic block"))))
  10833. (defun org-update-all-dblocks ()
  10834. "Update all dynamic blocks in the buffer.
  10835. This function can be used in a hook."
  10836. (interactive)
  10837. (when (derived-mode-p 'org-mode)
  10838. (org-map-dblocks 'org-update-dblock)))
  10839. ;;;; Completion
  10840. (declare-function org-export-backend-name "org-export" (cl-x))
  10841. (declare-function org-export-backend-options "org-export" (cl-x))
  10842. (defun org-get-export-keywords ()
  10843. "Return a list of all currently understood export keywords.
  10844. Export keywords include options, block names, attributes and
  10845. keywords relative to each registered export back-end."
  10846. (let (keywords)
  10847. (dolist (backend
  10848. (org-bound-and-true-p org-export--registered-backends)
  10849. (delq nil keywords))
  10850. ;; Back-end name (for keywords, like #+LATEX:)
  10851. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10852. (dolist (option-entry (org-export-backend-options backend))
  10853. ;; Back-end options.
  10854. (push (nth 1 option-entry) keywords)))))
  10855. (defconst org-options-keywords
  10856. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10857. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10858. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10859. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10860. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10861. (defcustom org-structure-template-alist
  10862. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10863. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10864. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10865. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10866. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10867. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10868. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10869. "<literal style=\"latex\">\n?\n</literal>")
  10870. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10871. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10872. "<literal style=\"html\">\n?\n</literal>")
  10873. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10874. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10875. ("A" "#+ASCII: " "")
  10876. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10877. ("I" "#+INCLUDE: %file ?"
  10878. "<include file=%file markup=\"?\">"))
  10879. "Structure completion elements.
  10880. This is a list of abbreviation keys and values. The value gets inserted
  10881. if you type `<' followed by the key and then press the completion key,
  10882. usually `M-TAB'. %file will be replaced by a file name after prompting
  10883. for the file using completion. The cursor will be placed at the position
  10884. of the `?` in the template.
  10885. There are two templates for each key, the first uses the original Org syntax,
  10886. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10887. the default when the /org-mtags.el/ module has been loaded. See also the
  10888. variable `org-mtags-prefer-muse-templates'."
  10889. :group 'org-completion
  10890. :type '(repeat
  10891. (list
  10892. (string :tag "Key")
  10893. (string :tag "Template")
  10894. (string :tag "Muse Template"))))
  10895. (defun org-try-structure-completion ()
  10896. "Try to complete a structure template before point.
  10897. This looks for strings like \"<e\" on an otherwise empty line and
  10898. expands them."
  10899. (let ((l (buffer-substring (point-at-bol) (point)))
  10900. a)
  10901. (when (and (looking-at "[ \t]*$")
  10902. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10903. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10904. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10905. (match-beginning 1)) a)
  10906. t)))
  10907. (defun org-complete-expand-structure-template (start cell)
  10908. "Expand a structure template."
  10909. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10910. (rpl (nth (if musep 2 1) cell))
  10911. (ind ""))
  10912. (delete-region start (point))
  10913. (when (string-match "\\`#\\+" rpl)
  10914. (cond
  10915. ((bolp))
  10916. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10917. (setq ind (buffer-substring (point-at-bol) (point))))
  10918. (t (newline))))
  10919. (setq start (point))
  10920. (if (string-match "%file" rpl)
  10921. (setq rpl (replace-match
  10922. (concat
  10923. "\""
  10924. (save-match-data
  10925. (abbreviate-file-name (read-file-name "Include file: ")))
  10926. "\"")
  10927. t t rpl)))
  10928. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10929. (concat "\n" ind)))
  10930. (insert rpl)
  10931. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10932. ;;;; TODO, DEADLINE, Comments
  10933. (defun org-toggle-comment ()
  10934. "Change the COMMENT state of an entry."
  10935. (interactive)
  10936. (save-excursion
  10937. (org-back-to-heading)
  10938. (let (case-fold-search)
  10939. (cond
  10940. ((looking-at (format org-heading-keyword-regexp-format
  10941. org-comment-string))
  10942. (goto-char (match-end 1))
  10943. (looking-at (concat " +" org-comment-string))
  10944. (replace-match "" t t)
  10945. (when (eolp) (insert " ")))
  10946. ((looking-at org-outline-regexp)
  10947. (goto-char (match-end 0))
  10948. (insert org-comment-string " "))))))
  10949. (defvar org-last-todo-state-is-todo nil
  10950. "This is non-nil when the last TODO state change led to a TODO state.
  10951. If the last change removed the TODO tag or switched to DONE, then
  10952. this is nil.")
  10953. (defvar org-setting-tags nil) ; dynamically skipped
  10954. (defvar org-todo-setup-filter-hook nil
  10955. "Hook for functions that pre-filter todo specs.
  10956. Each function takes a todo spec and returns either nil or the spec
  10957. transformed into canonical form." )
  10958. (defvar org-todo-get-default-hook nil
  10959. "Hook for functions that get a default item for todo.
  10960. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10961. nil or a string to be used for the todo mark." )
  10962. (defvar org-agenda-headline-snapshot-before-repeat)
  10963. (defun org-current-effective-time ()
  10964. "Return current time adjusted for `org-extend-today-until' variable."
  10965. (let* ((ct (org-current-time))
  10966. (dct (decode-time ct))
  10967. (ct1
  10968. (cond
  10969. (org-use-last-clock-out-time-as-effective-time
  10970. (or (org-clock-get-last-clock-out-time) ct))
  10971. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10972. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10973. (t ct))))
  10974. ct1))
  10975. (defun org-todo-yesterday (&optional arg)
  10976. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10977. (interactive "P")
  10978. (if (eq major-mode 'org-agenda-mode)
  10979. (apply 'org-agenda-todo-yesterday arg)
  10980. (let* ((hour (third (decode-time
  10981. (org-current-time))))
  10982. (org-extend-today-until (1+ hour)))
  10983. (org-todo arg))))
  10984. (defvar org-block-entry-blocking ""
  10985. "First entry preventing the TODO state change.")
  10986. (defun org-cancel-repeater ()
  10987. "Cancel a repeater by setting its numeric value to zero."
  10988. (interactive)
  10989. (save-excursion
  10990. (org-back-to-heading t)
  10991. (let ((bound1 (point))
  10992. (bound0 (save-excursion (outline-next-heading) (point))))
  10993. (when (re-search-forward
  10994. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10995. org-deadline-time-regexp "\\)\\|\\("
  10996. org-ts-regexp "\\)")
  10997. bound0 t)
  10998. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  10999. (replace-match "0" t nil nil 1))))))
  11000. (defun org-todo (&optional arg)
  11001. "Change the TODO state of an item.
  11002. The state of an item is given by a keyword at the start of the heading,
  11003. like
  11004. *** TODO Write paper
  11005. *** DONE Call mom
  11006. The different keywords are specified in the variable `org-todo-keywords'.
  11007. By default the available states are \"TODO\" and \"DONE\".
  11008. So for this example: when the item starts with TODO, it is changed to DONE.
  11009. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11010. DONE are present, add TODO at the beginning of the heading.
  11011. With \\[universal-argument] prefix arg, use completion to determine the new \
  11012. state.
  11013. With numeric prefix arg, switch to that state.
  11014. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11015. keywords (nextset).
  11016. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11017. With a numeric prefix arg of 0, inhibit note taking for the change.
  11018. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11019. When called through ELisp, arg is also interpreted in the following way:
  11020. 'none -> empty state
  11021. \"\"(empty string) -> switch to empty state
  11022. 'done -> switch to DONE
  11023. 'nextset -> switch to the next set of keywords
  11024. 'previousset -> switch to the previous set of keywords
  11025. \"WAITING\" -> switch to the specified keyword, but only if it
  11026. really is a member of `org-todo-keywords'."
  11027. (interactive "P")
  11028. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11029. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11030. 'region-start-level 'region))
  11031. org-loop-over-headlines-in-active-region)
  11032. (org-map-entries
  11033. `(org-todo ,arg)
  11034. org-loop-over-headlines-in-active-region
  11035. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11036. (if (equal arg '(16)) (setq arg 'nextset))
  11037. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11038. (let ((org-blocker-hook org-blocker-hook)
  11039. commentp
  11040. case-fold-search)
  11041. (when (equal arg '(64))
  11042. (setq arg nil org-blocker-hook nil))
  11043. (when (and org-blocker-hook
  11044. (or org-inhibit-blocking
  11045. (org-entry-get nil "NOBLOCKING")))
  11046. (setq org-blocker-hook nil))
  11047. (save-excursion
  11048. (catch 'exit
  11049. (org-back-to-heading t)
  11050. (when (looking-at (concat "^\\*+ " org-comment-string))
  11051. (org-toggle-comment)
  11052. (setq commentp t))
  11053. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11054. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11055. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11056. (let* ((match-data (match-data))
  11057. (startpos (point-at-bol))
  11058. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11059. (org-log-done org-log-done)
  11060. (org-log-repeat org-log-repeat)
  11061. (org-todo-log-states org-todo-log-states)
  11062. (org-inhibit-logging
  11063. (if (equal arg 0)
  11064. (progn (setq arg nil) 'note) org-inhibit-logging))
  11065. (this (match-string 1))
  11066. (hl-pos (match-beginning 0))
  11067. (head (org-get-todo-sequence-head this))
  11068. (ass (assoc head org-todo-kwd-alist))
  11069. (interpret (nth 1 ass))
  11070. (done-word (nth 3 ass))
  11071. (final-done-word (nth 4 ass))
  11072. (org-last-state (or this ""))
  11073. (completion-ignore-case t)
  11074. (member (member this org-todo-keywords-1))
  11075. (tail (cdr member))
  11076. (org-state (cond
  11077. ((and org-todo-key-trigger
  11078. (or (and (equal arg '(4))
  11079. (eq org-use-fast-todo-selection 'prefix))
  11080. (and (not arg) org-use-fast-todo-selection
  11081. (not (eq org-use-fast-todo-selection
  11082. 'prefix)))))
  11083. ;; Use fast selection
  11084. (org-fast-todo-selection))
  11085. ((and (equal arg '(4))
  11086. (or (not org-use-fast-todo-selection)
  11087. (not org-todo-key-trigger)))
  11088. ;; Read a state with completion
  11089. (org-icompleting-read
  11090. "State: " (mapcar 'list org-todo-keywords-1)
  11091. nil t))
  11092. ((eq arg 'right)
  11093. (if this
  11094. (if tail (car tail) nil)
  11095. (car org-todo-keywords-1)))
  11096. ((eq arg 'left)
  11097. (if (equal member org-todo-keywords-1)
  11098. nil
  11099. (if this
  11100. (nth (- (length org-todo-keywords-1)
  11101. (length tail) 2)
  11102. org-todo-keywords-1)
  11103. (org-last org-todo-keywords-1))))
  11104. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11105. (setq arg nil))) ; hack to fall back to cycling
  11106. (arg
  11107. ;; user or caller requests a specific state
  11108. (cond
  11109. ((equal arg "") nil)
  11110. ((eq arg 'none) nil)
  11111. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11112. ((eq arg 'nextset)
  11113. (or (car (cdr (member head org-todo-heads)))
  11114. (car org-todo-heads)))
  11115. ((eq arg 'previousset)
  11116. (let ((org-todo-heads (reverse org-todo-heads)))
  11117. (or (car (cdr (member head org-todo-heads)))
  11118. (car org-todo-heads))))
  11119. ((car (member arg org-todo-keywords-1)))
  11120. ((stringp arg)
  11121. (user-error "State `%s' not valid in this file" arg))
  11122. ((nth (1- (prefix-numeric-value arg))
  11123. org-todo-keywords-1))))
  11124. ((null member) (or head (car org-todo-keywords-1)))
  11125. ((equal this final-done-word) nil) ;; -> make empty
  11126. ((null tail) nil) ;; -> first entry
  11127. ((memq interpret '(type priority))
  11128. (if (eq this-command last-command)
  11129. (car tail)
  11130. (if (> (length tail) 0)
  11131. (or done-word (car org-done-keywords))
  11132. nil)))
  11133. (t
  11134. (car tail))))
  11135. (org-state (or
  11136. (run-hook-with-args-until-success
  11137. 'org-todo-get-default-hook org-state org-last-state)
  11138. org-state))
  11139. (next (if org-state (concat " " org-state " ") " "))
  11140. (change-plist (list :type 'todo-state-change :from this :to org-state
  11141. :position startpos))
  11142. dolog now-done-p)
  11143. (when org-blocker-hook
  11144. (setq org-last-todo-state-is-todo
  11145. (not (member this org-done-keywords)))
  11146. (unless (save-excursion
  11147. (save-match-data
  11148. (org-with-wide-buffer
  11149. (run-hook-with-args-until-failure
  11150. 'org-blocker-hook change-plist))))
  11151. (if (org-called-interactively-p 'interactive)
  11152. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11153. this org-state org-block-entry-blocking)
  11154. ;; fail silently
  11155. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11156. this org-state org-block-entry-blocking)
  11157. (throw 'exit nil))))
  11158. (store-match-data match-data)
  11159. (replace-match next t t)
  11160. (unless (pos-visible-in-window-p hl-pos)
  11161. (message "TODO state changed to %s" (org-trim next)))
  11162. (unless head
  11163. (setq head (org-get-todo-sequence-head org-state)
  11164. ass (assoc head org-todo-kwd-alist)
  11165. interpret (nth 1 ass)
  11166. done-word (nth 3 ass)
  11167. final-done-word (nth 4 ass)))
  11168. (when (memq arg '(nextset previousset))
  11169. (message "Keyword-Set %d/%d: %s"
  11170. (- (length org-todo-sets) -1
  11171. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11172. (length org-todo-sets)
  11173. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11174. (setq org-last-todo-state-is-todo
  11175. (not (member org-state org-done-keywords)))
  11176. (setq now-done-p (and (member org-state org-done-keywords)
  11177. (not (member this org-done-keywords))))
  11178. (and logging (org-local-logging logging))
  11179. (when (and (or org-todo-log-states org-log-done)
  11180. (not (eq org-inhibit-logging t))
  11181. (not (memq arg '(nextset previousset))))
  11182. ;; we need to look at recording a time and note
  11183. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11184. (nth 2 (assoc this org-todo-log-states))))
  11185. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11186. (setq dolog 'time))
  11187. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11188. (and org-state
  11189. (member org-state org-not-done-keywords)
  11190. (not (member this org-not-done-keywords))))
  11191. ;; This is now a todo state and was not one before
  11192. ;; If there was a CLOSED time stamp, get rid of it.
  11193. (org-add-planning-info nil nil 'closed))
  11194. (when (and now-done-p org-log-done)
  11195. ;; It is now done, and it was not done before
  11196. (org-add-planning-info 'closed (org-current-effective-time))
  11197. (if (and (not dolog) (eq 'note org-log-done))
  11198. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11199. (when (and org-state dolog)
  11200. ;; This is a non-nil state, and we need to log it
  11201. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11202. ;; Fixup tag positioning
  11203. (org-todo-trigger-tag-changes org-state)
  11204. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11205. (when org-provide-todo-statistics
  11206. (org-update-parent-todo-statistics))
  11207. (run-hooks 'org-after-todo-state-change-hook)
  11208. (if (and arg (not (member org-state org-done-keywords)))
  11209. (setq head (org-get-todo-sequence-head org-state)))
  11210. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11211. ;; Do we need to trigger a repeat?
  11212. (when now-done-p
  11213. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11214. ;; This is for the agenda, take a snapshot of the headline.
  11215. (save-match-data
  11216. (setq org-agenda-headline-snapshot-before-repeat
  11217. (org-get-heading))))
  11218. (org-auto-repeat-maybe org-state))
  11219. ;; Fixup cursor location if close to the keyword
  11220. (if (and (outline-on-heading-p)
  11221. (not (bolp))
  11222. (save-excursion (beginning-of-line 1)
  11223. (looking-at org-todo-line-regexp))
  11224. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11225. (progn
  11226. (goto-char (or (match-end 2) (match-end 1)))
  11227. (and (looking-at " ") (just-one-space))))
  11228. (when org-trigger-hook
  11229. (save-excursion
  11230. (run-hook-with-args 'org-trigger-hook change-plist)))
  11231. (when commentp (org-toggle-comment))))))))
  11232. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11233. "Block turning an entry into a TODO, using the hierarchy.
  11234. This checks whether the current task should be blocked from state
  11235. changes. Such blocking occurs when:
  11236. 1. The task has children which are not all in a completed state.
  11237. 2. A task has a parent with the property :ORDERED:, and there
  11238. are siblings prior to the current task with incomplete
  11239. status.
  11240. 3. The parent of the task is blocked because it has siblings that should
  11241. be done first, or is child of a block grandparent TODO entry."
  11242. (if (not org-enforce-todo-dependencies)
  11243. t ; if locally turned off don't block
  11244. (catch 'dont-block
  11245. ;; If this is not a todo state change, or if this entry is already DONE,
  11246. ;; do not block
  11247. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11248. (member (plist-get change-plist :from)
  11249. (cons 'done org-done-keywords))
  11250. (member (plist-get change-plist :to)
  11251. (cons 'todo org-not-done-keywords))
  11252. (not (plist-get change-plist :to)))
  11253. (throw 'dont-block t))
  11254. ;; If this task has children, and any are undone, it's blocked
  11255. (save-excursion
  11256. (org-back-to-heading t)
  11257. (let ((this-level (funcall outline-level)))
  11258. (outline-next-heading)
  11259. (let ((child-level (funcall outline-level)))
  11260. (while (and (not (eobp))
  11261. (> child-level this-level))
  11262. ;; this todo has children, check whether they are all
  11263. ;; completed
  11264. (if (and (not (org-entry-is-done-p))
  11265. (org-entry-is-todo-p))
  11266. (progn (setq org-block-entry-blocking (org-get-heading))
  11267. (throw 'dont-block nil)))
  11268. (outline-next-heading)
  11269. (setq child-level (funcall outline-level))))))
  11270. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11271. ;; any previous siblings are undone, it's blocked
  11272. (save-excursion
  11273. (org-back-to-heading t)
  11274. (let* ((pos (point))
  11275. (parent-pos (and (org-up-heading-safe) (point))))
  11276. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11277. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11278. (forward-line 1)
  11279. (re-search-forward org-not-done-heading-regexp pos t))
  11280. (setq org-block-entry-blocking (match-string 0))
  11281. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11282. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11283. (while t
  11284. (goto-char parent-pos)
  11285. (if (not (looking-at org-not-done-heading-regexp))
  11286. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11287. (setq pos (point))
  11288. (setq parent-pos (and (org-up-heading-safe) (point)))
  11289. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11290. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11291. (forward-line 1)
  11292. (re-search-forward org-not-done-heading-regexp pos t)
  11293. (setq org-block-entry-blocking (org-get-heading)))
  11294. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11295. (defcustom org-track-ordered-property-with-tag nil
  11296. "Should the ORDERED property also be shown as a tag?
  11297. The ORDERED property decides if an entry should require subtasks to be
  11298. completed in sequence. Since a property is not very visible, setting
  11299. this option means that toggling the ORDERED property with the command
  11300. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11301. not relevant for the behavior, but it makes things more visible.
  11302. Note that toggling the tag with tags commands will not change the property
  11303. and therefore not influence behavior!
  11304. This can be t, meaning the tag ORDERED should be used, It can also be a
  11305. string to select a different tag for this task."
  11306. :group 'org-todo
  11307. :type '(choice
  11308. (const :tag "No tracking" nil)
  11309. (const :tag "Track with ORDERED tag" t)
  11310. (string :tag "Use other tag")))
  11311. (defun org-toggle-ordered-property ()
  11312. "Toggle the ORDERED property of the current entry.
  11313. For better visibility, you can track the value of this property with a tag.
  11314. See variable `org-track-ordered-property-with-tag'."
  11315. (interactive)
  11316. (let* ((t1 org-track-ordered-property-with-tag)
  11317. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11318. (save-excursion
  11319. (org-back-to-heading)
  11320. (if (org-entry-get nil "ORDERED")
  11321. (progn
  11322. (org-delete-property "ORDERED")
  11323. (and tag (org-toggle-tag tag 'off))
  11324. (message "Subtasks can be completed in arbitrary order"))
  11325. (org-entry-put nil "ORDERED" "t")
  11326. (and tag (org-toggle-tag tag 'on))
  11327. (message "Subtasks must be completed in sequence")))))
  11328. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11329. (defun org-block-todo-from-checkboxes (change-plist)
  11330. "Block turning an entry into a TODO, using checkboxes.
  11331. This checks whether the current task should be blocked from state
  11332. changes because there are unchecked boxes in this entry."
  11333. (if (not org-enforce-todo-checkbox-dependencies)
  11334. t ; if locally turned off don't block
  11335. (catch 'dont-block
  11336. ;; If this is not a todo state change, or if this entry is already DONE,
  11337. ;; do not block
  11338. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11339. (member (plist-get change-plist :from)
  11340. (cons 'done org-done-keywords))
  11341. (member (plist-get change-plist :to)
  11342. (cons 'todo org-not-done-keywords))
  11343. (not (plist-get change-plist :to)))
  11344. (throw 'dont-block t))
  11345. ;; If this task has checkboxes that are not checked, it's blocked
  11346. (save-excursion
  11347. (org-back-to-heading t)
  11348. (let ((beg (point)) end)
  11349. (outline-next-heading)
  11350. (setq end (point))
  11351. (goto-char beg)
  11352. (if (org-list-search-forward
  11353. (concat (org-item-beginning-re)
  11354. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11355. "\\[[- ]\\]")
  11356. end t)
  11357. (progn
  11358. (if (boundp 'org-blocked-by-checkboxes)
  11359. (setq org-blocked-by-checkboxes t))
  11360. (throw 'dont-block nil)))))
  11361. t))) ; do not block
  11362. (defun org-entry-blocked-p ()
  11363. "Is the current entry blocked?"
  11364. (org-with-silent-modifications
  11365. (if (org-entry-get nil "NOBLOCKING")
  11366. nil ;; Never block this entry
  11367. (not (run-hook-with-args-until-failure
  11368. 'org-blocker-hook
  11369. (list :type 'todo-state-change
  11370. :position (point)
  11371. :from 'todo
  11372. :to 'done))))))
  11373. (defun org-update-statistics-cookies (all)
  11374. "Update the statistics cookie, either from TODO or from checkboxes.
  11375. This should be called with the cursor in a line with a statistics cookie."
  11376. (interactive "P")
  11377. (if all
  11378. (progn
  11379. (org-update-checkbox-count 'all)
  11380. (org-map-entries 'org-update-parent-todo-statistics))
  11381. (if (not (org-at-heading-p))
  11382. (org-update-checkbox-count)
  11383. (let ((pos (point-marker))
  11384. end l1 l2)
  11385. (ignore-errors (org-back-to-heading t))
  11386. (if (not (org-at-heading-p))
  11387. (org-update-checkbox-count)
  11388. (setq l1 (org-outline-level))
  11389. (setq end (save-excursion
  11390. (outline-next-heading)
  11391. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11392. (point)))
  11393. (if (and (save-excursion
  11394. (re-search-forward
  11395. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11396. (not (save-excursion (re-search-forward
  11397. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11398. (org-update-checkbox-count)
  11399. (if (and l2 (> l2 l1))
  11400. (progn
  11401. (goto-char end)
  11402. (org-update-parent-todo-statistics))
  11403. (goto-char pos)
  11404. (beginning-of-line 1)
  11405. (while (re-search-forward
  11406. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11407. (point-at-eol) t)
  11408. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11409. (goto-char pos)
  11410. (move-marker pos nil)))))
  11411. (defvar org-entry-property-inherited-from) ;; defined below
  11412. (defun org-update-parent-todo-statistics ()
  11413. "Update any statistics cookie in the parent of the current headline.
  11414. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11415. the entire subtree and this will travel up the hierarchy and update
  11416. statistics everywhere."
  11417. (let* ((prop (save-excursion (org-up-heading-safe)
  11418. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11419. (recursive (or (not org-hierarchical-todo-statistics)
  11420. (and prop (string-match "\\<recursive\\>" prop))))
  11421. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11422. 0))
  11423. (first t)
  11424. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11425. level ltoggle l1 new ndel
  11426. (cnt-all 0) (cnt-done 0) is-percent kwd
  11427. checkbox-beg ov ovs ove cookie-present)
  11428. (catch 'exit
  11429. (save-excursion
  11430. (beginning-of-line 1)
  11431. (setq ltoggle (funcall outline-level))
  11432. ;; Three situations are to consider:
  11433. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11434. ;; to the top-level ancestor on the headline;
  11435. ;; 2. If parent has "recursive" property, repeat up to the
  11436. ;; headline setting that property, taking inheritance into
  11437. ;; account;
  11438. ;; 3. Else, move up to direct parent and proceed only once.
  11439. (while (and (setq level (org-up-heading-safe))
  11440. (or recursive first)
  11441. (>= (point) lim))
  11442. (setq first nil cookie-present nil)
  11443. (unless (and level
  11444. (not (string-match
  11445. "\\<checkbox\\>"
  11446. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11447. "")))))
  11448. (throw 'exit nil))
  11449. (while (re-search-forward box-re (point-at-eol) t)
  11450. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11451. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11452. (save-match-data
  11453. (unless (outline-next-heading) (throw 'exit nil))
  11454. (while (and (looking-at org-complex-heading-regexp)
  11455. (> (setq l1 (length (match-string 1))) level))
  11456. (setq kwd (and (or recursive (= l1 ltoggle))
  11457. (match-string 2)))
  11458. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11459. (and (listp org-provide-todo-statistics)
  11460. (or (member kwd org-provide-todo-statistics)
  11461. (member kwd org-done-keywords))))
  11462. (setq cnt-all (1+ cnt-all))
  11463. (if (eq org-provide-todo-statistics t)
  11464. (and kwd (setq cnt-all (1+ cnt-all)))))
  11465. (and (member kwd org-done-keywords)
  11466. (setq cnt-done (1+ cnt-done)))
  11467. (outline-next-heading)))
  11468. (setq new
  11469. (if is-percent
  11470. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11471. (format "[%d/%d]" cnt-done cnt-all))
  11472. ndel (- (match-end 0) checkbox-beg))
  11473. ;; handle overlays when updating cookie from column view
  11474. (when (setq ov (car (overlays-at checkbox-beg)))
  11475. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11476. (delete-overlay ov))
  11477. (goto-char checkbox-beg)
  11478. (insert new)
  11479. (delete-region (point) (+ (point) ndel))
  11480. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11481. (when ov (move-overlay ov ovs ove)))
  11482. (when cookie-present
  11483. (run-hook-with-args 'org-after-todo-statistics-hook
  11484. cnt-done (- cnt-all cnt-done))))))
  11485. (run-hooks 'org-todo-statistics-hook)))
  11486. (defvar org-after-todo-statistics-hook nil
  11487. "Hook that is called after a TODO statistics cookie has been updated.
  11488. Each function is called with two arguments: the number of not-done entries
  11489. and the number of done entries.
  11490. For example, the following function, when added to this hook, will switch
  11491. an entry to DONE when all children are done, and back to TODO when new
  11492. entries are set to a TODO status. Note that this hook is only called
  11493. when there is a statistics cookie in the headline!
  11494. (defun org-summary-todo (n-done n-not-done)
  11495. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11496. (let (org-log-done org-log-states) ; turn off logging
  11497. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11498. ")
  11499. (defvar org-todo-statistics-hook nil
  11500. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11501. This hook runs even if there is no statistics cookie present, in which case
  11502. `org-after-todo-statistics-hook' would not run.")
  11503. (defun org-todo-trigger-tag-changes (state)
  11504. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11505. (let ((l org-todo-state-tags-triggers)
  11506. changes)
  11507. (when (or (not state) (equal state ""))
  11508. (setq changes (append changes (cdr (assoc "" l)))))
  11509. (when (and (stringp state) (> (length state) 0))
  11510. (setq changes (append changes (cdr (assoc state l)))))
  11511. (when (member state org-not-done-keywords)
  11512. (setq changes (append changes (cdr (assoc 'todo l)))))
  11513. (when (member state org-done-keywords)
  11514. (setq changes (append changes (cdr (assoc 'done l)))))
  11515. (dolist (c changes)
  11516. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11517. (defun org-local-logging (value)
  11518. "Get logging settings from a property VALUE."
  11519. (let* (words w a)
  11520. ;; directly set the variables, they are already local.
  11521. (setq org-log-done nil
  11522. org-log-repeat nil
  11523. org-todo-log-states nil)
  11524. (setq words (org-split-string value))
  11525. (while (setq w (pop words))
  11526. (cond
  11527. ((setq a (assoc w org-startup-options))
  11528. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11529. (set (nth 1 a) (nth 2 a))))
  11530. ((setq a (org-extract-log-state-settings w))
  11531. (and (member (car a) org-todo-keywords-1)
  11532. (push a org-todo-log-states)))))))
  11533. (defun org-get-todo-sequence-head (kwd)
  11534. "Return the head of the TODO sequence to which KWD belongs.
  11535. If KWD is not set, check if there is a text property remembering the
  11536. right sequence."
  11537. (let (p)
  11538. (cond
  11539. ((not kwd)
  11540. (or (get-text-property (point-at-bol) 'org-todo-head)
  11541. (progn
  11542. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11543. nil (point-at-eol)))
  11544. (get-text-property p 'org-todo-head))))
  11545. ((not (member kwd org-todo-keywords-1))
  11546. (car org-todo-keywords-1))
  11547. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11548. (defun org-fast-todo-selection ()
  11549. "Fast TODO keyword selection with single keys.
  11550. Returns the new TODO keyword, or nil if no state change should occur."
  11551. (let* ((fulltable org-todo-key-alist)
  11552. (done-keywords org-done-keywords) ;; needed for the faces.
  11553. (maxlen (apply 'max (mapcar
  11554. (lambda (x)
  11555. (if (stringp (car x)) (string-width (car x)) 0))
  11556. fulltable)))
  11557. (expert nil)
  11558. (fwidth (+ maxlen 3 1 3))
  11559. (ncol (/ (- (window-width) 4) fwidth))
  11560. tg cnt e c tbl
  11561. groups ingroup)
  11562. (save-excursion
  11563. (save-window-excursion
  11564. (if expert
  11565. (set-buffer (get-buffer-create " *Org todo*"))
  11566. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11567. (erase-buffer)
  11568. (org-set-local 'org-done-keywords done-keywords)
  11569. (setq tbl fulltable cnt 0)
  11570. (while (setq e (pop tbl))
  11571. (cond
  11572. ((equal e '(:startgroup))
  11573. (push '() groups) (setq ingroup t)
  11574. (when (not (= cnt 0))
  11575. (setq cnt 0)
  11576. (insert "\n"))
  11577. (insert "{ "))
  11578. ((equal e '(:endgroup))
  11579. (setq ingroup nil cnt 0)
  11580. (insert "}\n"))
  11581. ((equal e '(:newline))
  11582. (when (not (= cnt 0))
  11583. (setq cnt 0)
  11584. (insert "\n")
  11585. (setq e (car tbl))
  11586. (while (equal (car tbl) '(:newline))
  11587. (insert "\n")
  11588. (setq tbl (cdr tbl)))))
  11589. (t
  11590. (setq tg (car e) c (cdr e))
  11591. (if ingroup (push tg (car groups)))
  11592. (setq tg (org-add-props tg nil 'face
  11593. (org-get-todo-face tg)))
  11594. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11595. (insert "[" c "] " tg (make-string
  11596. (- fwidth 4 (length tg)) ?\ ))
  11597. (when (= (setq cnt (1+ cnt)) ncol)
  11598. (insert "\n")
  11599. (if ingroup (insert " "))
  11600. (setq cnt 0)))))
  11601. (insert "\n")
  11602. (goto-char (point-min))
  11603. (if (not expert) (org-fit-window-to-buffer))
  11604. (message "[a-z..]:Set [SPC]:clear")
  11605. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11606. (cond
  11607. ((or (= c ?\C-g)
  11608. (and (= c ?q) (not (rassoc c fulltable))))
  11609. (setq quit-flag t))
  11610. ((= c ?\ ) nil)
  11611. ((setq e (rassoc c fulltable) tg (car e))
  11612. tg)
  11613. (t (setq quit-flag t)))))))
  11614. (defun org-entry-is-todo-p ()
  11615. (member (org-get-todo-state) org-not-done-keywords))
  11616. (defun org-entry-is-done-p ()
  11617. (member (org-get-todo-state) org-done-keywords))
  11618. (defun org-get-todo-state ()
  11619. "Return the TODO keyword of the current subtree."
  11620. (save-excursion
  11621. (org-back-to-heading t)
  11622. (and (looking-at org-todo-line-regexp)
  11623. (match-end 2)
  11624. (match-string 2))))
  11625. (defun org-at-date-range-p (&optional inactive-ok)
  11626. "Is the cursor inside a date range?"
  11627. (interactive)
  11628. (save-excursion
  11629. (catch 'exit
  11630. (let ((pos (point)))
  11631. (skip-chars-backward "^[<\r\n")
  11632. (skip-chars-backward "<[")
  11633. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11634. (>= (match-end 0) pos)
  11635. (throw 'exit t))
  11636. (skip-chars-backward "^<[\r\n")
  11637. (skip-chars-backward "<[")
  11638. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11639. (>= (match-end 0) pos)
  11640. (throw 'exit t)))
  11641. nil)))
  11642. (defun org-get-repeat (&optional tagline)
  11643. "Check if there is a deadline/schedule with repeater in this entry."
  11644. (save-match-data
  11645. (save-excursion
  11646. (org-back-to-heading t)
  11647. (and (re-search-forward (if tagline
  11648. (concat tagline "\\s-*" org-repeat-re)
  11649. org-repeat-re)
  11650. (org-entry-end-position) t)
  11651. (match-string-no-properties 1)))))
  11652. (defvar org-last-changed-timestamp)
  11653. (defvar org-last-inserted-timestamp)
  11654. (defvar org-log-post-message)
  11655. (defvar org-log-note-purpose)
  11656. (defvar org-log-note-how)
  11657. (defvar org-log-note-extra)
  11658. (defun org-auto-repeat-maybe (done-word)
  11659. "Check if the current headline contains a repeated deadline/schedule.
  11660. If yes, set TODO state back to what it was and change the base date
  11661. of repeating deadline/scheduled time stamps to new date.
  11662. This function is run automatically after each state change to a DONE state."
  11663. ;; last-state is dynamically scoped into this function
  11664. (let* ((repeat (org-get-repeat))
  11665. (aa (assoc org-last-state org-todo-kwd-alist))
  11666. (interpret (nth 1 aa))
  11667. (head (nth 2 aa))
  11668. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11669. (msg "Entry repeats: ")
  11670. (org-log-done nil)
  11671. (org-todo-log-states nil)
  11672. re type n what ts time to-state)
  11673. (when (and repeat (not (zerop (string-to-number repeat))))
  11674. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11675. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11676. org-todo-repeat-to-state))
  11677. (unless (and to-state (member to-state org-todo-keywords-1))
  11678. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11679. (org-todo to-state)
  11680. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11681. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11682. (org-time-stamp-format t t))))
  11683. (when org-log-repeat
  11684. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11685. (memq 'org-add-log-note post-command-hook))
  11686. ;; OK, we are already setup for some record
  11687. (if (eq org-log-repeat 'note)
  11688. ;; make sure we take a note, not only a time stamp
  11689. (setq org-log-note-how 'note))
  11690. ;; Set up for taking a record
  11691. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11692. org-last-state
  11693. 'findpos org-log-repeat)))
  11694. (org-back-to-heading t)
  11695. (org-add-planning-info nil nil 'closed)
  11696. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11697. org-deadline-time-regexp "\\)\\|\\("
  11698. org-ts-regexp "\\)"))
  11699. (while (re-search-forward
  11700. re (save-excursion (outline-next-heading) (point)) t)
  11701. (setq type (if (match-end 1) org-scheduled-string
  11702. (if (match-end 3) org-deadline-string "Plain:"))
  11703. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11704. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11705. (setq n (string-to-number (match-string 2 ts))
  11706. what (match-string 3 ts))
  11707. (if (equal what "w") (setq n (* n 7) what "d"))
  11708. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11709. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11710. ;; Preparation, see if we need to modify the start date for the change
  11711. (when (match-end 1)
  11712. (setq time (save-match-data (org-time-string-to-time ts)))
  11713. (cond
  11714. ((equal (match-string 1 ts) ".")
  11715. ;; Shift starting date to today
  11716. (org-timestamp-change
  11717. (- (org-today) (time-to-days time))
  11718. 'day))
  11719. ((equal (match-string 1 ts) "+")
  11720. (let ((nshiftmax 10) (nshift 0))
  11721. (while (or (= nshift 0)
  11722. (<= (time-to-days time)
  11723. (time-to-days (current-time))))
  11724. (when (= (incf nshift) nshiftmax)
  11725. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11726. (error "Abort")))
  11727. (org-timestamp-change n (cdr (assoc what whata)))
  11728. (org-at-timestamp-p t)
  11729. (setq ts (match-string 1))
  11730. (setq time (save-match-data (org-time-string-to-time ts)))))
  11731. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11732. ;; rematch, so that we have everything in place for the real shift
  11733. (org-at-timestamp-p t)
  11734. (setq ts (match-string 1))
  11735. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11736. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11737. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11738. (setq org-log-post-message msg)
  11739. (message "%s" msg))))
  11740. (defun org-show-todo-tree (arg)
  11741. "Make a compact tree which shows all headlines marked with TODO.
  11742. The tree will show the lines where the regexp matches, and all higher
  11743. headlines above the match.
  11744. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11745. With a numeric prefix N, construct a sparse tree for the Nth element
  11746. of `org-todo-keywords-1'."
  11747. (interactive "P")
  11748. (let ((case-fold-search nil)
  11749. (kwd-re
  11750. (cond ((null arg) org-not-done-regexp)
  11751. ((equal arg '(4))
  11752. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11753. (mapcar 'list org-todo-keywords-1))))
  11754. (concat "\\("
  11755. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11756. "\\)\\>")))
  11757. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11758. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11759. org-todo-keywords-1)))
  11760. (t (user-error "Invalid prefix argument: %s" arg)))))
  11761. (message "%d TODO entries found"
  11762. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11763. (defun org-deadline (arg &optional time)
  11764. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11765. With one universal prefix argument, remove any deadline from the item.
  11766. With two universal prefix arguments, prompt for a warning delay.
  11767. With argument TIME, set the deadline at the corresponding date. TIME
  11768. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11769. (interactive "P")
  11770. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11771. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11772. 'region-start-level 'region))
  11773. org-loop-over-headlines-in-active-region)
  11774. (org-map-entries
  11775. `(org-deadline ',arg ,time)
  11776. org-loop-over-headlines-in-active-region
  11777. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11778. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11779. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11780. (repeater (and old-date
  11781. (string-match
  11782. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11783. old-date)
  11784. (match-string 1 old-date))))
  11785. (cond
  11786. ((equal arg '(4))
  11787. (when (and old-date org-log-redeadline)
  11788. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11789. org-log-redeadline))
  11790. (org-remove-timestamp-with-keyword org-deadline-string)
  11791. (message "Item no longer has a deadline."))
  11792. ((equal arg '(16))
  11793. (save-excursion
  11794. (org-back-to-heading t)
  11795. (if (re-search-forward
  11796. org-deadline-time-regexp
  11797. (save-excursion (outline-next-heading) (point)) t)
  11798. (let* ((rpl0 (match-string 1))
  11799. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11800. (replace-match
  11801. (concat org-deadline-string
  11802. " <" rpl
  11803. (format " -%dd"
  11804. (abs
  11805. (- (time-to-days
  11806. (save-match-data
  11807. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11808. (time-to-days old-date-time))))
  11809. ">") t t))
  11810. (user-error "No deadline information to update"))))
  11811. (t
  11812. (org-add-planning-info 'deadline time 'closed)
  11813. (when (and old-date org-log-redeadline
  11814. (not (equal old-date
  11815. (substring org-last-inserted-timestamp 1 -1))))
  11816. (org-add-log-setup 'redeadline nil old-date 'findpos
  11817. org-log-redeadline))
  11818. (when repeater
  11819. (save-excursion
  11820. (org-back-to-heading t)
  11821. (when (re-search-forward (concat org-deadline-string " "
  11822. org-last-inserted-timestamp)
  11823. (save-excursion
  11824. (outline-next-heading) (point)) t)
  11825. (goto-char (1- (match-end 0)))
  11826. (insert " " repeater)
  11827. (setq org-last-inserted-timestamp
  11828. (concat (substring org-last-inserted-timestamp 0 -1)
  11829. " " repeater
  11830. (substring org-last-inserted-timestamp -1))))))
  11831. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11832. (defun org-schedule (arg &optional time)
  11833. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11834. With one universal prefix argument, remove any scheduling date from the item.
  11835. With two universal prefix arguments, prompt for a delay cookie.
  11836. With argument TIME, scheduled at the corresponding date. TIME can
  11837. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11838. (interactive "P")
  11839. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11840. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11841. 'region-start-level 'region))
  11842. org-loop-over-headlines-in-active-region)
  11843. (org-map-entries
  11844. `(org-schedule ',arg ,time)
  11845. org-loop-over-headlines-in-active-region
  11846. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11847. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11848. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11849. (repeater (and old-date
  11850. (string-match
  11851. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11852. old-date)
  11853. (match-string 1 old-date))))
  11854. (cond
  11855. ((equal arg '(4))
  11856. (progn
  11857. (when (and old-date org-log-reschedule)
  11858. (org-add-log-setup 'delschedule nil old-date 'findpos
  11859. org-log-reschedule))
  11860. (org-remove-timestamp-with-keyword org-scheduled-string)
  11861. (message "Item is no longer scheduled.")))
  11862. ((equal arg '(16))
  11863. (save-excursion
  11864. (org-back-to-heading t)
  11865. (if (re-search-forward
  11866. org-scheduled-time-regexp
  11867. (save-excursion (outline-next-heading) (point)) t)
  11868. (let* ((rpl0 (match-string 1))
  11869. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11870. (replace-match
  11871. (concat org-scheduled-string
  11872. " <" rpl
  11873. (format " -%dd"
  11874. (abs
  11875. (- (time-to-days
  11876. (save-match-data
  11877. (org-read-date nil t nil "Delay until" old-date-time)))
  11878. (time-to-days old-date-time))))
  11879. ">") t t))
  11880. (user-error "No scheduled information to update"))))
  11881. (t
  11882. (org-add-planning-info 'scheduled time 'closed)
  11883. (when (and old-date org-log-reschedule
  11884. (not (equal old-date
  11885. (substring org-last-inserted-timestamp 1 -1))))
  11886. (org-add-log-setup 'reschedule nil old-date 'findpos
  11887. org-log-reschedule))
  11888. (when repeater
  11889. (save-excursion
  11890. (org-back-to-heading t)
  11891. (when (re-search-forward (concat org-scheduled-string " "
  11892. org-last-inserted-timestamp)
  11893. (save-excursion
  11894. (outline-next-heading) (point)) t)
  11895. (goto-char (1- (match-end 0)))
  11896. (insert " " repeater)
  11897. (setq org-last-inserted-timestamp
  11898. (concat (substring org-last-inserted-timestamp 0 -1)
  11899. " " repeater
  11900. (substring org-last-inserted-timestamp -1))))))
  11901. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11902. (defun org-get-scheduled-time (pom &optional inherit)
  11903. "Get the scheduled time as a time tuple, of a format suitable
  11904. for calling org-schedule with, or if there is no scheduling,
  11905. returns nil."
  11906. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11907. (when time
  11908. (apply 'encode-time (org-parse-time-string time)))))
  11909. (defun org-get-deadline-time (pom &optional inherit)
  11910. "Get the deadline as a time tuple, of a format suitable for
  11911. calling org-deadline with, or if there is no scheduling, returns
  11912. nil."
  11913. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11914. (when time
  11915. (apply 'encode-time (org-parse-time-string time)))))
  11916. (defun org-remove-timestamp-with-keyword (keyword)
  11917. "Remove all time stamps with KEYWORD in the current entry."
  11918. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11919. beg)
  11920. (save-excursion
  11921. (org-back-to-heading t)
  11922. (setq beg (point))
  11923. (outline-next-heading)
  11924. (while (re-search-backward re beg t)
  11925. (replace-match "")
  11926. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11927. (equal (char-before) ?\ ))
  11928. (backward-delete-char 1)
  11929. (if (string-match "^[ \t]*$" (buffer-substring
  11930. (point-at-bol) (point-at-eol)))
  11931. (delete-region (point-at-bol)
  11932. (min (point-max) (1+ (point-at-eol))))))))))
  11933. (defvar org-time-was-given) ; dynamically scoped parameter
  11934. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11935. (defun org-add-planning-info (what &optional time &rest remove)
  11936. "Insert new timestamp with keyword in the line directly after the headline.
  11937. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11938. If non is given, the user is prompted for a date.
  11939. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11940. be removed."
  11941. (interactive)
  11942. (let (org-time-was-given org-end-time-was-given ts
  11943. end default-time default-input)
  11944. (catch 'exit
  11945. (when (and (memq what '(scheduled deadline))
  11946. (or (not time)
  11947. (and (stringp time)
  11948. (string-match "^[-+]+[0-9]" time))))
  11949. ;; Try to get a default date/time from existing timestamp
  11950. (save-excursion
  11951. (org-back-to-heading t)
  11952. (setq end (save-excursion (outline-next-heading) (point)))
  11953. (when (re-search-forward (if (eq what 'scheduled)
  11954. org-scheduled-time-regexp
  11955. org-deadline-time-regexp)
  11956. end t)
  11957. (setq ts (match-string 1)
  11958. default-time
  11959. (apply 'encode-time (org-parse-time-string ts))
  11960. default-input (and ts (org-get-compact-tod ts))))))
  11961. (when what
  11962. (setq time
  11963. (if (stringp time)
  11964. ;; This is a string (relative or absolute), set proper date
  11965. (apply 'encode-time
  11966. (org-read-date-analyze
  11967. time default-time (decode-time default-time)))
  11968. ;; If necessary, get the time from the user
  11969. (or time (org-read-date nil 'to-time nil nil
  11970. default-time default-input)))))
  11971. (when (and org-insert-labeled-timestamps-at-point
  11972. (member what '(scheduled deadline)))
  11973. (insert
  11974. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11975. (org-insert-time-stamp time org-time-was-given
  11976. nil nil nil (list org-end-time-was-given))
  11977. (setq what nil))
  11978. (save-excursion
  11979. (save-restriction
  11980. (let (col list elt ts buffer-invisibility-spec)
  11981. (org-back-to-heading t)
  11982. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11983. (goto-char (match-end 1))
  11984. (setq col (current-column))
  11985. (goto-char (match-end 0))
  11986. (if (eobp) (insert "\n") (forward-char 1))
  11987. (when (and (not what)
  11988. (not (looking-at
  11989. (concat "[ \t]*"
  11990. org-keyword-time-not-clock-regexp))))
  11991. ;; Nothing to add, nothing to remove...... :-)
  11992. (throw 'exit nil))
  11993. (if (and (not (looking-at org-outline-regexp))
  11994. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11995. "[^\r\n]*"))
  11996. (not (equal (match-string 1) org-clock-string)))
  11997. (narrow-to-region (match-beginning 0) (match-end 0))
  11998. (insert-before-markers "\n")
  11999. (backward-char 1)
  12000. (narrow-to-region (point) (point))
  12001. (and org-adapt-indentation (org-indent-to-column col)))
  12002. ;; Check if we have to remove something.
  12003. (setq list (cons what remove))
  12004. (while list
  12005. (setq elt (pop list))
  12006. (when (or (and (eq elt 'scheduled)
  12007. (re-search-forward org-scheduled-time-regexp nil t))
  12008. (and (eq elt 'deadline)
  12009. (re-search-forward org-deadline-time-regexp nil t))
  12010. (and (eq elt 'closed)
  12011. (re-search-forward org-closed-time-regexp nil t)))
  12012. (replace-match "")
  12013. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12014. (and (looking-at "[ \t]+") (replace-match ""))
  12015. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12016. (when what
  12017. (insert
  12018. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12019. (cond ((eq what 'scheduled) org-scheduled-string)
  12020. ((eq what 'deadline) org-deadline-string)
  12021. ((eq what 'closed) org-closed-string))
  12022. " ")
  12023. (setq ts (org-insert-time-stamp
  12024. time
  12025. (or org-time-was-given
  12026. (and (eq what 'closed) org-log-done-with-time))
  12027. (eq what 'closed)
  12028. nil nil (list org-end-time-was-given)))
  12029. (insert
  12030. (if (not (or (bolp) (eq (char-before) ?\ )
  12031. (memq (char-after) '(32 10))
  12032. (eobp))) " " ""))
  12033. (end-of-line 1))
  12034. (goto-char (point-min))
  12035. (widen)
  12036. (if (and (looking-at "[ \t]*\n")
  12037. (equal (char-before) ?\n))
  12038. (delete-region (1- (point)) (point-at-eol)))
  12039. ts))))))
  12040. (defvar org-log-note-marker (make-marker))
  12041. (defvar org-log-note-purpose nil)
  12042. (defvar org-log-note-state nil)
  12043. (defvar org-log-note-previous-state nil)
  12044. (defvar org-log-note-how nil)
  12045. (defvar org-log-note-extra nil)
  12046. (defvar org-log-note-window-configuration nil)
  12047. (defvar org-log-note-return-to (make-marker))
  12048. (defvar org-log-note-effective-time nil
  12049. "Remembered current time so that dynamically scoped
  12050. `org-extend-today-until' affects tha timestamps in state change
  12051. log")
  12052. (defvar org-log-post-message nil
  12053. "Message to be displayed after a log note has been stored.
  12054. The auto-repeater uses this.")
  12055. (defun org-add-note ()
  12056. "Add a note to the current entry.
  12057. This is done in the same way as adding a state change note."
  12058. (interactive)
  12059. (org-add-log-setup 'note nil nil 'findpos nil))
  12060. (defvar org-property-end-re)
  12061. (defun org-add-log-setup (&optional purpose state prev-state
  12062. findpos how extra)
  12063. "Set up the post command hook to take a note.
  12064. If this is about to TODO state change, the new state is expected in STATE.
  12065. When FINDPOS is non-nil, find the correct position for the note in
  12066. the current entry. If not, assume that it can be inserted at point.
  12067. HOW is an indicator what kind of note should be created.
  12068. EXTRA is additional text that will be inserted into the notes buffer."
  12069. (let* ((org-log-into-drawer (org-log-into-drawer))
  12070. (drawer (cond ((stringp org-log-into-drawer)
  12071. org-log-into-drawer)
  12072. (org-log-into-drawer "LOGBOOK"))))
  12073. (save-restriction
  12074. (save-excursion
  12075. (when findpos
  12076. (org-back-to-heading t)
  12077. (narrow-to-region (point) (save-excursion
  12078. (outline-next-heading) (point)))
  12079. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12080. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12081. "[^\r\n]*\\)?"))
  12082. (goto-char (match-end 0))
  12083. (cond
  12084. (drawer
  12085. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12086. nil t)
  12087. (progn
  12088. (goto-char (match-end 0))
  12089. (or org-log-states-order-reversed
  12090. (and (re-search-forward org-property-end-re nil t)
  12091. (goto-char (1- (match-beginning 0))))))
  12092. (insert "\n:" drawer ":\n:END:")
  12093. (beginning-of-line 0)
  12094. (org-indent-line)
  12095. (beginning-of-line 2)
  12096. (org-indent-line)
  12097. (end-of-line 0)))
  12098. ((and org-log-state-notes-insert-after-drawers
  12099. (save-excursion
  12100. (forward-line) (looking-at org-drawer-regexp)))
  12101. (forward-line)
  12102. (while (looking-at org-drawer-regexp)
  12103. (goto-char (match-end 0))
  12104. (re-search-forward org-property-end-re (point-max) t)
  12105. (forward-line))
  12106. (forward-line -1)))
  12107. (unless org-log-states-order-reversed
  12108. (and (= (char-after) ?\n) (forward-char 1))
  12109. (org-skip-over-state-notes)
  12110. (skip-chars-backward " \t\n\r")))
  12111. (move-marker org-log-note-marker (point))
  12112. (setq org-log-note-purpose purpose
  12113. org-log-note-state state
  12114. org-log-note-previous-state prev-state
  12115. org-log-note-how how
  12116. org-log-note-extra extra
  12117. org-log-note-effective-time (org-current-effective-time))
  12118. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12119. (defun org-skip-over-state-notes ()
  12120. "Skip past the list of State notes in an entry."
  12121. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12122. (when (ignore-errors (goto-char (org-in-item-p)))
  12123. (let* ((struct (org-list-struct))
  12124. (prevs (org-list-prevs-alist struct)))
  12125. (while (looking-at "[ \t]*- State")
  12126. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12127. (org-list-get-item-end (point) struct)))))))
  12128. (defun org-add-log-note (&optional purpose)
  12129. "Pop up a window for taking a note, and add this note later at point."
  12130. (remove-hook 'post-command-hook 'org-add-log-note)
  12131. (setq org-log-note-window-configuration (current-window-configuration))
  12132. (delete-other-windows)
  12133. (move-marker org-log-note-return-to (point))
  12134. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12135. (goto-char org-log-note-marker)
  12136. (org-switch-to-buffer-other-window "*Org Note*")
  12137. (erase-buffer)
  12138. (if (memq org-log-note-how '(time state))
  12139. (let (current-prefix-arg) (org-store-log-note))
  12140. (let ((org-inhibit-startup t)) (org-mode))
  12141. (insert (format "# Insert note for %s.
  12142. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12143. (cond
  12144. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12145. ((eq org-log-note-purpose 'done) "closed todo item")
  12146. ((eq org-log-note-purpose 'state)
  12147. (format "state change from \"%s\" to \"%s\""
  12148. (or org-log-note-previous-state "")
  12149. (or org-log-note-state "")))
  12150. ((eq org-log-note-purpose 'reschedule)
  12151. "rescheduling")
  12152. ((eq org-log-note-purpose 'delschedule)
  12153. "no longer scheduled")
  12154. ((eq org-log-note-purpose 'redeadline)
  12155. "changing deadline")
  12156. ((eq org-log-note-purpose 'deldeadline)
  12157. "removing deadline")
  12158. ((eq org-log-note-purpose 'refile)
  12159. "refiling")
  12160. ((eq org-log-note-purpose 'note)
  12161. "this entry")
  12162. (t (error "This should not happen")))))
  12163. (if org-log-note-extra (insert org-log-note-extra))
  12164. (org-set-local 'org-finish-function 'org-store-log-note)
  12165. (run-hooks 'org-log-buffer-setup-hook)))
  12166. (defvar org-note-abort nil) ; dynamically scoped
  12167. (defun org-store-log-note ()
  12168. "Finish taking a log note, and insert it to where it belongs."
  12169. (let ((txt (buffer-string)))
  12170. (kill-buffer (current-buffer))
  12171. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12172. lines ind bul)
  12173. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12174. (setq txt (replace-match "" t t txt)))
  12175. (if (string-match "\\s-+\\'" txt)
  12176. (setq txt (replace-match "" t t txt)))
  12177. (setq lines (org-split-string txt "\n"))
  12178. (when (and note (string-match "\\S-" note))
  12179. (setq note
  12180. (org-replace-escapes
  12181. note
  12182. (list (cons "%u" (user-login-name))
  12183. (cons "%U" user-full-name)
  12184. (cons "%t" (format-time-string
  12185. (org-time-stamp-format 'long 'inactive)
  12186. org-log-note-effective-time))
  12187. (cons "%T" (format-time-string
  12188. (org-time-stamp-format 'long nil)
  12189. org-log-note-effective-time))
  12190. (cons "%d" (format-time-string
  12191. (org-time-stamp-format nil 'inactive)
  12192. org-log-note-effective-time))
  12193. (cons "%D" (format-time-string
  12194. (org-time-stamp-format nil nil)
  12195. org-log-note-effective-time))
  12196. (cons "%s" (if org-log-note-state
  12197. (concat "\"" org-log-note-state "\"")
  12198. ""))
  12199. (cons "%S" (if org-log-note-previous-state
  12200. (concat "\"" org-log-note-previous-state "\"")
  12201. "\"\"")))))
  12202. (if lines (setq note (concat note " \\\\")))
  12203. (push note lines))
  12204. (when (or current-prefix-arg org-note-abort)
  12205. (when org-log-into-drawer
  12206. (org-remove-empty-drawer-at org-log-note-marker))
  12207. (setq lines nil))
  12208. (when lines
  12209. (with-current-buffer (marker-buffer org-log-note-marker)
  12210. (save-excursion
  12211. (goto-char org-log-note-marker)
  12212. (move-marker org-log-note-marker nil)
  12213. (end-of-line 1)
  12214. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12215. (setq ind (save-excursion
  12216. (if (ignore-errors (goto-char (org-in-item-p)))
  12217. (let ((struct (org-list-struct)))
  12218. (org-list-get-ind
  12219. (org-list-get-top-point struct) struct))
  12220. (skip-chars-backward " \r\t\n")
  12221. (cond
  12222. ((and (org-at-heading-p)
  12223. org-adapt-indentation)
  12224. (1+ (org-current-level)))
  12225. ((org-at-heading-p) 0)
  12226. (t (org-get-indentation))))))
  12227. (setq bul (org-list-bullet-string "-"))
  12228. (org-indent-line-to ind)
  12229. (insert bul (pop lines))
  12230. (let ((ind-body (+ (length bul) ind)))
  12231. (while lines
  12232. (insert "\n")
  12233. (org-indent-line-to ind-body)
  12234. (insert (pop lines))))
  12235. (message "Note stored")
  12236. (org-back-to-heading t)
  12237. (org-cycle-hide-drawers 'children))
  12238. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12239. ;; from within `org-add-log-note' because `buffer-undo-list'
  12240. ;; is then modified outside of `org-with-remote-undo'.
  12241. (when (eq this-command 'org-agenda-todo)
  12242. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12243. ;; Don't add undo information when called from `org-agenda-todo'
  12244. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12245. (set-window-configuration org-log-note-window-configuration)
  12246. (with-current-buffer (marker-buffer org-log-note-return-to)
  12247. (goto-char org-log-note-return-to))
  12248. (move-marker org-log-note-return-to nil)
  12249. (and org-log-post-message (message "%s" org-log-post-message))))
  12250. (defun org-remove-empty-drawer-at (pos)
  12251. "Remove an empty drawer at position POS.
  12252. POS may also be a marker."
  12253. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12254. (org-with-wide-buffer
  12255. (goto-char pos)
  12256. (let ((drawer (org-element-at-point)))
  12257. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12258. (not (org-element-property :contents-begin drawer)))
  12259. (delete-region (org-element-property :begin drawer)
  12260. (progn (goto-char (org-element-property :end drawer))
  12261. (skip-chars-backward " \r\t\n")
  12262. (forward-line)
  12263. (point))))))))
  12264. (defvar org-ts-type nil)
  12265. (defun org-sparse-tree (&optional arg type)
  12266. "Create a sparse tree, prompt for the details.
  12267. This command can create sparse trees. You first need to select the type
  12268. of match used to create the tree:
  12269. t Show all TODO entries.
  12270. T Show entries with a specific TODO keyword.
  12271. m Show entries selected by a tags/property match.
  12272. p Enter a property name and its value (both with completion on existing
  12273. names/values) and show entries with that property.
  12274. r Show entries matching a regular expression (`/' can be used as well).
  12275. b Show deadlines and scheduled items before a date.
  12276. a Show deadlines and scheduled items after a date.
  12277. d Show deadlines due within `org-deadline-warning-days'.
  12278. D Show deadlines and scheduled items between a date range."
  12279. (interactive "P")
  12280. (let (ans kwd value ts-type)
  12281. (setq type (or type org-sparse-tree-default-date-type))
  12282. (setq org-ts-type type)
  12283. (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"
  12284. (cond ((eq type 'all) "all timestamps")
  12285. ((eq type 'scheduled) "only scheduled")
  12286. ((eq type 'deadline) "only deadline")
  12287. ((eq type 'active) "only active timestamps")
  12288. ((eq type 'inactive) "only inactive timestamps")
  12289. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12290. ((eq type 'closed) "with a closed time-stamp")
  12291. (t "scheduled/deadline")))
  12292. (setq ans (read-char-exclusive))
  12293. (cond
  12294. ((equal ans ?c)
  12295. (org-sparse-tree
  12296. arg (cadr (member type '(scheduled-or-deadline
  12297. all scheduled deadline active inactive closed)))))
  12298. ((equal ans ?d)
  12299. (call-interactively 'org-check-deadlines))
  12300. ((equal ans ?b)
  12301. (call-interactively 'org-check-before-date))
  12302. ((equal ans ?a)
  12303. (call-interactively 'org-check-after-date))
  12304. ((equal ans ?D)
  12305. (call-interactively 'org-check-dates-range))
  12306. ((equal ans ?t)
  12307. (call-interactively 'org-show-todo-tree))
  12308. ((equal ans ?T)
  12309. (org-show-todo-tree '(4)))
  12310. ((member ans '(?T ?m))
  12311. (call-interactively 'org-match-sparse-tree))
  12312. ((member ans '(?p ?P))
  12313. (setq kwd (org-icompleting-read "Property: "
  12314. (mapcar 'list (org-buffer-property-keys))))
  12315. (setq value (org-icompleting-read "Value: "
  12316. (mapcar 'list (org-property-values kwd))))
  12317. (unless (string-match "\\`{.*}\\'" value)
  12318. (setq value (concat "\"" value "\"")))
  12319. (org-match-sparse-tree arg (concat kwd "=" value)))
  12320. ((member ans '(?r ?R ?/))
  12321. (call-interactively 'org-occur))
  12322. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12323. (defvar org-occur-highlights nil
  12324. "List of overlays used for occur matches.")
  12325. (make-variable-buffer-local 'org-occur-highlights)
  12326. (defvar org-occur-parameters nil
  12327. "Parameters of the active org-occur calls.
  12328. This is a list, each call to org-occur pushes as cons cell,
  12329. containing the regular expression and the callback, onto the list.
  12330. The list can contain several entries if `org-occur' has been called
  12331. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12332. will only contain one set of parameters. When the highlights are
  12333. removed (for example with `C-c C-c', or with the next edit (depending
  12334. on `org-remove-highlights-with-change'), this variable is emptied
  12335. as well.")
  12336. (make-variable-buffer-local 'org-occur-parameters)
  12337. (defun org-occur (regexp &optional keep-previous callback)
  12338. "Make a compact tree which shows all matches of REGEXP.
  12339. The tree will show the lines where the regexp matches, and all higher
  12340. headlines above the match. It will also show the heading after the match,
  12341. to make sure editing the matching entry is easy.
  12342. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12343. call to `org-occur' will be kept, to allow stacking of calls to this
  12344. command.
  12345. If CALLBACK is non-nil, it is a function which is called to confirm
  12346. that the match should indeed be shown."
  12347. (interactive "sRegexp: \nP")
  12348. (when (equal regexp "")
  12349. (user-error "Regexp cannot be empty"))
  12350. (unless keep-previous
  12351. (org-remove-occur-highlights nil nil t))
  12352. (push (cons regexp callback) org-occur-parameters)
  12353. (let ((cnt 0))
  12354. (save-excursion
  12355. (goto-char (point-min))
  12356. (if (or (not keep-previous) ; do not want to keep
  12357. (not org-occur-highlights)) ; no previous matches
  12358. ;; hide everything
  12359. (org-overview))
  12360. (while (re-search-forward regexp nil t)
  12361. (when (or (not callback)
  12362. (save-match-data (funcall callback)))
  12363. (setq cnt (1+ cnt))
  12364. (when org-highlight-sparse-tree-matches
  12365. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12366. (org-show-context 'occur-tree))))
  12367. (when org-remove-highlights-with-change
  12368. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12369. nil 'local))
  12370. (unless org-sparse-tree-open-archived-trees
  12371. (org-hide-archived-subtrees (point-min) (point-max)))
  12372. (run-hooks 'org-occur-hook)
  12373. (if (org-called-interactively-p 'interactive)
  12374. (message "%d match(es) for regexp %s" cnt regexp))
  12375. cnt))
  12376. (defun org-occur-next-match (&optional n reset)
  12377. "Function for `next-error-function' to find sparse tree matches.
  12378. N is the number of matches to move, when negative move backwards.
  12379. RESET is entirely ignored - this function always goes back to the
  12380. starting point when no match is found."
  12381. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12382. (search-func (if (< n 0)
  12383. 'previous-single-char-property-change
  12384. 'next-single-char-property-change))
  12385. (n (abs n))
  12386. (pos (point))
  12387. p1)
  12388. (catch 'exit
  12389. (while (setq p1 (funcall search-func (point) 'org-type))
  12390. (when (equal p1 limit)
  12391. (goto-char pos)
  12392. (error "No more matches"))
  12393. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12394. (setq n (1- n))
  12395. (when (= n 0)
  12396. (goto-char p1)
  12397. (throw 'exit (point))))
  12398. (goto-char p1))
  12399. (goto-char p1)
  12400. (error "No more matches"))))
  12401. (defun org-show-context (&optional key)
  12402. "Make sure point and context are visible.
  12403. How much context is shown depends upon the variables
  12404. `org-show-hierarchy-above', `org-show-following-heading',
  12405. `org-show-entry-below' and `org-show-siblings'."
  12406. (let ((heading-p (org-at-heading-p t))
  12407. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12408. (following-p (org-get-alist-option org-show-following-heading key))
  12409. (entry-p (org-get-alist-option org-show-entry-below key))
  12410. (siblings-p (org-get-alist-option org-show-siblings key)))
  12411. ;; Show heading or entry text
  12412. (if (and heading-p (not entry-p))
  12413. (org-flag-heading nil) ; only show the heading
  12414. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12415. (org-show-hidden-entry))) ; show entire entry
  12416. (when following-p
  12417. ;; Show next sibling, or heading below text
  12418. (save-excursion
  12419. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12420. (org-flag-heading nil))))
  12421. (when siblings-p (org-show-siblings))
  12422. (when hierarchy-p
  12423. ;; show all higher headings, possibly with siblings
  12424. (save-excursion
  12425. (while (and (condition-case nil
  12426. (progn (org-up-heading-all 1) t)
  12427. (error nil))
  12428. (not (bobp)))
  12429. (org-flag-heading nil)
  12430. (when siblings-p (org-show-siblings)))))
  12431. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12432. (defvar org-reveal-start-hook nil
  12433. "Hook run before revealing a location.")
  12434. (defun org-reveal (&optional siblings)
  12435. "Show current entry, hierarchy above it, and the following headline.
  12436. This can be used to show a consistent set of context around locations
  12437. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12438. not t for the search context.
  12439. With optional argument SIBLINGS, on each level of the hierarchy all
  12440. siblings are shown. This repairs the tree structure to what it would
  12441. look like when opened with hierarchical calls to `org-cycle'.
  12442. With double optional argument \\[universal-argument] \\[universal-argument], \
  12443. go to the parent and show the
  12444. entire tree."
  12445. (interactive "P")
  12446. (run-hooks 'org-reveal-start-hook)
  12447. (let ((org-show-hierarchy-above t)
  12448. (org-show-following-heading t)
  12449. (org-show-siblings (if siblings t org-show-siblings)))
  12450. (org-show-context nil))
  12451. (when (equal siblings '(16))
  12452. (save-excursion
  12453. (when (org-up-heading-safe)
  12454. (org-show-subtree)
  12455. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12456. (defun org-highlight-new-match (beg end)
  12457. "Highlight from BEG to END and mark the highlight is an occur headline."
  12458. (let ((ov (make-overlay beg end)))
  12459. (overlay-put ov 'face 'secondary-selection)
  12460. (overlay-put ov 'org-type 'org-occur)
  12461. (push ov org-occur-highlights)))
  12462. (defun org-remove-occur-highlights (&optional beg end noremove)
  12463. "Remove the occur highlights from the buffer.
  12464. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12465. from the `before-change-functions' in the current buffer."
  12466. (interactive)
  12467. (unless org-inhibit-highlight-removal
  12468. (mapc 'delete-overlay org-occur-highlights)
  12469. (setq org-occur-highlights nil)
  12470. (setq org-occur-parameters nil)
  12471. (unless noremove
  12472. (remove-hook 'before-change-functions
  12473. 'org-remove-occur-highlights 'local))))
  12474. ;;;; Priorities
  12475. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12476. "Regular expression matching the priority indicator.")
  12477. (defvar org-remove-priority-next-time nil)
  12478. (defun org-priority-up ()
  12479. "Increase the priority of the current item."
  12480. (interactive)
  12481. (org-priority 'up))
  12482. (defun org-priority-down ()
  12483. "Decrease the priority of the current item."
  12484. (interactive)
  12485. (org-priority 'down))
  12486. (defun org-priority (&optional action show)
  12487. "Change the priority of an item.
  12488. ACTION can be `set', `up', `down', or a character."
  12489. (interactive "P")
  12490. (if (equal action '(4))
  12491. (org-show-priority)
  12492. (unless org-enable-priority-commands
  12493. (user-error "Priority commands are disabled"))
  12494. (setq action (or action 'set))
  12495. (let (current new news have remove)
  12496. (save-excursion
  12497. (org-back-to-heading t)
  12498. (if (looking-at org-priority-regexp)
  12499. (setq current (string-to-char (match-string 2))
  12500. have t))
  12501. (cond
  12502. ((eq action 'remove)
  12503. (setq remove t new ?\ ))
  12504. ((or (eq action 'set)
  12505. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12506. (if (not (eq action 'set))
  12507. (setq new action)
  12508. (message "Priority %c-%c, SPC to remove: "
  12509. org-highest-priority org-lowest-priority)
  12510. (save-match-data
  12511. (setq new (read-char-exclusive))))
  12512. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12513. (= (upcase org-lowest-priority) org-lowest-priority))
  12514. (setq new (upcase new)))
  12515. (cond ((equal new ?\ ) (setq remove t))
  12516. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12517. (user-error "Priority must be between `%c' and `%c'"
  12518. org-highest-priority org-lowest-priority))))
  12519. ((eq action 'up)
  12520. (setq new (if have
  12521. (1- current) ; normal cycling
  12522. ;; last priority was empty
  12523. (if (eq last-command this-command)
  12524. org-lowest-priority ; wrap around empty to lowest
  12525. ;; default
  12526. (if org-priority-start-cycle-with-default
  12527. org-default-priority
  12528. (1- org-default-priority))))))
  12529. ((eq action 'down)
  12530. (setq new (if have
  12531. (1+ current) ; normal cycling
  12532. ;; last priority was empty
  12533. (if (eq last-command this-command)
  12534. org-highest-priority ; wrap around empty to highest
  12535. ;; default
  12536. (if org-priority-start-cycle-with-default
  12537. org-default-priority
  12538. (1+ org-default-priority))))))
  12539. (t (user-error "Invalid action")))
  12540. (if (or (< (upcase new) org-highest-priority)
  12541. (> (upcase new) org-lowest-priority))
  12542. (if (and (memq action '(up down))
  12543. (not have) (not (eq last-command this-command)))
  12544. ;; `new' is from default priority
  12545. (error
  12546. "The default can not be set, see `org-default-priority' why")
  12547. ;; normal cycling: `new' is beyond highest/lowest priority
  12548. ;; and is wrapped around to the empty priority
  12549. (setq remove t)))
  12550. (setq news (format "%c" new))
  12551. (if have
  12552. (if remove
  12553. (replace-match "" t t nil 1)
  12554. (replace-match news t t nil 2))
  12555. (if remove
  12556. (user-error "No priority cookie found in line")
  12557. (let ((case-fold-search nil))
  12558. (looking-at org-todo-line-regexp))
  12559. (if (match-end 2)
  12560. (progn
  12561. (goto-char (match-end 2))
  12562. (insert " [#" news "]"))
  12563. (goto-char (match-beginning 3))
  12564. (insert "[#" news "] "))))
  12565. (org-preserve-lc (org-set-tags nil 'align)))
  12566. (if remove
  12567. (message "Priority removed")
  12568. (message "Priority of current item set to %s" news)))))
  12569. (defun org-show-priority ()
  12570. "Show the priority of the current item.
  12571. This priority is composed of the main priority given with the [#A] cookies,
  12572. and by additional input from the age of a schedules or deadline entry."
  12573. (interactive)
  12574. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12575. (org-get-at-bol 'priority)
  12576. (save-excursion
  12577. (save-match-data
  12578. (beginning-of-line)
  12579. (and (looking-at org-heading-regexp)
  12580. (org-get-priority (match-string 0))))))))
  12581. (message "Priority is %d" (if pri pri -1000))))
  12582. (defun org-get-priority (s)
  12583. "Find priority cookie and return priority."
  12584. (save-match-data
  12585. (if (functionp org-get-priority-function)
  12586. (funcall org-get-priority-function)
  12587. (if (not (string-match org-priority-regexp s))
  12588. (* 1000 (- org-lowest-priority org-default-priority))
  12589. (* 1000 (- org-lowest-priority
  12590. (string-to-char (match-string 2 s))))))))
  12591. ;;;; Tags
  12592. (defvar org-agenda-archives-mode)
  12593. (defvar org-map-continue-from nil
  12594. "Position from where mapping should continue.
  12595. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12596. (defvar org-scanner-tags nil
  12597. "The current tag list while the tags scanner is running.")
  12598. (defvar org-trust-scanner-tags nil
  12599. "Should `org-get-tags-at' use the tags for the scanner.
  12600. This is for internal dynamical scoping only.
  12601. When this is non-nil, the function `org-get-tags-at' will return the value
  12602. of `org-scanner-tags' instead of building the list by itself. This
  12603. can lead to large speed-ups when the tags scanner is used in a file with
  12604. many entries, and when the list of tags is retrieved, for example to
  12605. obtain a list of properties. Building the tags list for each entry in such
  12606. a file becomes an N^2 operation - but with this variable set, it scales
  12607. as N.")
  12608. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12609. "Sca headline tags with inheritance and produce output ACTION.
  12610. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12611. or `agenda' to produce an entry list for an agenda view. It can also be
  12612. a Lisp form or a function that should be called at each matched headline, in
  12613. this case the return value is a list of all return values from these calls.
  12614. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12615. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12616. only lines with a not-done TODO keyword are included in the output.
  12617. This should be the same variable that was scoped into
  12618. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12619. START-LEVEL can be a string with asterisks, reducing the scope to
  12620. headlines matching this string."
  12621. (require 'org-agenda)
  12622. (let* ((re (concat "^"
  12623. (if start-level
  12624. ;; Get the correct level to match
  12625. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12626. org-outline-regexp)
  12627. " *\\(\\<\\("
  12628. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12629. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12630. (props (list 'face 'default
  12631. 'done-face 'org-agenda-done
  12632. 'undone-face 'default
  12633. 'mouse-face 'highlight
  12634. 'org-not-done-regexp org-not-done-regexp
  12635. 'org-todo-regexp org-todo-regexp
  12636. 'org-complex-heading-regexp org-complex-heading-regexp
  12637. 'help-echo
  12638. (format "mouse-2 or RET jump to org file %s"
  12639. (abbreviate-file-name
  12640. (or (buffer-file-name (buffer-base-buffer))
  12641. (buffer-name (buffer-base-buffer)))))))
  12642. (org-map-continue-from nil)
  12643. lspos tags tags-list
  12644. (tags-alist (list (cons 0 org-file-tags)))
  12645. (llast 0) rtn rtn1 level category i txt
  12646. todo marker entry priority)
  12647. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12648. (setq action (list 'lambda nil action)))
  12649. (save-excursion
  12650. (goto-char (point-min))
  12651. (when (eq action 'sparse-tree)
  12652. (org-overview)
  12653. (org-remove-occur-highlights))
  12654. (while (let (case-fold-search)
  12655. (re-search-forward re nil t))
  12656. (setq org-map-continue-from nil)
  12657. (catch :skip
  12658. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12659. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12660. (goto-char (setq lspos (match-beginning 0)))
  12661. (setq level (org-reduced-level (org-outline-level))
  12662. category (org-get-category))
  12663. (setq i llast llast level)
  12664. ;; remove tag lists from same and sublevels
  12665. (while (>= i level)
  12666. (when (setq entry (assoc i tags-alist))
  12667. (setq tags-alist (delete entry tags-alist)))
  12668. (setq i (1- i)))
  12669. ;; add the next tags
  12670. (when tags
  12671. (setq tags (org-split-string tags ":")
  12672. tags-alist
  12673. (cons (cons level tags) tags-alist)))
  12674. ;; compile tags for current headline
  12675. (setq tags-list
  12676. (if org-use-tag-inheritance
  12677. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12678. tags)
  12679. org-scanner-tags tags-list)
  12680. (when org-use-tag-inheritance
  12681. (setcdr (car tags-alist)
  12682. (mapcar (lambda (x)
  12683. (setq x (copy-sequence x))
  12684. (org-add-prop-inherited x))
  12685. (cdar tags-alist))))
  12686. (when (and tags org-use-tag-inheritance
  12687. (or (not (eq t org-use-tag-inheritance))
  12688. org-tags-exclude-from-inheritance))
  12689. ;; selective inheritance, remove uninherited ones
  12690. (setcdr (car tags-alist)
  12691. (org-remove-uninherited-tags (cdar tags-alist))))
  12692. (when (and
  12693. ;; eval matcher only when the todo condition is OK
  12694. (and (or (not todo-only) (member todo org-not-done-keywords))
  12695. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12696. (eval matcher)))
  12697. ;; Call the skipper, but return t if it does not skip,
  12698. ;; so that the `and' form continues evaluating
  12699. (progn
  12700. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12701. t)
  12702. ;; Check if timestamps are deselecting this entry
  12703. (or (not todo-only)
  12704. (and (member todo org-not-done-keywords)
  12705. (or (not org-agenda-tags-todo-honor-ignore-options)
  12706. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12707. ;; select this headline
  12708. (cond
  12709. ((eq action 'sparse-tree)
  12710. (and org-highlight-sparse-tree-matches
  12711. (org-get-heading) (match-end 0)
  12712. (org-highlight-new-match
  12713. (match-beginning 1) (match-end 1)))
  12714. (org-show-context 'tags-tree))
  12715. ((eq action 'agenda)
  12716. (setq txt (org-agenda-format-item
  12717. ""
  12718. (concat
  12719. (if (eq org-tags-match-list-sublevels 'indented)
  12720. (make-string (1- level) ?.) "")
  12721. (org-get-heading))
  12722. level category
  12723. tags-list)
  12724. priority (org-get-priority txt))
  12725. (goto-char lspos)
  12726. (setq marker (org-agenda-new-marker))
  12727. (org-add-props txt props
  12728. 'org-marker marker 'org-hd-marker marker 'org-category category
  12729. 'todo-state todo
  12730. 'priority priority 'type "tagsmatch")
  12731. (push txt rtn))
  12732. ((functionp action)
  12733. (setq org-map-continue-from nil)
  12734. (save-excursion
  12735. (setq rtn1 (funcall action))
  12736. (push rtn1 rtn)))
  12737. (t (user-error "Invalid action")))
  12738. ;; if we are to skip sublevels, jump to end of subtree
  12739. (unless org-tags-match-list-sublevels
  12740. (org-end-of-subtree t)
  12741. (backward-char 1))))
  12742. ;; Get the correct position from where to continue
  12743. (if org-map-continue-from
  12744. (goto-char org-map-continue-from)
  12745. (and (= (point) lspos) (end-of-line 1)))))
  12746. (when (and (eq action 'sparse-tree)
  12747. (not org-sparse-tree-open-archived-trees))
  12748. (org-hide-archived-subtrees (point-min) (point-max)))
  12749. (nreverse rtn)))
  12750. (defun org-remove-uninherited-tags (tags)
  12751. "Remove all tags that are not inherited from the list TAGS."
  12752. (cond
  12753. ((eq org-use-tag-inheritance t)
  12754. (if org-tags-exclude-from-inheritance
  12755. (org-delete-all org-tags-exclude-from-inheritance tags)
  12756. tags))
  12757. ((not org-use-tag-inheritance) nil)
  12758. ((stringp org-use-tag-inheritance)
  12759. (delq nil (mapcar
  12760. (lambda (x)
  12761. (if (and (string-match org-use-tag-inheritance x)
  12762. (not (member x org-tags-exclude-from-inheritance)))
  12763. x nil))
  12764. tags)))
  12765. ((listp org-use-tag-inheritance)
  12766. (delq nil (mapcar
  12767. (lambda (x)
  12768. (if (member x org-use-tag-inheritance) x nil))
  12769. tags)))))
  12770. (defun org-match-sparse-tree (&optional todo-only match)
  12771. "Create a sparse tree according to tags string MATCH.
  12772. MATCH can contain positive and negative selection of tags, like
  12773. \"+WORK+URGENT-WITHBOSS\".
  12774. If optional argument TODO-ONLY is non-nil, only select lines that are
  12775. also TODO lines."
  12776. (interactive "P")
  12777. (org-agenda-prepare-buffers (list (current-buffer)))
  12778. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12779. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12780. (defvar org-cached-props nil)
  12781. (defun org-cached-entry-get (pom property)
  12782. (if (or (eq t org-use-property-inheritance)
  12783. (and (stringp org-use-property-inheritance)
  12784. (string-match org-use-property-inheritance property))
  12785. (and (listp org-use-property-inheritance)
  12786. (member property org-use-property-inheritance)))
  12787. ;; Caching is not possible, check it directly
  12788. (org-entry-get pom property 'inherit)
  12789. ;; Get all properties, so that we can do complicated checks easily
  12790. (cdr (assoc property (or org-cached-props
  12791. (setq org-cached-props
  12792. (org-entry-properties pom)))))))
  12793. (defun org-global-tags-completion-table (&optional files)
  12794. "Return the list of all tags in all agenda buffer/files.
  12795. Optional FILES argument is a list of files which can be used
  12796. instead of the agenda files."
  12797. (save-excursion
  12798. (org-uniquify
  12799. (delq nil
  12800. (apply 'append
  12801. (mapcar
  12802. (lambda (file)
  12803. (set-buffer (find-file-noselect file))
  12804. (append (org-get-buffer-tags)
  12805. (mapcar (lambda (x) (if (stringp (car-safe x))
  12806. (list (car-safe x)) nil))
  12807. org-tag-alist)))
  12808. (if (and files (car files))
  12809. files
  12810. (org-agenda-files))))))))
  12811. (defun org-make-tags-matcher (match)
  12812. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12813. The variable `todo-only' is scoped dynamically into this function.
  12814. It will be set to t if the matcher restricts matching to TODO entries,
  12815. otherwise will not be touched.
  12816. Returns a cons of the selection string MATCH and the constructed
  12817. lisp form implementing the matcher. The matcher is to be evaluated
  12818. at an Org entry, with point on the headline, and returns t if the
  12819. entry matches the selection string MATCH. The returned lisp form
  12820. references two variables with information about the entry, which
  12821. must be bound around the form's evaluation: todo, the TODO keyword
  12822. at the entry (or nil of none); and tags-list, the list of all tags
  12823. at the entry including inherited ones. Additionally, the category
  12824. of the entry (if any) must be specified as the text property
  12825. 'org-category on the headline.
  12826. See also `org-scan-tags'.
  12827. "
  12828. (declare (special todo-only))
  12829. (unless (boundp 'todo-only)
  12830. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12831. (unless match
  12832. ;; Get a new match request, with completion against the global
  12833. ;; tags table and the local tags in current buffer
  12834. (let ((org-last-tags-completion-table
  12835. (org-uniquify
  12836. (delq nil (append (org-get-buffer-tags)
  12837. (org-global-tags-completion-table))))))
  12838. (setq match (org-completing-read-no-i
  12839. "Match: " 'org-tags-completion-function nil nil nil
  12840. 'org-tags-history))))
  12841. ;; Parse the string and create a lisp form
  12842. (let ((match0 match)
  12843. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12844. minus tag mm
  12845. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12846. orterms term orlist re-p str-p level-p level-op time-p
  12847. prop-p pn pv po gv rest (start 0) (ss 0))
  12848. ;; Expand group tags
  12849. (setq match (org-tags-expand match))
  12850. ;; Check if there is a TODO part of this match, which would be the
  12851. ;; part after a "/". TO make sure that this slash is not part of
  12852. ;; a property value to be matched against, we also check that there
  12853. ;; is no " after that slash.
  12854. ;; First, find the last slash
  12855. (while (string-match "/+" match ss)
  12856. (setq start (match-beginning 0) ss (match-end 0)))
  12857. (if (and (string-match "/+" match start)
  12858. (not (save-match-data (string-match "\"" match start))))
  12859. ;; match contains also a todo-matching request
  12860. (progn
  12861. (setq tagsmatch (substring match 0 (match-beginning 0))
  12862. todomatch (substring match (match-end 0)))
  12863. (if (string-match "^!" todomatch)
  12864. (setq todo-only t todomatch (substring todomatch 1)))
  12865. (if (string-match "^\\s-*$" todomatch)
  12866. (setq todomatch nil)))
  12867. ;; only matching tags
  12868. (setq tagsmatch match todomatch nil))
  12869. ;; Make the tags matcher
  12870. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12871. (setq tagsmatcher t)
  12872. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12873. (while (setq term (pop orterms))
  12874. (while (and (equal (substring term -1) "\\") orterms)
  12875. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12876. (while (string-match re term)
  12877. (setq rest (substring term (match-end 0))
  12878. minus (and (match-end 1)
  12879. (equal (match-string 1 term) "-"))
  12880. tag (save-match-data (replace-regexp-in-string
  12881. "\\\\-" "-"
  12882. (match-string 2 term)))
  12883. re-p (equal (string-to-char tag) ?{)
  12884. level-p (match-end 4)
  12885. prop-p (match-end 5)
  12886. mm (cond
  12887. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12888. (level-p
  12889. (setq level-op (org-op-to-function (match-string 3 term)))
  12890. `(,level-op level ,(string-to-number
  12891. (match-string 4 term))))
  12892. (prop-p
  12893. (setq pn (match-string 5 term)
  12894. po (match-string 6 term)
  12895. pv (match-string 7 term)
  12896. re-p (equal (string-to-char pv) ?{)
  12897. str-p (equal (string-to-char pv) ?\")
  12898. time-p (save-match-data
  12899. (string-match "^\"[[<].*[]>]\"$" pv))
  12900. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12901. (if time-p (setq pv (org-matcher-time pv)))
  12902. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12903. (cond
  12904. ((equal pn "CATEGORY")
  12905. (setq gv '(get-text-property (point) 'org-category)))
  12906. ((equal pn "TODO")
  12907. (setq gv 'todo))
  12908. (t
  12909. (setq gv `(org-cached-entry-get nil ,pn))))
  12910. (if re-p
  12911. (if (eq po 'org<>)
  12912. `(not (string-match ,pv (or ,gv "")))
  12913. `(string-match ,pv (or ,gv "")))
  12914. (if str-p
  12915. `(,po (or ,gv "") ,pv)
  12916. `(,po (string-to-number (or ,gv ""))
  12917. ,(string-to-number pv) ))))
  12918. (t `(member ,tag tags-list)))
  12919. mm (if minus (list 'not mm) mm)
  12920. term rest)
  12921. (push mm tagsmatcher))
  12922. (push (if (> (length tagsmatcher) 1)
  12923. (cons 'and tagsmatcher)
  12924. (car tagsmatcher))
  12925. orlist)
  12926. (setq tagsmatcher nil))
  12927. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12928. (setq tagsmatcher
  12929. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12930. ;; Make the todo matcher
  12931. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12932. (setq todomatcher t)
  12933. (setq orterms (org-split-string todomatch "|") orlist nil)
  12934. (while (setq term (pop orterms))
  12935. (while (string-match re term)
  12936. (setq minus (and (match-end 1)
  12937. (equal (match-string 1 term) "-"))
  12938. kwd (match-string 2 term)
  12939. re-p (equal (string-to-char kwd) ?{)
  12940. term (substring term (match-end 0))
  12941. mm (if re-p
  12942. `(string-match ,(substring kwd 1 -1) todo)
  12943. (list 'equal 'todo kwd))
  12944. mm (if minus (list 'not mm) mm))
  12945. (push mm todomatcher))
  12946. (push (if (> (length todomatcher) 1)
  12947. (cons 'and todomatcher)
  12948. (car todomatcher))
  12949. orlist)
  12950. (setq todomatcher nil))
  12951. (setq todomatcher (if (> (length orlist) 1)
  12952. (cons 'or orlist) (car orlist))))
  12953. ;; Return the string and lisp forms of the matcher
  12954. (setq matcher (if todomatcher
  12955. (list 'and tagsmatcher todomatcher)
  12956. tagsmatcher))
  12957. (when todo-only
  12958. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12959. matcher)))
  12960. (cons match0 matcher)))
  12961. (defun org-tags-expand (match &optional single-as-list downcased)
  12962. "Expand group tags in MATCH.
  12963. This replaces every group tag in MATCH with a regexp tag search.
  12964. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12965. will be replaced like this:
  12966. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12967. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12968. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12969. Replacing by a regexp preserves the structure of the match.
  12970. E.g., this expansion
  12971. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12972. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12973. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12974. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12975. assumed to be a single group tag, and the function will return
  12976. the list of tags in this group.
  12977. When DOWNCASE is non-nil, expand downcased TAGS."
  12978. (if org-group-tags
  12979. (let* ((case-fold-search t)
  12980. (stable org-mode-syntax-table)
  12981. (tal (or org-tag-groups-alist-for-agenda
  12982. org-tag-groups-alist))
  12983. (tal (if downcased
  12984. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12985. (tml (mapcar 'car tal))
  12986. (rtnmatch match) rpl)
  12987. ;; @ and _ are allowed as word-components in tags
  12988. (modify-syntax-entry ?@ "w" stable)
  12989. (modify-syntax-entry ?_ "w" stable)
  12990. (while (and tml
  12991. (with-syntax-table stable
  12992. (string-match
  12993. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12994. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12995. (let* ((dir (match-string 1 rtnmatch))
  12996. (tag (match-string 2 rtnmatch))
  12997. (tag (if downcased (downcase tag) tag)))
  12998. (setq tml (delete tag tml))
  12999. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13000. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13001. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13002. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13003. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13004. (if single-as-list
  13005. (or (reverse rpl) (list rtnmatch))
  13006. rtnmatch))
  13007. (if single-as-list (list (if downcased (downcase match) match))
  13008. match)))
  13009. (defun org-op-to-function (op &optional stringp)
  13010. "Turn an operator into the appropriate function."
  13011. (setq op
  13012. (cond
  13013. ((equal op "<" ) '(< string< org-time<))
  13014. ((equal op ">" ) '(> org-string> org-time>))
  13015. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13016. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13017. ((member op '("=" "==")) '(= string= org-time=))
  13018. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13019. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13020. (defun org<> (a b) (not (= a b)))
  13021. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13022. (defun org-string>= (a b) (not (string< a b)))
  13023. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13024. (defun org-string<> (a b) (not (string= a b)))
  13025. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13026. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13027. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13028. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13029. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13030. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13031. (defun org-2ft (s)
  13032. "Convert S to a floating point time.
  13033. If S is already a number, just return it. If it is a string, parse
  13034. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13035. (cond
  13036. ((numberp s) s)
  13037. ((stringp s)
  13038. (condition-case nil
  13039. (float-time (apply 'encode-time (org-parse-time-string s)))
  13040. (error 0.)))
  13041. (t 0.)))
  13042. (defun org-time-today ()
  13043. "Time in seconds today at 0:00.
  13044. Returns the float number of seconds since the beginning of the
  13045. epoch to the beginning of today (00:00)."
  13046. (float-time (apply 'encode-time
  13047. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13048. (defun org-matcher-time (s)
  13049. "Interpret a time comparison value."
  13050. (save-match-data
  13051. (cond
  13052. ((string= s "<now>") (float-time))
  13053. ((string= s "<today>") (org-time-today))
  13054. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13055. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13056. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13057. (+ (org-time-today)
  13058. (* (string-to-number (match-string 1 s))
  13059. (cdr (assoc (match-string 2 s)
  13060. '(("d" . 86400.0) ("w" . 604800.0)
  13061. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13062. (t (org-2ft s)))))
  13063. (defun org-match-any-p (re list)
  13064. "Does re match any element of list?"
  13065. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13066. (delq nil list))
  13067. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13068. (defvar org-tags-overlay (make-overlay 1 1))
  13069. (org-detach-overlay org-tags-overlay)
  13070. (defun org-get-local-tags-at (&optional pos)
  13071. "Get a list of tags defined in the current headline."
  13072. (org-get-tags-at pos 'local))
  13073. (defun org-get-local-tags ()
  13074. "Get a list of tags defined in the current headline."
  13075. (org-get-tags-at nil 'local))
  13076. (defun org-get-tags-at (&optional pos local)
  13077. "Get a list of all headline tags applicable at POS.
  13078. POS defaults to point. If tags are inherited, the list contains
  13079. the targets in the same sequence as the headlines appear, i.e.
  13080. the tags of the current headline come last.
  13081. When LOCAL is non-nil, only return tags from the current headline,
  13082. ignore inherited ones."
  13083. (interactive)
  13084. (if (and org-trust-scanner-tags
  13085. (or (not pos) (equal pos (point)))
  13086. (not local))
  13087. org-scanner-tags
  13088. (let (tags ltags lastpos parent)
  13089. (save-excursion
  13090. (save-restriction
  13091. (widen)
  13092. (goto-char (or pos (point)))
  13093. (save-match-data
  13094. (catch 'done
  13095. (condition-case nil
  13096. (progn
  13097. (org-back-to-heading t)
  13098. (while (not (equal lastpos (point)))
  13099. (setq lastpos (point))
  13100. (when (looking-at
  13101. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13102. (setq ltags (org-split-string
  13103. (org-match-string-no-properties 1) ":"))
  13104. (when parent
  13105. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13106. (setq tags (append
  13107. (if parent
  13108. (org-remove-uninherited-tags ltags)
  13109. ltags)
  13110. tags)))
  13111. (or org-use-tag-inheritance (throw 'done t))
  13112. (if local (throw 'done t))
  13113. (or (org-up-heading-safe) (error nil))
  13114. (setq parent t)))
  13115. (error nil)))))
  13116. (if local
  13117. tags
  13118. (reverse (delete-dups
  13119. (reverse (append
  13120. (org-remove-uninherited-tags
  13121. org-file-tags) tags)))))))))
  13122. (defun org-add-prop-inherited (s)
  13123. (add-text-properties 0 (length s) '(inherited t) s)
  13124. s)
  13125. (defun org-toggle-tag (tag &optional onoff)
  13126. "Toggle the tag TAG for the current line.
  13127. If ONOFF is `on' or `off', don't toggle but set to this state."
  13128. (let (res current)
  13129. (save-excursion
  13130. (org-back-to-heading t)
  13131. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13132. (point-at-eol) t)
  13133. (progn
  13134. (setq current (match-string 1))
  13135. (replace-match ""))
  13136. (setq current ""))
  13137. (setq current (nreverse (org-split-string current ":")))
  13138. (cond
  13139. ((eq onoff 'on)
  13140. (setq res t)
  13141. (or (member tag current) (push tag current)))
  13142. ((eq onoff 'off)
  13143. (or (not (member tag current)) (setq current (delete tag current))))
  13144. (t (if (member tag current)
  13145. (setq current (delete tag current))
  13146. (setq res t)
  13147. (push tag current))))
  13148. (end-of-line 1)
  13149. (if current
  13150. (progn
  13151. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13152. (org-set-tags nil t))
  13153. (delete-horizontal-space))
  13154. (run-hooks 'org-after-tags-change-hook))
  13155. res))
  13156. (defun org-align-tags-here (to-col)
  13157. ;; Assumes that this is a headline
  13158. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13159. (beginning-of-line 1)
  13160. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13161. (< pos (match-beginning 2)))
  13162. (progn
  13163. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13164. (goto-char (match-beginning 1))
  13165. (insert " ")
  13166. (delete-region (point) (1+ (match-beginning 2)))
  13167. (setq ncol (max (current-column)
  13168. (1+ col)
  13169. (if (> to-col 0)
  13170. to-col
  13171. (- (abs to-col) tags-l))))
  13172. (setq p (point))
  13173. (insert (make-string (- ncol (current-column)) ?\ ))
  13174. (setq ncol (current-column))
  13175. (when indent-tabs-mode (tabify p (point-at-eol)))
  13176. (org-move-to-column (min ncol col) t nil t))
  13177. (goto-char pos))))
  13178. (defun org-set-tags-command (&optional arg just-align)
  13179. "Call the set-tags command for the current entry."
  13180. (interactive "P")
  13181. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13182. (org-set-tags arg just-align)
  13183. (save-excursion
  13184. (unless (and (org-region-active-p)
  13185. org-loop-over-headlines-in-active-region)
  13186. (org-back-to-heading t))
  13187. (org-set-tags arg just-align))))
  13188. (defun org-set-tags-to (data)
  13189. "Set the tags of the current entry to DATA, replacing the current tags.
  13190. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13191. If DATA is nil or the empty string, any tags will be removed."
  13192. (interactive "sTags: ")
  13193. (setq data
  13194. (cond
  13195. ((eq data nil) "")
  13196. ((equal data "") "")
  13197. ((stringp data)
  13198. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13199. ":"))
  13200. ((listp data)
  13201. (concat ":" (mapconcat 'identity data ":") ":"))))
  13202. (when data
  13203. (save-excursion
  13204. (org-back-to-heading t)
  13205. (when (looking-at org-complex-heading-regexp)
  13206. (if (match-end 5)
  13207. (progn
  13208. (goto-char (match-beginning 5))
  13209. (insert data)
  13210. (delete-region (point) (point-at-eol))
  13211. (org-set-tags nil 'align))
  13212. (goto-char (point-at-eol))
  13213. (insert " " data)
  13214. (org-set-tags nil 'align)))
  13215. (beginning-of-line 1)
  13216. (if (looking-at ".*?\\([ \t]+\\)$")
  13217. (delete-region (match-beginning 1) (match-end 1))))))
  13218. (defun org-align-all-tags ()
  13219. "Align the tags i all headings."
  13220. (interactive)
  13221. (save-excursion
  13222. (or (ignore-errors (org-back-to-heading t))
  13223. (outline-next-heading))
  13224. (if (org-at-heading-p)
  13225. (org-set-tags t)
  13226. (message "No headings"))))
  13227. (defvar org-indent-indentation-per-level)
  13228. (defun org-set-tags (&optional arg just-align)
  13229. "Set the tags for the current headline.
  13230. With prefix ARG, realign all tags in headings in the current buffer."
  13231. (interactive "P")
  13232. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13233. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13234. 'region-start-level 'region))
  13235. org-loop-over-headlines-in-active-region)
  13236. (org-map-entries
  13237. ;; We don't use ARG and JUST-ALIGN here these args are not
  13238. ;; useful when looping over headlines
  13239. `(org-set-tags)
  13240. org-loop-over-headlines-in-active-region
  13241. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13242. (let* ((re org-outline-regexp-bol)
  13243. (current (unless arg (org-get-tags-string)))
  13244. (col (current-column))
  13245. (org-setting-tags t)
  13246. table current-tags inherited-tags ; computed below when needed
  13247. tags p0 c0 c1 rpl di tc level)
  13248. (if arg
  13249. (save-excursion
  13250. (goto-char (point-min))
  13251. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13252. (while (re-search-forward re nil t)
  13253. (org-set-tags nil t)
  13254. (end-of-line 1)))
  13255. (message "All tags realigned to column %d" org-tags-column))
  13256. (if just-align
  13257. (setq tags current)
  13258. ;; Get a new set of tags from the user
  13259. (save-excursion
  13260. (setq table (append org-tag-persistent-alist
  13261. (or org-tag-alist (org-get-buffer-tags))
  13262. (and
  13263. org-complete-tags-always-offer-all-agenda-tags
  13264. (org-global-tags-completion-table
  13265. (org-agenda-files))))
  13266. org-last-tags-completion-table table
  13267. current-tags (org-split-string current ":")
  13268. inherited-tags (nreverse
  13269. (nthcdr (length current-tags)
  13270. (nreverse (org-get-tags-at))))
  13271. tags
  13272. (if (or (eq t org-use-fast-tag-selection)
  13273. (and org-use-fast-tag-selection
  13274. (delq nil (mapcar 'cdr table))))
  13275. (org-fast-tag-selection
  13276. current-tags inherited-tags table
  13277. (if org-fast-tag-selection-include-todo
  13278. org-todo-key-alist))
  13279. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13280. (org-trim
  13281. (org-icompleting-read "Tags: "
  13282. 'org-tags-completion-function
  13283. nil nil current 'org-tags-history))))))
  13284. (while (string-match "[-+&]+" tags)
  13285. ;; No boolean logic, just a list
  13286. (setq tags (replace-match ":" t t tags))))
  13287. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13288. (if org-tags-sort-function
  13289. (setq tags (mapconcat 'identity
  13290. (sort (org-split-string
  13291. tags (org-re "[^[:alnum:]_@#%]+"))
  13292. org-tags-sort-function) ":")))
  13293. (if (string-match "\\`[\t ]*\\'" tags)
  13294. (setq tags "")
  13295. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13296. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13297. ;; Insert new tags at the correct column
  13298. (beginning-of-line 1)
  13299. (setq level (or (and (looking-at org-outline-regexp)
  13300. (- (match-end 0) (point) 1))
  13301. 1))
  13302. (cond
  13303. ((and (equal current "") (equal tags "")))
  13304. ((re-search-forward
  13305. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13306. (point-at-eol) t)
  13307. (if (equal tags "")
  13308. (setq rpl "")
  13309. (goto-char (match-beginning 0))
  13310. (setq c0 (current-column)
  13311. ;; compute offset for the case of org-indent-mode active
  13312. di (if org-indent-mode
  13313. (* (1- org-indent-indentation-per-level) (1- level))
  13314. 0)
  13315. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13316. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13317. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13318. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13319. (replace-match rpl t t)
  13320. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13321. tags)
  13322. (t (error "Tags alignment failed")))
  13323. (org-move-to-column col)
  13324. (unless just-align
  13325. (run-hooks 'org-after-tags-change-hook))))))
  13326. (defun org-change-tag-in-region (beg end tag off)
  13327. "Add or remove TAG for each entry in the region.
  13328. This works in the agenda, and also in an org-mode buffer."
  13329. (interactive
  13330. (list (region-beginning) (region-end)
  13331. (let ((org-last-tags-completion-table
  13332. (if (derived-mode-p 'org-mode)
  13333. (org-uniquify
  13334. (delq nil (append (org-get-buffer-tags)
  13335. (org-global-tags-completion-table))))
  13336. (org-global-tags-completion-table))))
  13337. (org-icompleting-read
  13338. "Tag: " 'org-tags-completion-function nil nil nil
  13339. 'org-tags-history))
  13340. (progn
  13341. (message "[s]et or [r]emove? ")
  13342. (equal (read-char-exclusive) ?r))))
  13343. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13344. (let ((agendap (equal major-mode 'org-agenda-mode))
  13345. l1 l2 m buf pos newhead (cnt 0))
  13346. (goto-char end)
  13347. (setq l2 (1- (org-current-line)))
  13348. (goto-char beg)
  13349. (setq l1 (org-current-line))
  13350. (loop for l from l1 to l2 do
  13351. (org-goto-line l)
  13352. (setq m (get-text-property (point) 'org-hd-marker))
  13353. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13354. (and agendap m))
  13355. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13356. pos (if agendap m (point)))
  13357. (with-current-buffer buf
  13358. (save-excursion
  13359. (save-restriction
  13360. (goto-char pos)
  13361. (setq cnt (1+ cnt))
  13362. (org-toggle-tag tag (if off 'off 'on))
  13363. (setq newhead (org-get-heading)))))
  13364. (and agendap (org-agenda-change-all-lines newhead m))))
  13365. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13366. (defun org-tags-completion-function (string predicate &optional flag)
  13367. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13368. (confirm (lambda (x) (stringp (car x)))))
  13369. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13370. (setq s1 (match-string 1 string)
  13371. s2 (match-string 2 string))
  13372. (setq s1 "" s2 string))
  13373. (cond
  13374. ((eq flag nil)
  13375. ;; try completion
  13376. (setq rtn (try-completion s2 ctable confirm))
  13377. (if (stringp rtn)
  13378. (setq rtn
  13379. (concat s1 s2 (substring rtn (length s2))
  13380. (if (and org-add-colon-after-tag-completion
  13381. (assoc rtn ctable))
  13382. ":" ""))))
  13383. rtn)
  13384. ((eq flag t)
  13385. ;; all-completions
  13386. (all-completions s2 ctable confirm))
  13387. ((eq flag 'lambda)
  13388. ;; exact match?
  13389. (assoc s2 ctable)))))
  13390. (defun org-fast-tag-insert (kwd tags face &optional end)
  13391. "Insert KDW, and the TAGS, the latter with face FACE.
  13392. Also insert END."
  13393. (insert (format "%-12s" (concat kwd ":"))
  13394. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13395. (or end "")))
  13396. (defun org-fast-tag-show-exit (flag)
  13397. (save-excursion
  13398. (org-goto-line 3)
  13399. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13400. (replace-match ""))
  13401. (when flag
  13402. (end-of-line 1)
  13403. (org-move-to-column (- (window-width) 19) t)
  13404. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13405. (defun org-set-current-tags-overlay (current prefix)
  13406. "Add an overlay to CURRENT tag with PREFIX."
  13407. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13408. (if (featurep 'xemacs)
  13409. (org-overlay-display org-tags-overlay (concat prefix s)
  13410. 'secondary-selection)
  13411. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13412. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13413. (defvar org-last-tag-selection-key nil)
  13414. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13415. "Fast tag selection with single keys.
  13416. CURRENT is the current list of tags in the headline, INHERITED is the
  13417. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13418. possibly with grouping information. TODO-TABLE is a similar table with
  13419. TODO keywords, should these have keys assigned to them.
  13420. If the keys are nil, a-z are automatically assigned.
  13421. Returns the new tags string, or nil to not change the current settings."
  13422. (let* ((fulltable (append table todo-table))
  13423. (maxlen (apply 'max (mapcar
  13424. (lambda (x)
  13425. (if (stringp (car x)) (string-width (car x)) 0))
  13426. fulltable)))
  13427. (buf (current-buffer))
  13428. (expert (eq org-fast-tag-selection-single-key 'expert))
  13429. (buffer-tags nil)
  13430. (fwidth (+ maxlen 3 1 3))
  13431. (ncol (/ (- (window-width) 4) fwidth))
  13432. (i-face 'org-done)
  13433. (c-face 'org-todo)
  13434. tg cnt e c char c1 c2 ntable tbl rtn
  13435. ov-start ov-end ov-prefix
  13436. (exit-after-next org-fast-tag-selection-single-key)
  13437. (done-keywords org-done-keywords)
  13438. groups ingroup)
  13439. (save-excursion
  13440. (beginning-of-line 1)
  13441. (if (looking-at
  13442. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13443. (setq ov-start (match-beginning 1)
  13444. ov-end (match-end 1)
  13445. ov-prefix "")
  13446. (setq ov-start (1- (point-at-eol))
  13447. ov-end (1+ ov-start))
  13448. (skip-chars-forward "^\n\r")
  13449. (setq ov-prefix
  13450. (concat
  13451. (buffer-substring (1- (point)) (point))
  13452. (if (> (current-column) org-tags-column)
  13453. " "
  13454. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13455. (move-overlay org-tags-overlay ov-start ov-end)
  13456. (save-window-excursion
  13457. (if expert
  13458. (set-buffer (get-buffer-create " *Org tags*"))
  13459. (delete-other-windows)
  13460. (split-window-vertically)
  13461. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13462. (erase-buffer)
  13463. (org-set-local 'org-done-keywords done-keywords)
  13464. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13465. (org-fast-tag-insert "Current" current c-face "\n\n")
  13466. (org-fast-tag-show-exit exit-after-next)
  13467. (org-set-current-tags-overlay current ov-prefix)
  13468. (setq tbl fulltable char ?a cnt 0)
  13469. (while (setq e (pop tbl))
  13470. (cond
  13471. ((equal (car e) :startgroup)
  13472. (push '() groups) (setq ingroup t)
  13473. (when (not (= cnt 0))
  13474. (setq cnt 0)
  13475. (insert "\n"))
  13476. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13477. ((equal (car e) :endgroup)
  13478. (setq ingroup nil cnt 0)
  13479. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13480. ((equal e '(:newline))
  13481. (when (not (= cnt 0))
  13482. (setq cnt 0)
  13483. (insert "\n")
  13484. (setq e (car tbl))
  13485. (while (equal (car tbl) '(:newline))
  13486. (insert "\n")
  13487. (setq tbl (cdr tbl)))))
  13488. ((equal e '(:grouptags)) nil)
  13489. (t
  13490. (setq tg (copy-sequence (car e)) c2 nil)
  13491. (if (cdr e)
  13492. (setq c (cdr e))
  13493. ;; automatically assign a character.
  13494. (setq c1 (string-to-char
  13495. (downcase (substring
  13496. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13497. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13498. (while (or (rassoc char ntable) (rassoc char table))
  13499. (setq char (1+ char)))
  13500. (setq c2 c1))
  13501. (setq c (or c2 char)))
  13502. (if ingroup (push tg (car groups)))
  13503. (setq tg (org-add-props tg nil 'face
  13504. (cond
  13505. ((not (assoc tg table))
  13506. (org-get-todo-face tg))
  13507. ((member tg current) c-face)
  13508. ((member tg inherited) i-face))))
  13509. (if (equal (caar tbl) :grouptags)
  13510. (org-add-props tg nil 'face 'org-tag-group))
  13511. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13512. (insert "[" c "] " tg (make-string
  13513. (- fwidth 4 (length tg)) ?\ ))
  13514. (push (cons tg c) ntable)
  13515. (when (= (setq cnt (1+ cnt)) ncol)
  13516. (insert "\n")
  13517. (if ingroup (insert " "))
  13518. (setq cnt 0)))))
  13519. (setq ntable (nreverse ntable))
  13520. (insert "\n")
  13521. (goto-char (point-min))
  13522. (if (not expert) (org-fit-window-to-buffer))
  13523. (setq rtn
  13524. (catch 'exit
  13525. (while t
  13526. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13527. (if (not groups) "no " "")
  13528. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13529. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13530. (setq org-last-tag-selection-key c)
  13531. (cond
  13532. ((= c ?\r) (throw 'exit t))
  13533. ((= c ?!)
  13534. (setq groups (not groups))
  13535. (goto-char (point-min))
  13536. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13537. ((= c ?\C-c)
  13538. (if (not expert)
  13539. (org-fast-tag-show-exit
  13540. (setq exit-after-next (not exit-after-next)))
  13541. (setq expert nil)
  13542. (delete-other-windows)
  13543. (set-window-buffer (split-window-vertically) " *Org tags*")
  13544. (org-switch-to-buffer-other-window " *Org tags*")
  13545. (org-fit-window-to-buffer)))
  13546. ((or (= c ?\C-g)
  13547. (and (= c ?q) (not (rassoc c ntable))))
  13548. (org-detach-overlay org-tags-overlay)
  13549. (setq quit-flag t))
  13550. ((= c ?\ )
  13551. (setq current nil)
  13552. (if exit-after-next (setq exit-after-next 'now)))
  13553. ((= c ?\t)
  13554. (condition-case nil
  13555. (setq tg (org-icompleting-read
  13556. "Tag: "
  13557. (or buffer-tags
  13558. (with-current-buffer buf
  13559. (org-get-buffer-tags)))))
  13560. (quit (setq tg "")))
  13561. (when (string-match "\\S-" tg)
  13562. (add-to-list 'buffer-tags (list tg))
  13563. (if (member tg current)
  13564. (setq current (delete tg current))
  13565. (push tg current)))
  13566. (if exit-after-next (setq exit-after-next 'now)))
  13567. ((setq e (rassoc c todo-table) tg (car e))
  13568. (with-current-buffer buf
  13569. (save-excursion (org-todo tg)))
  13570. (if exit-after-next (setq exit-after-next 'now)))
  13571. ((setq e (rassoc c ntable) tg (car e))
  13572. (if (member tg current)
  13573. (setq current (delete tg current))
  13574. (loop for g in groups do
  13575. (if (member tg g)
  13576. (mapc (lambda (x)
  13577. (setq current (delete x current)))
  13578. g)))
  13579. (push tg current))
  13580. (if exit-after-next (setq exit-after-next 'now))))
  13581. ;; Create a sorted list
  13582. (setq current
  13583. (sort current
  13584. (lambda (a b)
  13585. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13586. (if (eq exit-after-next 'now) (throw 'exit t))
  13587. (goto-char (point-min))
  13588. (beginning-of-line 2)
  13589. (delete-region (point) (point-at-eol))
  13590. (org-fast-tag-insert "Current" current c-face)
  13591. (org-set-current-tags-overlay current ov-prefix)
  13592. (while (re-search-forward
  13593. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13594. (setq tg (match-string 1))
  13595. (add-text-properties
  13596. (match-beginning 1) (match-end 1)
  13597. (list 'face
  13598. (cond
  13599. ((member tg current) c-face)
  13600. ((member tg inherited) i-face)
  13601. (t (get-text-property (match-beginning 1) 'face))))))
  13602. (goto-char (point-min)))))
  13603. (org-detach-overlay org-tags-overlay)
  13604. (if rtn
  13605. (mapconcat 'identity current ":")
  13606. nil))))
  13607. (defun org-get-tags-string ()
  13608. "Get the TAGS string in the current headline."
  13609. (unless (org-at-heading-p t)
  13610. (user-error "Not on a heading"))
  13611. (save-excursion
  13612. (beginning-of-line 1)
  13613. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13614. (org-match-string-no-properties 1)
  13615. "")))
  13616. (defun org-get-tags ()
  13617. "Get the list of tags specified in the current headline."
  13618. (org-split-string (org-get-tags-string) ":"))
  13619. (defun org-get-buffer-tags ()
  13620. "Get a table of all tags used in the buffer, for completion."
  13621. (let (tags)
  13622. (save-excursion
  13623. (goto-char (point-min))
  13624. (while (re-search-forward
  13625. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13626. (when (equal (char-after (point-at-bol 0)) ?*)
  13627. (mapc (lambda (x) (add-to-list 'tags x))
  13628. (org-split-string (org-match-string-no-properties 1) ":")))))
  13629. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13630. (mapcar 'list tags)))
  13631. ;;;; The mapping API
  13632. (defun org-map-entries (func &optional match scope &rest skip)
  13633. "Call FUNC at each headline selected by MATCH in SCOPE.
  13634. FUNC is a function or a lisp form. The function will be called without
  13635. arguments, with the cursor positioned at the beginning of the headline.
  13636. The return values of all calls to the function will be collected and
  13637. returned as a list.
  13638. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13639. does not need to preserve point. After evaluation, the cursor will be
  13640. moved to the end of the line (presumably of the headline of the
  13641. processed entry) and search continues from there. Under some
  13642. circumstances, this may not produce the wanted results. For example,
  13643. if you have removed (e.g. archived) the current (sub)tree it could
  13644. mean that the next entry will be skipped entirely. In such cases, you
  13645. can specify the position from where search should continue by making
  13646. FUNC set the variable `org-map-continue-from' to the desired buffer
  13647. position.
  13648. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13649. Only headlines that are matched by this query will be considered during
  13650. the iteration. When MATCH is nil or t, all headlines will be
  13651. visited by the iteration.
  13652. SCOPE determines the scope of this command. It can be any of:
  13653. nil The current buffer, respecting the restriction if any
  13654. tree The subtree started with the entry at point
  13655. region The entries within the active region, if any
  13656. region-start-level
  13657. The entries within the active region, but only those at
  13658. the same level than the first one.
  13659. file The current buffer, without restriction
  13660. file-with-archives
  13661. The current buffer, and any archives associated with it
  13662. agenda All agenda files
  13663. agenda-with-archives
  13664. All agenda files with any archive files associated with them
  13665. \(file1 file2 ...)
  13666. If this is a list, all files in the list will be scanned
  13667. The remaining args are treated as settings for the skipping facilities of
  13668. the scanner. The following items can be given here:
  13669. archive skip trees with the archive tag
  13670. comment skip trees with the COMMENT keyword
  13671. function or Emacs Lisp form:
  13672. will be used as value for `org-agenda-skip-function', so
  13673. whenever the function returns a position, FUNC will not be
  13674. called for that entry and search will continue from the
  13675. position returned
  13676. If your function needs to retrieve the tags including inherited tags
  13677. at the *current* entry, you can use the value of the variable
  13678. `org-scanner-tags' which will be much faster than getting the value
  13679. with `org-get-tags-at'. If your function gets properties with
  13680. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13681. to t around the call to `org-entry-properties' to get the same speedup.
  13682. Note that if your function moves around to retrieve tags and properties at
  13683. a *different* entry, you cannot use these techniques."
  13684. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13685. (not (org-region-active-p)))
  13686. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13687. (org-agenda-skip-archived-trees (memq 'archive skip))
  13688. (org-agenda-skip-comment-trees (memq 'comment skip))
  13689. (org-agenda-skip-function
  13690. (car (org-delete-all '(comment archive) skip)))
  13691. (org-tags-match-list-sublevels t)
  13692. (start-level (eq scope 'region-start-level))
  13693. matcher file res
  13694. org-todo-keywords-for-agenda
  13695. org-done-keywords-for-agenda
  13696. org-todo-keyword-alist-for-agenda
  13697. org-tag-alist-for-agenda
  13698. todo-only)
  13699. (cond
  13700. ((eq match t) (setq matcher t))
  13701. ((eq match nil) (setq matcher t))
  13702. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13703. (save-window-excursion
  13704. (save-restriction
  13705. (cond ((eq scope 'tree)
  13706. (org-back-to-heading t)
  13707. (org-narrow-to-subtree)
  13708. (setq scope nil))
  13709. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13710. (org-region-active-p))
  13711. ;; If needed, set start-level to a string like "2"
  13712. (when start-level
  13713. (save-excursion
  13714. (goto-char (region-beginning))
  13715. (unless (org-at-heading-p) (outline-next-heading))
  13716. (setq start-level (org-current-level))))
  13717. (narrow-to-region (region-beginning)
  13718. (save-excursion
  13719. (goto-char (region-end))
  13720. (unless (and (bolp) (org-at-heading-p))
  13721. (outline-next-heading))
  13722. (point)))
  13723. (setq scope nil)))
  13724. (if (not scope)
  13725. (progn
  13726. (org-agenda-prepare-buffers
  13727. (list (buffer-file-name (current-buffer))))
  13728. (setq res (org-scan-tags func matcher todo-only start-level)))
  13729. ;; Get the right scope
  13730. (cond
  13731. ((and scope (listp scope) (symbolp (car scope)))
  13732. (setq scope (eval scope)))
  13733. ((eq scope 'agenda)
  13734. (setq scope (org-agenda-files t)))
  13735. ((eq scope 'agenda-with-archives)
  13736. (setq scope (org-agenda-files t))
  13737. (setq scope (org-add-archive-files scope)))
  13738. ((eq scope 'file)
  13739. (setq scope (list (buffer-file-name))))
  13740. ((eq scope 'file-with-archives)
  13741. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13742. (org-agenda-prepare-buffers scope)
  13743. (while (setq file (pop scope))
  13744. (with-current-buffer (org-find-base-buffer-visiting file)
  13745. (save-excursion
  13746. (save-restriction
  13747. (widen)
  13748. (goto-char (point-min))
  13749. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13750. res)))
  13751. ;;;; Properties
  13752. ;;; Setting and retrieving properties
  13753. (defconst org-special-properties
  13754. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13755. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13756. "The special properties valid in Org-mode.
  13757. These are properties that are not defined in the property drawer,
  13758. but in some other way.")
  13759. (defconst org-default-properties
  13760. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13761. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13762. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13763. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13764. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13765. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13766. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13767. "Some properties that are used by Org-mode for various purposes.
  13768. Being in this list makes sure that they are offered for completion.")
  13769. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13770. "Regular expression matching the first line of a property drawer.")
  13771. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13772. "Regular expression matching the last line of a property drawer.")
  13773. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13774. "Regular expression matching the first line of a property drawer.")
  13775. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13776. "Regular expression matching the first line of a property drawer.")
  13777. (defconst org-property-drawer-re
  13778. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  13779. org-property-end-re "\\)\n?")
  13780. "Matches an entire property drawer.")
  13781. (defconst org-clock-drawer-re
  13782. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  13783. org-property-end-re "\\)\n?")
  13784. "Matches an entire clock drawer.")
  13785. (defun org-property-action ()
  13786. "Do an action on properties."
  13787. (interactive)
  13788. (let (c)
  13789. (org-at-property-p)
  13790. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13791. (setq c (read-char-exclusive))
  13792. (cond
  13793. ((equal c ?s)
  13794. (call-interactively 'org-set-property))
  13795. ((equal c ?d)
  13796. (call-interactively 'org-delete-property))
  13797. ((equal c ?D)
  13798. (call-interactively 'org-delete-property-globally))
  13799. ((equal c ?c)
  13800. (call-interactively 'org-compute-property-at-point))
  13801. (t (user-error "No such property action %c" c)))))
  13802. (defun org-inc-effort ()
  13803. "Increment the value of the effort property in the current entry."
  13804. (interactive)
  13805. (org-set-effort nil t))
  13806. (defvar org-clock-effort) ;; Defined in org-clock.el
  13807. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13808. (defun org-set-effort (&optional value increment)
  13809. "Set the effort property of the current entry.
  13810. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13811. 10th allowed value.
  13812. When INCREMENT is non-nil, set the property to the next allowed value."
  13813. (interactive "P")
  13814. (if (equal value 0) (setq value 10))
  13815. (let* ((completion-ignore-case t)
  13816. (prop org-effort-property)
  13817. (cur (org-entry-get nil prop))
  13818. (allowed (org-property-get-allowed-values nil prop 'table))
  13819. (existing (mapcar 'list (org-property-values prop)))
  13820. (heading (nth 4 (org-heading-components)))
  13821. rpl
  13822. (val (cond
  13823. ((stringp value) value)
  13824. ((and allowed (integerp value))
  13825. (or (car (nth (1- value) allowed))
  13826. (car (org-last allowed))))
  13827. ((and allowed increment)
  13828. (or (caadr (member (list cur) allowed))
  13829. (user-error "Allowed effort values are not set")))
  13830. (allowed
  13831. (message "Select 1-9,0, [RET%s]: %s"
  13832. (if cur (concat "=" cur) "")
  13833. (mapconcat 'car allowed " "))
  13834. (setq rpl (read-char-exclusive))
  13835. (if (equal rpl ?\r)
  13836. cur
  13837. (setq rpl (- rpl ?0))
  13838. (if (equal rpl 0) (setq rpl 10))
  13839. (if (and (> rpl 0) (<= rpl (length allowed)))
  13840. (car (nth (1- rpl) allowed))
  13841. (org-completing-read "Effort: " allowed nil))))
  13842. (t
  13843. (let (org-completion-use-ido org-completion-use-iswitchb)
  13844. (org-completing-read
  13845. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13846. (concat "[" cur "]") "")
  13847. ": ")
  13848. existing nil nil "" nil cur))))))
  13849. (unless (equal (org-entry-get nil prop) val)
  13850. (org-entry-put nil prop val))
  13851. (save-excursion
  13852. (org-back-to-heading t)
  13853. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13854. (when (string= heading org-clock-current-task)
  13855. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13856. (org-clock-update-mode-line))
  13857. (message "%s is now %s" prop val)))
  13858. (defun org-at-property-p ()
  13859. "Is cursor inside a property drawer?"
  13860. (save-excursion
  13861. (when (equal 'node-property (car (org-element-at-point)))
  13862. (beginning-of-line 1)
  13863. (looking-at org-property-re))))
  13864. (defun org-get-property-block (&optional beg end force)
  13865. "Return the (beg . end) range of the body of the property drawer.
  13866. BEG and END are the beginning and end of the current subtree, or of
  13867. the part before the first headline. If they are not given, they will
  13868. be found. If the drawer does not exist and FORCE is non-nil, create
  13869. the drawer."
  13870. (catch 'exit
  13871. (save-excursion
  13872. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13873. (progn (org-back-to-heading t) (point))))
  13874. (end (or end (and (not (outline-next-heading)) (point-max))
  13875. (point))))
  13876. (goto-char beg)
  13877. (if (re-search-forward org-property-start-re end t)
  13878. (setq beg (1+ (match-end 0)))
  13879. (if force
  13880. (save-excursion
  13881. (org-insert-property-drawer)
  13882. (setq end (progn (outline-next-heading) (point))))
  13883. (throw 'exit nil))
  13884. (goto-char beg)
  13885. (if (re-search-forward org-property-start-re end t)
  13886. (setq beg (1+ (match-end 0)))))
  13887. (if (re-search-forward org-property-end-re end t)
  13888. (setq end (match-beginning 0))
  13889. (or force (throw 'exit nil))
  13890. (goto-char beg)
  13891. (setq end beg)
  13892. (org-indent-line)
  13893. (insert ":END:\n"))
  13894. (cons beg end)))))
  13895. (defun org-entry-properties (&optional pom which specific)
  13896. "Get all properties of the entry at point-or-marker POM.
  13897. This includes the TODO keyword, the tags, time strings for deadline,
  13898. scheduled, and clocking, and any additional properties defined in the
  13899. entry. The return value is an alist, keys may occur multiple times
  13900. if the property key was used several times.
  13901. POM may also be nil, in which case the current entry is used.
  13902. If WHICH is nil or `all', get all properties. If WHICH is
  13903. `special' or `standard', only get that subclass. If WHICH
  13904. is a string only get exactly this property. SPECIFIC can be a string, the
  13905. specific property we are interested in. Specifying it can speed
  13906. things up because then unnecessary parsing is avoided."
  13907. (setq which (or which 'all))
  13908. (org-with-point-at pom
  13909. (let ((clockstr (substring org-clock-string 0 -1))
  13910. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13911. (case-fold-search nil)
  13912. beg end range props sum-props key key1 value string clocksum clocksumt)
  13913. (save-excursion
  13914. (when (condition-case nil
  13915. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13916. (error nil))
  13917. (setq beg (point))
  13918. (setq sum-props (get-text-property (point) 'org-summaries))
  13919. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13920. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13921. (outline-next-heading)
  13922. (setq end (point))
  13923. (when (memq which '(all special))
  13924. ;; Get the special properties, like TODO and tags
  13925. (goto-char beg)
  13926. (when (and (or (not specific) (string= specific "TODO"))
  13927. (looking-at org-todo-line-regexp) (match-end 2))
  13928. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13929. (when (and (or (not specific) (string= specific "PRIORITY"))
  13930. (looking-at org-priority-regexp))
  13931. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13932. (when (or (not specific) (string= specific "FILE"))
  13933. (push (cons "FILE" buffer-file-name) props))
  13934. (when (and (or (not specific) (string= specific "TAGS"))
  13935. (setq value (org-get-tags-string))
  13936. (string-match "\\S-" value))
  13937. (push (cons "TAGS" value) props))
  13938. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13939. (setq value (org-get-tags-at)))
  13940. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13941. ":"))
  13942. props))
  13943. (when (or (not specific) (string= specific "BLOCKED"))
  13944. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13945. (when (or (not specific)
  13946. (member specific
  13947. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13948. "TIMESTAMP" "TIMESTAMP_IA")))
  13949. (catch 'match
  13950. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13951. (setq key (if (match-end 1)
  13952. (substring (org-match-string-no-properties 1)
  13953. 0 -1))
  13954. string (if (equal key clockstr)
  13955. (org-trim
  13956. (buffer-substring-no-properties
  13957. (match-beginning 3) (goto-char
  13958. (point-at-eol))))
  13959. (substring (org-match-string-no-properties 3)
  13960. 1 -1)))
  13961. ;; Get the correct property name from the key. This is
  13962. ;; necessary if the user has configured time keywords.
  13963. (setq key1 (concat key ":"))
  13964. (cond
  13965. ((not key)
  13966. (setq key
  13967. (if (= (char-after (match-beginning 3)) ?\[)
  13968. "TIMESTAMP_IA" "TIMESTAMP")))
  13969. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13970. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13971. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13972. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13973. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13974. (progn
  13975. (push (cons key string) props)
  13976. ;; no need to search further if match is found
  13977. (throw 'match t))
  13978. (when (or (equal key "CLOCK") (not (assoc key props)))
  13979. (push (cons key string) props)))))))
  13980. (when (memq which '(all standard))
  13981. ;; Get the standard properties, like :PROP: ...
  13982. (setq range (org-get-property-block beg end))
  13983. (when range
  13984. (goto-char (car range))
  13985. (while (re-search-forward org-property-re
  13986. (cdr range) t)
  13987. (setq key (org-match-string-no-properties 2)
  13988. value (org-trim (or (org-match-string-no-properties 3) "")))
  13989. (unless (member key excluded)
  13990. (push (cons key (or value "")) props)))))
  13991. (if clocksum
  13992. (push (cons "CLOCKSUM"
  13993. (org-columns-number-to-string (/ (float clocksum) 60.)
  13994. 'add_times))
  13995. props))
  13996. (if clocksumt
  13997. (push (cons "CLOCKSUM_T"
  13998. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13999. 'add_times))
  14000. props))
  14001. (unless (assoc "CATEGORY" props)
  14002. (push (cons "CATEGORY" (org-get-category)) props))
  14003. (append sum-props (nreverse props)))))))
  14004. (defun org-entry-get (pom property &optional inherit literal-nil)
  14005. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14006. If INHERIT is non-nil and the entry does not have the property,
  14007. then also check higher levels of the hierarchy.
  14008. If INHERIT is the symbol `selective', use inheritance only if the setting
  14009. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14010. If the property is present but empty, the return value is the empty string.
  14011. If the property is not present at all, nil is returned.
  14012. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14013. do not interpret it as the list atom nil. This is used for inheritance
  14014. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14015. (org-with-point-at pom
  14016. (if (and inherit (if (eq inherit 'selective)
  14017. (org-property-inherit-p property)
  14018. t))
  14019. (org-entry-get-with-inheritance property literal-nil)
  14020. (if (member property org-special-properties)
  14021. ;; We need a special property. Use `org-entry-properties' to
  14022. ;; retrieve it, but specify the wanted property
  14023. (cdr (assoc property (org-entry-properties nil 'special property)))
  14024. (let ((range (org-get-property-block)))
  14025. (when (and range (not (eq (car range) (cdr range))))
  14026. (let* ((props (list (or (assoc property org-file-properties)
  14027. (assoc property org-global-properties)
  14028. (assoc property org-global-properties-fixed))))
  14029. (ap (lambda (key)
  14030. (when (re-search-forward
  14031. (org-re-property key) (cdr range) t)
  14032. (setq props
  14033. (org-update-property-plist
  14034. key
  14035. (if (match-end 3)
  14036. (org-match-string-no-properties 3) "")
  14037. props)))))
  14038. val)
  14039. (goto-char (car range))
  14040. (funcall ap property)
  14041. (goto-char (car range))
  14042. (while (funcall ap (concat property "+")))
  14043. (setq val (cdr (assoc property props)))
  14044. (when val (if literal-nil val (org-not-nil val))))))))))
  14045. (defun org-property-or-variable-value (var &optional inherit)
  14046. "Check if there is a property fixing the value of VAR.
  14047. If yes, return this value. If not, return the current value of the variable."
  14048. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14049. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14050. (read prop)
  14051. (symbol-value var))))
  14052. (defun org-entry-delete (pom property)
  14053. "Delete the property PROPERTY from entry at point-or-marker POM."
  14054. (unless (member property org-special-properties)
  14055. (org-with-point-at pom
  14056. (let ((range (org-get-property-block)))
  14057. (if (and range
  14058. (goto-char (car range))
  14059. (re-search-forward
  14060. (org-re-property property)
  14061. (cdr range) t))
  14062. (progn
  14063. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14064. (org-remove-empty-drawer-at (car range))
  14065. t)
  14066. nil)))))
  14067. ;; Multi-values properties are properties that contain multiple values
  14068. ;; These values are assumed to be single words, separated by whitespace.
  14069. (defun org-entry-add-to-multivalued-property (pom property value)
  14070. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14071. (let* ((old (org-entry-get pom property))
  14072. (values (and old (org-split-string old "[ \t]"))))
  14073. (setq value (org-entry-protect-space value))
  14074. (unless (member value values)
  14075. (setq values (append values (list value)))
  14076. (org-entry-put pom property
  14077. (mapconcat 'identity values " ")))))
  14078. (defun org-entry-remove-from-multivalued-property (pom property value)
  14079. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14080. (let* ((old (org-entry-get pom property))
  14081. (values (and old (org-split-string old "[ \t]"))))
  14082. (setq value (org-entry-protect-space value))
  14083. (when (member value values)
  14084. (setq values (delete value values))
  14085. (org-entry-put pom property
  14086. (mapconcat 'identity values " ")))))
  14087. (defun org-entry-member-in-multivalued-property (pom property value)
  14088. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14089. (let* ((old (org-entry-get pom property))
  14090. (values (and old (org-split-string old "[ \t]"))))
  14091. (setq value (org-entry-protect-space value))
  14092. (member value values)))
  14093. (defun org-entry-get-multivalued-property (pom property)
  14094. "Return a list of values in a multivalued property."
  14095. (let* ((value (org-entry-get pom property))
  14096. (values (and value (org-split-string value "[ \t]"))))
  14097. (mapcar 'org-entry-restore-space values)))
  14098. (defun org-entry-put-multivalued-property (pom property &rest values)
  14099. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14100. VALUES should be a list of strings. Spaces will be protected."
  14101. (org-entry-put pom property
  14102. (mapconcat 'org-entry-protect-space values " "))
  14103. (let* ((value (org-entry-get pom property))
  14104. (values (and value (org-split-string value "[ \t]"))))
  14105. (mapcar 'org-entry-restore-space values)))
  14106. (defun org-entry-protect-space (s)
  14107. "Protect spaces and newline in string S."
  14108. (while (string-match " " s)
  14109. (setq s (replace-match "%20" t t s)))
  14110. (while (string-match "\n" s)
  14111. (setq s (replace-match "%0A" t t s)))
  14112. s)
  14113. (defun org-entry-restore-space (s)
  14114. "Restore spaces and newline in string S."
  14115. (while (string-match "%20" s)
  14116. (setq s (replace-match " " t t s)))
  14117. (while (string-match "%0A" s)
  14118. (setq s (replace-match "\n" t t s)))
  14119. s)
  14120. (defvar org-entry-property-inherited-from (make-marker)
  14121. "Marker pointing to the entry from where a property was inherited.
  14122. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14123. location of the entry where the inheritance search matched. If there was
  14124. no match, the marker will point nowhere.
  14125. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14126. is set.")
  14127. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14128. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14129. The search will stop at the first ancestor which has the property defined.
  14130. If the value found is \"nil\", return nil to show that the property
  14131. should be considered as undefined (this is the meaning of nil here).
  14132. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14133. (move-marker org-entry-property-inherited-from nil)
  14134. (let (tmp)
  14135. (save-excursion
  14136. (save-restriction
  14137. (widen)
  14138. (catch 'ex
  14139. (while t
  14140. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14141. (or (ignore-errors (org-back-to-heading t))
  14142. (goto-char (point-min)))
  14143. (move-marker org-entry-property-inherited-from (point))
  14144. (throw 'ex tmp))
  14145. (or (ignore-errors (org-up-heading-safe))
  14146. (throw 'ex nil))))))
  14147. (setq tmp (or tmp
  14148. (cdr (assoc property org-file-properties))
  14149. (cdr (assoc property org-global-properties))
  14150. (cdr (assoc property org-global-properties-fixed))))
  14151. (if literal-nil tmp (org-not-nil tmp))))
  14152. (defvar org-property-changed-functions nil
  14153. "Hook called when the value of a property has changed.
  14154. Each hook function should accept two arguments, the name of the property
  14155. and the new value.")
  14156. (defun org-entry-put (pom property value)
  14157. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14158. (org-with-point-at pom
  14159. (org-back-to-heading t)
  14160. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14161. range)
  14162. (cond
  14163. ((equal property "TODO")
  14164. (when (and (stringp value) (string-match "\\S-" value)
  14165. (not (member value org-todo-keywords-1)))
  14166. (user-error "\"%s\" is not a valid TODO state" value))
  14167. (if (or (not value)
  14168. (not (string-match "\\S-" value)))
  14169. (setq value 'none))
  14170. (org-todo value)
  14171. (org-set-tags nil 'align))
  14172. ((equal property "PRIORITY")
  14173. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14174. (string-to-char value) ?\ ))
  14175. (org-set-tags nil 'align))
  14176. ((equal property "CLOCKSUM")
  14177. (if (not (re-search-forward
  14178. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14179. (error "Cannot find a clock log")
  14180. (goto-char (- (match-end 1) 2))
  14181. (cond
  14182. ((eq value 'earlier) (org-timestamp-down))
  14183. ((eq value 'later) (org-timestamp-up)))
  14184. (org-clock-sum-current-item)))
  14185. ((equal property "SCHEDULED")
  14186. (if (re-search-forward org-scheduled-time-regexp end t)
  14187. (cond
  14188. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14189. ((eq value 'later) (org-timestamp-change 1 'day))
  14190. (t (call-interactively 'org-schedule)))
  14191. (call-interactively 'org-schedule)))
  14192. ((equal property "DEADLINE")
  14193. (if (re-search-forward org-deadline-time-regexp end t)
  14194. (cond
  14195. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14196. ((eq value 'later) (org-timestamp-change 1 'day))
  14197. (t (call-interactively 'org-deadline)))
  14198. (call-interactively 'org-deadline)))
  14199. ((member property org-special-properties)
  14200. (error "The %s property can not yet be set with `org-entry-put'"
  14201. property))
  14202. (t ; a non-special property
  14203. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14204. (setq range (org-get-property-block beg end 'force))
  14205. (goto-char (car range))
  14206. (if (re-search-forward
  14207. (org-re-property property) (cdr range) t)
  14208. (progn
  14209. (delete-region (match-beginning 0) (match-end 0))
  14210. (goto-char (match-beginning 0)))
  14211. (goto-char (cdr range))
  14212. (insert "\n")
  14213. (backward-char 1)
  14214. (org-indent-line))
  14215. (insert ":" property ":")
  14216. (and value (insert " " value))
  14217. (org-indent-line)))))
  14218. (run-hook-with-args 'org-property-changed-functions property value)))
  14219. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14220. "Get all property keys in the current buffer.
  14221. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14222. like tags and TODO state.
  14223. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14224. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14225. and others.
  14226. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14227. formats in the current buffer."
  14228. (let (rtn range cfmt s p)
  14229. (save-excursion
  14230. (save-restriction
  14231. (widen)
  14232. (goto-char (point-min))
  14233. (while (re-search-forward org-property-start-re nil t)
  14234. (setq range (org-get-property-block))
  14235. (goto-char (car range))
  14236. (while (re-search-forward org-property-re
  14237. (cdr range) t)
  14238. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14239. (outline-next-heading))))
  14240. (when include-specials
  14241. (setq rtn (append org-special-properties rtn)))
  14242. (when include-defaults
  14243. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14244. (add-to-list 'rtn org-effort-property))
  14245. (when include-columns
  14246. (save-excursion
  14247. (save-restriction
  14248. (widen)
  14249. (goto-char (point-min))
  14250. (while (re-search-forward
  14251. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14252. nil t)
  14253. (setq cfmt (match-string 2) s 0)
  14254. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14255. cfmt s)
  14256. (setq s (match-end 0)
  14257. p (match-string 1 cfmt))
  14258. (unless (or (equal p "ITEM")
  14259. (member p org-special-properties))
  14260. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14261. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14262. (defun org-property-values (key)
  14263. "Return a list of all values of property KEY in the current buffer."
  14264. (save-excursion
  14265. (save-restriction
  14266. (widen)
  14267. (goto-char (point-min))
  14268. (let ((re (org-re-property key))
  14269. values)
  14270. (while (re-search-forward re nil t)
  14271. (add-to-list 'values (org-trim (match-string 3))))
  14272. (delete "" values)))))
  14273. (defun org-insert-property-drawer ()
  14274. "Insert a property drawer into the current entry."
  14275. (org-back-to-heading t)
  14276. (looking-at org-outline-regexp)
  14277. (let ((indent (if org-adapt-indentation
  14278. (- (match-end 0) (match-beginning 0))
  14279. 0))
  14280. (beg (point))
  14281. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14282. end hiddenp)
  14283. (outline-next-heading)
  14284. (setq end (point))
  14285. (goto-char beg)
  14286. (while (re-search-forward re end t))
  14287. (setq hiddenp (outline-invisible-p))
  14288. (end-of-line 1)
  14289. (and (equal (char-after) ?\n) (forward-char 1))
  14290. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14291. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14292. ;; just skip this line
  14293. (beginning-of-line 2)
  14294. ;; Drawer start, find the end
  14295. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14296. (beginning-of-line 1)))
  14297. (org-skip-over-state-notes)
  14298. (skip-chars-backward " \t\n\r")
  14299. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14300. (forward-char 1))
  14301. (goto-char (point-at-eol))
  14302. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14303. (beginning-of-line 0)
  14304. (org-indent-to-column indent)
  14305. (beginning-of-line 2)
  14306. (org-indent-to-column indent)
  14307. (beginning-of-line 0)
  14308. (if hiddenp
  14309. (save-excursion
  14310. (org-back-to-heading t)
  14311. (hide-entry))
  14312. (org-flag-drawer t))))
  14313. (defun org-insert-drawer (&optional arg drawer)
  14314. "Insert a drawer at point.
  14315. Optional argument DRAWER, when non-nil, is a string representing
  14316. drawer's name. Otherwise, the user is prompted for a name.
  14317. If a region is active, insert the drawer around that region
  14318. instead.
  14319. Point is left between drawer's boundaries."
  14320. (interactive "P")
  14321. (let* ((drawer (if arg "PROPERTIES"
  14322. (or drawer (read-from-minibuffer "Drawer: ")))))
  14323. (cond
  14324. ;; With C-u, fall back on `org-insert-property-drawer'
  14325. (arg (org-insert-property-drawer))
  14326. ;;
  14327. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14328. (user-error "Invalid drawer name"))
  14329. ;; With an active region, insert a drawer at point.
  14330. ((not (org-region-active-p))
  14331. (progn
  14332. (unless (bolp) (insert "\n"))
  14333. (insert (format ":%s:\n\n:END:\n" drawer))
  14334. (forward-line -2)))
  14335. ;; Otherwise, insert the drawer at point
  14336. (t
  14337. (let ((rbeg (region-beginning))
  14338. (rend (copy-marker (region-end))))
  14339. (unwind-protect
  14340. (progn
  14341. (goto-char rbeg)
  14342. (beginning-of-line)
  14343. (when (save-excursion
  14344. (re-search-forward org-outline-regexp-bol rend t))
  14345. (user-error "Drawers cannot contain headlines"))
  14346. ;; Position point at the beginning of the first
  14347. ;; non-blank line in region. Insert drawer's opening
  14348. ;; there, then indent it.
  14349. (org-skip-whitespace)
  14350. (beginning-of-line)
  14351. (insert ":" drawer ":\n")
  14352. (forward-line -1)
  14353. (indent-for-tab-command)
  14354. ;; Move point to the beginning of the first blank line
  14355. ;; after the last non-blank line in region. Insert
  14356. ;; drawer's closing, then indent it.
  14357. (goto-char rend)
  14358. (skip-chars-backward " \r\t\n")
  14359. (insert "\n:END:")
  14360. (deactivate-mark t)
  14361. (indent-for-tab-command)
  14362. (unless (eolp) (insert "\n")))
  14363. ;; Clear marker, whatever the outcome of insertion is.
  14364. (set-marker rend nil)))))))
  14365. (defvar org-property-set-functions-alist nil
  14366. "Property set function alist.
  14367. Each entry should have the following format:
  14368. (PROPERTY . READ-FUNCTION)
  14369. The read function will be called with the same argument as
  14370. `org-completing-read'.")
  14371. (defun org-set-property-function (property)
  14372. "Get the function that should be used to set PROPERTY.
  14373. This is computed according to `org-property-set-functions-alist'."
  14374. (or (cdr (assoc property org-property-set-functions-alist))
  14375. 'org-completing-read))
  14376. (defun org-read-property-value (property)
  14377. "Read PROPERTY value from user."
  14378. (let* ((completion-ignore-case t)
  14379. (allowed (org-property-get-allowed-values nil property 'table))
  14380. (cur (org-entry-get nil property))
  14381. (prompt (concat property " value"
  14382. (if (and cur (string-match "\\S-" cur))
  14383. (concat " [" cur "]") "") ": "))
  14384. (set-function (org-set-property-function property))
  14385. (val (if allowed
  14386. (funcall set-function prompt allowed nil
  14387. (not (get-text-property 0 'org-unrestricted
  14388. (caar allowed))))
  14389. (let (org-completion-use-ido org-completion-use-iswitchb)
  14390. (funcall set-function prompt
  14391. (mapcar 'list (org-property-values property))
  14392. nil nil "" nil cur)))))
  14393. (if (equal val "")
  14394. cur
  14395. val)))
  14396. (defvar org-last-set-property nil)
  14397. (defvar org-last-set-property-value nil)
  14398. (defun org-read-property-name ()
  14399. "Read a property name."
  14400. (let* ((completion-ignore-case t)
  14401. (keys (org-buffer-property-keys nil t t))
  14402. (default-prop (or (save-excursion
  14403. (save-match-data
  14404. (beginning-of-line)
  14405. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14406. (null (string= (match-string 1) "END"))
  14407. (match-string 1))))
  14408. org-last-set-property))
  14409. (property (org-icompleting-read
  14410. (concat "Property"
  14411. (if default-prop (concat " [" default-prop "]") "")
  14412. ": ")
  14413. (mapcar 'list keys)
  14414. nil nil nil nil
  14415. default-prop)))
  14416. (if (member property keys)
  14417. property
  14418. (or (cdr (assoc (downcase property)
  14419. (mapcar (lambda (x) (cons (downcase x) x))
  14420. keys)))
  14421. property))))
  14422. (defun org-set-property-and-value (use-last)
  14423. "Allow to set [PROPERTY]: [value] direction from prompt.
  14424. When use-default, don't even ask, just use the last
  14425. \"[PROPERTY]: [value]\" string from the history."
  14426. (interactive "P")
  14427. (let* ((completion-ignore-case t)
  14428. (pv (or (and use-last org-last-set-property-value)
  14429. (org-completing-read
  14430. "Enter a \"[Property]: [value]\" pair: "
  14431. nil nil nil nil nil
  14432. org-last-set-property-value)))
  14433. prop val)
  14434. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14435. (setq prop (match-string 1 pv)
  14436. val (match-string 2 pv))
  14437. (org-set-property prop val))))
  14438. (defun org-set-property (property value)
  14439. "In the current entry, set PROPERTY to VALUE.
  14440. When called interactively, this will prompt for a property name, offering
  14441. completion on existing and default properties. And then it will prompt
  14442. for a value, offering completion either on allowed values (via an inherited
  14443. xxx_ALL property) or on existing values in other instances of this property
  14444. in the current file."
  14445. (interactive (list nil nil))
  14446. (let* ((property (or property (org-read-property-name)))
  14447. (value (or value (org-read-property-value property)))
  14448. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14449. (setq org-last-set-property property)
  14450. (setq org-last-set-property-value (concat property ": " value))
  14451. ;; Possibly postprocess the inserted value:
  14452. (when fn (setq value (funcall fn value)))
  14453. (unless (equal (org-entry-get nil property) value)
  14454. (org-entry-put nil property value))))
  14455. (defun org-delete-property (property)
  14456. "In the current entry, delete PROPERTY."
  14457. (interactive
  14458. (let* ((completion-ignore-case t)
  14459. (prop (org-icompleting-read "Property: "
  14460. (org-entry-properties nil 'standard))))
  14461. (list prop)))
  14462. (message "Property %s %s" property
  14463. (if (org-entry-delete nil property)
  14464. "deleted"
  14465. "was not present in the entry")))
  14466. (defun org-delete-property-globally (property)
  14467. "Remove PROPERTY globally, from all entries."
  14468. (interactive
  14469. (let* ((completion-ignore-case t)
  14470. (prop (org-icompleting-read
  14471. "Globally remove property: "
  14472. (mapcar 'list (org-buffer-property-keys)))))
  14473. (list prop)))
  14474. (save-excursion
  14475. (save-restriction
  14476. (widen)
  14477. (goto-char (point-min))
  14478. (let ((cnt 0))
  14479. (while (re-search-forward
  14480. (org-re-property property)
  14481. nil t)
  14482. (setq cnt (1+ cnt))
  14483. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14484. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14485. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14486. (defun org-compute-property-at-point ()
  14487. "Compute the property at point.
  14488. This looks for an enclosing column format, extracts the operator and
  14489. then applies it to the property in the column format's scope."
  14490. (interactive)
  14491. (unless (org-at-property-p)
  14492. (user-error "Not at a property"))
  14493. (let ((prop (org-match-string-no-properties 2)))
  14494. (org-columns-get-format-and-top-level)
  14495. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14496. (user-error "No operator defined for property %s" prop))
  14497. (org-columns-compute prop)))
  14498. (defvar org-property-allowed-value-functions nil
  14499. "Hook for functions supplying allowed values for a specific property.
  14500. The functions must take a single argument, the name of the property, and
  14501. return a flat list of allowed values. If \":ETC\" is one of
  14502. the values, this means that these values are intended as defaults for
  14503. completion, but that other values should be allowed too.
  14504. The functions must return nil if they are not responsible for this
  14505. property.")
  14506. (defun org-property-get-allowed-values (pom property &optional table)
  14507. "Get allowed values for the property PROPERTY.
  14508. When TABLE is non-nil, return an alist that can directly be used for
  14509. completion."
  14510. (let (vals)
  14511. (cond
  14512. ((equal property "TODO")
  14513. (setq vals (org-with-point-at pom
  14514. (append org-todo-keywords-1 '("")))))
  14515. ((equal property "PRIORITY")
  14516. (let ((n org-lowest-priority))
  14517. (while (>= n org-highest-priority)
  14518. (push (char-to-string n) vals)
  14519. (setq n (1- n)))))
  14520. ((member property org-special-properties))
  14521. ((setq vals (run-hook-with-args-until-success
  14522. 'org-property-allowed-value-functions property)))
  14523. (t
  14524. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14525. (when (and vals (string-match "\\S-" vals))
  14526. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14527. (setq vals (mapcar (lambda (x)
  14528. (cond ((stringp x) x)
  14529. ((numberp x) (number-to-string x))
  14530. ((symbolp x) (symbol-name x))
  14531. (t "???")))
  14532. vals)))))
  14533. (when (member ":ETC" vals)
  14534. (setq vals (remove ":ETC" vals))
  14535. (org-add-props (car vals) '(org-unrestricted t)))
  14536. (if table (mapcar 'list vals) vals)))
  14537. (defun org-property-previous-allowed-value (&optional previous)
  14538. "Switch to the next allowed value for this property."
  14539. (interactive)
  14540. (org-property-next-allowed-value t))
  14541. (defun org-property-next-allowed-value (&optional previous)
  14542. "Switch to the next allowed value for this property."
  14543. (interactive)
  14544. (unless (org-at-property-p)
  14545. (user-error "Not at a property"))
  14546. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14547. (key (match-string 2))
  14548. (value (match-string 3))
  14549. (allowed (or (org-property-get-allowed-values (point) key)
  14550. (and (member value '("[ ]" "[-]" "[X]"))
  14551. '("[ ]" "[X]"))))
  14552. (heading (save-match-data (nth 4 (org-heading-components))))
  14553. nval)
  14554. (unless allowed
  14555. (user-error "Allowed values for this property have not been defined"))
  14556. (if previous (setq allowed (reverse allowed)))
  14557. (if (member value allowed)
  14558. (setq nval (car (cdr (member value allowed)))))
  14559. (setq nval (or nval (car allowed)))
  14560. (if (equal nval value)
  14561. (user-error "Only one allowed value for this property"))
  14562. (org-at-property-p)
  14563. (replace-match (concat " :" key ": " nval) t t)
  14564. (org-indent-line)
  14565. (beginning-of-line 1)
  14566. (skip-chars-forward " \t")
  14567. (when (equal prop org-effort-property)
  14568. (save-excursion
  14569. (org-back-to-heading t)
  14570. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14571. (when (string= org-clock-current-task heading)
  14572. (setq org-clock-effort nval)
  14573. (org-clock-update-mode-line)))
  14574. (run-hook-with-args 'org-property-changed-functions key nval)))
  14575. (defun org-find-olp (path &optional this-buffer)
  14576. "Return a marker pointing to the entry at outline path OLP.
  14577. If anything goes wrong, throw an error.
  14578. You can wrap this call to catch the error like this:
  14579. (condition-case msg
  14580. (org-mobile-locate-entry (match-string 4))
  14581. (error (nth 1 msg)))
  14582. The return value will then be either a string with the error message,
  14583. or a marker if everything is OK.
  14584. If THIS-BUFFER is set, the outline path does not contain a file,
  14585. only headings."
  14586. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14587. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14588. (level 1)
  14589. (lmin 1)
  14590. (lmax 1)
  14591. limit re end found pos heading cnt flevel)
  14592. (unless buffer (error "File not found :%s" file))
  14593. (with-current-buffer buffer
  14594. (save-excursion
  14595. (save-restriction
  14596. (widen)
  14597. (setq limit (point-max))
  14598. (goto-char (point-min))
  14599. (while (setq heading (pop path))
  14600. (setq re (format org-complex-heading-regexp-format
  14601. (regexp-quote heading)))
  14602. (setq cnt 0 pos (point))
  14603. (while (re-search-forward re end t)
  14604. (setq level (- (match-end 1) (match-beginning 1)))
  14605. (if (and (>= level lmin) (<= level lmax))
  14606. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14607. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14608. lmax heading))
  14609. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14610. lmax heading))
  14611. (goto-char found)
  14612. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14613. (setq end (save-excursion (org-end-of-subtree t t))))
  14614. (when (org-at-heading-p)
  14615. (point-marker)))))))
  14616. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14617. "Find node HEADING in BUFFER.
  14618. Return a marker to the heading if it was found, or nil if not.
  14619. If POS-ONLY is set, return just the position instead of a marker.
  14620. The heading text must match exact, but it may have a TODO keyword,
  14621. a priority cookie and tags in the standard locations."
  14622. (with-current-buffer (or buffer (current-buffer))
  14623. (save-excursion
  14624. (save-restriction
  14625. (widen)
  14626. (goto-char (point-min))
  14627. (let (case-fold-search)
  14628. (if (re-search-forward
  14629. (format org-complex-heading-regexp-format
  14630. (regexp-quote heading)) nil t)
  14631. (if pos-only
  14632. (match-beginning 0)
  14633. (move-marker (make-marker) (match-beginning 0)))))))))
  14634. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14635. "Find Org node headline HEADING in all .org files in directory DIR.
  14636. When the target headline is found, return a marker to this location."
  14637. (let ((files (directory-files (or dir default-directory)
  14638. nil "\\`[^.#].*\\.org\\'"))
  14639. file visiting m buffer)
  14640. (catch 'found
  14641. (while (setq file (pop files))
  14642. (message "trying %s" file)
  14643. (setq visiting (org-find-base-buffer-visiting file))
  14644. (setq buffer (or visiting (find-file-noselect file)))
  14645. (setq m (org-find-exact-headline-in-buffer
  14646. heading buffer))
  14647. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14648. (and m (throw 'found m))))))
  14649. (defun org-find-entry-with-id (ident)
  14650. "Locate the entry that contains the ID property with exact value IDENT.
  14651. IDENT can be a string, a symbol or a number, this function will search for
  14652. the string representation of it.
  14653. Return the position where this entry starts, or nil if there is no such entry."
  14654. (interactive "sID: ")
  14655. (let ((id (cond
  14656. ((stringp ident) ident)
  14657. ((symbol-name ident) (symbol-name ident))
  14658. ((numberp ident) (number-to-string ident))
  14659. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14660. (case-fold-search nil))
  14661. (save-excursion
  14662. (save-restriction
  14663. (widen)
  14664. (goto-char (point-min))
  14665. (when (re-search-forward
  14666. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14667. nil t)
  14668. (org-back-to-heading t)
  14669. (point))))))
  14670. ;;;; Timestamps
  14671. (defvar org-last-changed-timestamp nil)
  14672. (defvar org-last-inserted-timestamp nil
  14673. "The last time stamp inserted with `org-insert-time-stamp'.")
  14674. (defvar org-ts-what) ; dynamically scoped parameter
  14675. (defun org-time-stamp (arg &optional inactive)
  14676. "Prompt for a date/time and insert a time stamp.
  14677. If the user specifies a time like HH:MM or if this command is
  14678. called with at least one prefix argument, the time stamp contains
  14679. the date and the time. Otherwise, only the date is be included.
  14680. All parts of a date not specified by the user is filled in from
  14681. the current date/time. So if you just press return without
  14682. typing anything, the time stamp will represent the current
  14683. date/time.
  14684. If there is already a timestamp at the cursor, it will be
  14685. modified.
  14686. With two universal prefix arguments, insert an active timestamp
  14687. with the current time without prompting the user.
  14688. When called from lisp, the timestamp is inactive if INACTIVE is
  14689. non-nil."
  14690. (interactive "P")
  14691. (let* ((ts nil)
  14692. (default-time
  14693. ;; Default time is either today, or, when entering a range,
  14694. ;; the range start.
  14695. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14696. (save-excursion
  14697. (re-search-backward
  14698. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14699. (- (point) 20) t)))
  14700. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14701. (current-time)))
  14702. (default-input (and ts (org-get-compact-tod ts)))
  14703. (repeater (save-excursion
  14704. (save-match-data
  14705. (beginning-of-line)
  14706. (when (re-search-forward
  14707. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14708. (save-excursion (progn (end-of-line) (point))) t)
  14709. (match-string 0)))))
  14710. org-time-was-given org-end-time-was-given time)
  14711. (cond
  14712. ((and (org-at-timestamp-p t)
  14713. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14714. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14715. (insert "--")
  14716. (setq time (let ((this-command this-command))
  14717. (org-read-date arg 'totime nil nil
  14718. default-time default-input inactive)))
  14719. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14720. ((org-at-timestamp-p t)
  14721. (setq time (let ((this-command this-command))
  14722. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14723. (when (org-at-timestamp-p t) ; just to get the match data
  14724. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14725. (replace-match "")
  14726. (setq org-last-changed-timestamp
  14727. (org-insert-time-stamp
  14728. time (or org-time-was-given arg)
  14729. inactive nil nil (list org-end-time-was-given)))
  14730. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14731. (setq org-last-changed-timestamp
  14732. (concat (substring org-last-inserted-timestamp 0 -1)
  14733. " " repeater ">"))))
  14734. (message "Timestamp updated"))
  14735. ((equal arg '(16))
  14736. (org-insert-time-stamp (current-time) t inactive))
  14737. (t
  14738. (setq time (let ((this-command this-command))
  14739. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14740. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14741. nil nil (list org-end-time-was-given))))))
  14742. ;; FIXME: can we use this for something else, like computing time differences?
  14743. (defun org-get-compact-tod (s)
  14744. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14745. (let* ((t1 (match-string 1 s))
  14746. (h1 (string-to-number (match-string 2 s)))
  14747. (m1 (string-to-number (match-string 3 s)))
  14748. (t2 (and (match-end 4) (match-string 5 s)))
  14749. (h2 (and t2 (string-to-number (match-string 6 s))))
  14750. (m2 (and t2 (string-to-number (match-string 7 s))))
  14751. dh dm)
  14752. (if (not t2)
  14753. t1
  14754. (setq dh (- h2 h1) dm (- m2 m1))
  14755. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14756. (concat t1 "+" (number-to-string dh)
  14757. (and (/= 0 dm) (format ":%02d" dm)))))))
  14758. (defun org-time-stamp-inactive (&optional arg)
  14759. "Insert an inactive time stamp.
  14760. An inactive time stamp is enclosed in square brackets instead of angle
  14761. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14762. does not link to the calendar and cannot be changed with the S-cursor keys.
  14763. So these are more for recording a certain time/date."
  14764. (interactive "P")
  14765. (org-time-stamp arg 'inactive))
  14766. (defvar org-date-ovl (make-overlay 1 1))
  14767. (overlay-put org-date-ovl 'face 'org-date-selected)
  14768. (org-detach-overlay org-date-ovl)
  14769. (defvar org-ans1) ; dynamically scoped parameter
  14770. (defvar org-ans2) ; dynamically scoped parameter
  14771. (defvar org-plain-time-of-day-regexp) ; defined below
  14772. (defvar org-overriding-default-time nil) ; dynamically scoped
  14773. (defvar org-read-date-overlay nil)
  14774. (defvar org-dcst nil) ; dynamically scoped
  14775. (defvar org-read-date-history nil)
  14776. (defvar org-read-date-final-answer nil)
  14777. (defvar org-read-date-analyze-futurep nil)
  14778. (defvar org-read-date-analyze-forced-year nil)
  14779. (defvar org-read-date-inactive)
  14780. (defvar org-read-date-minibuffer-local-map
  14781. (let* ((org-replace-disputed-keys nil)
  14782. (map (make-sparse-keymap)))
  14783. (set-keymap-parent map minibuffer-local-map)
  14784. (org-defkey map (kbd ".")
  14785. (lambda () (interactive)
  14786. ;; Are we at the beginning of the prompt?
  14787. (if (looking-back "^[^:]+: ")
  14788. (org-eval-in-calendar '(calendar-goto-today))
  14789. (insert "."))))
  14790. (org-defkey map (kbd "C-.")
  14791. (lambda () (interactive)
  14792. (org-eval-in-calendar '(calendar-goto-today))))
  14793. (org-defkey map [(meta shift left)]
  14794. (lambda () (interactive)
  14795. (org-eval-in-calendar '(calendar-backward-month 1))))
  14796. (org-defkey map [(meta shift right)]
  14797. (lambda () (interactive)
  14798. (org-eval-in-calendar '(calendar-forward-month 1))))
  14799. (org-defkey map [(meta shift up)]
  14800. (lambda () (interactive)
  14801. (org-eval-in-calendar '(calendar-backward-year 1))))
  14802. (org-defkey map [(meta shift down)]
  14803. (lambda () (interactive)
  14804. (org-eval-in-calendar '(calendar-forward-year 1))))
  14805. (org-defkey map [?\e (shift left)]
  14806. (lambda () (interactive)
  14807. (org-eval-in-calendar '(calendar-backward-month 1))))
  14808. (org-defkey map [?\e (shift right)]
  14809. (lambda () (interactive)
  14810. (org-eval-in-calendar '(calendar-forward-month 1))))
  14811. (org-defkey map [?\e (shift up)]
  14812. (lambda () (interactive)
  14813. (org-eval-in-calendar '(calendar-backward-year 1))))
  14814. (org-defkey map [?\e (shift down)]
  14815. (lambda () (interactive)
  14816. (org-eval-in-calendar '(calendar-forward-year 1))))
  14817. (org-defkey map [(shift up)]
  14818. (lambda () (interactive)
  14819. (org-eval-in-calendar '(calendar-backward-week 1))))
  14820. (org-defkey map [(shift down)]
  14821. (lambda () (interactive)
  14822. (org-eval-in-calendar '(calendar-forward-week 1))))
  14823. (org-defkey map [(shift left)]
  14824. (lambda () (interactive)
  14825. (org-eval-in-calendar '(calendar-backward-day 1))))
  14826. (org-defkey map [(shift right)]
  14827. (lambda () (interactive)
  14828. (org-eval-in-calendar '(calendar-forward-day 1))))
  14829. (org-defkey map "!"
  14830. (lambda () (interactive)
  14831. (org-eval-in-calendar '(diary-view-entries))
  14832. (message "")))
  14833. (org-defkey map ">"
  14834. (lambda () (interactive)
  14835. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14836. (org-defkey map "<"
  14837. (lambda () (interactive)
  14838. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14839. (org-defkey map "\C-v"
  14840. (lambda () (interactive)
  14841. (org-eval-in-calendar
  14842. '(calendar-scroll-left-three-months 1))))
  14843. (org-defkey map "\M-v"
  14844. (lambda () (interactive)
  14845. (org-eval-in-calendar
  14846. '(calendar-scroll-right-three-months 1))))
  14847. map)
  14848. "Keymap for minibuffer commands when using `org-read-date'.")
  14849. (defvar org-def)
  14850. (defvar org-defdecode)
  14851. (defvar org-with-time)
  14852. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14853. default-time default-input inactive)
  14854. "Read a date, possibly a time, and make things smooth for the user.
  14855. The prompt will suggest to enter an ISO date, but you can also enter anything
  14856. which will at least partially be understood by `parse-time-string'.
  14857. Unrecognized parts of the date will default to the current day, month, year,
  14858. hour and minute. If this command is called to replace a timestamp at point,
  14859. or to enter the second timestamp of a range, the default time is taken
  14860. from the existing stamp. Furthermore, the command prefers the future,
  14861. so if you are giving a date where the year is not given, and the day-month
  14862. combination is already past in the current year, it will assume you
  14863. mean next year. For details, see the manual. A few examples:
  14864. 3-2-5 --> 2003-02-05
  14865. feb 15 --> currentyear-02-15
  14866. 2/15 --> currentyear-02-15
  14867. sep 12 9 --> 2009-09-12
  14868. 12:45 --> today 12:45
  14869. 22 sept 0:34 --> currentyear-09-22 0:34
  14870. 12 --> currentyear-currentmonth-12
  14871. Fri --> nearest Friday after today
  14872. -Tue --> last Tuesday
  14873. etc.
  14874. Furthermore you can specify a relative date by giving, as the *first* thing
  14875. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14876. change in days weeks, months, years.
  14877. With a single plus or minus, the date is relative to today. With a double
  14878. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14879. +4d --> four days from today
  14880. +4 --> same as above
  14881. +2w --> two weeks from today
  14882. ++5 --> five days from default date
  14883. The function understands only English month and weekday abbreviations.
  14884. While prompting, a calendar is popped up - you can also select the
  14885. date with the mouse (button 1). The calendar shows a period of three
  14886. months. To scroll it to other months, use the keys `>' and `<'.
  14887. If you don't like the calendar, turn it off with
  14888. \(setq org-read-date-popup-calendar nil)
  14889. With optional argument TO-TIME, the date will immediately be converted
  14890. to an internal time.
  14891. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14892. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14893. still enter a time, and this function will inform the calling routine
  14894. about this change. The calling routine may then choose to change the
  14895. format used to insert the time stamp into the buffer to include the time.
  14896. With optional argument FROM-STRING, read from this string instead from
  14897. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14898. the time/date that is used for everything that is not specified by the
  14899. user."
  14900. (require 'parse-time)
  14901. (let* ((org-time-stamp-rounding-minutes
  14902. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14903. (org-dcst org-display-custom-times)
  14904. (ct (org-current-time))
  14905. (org-def (or org-overriding-default-time default-time ct))
  14906. (org-defdecode (decode-time org-def))
  14907. (dummy (progn
  14908. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14909. (setcar (nthcdr 2 org-defdecode) -1)
  14910. (setcar (nthcdr 1 org-defdecode) 59)
  14911. (setq org-def (apply 'encode-time org-defdecode)
  14912. org-defdecode (decode-time org-def)))))
  14913. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14914. (calendar-frame-setup nil)
  14915. (calendar-setup nil)
  14916. (calendar-move-hook nil)
  14917. (calendar-view-diary-initially-flag nil)
  14918. (calendar-view-holidays-initially-flag nil)
  14919. (timestr (format-time-string
  14920. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14921. (prompt (concat (if prompt (concat prompt " ") "")
  14922. (format "Date+time [%s]: " timestr)))
  14923. ans (org-ans0 "") org-ans1 org-ans2 final)
  14924. (cond
  14925. (from-string (setq ans from-string))
  14926. (org-read-date-popup-calendar
  14927. (save-excursion
  14928. (save-window-excursion
  14929. (calendar)
  14930. (org-eval-in-calendar '(setq cursor-type nil) t)
  14931. (unwind-protect
  14932. (progn
  14933. (calendar-forward-day (- (time-to-days org-def)
  14934. (calendar-absolute-from-gregorian
  14935. (calendar-current-date))))
  14936. (org-eval-in-calendar nil t)
  14937. (let* ((old-map (current-local-map))
  14938. (map (copy-keymap calendar-mode-map))
  14939. (minibuffer-local-map
  14940. (copy-keymap org-read-date-minibuffer-local-map)))
  14941. (org-defkey map (kbd "RET") 'org-calendar-select)
  14942. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14943. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14944. (unwind-protect
  14945. (progn
  14946. (use-local-map map)
  14947. (setq org-read-date-inactive inactive)
  14948. (add-hook 'post-command-hook 'org-read-date-display)
  14949. (setq org-ans0 (read-string prompt default-input
  14950. 'org-read-date-history nil))
  14951. ;; org-ans0: from prompt
  14952. ;; org-ans1: from mouse click
  14953. ;; org-ans2: from calendar motion
  14954. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14955. (remove-hook 'post-command-hook 'org-read-date-display)
  14956. (use-local-map old-map)
  14957. (when org-read-date-overlay
  14958. (delete-overlay org-read-date-overlay)
  14959. (setq org-read-date-overlay nil)))))
  14960. (bury-buffer "*Calendar*")))))
  14961. (t ; Naked prompt only
  14962. (unwind-protect
  14963. (setq ans (read-string prompt default-input
  14964. 'org-read-date-history timestr))
  14965. (when org-read-date-overlay
  14966. (delete-overlay org-read-date-overlay)
  14967. (setq org-read-date-overlay nil)))))
  14968. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14969. (when org-read-date-analyze-forced-year
  14970. (message "Year was forced into %s"
  14971. (if org-read-date-force-compatible-dates
  14972. "compatible range (1970-2037)"
  14973. "range representable on this machine"))
  14974. (ding))
  14975. ;; One round trip to get rid of 34th of August and stuff like that....
  14976. (setq final (decode-time (apply 'encode-time final)))
  14977. (setq org-read-date-final-answer ans)
  14978. (if to-time
  14979. (apply 'encode-time final)
  14980. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14981. (format "%04d-%02d-%02d %02d:%02d"
  14982. (nth 5 final) (nth 4 final) (nth 3 final)
  14983. (nth 2 final) (nth 1 final))
  14984. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14985. (defun org-read-date-display ()
  14986. "Display the current date prompt interpretation in the minibuffer."
  14987. (when org-read-date-display-live
  14988. (when org-read-date-overlay
  14989. (delete-overlay org-read-date-overlay))
  14990. (when (minibufferp (current-buffer))
  14991. (save-excursion
  14992. (end-of-line 1)
  14993. (while (not (equal (buffer-substring
  14994. (max (point-min) (- (point) 4)) (point))
  14995. " "))
  14996. (insert " ")))
  14997. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14998. " " (or org-ans1 org-ans2)))
  14999. (org-end-time-was-given nil)
  15000. (f (org-read-date-analyze ans org-def org-defdecode))
  15001. (fmts (if org-dcst
  15002. org-time-stamp-custom-formats
  15003. org-time-stamp-formats))
  15004. (fmt (if (or org-with-time
  15005. (and (boundp 'org-time-was-given) org-time-was-given))
  15006. (cdr fmts)
  15007. (car fmts)))
  15008. (txt (format-time-string fmt (apply 'encode-time f)))
  15009. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15010. (txt (concat "=> " txt)))
  15011. (when (and org-end-time-was-given
  15012. (string-match org-plain-time-of-day-regexp txt))
  15013. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15014. org-end-time-was-given
  15015. (substring txt (match-end 0)))))
  15016. (when org-read-date-analyze-futurep
  15017. (setq txt (concat txt " (=>F)")))
  15018. (setq org-read-date-overlay
  15019. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15020. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15021. (defun org-read-date-analyze (ans org-def org-defdecode)
  15022. "Analyze the combined answer of the date prompt."
  15023. ;; FIXME: cleanup and comment
  15024. (let ((nowdecode (decode-time (current-time)))
  15025. delta deltan deltaw deltadef year month day
  15026. hour minute second wday pm h2 m2 tl wday1
  15027. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15028. (setq org-read-date-analyze-futurep nil
  15029. org-read-date-analyze-forced-year nil)
  15030. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15031. (setq ans "+0"))
  15032. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15033. (setq ans (replace-match "" t t ans)
  15034. deltan (car delta)
  15035. deltaw (nth 1 delta)
  15036. deltadef (nth 2 delta)))
  15037. ;; Check if there is an iso week date in there. If yes, store the
  15038. ;; info and postpone interpreting it until the rest of the parsing
  15039. ;; is done.
  15040. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15041. (setq iso-year (if (match-end 1)
  15042. (org-small-year-to-year
  15043. (string-to-number (match-string 1 ans))))
  15044. iso-weekday (if (match-end 3)
  15045. (string-to-number (match-string 3 ans)))
  15046. iso-week (string-to-number (match-string 2 ans)))
  15047. (setq ans (replace-match "" t t ans)))
  15048. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15049. (when (string-match
  15050. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15051. (setq year (if (match-end 2)
  15052. (string-to-number (match-string 2 ans))
  15053. (progn (setq kill-year t)
  15054. (string-to-number (format-time-string "%Y"))))
  15055. month (string-to-number (match-string 3 ans))
  15056. day (string-to-number (match-string 4 ans)))
  15057. (if (< year 100) (setq year (+ 2000 year)))
  15058. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15059. t nil ans)))
  15060. ;; Help matching dotted european dates
  15061. (when (string-match
  15062. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15063. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15064. (setq kill-year t)
  15065. (string-to-number (format-time-string "%Y")))
  15066. day (string-to-number (match-string 1 ans))
  15067. month (string-to-number (match-string 2 ans))
  15068. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15069. t nil ans)))
  15070. ;; Help matching american dates, like 5/30 or 5/30/7
  15071. (when (string-match
  15072. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15073. (setq year (if (match-end 4)
  15074. (string-to-number (match-string 4 ans))
  15075. (progn (setq kill-year t)
  15076. (string-to-number (format-time-string "%Y"))))
  15077. month (string-to-number (match-string 1 ans))
  15078. day (string-to-number (match-string 2 ans)))
  15079. (if (< year 100) (setq year (+ 2000 year)))
  15080. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15081. t nil ans)))
  15082. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15083. ;; If there is a time with am/pm, and *no* time without it, we convert
  15084. ;; so that matching will be successful.
  15085. (loop for i from 1 to 2 do ; twice, for end time as well
  15086. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15087. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15088. (setq hour (string-to-number (match-string 1 ans))
  15089. minute (if (match-end 3)
  15090. (string-to-number (match-string 3 ans))
  15091. 0)
  15092. pm (equal ?p
  15093. (string-to-char (downcase (match-string 4 ans)))))
  15094. (if (and (= hour 12) (not pm))
  15095. (setq hour 0)
  15096. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15097. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15098. t t ans))))
  15099. ;; Check if a time range is given as a duration
  15100. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15101. (setq hour (string-to-number (match-string 1 ans))
  15102. h2 (+ hour (string-to-number (match-string 3 ans)))
  15103. minute (string-to-number (match-string 2 ans))
  15104. m2 (+ minute (if (match-end 5) (string-to-number
  15105. (match-string 5 ans))0)))
  15106. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15107. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15108. t t ans)))
  15109. ;; Check if there is a time range
  15110. (when (boundp 'org-end-time-was-given)
  15111. (setq org-time-was-given nil)
  15112. (when (and (string-match org-plain-time-of-day-regexp ans)
  15113. (match-end 8))
  15114. (setq org-end-time-was-given (match-string 8 ans))
  15115. (setq ans (concat (substring ans 0 (match-beginning 7))
  15116. (substring ans (match-end 7))))))
  15117. (setq tl (parse-time-string ans)
  15118. day (or (nth 3 tl) (nth 3 org-defdecode))
  15119. month (or (nth 4 tl)
  15120. (if (and org-read-date-prefer-future
  15121. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15122. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15123. (nth 4 org-defdecode)))
  15124. year (or (and (not kill-year) (nth 5 tl))
  15125. (if (and org-read-date-prefer-future
  15126. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15127. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15128. (nth 5 org-defdecode)))
  15129. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15130. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15131. second (or (nth 0 tl) 0)
  15132. wday (nth 6 tl))
  15133. (when (and (eq org-read-date-prefer-future 'time)
  15134. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15135. (equal day (nth 3 nowdecode))
  15136. (equal month (nth 4 nowdecode))
  15137. (equal year (nth 5 nowdecode))
  15138. (nth 2 tl)
  15139. (or (< (nth 2 tl) (nth 2 nowdecode))
  15140. (and (= (nth 2 tl) (nth 2 nowdecode))
  15141. (nth 1 tl)
  15142. (< (nth 1 tl) (nth 1 nowdecode)))))
  15143. (setq day (1+ day)
  15144. futurep t))
  15145. ;; Special date definitions below
  15146. (cond
  15147. (iso-week
  15148. ;; There was an iso week
  15149. (require 'cal-iso)
  15150. (setq futurep nil)
  15151. (setq year (or iso-year year)
  15152. day (or iso-weekday wday 1)
  15153. wday nil ; to make sure that the trigger below does not match
  15154. iso-date (calendar-gregorian-from-absolute
  15155. (calendar-absolute-from-iso
  15156. (list iso-week day year))))
  15157. ; FIXME: Should we also push ISO weeks into the future?
  15158. ; (when (and org-read-date-prefer-future
  15159. ; (not iso-year)
  15160. ; (< (calendar-absolute-from-gregorian iso-date)
  15161. ; (time-to-days (current-time))))
  15162. ; (setq year (1+ year)
  15163. ; iso-date (calendar-gregorian-from-absolute
  15164. ; (calendar-absolute-from-iso
  15165. ; (list iso-week day year)))))
  15166. (setq month (car iso-date)
  15167. year (nth 2 iso-date)
  15168. day (nth 1 iso-date)))
  15169. (deltan
  15170. (setq futurep nil)
  15171. (unless deltadef
  15172. (let ((now (decode-time (current-time))))
  15173. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15174. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15175. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15176. ((equal deltaw "m") (setq month (+ month deltan)))
  15177. ((equal deltaw "y") (setq year (+ year deltan)))))
  15178. ((and wday (not (nth 3 tl)))
  15179. ;; Weekday was given, but no day, so pick that day in the week
  15180. ;; on or after the derived date.
  15181. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15182. (unless (equal wday wday1)
  15183. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15184. (if (and (boundp 'org-time-was-given)
  15185. (nth 2 tl))
  15186. (setq org-time-was-given t))
  15187. (if (< year 100) (setq year (+ 2000 year)))
  15188. ;; Check of the date is representable
  15189. (if org-read-date-force-compatible-dates
  15190. (progn
  15191. (if (< year 1970)
  15192. (setq year 1970 org-read-date-analyze-forced-year t))
  15193. (if (> year 2037)
  15194. (setq year 2037 org-read-date-analyze-forced-year t)))
  15195. (condition-case nil
  15196. (ignore (encode-time second minute hour day month year))
  15197. (error
  15198. (setq year (nth 5 org-defdecode))
  15199. (setq org-read-date-analyze-forced-year t))))
  15200. (setq org-read-date-analyze-futurep futurep)
  15201. (list second minute hour day month year)))
  15202. (defvar parse-time-weekdays)
  15203. (defun org-read-date-get-relative (s today default)
  15204. "Check string S for special relative date string.
  15205. TODAY and DEFAULT are internal times, for today and for a default.
  15206. Return shift list (N what def-flag)
  15207. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15208. N is the number of WHATs to shift.
  15209. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15210. the DEFAULT date rather than TODAY."
  15211. (require 'parse-time)
  15212. (when (and
  15213. (string-match
  15214. (concat
  15215. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15216. "\\([0-9]+\\)?"
  15217. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15218. "\\([ \t]\\|$\\)") s)
  15219. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15220. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15221. (string-to-char (substring (match-string 1 s) -1))
  15222. ?+))
  15223. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15224. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15225. (what (if (match-end 3) (match-string 3 s) "d"))
  15226. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15227. (date (if rel default today))
  15228. (wday (nth 6 (decode-time date)))
  15229. delta)
  15230. (if wday1
  15231. (progn
  15232. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15233. (if (= delta 0) (setq delta 7))
  15234. (if (= dir ?-)
  15235. (progn
  15236. (setq delta (- delta 7))
  15237. (if (= delta 0) (setq delta -7))))
  15238. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15239. (list delta "d" rel))
  15240. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15241. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15242. "Turn a user-specified date into the internal representation.
  15243. The internal representation needed by the calendar is (month day year).
  15244. This is a wrapper to handle the brain-dead convention in calendar that
  15245. user function argument order change dependent on argument order."
  15246. (if (boundp 'calendar-date-style)
  15247. (cond
  15248. ((eq calendar-date-style 'american)
  15249. (list arg1 arg2 arg3))
  15250. ((eq calendar-date-style 'european)
  15251. (list arg2 arg1 arg3))
  15252. ((eq calendar-date-style 'iso)
  15253. (list arg2 arg3 arg1)))
  15254. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15255. (if (org-bound-and-true-p european-calendar-style)
  15256. (list arg2 arg1 arg3)
  15257. (list arg1 arg2 arg3)))))
  15258. (defun org-eval-in-calendar (form &optional keepdate)
  15259. "Eval FORM in the calendar window and return to current window.
  15260. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15261. otherwise stick to the current value of `org-ans2'."
  15262. (let ((sf (selected-frame))
  15263. (sw (selected-window)))
  15264. (select-window (get-buffer-window "*Calendar*" t))
  15265. (eval form)
  15266. (when (and (not keepdate) (calendar-cursor-to-date))
  15267. (let* ((date (calendar-cursor-to-date))
  15268. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15269. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15270. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15271. (select-window sw)
  15272. (org-select-frame-set-input-focus sf)))
  15273. (defun org-calendar-select ()
  15274. "Return to `org-read-date' with the date currently selected.
  15275. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15276. (interactive)
  15277. (when (calendar-cursor-to-date)
  15278. (let* ((date (calendar-cursor-to-date))
  15279. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15280. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15281. (if (active-minibuffer-window) (exit-minibuffer))))
  15282. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15283. "Insert a date stamp for the date given by the internal TIME.
  15284. WITH-HM means use the stamp format that includes the time of the day.
  15285. INACTIVE means use square brackets instead of angular ones, so that the
  15286. stamp will not contribute to the agenda.
  15287. PRE and POST are optional strings to be inserted before and after the
  15288. stamp.
  15289. The command returns the inserted time stamp."
  15290. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15291. stamp)
  15292. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15293. (insert-before-markers (or pre ""))
  15294. (when (listp extra)
  15295. (setq extra (car extra))
  15296. (if (and (stringp extra)
  15297. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15298. (setq extra (format "-%02d:%02d"
  15299. (string-to-number (match-string 1 extra))
  15300. (string-to-number (match-string 2 extra))))
  15301. (setq extra nil)))
  15302. (when extra
  15303. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15304. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15305. (insert-before-markers (or post ""))
  15306. (setq org-last-inserted-timestamp stamp)))
  15307. (defun org-toggle-time-stamp-overlays ()
  15308. "Toggle the use of custom time stamp formats."
  15309. (interactive)
  15310. (setq org-display-custom-times (not org-display-custom-times))
  15311. (unless org-display-custom-times
  15312. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15313. (while (setq p (next-single-property-change p 'display))
  15314. (if (and (get-text-property p 'display)
  15315. (eq (get-text-property p 'face) 'org-date))
  15316. (remove-text-properties
  15317. p (setq p (next-single-property-change p 'display))
  15318. '(display t))))
  15319. (set-buffer-modified-p bmp)))
  15320. (if (featurep 'xemacs)
  15321. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15322. (org-restart-font-lock)
  15323. (setq org-table-may-need-update t)
  15324. (if org-display-custom-times
  15325. (message "Time stamps are overlaid with custom format")
  15326. (message "Time stamp overlays removed")))
  15327. (defun org-display-custom-time (beg end)
  15328. "Overlay modified time stamp format over timestamp between BEG and END."
  15329. (let* ((ts (buffer-substring beg end))
  15330. t1 w1 with-hm tf time str w2 (off 0))
  15331. (save-match-data
  15332. (setq t1 (org-parse-time-string ts t))
  15333. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15334. (setq off (- (match-end 0) (match-beginning 0)))))
  15335. (setq end (- end off))
  15336. (setq w1 (- end beg)
  15337. with-hm (and (nth 1 t1) (nth 2 t1))
  15338. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15339. time (org-fix-decoded-time t1)
  15340. str (org-add-props
  15341. (format-time-string
  15342. (substring tf 1 -1) (apply 'encode-time time))
  15343. nil 'mouse-face 'highlight)
  15344. w2 (length str))
  15345. (if (not (= w2 w1))
  15346. (add-text-properties (1+ beg) (+ 2 beg)
  15347. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15348. (if (featurep 'xemacs)
  15349. (progn
  15350. (put-text-property beg end 'invisible t)
  15351. (put-text-property beg end 'end-glyph (make-glyph str)))
  15352. (put-text-property beg end 'display str))))
  15353. (defun org-translate-time (string)
  15354. "Translate all timestamps in STRING to custom format.
  15355. But do this only if the variable `org-display-custom-times' is set."
  15356. (when org-display-custom-times
  15357. (save-match-data
  15358. (let* ((start 0)
  15359. (re org-ts-regexp-both)
  15360. t1 with-hm inactive tf time str beg end)
  15361. (while (setq start (string-match re string start))
  15362. (setq beg (match-beginning 0)
  15363. end (match-end 0)
  15364. t1 (save-match-data
  15365. (org-parse-time-string (substring string beg end) t))
  15366. with-hm (and (nth 1 t1) (nth 2 t1))
  15367. inactive (equal (substring string beg (1+ beg)) "[")
  15368. tf (funcall (if with-hm 'cdr 'car)
  15369. org-time-stamp-custom-formats)
  15370. time (org-fix-decoded-time t1)
  15371. str (format-time-string
  15372. (concat
  15373. (if inactive "[" "<") (substring tf 1 -1)
  15374. (if inactive "]" ">"))
  15375. (apply 'encode-time time))
  15376. string (replace-match str t t string)
  15377. start (+ start (length str)))))))
  15378. string)
  15379. (defun org-fix-decoded-time (time)
  15380. "Set 0 instead of nil for the first 6 elements of time.
  15381. Don't touch the rest."
  15382. (let ((n 0))
  15383. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15384. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15385. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15386. "Difference between TIMESTAMP-STRING and now in days.
  15387. If SECONDS is non-nil, return the difference in seconds."
  15388. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15389. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15390. (funcall fdiff (current-time)))))
  15391. (defun org-deadline-close (timestamp-string &optional ndays)
  15392. "Is the time in TIMESTAMP-STRING close to the current date?"
  15393. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15394. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15395. (not (org-entry-is-done-p))))
  15396. (defun org-get-wdays (ts &optional delay zero-delay)
  15397. "Get the deadline lead time appropriate for timestring TS.
  15398. When DELAY is non-nil, get the delay time for scheduled items
  15399. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15400. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15401. don't try to find the delay cookie in the scheduled timestamp."
  15402. (let ((tv (if delay org-scheduled-delay-days
  15403. org-deadline-warning-days)))
  15404. (cond
  15405. ((or (and delay (< tv 0))
  15406. (and delay zero-delay (<= tv 0))
  15407. (and (not delay) (<= tv 0)))
  15408. ;; Enforce this value no matter what
  15409. (- tv))
  15410. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15411. ;; lead time is specified.
  15412. (floor (* (string-to-number (match-string 1 ts))
  15413. (cdr (assoc (match-string 2 ts)
  15414. '(("d" . 1) ("w" . 7)
  15415. ("m" . 30.4) ("y" . 365.25)
  15416. ("h" . 0.041667)))))))
  15417. ;; go for the default.
  15418. (t tv))))
  15419. (defun org-calendar-select-mouse (ev)
  15420. "Return to `org-read-date' with the date currently selected.
  15421. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15422. (interactive "e")
  15423. (mouse-set-point ev)
  15424. (when (calendar-cursor-to-date)
  15425. (let* ((date (calendar-cursor-to-date))
  15426. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15427. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15428. (if (active-minibuffer-window) (exit-minibuffer))))
  15429. (defun org-check-deadlines (ndays)
  15430. "Check if there are any deadlines due or past due.
  15431. A deadline is considered due if it happens within `org-deadline-warning-days'
  15432. days from today's date. If the deadline appears in an entry marked DONE,
  15433. it is not shown. The prefix arg NDAYS can be used to test that many
  15434. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15435. (interactive "P")
  15436. (let* ((org-warn-days
  15437. (cond
  15438. ((equal ndays '(4)) 100000)
  15439. (ndays (prefix-numeric-value ndays))
  15440. (t (abs org-deadline-warning-days))))
  15441. (case-fold-search nil)
  15442. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15443. (callback
  15444. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15445. (message "%d deadlines past-due or due within %d days"
  15446. (org-occur regexp nil callback)
  15447. org-warn-days)))
  15448. (defsubst org-re-timestamp (type)
  15449. "Return a regexp for timestamp TYPE.
  15450. Allowed values for TYPE are:
  15451. all: all timestamps
  15452. active: only active timestamps (<...>)
  15453. inactive: only inactive timestamps ([...])
  15454. scheduled: only scheduled timestamps
  15455. deadline: only deadline timestamps
  15456. closed: only closed time-stamps
  15457. When TYPE is nil, fall back on returning a regexp that matches
  15458. both scheduled and deadline timestamps."
  15459. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15460. ((eq type 'active) org-ts-regexp)
  15461. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15462. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15463. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15464. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15465. ((eq type 'scheduled-or-deadline)
  15466. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15467. (defun org-check-before-date (date)
  15468. "Check if there are deadlines or scheduled entries before DATE."
  15469. (interactive (list (org-read-date)))
  15470. (let ((case-fold-search nil)
  15471. (regexp (org-re-timestamp org-ts-type))
  15472. (callback
  15473. (lambda () (time-less-p
  15474. (org-time-string-to-time (match-string 1))
  15475. (org-time-string-to-time date)))))
  15476. (message "%d entries before %s"
  15477. (org-occur regexp nil callback) date)))
  15478. (defun org-check-after-date (date)
  15479. "Check if there are deadlines or scheduled entries after DATE."
  15480. (interactive (list (org-read-date)))
  15481. (let ((case-fold-search nil)
  15482. (regexp (org-re-timestamp org-ts-type))
  15483. (callback
  15484. (lambda () (not
  15485. (time-less-p
  15486. (org-time-string-to-time (match-string 1))
  15487. (org-time-string-to-time date))))))
  15488. (message "%d entries after %s"
  15489. (org-occur regexp nil callback) date)))
  15490. (defun org-check-dates-range (start-date end-date)
  15491. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15492. (interactive (list (org-read-date nil nil nil "Range starts")
  15493. (org-read-date nil nil nil "Range end")))
  15494. (let ((case-fold-search nil)
  15495. (regexp (org-re-timestamp org-ts-type))
  15496. (callback
  15497. (lambda ()
  15498. (let ((match (match-string 1)))
  15499. (and
  15500. (not (time-less-p
  15501. (org-time-string-to-time match)
  15502. (org-time-string-to-time start-date)))
  15503. (time-less-p
  15504. (org-time-string-to-time match)
  15505. (org-time-string-to-time end-date)))))))
  15506. (message "%d entries between %s and %s"
  15507. (org-occur regexp nil callback) start-date end-date)))
  15508. (defun org-evaluate-time-range (&optional to-buffer)
  15509. "Evaluate a time range by computing the difference between start and end.
  15510. Normally the result is just printed in the echo area, but with prefix arg
  15511. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15512. If the time range is actually in a table, the result is inserted into the
  15513. next column.
  15514. For time difference computation, a year is assumed to be exactly 365
  15515. days in order to avoid rounding problems."
  15516. (interactive "P")
  15517. (or
  15518. (org-clock-update-time-maybe)
  15519. (save-excursion
  15520. (unless (org-at-date-range-p t)
  15521. (goto-char (point-at-bol))
  15522. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15523. (if (not (org-at-date-range-p t))
  15524. (user-error "Not at a time-stamp range, and none found in current line")))
  15525. (let* ((ts1 (match-string 1))
  15526. (ts2 (match-string 2))
  15527. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15528. (match-end (match-end 0))
  15529. (time1 (org-time-string-to-time ts1))
  15530. (time2 (org-time-string-to-time ts2))
  15531. (t1 (org-float-time time1))
  15532. (t2 (org-float-time time2))
  15533. (diff (abs (- t2 t1)))
  15534. (negative (< (- t2 t1) 0))
  15535. ;; (ys (floor (* 365 24 60 60)))
  15536. (ds (* 24 60 60))
  15537. (hs (* 60 60))
  15538. (fy "%dy %dd %02d:%02d")
  15539. (fy1 "%dy %dd")
  15540. (fd "%dd %02d:%02d")
  15541. (fd1 "%dd")
  15542. (fh "%02d:%02d")
  15543. y d h m align)
  15544. (if havetime
  15545. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15546. y 0
  15547. d (floor (/ diff ds)) diff (mod diff ds)
  15548. h (floor (/ diff hs)) diff (mod diff hs)
  15549. m (floor (/ diff 60)))
  15550. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15551. y 0
  15552. d (floor (+ (/ diff ds) 0.5))
  15553. h 0 m 0))
  15554. (if (not to-buffer)
  15555. (message "%s" (org-make-tdiff-string y d h m))
  15556. (if (org-at-table-p)
  15557. (progn
  15558. (goto-char match-end)
  15559. (setq align t)
  15560. (and (looking-at " *|") (goto-char (match-end 0))))
  15561. (goto-char match-end))
  15562. (if (looking-at
  15563. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15564. (replace-match ""))
  15565. (if negative (insert " -"))
  15566. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15567. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15568. (insert " " (format fh h m))))
  15569. (if align (org-table-align))
  15570. (message "Time difference inserted")))))
  15571. (defun org-make-tdiff-string (y d h m)
  15572. (let ((fmt "")
  15573. (l nil))
  15574. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15575. l (push y l)))
  15576. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15577. l (push d l)))
  15578. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15579. l (push h l)))
  15580. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15581. l (push m l)))
  15582. (apply 'format fmt (nreverse l))))
  15583. (defun org-time-string-to-time (s &optional buffer pos)
  15584. "Convert a timestamp string into internal time."
  15585. (condition-case errdata
  15586. (apply 'encode-time (org-parse-time-string s))
  15587. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15588. s (if (not (and buffer pos))
  15589. ""
  15590. (format " at %d in buffer `%s'" pos buffer))
  15591. (cdr errdata)))))
  15592. (defun org-time-string-to-seconds (s)
  15593. "Convert a timestamp string to a number of seconds."
  15594. (org-float-time (org-time-string-to-time s)))
  15595. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15596. "Convert a time stamp to an absolute day number.
  15597. If there is a specifier for a cyclic time stamp, get the closest
  15598. date to DAYNR.
  15599. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15600. The variable `date' is bound by the calendar when this is called."
  15601. (cond
  15602. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15603. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15604. daynr
  15605. (+ daynr 1000)))
  15606. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15607. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15608. (time-to-days (current-time))) (match-string 0 s)
  15609. prefer show-all))
  15610. (t (time-to-days
  15611. (condition-case errdata
  15612. (apply 'encode-time (org-parse-time-string s))
  15613. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15614. s (if (not (and buffer pos))
  15615. ""
  15616. (format " at %d in buffer `%s'" pos buffer))
  15617. (cdr errdata))))))))
  15618. (defun org-days-to-iso-week (days)
  15619. "Return the iso week number."
  15620. (require 'cal-iso)
  15621. (car (calendar-iso-from-absolute days)))
  15622. (defun org-small-year-to-year (year)
  15623. "Convert 2-digit years into 4-digit years.
  15624. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15625. The year 2000 cannot be abbreviated. Any year larger than 99
  15626. is returned unchanged."
  15627. (if (< year 38)
  15628. (setq year (+ 2000 year))
  15629. (if (< year 100)
  15630. (setq year (+ 1900 year))))
  15631. year)
  15632. (defun org-time-from-absolute (d)
  15633. "Return the time corresponding to date D.
  15634. D may be an absolute day number, or a calendar-type list (month day year)."
  15635. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15636. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15637. (defun org-calendar-holiday ()
  15638. "List of holidays, for Diary display in Org-mode."
  15639. (require 'holidays)
  15640. (let ((hl (funcall
  15641. (if (fboundp 'calendar-check-holidays)
  15642. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15643. (if hl (mapconcat 'identity hl "; "))))
  15644. (defun org-diary-sexp-entry (sexp entry date)
  15645. "Process a SEXP diary ENTRY for DATE."
  15646. (require 'diary-lib)
  15647. (let ((result (if calendar-debug-sexp
  15648. (let ((stack-trace-on-error t))
  15649. (eval (car (read-from-string sexp))))
  15650. (condition-case nil
  15651. (eval (car (read-from-string sexp)))
  15652. (error
  15653. (beep)
  15654. (message "Bad sexp at line %d in %s: %s"
  15655. (org-current-line)
  15656. (buffer-file-name) sexp)
  15657. (sleep-for 2))))))
  15658. (cond ((stringp result) (split-string result "; "))
  15659. ((and (consp result)
  15660. (not (consp (cdr result)))
  15661. (stringp (cdr result))) (cdr result))
  15662. ((and (consp result)
  15663. (stringp (car result))) result)
  15664. (result entry))))
  15665. (defun org-diary-to-ical-string (frombuf)
  15666. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15667. This uses the icalendar.el library."
  15668. (let* ((tmpdir (if (featurep 'xemacs)
  15669. (temp-directory)
  15670. temporary-file-directory))
  15671. (tmpfile (make-temp-name
  15672. (expand-file-name "orgics" tmpdir)))
  15673. buf rtn b e)
  15674. (with-current-buffer frombuf
  15675. (icalendar-export-region (point-min) (point-max) tmpfile)
  15676. (setq buf (find-buffer-visiting tmpfile))
  15677. (set-buffer buf)
  15678. (goto-char (point-min))
  15679. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15680. (setq b (match-beginning 0)))
  15681. (goto-char (point-max))
  15682. (if (re-search-backward "^END:VEVENT" nil t)
  15683. (setq e (match-end 0)))
  15684. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15685. (kill-buffer buf)
  15686. (delete-file tmpfile)
  15687. rtn))
  15688. (defun org-closest-date (start current change prefer show-all)
  15689. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15690. When PREFER is `past', return a date that is either CURRENT or past.
  15691. When PREFER is `future', return a date that is either CURRENT or future.
  15692. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15693. ;; Make the proper lists from the dates
  15694. (catch 'exit
  15695. (let ((a1 '(("h" . hour)
  15696. ("d" . day)
  15697. ("w" . week)
  15698. ("m" . month)
  15699. ("y" . year)))
  15700. (shour (nth 2 (org-parse-time-string start)))
  15701. dn dw sday cday n1 n2 n0
  15702. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15703. (setq start (org-date-to-gregorian start)
  15704. current (org-date-to-gregorian
  15705. (if show-all
  15706. current
  15707. (time-to-days (current-time))))
  15708. sday (calendar-absolute-from-gregorian start)
  15709. cday (calendar-absolute-from-gregorian current))
  15710. (if (<= cday sday) (throw 'exit sday))
  15711. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15712. (setq dn (string-to-number (match-string 1 change))
  15713. dw (cdr (assoc (match-string 2 change) a1)))
  15714. (user-error "Invalid change specifier: %s" change))
  15715. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15716. (cond
  15717. ((eq dw 'hour)
  15718. (let ((missing-hours
  15719. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15720. dn)))
  15721. (setq n1 (if (zerop missing-hours) cday
  15722. (- cday (1+ (floor (/ missing-hours 24)))))
  15723. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15724. ((eq dw 'day)
  15725. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15726. n2 (+ n1 dn)))
  15727. ((eq dw 'year)
  15728. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15729. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15730. (setq date1 (list m d y1)
  15731. n1 (calendar-absolute-from-gregorian date1)
  15732. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15733. n2 (calendar-absolute-from-gregorian date2)))
  15734. ((eq dw 'month)
  15735. ;; approx number of month between the two dates
  15736. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15737. ;; How often does dn fit in there?
  15738. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15739. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15740. m (+ m nm)
  15741. ny (floor (/ m 12))
  15742. y (+ y ny)
  15743. m (- m (* ny 12)))
  15744. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15745. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15746. (setq m2 (+ m dn) y2 y)
  15747. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15748. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15749. (while (<= n2 cday)
  15750. (setq n1 n2 m m2 y y2)
  15751. (setq m2 (+ m dn) y2 y)
  15752. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15753. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15754. ;; Make sure n1 is the earlier date
  15755. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15756. (if show-all
  15757. (cond
  15758. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15759. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15760. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15761. (cond
  15762. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15763. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15764. (t (if (= cday n1) n1 n2)))))))
  15765. (defun org-date-to-gregorian (date)
  15766. "Turn any specification of DATE into a Gregorian date for the calendar."
  15767. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15768. ((and (listp date) (= (length date) 3)) date)
  15769. ((stringp date)
  15770. (setq date (org-parse-time-string date))
  15771. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15772. ((listp date)
  15773. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15774. (defun org-parse-time-string (s &optional nodefault)
  15775. "Parse the standard Org-mode time string.
  15776. This should be a lot faster than the normal `parse-time-string'.
  15777. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15778. hour and minute fields will be nil if not given."
  15779. (cond ((string-match org-ts-regexp0 s)
  15780. (list 0
  15781. (if (or (match-beginning 8) (not nodefault))
  15782. (string-to-number (or (match-string 8 s) "0")))
  15783. (if (or (match-beginning 7) (not nodefault))
  15784. (string-to-number (or (match-string 7 s) "0")))
  15785. (string-to-number (match-string 4 s))
  15786. (string-to-number (match-string 3 s))
  15787. (string-to-number (match-string 2 s))
  15788. nil nil nil))
  15789. ((string-match "^<[^>]+>$" s)
  15790. (decode-time (seconds-to-time (org-matcher-time s))))
  15791. (t (error "Not a standard Org-mode time string: %s" s))))
  15792. (defun org-timestamp-up (&optional arg)
  15793. "Increase the date item at the cursor by one.
  15794. If the cursor is on the year, change the year. If it is on the month,
  15795. the day or the time, change that.
  15796. With prefix ARG, change by that many units."
  15797. (interactive "p")
  15798. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15799. (defun org-timestamp-down (&optional arg)
  15800. "Decrease the date item at the cursor by one.
  15801. If the cursor is on the year, change the year. If it is on the month,
  15802. the day or the time, change that.
  15803. With prefix ARG, change by that many units."
  15804. (interactive "p")
  15805. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15806. (defun org-timestamp-up-day (&optional arg)
  15807. "Increase the date in the time stamp by one day.
  15808. With prefix ARG, change that many days."
  15809. (interactive "p")
  15810. (if (and (not (org-at-timestamp-p t))
  15811. (org-at-heading-p))
  15812. (org-todo 'up)
  15813. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15814. (defun org-timestamp-down-day (&optional arg)
  15815. "Decrease the date in the time stamp by one day.
  15816. With prefix ARG, change that many days."
  15817. (interactive "p")
  15818. (if (and (not (org-at-timestamp-p t))
  15819. (org-at-heading-p))
  15820. (org-todo 'down)
  15821. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15822. (defun org-at-timestamp-p (&optional inactive-ok)
  15823. "Determine if the cursor is in or at a timestamp."
  15824. (interactive)
  15825. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15826. (pos (point))
  15827. (ans (or (looking-at tsr)
  15828. (save-excursion
  15829. (skip-chars-backward "^[<\n\r\t")
  15830. (if (> (point) (point-min)) (backward-char 1))
  15831. (and (looking-at tsr)
  15832. (> (- (match-end 0) pos) -1))))))
  15833. (and ans
  15834. (boundp 'org-ts-what)
  15835. (setq org-ts-what
  15836. (cond
  15837. ((= pos (match-beginning 0)) 'bracket)
  15838. ;; Point is considered to be "on the bracket" whether
  15839. ;; it's really on it or right after it.
  15840. ((= pos (1- (match-end 0))) 'bracket)
  15841. ((= pos (match-end 0)) 'after)
  15842. ((org-pos-in-match-range pos 2) 'year)
  15843. ((org-pos-in-match-range pos 3) 'month)
  15844. ((org-pos-in-match-range pos 7) 'hour)
  15845. ((org-pos-in-match-range pos 8) 'minute)
  15846. ((or (org-pos-in-match-range pos 4)
  15847. (org-pos-in-match-range pos 5)) 'day)
  15848. ((and (> pos (or (match-end 8) (match-end 5)))
  15849. (< pos (match-end 0)))
  15850. (- pos (or (match-end 8) (match-end 5))))
  15851. (t 'day))))
  15852. ans))
  15853. (defun org-toggle-timestamp-type ()
  15854. "Toggle the type (<active> or [inactive]) of a time stamp."
  15855. (interactive)
  15856. (when (org-at-timestamp-p t)
  15857. (let ((beg (match-beginning 0)) (end (match-end 0))
  15858. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15859. (save-excursion
  15860. (goto-char beg)
  15861. (while (re-search-forward "[][<>]" end t)
  15862. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15863. t t)))
  15864. (message "Timestamp is now %sactive"
  15865. (if (equal (char-after beg) ?<) "" "in")))))
  15866. (defun org-at-clock-log-p nil
  15867. "Is the cursor on the clock log line?"
  15868. (save-excursion
  15869. (move-beginning-of-line 1)
  15870. (looking-at "^[ \t]*CLOCK:")))
  15871. (defvar org-clock-history) ; defined in org-clock.el
  15872. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15873. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15874. "Change the date in the time stamp at point.
  15875. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15876. `year', `minute', `second'. If WHAT is not given, the cursor position
  15877. in the timestamp determines what will be changed.
  15878. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15879. (let ((origin (point)) origin-cat
  15880. with-hm inactive
  15881. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15882. org-ts-what
  15883. extra rem
  15884. ts time time0 fixnext clrgx)
  15885. (if (not (org-at-timestamp-p t))
  15886. (user-error "Not at a timestamp"))
  15887. (if (and (not what) (eq org-ts-what 'bracket))
  15888. (org-toggle-timestamp-type)
  15889. ;; Point isn't on brackets. Remember the part of the time-stamp
  15890. ;; the point was in. Indeed, size of time-stamps may change,
  15891. ;; but point must be kept in the same category nonetheless.
  15892. (setq origin-cat org-ts-what)
  15893. (if (and (not what) (not (eq org-ts-what 'day))
  15894. org-display-custom-times
  15895. (get-text-property (point) 'display)
  15896. (not (get-text-property (1- (point)) 'display)))
  15897. (setq org-ts-what 'day))
  15898. (setq org-ts-what (or what org-ts-what)
  15899. inactive (= (char-after (match-beginning 0)) ?\[)
  15900. ts (match-string 0))
  15901. (replace-match "")
  15902. (when (string-match
  15903. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15904. ts)
  15905. (setq extra (match-string 1 ts))
  15906. (if suppress-tmp-delay
  15907. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15908. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15909. (setq with-hm t))
  15910. (setq time0 (org-parse-time-string ts))
  15911. (when (and updown
  15912. (eq org-ts-what 'minute)
  15913. (not current-prefix-arg))
  15914. ;; This looks like s-up and s-down. Change by one rounding step.
  15915. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15916. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15917. (setcar (cdr time0) (+ (nth 1 time0)
  15918. (if (> n 0) (- rem) (- dm rem))))))
  15919. (setq time
  15920. (encode-time (or (car time0) 0)
  15921. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15922. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15923. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15924. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15925. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15926. (nthcdr 6 time0)))
  15927. (when (and (member org-ts-what '(hour minute))
  15928. extra
  15929. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15930. (setq extra (org-modify-ts-extra
  15931. extra
  15932. (if (eq org-ts-what 'hour) 2 5)
  15933. n dm)))
  15934. (when (integerp org-ts-what)
  15935. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15936. (if (eq what 'calendar)
  15937. (let ((cal-date (org-get-date-from-calendar)))
  15938. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15939. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15940. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15941. (setcar time0 (or (car time0) 0))
  15942. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15943. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15944. (setq time (apply 'encode-time time0))))
  15945. ;; Insert the new time-stamp, and ensure point stays in the same
  15946. ;; category as before (i.e. not after the last position in that
  15947. ;; category).
  15948. (let ((pos (point)))
  15949. ;; Stay before inserted string. `save-excursion' is of no use.
  15950. (setq org-last-changed-timestamp
  15951. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15952. (goto-char pos))
  15953. (save-match-data
  15954. (looking-at org-ts-regexp3)
  15955. (goto-char (cond
  15956. ;; `day' category ends before `hour' if any, or at
  15957. ;; the end of the day name.
  15958. ((eq origin-cat 'day)
  15959. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15960. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15961. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15962. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15963. ;; `year' and `month' have both fixed size: point
  15964. ;; couldn't have moved into another part.
  15965. (t origin))))
  15966. ;; Update clock if on a CLOCK line.
  15967. (org-clock-update-time-maybe)
  15968. ;; Maybe adjust the closest clock in `org-clock-history'
  15969. (when org-clock-adjust-closest
  15970. (if (not (and (org-at-clock-log-p)
  15971. (< 1 (length (delq nil (mapcar 'marker-position
  15972. org-clock-history))))))
  15973. (message "No clock to adjust")
  15974. (cond ((save-excursion ; fix previous clock?
  15975. (re-search-backward org-ts-regexp0 nil t)
  15976. (org-looking-back (concat org-clock-string " \\[")))
  15977. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15978. ((save-excursion ; fix next clock?
  15979. (re-search-backward org-ts-regexp0 nil t)
  15980. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15981. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15982. (save-window-excursion
  15983. ;; Find closest clock to point, adjust the previous/next one in history
  15984. (let* ((p (save-excursion (org-back-to-heading t)))
  15985. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15986. (clfixnth
  15987. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15988. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15989. (if (not clfixpos)
  15990. (message "No clock to adjust")
  15991. (save-excursion
  15992. (org-goto-marker-or-bmk clfixpos)
  15993. (org-show-subtree)
  15994. (when (re-search-forward clrgx nil t)
  15995. (goto-char (match-beginning 1))
  15996. (let (org-clock-adjust-closest)
  15997. (org-timestamp-change n org-ts-what updown))
  15998. (message "Clock adjusted in %s for heading: %s"
  15999. (file-name-nondirectory (buffer-file-name))
  16000. (org-get-heading t t)))))))))
  16001. ;; Try to recenter the calendar window, if any.
  16002. (if (and org-calendar-follow-timestamp-change
  16003. (get-buffer-window "*Calendar*" t)
  16004. (memq org-ts-what '(day month year)))
  16005. (org-recenter-calendar (time-to-days time))))))
  16006. (defun org-modify-ts-extra (s pos n dm)
  16007. "Change the different parts of the lead-time and repeat fields in timestamp."
  16008. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16009. ng h m new rem)
  16010. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16011. (cond
  16012. ((or (org-pos-in-match-range pos 2)
  16013. (org-pos-in-match-range pos 3))
  16014. (setq m (string-to-number (match-string 3 s))
  16015. h (string-to-number (match-string 2 s)))
  16016. (if (org-pos-in-match-range pos 2)
  16017. (setq h (+ h n))
  16018. (setq n (* dm (org-no-warnings (signum n))))
  16019. (when (not (= 0 (setq rem (% m dm))))
  16020. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16021. (setq m (+ m n)))
  16022. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16023. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16024. (setq h (min 24 (max 0 h)))
  16025. (setq ng 1 new (format "-%02d:%02d" h m)))
  16026. ((org-pos-in-match-range pos 6)
  16027. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16028. ((org-pos-in-match-range pos 5)
  16029. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16030. ((org-pos-in-match-range pos 9)
  16031. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16032. ((org-pos-in-match-range pos 8)
  16033. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16034. (when ng
  16035. (setq s (concat
  16036. (substring s 0 (match-beginning ng))
  16037. new
  16038. (substring s (match-end ng))))))
  16039. s))
  16040. (defun org-recenter-calendar (date)
  16041. "If the calendar is visible, recenter it to DATE."
  16042. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16043. (when cwin
  16044. (let ((calendar-move-hook nil))
  16045. (with-selected-window cwin
  16046. (calendar-goto-date (if (listp date) date
  16047. (calendar-gregorian-from-absolute date))))))))
  16048. (defun org-goto-calendar (&optional arg)
  16049. "Go to the Emacs calendar at the current date.
  16050. If there is a time stamp in the current line, go to that date.
  16051. A prefix ARG can be used to force the current date."
  16052. (interactive "P")
  16053. (let ((tsr org-ts-regexp) diff
  16054. (calendar-move-hook nil)
  16055. (calendar-view-holidays-initially-flag nil)
  16056. (calendar-view-diary-initially-flag nil))
  16057. (if (or (org-at-timestamp-p)
  16058. (save-excursion
  16059. (beginning-of-line 1)
  16060. (looking-at (concat ".*" tsr))))
  16061. (let ((d1 (time-to-days (current-time)))
  16062. (d2 (time-to-days
  16063. (org-time-string-to-time (match-string 1)))))
  16064. (setq diff (- d2 d1))))
  16065. (calendar)
  16066. (calendar-goto-today)
  16067. (if (and diff (not arg)) (calendar-forward-day diff))))
  16068. (defun org-get-date-from-calendar ()
  16069. "Return a list (month day year) of date at point in calendar."
  16070. (with-current-buffer "*Calendar*"
  16071. (save-match-data
  16072. (calendar-cursor-to-date))))
  16073. (defun org-date-from-calendar ()
  16074. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16075. If there is already a time stamp at the cursor position, update it."
  16076. (interactive)
  16077. (if (org-at-timestamp-p t)
  16078. (org-timestamp-change 0 'calendar)
  16079. (let ((cal-date (org-get-date-from-calendar)))
  16080. (org-insert-time-stamp
  16081. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16082. (defcustom org-effort-durations
  16083. `(("h" . 60)
  16084. ("d" . ,(* 60 8))
  16085. ("w" . ,(* 60 8 5))
  16086. ("m" . ,(* 60 8 5 4))
  16087. ("y" . ,(* 60 8 5 40)))
  16088. "Conversion factor to minutes for an effort modifier.
  16089. Each entry has the form (MODIFIER . MINUTES).
  16090. In an effort string, a number followed by MODIFIER is multiplied
  16091. by the specified number of MINUTES to obtain an effort in
  16092. minutes.
  16093. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16094. effort string \"2hours\" is equivalent to 120 minutes."
  16095. :group 'org-agenda
  16096. :version "24.1"
  16097. :type '(alist :key-type (string :tag "Modifier")
  16098. :value-type (number :tag "Minutes")))
  16099. (defun org-minutes-to-clocksum-string (m)
  16100. "Format number of minutes as a clocksum string.
  16101. The format is determined by `org-time-clocksum-format',
  16102. `org-time-clocksum-use-fractional' and
  16103. `org-time-clocksum-fractional-format' and
  16104. `org-time-clocksum-use-effort-durations'."
  16105. (let ((clocksum "")
  16106. (m (round m)) ; Don't allow fractions of minutes
  16107. h d w mo y fmt n)
  16108. (setq h (if org-time-clocksum-use-effort-durations
  16109. (cdr (assoc "h" org-effort-durations)) 60)
  16110. d (if org-time-clocksum-use-effort-durations
  16111. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16112. w (if org-time-clocksum-use-effort-durations
  16113. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16114. mo (if org-time-clocksum-use-effort-durations
  16115. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16116. y (if org-time-clocksum-use-effort-durations
  16117. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16118. ;; fractional format
  16119. (if org-time-clocksum-use-fractional
  16120. (cond
  16121. ;; single format string
  16122. ((stringp org-time-clocksum-fractional-format)
  16123. (format org-time-clocksum-fractional-format (/ m (float h))))
  16124. ;; choice of fractional formats for different time units
  16125. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16126. (> (/ (truncate m) (* y d h)) 0))
  16127. (format fmt (/ m (* y d (float h)))))
  16128. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16129. (> (/ (truncate m) (* mo d h)) 0))
  16130. (format fmt (/ m (* mo d (float h)))))
  16131. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16132. (> (/ (truncate m) (* w d h)) 0))
  16133. (format fmt (/ m (* w d (float h)))))
  16134. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16135. (> (/ (truncate m) (* d h)) 0))
  16136. (format fmt (/ m (* d (float h)))))
  16137. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16138. (> (/ (truncate m) h) 0))
  16139. (format fmt (/ m (float h))))
  16140. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16141. (format fmt m))
  16142. ;; fall back to smallest time unit with a format
  16143. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16144. (format fmt (/ m (float h))))
  16145. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16146. (format fmt (/ m (* d (float h)))))
  16147. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16148. (format fmt (/ m (* w d (float h)))))
  16149. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16150. (format fmt (/ m (* mo d (float h)))))
  16151. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16152. (format fmt (/ m (* y d (float h))))))
  16153. ;; standard (non-fractional) format, with single format string
  16154. (if (stringp org-time-clocksum-format)
  16155. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16156. ;; separate formats components
  16157. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16158. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16159. (plist-get org-time-clocksum-format :require-years))
  16160. (setq clocksum (concat clocksum (format fmt n))
  16161. m (- m (* n y d h))))
  16162. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16163. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16164. (plist-get org-time-clocksum-format :require-months))
  16165. (setq clocksum (concat clocksum (format fmt n))
  16166. m (- m (* n mo d h))))
  16167. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16168. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16169. (plist-get org-time-clocksum-format :require-weeks))
  16170. (setq clocksum (concat clocksum (format fmt n))
  16171. m (- m (* n w d h))))
  16172. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16173. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16174. (plist-get org-time-clocksum-format :require-days))
  16175. (setq clocksum (concat clocksum (format fmt n))
  16176. m (- m (* n d h))))
  16177. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16178. (or (> (setq n (/ (truncate m) h)) 0)
  16179. (plist-get org-time-clocksum-format :require-hours))
  16180. (setq clocksum (concat clocksum (format fmt n))
  16181. m (- m (* n h))))
  16182. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16183. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16184. (setq clocksum (concat clocksum (format fmt m))))
  16185. ;; return formatted time duration
  16186. clocksum))))
  16187. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16188. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16189. "Org mode version 8.0")
  16190. (defun org-hours-to-clocksum-string (n)
  16191. (org-minutes-to-clocksum-string (* n 60)))
  16192. (defun org-hh:mm-string-to-minutes (s)
  16193. "Convert a string H:MM to a number of minutes.
  16194. If the string is just a number, interpret it as minutes.
  16195. In fact, the first hh:mm or number in the string will be taken,
  16196. there can be extra stuff in the string.
  16197. If no number is found, the return value is 0."
  16198. (cond
  16199. ((integerp s) s)
  16200. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16201. (+ (* (string-to-number (match-string 1 s)) 60)
  16202. (string-to-number (match-string 2 s))))
  16203. ((string-match "\\([0-9]+\\)" s)
  16204. (string-to-number (match-string 1 s)))
  16205. (t 0)))
  16206. (defcustom org-image-actual-width t
  16207. "Should we use the actual width of images when inlining them?
  16208. When set to `t', always use the image width.
  16209. When set to a number, use imagemagick (when available) to set
  16210. the image's width to this value.
  16211. When set to a number in a list, try to get the width from any
  16212. #+ATTR.* keyword if it matches a width specification like
  16213. #+ATTR_HTML: :width 300px
  16214. and fall back on that number if none is found.
  16215. When set to nil, try to get the width from an #+ATTR.* keyword
  16216. and fall back on the original width if none is found.
  16217. This requires Emacs >= 24.1, build with imagemagick support."
  16218. :group 'org-appearance
  16219. :version "24.4"
  16220. :package-version '(Org . "8.0")
  16221. :type '(choice
  16222. (const :tag "Use the image width" t)
  16223. (integer :tag "Use a number of pixels")
  16224. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16225. (const :tag "Use #+ATTR* or don't resize" nil)))
  16226. (defcustom org-agenda-inhibit-startup nil
  16227. "Inhibit startup when preparing agenda buffers.
  16228. When this variable is `t' (the default), the initialization of
  16229. the Org agenda buffers is inhibited: e.g. the visibility state
  16230. is not set, the tables are not re-aligned, etc."
  16231. :type 'boolean
  16232. :version "24.3"
  16233. :group 'org-agenda)
  16234. (defcustom org-agenda-ignore-drawer-properties nil
  16235. "Avoid updating text properties when building the agenda.
  16236. Properties are used to prepare buffers for effort estimates, appointments,
  16237. and subtree-local categories.
  16238. If you don't use these in the agenda, you can add them to this list and
  16239. agenda building will be a bit faster.
  16240. The value is a list, with zero or more of the symbols `effort', `appt',
  16241. or `category'."
  16242. :type '(set :greedy t
  16243. (const effort)
  16244. (const appt)
  16245. (const category))
  16246. :version "24.3"
  16247. :group 'org-agenda)
  16248. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16249. "Convert a duration string S to minutes.
  16250. A bare number is interpreted as minutes, modifiers can be set by
  16251. customizing `org-effort-durations' (which see).
  16252. Entries containing a colon are interpreted as H:MM by
  16253. `org-hh:mm-string-to-minutes'."
  16254. (let ((result 0)
  16255. (re (concat "\\([0-9.]+\\) *\\("
  16256. (regexp-opt (mapcar 'car org-effort-durations))
  16257. "\\)")))
  16258. (while (string-match re s)
  16259. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16260. (string-to-number (match-string 1 s))))
  16261. (setq s (replace-match "" nil t s)))
  16262. (setq result (floor result))
  16263. (incf result (org-hh:mm-string-to-minutes s))
  16264. (if output-to-string (number-to-string result) result)))
  16265. ;;;; Files
  16266. (defun org-save-all-org-buffers ()
  16267. "Save all Org-mode buffers without user confirmation."
  16268. (interactive)
  16269. (message "Saving all Org-mode buffers...")
  16270. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16271. (when (featurep 'org-id) (org-id-locations-save))
  16272. (message "Saving all Org-mode buffers... done"))
  16273. (defun org-revert-all-org-buffers ()
  16274. "Revert all Org-mode buffers.
  16275. Prompt for confirmation when there are unsaved changes.
  16276. Be sure you know what you are doing before letting this function
  16277. overwrite your changes.
  16278. This function is useful in a setup where one tracks org files
  16279. with a version control system, to revert on one machine after pulling
  16280. changes from another. I believe the procedure must be like this:
  16281. 1. M-x org-save-all-org-buffers
  16282. 2. Pull changes from the other machine, resolve conflicts
  16283. 3. M-x org-revert-all-org-buffers"
  16284. (interactive)
  16285. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16286. (user-error "Abort"))
  16287. (save-excursion
  16288. (save-window-excursion
  16289. (mapc
  16290. (lambda (b)
  16291. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16292. (with-current-buffer b buffer-file-name))
  16293. (org-pop-to-buffer-same-window b)
  16294. (revert-buffer t 'no-confirm)))
  16295. (buffer-list))
  16296. (when (and (featurep 'org-id) org-id-track-globally)
  16297. (org-id-locations-load)))))
  16298. ;;;; Agenda files
  16299. ;;;###autoload
  16300. (defun org-switchb (&optional arg)
  16301. "Switch between Org buffers.
  16302. With one prefix argument, restrict available buffers to files.
  16303. With two prefix arguments, restrict available buffers to agenda files.
  16304. Defaults to `iswitchb' for buffer name completion.
  16305. Set `org-completion-use-ido' to make it use ido instead."
  16306. (interactive "P")
  16307. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16308. ((equal arg '(16)) (org-buffer-list 'agenda))
  16309. (t (org-buffer-list))))
  16310. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16311. (org-completion-use-ido org-completion-use-ido))
  16312. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16313. (setq org-completion-use-iswitchb t))
  16314. (org-pop-to-buffer-same-window
  16315. (org-icompleting-read "Org buffer: "
  16316. (mapcar 'list (mapcar 'buffer-name blist))
  16317. nil t))))
  16318. ;;; Define some older names previously used for this functionality
  16319. ;;;###autoload
  16320. (defalias 'org-ido-switchb 'org-switchb)
  16321. ;;;###autoload
  16322. (defalias 'org-iswitchb 'org-switchb)
  16323. (defun org-buffer-list (&optional predicate exclude-tmp)
  16324. "Return a list of Org buffers.
  16325. PREDICATE can be `export', `files' or `agenda'.
  16326. export restrict the list to Export buffers.
  16327. files restrict the list to buffers visiting Org files.
  16328. agenda restrict the list to buffers visiting agenda files.
  16329. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16330. (let* ((bfn nil)
  16331. (agenda-files (and (eq predicate 'agenda)
  16332. (mapcar 'file-truename (org-agenda-files t))))
  16333. (filter
  16334. (cond
  16335. ((eq predicate 'files)
  16336. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16337. ((eq predicate 'export)
  16338. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16339. ((eq predicate 'agenda)
  16340. (lambda (b)
  16341. (with-current-buffer b
  16342. (and (derived-mode-p 'org-mode)
  16343. (setq bfn (buffer-file-name b))
  16344. (member (file-truename bfn) agenda-files)))))
  16345. (t (lambda (b) (with-current-buffer b
  16346. (or (derived-mode-p 'org-mode)
  16347. (string-match "\*Org .*Export"
  16348. (buffer-name b)))))))))
  16349. (delq nil
  16350. (mapcar
  16351. (lambda(b)
  16352. (if (and (funcall filter b)
  16353. (or (not exclude-tmp)
  16354. (not (string-match "tmp" (buffer-name b)))))
  16355. b
  16356. nil))
  16357. (buffer-list)))))
  16358. (defun org-agenda-files (&optional unrestricted archives)
  16359. "Get the list of agenda files.
  16360. Optional UNRESTRICTED means return the full list even if a restriction
  16361. is currently in place.
  16362. When ARCHIVES is t, include all archive files that are really being
  16363. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16364. `org-agenda-archives-mode' is t."
  16365. (let ((files
  16366. (cond
  16367. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16368. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16369. ((listp org-agenda-files) org-agenda-files)
  16370. (t (error "Invalid value of `org-agenda-files'")))))
  16371. (setq files (apply 'append
  16372. (mapcar (lambda (f)
  16373. (if (file-directory-p f)
  16374. (directory-files
  16375. f t org-agenda-file-regexp)
  16376. (list f)))
  16377. files)))
  16378. (when org-agenda-skip-unavailable-files
  16379. (setq files (delq nil
  16380. (mapcar (function
  16381. (lambda (file)
  16382. (and (file-readable-p file) file)))
  16383. files))))
  16384. (when (or (eq archives t)
  16385. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16386. (setq files (org-add-archive-files files)))
  16387. files))
  16388. (defun org-agenda-file-p (&optional file)
  16389. "Return non-nil, if FILE is an agenda file.
  16390. If FILE is omitted, use the file associated with the current
  16391. buffer."
  16392. (member (or file (buffer-file-name))
  16393. (org-agenda-files t)))
  16394. (defun org-edit-agenda-file-list ()
  16395. "Edit the list of agenda files.
  16396. Depending on setup, this either uses customize to edit the variable
  16397. `org-agenda-files', or it visits the file that is holding the list. In the
  16398. latter case, the buffer is set up in a way that saving it automatically kills
  16399. the buffer and restores the previous window configuration."
  16400. (interactive)
  16401. (if (stringp org-agenda-files)
  16402. (let ((cw (current-window-configuration)))
  16403. (find-file org-agenda-files)
  16404. (org-set-local 'org-window-configuration cw)
  16405. (org-add-hook 'after-save-hook
  16406. (lambda ()
  16407. (set-window-configuration
  16408. (prog1 org-window-configuration
  16409. (kill-buffer (current-buffer))))
  16410. (org-install-agenda-files-menu)
  16411. (message "New agenda file list installed"))
  16412. nil 'local)
  16413. (message "%s" (substitute-command-keys
  16414. "Edit list and finish with \\[save-buffer]")))
  16415. (customize-variable 'org-agenda-files)))
  16416. (defun org-store-new-agenda-file-list (list)
  16417. "Set new value for the agenda file list and save it correctly."
  16418. (if (stringp org-agenda-files)
  16419. (let ((fe (org-read-agenda-file-list t)) b u)
  16420. (while (setq b (find-buffer-visiting org-agenda-files))
  16421. (kill-buffer b))
  16422. (with-temp-file org-agenda-files
  16423. (insert
  16424. (mapconcat
  16425. (lambda (f) ;; Keep un-expanded entries.
  16426. (if (setq u (assoc f fe))
  16427. (cdr u)
  16428. f))
  16429. list "\n")
  16430. "\n")))
  16431. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16432. (org-insert-mode-line-in-empty-file nil))
  16433. (setq org-agenda-files list)
  16434. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16435. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16436. "Read the list of agenda files from a file.
  16437. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16438. filenames, used by `org-store-new-agenda-file-list' to write back
  16439. un-expanded file names."
  16440. (when (file-directory-p org-agenda-files)
  16441. (error "`org-agenda-files' cannot be a single directory"))
  16442. (when (stringp org-agenda-files)
  16443. (with-temp-buffer
  16444. (insert-file-contents org-agenda-files)
  16445. (mapcar
  16446. (lambda (f)
  16447. (let ((e (expand-file-name (substitute-in-file-name f)
  16448. org-directory)))
  16449. (if pair-with-expansion
  16450. (cons e f)
  16451. e)))
  16452. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16453. ;;;###autoload
  16454. (defun org-cycle-agenda-files ()
  16455. "Cycle through the files in `org-agenda-files'.
  16456. If the current buffer visits an agenda file, find the next one in the list.
  16457. If the current buffer does not, find the first agenda file."
  16458. (interactive)
  16459. (let* ((fs (org-agenda-files t))
  16460. (files (append fs (list (car fs))))
  16461. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16462. file)
  16463. (unless files (user-error "No agenda files"))
  16464. (catch 'exit
  16465. (while (setq file (pop files))
  16466. (if (equal (file-truename file) tcf)
  16467. (when (car files)
  16468. (find-file (car files))
  16469. (throw 'exit t))))
  16470. (find-file (car fs)))
  16471. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16472. (defun org-agenda-file-to-front (&optional to-end)
  16473. "Move/add the current file to the top of the agenda file list.
  16474. If the file is not present in the list, it is added to the front. If it is
  16475. present, it is moved there. With optional argument TO-END, add/move to the
  16476. end of the list."
  16477. (interactive "P")
  16478. (let ((org-agenda-skip-unavailable-files nil)
  16479. (file-alist (mapcar (lambda (x)
  16480. (cons (file-truename x) x))
  16481. (org-agenda-files t)))
  16482. (ctf (file-truename
  16483. (or buffer-file-name
  16484. (user-error "Please save the current buffer to a file"))))
  16485. x had)
  16486. (setq x (assoc ctf file-alist) had x)
  16487. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16488. (if to-end
  16489. (setq file-alist (append (delq x file-alist) (list x)))
  16490. (setq file-alist (cons x (delq x file-alist))))
  16491. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16492. (org-install-agenda-files-menu)
  16493. (message "File %s to %s of agenda file list"
  16494. (if had "moved" "added") (if to-end "end" "front"))))
  16495. (defun org-remove-file (&optional file)
  16496. "Remove current file from the list of files in variable `org-agenda-files'.
  16497. These are the files which are being checked for agenda entries.
  16498. Optional argument FILE means use this file instead of the current."
  16499. (interactive)
  16500. (let* ((org-agenda-skip-unavailable-files nil)
  16501. (file (or file buffer-file-name
  16502. (user-error "Current buffer does not visit a file")))
  16503. (true-file (file-truename file))
  16504. (afile (abbreviate-file-name file))
  16505. (files (delq nil (mapcar
  16506. (lambda (x)
  16507. (if (equal true-file
  16508. (file-truename x))
  16509. nil x))
  16510. (org-agenda-files t)))))
  16511. (if (not (= (length files) (length (org-agenda-files t))))
  16512. (progn
  16513. (org-store-new-agenda-file-list files)
  16514. (org-install-agenda-files-menu)
  16515. (message "Removed file: %s" afile))
  16516. (message "File was not in list: %s (not removed)" afile))))
  16517. (defun org-file-menu-entry (file)
  16518. (vector file (list 'find-file file) t))
  16519. (defun org-check-agenda-file (file)
  16520. "Make sure FILE exists. If not, ask user what to do."
  16521. (when (not (file-exists-p file))
  16522. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16523. (abbreviate-file-name file))
  16524. (let ((r (downcase (read-char-exclusive))))
  16525. (cond
  16526. ((equal r ?r)
  16527. (org-remove-file file)
  16528. (throw 'nextfile t))
  16529. (t (error "Abort"))))))
  16530. (defun org-get-agenda-file-buffer (file)
  16531. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16532. it to the list of buffers which might be released later."
  16533. (let ((buf (org-find-base-buffer-visiting file)))
  16534. (if buf
  16535. buf ; just return it
  16536. ;; Make a new buffer and remember it
  16537. (setq buf (find-file-noselect file))
  16538. (if buf (push buf org-agenda-new-buffers))
  16539. buf)))
  16540. (defun org-release-buffers (blist)
  16541. "Release all buffers in list, asking the user for confirmation when needed.
  16542. When a buffer is unmodified, it is just killed. When modified, it is saved
  16543. \(if the user agrees) and then killed."
  16544. (let (buf file)
  16545. (while (setq buf (pop blist))
  16546. (setq file (buffer-file-name buf))
  16547. (when (and (buffer-modified-p buf)
  16548. file
  16549. (y-or-n-p (format "Save file %s? " file)))
  16550. (with-current-buffer buf (save-buffer)))
  16551. (kill-buffer buf))))
  16552. (defun org-agenda-prepare-buffers (files)
  16553. "Create buffers for all agenda files, protect archived trees and comments."
  16554. (interactive)
  16555. (let ((pa '(:org-archived t))
  16556. (pc '(:org-comment t))
  16557. (pall '(:org-archived t :org-comment t))
  16558. (inhibit-read-only t)
  16559. (org-inhibit-startup org-agenda-inhibit-startup)
  16560. (rea (concat ":" org-archive-tag ":"))
  16561. file re pos)
  16562. (setq org-tag-alist-for-agenda nil
  16563. org-tag-groups-alist-for-agenda nil)
  16564. (save-window-excursion
  16565. (save-restriction
  16566. (while (setq file (pop files))
  16567. (catch 'nextfile
  16568. (if (bufferp file)
  16569. (set-buffer file)
  16570. (org-check-agenda-file file)
  16571. (set-buffer (org-get-agenda-file-buffer file)))
  16572. (widen)
  16573. (org-set-regexps-and-options-for-tags)
  16574. (setq pos (point))
  16575. (goto-char (point-min))
  16576. (let ((case-fold-search t))
  16577. (when (search-forward "#+setupfile" nil t)
  16578. ;; Don't set all regexps and options systematically as
  16579. ;; this is only run for setting agenda tags from setup
  16580. ;; file
  16581. (org-set-regexps-and-options)))
  16582. (or (memq 'category org-agenda-ignore-drawer-properties)
  16583. (org-refresh-category-properties))
  16584. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16585. (org-refresh-properties org-effort-property 'org-effort))
  16586. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16587. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16588. (setq org-todo-keywords-for-agenda
  16589. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16590. (setq org-done-keywords-for-agenda
  16591. (append org-done-keywords-for-agenda org-done-keywords))
  16592. (setq org-todo-keyword-alist-for-agenda
  16593. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16594. (setq org-tag-alist-for-agenda
  16595. (org-uniquify
  16596. (append org-tag-alist-for-agenda
  16597. org-tag-alist
  16598. org-tag-persistent-alist)))
  16599. (if org-group-tags
  16600. (setq org-tag-groups-alist-for-agenda
  16601. (org-uniquify-alist
  16602. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16603. (org-with-silent-modifications
  16604. (save-excursion
  16605. (remove-text-properties (point-min) (point-max) pall)
  16606. (when org-agenda-skip-archived-trees
  16607. (goto-char (point-min))
  16608. (while (re-search-forward rea nil t)
  16609. (if (org-at-heading-p t)
  16610. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16611. (goto-char (point-min))
  16612. (setq re (format org-heading-keyword-regexp-format
  16613. org-comment-string))
  16614. (while (re-search-forward re nil t)
  16615. (add-text-properties
  16616. (match-beginning 0) (org-end-of-subtree t) pc))))
  16617. (goto-char pos)))))
  16618. (setq org-todo-keywords-for-agenda
  16619. (org-uniquify org-todo-keywords-for-agenda))
  16620. (setq org-todo-keyword-alist-for-agenda
  16621. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16622. ;;;; CDLaTeX minor mode
  16623. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16624. "Keymap for the minor `org-cdlatex-mode'.")
  16625. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16626. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16627. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16628. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16629. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16630. (defvar org-cdlatex-texmathp-advice-is-done nil
  16631. "Flag remembering if we have applied the advice to texmathp already.")
  16632. (define-minor-mode org-cdlatex-mode
  16633. "Toggle the minor `org-cdlatex-mode'.
  16634. This mode supports entering LaTeX environment and math in LaTeX fragments
  16635. in Org-mode.
  16636. \\{org-cdlatex-mode-map}"
  16637. nil " OCDL" nil
  16638. (when org-cdlatex-mode
  16639. (require 'cdlatex)
  16640. (run-hooks 'cdlatex-mode-hook)
  16641. (cdlatex-compute-tables))
  16642. (unless org-cdlatex-texmathp-advice-is-done
  16643. (setq org-cdlatex-texmathp-advice-is-done t)
  16644. (defadvice texmathp (around org-math-always-on activate)
  16645. "Always return t in org-mode buffers.
  16646. This is because we want to insert math symbols without dollars even outside
  16647. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16648. an embedded LaTeX fragment, let texmathp do its job.
  16649. \\[org-cdlatex-mode-map]"
  16650. (interactive)
  16651. (let (p)
  16652. (cond
  16653. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16654. ((eq this-command 'cdlatex-math-symbol)
  16655. (setq ad-return-value t
  16656. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16657. (t
  16658. (let ((p (org-inside-LaTeX-fragment-p)))
  16659. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16660. (setq ad-return-value t
  16661. texmathp-why '("Org-mode embedded math" . 0))
  16662. (if p ad-do-it)))))))))
  16663. (defun turn-on-org-cdlatex ()
  16664. "Unconditionally turn on `org-cdlatex-mode'."
  16665. (org-cdlatex-mode 1))
  16666. (defun org-try-cdlatex-tab ()
  16667. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16668. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16669. - inside a LaTeX fragment, or
  16670. - after the first word in a line, where an abbreviation expansion could
  16671. insert a LaTeX environment."
  16672. (when org-cdlatex-mode
  16673. (cond
  16674. ;; Before any word on the line: No expansion possible.
  16675. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16676. ;; Just after first word on the line: Expand it. Make sure it
  16677. ;; cannot happen on headlines, though.
  16678. ((save-excursion
  16679. (skip-chars-backward "a-zA-Z0-9*")
  16680. (skip-chars-backward " \t")
  16681. (and (bolp) (not (org-at-heading-p))))
  16682. (cdlatex-tab) t)
  16683. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16684. (defun org-cdlatex-underscore-caret (&optional arg)
  16685. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16686. Revert to the normal definition outside of these fragments."
  16687. (interactive "P")
  16688. (if (org-inside-LaTeX-fragment-p)
  16689. (call-interactively 'cdlatex-sub-superscript)
  16690. (let (org-cdlatex-mode)
  16691. (call-interactively (key-binding (vector last-input-event))))))
  16692. (defun org-cdlatex-math-modify (&optional arg)
  16693. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16694. Revert to the normal definition outside of these fragments."
  16695. (interactive "P")
  16696. (if (org-inside-LaTeX-fragment-p)
  16697. (call-interactively 'cdlatex-math-modify)
  16698. (let (org-cdlatex-mode)
  16699. (call-interactively (key-binding (vector last-input-event))))))
  16700. ;;;; LaTeX fragments
  16701. (defvar org-latex-regexps
  16702. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16703. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16704. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16705. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16706. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16707. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16708. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16709. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16710. "Regular expressions for matching embedded LaTeX.")
  16711. (defun org-inside-LaTeX-fragment-p ()
  16712. "Test if point is inside a LaTeX fragment.
  16713. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16714. sequence appearing also before point.
  16715. Even though the matchers for math are configurable, this function assumes
  16716. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16717. delimiters are skipped when they have been removed by customization.
  16718. The return value is nil, or a cons cell with the delimiter and the
  16719. position of this delimiter.
  16720. This function does a reasonably good job, but can locally be fooled by
  16721. for example currency specifications. For example it will assume being in
  16722. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16723. fragments that are properly closed, but during editing, we have to live
  16724. with the uncertainty caused by missing closing delimiters. This function
  16725. looks only before point, not after."
  16726. (catch 'exit
  16727. (let ((pos (point))
  16728. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16729. (lim (progn
  16730. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16731. (point)))
  16732. dd-on str (start 0) m re)
  16733. (goto-char pos)
  16734. (when dodollar
  16735. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16736. re (nth 1 (assoc "$" org-latex-regexps)))
  16737. (while (string-match re str start)
  16738. (cond
  16739. ((= (match-end 0) (length str))
  16740. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16741. ((= (match-end 0) (- (length str) 5))
  16742. (throw 'exit nil))
  16743. (t (setq start (match-end 0))))))
  16744. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16745. (goto-char pos)
  16746. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16747. (and (match-beginning 2) (throw 'exit nil))
  16748. ;; count $$
  16749. (while (re-search-backward "\\$\\$" lim t)
  16750. (setq dd-on (not dd-on)))
  16751. (goto-char pos)
  16752. (if dd-on (cons "$$" m))))))
  16753. (defun org-inside-latex-macro-p ()
  16754. "Is point inside a LaTeX macro or its arguments?"
  16755. (save-match-data
  16756. (org-in-regexp
  16757. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16758. (defvar org-latex-fragment-image-overlays nil
  16759. "List of overlays carrying the images of latex fragments.")
  16760. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16761. (defun org-remove-latex-fragment-image-overlays ()
  16762. "Remove all overlays with LaTeX fragment images in current buffer."
  16763. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16764. (setq org-latex-fragment-image-overlays nil))
  16765. (defun org-preview-latex-fragment (&optional subtree)
  16766. "Preview the LaTeX fragment at point, or all locally or globally.
  16767. If the cursor is in a LaTeX fragment, create the image and overlay
  16768. it over the source code. If there is no fragment at point, display
  16769. all fragments in the current text, from one headline to the next. With
  16770. prefix SUBTREE, display all fragments in the current subtree. With a
  16771. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16772. the cursor is before the first headline,
  16773. display all fragments in the buffer.
  16774. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16775. (interactive "P")
  16776. (unless buffer-file-name
  16777. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16778. (when (display-graphic-p)
  16779. (org-remove-latex-fragment-image-overlays)
  16780. (save-excursion
  16781. (save-restriction
  16782. (let (beg end at msg)
  16783. (cond
  16784. ((or (equal subtree '(16))
  16785. (not (save-excursion
  16786. (re-search-backward org-outline-regexp-bol nil t))))
  16787. (setq beg (point-min) end (point-max)
  16788. msg "Creating images for buffer...%s"))
  16789. ((equal subtree '(4))
  16790. (org-back-to-heading)
  16791. (setq beg (point) end (org-end-of-subtree t)
  16792. msg "Creating images for subtree...%s"))
  16793. (t
  16794. (if (setq at (org-inside-LaTeX-fragment-p))
  16795. (goto-char (max (point-min) (- (cdr at) 2)))
  16796. (org-back-to-heading))
  16797. (setq beg (point) end (progn (outline-next-heading) (point))
  16798. msg (if at "Creating image...%s"
  16799. "Creating images for entry...%s"))))
  16800. (message msg "")
  16801. (narrow-to-region beg end)
  16802. (goto-char beg)
  16803. (org-format-latex
  16804. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16805. (file-name-nondirectory
  16806. buffer-file-name)))
  16807. default-directory 'overlays msg at 'forbuffer
  16808. org-latex-create-formula-image-program)
  16809. (message msg "done. Use `C-c C-c' to remove images."))))))
  16810. (defun org-format-latex (prefix &optional dir overlays msg at
  16811. forbuffer processing-type)
  16812. "Replace LaTeX fragments with links to an image, and produce images.
  16813. Some of the options can be changed using the variable
  16814. `org-format-latex-options'."
  16815. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16816. (let* ((prefixnodir (file-name-nondirectory prefix))
  16817. (absprefix (expand-file-name prefix dir))
  16818. (todir (file-name-directory absprefix))
  16819. (opt org-format-latex-options)
  16820. (optnew org-format-latex-options)
  16821. (matchers (plist-get opt :matchers))
  16822. (re-list org-latex-regexps)
  16823. (cnt 0) txt hash link beg end re e checkdir
  16824. string
  16825. m n block-type block linkfile movefile ov)
  16826. ;; Check the different regular expressions
  16827. (while (setq e (pop re-list))
  16828. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16829. block (if block-type "\n\n" ""))
  16830. (when (member m matchers)
  16831. (goto-char (point-min))
  16832. (while (re-search-forward re nil t)
  16833. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16834. (or (not overlays)
  16835. (not (eq (get-char-property (match-beginning n)
  16836. 'org-overlay-type)
  16837. 'org-latex-overlay))))
  16838. (cond
  16839. ((eq processing-type 'verbatim))
  16840. ((eq processing-type 'mathjax)
  16841. ;; Prepare for MathJax processing.
  16842. (setq string (match-string n))
  16843. (when (member m '("$" "$1"))
  16844. (save-excursion
  16845. (delete-region (match-beginning n) (match-end n))
  16846. (goto-char (match-beginning n))
  16847. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16848. ((or (eq processing-type 'dvipng)
  16849. (eq processing-type 'imagemagick))
  16850. ;; Process to an image.
  16851. (setq txt (match-string n)
  16852. beg (match-beginning n) end (match-end n)
  16853. cnt (1+ cnt))
  16854. (let ((face (face-at-point))
  16855. (fg (plist-get opt :foreground))
  16856. (bg (plist-get opt :background))
  16857. ;; Ensure full list is printed.
  16858. print-length print-level)
  16859. (when forbuffer
  16860. ;; Get the colors from the face at point.
  16861. (goto-char beg)
  16862. (when (eq fg 'auto)
  16863. (setq fg (face-attribute face :foreground nil 'default)))
  16864. (when (eq bg 'auto)
  16865. (setq bg (face-attribute face :background nil 'default)))
  16866. (setq optnew (copy-sequence opt))
  16867. (plist-put optnew :foreground fg)
  16868. (plist-put optnew :background bg))
  16869. (setq hash (sha1 (prin1-to-string
  16870. (list org-format-latex-header
  16871. org-latex-default-packages-alist
  16872. org-latex-packages-alist
  16873. org-format-latex-options
  16874. forbuffer txt fg bg)))
  16875. linkfile (format "%s_%s.png" prefix hash)
  16876. movefile (format "%s_%s.png" absprefix hash)))
  16877. (setq link (concat block "[[file:" linkfile "]]" block))
  16878. (if msg (message msg cnt))
  16879. (goto-char beg)
  16880. (unless checkdir ; Ensure the directory exists.
  16881. (setq checkdir t)
  16882. (or (file-directory-p todir) (make-directory todir t)))
  16883. (unless (file-exists-p movefile)
  16884. (org-create-formula-image
  16885. txt movefile optnew forbuffer processing-type))
  16886. (if overlays
  16887. (progn
  16888. (mapc (lambda (o)
  16889. (if (eq (overlay-get o 'org-overlay-type)
  16890. 'org-latex-overlay)
  16891. (delete-overlay o)))
  16892. (overlays-in beg end))
  16893. (setq ov (make-overlay beg end))
  16894. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16895. (if (featurep 'xemacs)
  16896. (progn
  16897. (overlay-put ov 'invisible t)
  16898. (overlay-put
  16899. ov 'end-glyph
  16900. (make-glyph (vector 'png :file movefile))))
  16901. (overlay-put
  16902. ov 'display
  16903. (list 'image :type 'png :file movefile :ascent 'center)))
  16904. (push ov org-latex-fragment-image-overlays)
  16905. (goto-char end))
  16906. (delete-region beg end)
  16907. (insert (org-add-props link
  16908. (list 'org-latex-src
  16909. (replace-regexp-in-string
  16910. "\"" "" txt)
  16911. 'org-latex-src-embed-type
  16912. (if block-type 'paragraph 'character))))))
  16913. ((eq processing-type 'mathml)
  16914. ;; Process to MathML
  16915. (unless (save-match-data (org-format-latex-mathml-available-p))
  16916. (user-error "LaTeX to MathML converter not configured"))
  16917. (setq txt (match-string n)
  16918. beg (match-beginning n) end (match-end n)
  16919. cnt (1+ cnt))
  16920. (if msg (message msg cnt))
  16921. (goto-char beg)
  16922. (delete-region beg end)
  16923. (insert (org-format-latex-as-mathml
  16924. txt block-type prefix dir)))
  16925. (t
  16926. (error "Unknown conversion type %s for LaTeX fragments"
  16927. processing-type)))))))))
  16928. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16929. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16930. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16931. sucessful, return the portion between \"<math...> </math>\"
  16932. elements otherwise return nil. When MATHML-FILE is specified,
  16933. write the results in to that file. When invoked as an
  16934. interactive command, prompt for LATEX-FRAG, with initial value
  16935. set to the current active region and echo the results for user
  16936. inspection."
  16937. (interactive (list (let ((frag (when (org-region-active-p)
  16938. (buffer-substring-no-properties
  16939. (region-beginning) (region-end)))))
  16940. (read-string "LaTeX Fragment: " frag nil frag))))
  16941. (unless latex-frag (error "Invalid LaTeX fragment"))
  16942. (let* ((tmp-in-file (file-relative-name
  16943. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16944. (ignore (write-region latex-frag nil tmp-in-file))
  16945. (tmp-out-file (file-relative-name
  16946. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16947. (cmd (format-spec
  16948. org-latex-to-mathml-convert-command
  16949. `((?j . ,(shell-quote-argument
  16950. (expand-file-name org-latex-to-mathml-jar-file)))
  16951. (?I . ,(shell-quote-argument tmp-in-file))
  16952. (?o . ,(shell-quote-argument tmp-out-file)))))
  16953. mathml shell-command-output)
  16954. (when (org-called-interactively-p 'any)
  16955. (unless (org-format-latex-mathml-available-p)
  16956. (user-error "LaTeX to MathML converter not configured")))
  16957. (message "Running %s" cmd)
  16958. (setq shell-command-output (shell-command-to-string cmd))
  16959. (setq mathml
  16960. (when (file-readable-p tmp-out-file)
  16961. (with-current-buffer (find-file-noselect tmp-out-file t)
  16962. (goto-char (point-min))
  16963. (when (re-search-forward
  16964. (concat
  16965. (regexp-quote
  16966. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16967. "\\(.\\|\n\\)*"
  16968. (regexp-quote "</math>")) nil t)
  16969. (prog1 (match-string 0) (kill-buffer))))))
  16970. (cond
  16971. (mathml
  16972. (setq mathml
  16973. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16974. (when mathml-file
  16975. (write-region mathml nil mathml-file))
  16976. (when (org-called-interactively-p 'any)
  16977. (message mathml)))
  16978. ((message "LaTeX to MathML conversion failed")
  16979. (message shell-command-output)))
  16980. (delete-file tmp-in-file)
  16981. (when (file-exists-p tmp-out-file)
  16982. (delete-file tmp-out-file))
  16983. mathml))
  16984. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16985. prefix &optional dir)
  16986. "Use `org-create-math-formula' but check local cache first."
  16987. (let* ((absprefix (expand-file-name prefix dir))
  16988. (print-length nil) (print-level nil)
  16989. (formula-id (concat
  16990. "formula-"
  16991. (sha1
  16992. (prin1-to-string
  16993. (list latex-frag
  16994. org-latex-to-mathml-convert-command)))))
  16995. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16996. (formula-cache-dir (file-name-directory formula-cache)))
  16997. (unless (file-directory-p formula-cache-dir)
  16998. (make-directory formula-cache-dir t))
  16999. (unless (file-exists-p formula-cache)
  17000. (org-create-math-formula latex-frag formula-cache))
  17001. (if (file-exists-p formula-cache)
  17002. ;; Successful conversion. Return the link to MathML file.
  17003. (org-add-props
  17004. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17005. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17006. 'org-latex-src-embed-type (if latex-frag-type
  17007. 'paragraph 'character)))
  17008. ;; Failed conversion. Return the LaTeX fragment verbatim
  17009. latex-frag)))
  17010. (defun org-create-formula-image (string tofile options buffer &optional type)
  17011. "Create an image from LaTeX source using dvipng or convert.
  17012. This function calls either `org-create-formula-image-with-dvipng'
  17013. or `org-create-formula-image-with-imagemagick' depending on the
  17014. value of `org-latex-create-formula-image-program' or on the value
  17015. of the optional TYPE variable.
  17016. Note: ultimately these two function should be combined as they
  17017. share a good deal of logic."
  17018. (org-check-external-command
  17019. "latex" "needed to convert LaTeX fragments to images")
  17020. (funcall
  17021. (case (or type org-latex-create-formula-image-program)
  17022. ('dvipng
  17023. (org-check-external-command
  17024. "dvipng" "needed to convert LaTeX fragments to images")
  17025. #'org-create-formula-image-with-dvipng)
  17026. ('imagemagick
  17027. (org-check-external-command
  17028. "convert" "you need to install imagemagick")
  17029. #'org-create-formula-image-with-imagemagick)
  17030. (t (error
  17031. "Invalid value of `org-latex-create-formula-image-program'")))
  17032. string tofile options buffer))
  17033. (declare-function org-export-get-backend "ox" (name))
  17034. (declare-function org-export--get-global-options "ox" (&optional backend))
  17035. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17036. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17037. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17038. (defun org-create-formula--latex-header ()
  17039. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17040. (let ((info (org-combine-plists (org-export--get-global-options
  17041. (org-export-get-backend 'latex))
  17042. (org-export--get-inbuffer-options
  17043. (org-export-get-backend 'latex)))))
  17044. (org-latex-guess-babel-language
  17045. (org-latex-guess-inputenc
  17046. (org-splice-latex-header
  17047. org-format-latex-header
  17048. org-latex-default-packages-alist
  17049. org-latex-packages-alist t
  17050. (plist-get info :latex-header)))
  17051. info)))
  17052. ;; This function borrows from Ganesh Swami's latex2png.el
  17053. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17054. "This calls dvipng."
  17055. (require 'ox-latex)
  17056. (let* ((tmpdir (if (featurep 'xemacs)
  17057. (temp-directory)
  17058. temporary-file-directory))
  17059. (texfilebase (make-temp-name
  17060. (expand-file-name "orgtex" tmpdir)))
  17061. (texfile (concat texfilebase ".tex"))
  17062. (dvifile (concat texfilebase ".dvi"))
  17063. (pngfile (concat texfilebase ".png"))
  17064. (fnh (if (featurep 'xemacs)
  17065. (font-height (face-font 'default))
  17066. (face-attribute 'default :height nil)))
  17067. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17068. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17069. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17070. "Black"))
  17071. (bg (or (plist-get options (if buffer :background :html-background))
  17072. "Transparent")))
  17073. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17074. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17075. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17076. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17077. (let ((latex-header (org-create-formula--latex-header)))
  17078. (with-temp-file texfile
  17079. (insert latex-header)
  17080. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17081. (let ((dir default-directory))
  17082. (condition-case nil
  17083. (progn
  17084. (cd tmpdir)
  17085. (call-process "latex" nil nil nil texfile))
  17086. (error nil))
  17087. (cd dir))
  17088. (if (not (file-exists-p dvifile))
  17089. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17090. (condition-case nil
  17091. (if (featurep 'xemacs)
  17092. (call-process "dvipng" nil nil nil
  17093. "-fg" fg "-bg" bg
  17094. "-T" "tight"
  17095. "-o" pngfile
  17096. dvifile)
  17097. (call-process "dvipng" nil nil nil
  17098. "-fg" fg "-bg" bg
  17099. "-D" dpi
  17100. ;;"-x" scale "-y" scale
  17101. "-T" "tight"
  17102. "-o" pngfile
  17103. dvifile))
  17104. (error nil))
  17105. (if (not (file-exists-p pngfile))
  17106. (if org-format-latex-signal-error
  17107. (error "Failed to create png file from %s" texfile)
  17108. (message "Failed to create png file from %s" texfile)
  17109. nil)
  17110. ;; Use the requested file name and clean up
  17111. (copy-file pngfile tofile 'replace)
  17112. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17113. (if (file-exists-p (concat texfilebase e))
  17114. (delete-file (concat texfilebase e))))
  17115. pngfile))))
  17116. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17117. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17118. "This calls convert, which is included into imagemagick."
  17119. (require 'ox-latex)
  17120. (let* ((tmpdir (if (featurep 'xemacs)
  17121. (temp-directory)
  17122. temporary-file-directory))
  17123. (texfilebase (make-temp-name
  17124. (expand-file-name "orgtex" tmpdir)))
  17125. (texfile (concat texfilebase ".tex"))
  17126. (pdffile (concat texfilebase ".pdf"))
  17127. (pngfile (concat texfilebase ".png"))
  17128. (fnh (if (featurep 'xemacs)
  17129. (font-height (face-font 'default))
  17130. (face-attribute 'default :height nil)))
  17131. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17132. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17133. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17134. "black"))
  17135. (bg (or (plist-get options (if buffer :background :html-background))
  17136. "white")))
  17137. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17138. (setq fg (org-latex-color-format fg)))
  17139. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17140. (setq bg (org-latex-color-format
  17141. (if (string= bg "Transparent") "white" bg))))
  17142. (let ((latex-header (org-create-formula--latex-header)))
  17143. (with-temp-file texfile
  17144. (insert latex-header)
  17145. (insert "\n\\begin{document}\n"
  17146. "\\definecolor{fg}{rgb}{" fg "}\n"
  17147. "\\definecolor{bg}{rgb}{" bg "}\n"
  17148. "\n\\pagecolor{bg}\n"
  17149. "\n{\\color{fg}\n"
  17150. string
  17151. "\n}\n"
  17152. "\n\\end{document}\n")))
  17153. (org-latex-compile texfile t)
  17154. (if (not (file-exists-p pdffile))
  17155. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17156. (condition-case nil
  17157. (if (featurep 'xemacs)
  17158. (call-process "convert" nil nil nil
  17159. "-density" "96"
  17160. "-trim"
  17161. "-antialias"
  17162. pdffile
  17163. "-quality" "100"
  17164. ;; "-sharpen" "0x1.0"
  17165. pngfile)
  17166. (call-process "convert" nil nil nil
  17167. "-density" dpi
  17168. "-trim"
  17169. "-antialias"
  17170. pdffile
  17171. "-quality" "100"
  17172. ;; "-sharpen" "0x1.0"
  17173. pngfile))
  17174. (error nil))
  17175. (if (not (file-exists-p pngfile))
  17176. (if org-format-latex-signal-error
  17177. (error "Failed to create png file from %s" texfile)
  17178. (message "Failed to create png file from %s" texfile)
  17179. nil)
  17180. ;; Use the requested file name and clean up
  17181. (copy-file pngfile tofile 'replace)
  17182. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17183. (if (file-exists-p (concat texfilebase e))
  17184. (delete-file (concat texfilebase e))))
  17185. pngfile))))
  17186. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17187. "Fill a LaTeX header template TPL.
  17188. In the template, the following place holders will be recognized:
  17189. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17190. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17191. [PACKAGES] \\usepackage statements for PKG
  17192. [NO-PACKAGES] do not include PKG
  17193. [EXTRA] the string EXTRA
  17194. [NO-EXTRA] do not include EXTRA
  17195. For backward compatibility, if both the positive and the negative place
  17196. holder is missing, the positive one (without the \"NO-\") will be
  17197. assumed to be present at the end of the template.
  17198. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17199. EXTRA is a string.
  17200. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17201. (let (rpl (end ""))
  17202. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17203. (setq rpl (if (or (match-end 1) (not def-pkg))
  17204. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17205. tpl (replace-match rpl t t tpl))
  17206. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17207. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17208. (setq rpl (if (or (match-end 1) (not pkg))
  17209. "" (org-latex-packages-to-string pkg snippets-p t))
  17210. tpl (replace-match rpl t t tpl))
  17211. (if pkg (setq end
  17212. (concat end "\n"
  17213. (org-latex-packages-to-string pkg snippets-p)))))
  17214. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17215. (setq rpl (if (or (match-end 1) (not extra))
  17216. "" (concat extra "\n"))
  17217. tpl (replace-match rpl t t tpl))
  17218. (if (and extra (string-match "\\S-" extra))
  17219. (setq end (concat end "\n" extra))))
  17220. (if (string-match "\\S-" end)
  17221. (concat tpl "\n" end)
  17222. tpl)))
  17223. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17224. "Turn an alist of packages into a string with the \\usepackage macros."
  17225. (setq pkg (mapconcat (lambda(p)
  17226. (cond
  17227. ((stringp p) p)
  17228. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17229. (format "%% Package %s omitted" (cadr p)))
  17230. ((equal "" (car p))
  17231. (format "\\usepackage{%s}" (cadr p)))
  17232. (t
  17233. (format "\\usepackage[%s]{%s}"
  17234. (car p) (cadr p)))))
  17235. pkg
  17236. "\n"))
  17237. (if newline (concat pkg "\n") pkg))
  17238. (defun org-dvipng-color (attr)
  17239. "Return a RGB color specification for dvipng."
  17240. (apply 'format "rgb %s %s %s"
  17241. (mapcar 'org-normalize-color
  17242. (if (featurep 'xemacs)
  17243. (color-rgb-components
  17244. (face-property 'default
  17245. (cond ((eq attr :foreground) 'foreground)
  17246. ((eq attr :background) 'background))))
  17247. (color-values (face-attribute 'default attr nil))))))
  17248. (defun org-dvipng-color-format (color-name)
  17249. "Convert COLOR-NAME to a RGB color value for dvipng."
  17250. (apply 'format "rgb %s %s %s"
  17251. (mapcar 'org-normalize-color
  17252. (color-values color-name))))
  17253. (defun org-latex-color (attr)
  17254. "Return a RGB color for the LaTeX color package."
  17255. (apply 'format "%s,%s,%s"
  17256. (mapcar 'org-normalize-color
  17257. (if (featurep 'xemacs)
  17258. (color-rgb-components
  17259. (face-property 'default
  17260. (cond ((eq attr :foreground) 'foreground)
  17261. ((eq attr :background) 'background))))
  17262. (color-values (face-attribute 'default attr nil))))))
  17263. (defun org-latex-color-format (color-name)
  17264. "Convert COLOR-NAME to a RGB color value."
  17265. (apply 'format "%s,%s,%s"
  17266. (mapcar 'org-normalize-color
  17267. (color-values color-name))))
  17268. (defun org-normalize-color (value)
  17269. "Return string to be used as color value for an RGB component."
  17270. (format "%g" (/ value 65535.0)))
  17271. ;; Image display
  17272. (defvar org-inline-image-overlays nil)
  17273. (make-variable-buffer-local 'org-inline-image-overlays)
  17274. (defun org-toggle-inline-images (&optional include-linked)
  17275. "Toggle the display of inline images.
  17276. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17277. (interactive "P")
  17278. (if org-inline-image-overlays
  17279. (progn
  17280. (org-remove-inline-images)
  17281. (message "Inline image display turned off"))
  17282. (org-display-inline-images include-linked)
  17283. (if (and (org-called-interactively-p)
  17284. org-inline-image-overlays)
  17285. (message "%d images displayed inline"
  17286. (length org-inline-image-overlays))
  17287. (message "No images to display inline"))))
  17288. (defun org-redisplay-inline-images ()
  17289. "Refresh the display of inline images."
  17290. (interactive)
  17291. (if (not org-inline-image-overlays)
  17292. (org-toggle-inline-images)
  17293. (org-toggle-inline-images)
  17294. (org-toggle-inline-images)))
  17295. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17296. "Display inline images.
  17297. An inline image is a link which follows either of these
  17298. conventions:
  17299. 1. Its path is a file with an extension matching return value
  17300. from `image-file-name-regexp' and it has no contents.
  17301. 2. Its description consists in a single link of the previous
  17302. type.
  17303. When optional argument INCLUDE-LINKED is non-nil, also links with
  17304. a text description part will be inlined. This can be nice for
  17305. a quick look at those images, but it does not reflect what
  17306. exported files will look like.
  17307. When optional argument REFRESH is non-nil, refresh existing
  17308. images between BEG and END. This will create new image displays
  17309. only if necessary. BEG and END default to the buffer
  17310. boundaries."
  17311. (interactive "P")
  17312. (when (display-graphic-p)
  17313. (unless refresh
  17314. (org-remove-inline-images)
  17315. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17316. (org-with-wide-buffer
  17317. (goto-char (or beg (point-min)))
  17318. (let ((case-fold-search t)
  17319. (file-extension-re (org-image-file-name-regexp)))
  17320. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17321. (let ((link (save-match-data (org-element-context))))
  17322. ;; Check if we're at an inline image.
  17323. (when (and (equal (org-element-property :type link) "file")
  17324. (or include-linked
  17325. (not (org-element-property :contents-begin link)))
  17326. (let ((parent (org-element-property :parent link)))
  17327. (or (not (eq (org-element-type parent) 'link))
  17328. (not (cdr (org-element-contents parent)))))
  17329. (org-string-match-p file-extension-re
  17330. (org-element-property :path link)))
  17331. (let ((file (expand-file-name (org-element-property :path link))))
  17332. (when (file-exists-p file)
  17333. (let ((width
  17334. ;; Apply `org-image-actual-width' specifications.
  17335. (cond
  17336. ((not (image-type-available-p 'imagemagick)) nil)
  17337. ((eq org-image-actual-width t) nil)
  17338. ((listp org-image-actual-width)
  17339. (or
  17340. ;; First try to find a width among
  17341. ;; attributes associated to the paragraph
  17342. ;; containing link.
  17343. (let ((paragraph
  17344. (let ((e link))
  17345. (while (and (setq e (org-element-property
  17346. :parent e))
  17347. (not (eq (org-element-type e)
  17348. 'paragraph))))
  17349. e)))
  17350. (when paragraph
  17351. (save-excursion
  17352. (goto-char (org-element-property :begin paragraph))
  17353. (when (save-match-data
  17354. (re-search-forward
  17355. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17356. (org-element-property
  17357. :post-affiliated paragraph)
  17358. t))
  17359. (string-to-number (match-string 1))))))
  17360. ;; Otherwise, fall-back to provided number.
  17361. (car org-image-actual-width)))
  17362. ((numberp org-image-actual-width)
  17363. org-image-actual-width)))
  17364. (old (get-char-property-and-overlay
  17365. (org-element-property :begin link)
  17366. 'org-image-overlay)))
  17367. (if (and (car-safe old) refresh)
  17368. (image-refresh (overlay-get (cdr old) 'display))
  17369. (let ((image (save-match-data
  17370. (create-image file
  17371. (and width 'imagemagick)
  17372. nil
  17373. :width width))))
  17374. (when image
  17375. (let* ((link
  17376. ;; If inline image is the description
  17377. ;; of another link, be sure to
  17378. ;; consider the latter as the one to
  17379. ;; apply the overlay on.
  17380. (let ((parent
  17381. (org-element-property :parent link)))
  17382. (if (eq (org-element-type parent) 'link)
  17383. parent
  17384. link)))
  17385. (ov (make-overlay
  17386. (org-element-property :begin link)
  17387. (progn
  17388. (goto-char
  17389. (org-element-property :end link))
  17390. (skip-chars-backward " \t")
  17391. (point)))))
  17392. (overlay-put ov 'display image)
  17393. (overlay-put ov 'face 'default)
  17394. (overlay-put ov 'org-image-overlay t)
  17395. (overlay-put
  17396. ov 'modification-hooks
  17397. (list 'org-display-inline-remove-overlay))
  17398. (push ov org-inline-image-overlays)))))))))))))))
  17399. (define-obsolete-function-alias
  17400. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17401. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17402. "Remove inline-display overlay if a corresponding region is modified."
  17403. (let ((inhibit-modification-hooks t))
  17404. (when (and ov after)
  17405. (delete ov org-inline-image-overlays)
  17406. (delete-overlay ov))))
  17407. (defun org-remove-inline-images ()
  17408. "Remove inline display of images."
  17409. (interactive)
  17410. (mapc 'delete-overlay org-inline-image-overlays)
  17411. (setq org-inline-image-overlays nil))
  17412. ;;;; Key bindings
  17413. ;; Outline functions from `outline-mode-prefix-map'
  17414. ;; that can be remapped in Org:
  17415. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17416. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17417. (define-key org-mode-map [remap outline-forward-same-level]
  17418. 'org-forward-heading-same-level)
  17419. (define-key org-mode-map [remap outline-backward-same-level]
  17420. 'org-backward-heading-same-level)
  17421. (define-key org-mode-map [remap show-branches]
  17422. 'org-kill-note-or-show-branches)
  17423. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17424. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17425. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17426. ;; Outline functions from `outline-mode-prefix-map' that can not
  17427. ;; be remapped in Org:
  17428. ;;
  17429. ;; - the column "key binding" shows whether the Outline function is still
  17430. ;; available in Org mode on the same key that it has been bound to in
  17431. ;; Outline mode:
  17432. ;; - "overridden": key used for a different functionality in Org mode
  17433. ;; - else: key still bound to the same Outline function in Org mode
  17434. ;;
  17435. ;; | Outline function | key binding | Org replacement |
  17436. ;; |------------------------------------+-------------+-----------------------|
  17437. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17438. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17439. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17440. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17441. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17442. ;; | `show-entry' | overridden | no replacement |
  17443. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17444. ;; | `show-branches' | `C-c C-k' | still same function |
  17445. ;; | `show-subtree' | overridden | visibility cycling |
  17446. ;; | `show-all' | overridden | no replacement |
  17447. ;; | `hide-subtree' | overridden | visibility cycling |
  17448. ;; | `hide-body' | overridden | no replacement |
  17449. ;; | `hide-entry' | overridden | visibility cycling |
  17450. ;; | `hide-leaves' | overridden | no replacement |
  17451. ;; | `hide-sublevels' | overridden | no replacement |
  17452. ;; | `hide-other' | overridden | no replacement |
  17453. ;; Make `C-c C-x' a prefix key
  17454. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17455. ;; TAB key with modifiers
  17456. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17457. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17458. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17459. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17460. ;; The following line is necessary under Suse GNU/Linux
  17461. (unless (featurep 'xemacs)
  17462. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17463. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17464. (define-key org-mode-map [backtab] 'org-shifttab)
  17465. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17466. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17467. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17468. ;; Cursor keys with modifiers
  17469. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17470. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17471. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17472. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17473. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17474. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17475. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17476. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17477. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17478. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17479. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17480. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17481. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17482. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17483. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17484. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17485. ;; Babel keys
  17486. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17487. (mapc (lambda (pair)
  17488. (define-key org-babel-map (car pair) (cdr pair)))
  17489. org-babel-key-bindings)
  17490. ;;; Extra keys for tty access.
  17491. ;; We only set them when really needed because otherwise the
  17492. ;; menus don't show the simple keys
  17493. (when (or org-use-extra-keys
  17494. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17495. (not window-system))
  17496. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17497. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17498. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17499. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17500. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17501. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17502. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17503. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17504. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17505. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17506. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17507. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17508. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17509. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17510. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17511. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17512. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17513. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17514. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17515. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17516. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17517. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17518. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17519. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17520. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17521. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17522. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17523. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17524. ;; All the other keys
  17525. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17526. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17527. (if (boundp 'narrow-map)
  17528. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17529. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17530. (if (boundp 'narrow-map)
  17531. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17532. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17533. (if (boundp 'narrow-map)
  17534. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17535. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17536. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17537. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17538. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17539. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17540. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17541. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17542. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17543. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17544. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17545. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17546. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17547. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17548. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17549. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17550. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17551. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17552. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17553. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17554. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17555. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17556. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17557. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17558. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17559. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17560. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17561. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17562. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17563. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17564. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17565. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17566. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17567. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17568. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17569. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17570. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17571. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17572. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17573. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17574. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17575. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17576. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17577. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17578. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17579. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17580. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17581. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17582. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17583. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17584. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17585. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17586. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17587. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17588. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17589. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17590. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17591. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17592. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17593. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17594. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17595. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17596. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17597. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17598. (org-defkey org-mode-map "\C-m" 'org-return)
  17599. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17600. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17601. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17602. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17603. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17604. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17605. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17606. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17607. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17608. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17609. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17610. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17611. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17612. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17613. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17614. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17615. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17616. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17617. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17618. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17619. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17620. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17621. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17622. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17623. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17624. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17625. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17626. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17627. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17628. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17629. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17630. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17631. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17632. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17633. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17634. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17635. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17636. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17637. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17638. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17639. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17640. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17641. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17642. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17643. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17644. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17645. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17646. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17647. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17648. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17649. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17650. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17651. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17652. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17653. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17654. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17655. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17656. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17657. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17658. (when (featurep 'xemacs)
  17659. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17660. (defconst org-speed-commands-default
  17661. '(
  17662. ("Outline Navigation")
  17663. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17664. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17665. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17666. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17667. ("F" . org-next-block)
  17668. ("B" . org-previous-block)
  17669. ("u" . (org-speed-move-safe 'outline-up-heading))
  17670. ("j" . org-goto)
  17671. ("g" . (org-refile t))
  17672. ("Outline Visibility")
  17673. ("c" . org-cycle)
  17674. ("C" . org-shifttab)
  17675. (" " . org-display-outline-path)
  17676. ("s" . org-narrow-to-subtree)
  17677. ("=" . org-columns)
  17678. ("Outline Structure Editing")
  17679. ("U" . org-shiftmetaup)
  17680. ("D" . org-shiftmetadown)
  17681. ("r" . org-metaright)
  17682. ("l" . org-metaleft)
  17683. ("R" . org-shiftmetaright)
  17684. ("L" . org-shiftmetaleft)
  17685. ("i" . (progn (forward-char 1) (call-interactively
  17686. 'org-insert-heading-respect-content)))
  17687. ("^" . org-sort)
  17688. ("w" . org-refile)
  17689. ("a" . org-archive-subtree-default-with-confirmation)
  17690. ("@" . org-mark-subtree)
  17691. ("#" . org-toggle-comment)
  17692. ("Clock Commands")
  17693. ("I" . org-clock-in)
  17694. ("O" . org-clock-out)
  17695. ("Meta Data Editing")
  17696. ("t" . org-todo)
  17697. ("," . (org-priority))
  17698. ("0" . (org-priority ?\ ))
  17699. ("1" . (org-priority ?A))
  17700. ("2" . (org-priority ?B))
  17701. ("3" . (org-priority ?C))
  17702. (":" . org-set-tags-command)
  17703. ("e" . org-set-effort)
  17704. ("E" . org-inc-effort)
  17705. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17706. (org-entry-put (point) "APPT_WARNTIME" m)))
  17707. ("Agenda Views etc")
  17708. ("v" . org-agenda)
  17709. ("/" . org-sparse-tree)
  17710. ("Misc")
  17711. ("o" . org-open-at-point)
  17712. ("?" . org-speed-command-help)
  17713. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17714. (">" . (org-agenda-remove-restriction-lock))
  17715. )
  17716. "The default speed commands.")
  17717. (defun org-print-speed-command (e)
  17718. (if (> (length (car e)) 1)
  17719. (progn
  17720. (princ "\n")
  17721. (princ (car e))
  17722. (princ "\n")
  17723. (princ (make-string (length (car e)) ?-))
  17724. (princ "\n"))
  17725. (princ (car e))
  17726. (princ " ")
  17727. (if (symbolp (cdr e))
  17728. (princ (symbol-name (cdr e)))
  17729. (prin1 (cdr e)))
  17730. (princ "\n")))
  17731. (defun org-speed-command-help ()
  17732. "Show the available speed commands."
  17733. (interactive)
  17734. (if (not org-use-speed-commands)
  17735. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17736. (with-output-to-temp-buffer "*Help*"
  17737. (princ "User-defined Speed commands\n===========================\n")
  17738. (mapc 'org-print-speed-command org-speed-commands-user)
  17739. (princ "\n")
  17740. (princ "Built-in Speed commands\n=======================\n")
  17741. (mapc 'org-print-speed-command org-speed-commands-default))
  17742. (with-current-buffer "*Help*"
  17743. (setq truncate-lines t))))
  17744. (defun org-speed-move-safe (cmd)
  17745. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17746. If not, return to the original position and throw an error."
  17747. (interactive)
  17748. (let ((pos (point)))
  17749. (call-interactively cmd)
  17750. (unless (and (bolp) (org-at-heading-p))
  17751. (goto-char pos)
  17752. (error "Boundary reached while executing %s" cmd))))
  17753. (defvar org-self-insert-command-undo-counter 0)
  17754. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17755. (defvar org-speed-command nil)
  17756. (define-obsolete-function-alias
  17757. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17758. (defun org-speed-command-activate (keys)
  17759. "Hook for activating single-letter speed commands.
  17760. `org-speed-commands-default' specifies a minimal command set.
  17761. Use `org-speed-commands-user' for further customization."
  17762. (when (or (and (bolp) (looking-at org-outline-regexp))
  17763. (and (functionp org-use-speed-commands)
  17764. (funcall org-use-speed-commands)))
  17765. (cdr (assoc keys (append org-speed-commands-user
  17766. org-speed-commands-default)))))
  17767. (define-obsolete-function-alias
  17768. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17769. (defun org-babel-speed-command-activate (keys)
  17770. "Hook for activating single-letter code block commands."
  17771. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17772. (cdr (assoc keys org-babel-key-bindings))))
  17773. (defcustom org-speed-command-hook
  17774. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17775. "Hook for activating speed commands at strategic locations.
  17776. Hook functions are called in sequence until a valid handler is
  17777. found.
  17778. Each hook takes a single argument, a user-pressed command key
  17779. which is also a `self-insert-command' from the global map.
  17780. Within the hook, examine the cursor position and the command key
  17781. and return nil or a valid handler as appropriate. Handler could
  17782. be one of an interactive command, a function, or a form.
  17783. Set `org-use-speed-commands' to non-nil value to enable this
  17784. hook. The default setting is `org-speed-command-activate'."
  17785. :group 'org-structure
  17786. :version "24.1"
  17787. :type 'hook)
  17788. (defun org-self-insert-command (N)
  17789. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17790. If the cursor is in a table looking at whitespace, the whitespace is
  17791. overwritten, and the table is not marked as requiring realignment."
  17792. (interactive "p")
  17793. (org-check-before-invisible-edit 'insert)
  17794. (cond
  17795. ((and org-use-speed-commands
  17796. (setq org-speed-command
  17797. (run-hook-with-args-until-success
  17798. 'org-speed-command-hook (this-command-keys))))
  17799. (cond
  17800. ((commandp org-speed-command)
  17801. (setq this-command org-speed-command)
  17802. (call-interactively org-speed-command))
  17803. ((functionp org-speed-command)
  17804. (funcall org-speed-command))
  17805. ((and org-speed-command (listp org-speed-command))
  17806. (eval org-speed-command))
  17807. (t (let (org-use-speed-commands)
  17808. (call-interactively 'org-self-insert-command)))))
  17809. ((and
  17810. (org-table-p)
  17811. (progn
  17812. ;; check if we blank the field, and if that triggers align
  17813. (and (featurep 'org-table) org-table-auto-blank-field
  17814. (member last-command
  17815. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17816. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17817. ;; got extra space, this field does not determine column width
  17818. (let (org-table-may-need-update) (org-table-blank-field))
  17819. ;; no extra space, this field may determine column width
  17820. (org-table-blank-field)))
  17821. t)
  17822. (eq N 1)
  17823. (looking-at "[^|\n]* |"))
  17824. (let (org-table-may-need-update)
  17825. (goto-char (1- (match-end 0)))
  17826. (backward-delete-char 1)
  17827. (goto-char (match-beginning 0))
  17828. (self-insert-command N)))
  17829. (t
  17830. (setq org-table-may-need-update t)
  17831. (self-insert-command N)
  17832. (org-fix-tags-on-the-fly)
  17833. (if org-self-insert-cluster-for-undo
  17834. (if (not (eq last-command 'org-self-insert-command))
  17835. (setq org-self-insert-command-undo-counter 1)
  17836. (if (>= org-self-insert-command-undo-counter 20)
  17837. (setq org-self-insert-command-undo-counter 1)
  17838. (and (> org-self-insert-command-undo-counter 0)
  17839. buffer-undo-list (listp buffer-undo-list)
  17840. (not (cadr buffer-undo-list)) ; remove nil entry
  17841. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17842. (setq org-self-insert-command-undo-counter
  17843. (1+ org-self-insert-command-undo-counter))))))))
  17844. (defun org-check-before-invisible-edit (kind)
  17845. "Check is editing if kind KIND would be dangerous with invisible text around.
  17846. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17847. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17848. (if (and org-catch-invisible-edits
  17849. (or (not (boundp 'visible-mode)) (not visible-mode))
  17850. (or (get-char-property (point) 'invisible)
  17851. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17852. ;; OK, we need to take a closer look
  17853. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17854. (invisible-before-point (if (bobp) nil (get-char-property
  17855. (1- (point)) 'invisible)))
  17856. (border-and-ok-direction
  17857. (or
  17858. ;; Check if we are acting predictably before invisible text
  17859. (and invisible-at-point (not invisible-before-point)
  17860. (memq kind '(insert delete-backward)))
  17861. ;; Check if we are acting predictably after invisible text
  17862. ;; This works not well, and I have turned it off. It seems
  17863. ;; better to always show and stop after invisible text.
  17864. ;; (and (not invisible-at-point) invisible-before-point
  17865. ;; (memq kind '(insert delete)))
  17866. )))
  17867. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17868. (memq invisible-before-point '(outline org-hide-block t)))
  17869. (if (eq org-catch-invisible-edits 'error)
  17870. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17871. (if (and org-custom-properties-overlays
  17872. (y-or-n-p "Display invisible properties in this buffer? "))
  17873. (org-toggle-custom-properties-visibility)
  17874. ;; Make the area visible
  17875. (save-excursion
  17876. (if invisible-before-point
  17877. (goto-char (previous-single-char-property-change
  17878. (point) 'invisible)))
  17879. (org-cycle))
  17880. (cond
  17881. ((eq org-catch-invisible-edits 'show)
  17882. ;; That's it, we do the edit after showing
  17883. (message
  17884. "Unfolding invisible region around point before editing")
  17885. (sit-for 1))
  17886. ((and (eq org-catch-invisible-edits 'smart)
  17887. border-and-ok-direction)
  17888. (message "Unfolding invisible region around point before editing"))
  17889. (t
  17890. ;; Don't do the edit, make the user repeat it in full visibility
  17891. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17892. (defun org-fix-tags-on-the-fly ()
  17893. (when (and (equal (char-after (point-at-bol)) ?*)
  17894. (org-at-heading-p))
  17895. (org-align-tags-here org-tags-column)))
  17896. (defun org-delete-backward-char (N)
  17897. "Like `delete-backward-char', insert whitespace at field end in tables.
  17898. When deleting backwards, in tables this function will insert whitespace in
  17899. front of the next \"|\" separator, to keep the table aligned. The table will
  17900. still be marked for re-alignment if the field did fill the entire column,
  17901. because, in this case the deletion might narrow the column."
  17902. (interactive "p")
  17903. (save-match-data
  17904. (org-check-before-invisible-edit 'delete-backward)
  17905. (if (and (org-table-p)
  17906. (eq N 1)
  17907. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17908. (looking-at ".*?|"))
  17909. (let ((pos (point))
  17910. (noalign (looking-at "[^|\n\r]* |"))
  17911. (c org-table-may-need-update))
  17912. (backward-delete-char N)
  17913. (if (not overwrite-mode)
  17914. (progn
  17915. (skip-chars-forward "^|")
  17916. (insert " ")
  17917. (goto-char (1- pos))))
  17918. ;; noalign: if there were two spaces at the end, this field
  17919. ;; does not determine the width of the column.
  17920. (if noalign (setq org-table-may-need-update c)))
  17921. (backward-delete-char N)
  17922. (org-fix-tags-on-the-fly))))
  17923. (defun org-delete-char (N)
  17924. "Like `delete-char', but insert whitespace at field end in tables.
  17925. When deleting characters, in tables this function will insert whitespace in
  17926. front of the next \"|\" separator, to keep the table aligned. The table will
  17927. still be marked for re-alignment if the field did fill the entire column,
  17928. because, in this case the deletion might narrow the column."
  17929. (interactive "p")
  17930. (save-match-data
  17931. (org-check-before-invisible-edit 'delete)
  17932. (if (and (org-table-p)
  17933. (not (bolp))
  17934. (not (= (char-after) ?|))
  17935. (eq N 1))
  17936. (if (looking-at ".*?|")
  17937. (let ((pos (point))
  17938. (noalign (looking-at "[^|\n\r]* |"))
  17939. (c org-table-may-need-update))
  17940. (replace-match
  17941. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17942. (goto-char pos)
  17943. ;; noalign: if there were two spaces at the end, this field
  17944. ;; does not determine the width of the column.
  17945. (if noalign (setq org-table-may-need-update c)))
  17946. (delete-char N))
  17947. (delete-char N)
  17948. (org-fix-tags-on-the-fly))))
  17949. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17950. (put 'org-self-insert-command 'delete-selection t)
  17951. (put 'orgtbl-self-insert-command 'delete-selection t)
  17952. (put 'org-delete-char 'delete-selection 'supersede)
  17953. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17954. (put 'org-yank 'delete-selection 'yank)
  17955. ;; Make `flyspell-mode' delay after some commands
  17956. (put 'org-self-insert-command 'flyspell-delayed t)
  17957. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17958. (put 'org-delete-char 'flyspell-delayed t)
  17959. (put 'org-delete-backward-char 'flyspell-delayed t)
  17960. ;; Make pabbrev-mode expand after org-mode commands
  17961. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17962. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17963. ;; How to do this: Measure non-white length of current string
  17964. ;; If equal to column width, we should realign.
  17965. (defun org-remap (map &rest commands)
  17966. "In MAP, remap the functions given in COMMANDS.
  17967. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17968. (let (new old)
  17969. (while commands
  17970. (setq old (pop commands) new (pop commands))
  17971. (if (fboundp 'command-remapping)
  17972. (org-defkey map (vector 'remap old) new)
  17973. (substitute-key-definition old new map global-map)))))
  17974. (defun org-transpose-words ()
  17975. "Transpose words for Org.
  17976. This uses the `org-mode-transpose-word-syntax-table' syntax
  17977. table, which interprets characters in `org-emphasis-alist' as
  17978. word constituants."
  17979. (interactive)
  17980. (with-syntax-table org-mode-transpose-word-syntax-table
  17981. (call-interactively 'transpose-words)))
  17982. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17983. (when (eq org-enable-table-editor 'optimized)
  17984. ;; If the user wants maximum table support, we need to hijack
  17985. ;; some standard editing functions
  17986. (org-remap org-mode-map
  17987. 'self-insert-command 'org-self-insert-command
  17988. 'delete-char 'org-delete-char
  17989. 'delete-backward-char 'org-delete-backward-char)
  17990. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17991. (defvar org-ctrl-c-ctrl-c-hook nil
  17992. "Hook for functions attaching themselves to `C-c C-c'.
  17993. This can be used to add additional functionality to the C-c C-c
  17994. key which executes context-dependent commands. This hook is run
  17995. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17996. run after the last test.
  17997. Each function will be called with no arguments. The function
  17998. must check if the context is appropriate for it to act. If yes,
  17999. it should do its thing and then return a non-nil value. If the
  18000. context is wrong, just do nothing and return nil.")
  18001. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18002. "Hook for functions attaching themselves to `C-c C-c'.
  18003. This can be used to add additional functionality to the C-c C-c
  18004. key which executes context-dependent commands. This hook is run
  18005. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18006. before the first test.
  18007. Each function will be called with no arguments. The function
  18008. must check if the context is appropriate for it to act. If yes,
  18009. it should do its thing and then return a non-nil value. If the
  18010. context is wrong, just do nothing and return nil.")
  18011. (defvar org-tab-first-hook nil
  18012. "Hook for functions to attach themselves to TAB.
  18013. See `org-ctrl-c-ctrl-c-hook' for more information.
  18014. This hook runs as the first action when TAB is pressed, even before
  18015. `org-cycle' messes around with the `outline-regexp' to cater for
  18016. inline tasks and plain list item folding.
  18017. If any function in this hook returns t, any other actions that
  18018. would have been caused by TAB (such as table field motion or visibility
  18019. cycling) will not occur.")
  18020. (defvar org-tab-after-check-for-table-hook nil
  18021. "Hook for functions to attach themselves to TAB.
  18022. See `org-ctrl-c-ctrl-c-hook' for more information.
  18023. This hook runs after it has been established that the cursor is not in a
  18024. table, but before checking if the cursor is in a headline or if global cycling
  18025. should be done.
  18026. If any function in this hook returns t, not other actions like visibility
  18027. cycling will be done.")
  18028. (defvar org-tab-after-check-for-cycling-hook nil
  18029. "Hook for functions to attach themselves to TAB.
  18030. See `org-ctrl-c-ctrl-c-hook' for more information.
  18031. This hook runs after it has been established that not table field motion and
  18032. not visibility should be done because of current context. This is probably
  18033. the place where a package like yasnippets can hook in.")
  18034. (defvar org-tab-before-tab-emulation-hook nil
  18035. "Hook for functions to attach themselves to TAB.
  18036. See `org-ctrl-c-ctrl-c-hook' for more information.
  18037. This hook runs after every other options for TAB have been exhausted, but
  18038. before indentation and \t insertion takes place.")
  18039. (defvar org-metaleft-hook nil
  18040. "Hook for functions attaching themselves to `M-left'.
  18041. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18042. (defvar org-metaright-hook nil
  18043. "Hook for functions attaching themselves to `M-right'.
  18044. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18045. (defvar org-metaup-hook nil
  18046. "Hook for functions attaching themselves to `M-up'.
  18047. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18048. (defvar org-metadown-hook nil
  18049. "Hook for functions attaching themselves to `M-down'.
  18050. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18051. (defvar org-shiftmetaleft-hook nil
  18052. "Hook for functions attaching themselves to `M-S-left'.
  18053. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18054. (defvar org-shiftmetaright-hook nil
  18055. "Hook for functions attaching themselves to `M-S-right'.
  18056. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18057. (defvar org-shiftmetaup-hook nil
  18058. "Hook for functions attaching themselves to `M-S-up'.
  18059. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18060. (defvar org-shiftmetadown-hook nil
  18061. "Hook for functions attaching themselves to `M-S-down'.
  18062. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18063. (defvar org-metareturn-hook nil
  18064. "Hook for functions attaching themselves to `M-RET'.
  18065. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18066. (defvar org-shiftup-hook nil
  18067. "Hook for functions attaching themselves to `S-up'.
  18068. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18069. (defvar org-shiftup-final-hook nil
  18070. "Hook for functions attaching themselves to `S-up'.
  18071. This one runs after all other options except shift-select have been excluded.
  18072. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18073. (defvar org-shiftdown-hook nil
  18074. "Hook for functions attaching themselves to `S-down'.
  18075. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18076. (defvar org-shiftdown-final-hook nil
  18077. "Hook for functions attaching themselves to `S-down'.
  18078. This one runs after all other options except shift-select have been excluded.
  18079. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18080. (defvar org-shiftleft-hook nil
  18081. "Hook for functions attaching themselves to `S-left'.
  18082. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18083. (defvar org-shiftleft-final-hook nil
  18084. "Hook for functions attaching themselves to `S-left'.
  18085. This one runs after all other options except shift-select have been excluded.
  18086. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18087. (defvar org-shiftright-hook nil
  18088. "Hook for functions attaching themselves to `S-right'.
  18089. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18090. (defvar org-shiftright-final-hook nil
  18091. "Hook for functions attaching themselves to `S-right'.
  18092. This one runs after all other options except shift-select have been excluded.
  18093. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18094. (defun org-modifier-cursor-error ()
  18095. "Throw an error, a modified cursor command was applied in wrong context."
  18096. (user-error "This command is active in special context like tables, headlines or items"))
  18097. (defun org-shiftselect-error ()
  18098. "Throw an error because Shift-Cursor command was applied in wrong context."
  18099. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18100. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18101. (user-error "This command works only in special context like headlines or timestamps")))
  18102. (defun org-call-for-shift-select (cmd)
  18103. (let ((this-command-keys-shift-translated t))
  18104. (call-interactively cmd)))
  18105. (defun org-shifttab (&optional arg)
  18106. "Global visibility cycling or move to previous table field.
  18107. Call `org-table-previous-field' within a table.
  18108. When ARG is nil, cycle globally through visibility states.
  18109. When ARG is a numeric prefix, show contents of this level."
  18110. (interactive "P")
  18111. (cond
  18112. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18113. ((integerp arg)
  18114. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18115. (message "Content view to level: %d" arg)
  18116. (org-content (prefix-numeric-value arg2))
  18117. (org-cycle-show-empty-lines t)
  18118. (setq org-cycle-global-status 'overview)))
  18119. (t (call-interactively 'org-global-cycle))))
  18120. (defun org-shiftmetaleft ()
  18121. "Promote subtree or delete table column.
  18122. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18123. `org-table-delete-column', depending on context. See the
  18124. individual commands for more information."
  18125. (interactive)
  18126. (cond
  18127. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18128. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18129. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18130. ((if (not (org-region-active-p)) (org-at-item-p)
  18131. (save-excursion (goto-char (region-beginning))
  18132. (org-at-item-p)))
  18133. (call-interactively 'org-outdent-item-tree))
  18134. (t (org-modifier-cursor-error))))
  18135. (defun org-shiftmetaright ()
  18136. "Demote subtree or insert table column.
  18137. Calls `org-demote-subtree', `org-indent-item-tree', or
  18138. `org-table-insert-column', depending on context. See the
  18139. individual commands for more information."
  18140. (interactive)
  18141. (cond
  18142. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18143. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18144. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18145. ((if (not (org-region-active-p)) (org-at-item-p)
  18146. (save-excursion (goto-char (region-beginning))
  18147. (org-at-item-p)))
  18148. (call-interactively 'org-indent-item-tree))
  18149. (t (org-modifier-cursor-error))))
  18150. (defun org-shiftmetaup (&optional arg)
  18151. "Move subtree up or kill table row.
  18152. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18153. `org-move-item-up' or `org-timestamp-up', depending on context.
  18154. See the individual commands for more information."
  18155. (interactive "P")
  18156. (cond
  18157. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18158. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18159. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18160. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18161. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18162. (call-interactively 'org-timestamp-up)))
  18163. (t (call-interactively 'org-drag-line-backward))))
  18164. (defun org-shiftmetadown (&optional arg)
  18165. "Move subtree down or insert table row.
  18166. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18167. `org-move-item-down' or `org-timestamp-up', depending on context.
  18168. See the individual commands for more information."
  18169. (interactive "P")
  18170. (cond
  18171. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18172. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18173. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18174. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18175. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18176. (call-interactively 'org-timestamp-down)))
  18177. (t (call-interactively 'org-drag-line-forward))))
  18178. (defsubst org-hidden-tree-error ()
  18179. (user-error
  18180. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18181. (defun org-metaleft (&optional arg)
  18182. "Promote heading or move table column to left.
  18183. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18184. With no specific context, calls the Emacs default `backward-word'.
  18185. See the individual commands for more information."
  18186. (interactive "P")
  18187. (cond
  18188. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18189. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18190. ((org-with-limited-levels
  18191. (or (org-at-heading-p)
  18192. (and (org-region-active-p)
  18193. (save-excursion
  18194. (goto-char (region-beginning))
  18195. (org-at-heading-p)))))
  18196. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18197. (call-interactively 'org-do-promote))
  18198. ;; At an inline task.
  18199. ((org-at-heading-p)
  18200. (call-interactively 'org-inlinetask-promote))
  18201. ((or (org-at-item-p)
  18202. (and (org-region-active-p)
  18203. (save-excursion
  18204. (goto-char (region-beginning))
  18205. (org-at-item-p))))
  18206. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18207. (call-interactively 'org-outdent-item))
  18208. (t (call-interactively 'backward-word))))
  18209. (defun org-metaright (&optional arg)
  18210. "Demote a subtree, a list item or move table column to right.
  18211. In front of a drawer or a block keyword, indent it correctly.
  18212. With no specific context, calls the Emacs default `forward-word'.
  18213. See the individual commands for more information."
  18214. (interactive "P")
  18215. (cond
  18216. ((run-hook-with-args-until-success 'org-metaright-hook))
  18217. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18218. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18219. ((org-at-block-p) (call-interactively 'org-indent-block))
  18220. ((org-with-limited-levels
  18221. (or (org-at-heading-p)
  18222. (and (org-region-active-p)
  18223. (save-excursion
  18224. (goto-char (region-beginning))
  18225. (org-at-heading-p)))))
  18226. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18227. (call-interactively 'org-do-demote))
  18228. ;; At an inline task.
  18229. ((org-at-heading-p)
  18230. (call-interactively 'org-inlinetask-demote))
  18231. ((or (org-at-item-p)
  18232. (and (org-region-active-p)
  18233. (save-excursion
  18234. (goto-char (region-beginning))
  18235. (org-at-item-p))))
  18236. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18237. (call-interactively 'org-indent-item))
  18238. (t (call-interactively 'forward-word))))
  18239. (defun org-check-for-hidden (what)
  18240. "Check if there are hidden headlines/items in the current visual line.
  18241. WHAT can be either `headlines' or `items'. If the current line is
  18242. an outline or item heading and it has a folded subtree below it,
  18243. this function returns t, nil otherwise."
  18244. (let ((re (cond
  18245. ((eq what 'headlines) org-outline-regexp-bol)
  18246. ((eq what 'items) (org-item-beginning-re))
  18247. (t (error "This should not happen"))))
  18248. beg end)
  18249. (save-excursion
  18250. (catch 'exit
  18251. (unless (org-region-active-p)
  18252. (setq beg (point-at-bol))
  18253. (beginning-of-line 2)
  18254. (while (and (not (eobp)) ;; this is like `next-line'
  18255. (get-char-property (1- (point)) 'invisible))
  18256. (beginning-of-line 2))
  18257. (setq end (point))
  18258. (goto-char beg)
  18259. (goto-char (point-at-eol))
  18260. (setq end (max end (point)))
  18261. (while (re-search-forward re end t)
  18262. (if (get-char-property (match-beginning 0) 'invisible)
  18263. (throw 'exit t))))
  18264. nil))))
  18265. (defun org-metaup (&optional arg)
  18266. "Move subtree up or move table row up.
  18267. Calls `org-move-subtree-up' or `org-table-move-row' or
  18268. `org-move-item-up', depending on context. See the individual commands
  18269. for more information."
  18270. (interactive "P")
  18271. (cond
  18272. ((run-hook-with-args-until-success 'org-metaup-hook))
  18273. ((org-region-active-p)
  18274. (let* ((a (min (region-beginning) (region-end)))
  18275. (b (1- (max (region-beginning) (region-end))))
  18276. (c (save-excursion (goto-char a)
  18277. (move-beginning-of-line 0)))
  18278. (d (save-excursion (goto-char a)
  18279. (move-end-of-line 0) (point))))
  18280. (transpose-regions a b c d)
  18281. (goto-char c)))
  18282. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18283. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18284. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18285. (t (org-drag-element-backward))))
  18286. (defun org-metadown (&optional arg)
  18287. "Move subtree down or move table row down.
  18288. Calls `org-move-subtree-down' or `org-table-move-row' or
  18289. `org-move-item-down', depending on context. See the individual
  18290. commands for more information."
  18291. (interactive "P")
  18292. (cond
  18293. ((run-hook-with-args-until-success 'org-metadown-hook))
  18294. ((org-region-active-p)
  18295. (let* ((a (min (region-beginning) (region-end)))
  18296. (b (max (region-beginning) (region-end)))
  18297. (c (save-excursion (goto-char b)
  18298. (move-beginning-of-line 1)))
  18299. (d (save-excursion (goto-char b)
  18300. (move-end-of-line 1) (1+ (point)))))
  18301. (transpose-regions a b c d)
  18302. (goto-char d)))
  18303. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18304. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18305. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18306. (t (org-drag-element-forward))))
  18307. (defun org-shiftup (&optional arg)
  18308. "Increase item in timestamp or increase priority of current headline.
  18309. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18310. depending on context. See the individual commands for more information."
  18311. (interactive "P")
  18312. (cond
  18313. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18314. ((and org-support-shift-select (org-region-active-p))
  18315. (org-call-for-shift-select 'previous-line))
  18316. ((org-at-timestamp-p t)
  18317. (call-interactively (if org-edit-timestamp-down-means-later
  18318. 'org-timestamp-down 'org-timestamp-up)))
  18319. ((and (not (eq org-support-shift-select 'always))
  18320. org-enable-priority-commands
  18321. (org-at-heading-p))
  18322. (call-interactively 'org-priority-up))
  18323. ((and (not org-support-shift-select) (org-at-item-p))
  18324. (call-interactively 'org-previous-item))
  18325. ((org-clocktable-try-shift 'up arg))
  18326. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18327. (org-support-shift-select
  18328. (org-call-for-shift-select 'previous-line))
  18329. (t (org-shiftselect-error))))
  18330. (defun org-shiftdown (&optional arg)
  18331. "Decrease item in timestamp or decrease priority of current headline.
  18332. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18333. depending on context. See the individual commands for more information."
  18334. (interactive "P")
  18335. (cond
  18336. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18337. ((and org-support-shift-select (org-region-active-p))
  18338. (org-call-for-shift-select 'next-line))
  18339. ((org-at-timestamp-p t)
  18340. (call-interactively (if org-edit-timestamp-down-means-later
  18341. 'org-timestamp-up 'org-timestamp-down)))
  18342. ((and (not (eq org-support-shift-select 'always))
  18343. org-enable-priority-commands
  18344. (org-at-heading-p))
  18345. (call-interactively 'org-priority-down))
  18346. ((and (not org-support-shift-select) (org-at-item-p))
  18347. (call-interactively 'org-next-item))
  18348. ((org-clocktable-try-shift 'down arg))
  18349. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18350. (org-support-shift-select
  18351. (org-call-for-shift-select 'next-line))
  18352. (t (org-shiftselect-error))))
  18353. (defun org-shiftright (&optional arg)
  18354. "Cycle the thing at point or in the current line, depending on context.
  18355. Depending on context, this does one of the following:
  18356. - switch a timestamp at point one day into the future
  18357. - on a headline, switch to the next TODO keyword.
  18358. - on an item, switch entire list to the next bullet type
  18359. - on a property line, switch to the next allowed value
  18360. - on a clocktable definition line, move time block into the future"
  18361. (interactive "P")
  18362. (cond
  18363. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18364. ((and org-support-shift-select (org-region-active-p))
  18365. (org-call-for-shift-select 'forward-char))
  18366. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18367. ((and (not (eq org-support-shift-select 'always))
  18368. (org-at-heading-p))
  18369. (let ((org-inhibit-logging
  18370. (not org-treat-S-cursor-todo-selection-as-state-change))
  18371. (org-inhibit-blocking
  18372. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18373. (org-call-with-arg 'org-todo 'right)))
  18374. ((or (and org-support-shift-select
  18375. (not (eq org-support-shift-select 'always))
  18376. (org-at-item-bullet-p))
  18377. (and (not org-support-shift-select) (org-at-item-p)))
  18378. (org-call-with-arg 'org-cycle-list-bullet nil))
  18379. ((and (not (eq org-support-shift-select 'always))
  18380. (org-at-property-p))
  18381. (call-interactively 'org-property-next-allowed-value))
  18382. ((org-clocktable-try-shift 'right arg))
  18383. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18384. (org-support-shift-select
  18385. (org-call-for-shift-select 'forward-char))
  18386. (t (org-shiftselect-error))))
  18387. (defun org-shiftleft (&optional arg)
  18388. "Cycle the thing at point or in the current line, depending on context.
  18389. Depending on context, this does one of the following:
  18390. - switch a timestamp at point one day into the past
  18391. - on a headline, switch to the previous TODO keyword.
  18392. - on an item, switch entire list to the previous bullet type
  18393. - on a property line, switch to the previous allowed value
  18394. - on a clocktable definition line, move time block into the past"
  18395. (interactive "P")
  18396. (cond
  18397. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18398. ((and org-support-shift-select (org-region-active-p))
  18399. (org-call-for-shift-select 'backward-char))
  18400. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18401. ((and (not (eq org-support-shift-select 'always))
  18402. (org-at-heading-p))
  18403. (let ((org-inhibit-logging
  18404. (not org-treat-S-cursor-todo-selection-as-state-change))
  18405. (org-inhibit-blocking
  18406. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18407. (org-call-with-arg 'org-todo 'left)))
  18408. ((or (and org-support-shift-select
  18409. (not (eq org-support-shift-select 'always))
  18410. (org-at-item-bullet-p))
  18411. (and (not org-support-shift-select) (org-at-item-p)))
  18412. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18413. ((and (not (eq org-support-shift-select 'always))
  18414. (org-at-property-p))
  18415. (call-interactively 'org-property-previous-allowed-value))
  18416. ((org-clocktable-try-shift 'left arg))
  18417. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18418. (org-support-shift-select
  18419. (org-call-for-shift-select 'backward-char))
  18420. (t (org-shiftselect-error))))
  18421. (defun org-shiftcontrolright ()
  18422. "Switch to next TODO set."
  18423. (interactive)
  18424. (cond
  18425. ((and org-support-shift-select (org-region-active-p))
  18426. (org-call-for-shift-select 'forward-word))
  18427. ((and (not (eq org-support-shift-select 'always))
  18428. (org-at-heading-p))
  18429. (org-call-with-arg 'org-todo 'nextset))
  18430. (org-support-shift-select
  18431. (org-call-for-shift-select 'forward-word))
  18432. (t (org-shiftselect-error))))
  18433. (defun org-shiftcontrolleft ()
  18434. "Switch to previous TODO set."
  18435. (interactive)
  18436. (cond
  18437. ((and org-support-shift-select (org-region-active-p))
  18438. (org-call-for-shift-select 'backward-word))
  18439. ((and (not (eq org-support-shift-select 'always))
  18440. (org-at-heading-p))
  18441. (org-call-with-arg 'org-todo 'previousset))
  18442. (org-support-shift-select
  18443. (org-call-for-shift-select 'backward-word))
  18444. (t (org-shiftselect-error))))
  18445. (defun org-shiftcontrolup (&optional n)
  18446. "Change timestamps synchronously up in CLOCK log lines.
  18447. Optional argument N tells to change by that many units."
  18448. (interactive "P")
  18449. (cond ((and (not org-support-shift-select)
  18450. (org-at-clock-log-p)
  18451. (org-at-timestamp-p t))
  18452. (org-clock-timestamps-up n))
  18453. (t (org-shiftselect-error))))
  18454. (defun org-shiftcontroldown (&optional n)
  18455. "Change timestamps synchronously down in CLOCK log lines.
  18456. Optional argument N tells to change by that many units."
  18457. (interactive "P")
  18458. (cond ((and (not org-support-shift-select)
  18459. (org-at-clock-log-p)
  18460. (org-at-timestamp-p t))
  18461. (org-clock-timestamps-down n))
  18462. (t (org-shiftselect-error))))
  18463. (defun org-ctrl-c-ret ()
  18464. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18465. (interactive)
  18466. (cond
  18467. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18468. (t (call-interactively 'org-insert-heading))))
  18469. (defun org-find-visible ()
  18470. (let ((s (point)))
  18471. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18472. (get-char-property s 'invisible)))
  18473. s))
  18474. (defun org-find-invisible ()
  18475. (let ((s (point)))
  18476. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18477. (not (get-char-property s 'invisible))))
  18478. s))
  18479. (defun org-copy-visible (beg end)
  18480. "Copy the visible parts of the region."
  18481. (interactive "r")
  18482. (let (snippets s)
  18483. (save-excursion
  18484. (save-restriction
  18485. (narrow-to-region beg end)
  18486. (setq s (goto-char (point-min)))
  18487. (while (not (= (point) (point-max)))
  18488. (goto-char (org-find-invisible))
  18489. (push (buffer-substring s (point)) snippets)
  18490. (setq s (goto-char (org-find-visible))))))
  18491. (kill-new (apply 'concat (nreverse snippets)))))
  18492. (defun org-copy-special ()
  18493. "Copy region in table or copy current subtree.
  18494. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18495. See the individual commands for more information."
  18496. (interactive)
  18497. (call-interactively
  18498. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18499. (defun org-cut-special ()
  18500. "Cut region in table or cut current subtree.
  18501. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18502. See the individual commands for more information."
  18503. (interactive)
  18504. (call-interactively
  18505. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18506. (defun org-paste-special (arg)
  18507. "Paste rectangular region into table, or past subtree relative to level.
  18508. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18509. See the individual commands for more information."
  18510. (interactive "P")
  18511. (if (org-at-table-p)
  18512. (org-table-paste-rectangle)
  18513. (org-paste-subtree arg)))
  18514. (defsubst org-in-fixed-width-region-p ()
  18515. "Is point in a fixed-width region?"
  18516. (save-match-data
  18517. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18518. (defun org-edit-special (&optional arg)
  18519. "Call a special editor for the element at point.
  18520. When at a table, call the formula editor with `org-table-edit-formulas'.
  18521. When in a source code block, call `org-edit-src-code'.
  18522. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18523. When at an #+INCLUDE keyword, visit the included file.
  18524. On a link, call `ffap' to visit the link at point.
  18525. Otherwise, return a user error."
  18526. (interactive "P")
  18527. (let ((element (org-element-at-point)))
  18528. (assert (not buffer-read-only) nil
  18529. "Buffer is read-only: %s" (buffer-name))
  18530. (case (org-element-type element)
  18531. (src-block
  18532. (if (not arg) (org-edit-src-code)
  18533. (let* ((info (org-babel-get-src-block-info))
  18534. (lang (nth 0 info))
  18535. (params (nth 2 info))
  18536. (session (cdr (assq :session params))))
  18537. (if (not session) (org-edit-src-code)
  18538. ;; At a src-block with a session and function called with
  18539. ;; an ARG: switch to the buffer related to the inferior
  18540. ;; process.
  18541. (switch-to-buffer
  18542. (funcall (intern (concat "org-babel-prep-session:" lang))
  18543. session params))))))
  18544. (keyword
  18545. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18546. (find-file
  18547. (org-remove-double-quotes
  18548. (car (org-split-string (org-element-property :value element)))))
  18549. (user-error "No special environment to edit here")))
  18550. (table
  18551. (if (eq (org-element-property :type element) 'table.el)
  18552. (org-edit-src-code)
  18553. (call-interactively 'org-table-edit-formulas)))
  18554. ;; Only Org tables contain `table-row' type elements.
  18555. (table-row (call-interactively 'org-table-edit-formulas))
  18556. ((example-block export-block) (org-edit-src-code))
  18557. (fixed-width (org-edit-fixed-width-region))
  18558. (otherwise
  18559. ;; No notable element at point. Though, we may be at a link,
  18560. ;; which is an object. Thus, scan deeper.
  18561. (if (eq (org-element-type (org-element-context element)) 'link)
  18562. (call-interactively 'ffap)
  18563. (user-error "No special environment to edit here"))))))
  18564. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18565. (defun org-ctrl-c-ctrl-c (&optional arg)
  18566. "Set tags in headline, or update according to changed information at point.
  18567. This command does many different things, depending on context:
  18568. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18569. this is what we do.
  18570. - If the cursor is on a statistics cookie, update it.
  18571. - If the cursor is in a headline, prompt for tags and insert them
  18572. into the current line, aligned to `org-tags-column'. When called
  18573. with prefix arg, realign all tags in the current buffer.
  18574. - If the cursor is in one of the special #+KEYWORD lines, this
  18575. triggers scanning the buffer for these lines and updating the
  18576. information.
  18577. - If the cursor is inside a table, realign the table. This command
  18578. works even if the automatic table editor has been turned off.
  18579. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18580. the entire table.
  18581. - If the cursor is at a footnote reference or definition, jump to
  18582. the corresponding definition or references, respectively.
  18583. - If the cursor is a the beginning of a dynamic block, update it.
  18584. - If the current buffer is a capture buffer, close note and file it.
  18585. - If the cursor is on a <<<target>>>, update radio targets and
  18586. corresponding links in this buffer.
  18587. - If the cursor is on a numbered item in a plain list, renumber the
  18588. ordered list.
  18589. - If the cursor is on a checkbox, toggle it.
  18590. - If the cursor is on a code block, evaluate it. The variable
  18591. `org-confirm-babel-evaluate' can be used to control prompting
  18592. before code block evaluation, by default every code block
  18593. evaluation requires confirmation. Code block evaluation can be
  18594. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18595. (interactive "P")
  18596. (cond
  18597. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18598. org-occur-highlights
  18599. org-latex-fragment-image-overlays)
  18600. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18601. (org-remove-occur-highlights)
  18602. (org-remove-latex-fragment-image-overlays)
  18603. (message "Temporary highlights/overlays removed from current buffer"))
  18604. ((and (local-variable-p 'org-finish-function (current-buffer))
  18605. (fboundp org-finish-function))
  18606. (funcall org-finish-function))
  18607. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18608. (t
  18609. (let* ((context (org-element-context)) (type (org-element-type context)))
  18610. ;; Test if point is within a blank line.
  18611. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18612. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18613. (user-error "C-c C-c can do nothing useful at this location"))
  18614. ;; When at a link, act according to the parent instead.
  18615. (when (eq type 'link)
  18616. (setq context (org-element-property :parent context))
  18617. (setq type (org-element-type context)))
  18618. ;; For convenience: at the first line of a paragraph on the
  18619. ;; same line as an item, apply function on that item instead.
  18620. (when (eq type 'paragraph)
  18621. (let ((parent (org-element-property :parent context)))
  18622. (when (and (eq (org-element-type parent) 'item)
  18623. (= (point-at-bol) (org-element-property :begin parent)))
  18624. (setq context parent type 'item))))
  18625. ;; Act according to type of element or object at point.
  18626. (case type
  18627. (clock (org-clock-update-time-maybe))
  18628. (dynamic-block
  18629. (save-excursion
  18630. (goto-char (org-element-property :post-affiliated context))
  18631. (org-update-dblock)))
  18632. (footnote-definition
  18633. (goto-char (org-element-property :post-affiliated context))
  18634. (call-interactively 'org-footnote-action))
  18635. (footnote-reference (call-interactively 'org-footnote-action))
  18636. ((headline inlinetask)
  18637. (save-excursion (goto-char (org-element-property :begin context))
  18638. (call-interactively 'org-set-tags)))
  18639. (item
  18640. ;; At an item: a double C-u set checkbox to "[-]"
  18641. ;; unconditionally, whereas a single one will toggle its
  18642. ;; presence. Without an universal argument, if the item
  18643. ;; has a checkbox, toggle it. Otherwise repair the list.
  18644. (let* ((box (org-element-property :checkbox context))
  18645. (struct (org-element-property :structure context))
  18646. (old-struct (copy-tree struct))
  18647. (parents (org-list-parents-alist struct))
  18648. (prevs (org-list-prevs-alist struct))
  18649. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18650. (org-list-set-checkbox
  18651. (org-element-property :begin context) struct
  18652. (cond ((equal arg '(16)) "[-]")
  18653. ((and (not box) (equal arg '(4))) "[ ]")
  18654. ((or (not box) (equal arg '(4))) nil)
  18655. ((eq box 'on) "[ ]")
  18656. (t "[X]")))
  18657. ;; Mimic `org-list-write-struct' but with grabbing
  18658. ;; a return value from `org-list-struct-fix-box'.
  18659. (org-list-struct-fix-ind struct parents 2)
  18660. (org-list-struct-fix-item-end struct)
  18661. (org-list-struct-fix-bul struct prevs)
  18662. (org-list-struct-fix-ind struct parents)
  18663. (let ((block-item
  18664. (org-list-struct-fix-box struct parents prevs orderedp)))
  18665. (if (and box (equal struct old-struct))
  18666. (if (equal arg '(16))
  18667. (message "Checkboxes already reset")
  18668. (user-error "Cannot toggle this checkbox: %s"
  18669. (if (eq box 'on)
  18670. "all subitems checked"
  18671. "unchecked subitems")))
  18672. (org-list-struct-apply-struct struct old-struct)
  18673. (org-update-checkbox-count-maybe))
  18674. (when block-item
  18675. (message "Checkboxes were removed due to empty box at line %d"
  18676. (org-current-line block-item))))))
  18677. (keyword
  18678. (let ((org-inhibit-startup-visibility-stuff t)
  18679. (org-startup-align-all-tables nil))
  18680. (when (boundp 'org-table-coordinate-overlays)
  18681. (mapc 'delete-overlay org-table-coordinate-overlays)
  18682. (setq org-table-coordinate-overlays nil))
  18683. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18684. (message "Local setup has been refreshed"))
  18685. (plain-list
  18686. ;; At a plain list, with a double C-u argument, set
  18687. ;; checkboxes of each item to "[-]", whereas a single one
  18688. ;; will toggle their presence according to the state of the
  18689. ;; first item in the list. Without an argument, repair the
  18690. ;; list.
  18691. (let* ((begin (org-element-property :contents-begin context))
  18692. (beginm (move-marker (make-marker) begin))
  18693. (struct (org-element-property :structure context))
  18694. (old-struct (copy-tree struct))
  18695. (first-box (save-excursion
  18696. (goto-char begin)
  18697. (looking-at org-list-full-item-re)
  18698. (match-string-no-properties 3)))
  18699. (new-box (cond ((equal arg '(16)) "[-]")
  18700. ((equal arg '(4)) (unless first-box "[ ]"))
  18701. ((equal first-box "[X]") "[ ]")
  18702. (t "[X]"))))
  18703. (cond
  18704. (arg
  18705. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18706. (org-list-get-all-items
  18707. begin struct (org-list-prevs-alist struct))))
  18708. ((and first-box (eq (point) begin))
  18709. ;; For convenience, when point is at bol on the first
  18710. ;; item of the list and no argument is provided, simply
  18711. ;; toggle checkbox of that item, if any.
  18712. (org-list-set-checkbox begin struct new-box)))
  18713. (org-list-write-struct
  18714. struct (org-list-parents-alist struct) old-struct)
  18715. (org-update-checkbox-count-maybe)
  18716. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18717. ((property-drawer node-property)
  18718. (call-interactively 'org-property-action))
  18719. ((radio-target target)
  18720. (call-interactively 'org-update-radio-target-regexp))
  18721. (statistics-cookie
  18722. (call-interactively 'org-update-statistics-cookies))
  18723. ((table table-cell table-row)
  18724. ;; At a table, recalculate every field and align it. Also
  18725. ;; send the table if necessary. If the table has
  18726. ;; a `table.el' type, just give up. At a table row or
  18727. ;; cell, maybe recalculate line but always align table.
  18728. (if (eq (org-element-property :type context) 'table.el)
  18729. (message "Use C-c ' to edit table.el tables")
  18730. (let ((org-enable-table-editor t))
  18731. (if (or (eq type 'table)
  18732. ;; Check if point is at a TBLFM line.
  18733. (and (eq type 'table-row)
  18734. (= (point) (org-element-property :end context))))
  18735. (save-excursion
  18736. (if (org-at-TBLFM-p)
  18737. (progn (require 'org-table)
  18738. (org-table-calc-current-TBLFM))
  18739. (goto-char (org-element-property :contents-begin context))
  18740. (org-call-with-arg 'org-table-recalculate (or arg t))
  18741. (orgtbl-send-table 'maybe)))
  18742. (org-table-maybe-eval-formula)
  18743. (cond (arg (call-interactively 'org-table-recalculate))
  18744. ((org-table-maybe-recalculate-line))
  18745. (t (org-table-align)))))))
  18746. (timestamp (org-timestamp-change 0 'day))
  18747. (otherwise
  18748. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18749. (user-error
  18750. "C-c C-c can do nothing useful at this location")))))))))
  18751. (defun org-mode-restart ()
  18752. "Restart Org-mode, to scan again for special lines.
  18753. Also updates the keyword regular expressions."
  18754. (interactive)
  18755. (org-mode)
  18756. (message "Org-mode restarted"))
  18757. (defun org-kill-note-or-show-branches ()
  18758. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18759. (interactive)
  18760. (if (not org-finish-function)
  18761. (progn
  18762. (hide-subtree)
  18763. (call-interactively 'show-branches))
  18764. (let ((org-note-abort t))
  18765. (funcall org-finish-function))))
  18766. (defun org-open-line (n)
  18767. "Insert a new row in tables, call `open-line' elsewhere.
  18768. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18769. (interactive "*p")
  18770. (cond
  18771. ((not org-special-ctrl-o)
  18772. (open-line n))
  18773. ((org-at-table-p)
  18774. (org-table-insert-row))
  18775. (t
  18776. (open-line n))))
  18777. (defun org-return (&optional indent)
  18778. "Goto next table row or insert a newline.
  18779. Calls `org-table-next-row' or `newline', depending on context.
  18780. See the individual commands for more information."
  18781. (interactive)
  18782. (let (org-ts-what)
  18783. (cond
  18784. ((or (bobp) (org-in-src-block-p))
  18785. (if indent (newline-and-indent) (newline)))
  18786. ((org-at-table-p)
  18787. (org-table-justify-field-maybe)
  18788. (call-interactively 'org-table-next-row))
  18789. ;; when `newline-and-indent' is called within a list, make sure
  18790. ;; text moved stays inside the item.
  18791. ((and (org-in-item-p) indent)
  18792. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18793. (progn
  18794. (save-match-data (newline))
  18795. (org-indent-line-to (length (match-string 0))))
  18796. (let ((ind (org-get-indentation)))
  18797. (newline)
  18798. (if (org-looking-back org-list-end-re)
  18799. (org-indent-line)
  18800. (org-indent-line-to ind)))))
  18801. ((and org-return-follows-link
  18802. (org-at-timestamp-p t)
  18803. (not (eq org-ts-what 'after)))
  18804. (org-follow-timestamp-link))
  18805. ((and org-return-follows-link
  18806. (let ((tprop (get-text-property (point) 'face)))
  18807. (or (eq tprop 'org-link)
  18808. (and (listp tprop) (memq 'org-link tprop)))))
  18809. (call-interactively 'org-open-at-point))
  18810. ((and (org-at-heading-p)
  18811. (looking-at
  18812. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18813. (org-show-entry)
  18814. (end-of-line 1)
  18815. (newline))
  18816. (t (if indent (newline-and-indent) (newline))))))
  18817. (defun org-return-indent ()
  18818. "Goto next table row or insert a newline and indent.
  18819. Calls `org-table-next-row' or `newline-and-indent', depending on
  18820. context. See the individual commands for more information."
  18821. (interactive)
  18822. (org-return t))
  18823. (defun org-ctrl-c-star ()
  18824. "Compute table, or change heading status of lines.
  18825. Calls `org-table-recalculate' or `org-toggle-heading',
  18826. depending on context."
  18827. (interactive)
  18828. (cond
  18829. ((org-at-table-p)
  18830. (call-interactively 'org-table-recalculate))
  18831. (t
  18832. ;; Convert all lines in region to list items
  18833. (call-interactively 'org-toggle-heading))))
  18834. (defun org-ctrl-c-minus ()
  18835. "Insert separator line in table or modify bullet status of line.
  18836. Also turns a plain line or a region of lines into list items.
  18837. Calls `org-table-insert-hline', `org-toggle-item', or
  18838. `org-cycle-list-bullet', depending on context."
  18839. (interactive)
  18840. (cond
  18841. ((org-at-table-p)
  18842. (call-interactively 'org-table-insert-hline))
  18843. ((org-region-active-p)
  18844. (call-interactively 'org-toggle-item))
  18845. ((org-in-item-p)
  18846. (call-interactively 'org-cycle-list-bullet))
  18847. (t
  18848. (call-interactively 'org-toggle-item))))
  18849. (defun org-toggle-item (arg)
  18850. "Convert headings or normal lines to items, items to normal lines.
  18851. If there is no active region, only the current line is considered.
  18852. If the first non blank line in the region is a headline, convert
  18853. all headlines to items, shifting text accordingly.
  18854. If it is an item, convert all items to normal lines.
  18855. If it is normal text, change region into a list of items.
  18856. With a prefix argument ARG, change the region in a single item."
  18857. (interactive "P")
  18858. (let ((shift-text
  18859. (function
  18860. ;; Shift text in current section to IND, from point to END.
  18861. ;; The function leaves point to END line.
  18862. (lambda (ind end)
  18863. (let ((min-i 1000) (end (copy-marker end)))
  18864. ;; First determine the minimum indentation (MIN-I) of
  18865. ;; the text.
  18866. (save-excursion
  18867. (catch 'exit
  18868. (while (< (point) end)
  18869. (let ((i (org-get-indentation)))
  18870. (cond
  18871. ;; Skip blank lines and inline tasks.
  18872. ((looking-at "^[ \t]*$"))
  18873. ((looking-at org-outline-regexp-bol))
  18874. ;; We can't find less than 0 indentation.
  18875. ((zerop i) (throw 'exit (setq min-i 0)))
  18876. ((< i min-i) (setq min-i i))))
  18877. (forward-line))))
  18878. ;; Then indent each line so that a line indented to
  18879. ;; MIN-I becomes indented to IND. Ignore blank lines
  18880. ;; and inline tasks in the process.
  18881. (let ((delta (- ind min-i)))
  18882. (while (< (point) end)
  18883. (unless (or (looking-at "^[ \t]*$")
  18884. (looking-at org-outline-regexp-bol))
  18885. (org-indent-line-to (+ (org-get-indentation) delta)))
  18886. (forward-line)))))))
  18887. (skip-blanks
  18888. (function
  18889. ;; Return beginning of first non-blank line, starting from
  18890. ;; line at POS.
  18891. (lambda (pos)
  18892. (save-excursion
  18893. (goto-char pos)
  18894. (skip-chars-forward " \r\t\n")
  18895. (point-at-bol)))))
  18896. beg end)
  18897. ;; Determine boundaries of changes.
  18898. (if (org-region-active-p)
  18899. (setq beg (funcall skip-blanks (region-beginning))
  18900. end (copy-marker (region-end)))
  18901. (setq beg (funcall skip-blanks (point-at-bol))
  18902. end (copy-marker (point-at-eol))))
  18903. ;; Depending on the starting line, choose an action on the text
  18904. ;; between BEG and END.
  18905. (org-with-limited-levels
  18906. (save-excursion
  18907. (goto-char beg)
  18908. (cond
  18909. ;; Case 1. Start at an item: de-itemize. Note that it only
  18910. ;; happens when a region is active: `org-ctrl-c-minus'
  18911. ;; would call `org-cycle-list-bullet' otherwise.
  18912. ((org-at-item-p)
  18913. (while (< (point) end)
  18914. (when (org-at-item-p)
  18915. (skip-chars-forward " \t")
  18916. (delete-region (point) (match-end 0)))
  18917. (forward-line)))
  18918. ;; Case 2. Start at an heading: convert to items.
  18919. ((org-at-heading-p)
  18920. (let* ((bul (org-list-bullet-string "-"))
  18921. (bul-len (length bul))
  18922. ;; Indentation of the first heading. It should be
  18923. ;; relative to the indentation of its parent, if any.
  18924. (start-ind (save-excursion
  18925. (cond
  18926. ((not org-adapt-indentation) 0)
  18927. ((not (outline-previous-heading)) 0)
  18928. (t (length (match-string 0))))))
  18929. ;; Level of first heading. Further headings will be
  18930. ;; compared to it to determine hierarchy in the list.
  18931. (ref-level (org-reduced-level (org-outline-level))))
  18932. (while (< (point) end)
  18933. (let* ((level (org-reduced-level (org-outline-level)))
  18934. (delta (max 0 (- level ref-level))))
  18935. ;; If current headline is less indented than the first
  18936. ;; one, set it as reference, in order to preserve
  18937. ;; subtrees.
  18938. (when (< level ref-level) (setq ref-level level))
  18939. (replace-match bul t t)
  18940. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18941. ;; Ensure all text down to END (or SECTION-END) belongs
  18942. ;; to the newly created item.
  18943. (let ((section-end (save-excursion
  18944. (or (outline-next-heading) (point)))))
  18945. (forward-line)
  18946. (funcall shift-text
  18947. (+ start-ind (* (1+ delta) bul-len))
  18948. (min end section-end)))))))
  18949. ;; Case 3. Normal line with ARG: make the first line of region
  18950. ;; an item, and shift indentation of others lines to
  18951. ;; set them as item's body.
  18952. (arg (let* ((bul (org-list-bullet-string "-"))
  18953. (bul-len (length bul))
  18954. (ref-ind (org-get-indentation)))
  18955. (skip-chars-forward " \t")
  18956. (insert bul)
  18957. (forward-line)
  18958. (while (< (point) end)
  18959. ;; Ensure that lines less indented than first one
  18960. ;; still get included in item body.
  18961. (funcall shift-text
  18962. (+ ref-ind bul-len)
  18963. (min end (save-excursion (or (outline-next-heading)
  18964. (point)))))
  18965. (forward-line))))
  18966. ;; Case 4. Normal line without ARG: turn each non-item line
  18967. ;; into an item.
  18968. (t
  18969. (while (< (point) end)
  18970. (unless (or (org-at-heading-p) (org-at-item-p))
  18971. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18972. (replace-match
  18973. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18974. (forward-line))))))))
  18975. (defun org-toggle-heading (&optional nstars)
  18976. "Convert headings to normal text, or items or text to headings.
  18977. If there is no active region, only convert the current line.
  18978. With a \\[universal-argument] prefix, convert the whole list at
  18979. point into heading.
  18980. In a region:
  18981. - If the first non blank line is a headline, remove the stars
  18982. from all headlines in the region.
  18983. - If it is a normal line, turn each and every normal line (i.e.,
  18984. not an heading or an item) in the region into headings. If you
  18985. want to convert only the first line of this region, use one
  18986. universal prefix argument.
  18987. - If it is a plain list item, turn all plain list items into headings.
  18988. When converting a line into a heading, the number of stars is chosen
  18989. such that the lines become children of the current entry. However,
  18990. when a numeric prefix argument is given, its value determines the
  18991. number of stars to add."
  18992. (interactive "P")
  18993. (let ((skip-blanks
  18994. (function
  18995. ;; Return beginning of first non-blank line, starting from
  18996. ;; line at POS.
  18997. (lambda (pos)
  18998. (save-excursion
  18999. (goto-char pos)
  19000. (while (org-at-comment-p) (forward-line))
  19001. (skip-chars-forward " \r\t\n")
  19002. (point-at-bol)))))
  19003. beg end toggled)
  19004. ;; Determine boundaries of changes. If a universal prefix has
  19005. ;; been given, put the list in a region. If region ends at a bol,
  19006. ;; do not consider the last line to be in the region.
  19007. (when (and current-prefix-arg (org-at-item-p))
  19008. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19009. (org-mark-element))
  19010. (if (org-region-active-p)
  19011. (setq beg (funcall skip-blanks (region-beginning))
  19012. end (copy-marker (save-excursion
  19013. (goto-char (region-end))
  19014. (if (bolp) (point) (point-at-eol)))))
  19015. (setq beg (funcall skip-blanks (point-at-bol))
  19016. end (copy-marker (point-at-eol))))
  19017. ;; Ensure inline tasks don't count as headings.
  19018. (org-with-limited-levels
  19019. (save-excursion
  19020. (goto-char beg)
  19021. (cond
  19022. ;; Case 1. Started at an heading: de-star headings.
  19023. ((org-at-heading-p)
  19024. (while (< (point) end)
  19025. (when (org-at-heading-p t)
  19026. (looking-at org-outline-regexp) (replace-match "")
  19027. (setq toggled t))
  19028. (forward-line)))
  19029. ;; Case 2. Started at an item: change items into headlines.
  19030. ;; One star will be added by `org-list-to-subtree'.
  19031. ((org-at-item-p)
  19032. (let* ((stars (make-string
  19033. ;; subtract the star that will be added again by
  19034. ;; `org-list-to-subtree'
  19035. (if (numberp nstars) (1- nstars)
  19036. (or (org-current-level) 0))
  19037. ?*))
  19038. (add-stars
  19039. (cond (nstars "") ; stars from prefix only
  19040. ((equal stars "") "") ; before first heading
  19041. (org-odd-levels-only "*") ; inside heading, odd
  19042. (t "")))) ; inside heading, oddeven
  19043. (while (< (point) end)
  19044. (when (org-at-item-p)
  19045. ;; Pay attention to cases when region ends before list.
  19046. (let* ((struct (org-list-struct))
  19047. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19048. (save-restriction
  19049. (narrow-to-region (point) list-end)
  19050. (insert
  19051. (org-list-to-subtree
  19052. (org-list-parse-list t)
  19053. '(:istart (concat stars add-stars (funcall get-stars depth))
  19054. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19055. (setq toggled t))
  19056. (forward-line))))
  19057. ;; Case 3. Started at normal text: make every line an heading,
  19058. ;; skipping headlines and items.
  19059. (t (let* ((stars
  19060. (make-string
  19061. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19062. (add-stars
  19063. (cond (nstars "") ; stars from prefix only
  19064. ((equal stars "") "*") ; before first heading
  19065. (org-odd-levels-only "**") ; inside heading, odd
  19066. (t "*"))) ; inside heading, oddeven
  19067. (rpl (concat stars add-stars " "))
  19068. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19069. (while (< (point) (if (equal nstars '(4)) lend end))
  19070. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19071. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19072. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19073. (forward-line)))))))
  19074. (unless toggled (message "Cannot toggle heading from here"))))
  19075. (defun org-meta-return (&optional arg)
  19076. "Insert a new heading or wrap a region in a table.
  19077. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19078. on context. See the individual commands for more information."
  19079. (interactive "P")
  19080. (org-check-before-invisible-edit 'insert)
  19081. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19082. (let* ((element (org-element-at-point))
  19083. (type (org-element-type element)))
  19084. (when (eq type 'table-row)
  19085. (setq element (org-element-property :parent element))
  19086. (setq type 'table))
  19087. (if (and (eq type 'table)
  19088. (eq (org-element-property :type element) 'org)
  19089. (>= (point) (org-element-property :contents-begin element))
  19090. (< (point) (org-element-property :contents-end element)))
  19091. (call-interactively 'org-table-wrap-region)
  19092. (call-interactively 'org-insert-heading)))))
  19093. ;;; Menu entries
  19094. (defsubst org-in-subtree-not-table-p ()
  19095. "Are we in a subtree and not in a table?"
  19096. (and (not (org-before-first-heading-p))
  19097. (not (org-at-table-p))))
  19098. ;; Define the Org-mode menus
  19099. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19100. '("Tbl"
  19101. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19102. ["Next Field" org-cycle (org-at-table-p)]
  19103. ["Previous Field" org-shifttab (org-at-table-p)]
  19104. ["Next Row" org-return (org-at-table-p)]
  19105. "--"
  19106. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19107. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19108. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19109. "--"
  19110. ("Column"
  19111. ["Move Column Left" org-metaleft (org-at-table-p)]
  19112. ["Move Column Right" org-metaright (org-at-table-p)]
  19113. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19114. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19115. ("Row"
  19116. ["Move Row Up" org-metaup (org-at-table-p)]
  19117. ["Move Row Down" org-metadown (org-at-table-p)]
  19118. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19119. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19120. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19121. "--"
  19122. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19123. ("Rectangle"
  19124. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19125. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19126. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19127. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19128. "--"
  19129. ("Calculate"
  19130. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19131. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19132. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19133. "--"
  19134. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19135. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19136. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19137. "--"
  19138. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19139. "--"
  19140. ["Sum Column/Rectangle" org-table-sum
  19141. (or (org-at-table-p) (org-region-active-p))]
  19142. ["Which Column?" org-table-current-column (org-at-table-p)])
  19143. ["Debug Formulas"
  19144. org-table-toggle-formula-debugger
  19145. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19146. ["Show Col/Row Numbers"
  19147. org-table-toggle-coordinate-overlays
  19148. :style toggle
  19149. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19150. "--"
  19151. ["Create" org-table-create (and (not (org-at-table-p))
  19152. org-enable-table-editor)]
  19153. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19154. ["Import from File" org-table-import (not (org-at-table-p))]
  19155. ["Export to File" org-table-export (org-at-table-p)]
  19156. "--"
  19157. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19158. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19159. '("Org"
  19160. ("Show/Hide"
  19161. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19162. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19163. ["Sparse Tree..." org-sparse-tree t]
  19164. ["Reveal Context" org-reveal t]
  19165. ["Show All" show-all t]
  19166. "--"
  19167. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19168. "--"
  19169. ["New Heading" org-insert-heading t]
  19170. ("Navigate Headings"
  19171. ["Up" outline-up-heading t]
  19172. ["Next" outline-next-visible-heading t]
  19173. ["Previous" outline-previous-visible-heading t]
  19174. ["Next Same Level" outline-forward-same-level t]
  19175. ["Previous Same Level" outline-backward-same-level t]
  19176. "--"
  19177. ["Jump" org-goto t])
  19178. ("Edit Structure"
  19179. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19180. "--"
  19181. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19182. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19183. "--"
  19184. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19185. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19186. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19187. "--"
  19188. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19189. "--"
  19190. ["Copy visible text" org-copy-visible t]
  19191. "--"
  19192. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19193. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19194. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19195. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19196. "--"
  19197. ["Sort Region/Children" org-sort t]
  19198. "--"
  19199. ["Convert to odd levels" org-convert-to-odd-levels t]
  19200. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19201. ("Editing"
  19202. ["Emphasis..." org-emphasize t]
  19203. ["Edit Source Example" org-edit-special t]
  19204. "--"
  19205. ["Footnote new/jump" org-footnote-action t]
  19206. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19207. ("Archive"
  19208. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19209. "--"
  19210. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19211. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19212. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19213. )
  19214. "--"
  19215. ("Hyperlinks"
  19216. ["Store Link (Global)" org-store-link t]
  19217. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19218. ["Insert Link" org-insert-link t]
  19219. ["Follow Link" org-open-at-point t]
  19220. "--"
  19221. ["Next link" org-next-link t]
  19222. ["Previous link" org-previous-link t]
  19223. "--"
  19224. ["Descriptive Links"
  19225. org-toggle-link-display
  19226. :style radio
  19227. :selected org-descriptive-links
  19228. ]
  19229. ["Literal Links"
  19230. org-toggle-link-display
  19231. :style radio
  19232. :selected (not org-descriptive-links)])
  19233. "--"
  19234. ("TODO Lists"
  19235. ["TODO/DONE/-" org-todo t]
  19236. ("Select keyword"
  19237. ["Next keyword" org-shiftright (org-at-heading-p)]
  19238. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19239. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19240. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19241. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19242. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19243. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19244. "--"
  19245. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19246. :selected org-enforce-todo-dependencies :style toggle :active t]
  19247. "Settings for tree at point"
  19248. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19249. :selected (org-entry-get nil "ORDERED")
  19250. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19251. ["Do Children parallel" org-toggle-ordered-property :style radio
  19252. :selected (not (org-entry-get nil "ORDERED"))
  19253. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19254. "--"
  19255. ["Set Priority" org-priority t]
  19256. ["Priority Up" org-shiftup t]
  19257. ["Priority Down" org-shiftdown t]
  19258. "--"
  19259. ["Get news from all feeds" org-feed-update-all t]
  19260. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19261. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19262. ("TAGS and Properties"
  19263. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19264. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19265. "--"
  19266. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19267. ["Column view of properties" org-columns t]
  19268. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19269. ("Dates and Scheduling"
  19270. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19271. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19272. ("Change Date"
  19273. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19274. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19275. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19276. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19277. ["Compute Time Range" org-evaluate-time-range t]
  19278. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19279. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19280. "--"
  19281. ["Custom time format" org-toggle-time-stamp-overlays
  19282. :style radio :selected org-display-custom-times]
  19283. "--"
  19284. ["Goto Calendar" org-goto-calendar t]
  19285. ["Date from Calendar" org-date-from-calendar t]
  19286. "--"
  19287. ["Start/Restart Timer" org-timer-start t]
  19288. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19289. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19290. ["Insert Timer String" org-timer t]
  19291. ["Insert Timer Item" org-timer-item t])
  19292. ("Logging work"
  19293. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19294. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19295. ["Clock out" org-clock-out t]
  19296. ["Clock cancel" org-clock-cancel t]
  19297. "--"
  19298. ["Mark as default task" org-clock-mark-default-task t]
  19299. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19300. ["Goto running clock" org-clock-goto t]
  19301. "--"
  19302. ["Display times" org-clock-display t]
  19303. ["Create clock table" org-clock-report t]
  19304. "--"
  19305. ["Record DONE time"
  19306. (progn (setq org-log-done (not org-log-done))
  19307. (message "Switching to %s will %s record a timestamp"
  19308. (car org-done-keywords)
  19309. (if org-log-done "automatically" "not")))
  19310. :style toggle :selected org-log-done])
  19311. "--"
  19312. ["Agenda Command..." org-agenda t]
  19313. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19314. ("File List for Agenda")
  19315. ("Special views current file"
  19316. ["TODO Tree" org-show-todo-tree t]
  19317. ["Check Deadlines" org-check-deadlines t]
  19318. ["Timeline" org-timeline t]
  19319. ["Tags/Property tree" org-match-sparse-tree t])
  19320. "--"
  19321. ["Export/Publish..." org-export-dispatch t]
  19322. ("LaTeX"
  19323. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19324. :selected org-cdlatex-mode]
  19325. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19326. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19327. ["Modify math symbol" org-cdlatex-math-modify
  19328. (org-inside-LaTeX-fragment-p)]
  19329. ["Insert citation" org-reftex-citation t]
  19330. "--"
  19331. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19332. "--"
  19333. ("MobileOrg"
  19334. ["Push Files and Views" org-mobile-push t]
  19335. ["Get Captured and Flagged" org-mobile-pull t]
  19336. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19337. "--"
  19338. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19339. "--"
  19340. ("Documentation"
  19341. ["Show Version" org-version t]
  19342. ["Info Documentation" org-info t])
  19343. ("Customize"
  19344. ["Browse Org Group" org-customize t]
  19345. "--"
  19346. ["Expand This Menu" org-create-customize-menu
  19347. (fboundp 'customize-menu-create)])
  19348. ["Send bug report" org-submit-bug-report t]
  19349. "--"
  19350. ("Refresh/Reload"
  19351. ["Refresh setup current buffer" org-mode-restart t]
  19352. ["Reload Org (after update)" org-reload t]
  19353. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19354. ))
  19355. (defun org-info (&optional node)
  19356. "Read documentation for Org-mode in the info system.
  19357. With optional NODE, go directly to that node."
  19358. (interactive)
  19359. (info (format "(org)%s" (or node ""))))
  19360. ;;;###autoload
  19361. (defun org-submit-bug-report ()
  19362. "Submit a bug report on Org-mode via mail.
  19363. Don't hesitate to report any problems or inaccurate documentation.
  19364. If you don't have setup sending mail from (X)Emacs, please copy the
  19365. output buffer into your mail program, as it gives us important
  19366. information about your Org-mode version and configuration."
  19367. (interactive)
  19368. (require 'reporter)
  19369. (org-load-modules-maybe)
  19370. (org-require-autoloaded-modules)
  19371. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19372. (reporter-submit-bug-report
  19373. "emacs-orgmode@gnu.org"
  19374. (org-version nil 'full)
  19375. (let (list)
  19376. (save-window-excursion
  19377. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19378. (delete-other-windows)
  19379. (erase-buffer)
  19380. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19381. We would like to add your full Org-mode and Outline configuration to the
  19382. bug report. This greatly simplifies the work of the maintainer and
  19383. other experts on the mailing list.
  19384. HOWEVER, some variables you have customized may contain private
  19385. information. The names of customers, colleagues, or friends, might
  19386. appear in the form of file names, tags, todo states, or search strings.
  19387. If you answer yes to the prompt, you might want to check and remove
  19388. such private information before sending the email.")
  19389. (add-text-properties (point-min) (point-max) '(face org-warning))
  19390. (when (yes-or-no-p "Include your Org-mode configuration ")
  19391. (mapatoms
  19392. (lambda (v)
  19393. (and (boundp v)
  19394. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19395. (or (and (symbol-value v)
  19396. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19397. (and
  19398. (get v 'custom-type) (get v 'standard-value)
  19399. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19400. (push v list)))))
  19401. (kill-buffer (get-buffer "*Warn about privacy*"))
  19402. list))
  19403. nil nil
  19404. "Remember to cover the basics, that is, what you expected to happen and
  19405. what in fact did happen. You don't know how to make a good report? See
  19406. http://orgmode.org/manual/Feedback.html#Feedback
  19407. Your bug report will be posted to the Org-mode mailing list.
  19408. ------------------------------------------------------------------------")
  19409. (save-excursion
  19410. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19411. (replace-match "\\1Bug: \\3 [\\2]")))))
  19412. (defun org-install-agenda-files-menu ()
  19413. (let ((bl (buffer-list)))
  19414. (save-excursion
  19415. (while bl
  19416. (set-buffer (pop bl))
  19417. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19418. (when (derived-mode-p 'org-mode)
  19419. (easy-menu-change
  19420. '("Org") "File List for Agenda"
  19421. (append
  19422. (list
  19423. ["Edit File List" (org-edit-agenda-file-list) t]
  19424. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19425. ["Remove Current File from List" org-remove-file t]
  19426. ["Cycle through agenda files" org-cycle-agenda-files t]
  19427. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19428. "--")
  19429. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19430. ;;;; Documentation
  19431. (defun org-require-autoloaded-modules ()
  19432. (interactive)
  19433. (mapc 'require
  19434. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19435. org-table org-timer)))
  19436. ;;;###autoload
  19437. (defun org-reload (&optional uncompiled)
  19438. "Reload all org lisp files.
  19439. With prefix arg UNCOMPILED, load the uncompiled versions."
  19440. (interactive "P")
  19441. (require 'loadhist)
  19442. (let* ((org-dir (org-find-library-dir "org"))
  19443. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19444. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19445. (remove-re (mapconcat 'identity
  19446. (mapcar (lambda (f) (concat "^" f "$"))
  19447. (list (if (featurep 'xemacs)
  19448. "org-colview"
  19449. "org-colview-xemacs")
  19450. "org" "org-loaddefs" "org-version"))
  19451. "\\|"))
  19452. (feats (delete-dups
  19453. (mapcar 'file-name-sans-extension
  19454. (mapcar 'file-name-nondirectory
  19455. (delq nil
  19456. (mapcar 'feature-file
  19457. features))))))
  19458. (lfeat (append
  19459. (sort
  19460. (setq feats
  19461. (delq nil (mapcar
  19462. (lambda (f)
  19463. (if (and (string-match feature-re f)
  19464. (not (string-match remove-re f)))
  19465. f nil))
  19466. feats)))
  19467. 'string-lessp)
  19468. (list "org-version" "org")))
  19469. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19470. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19471. load-uncore load-misses)
  19472. (setq load-misses
  19473. (delq 't
  19474. (mapcar (lambda (f)
  19475. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19476. (and (string= org-dir contrib-dir)
  19477. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19478. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19479. (add-to-list 'load-uncore f 'append)
  19480. 't)
  19481. f))
  19482. lfeat)))
  19483. (if load-uncore
  19484. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19485. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19486. (if load-misses
  19487. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19488. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19489. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19490. ;;;###autoload
  19491. (defun org-customize ()
  19492. "Call the customize function with org as argument."
  19493. (interactive)
  19494. (org-load-modules-maybe)
  19495. (org-require-autoloaded-modules)
  19496. (customize-browse 'org))
  19497. (defun org-create-customize-menu ()
  19498. "Create a full customization menu for Org-mode, insert it into the menu."
  19499. (interactive)
  19500. (org-load-modules-maybe)
  19501. (org-require-autoloaded-modules)
  19502. (if (fboundp 'customize-menu-create)
  19503. (progn
  19504. (easy-menu-change
  19505. '("Org") "Customize"
  19506. `(["Browse Org group" org-customize t]
  19507. "--"
  19508. ,(customize-menu-create 'org)
  19509. ["Set" Custom-set t]
  19510. ["Save" Custom-save t]
  19511. ["Reset to Current" Custom-reset-current t]
  19512. ["Reset to Saved" Custom-reset-saved t]
  19513. ["Reset to Standard Settings" Custom-reset-standard t]))
  19514. (message "\"Org\"-menu now contains full customization menu"))
  19515. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19516. ;;;; Miscellaneous stuff
  19517. ;;; Generally useful functions
  19518. (defun org-get-at-bol (property)
  19519. "Get text property PROPERTY at beginning of line."
  19520. (get-text-property (point-at-bol) property))
  19521. (defun org-find-text-property-in-string (prop s)
  19522. "Return the first non-nil value of property PROP in string S."
  19523. (or (get-text-property 0 prop s)
  19524. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19525. prop s)))
  19526. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19527. "Display the given MESSAGE as a warning."
  19528. (if (fboundp 'display-warning)
  19529. (display-warning 'org message
  19530. (if (featurep 'xemacs) 'warning :warning))
  19531. (let ((buf (get-buffer-create "*Org warnings*")))
  19532. (with-current-buffer buf
  19533. (goto-char (point-max))
  19534. (insert "Warning (Org): " message)
  19535. (unless (bolp)
  19536. (newline)))
  19537. (display-buffer buf)
  19538. (sit-for 0))))
  19539. (defun org-eval (form)
  19540. "Eval FORM and return result."
  19541. (condition-case error
  19542. (eval form)
  19543. (error (format "%%![Error: %s]" error))))
  19544. (defun org-in-clocktable-p ()
  19545. "Check if the cursor is in a clocktable."
  19546. (let ((pos (point)) start)
  19547. (save-excursion
  19548. (end-of-line 1)
  19549. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19550. (setq start (match-beginning 0))
  19551. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19552. (>= (match-end 0) pos)
  19553. start))))
  19554. (defun org-in-commented-line ()
  19555. "Is point in a line starting with `#'?"
  19556. (equal (char-after (point-at-bol)) ?#))
  19557. (defun org-in-indented-comment-line ()
  19558. "Is point in a line starting with `#' after some white space?"
  19559. (save-excursion
  19560. (save-match-data
  19561. (goto-char (point-at-bol))
  19562. (looking-at "[ \t]*#"))))
  19563. (defun org-in-verbatim-emphasis ()
  19564. (save-match-data
  19565. (and (org-in-regexp org-emph-re 2)
  19566. (>= (point) (match-beginning 3))
  19567. (<= (point) (match-end 4))
  19568. (member (match-string 3) '("=" "~")))))
  19569. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19570. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19571. (if (and marker (marker-buffer marker)
  19572. (buffer-live-p (marker-buffer marker)))
  19573. (progn
  19574. (org-pop-to-buffer-same-window (marker-buffer marker))
  19575. (if (or (> marker (point-max)) (< marker (point-min)))
  19576. (widen))
  19577. (goto-char marker)
  19578. (org-show-context 'org-goto))
  19579. (if bookmark
  19580. (bookmark-jump bookmark)
  19581. (error "Cannot find location"))))
  19582. (defun org-quote-csv-field (s)
  19583. "Quote field for inclusion in CSV material."
  19584. (if (string-match "[\",]" s)
  19585. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19586. s))
  19587. (defun org-force-self-insert (N)
  19588. "Needed to enforce self-insert under remapping."
  19589. (interactive "p")
  19590. (self-insert-command N))
  19591. (defun org-string-width (s)
  19592. "Compute width of string, ignoring invisible characters.
  19593. This ignores character with invisibility property `org-link', and also
  19594. characters with property `org-cwidth', because these will become invisible
  19595. upon the next fontification round."
  19596. (let (b l)
  19597. (when (or (eq t buffer-invisibility-spec)
  19598. (assq 'org-link buffer-invisibility-spec))
  19599. (while (setq b (text-property-any 0 (length s)
  19600. 'invisible 'org-link s))
  19601. (setq s (concat (substring s 0 b)
  19602. (substring s (or (next-single-property-change
  19603. b 'invisible s) (length s)))))))
  19604. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19605. (setq s (concat (substring s 0 b)
  19606. (substring s (or (next-single-property-change
  19607. b 'org-cwidth s) (length s))))))
  19608. (setq l (string-width s) b -1)
  19609. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19610. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19611. l))
  19612. (defun org-shorten-string (s maxlength)
  19613. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19614. If the string is shorter or has length MAXLENGTH, just return the
  19615. original string. If it is longer, the functions finds a space in the
  19616. string, breaks this string off at that locations and adds three dots
  19617. as ellipsis. Including the ellipsis, the string will not be longer
  19618. than MAXLENGTH. If finding a good breaking point in the string does
  19619. not work, the string is just chopped off in the middle of a word
  19620. if necessary."
  19621. (if (<= (length s) maxlength)
  19622. s
  19623. (let* ((n (max (- maxlength 4) 1))
  19624. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19625. (if (string-match re s)
  19626. (concat (match-string 1 s) "...")
  19627. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19628. (defun org-get-indentation (&optional line)
  19629. "Get the indentation of the current line, interpreting tabs.
  19630. When LINE is given, assume it represents a line and compute its indentation."
  19631. (if line
  19632. (if (string-match "^ *" (org-remove-tabs line))
  19633. (match-end 0))
  19634. (save-excursion
  19635. (beginning-of-line 1)
  19636. (skip-chars-forward " \t")
  19637. (current-column))))
  19638. (defun org-get-string-indentation (s)
  19639. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19640. (let ((n -1) (i 0) (w tab-width) c)
  19641. (catch 'exit
  19642. (while (< (setq n (1+ n)) (length s))
  19643. (setq c (aref s n))
  19644. (cond ((= c ?\ ) (setq i (1+ i)))
  19645. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19646. (t (throw 'exit t)))))
  19647. i))
  19648. (defun org-remove-tabs (s &optional width)
  19649. "Replace tabulators in S with spaces.
  19650. Assumes that s is a single line, starting in column 0."
  19651. (setq width (or width tab-width))
  19652. (while (string-match "\t" s)
  19653. (setq s (replace-match
  19654. (make-string
  19655. (- (* width (/ (+ (match-beginning 0) width) width))
  19656. (match-beginning 0)) ?\ )
  19657. t t s)))
  19658. s)
  19659. (defun org-fix-indentation (line ind)
  19660. "Fix indentation in LINE.
  19661. IND is a cons cell with target and minimum indentation.
  19662. If the current indentation in LINE is smaller than the minimum,
  19663. leave it alone. If it is larger than ind, set it to the target."
  19664. (let* ((l (org-remove-tabs line))
  19665. (i (org-get-indentation l))
  19666. (i1 (car ind)) (i2 (cdr ind)))
  19667. (if (>= i i2) (setq l (substring line i2)))
  19668. (if (> i1 0)
  19669. (concat (make-string i1 ?\ ) l)
  19670. l)))
  19671. (defun org-remove-indentation (code &optional n)
  19672. "Remove the maximum common indentation from the lines in CODE.
  19673. N may optionally be the number of spaces to remove."
  19674. (with-temp-buffer
  19675. (insert code)
  19676. (org-do-remove-indentation n)
  19677. (buffer-string)))
  19678. (defun org-do-remove-indentation (&optional n)
  19679. "Remove the maximum common indentation from the buffer."
  19680. (untabify (point-min) (point-max))
  19681. (let ((min 10000) re)
  19682. (if n
  19683. (setq min n)
  19684. (goto-char (point-min))
  19685. (while (re-search-forward "^ *[^ \n]" nil t)
  19686. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19687. (unless (or (= min 0) (= min 10000))
  19688. (setq re (format "^ \\{%d\\}" min))
  19689. (goto-char (point-min))
  19690. (while (re-search-forward re nil t)
  19691. (replace-match "")
  19692. (end-of-line 1))
  19693. min)))
  19694. (defun org-fill-template (template alist)
  19695. "Find each %key of ALIST in TEMPLATE and replace it."
  19696. (let ((case-fold-search nil)
  19697. entry key value)
  19698. (setq alist (sort (copy-sequence alist)
  19699. (lambda (a b) (< (length (car a)) (length (car b))))))
  19700. (while (setq entry (pop alist))
  19701. (setq template
  19702. (replace-regexp-in-string
  19703. (concat "%" (regexp-quote (car entry)))
  19704. (or (cdr entry) "") template t t)))
  19705. template))
  19706. (defun org-base-buffer (buffer)
  19707. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19708. (if (not buffer)
  19709. buffer
  19710. (or (buffer-base-buffer buffer)
  19711. buffer)))
  19712. (defun org-trim (s)
  19713. "Remove whitespace at beginning and end of string."
  19714. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19715. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19716. s)
  19717. (defun org-wrap (string &optional width lines)
  19718. "Wrap string to either a number of lines, or a width in characters.
  19719. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19720. that costs. If there is a word longer than WIDTH, the text is actually
  19721. wrapped to the length of that word.
  19722. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19723. many lines, whatever width that takes.
  19724. The return value is a list of lines, without newlines at the end."
  19725. (let* ((words (org-split-string string "[ \t\n]+"))
  19726. (maxword (apply 'max (mapcar 'org-string-width words)))
  19727. w ll)
  19728. (cond (width
  19729. (org-do-wrap words (max maxword width)))
  19730. (lines
  19731. (setq w maxword)
  19732. (setq ll (org-do-wrap words maxword))
  19733. (if (<= (length ll) lines)
  19734. ll
  19735. (setq ll words)
  19736. (while (> (length ll) lines)
  19737. (setq w (1+ w))
  19738. (setq ll (org-do-wrap words w)))
  19739. ll))
  19740. (t (error "Cannot wrap this")))))
  19741. (defun org-do-wrap (words width)
  19742. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19743. (let (lines line)
  19744. (while words
  19745. (setq line (pop words))
  19746. (while (and words (< (+ (length line) (length (car words))) width))
  19747. (setq line (concat line " " (pop words))))
  19748. (setq lines (push line lines)))
  19749. (nreverse lines)))
  19750. (defun org-split-string (string &optional separators)
  19751. "Splits STRING into substrings at SEPARATORS.
  19752. No empty strings are returned if there are matches at the beginning
  19753. and end of string."
  19754. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19755. (start 0)
  19756. notfirst
  19757. (list nil))
  19758. (while (and (string-match rexp string
  19759. (if (and notfirst
  19760. (= start (match-beginning 0))
  19761. (< start (length string)))
  19762. (1+ start) start))
  19763. (< (match-beginning 0) (length string)))
  19764. (setq notfirst t)
  19765. (or (eq (match-beginning 0) 0)
  19766. (and (eq (match-beginning 0) (match-end 0))
  19767. (eq (match-beginning 0) start))
  19768. (setq list
  19769. (cons (substring string start (match-beginning 0))
  19770. list)))
  19771. (setq start (match-end 0)))
  19772. (or (eq start (length string))
  19773. (setq list
  19774. (cons (substring string start)
  19775. list)))
  19776. (nreverse list)))
  19777. (defun org-quote-vert (s)
  19778. "Replace \"|\" with \"\\vert\"."
  19779. (while (string-match "|" s)
  19780. (setq s (replace-match "\\vert" t t s)))
  19781. s)
  19782. (defun org-uuidgen-p (s)
  19783. "Is S an ID created by UUIDGEN?"
  19784. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19785. (defun org-in-src-block-p (&optional inside)
  19786. "Whether point is in a code source block.
  19787. When INSIDE is non-nil, don't consider we are within a src block
  19788. when point is at #+BEGIN_SRC or #+END_SRC."
  19789. (let ((case-fold-search t) ov)
  19790. (or (and (setq ov (overlays-at (point)))
  19791. (memq 'org-block-background
  19792. (overlay-properties (car ov))))
  19793. (and (not inside)
  19794. (save-match-data
  19795. (save-excursion
  19796. (beginning-of-line)
  19797. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19798. (defun org-context ()
  19799. "Return a list of contexts of the current cursor position.
  19800. If several contexts apply, all are returned.
  19801. Each context entry is a list with a symbol naming the context, and
  19802. two positions indicating start and end of the context. Possible
  19803. contexts are:
  19804. :headline anywhere in a headline
  19805. :headline-stars on the leading stars in a headline
  19806. :todo-keyword on a TODO keyword (including DONE) in a headline
  19807. :tags on the TAGS in a headline
  19808. :priority on the priority cookie in a headline
  19809. :item on the first line of a plain list item
  19810. :item-bullet on the bullet/number of a plain list item
  19811. :checkbox on the checkbox in a plain list item
  19812. :table in an org-mode table
  19813. :table-special on a special filed in a table
  19814. :table-table in a table.el table
  19815. :clocktable in a clocktable
  19816. :src-block in a source block
  19817. :link on a hyperlink
  19818. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19819. :target on a <<target>>
  19820. :radio-target on a <<<radio-target>>>
  19821. :latex-fragment on a LaTeX fragment
  19822. :latex-preview on a LaTeX fragment with overlaid preview image
  19823. This function expects the position to be visible because it uses font-lock
  19824. faces as a help to recognize the following contexts: :table-special, :link,
  19825. and :keyword."
  19826. (let* ((f (get-text-property (point) 'face))
  19827. (faces (if (listp f) f (list f)))
  19828. (case-fold-search t)
  19829. (p (point)) clist o)
  19830. ;; First the large context
  19831. (cond
  19832. ((org-at-heading-p t)
  19833. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19834. (when (progn
  19835. (beginning-of-line 1)
  19836. (looking-at org-todo-line-tags-regexp))
  19837. (push (org-point-in-group p 1 :headline-stars) clist)
  19838. (push (org-point-in-group p 2 :todo-keyword) clist)
  19839. (push (org-point-in-group p 4 :tags) clist))
  19840. (goto-char p)
  19841. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19842. (if (looking-at "\\[#[A-Z0-9]\\]")
  19843. (push (org-point-in-group p 0 :priority) clist)))
  19844. ((org-at-item-p)
  19845. (push (org-point-in-group p 2 :item-bullet) clist)
  19846. (push (list :item (point-at-bol)
  19847. (save-excursion (org-end-of-item) (point)))
  19848. clist)
  19849. (and (org-at-item-checkbox-p)
  19850. (push (org-point-in-group p 0 :checkbox) clist)))
  19851. ((org-at-table-p)
  19852. (push (list :table (org-table-begin) (org-table-end)) clist)
  19853. (if (memq 'org-formula faces)
  19854. (push (list :table-special
  19855. (previous-single-property-change p 'face)
  19856. (next-single-property-change p 'face)) clist)))
  19857. ((org-at-table-p 'any)
  19858. (push (list :table-table) clist)))
  19859. (goto-char p)
  19860. (let ((case-fold-search t))
  19861. ;; New the "medium" contexts: clocktables, source blocks
  19862. (cond ((org-in-clocktable-p)
  19863. (push (list :clocktable
  19864. (and (or (looking-at "#\\+BEGIN: clocktable")
  19865. (search-backward "#+BEGIN: clocktable" nil t))
  19866. (match-beginning 0))
  19867. (and (re-search-forward "#\\+END:?" nil t)
  19868. (match-end 0))) clist))
  19869. ((org-in-src-block-p)
  19870. (push (list :src-block
  19871. (and (or (looking-at "#\\+BEGIN_SRC")
  19872. (search-backward "#+BEGIN_SRC" nil t))
  19873. (match-beginning 0))
  19874. (and (search-forward "#+END_SRC" nil t)
  19875. (match-beginning 0))) clist))))
  19876. (goto-char p)
  19877. ;; Now the small context
  19878. (cond
  19879. ((org-at-timestamp-p)
  19880. (push (org-point-in-group p 0 :timestamp) clist))
  19881. ((memq 'org-link faces)
  19882. (push (list :link
  19883. (previous-single-property-change p 'face)
  19884. (next-single-property-change p 'face)) clist))
  19885. ((memq 'org-special-keyword faces)
  19886. (push (list :keyword
  19887. (previous-single-property-change p 'face)
  19888. (next-single-property-change p 'face)) clist))
  19889. ((org-at-target-p)
  19890. (push (org-point-in-group p 0 :target) clist)
  19891. (goto-char (1- (match-beginning 0)))
  19892. (if (looking-at org-radio-target-regexp)
  19893. (push (org-point-in-group p 0 :radio-target) clist))
  19894. (goto-char p))
  19895. ((setq o (car (delq nil
  19896. (mapcar
  19897. (lambda (x)
  19898. (if (memq x org-latex-fragment-image-overlays) x))
  19899. (overlays-at (point))))))
  19900. (push (list :latex-fragment
  19901. (overlay-start o) (overlay-end o)) clist)
  19902. (push (list :latex-preview
  19903. (overlay-start o) (overlay-end o)) clist))
  19904. ((org-inside-LaTeX-fragment-p)
  19905. ;; FIXME: positions wrong.
  19906. (push (list :latex-fragment (point) (point)) clist)))
  19907. (setq clist (nreverse (delq nil clist)))
  19908. clist))
  19909. ;; FIXME: Compare with at-regexp-p Do we need both?
  19910. (defun org-in-regexp (re &optional nlines visually)
  19911. "Check if point is inside a match of regexp.
  19912. Normally only the current line is checked, but you can include NLINES extra
  19913. lines both before and after point into the search.
  19914. If VISUALLY is set, require that the cursor is not after the match but
  19915. really on, so that the block visually is on the match."
  19916. (catch 'exit
  19917. (let ((pos (point))
  19918. (eol (point-at-eol (+ 1 (or nlines 0))))
  19919. (inc (if visually 1 0)))
  19920. (save-excursion
  19921. (beginning-of-line (- 1 (or nlines 0)))
  19922. (while (re-search-forward re eol t)
  19923. (if (and (<= (match-beginning 0) pos)
  19924. (>= (+ inc (match-end 0)) pos))
  19925. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19926. (defun org-at-regexp-p (regexp)
  19927. "Is point inside a match of REGEXP in the current line?"
  19928. (catch 'exit
  19929. (save-excursion
  19930. (let ((pos (point)) (end (point-at-eol)))
  19931. (beginning-of-line 1)
  19932. (while (re-search-forward regexp end t)
  19933. (if (and (<= (match-beginning 0) pos)
  19934. (>= (match-end 0) pos))
  19935. (throw 'exit t)))
  19936. nil))))
  19937. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19938. "Non-nil when point is between matches of START-RE and END-RE.
  19939. Also return a non-nil value when point is on one of the matches.
  19940. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19941. buffer positions. Default values are the positions of headlines
  19942. surrounding the point.
  19943. The functions returns a cons cell whose car (resp. cdr) is the
  19944. position before START-RE (resp. after END-RE)."
  19945. (save-match-data
  19946. (let ((pos (point))
  19947. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19948. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19949. beg end)
  19950. (save-excursion
  19951. ;; Point is on a block when on START-RE or if START-RE can be
  19952. ;; found before it...
  19953. (and (or (org-at-regexp-p start-re)
  19954. (re-search-backward start-re limit-up t))
  19955. (setq beg (match-beginning 0))
  19956. ;; ... and END-RE after it...
  19957. (goto-char (match-end 0))
  19958. (re-search-forward end-re limit-down t)
  19959. (> (setq end (match-end 0)) pos)
  19960. ;; ... without another START-RE in-between.
  19961. (goto-char (match-beginning 0))
  19962. (not (re-search-backward start-re (1+ beg) t))
  19963. ;; Return value.
  19964. (cons beg end))))))
  19965. (defun org-in-block-p (names)
  19966. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19967. NAMES is a list of strings containing names of blocks.
  19968. Return first block name matched, or nil. Beware that in case of
  19969. nested blocks, the returned name may not belong to the closest
  19970. block from point."
  19971. (save-match-data
  19972. (catch 'exit
  19973. (let ((case-fold-search t)
  19974. (lim-up (save-excursion (outline-previous-heading)))
  19975. (lim-down (save-excursion (outline-next-heading))))
  19976. (mapc (lambda (name)
  19977. (let ((n (regexp-quote name)))
  19978. (when (org-between-regexps-p
  19979. (concat "^[ \t]*#\\+begin_" n)
  19980. (concat "^[ \t]*#\\+end_" n)
  19981. lim-up lim-down)
  19982. (throw 'exit n))))
  19983. names))
  19984. nil)))
  19985. (defun org-in-drawer-p ()
  19986. "Non-nil if point is within a drawer.
  19987. If point is within a drawer, return it, as parsed data."
  19988. (let ((element (save-match-data (org-element-at-point))))
  19989. (while (and element (not (memq (org-element-type element)
  19990. '(drawer property-drawer))))
  19991. (setq element (org-element-property :parent element)))
  19992. element))
  19993. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19994. "Call `multi-occur' with buffers for all agenda files."
  19995. (interactive "sOrg-files matching: \np")
  19996. (let* ((files (org-agenda-files))
  19997. (tnames (mapcar 'file-truename files))
  19998. (extra org-agenda-text-search-extra-files)
  19999. f)
  20000. (when (eq (car extra) 'agenda-archives)
  20001. (setq extra (cdr extra))
  20002. (setq files (org-add-archive-files files)))
  20003. (while (setq f (pop extra))
  20004. (unless (member (file-truename f) tnames)
  20005. (add-to-list 'files f 'append)
  20006. (add-to-list 'tnames (file-truename f) 'append)))
  20007. (multi-occur
  20008. (mapcar (lambda (x)
  20009. (with-current-buffer
  20010. (or (get-file-buffer x) (find-file-noselect x))
  20011. (widen)
  20012. (current-buffer)))
  20013. files)
  20014. regexp)))
  20015. (if (boundp 'occur-mode-find-occurrence-hook)
  20016. ;; Emacs 23
  20017. (add-hook 'occur-mode-find-occurrence-hook
  20018. (lambda ()
  20019. (when (derived-mode-p 'org-mode)
  20020. (org-reveal))))
  20021. ;; Emacs 22
  20022. (defadvice occur-mode-goto-occurrence
  20023. (after org-occur-reveal activate)
  20024. (and (derived-mode-p 'org-mode) (org-reveal)))
  20025. (defadvice occur-mode-goto-occurrence-other-window
  20026. (after org-occur-reveal activate)
  20027. (and (derived-mode-p 'org-mode) (org-reveal)))
  20028. (defadvice occur-mode-display-occurrence
  20029. (after org-occur-reveal activate)
  20030. (when (derived-mode-p 'org-mode)
  20031. (let ((pos (occur-mode-find-occurrence)))
  20032. (with-current-buffer (marker-buffer pos)
  20033. (save-excursion
  20034. (goto-char pos)
  20035. (org-reveal)))))))
  20036. (defun org-occur-link-in-agenda-files ()
  20037. "Create a link and search for it in the agendas.
  20038. The link is not stored in `org-stored-links', it is just created
  20039. for the search purpose."
  20040. (interactive)
  20041. (let ((link (condition-case nil
  20042. (org-store-link nil)
  20043. (error "Unable to create a link to here"))))
  20044. (org-occur-in-agenda-files (regexp-quote link))))
  20045. (defun org-reverse-string (string)
  20046. "Return the reverse of STRING."
  20047. (apply 'string (reverse (string-to-list string))))
  20048. (defsubst org-uniquify (list)
  20049. "Non-destructively remove duplicate elements from LIST."
  20050. (let ((res (copy-sequence list))) (delete-dups res)))
  20051. (defun org-uniquify-alist (alist)
  20052. "Merge elements of ALIST with the same key.
  20053. For example, in this alist:
  20054. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20055. => '((a 1 3) (b 2))
  20056. merge (a 1) and (a 3) into (a 1 3).
  20057. The function returns the new ALIST."
  20058. (let (rtn)
  20059. (mapc
  20060. (lambda (e)
  20061. (let (n)
  20062. (if (not (assoc (car e) rtn))
  20063. (push e rtn)
  20064. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20065. (setq rtn (assq-delete-all (car e) rtn))
  20066. (push n rtn))))
  20067. alist)
  20068. rtn))
  20069. (defun org-delete-all (elts list)
  20070. "Remove all elements in ELTS from LIST."
  20071. (while elts
  20072. (setq list (delete (pop elts) list)))
  20073. list)
  20074. (defun org-count (cl-item cl-seq)
  20075. "Count the number of occurrences of ITEM in SEQ.
  20076. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20077. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20078. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20079. (while (< cl-start cl-end)
  20080. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20081. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20082. (setq cl-start (1+ cl-start)))
  20083. cl-count))
  20084. (defun org-remove-if (predicate seq)
  20085. "Remove everything from SEQ that fulfills PREDICATE."
  20086. (let (res e)
  20087. (while seq
  20088. (setq e (pop seq))
  20089. (if (not (funcall predicate e)) (push e res)))
  20090. (nreverse res)))
  20091. (defun org-remove-if-not (predicate seq)
  20092. "Remove everything from SEQ that does not fulfill PREDICATE."
  20093. (let (res e)
  20094. (while seq
  20095. (setq e (pop seq))
  20096. (if (funcall predicate e) (push e res)))
  20097. (nreverse res)))
  20098. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20099. "Reduce two-argument FUNCTION across SEQ.
  20100. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20101. \":initial-value\" removed."
  20102. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20103. (cadr (memq :initial-value cl-keys)))
  20104. (cl-seq (pop cl-seq))
  20105. (t (funcall cl-func)))))
  20106. (while cl-seq
  20107. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20108. cl-accum))
  20109. (defun org-every (pred seq)
  20110. "Return true if PREDICATE is true of every element of SEQ.
  20111. Adapted from `every' in cl.el."
  20112. (catch 'org-every
  20113. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20114. t))
  20115. (defun org-some (pred seq)
  20116. "Return true if PREDICATE is true of any element of SEQ.
  20117. Adapted from `some' in cl.el."
  20118. (catch 'org-some
  20119. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20120. nil))
  20121. (defun org-back-over-empty-lines ()
  20122. "Move backwards over whitespace, to the beginning of the first empty line.
  20123. Returns the number of empty lines passed."
  20124. (let ((pos (point)))
  20125. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20126. (skip-chars-backward " \t\n\r")
  20127. (unless (eobp)
  20128. (forward-line -1)))
  20129. (beginning-of-line 2)
  20130. (goto-char (min (point) pos))
  20131. (count-lines (point) pos)))
  20132. (defun org-skip-whitespace ()
  20133. (skip-chars-forward " \t\n\r"))
  20134. (defun org-point-in-group (point group &optional context)
  20135. "Check if POINT is in match-group GROUP.
  20136. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20137. match. If the match group does not exist or point is not inside it,
  20138. return nil."
  20139. (and (match-beginning group)
  20140. (>= point (match-beginning group))
  20141. (<= point (match-end group))
  20142. (if context
  20143. (list context (match-beginning group) (match-end group))
  20144. t)))
  20145. (defun org-switch-to-buffer-other-window (&rest args)
  20146. "Switch to buffer in a second window on the current frame.
  20147. In particular, do not allow pop-up frames.
  20148. Returns the newly created buffer."
  20149. (org-no-popups
  20150. (apply 'switch-to-buffer-other-window args)))
  20151. (defun org-combine-plists (&rest plists)
  20152. "Create a single property list from all plists in PLISTS.
  20153. The process starts by copying the first list, and then setting properties
  20154. from the other lists. Settings in the last list are the most significant
  20155. ones and overrule settings in the other lists."
  20156. (let ((rtn (copy-sequence (pop plists)))
  20157. p v ls)
  20158. (while plists
  20159. (setq ls (pop plists))
  20160. (while ls
  20161. (setq p (pop ls) v (pop ls))
  20162. (setq rtn (plist-put rtn p v))))
  20163. rtn))
  20164. (defun org-replace-escapes (string table)
  20165. "Replace %-escapes in STRING with values in TABLE.
  20166. TABLE is an association list with keys like \"%a\" and string values.
  20167. The sequences in STRING may contain normal field width and padding information,
  20168. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20169. so values can contain further %-escapes if they are define later in TABLE."
  20170. (let ((tbl (copy-alist table))
  20171. (case-fold-search nil)
  20172. (pchg 0)
  20173. e re rpl)
  20174. (while (setq e (pop tbl))
  20175. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20176. (when (and (cdr e) (string-match re (cdr e)))
  20177. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20178. (safe "SREF"))
  20179. (add-text-properties 0 3 (list 'sref sref) safe)
  20180. (setcdr e (replace-match safe t t (cdr e)))))
  20181. (while (string-match re string)
  20182. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20183. (cdr e)))
  20184. (setq string (replace-match rpl t t string))))
  20185. (while (setq pchg (next-property-change pchg string))
  20186. (let ((sref (get-text-property pchg 'sref string)))
  20187. (when (and sref (string-match "SREF" string pchg))
  20188. (setq string (replace-match sref t t string)))))
  20189. string))
  20190. (defun org-sublist (list start end)
  20191. "Return a section of LIST, from START to END.
  20192. Counting starts at 1."
  20193. (let (rtn (c start))
  20194. (setq list (nthcdr (1- start) list))
  20195. (while (and list (<= c end))
  20196. (push (pop list) rtn)
  20197. (setq c (1+ c)))
  20198. (nreverse rtn)))
  20199. (defun org-find-base-buffer-visiting (file)
  20200. "Like `find-buffer-visiting' but always return the base buffer and
  20201. not an indirect buffer."
  20202. (let ((buf (or (get-file-buffer file)
  20203. (find-buffer-visiting file))))
  20204. (if buf
  20205. (or (buffer-base-buffer buf) buf)
  20206. nil)))
  20207. (defun org-image-file-name-regexp (&optional extensions)
  20208. "Return regexp matching the file names of images.
  20209. If EXTENSIONS is given, only match these."
  20210. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20211. (image-file-name-regexp)
  20212. (let ((image-file-name-extensions
  20213. (or extensions
  20214. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20215. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20216. (concat "\\."
  20217. (regexp-opt (nconc (mapcar 'upcase
  20218. image-file-name-extensions)
  20219. image-file-name-extensions)
  20220. t)
  20221. "\\'"))))
  20222. (defun org-file-image-p (file &optional extensions)
  20223. "Return non-nil if FILE is an image."
  20224. (save-match-data
  20225. (string-match (org-image-file-name-regexp extensions) file)))
  20226. (defun org-get-cursor-date (&optional with-time)
  20227. "Return the date at cursor in as a time.
  20228. This works in the calendar and in the agenda, anywhere else it just
  20229. returns the current time.
  20230. If WITH-TIME is non-nil, returns the time of the event at point (in
  20231. the agenda) or the current time of the day."
  20232. (let (date day defd tp tm hod mod)
  20233. (when with-time
  20234. (setq tp (get-text-property (point) 'time))
  20235. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20236. (setq hod (string-to-number (match-string 1 tp))
  20237. mod (string-to-number (match-string 2 tp))))
  20238. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20239. mod (nth 1 (decode-time (current-time))))))
  20240. (cond
  20241. ((eq major-mode 'calendar-mode)
  20242. (setq date (calendar-cursor-to-date)
  20243. defd (encode-time 0 (or mod 0) (or hod 0)
  20244. (nth 1 date) (nth 0 date) (nth 2 date))))
  20245. ((eq major-mode 'org-agenda-mode)
  20246. (setq day (get-text-property (point) 'day))
  20247. (if day
  20248. (setq date (calendar-gregorian-from-absolute day)
  20249. defd (encode-time 0 (or mod 0) (or hod 0)
  20250. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20251. (or defd (current-time))))
  20252. (defun org-mark-subtree (&optional up)
  20253. "Mark the current subtree.
  20254. This puts point at the start of the current subtree, and mark at
  20255. the end. If a numeric prefix UP is given, move up into the
  20256. hierarchy of headlines by UP levels before marking the subtree."
  20257. (interactive "P")
  20258. (org-with-limited-levels
  20259. (cond ((org-at-heading-p) (beginning-of-line))
  20260. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20261. (t (outline-previous-visible-heading 1))))
  20262. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20263. (if (org-called-interactively-p 'any)
  20264. (call-interactively 'org-mark-element)
  20265. (org-mark-element)))
  20266. ;;; Indentation
  20267. (defun org-indent-line ()
  20268. "Indent line depending on context."
  20269. (interactive)
  20270. (let* ((pos (point))
  20271. (itemp (org-at-item-p))
  20272. (case-fold-search t)
  20273. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20274. (inline-task-p (and (featurep 'org-inlinetask)
  20275. (org-inlinetask-in-task-p)))
  20276. (inline-re (and inline-task-p
  20277. (org-inlinetask-outline-regexp)))
  20278. column)
  20279. (if (and orgstruct-is-++ (eq pos (point)))
  20280. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20281. (indent-according-to-mode))
  20282. (beginning-of-line 1)
  20283. (cond
  20284. ;; Headings
  20285. ((looking-at org-outline-regexp) (setq column 0))
  20286. ;; Footnote definition
  20287. ((looking-at org-footnote-definition-re) (setq column 0))
  20288. ;; Literal examples
  20289. ((looking-at "[ \t]*:\\( \\|$\\)")
  20290. (setq column (org-get-indentation))) ; do nothing
  20291. ;; Lists
  20292. ((ignore-errors (goto-char (org-in-item-p)))
  20293. (setq column (if itemp
  20294. (org-get-indentation)
  20295. (org-list-item-body-column (point))))
  20296. (goto-char pos))
  20297. ;; Drawers
  20298. ((and (looking-at "[ \t]*:END:")
  20299. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20300. (save-excursion
  20301. (goto-char (1- (match-beginning 1)))
  20302. (setq column (current-column))))
  20303. ;; Special blocks
  20304. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20305. (save-excursion
  20306. (re-search-backward
  20307. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20308. (setq column (org-get-indentation (match-string 0))))
  20309. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20310. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20311. (save-excursion
  20312. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20313. (setq column
  20314. (cond ((equal (downcase (match-string 1)) "src")
  20315. ;; src blocks: let `org-edit-src-exit' handle them
  20316. (org-get-indentation))
  20317. ((equal (downcase (match-string 1)) "example")
  20318. (max (org-get-indentation)
  20319. (org-get-indentation (match-string 0))))
  20320. (t
  20321. (org-get-indentation (match-string 0))))))
  20322. ;; This line has nothing special, look at the previous relevant
  20323. ;; line to compute indentation
  20324. (t
  20325. (beginning-of-line 0)
  20326. (while (and (not (bobp))
  20327. (not (looking-at org-table-line-regexp))
  20328. (not (looking-at org-drawer-regexp))
  20329. ;; When point started in an inline task, do not move
  20330. ;; above task starting line.
  20331. (not (and inline-task-p (looking-at inline-re)))
  20332. ;; Skip drawers, blocks, empty lines, verbatim,
  20333. ;; comments, tables, footnotes definitions, lists,
  20334. ;; inline tasks.
  20335. (or (and (looking-at "[ \t]*:END:")
  20336. (re-search-backward org-drawer-regexp nil t))
  20337. (and (looking-at "[ \t]*#\\+end_")
  20338. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20339. (looking-at "[ \t]*[\n:#|]")
  20340. (looking-at org-footnote-definition-re)
  20341. (and (not inline-task-p)
  20342. (featurep 'org-inlinetask)
  20343. (org-inlinetask-in-task-p)
  20344. (or (org-inlinetask-goto-beginning) t))))
  20345. (beginning-of-line 0))
  20346. (cond
  20347. ;; There was a list item above.
  20348. ((ignore-errors (goto-char (org-in-item-p)))
  20349. (goto-char (org-list-get-top-point (org-list-struct)))
  20350. (setq column (org-get-indentation)))
  20351. ;; There was an heading above.
  20352. ((looking-at "\\*+[ \t]+")
  20353. (if (not org-adapt-indentation)
  20354. (setq column 0)
  20355. (goto-char (match-end 0))
  20356. (setq column (current-column))))
  20357. ;; A drawer had started and is unfinished
  20358. ((looking-at org-drawer-regexp)
  20359. (goto-char (1- (match-beginning 1)))
  20360. (setq column (current-column)))
  20361. ;; Else, nothing noticeable found: get indentation and go on.
  20362. (t (setq column (org-get-indentation))))))
  20363. ;; Now apply indentation and move cursor accordingly
  20364. (goto-char pos)
  20365. (if (<= (current-column) (current-indentation))
  20366. (org-indent-line-to column)
  20367. (save-excursion (org-indent-line-to column)))
  20368. ;; Special polishing for properties, see `org-property-format'
  20369. (setq column (current-column))
  20370. (beginning-of-line 1)
  20371. (if (looking-at org-property-re)
  20372. (replace-match (concat (match-string 4)
  20373. (format org-property-format
  20374. (match-string 1) (match-string 3)))
  20375. t t))
  20376. (org-move-to-column column))))
  20377. (defun org-indent-drawer ()
  20378. "Indent the drawer at point."
  20379. (interactive)
  20380. (let ((p (point))
  20381. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20382. (match-end 0)))
  20383. (folded
  20384. (save-excursion
  20385. (end-of-line)
  20386. (when (overlays-at (point))
  20387. (member 'invisible (overlay-properties
  20388. (car (overlays-at (point)))))))))
  20389. (when folded (org-cycle))
  20390. (indent-for-tab-command)
  20391. (while (and (move-beginning-of-line 2) (< (point) e))
  20392. (indent-for-tab-command))
  20393. (goto-char p)
  20394. (when folded (org-cycle)))
  20395. (message "Drawer at point indented"))
  20396. (defun org-indent-block ()
  20397. "Indent the block at point."
  20398. (interactive)
  20399. (let ((p (point))
  20400. (case-fold-search t)
  20401. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20402. (match-end 0)))
  20403. (folded
  20404. (save-excursion
  20405. (end-of-line)
  20406. (when (overlays-at (point))
  20407. (member 'invisible (overlay-properties
  20408. (car (overlays-at (point)))))))))
  20409. (when folded (org-cycle))
  20410. (indent-for-tab-command)
  20411. (while (and (move-beginning-of-line 2) (< (point) e))
  20412. (indent-for-tab-command))
  20413. (goto-char p)
  20414. (when folded (org-cycle)))
  20415. (message "Block at point indented"))
  20416. (defun org-indent-region (start end)
  20417. "Indent region."
  20418. (interactive "r")
  20419. (save-excursion
  20420. (let ((line-end (org-current-line end)))
  20421. (goto-char start)
  20422. (while (< (org-current-line) line-end)
  20423. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20424. (t (call-interactively 'org-indent-line)))
  20425. (move-beginning-of-line 2)))))
  20426. ;;; Filling
  20427. ;; We use our own fill-paragraph and auto-fill functions.
  20428. ;; `org-fill-paragraph' relies on adaptive filling and context
  20429. ;; checking. Appropriate `fill-prefix' is computed with
  20430. ;; `org-adaptive-fill-function'.
  20431. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20432. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20433. ;; `org-adaptive-fill-function' value. Internally,
  20434. ;; `org-comment-line-break-function' breaks the line.
  20435. ;; `org-setup-filling' installs filling and auto-filling related
  20436. ;; variables during `org-mode' initialization.
  20437. (defvar org-element-paragraph-separate) ; org-element.el
  20438. (defun org-setup-filling ()
  20439. (require 'org-element)
  20440. ;; Prevent auto-fill from inserting unwanted new items.
  20441. (when (boundp 'fill-nobreak-predicate)
  20442. (org-set-local
  20443. 'fill-nobreak-predicate
  20444. (org-uniquify
  20445. (append fill-nobreak-predicate
  20446. '(org-fill-line-break-nobreak-p
  20447. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20448. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20449. (org-set-local 'paragraph-start paragraph-ending)
  20450. (org-set-local 'paragraph-separate paragraph-ending))
  20451. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20452. (org-set-local 'auto-fill-inhibit-regexp nil)
  20453. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20454. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20455. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20456. (defun org-fill-line-break-nobreak-p ()
  20457. "Non-nil when a new line at point would create an Org line break."
  20458. (save-excursion
  20459. (skip-chars-backward "[ \t]")
  20460. (skip-chars-backward "\\\\")
  20461. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20462. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20463. "Non-nil when a new line at point would split a timestamp."
  20464. (and (org-at-timestamp-p t)
  20465. (not (looking-at org-ts-regexp-both))))
  20466. (declare-function message-in-body-p "message" ())
  20467. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20468. (defun org-adaptive-fill-function ()
  20469. "Compute a fill prefix for the current line.
  20470. Return fill prefix, as a string, or nil if current line isn't
  20471. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20472. matches in paragraphs or comments, use it."
  20473. (catch 'exit
  20474. (when (derived-mode-p 'message-mode)
  20475. (save-excursion
  20476. (beginning-of-line)
  20477. (cond ((or (not (message-in-body-p))
  20478. (looking-at orgtbl-line-start-regexp))
  20479. (throw 'exit nil))
  20480. ((looking-at message-cite-prefix-regexp)
  20481. (throw 'exit (match-string-no-properties 0)))
  20482. ((looking-at org-outline-regexp)
  20483. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20484. (org-with-wide-buffer
  20485. (let* ((p (line-beginning-position))
  20486. (element (save-excursion
  20487. (beginning-of-line)
  20488. (or (ignore-errors (org-element-at-point))
  20489. (user-error "An element cannot be parsed line %d"
  20490. (line-number-at-pos (point))))))
  20491. (type (org-element-type element))
  20492. (post-affiliated (org-element-property :post-affiliated element)))
  20493. (unless (and post-affiliated (< p post-affiliated))
  20494. (case type
  20495. (comment
  20496. (save-excursion
  20497. (beginning-of-line)
  20498. (looking-at "[ \t]*")
  20499. (concat (match-string 0) "# ")))
  20500. (footnote-definition "")
  20501. ((item plain-list)
  20502. (make-string (org-list-item-body-column
  20503. (or post-affiliated
  20504. (org-element-property :begin element)))
  20505. ? ))
  20506. (paragraph
  20507. ;; Fill prefix is usually the same as the current line,
  20508. ;; unless the paragraph is at the beginning of an item.
  20509. (let ((parent (org-element-property :parent element)))
  20510. (save-excursion
  20511. (beginning-of-line)
  20512. (cond ((eq (org-element-type parent) 'item)
  20513. (make-string (org-list-item-body-column
  20514. (org-element-property :begin parent))
  20515. ? ))
  20516. ((and adaptive-fill-regexp
  20517. ;; Locally disable
  20518. ;; `adaptive-fill-function' to let
  20519. ;; `fill-context-prefix' handle
  20520. ;; `adaptive-fill-regexp' variable.
  20521. (let (adaptive-fill-function)
  20522. (fill-context-prefix
  20523. post-affiliated
  20524. (org-element-property :end element)))))
  20525. ((looking-at "[ \t]+") (match-string 0))
  20526. (t "")))))
  20527. (comment-block
  20528. ;; Only fill contents if P is within block boundaries.
  20529. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20530. (forward-line)
  20531. (point)))
  20532. (cend (save-excursion
  20533. (goto-char (org-element-property :end element))
  20534. (skip-chars-backward " \r\t\n")
  20535. (line-beginning-position))))
  20536. (when (and (>= p cbeg) (< p cend))
  20537. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20538. (match-string 0)
  20539. ""))))))))))
  20540. (declare-function message-goto-body "message" ())
  20541. (defvar message-cite-prefix-regexp) ; From message.el
  20542. (defun org-fill-paragraph (&optional justify)
  20543. "Fill element at point, when applicable.
  20544. This function only applies to comment blocks, comments, example
  20545. blocks and paragraphs. Also, as a special case, re-align table
  20546. when point is at one.
  20547. If JUSTIFY is non-nil (interactively, with prefix argument),
  20548. justify as well. If `sentence-end-double-space' is non-nil, then
  20549. period followed by one space does not end a sentence, so don't
  20550. break a line there. The variable `fill-column' controls the
  20551. width for filling.
  20552. For convenience, when point is at a plain list, an item or
  20553. a footnote definition, try to fill the first paragraph within."
  20554. (interactive)
  20555. (if (and (derived-mode-p 'message-mode)
  20556. (or (not (message-in-body-p))
  20557. (save-excursion (move-beginning-of-line 1)
  20558. (looking-at message-cite-prefix-regexp))))
  20559. ;; First ensure filling is correct in message-mode.
  20560. (let ((fill-paragraph-function
  20561. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20562. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20563. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20564. (paragraph-separate
  20565. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20566. (fill-paragraph nil))
  20567. (with-syntax-table org-mode-transpose-word-syntax-table
  20568. ;; Move to end of line in order to get the first paragraph
  20569. ;; within a plain list or a footnote definition.
  20570. (let ((element (save-excursion
  20571. (end-of-line)
  20572. (or (ignore-errors (org-element-at-point))
  20573. (user-error "An element cannot be parsed line %d"
  20574. (line-number-at-pos (point)))))))
  20575. ;; First check if point is in a blank line at the beginning of
  20576. ;; the buffer. In that case, ignore filling.
  20577. (case (org-element-type element)
  20578. ;; Use major mode filling function is src blocks.
  20579. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20580. ;; Align Org tables, leave table.el tables as-is.
  20581. (table-row (org-table-align) t)
  20582. (table
  20583. (when (eq (org-element-property :type element) 'org)
  20584. (save-excursion
  20585. (goto-char (org-element-property :post-affiliated element))
  20586. (org-table-align)))
  20587. t)
  20588. (paragraph
  20589. ;; Paragraphs may contain `line-break' type objects.
  20590. (let ((beg (max (point-min)
  20591. (org-element-property :contents-begin element)))
  20592. (end (min (point-max)
  20593. (org-element-property :contents-end element))))
  20594. ;; Do nothing if point is at an affiliated keyword.
  20595. (if (< (line-end-position) beg) t
  20596. (when (derived-mode-p 'message-mode)
  20597. ;; In `message-mode', do not fill following citation
  20598. ;; in current paragraph nor text before message body.
  20599. (let ((body-start (save-excursion (message-goto-body))))
  20600. (when body-start (setq beg (max body-start beg))))
  20601. (when (save-excursion
  20602. (re-search-forward
  20603. (concat "^" message-cite-prefix-regexp) end t))
  20604. (setq end (match-beginning 0))))
  20605. ;; Fill paragraph, taking line breaks into account.
  20606. ;; For that, slice the paragraph using line breaks as
  20607. ;; separators, and fill the parts in reverse order to
  20608. ;; avoid messing with markers.
  20609. (save-excursion
  20610. (goto-char end)
  20611. (mapc
  20612. (lambda (pos)
  20613. (fill-region-as-paragraph pos (point) justify)
  20614. (goto-char pos))
  20615. ;; Find the list of ending positions for line breaks
  20616. ;; in the current paragraph. Add paragraph
  20617. ;; beginning to include first slice.
  20618. (nreverse
  20619. (cons beg
  20620. (org-element-map
  20621. (org-element--parse-objects
  20622. beg end nil (org-element-restriction 'paragraph))
  20623. 'line-break
  20624. (lambda (lb) (org-element-property :end lb)))))))
  20625. t)))
  20626. ;; Contents of `comment-block' type elements should be
  20627. ;; filled as plain text, but only if point is within block
  20628. ;; markers.
  20629. (comment-block
  20630. (let* ((case-fold-search t)
  20631. (beg (save-excursion
  20632. (goto-char (org-element-property :begin element))
  20633. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20634. (forward-line)
  20635. (point)))
  20636. (end (save-excursion
  20637. (goto-char (org-element-property :end element))
  20638. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20639. (line-beginning-position))))
  20640. (if (or (< (point) beg) (> (point) end)) t
  20641. (fill-region-as-paragraph
  20642. (save-excursion (end-of-line)
  20643. (re-search-backward "^[ \t]*$" beg 'move)
  20644. (line-beginning-position))
  20645. (save-excursion (beginning-of-line)
  20646. (re-search-forward "^[ \t]*$" end 'move)
  20647. (line-beginning-position))
  20648. justify))))
  20649. ;; Fill comments.
  20650. (comment
  20651. (let ((begin (org-element-property :post-affiliated element))
  20652. (end (org-element-property :end element)))
  20653. (when (and (>= (point) begin) (<= (point) end))
  20654. (let ((begin (save-excursion
  20655. (end-of-line)
  20656. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20657. (progn (forward-line) (point))
  20658. begin)))
  20659. (end (save-excursion
  20660. (end-of-line)
  20661. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20662. (1- (line-beginning-position))
  20663. (skip-chars-backward " \r\t\n")
  20664. (line-end-position)))))
  20665. ;; Do not fill comments when at a blank line.
  20666. (when (> end begin)
  20667. (let ((fill-prefix
  20668. (save-excursion
  20669. (beginning-of-line)
  20670. (looking-at "[ \t]*#")
  20671. (let ((comment-prefix (match-string 0)))
  20672. (goto-char (match-end 0))
  20673. (if (looking-at adaptive-fill-regexp)
  20674. (concat comment-prefix (match-string 0))
  20675. (concat comment-prefix " "))))))
  20676. (save-excursion
  20677. (fill-region-as-paragraph begin end justify))))))
  20678. t))
  20679. ;; Ignore every other element.
  20680. (otherwise t))))))
  20681. (defun org-auto-fill-function ()
  20682. "Auto-fill function."
  20683. ;; Check if auto-filling is meaningful.
  20684. (let ((fc (current-fill-column)))
  20685. (when (and fc (> (current-column) fc))
  20686. (let* ((fill-prefix (org-adaptive-fill-function))
  20687. ;; Enforce empty fill prefix, if required. Otherwise, it
  20688. ;; will be computed again.
  20689. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20690. (when fill-prefix (do-auto-fill))))))
  20691. (defun org-comment-line-break-function (&optional soft)
  20692. "Break line at point and indent, continuing comment if within one.
  20693. The inserted newline is marked hard if variable
  20694. `use-hard-newlines' is true, unless optional argument SOFT is
  20695. non-nil."
  20696. (if soft (insert-and-inherit ?\n) (newline 1))
  20697. (save-excursion (forward-char -1) (delete-horizontal-space))
  20698. (delete-horizontal-space)
  20699. (indent-to-left-margin)
  20700. (insert-before-markers-and-inherit fill-prefix))
  20701. ;;; Comments
  20702. ;; Org comments syntax is quite complex. It requires the entire line
  20703. ;; to be just a comment. Also, even with the right syntax at the
  20704. ;; beginning of line, some some elements (i.e. verse-block or
  20705. ;; example-block) don't accept comments. Usual Emacs comment commands
  20706. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20707. ;; Org still relies on `comment-dwim', but cannot trust
  20708. ;; `comment-only-p'. So, `comment-region-function' and
  20709. ;; `uncomment-region-function' both point
  20710. ;; to`org-comment-or-uncomment-region'. Eventually,
  20711. ;; `org-insert-comment' takes care of insertion of comments at the
  20712. ;; beginning of line.
  20713. ;; `org-setup-comments-handling' install comments related variables
  20714. ;; during `org-mode' initialization.
  20715. (defun org-setup-comments-handling ()
  20716. (interactive)
  20717. (org-set-local 'comment-use-syntax nil)
  20718. (org-set-local 'comment-start "# ")
  20719. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20720. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20721. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20722. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20723. (defun org-insert-comment ()
  20724. "Insert an empty comment above current line.
  20725. If the line is empty, insert comment at its beginning. When
  20726. point is within a source block, comment according to the related
  20727. major mode."
  20728. (if (let ((element (org-element-at-point)))
  20729. (and (eq (org-element-type element) 'src-block)
  20730. (< (save-excursion
  20731. (goto-char (org-element-property :post-affiliated element))
  20732. (line-end-position))
  20733. (point))
  20734. (> (save-excursion
  20735. (goto-char (org-element-property :end element))
  20736. (skip-chars-backward " \r\t\n")
  20737. (line-beginning-position))
  20738. (point))))
  20739. (progn
  20740. (require 'ob-core)
  20741. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
  20742. (beginning-of-line)
  20743. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  20744. (open-line 1))
  20745. (org-indent-line)
  20746. (insert "# ")))
  20747. (defvar comment-empty-lines) ; From newcomment.el.
  20748. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20749. "Comment or uncomment each non-blank line in the region.
  20750. Uncomment each non-blank line between BEG and END if it only
  20751. contains commented lines. Otherwise, comment them. If region is
  20752. strictly within a source block, use appropriate comment syntax."
  20753. (if (let ((element (org-element-at-point)))
  20754. (and (eq (org-element-type element) 'src-block)
  20755. (< (save-excursion
  20756. (goto-char (org-element-property :post-affiliated element))
  20757. (line-end-position))
  20758. beg)
  20759. (>= (save-excursion
  20760. (goto-char (org-element-property :end element))
  20761. (skip-chars-backward " \r\t\n")
  20762. (line-beginning-position))
  20763. end)))
  20764. (progn
  20765. (require 'ob-core)
  20766. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
  20767. (save-restriction
  20768. ;; Restrict region
  20769. (narrow-to-region (save-excursion (goto-char beg)
  20770. (skip-chars-forward " \r\t\n" end)
  20771. (line-beginning-position))
  20772. (save-excursion (goto-char end)
  20773. (skip-chars-backward " \r\t\n" beg)
  20774. (line-end-position)))
  20775. (let ((uncommentp
  20776. ;; UNCOMMENTP is non-nil when every non blank line between
  20777. ;; BEG and END is a comment.
  20778. (save-excursion
  20779. (goto-char (point-min))
  20780. (while (and (not (eobp))
  20781. (let ((element (org-element-at-point)))
  20782. (and (eq (org-element-type element) 'comment)
  20783. (goto-char (min (point-max)
  20784. (org-element-property
  20785. :end element)))))))
  20786. (eobp))))
  20787. (if uncommentp
  20788. ;; Only blank lines and comments in region: uncomment it.
  20789. (save-excursion
  20790. (goto-char (point-min))
  20791. (while (not (eobp))
  20792. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20793. (replace-match "" nil nil nil 1))
  20794. (forward-line)))
  20795. ;; Comment each line in region.
  20796. (let ((min-indent (point-max)))
  20797. ;; First find the minimum indentation across all lines.
  20798. (save-excursion
  20799. (goto-char (point-min))
  20800. (while (and (not (eobp)) (not (zerop min-indent)))
  20801. (unless (looking-at "[ \t]*$")
  20802. (setq min-indent (min min-indent (current-indentation))))
  20803. (forward-line)))
  20804. ;; Then loop over all lines.
  20805. (save-excursion
  20806. (goto-char (point-min))
  20807. (while (not (eobp))
  20808. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20809. ;; Don't get fooled by invisible text (e.g. link path)
  20810. ;; when moving to column MIN-INDENT.
  20811. (let ((buffer-invisibility-spec nil))
  20812. (org-move-to-column min-indent t))
  20813. (insert comment-start))
  20814. (forward-line)))))))))
  20815. ;;; Planning
  20816. ;; This section contains tools to operate on timestamp objects, as
  20817. ;; returned by, e.g. `org-element-context'.
  20818. (defun org-timestamp-has-time-p (timestamp)
  20819. "Non-nil when TIMESTAMP has a time specified."
  20820. (org-element-property :hour-start timestamp))
  20821. (defun org-timestamp-format (timestamp format &optional end utc)
  20822. "Format a TIMESTAMP element into a string.
  20823. FORMAT is a format specifier to be passed to
  20824. `format-time-string'.
  20825. When optional argument END is non-nil, use end of date-range or
  20826. time-range, if possible.
  20827. When optional argument UTC is non-nil, time will be expressed as
  20828. Universal Time."
  20829. (format-time-string
  20830. format
  20831. (apply 'encode-time
  20832. (cons 0
  20833. (mapcar
  20834. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20835. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20836. '(:minute-start :hour-start :day-start :month-start
  20837. :year-start)))))
  20838. utc))
  20839. (defun org-timestamp-split-range (timestamp &optional end)
  20840. "Extract a timestamp object from a date or time range.
  20841. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20842. the end of the range. Otherwise, extract its start.
  20843. Return a new timestamp object sharing the same parent as
  20844. TIMESTAMP."
  20845. (let ((type (org-element-property :type timestamp)))
  20846. (if (memq type '(active inactive diary)) timestamp
  20847. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20848. ;; Set new type.
  20849. (org-element-put-property
  20850. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20851. ;; Copy start properties over end properties if END is
  20852. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20853. (let ((p-alist '((:minute-start . :minute-end)
  20854. (:hour-start . :hour-end)
  20855. (:day-start . :day-end)
  20856. (:month-start . :month-end)
  20857. (:year-start . :year-end))))
  20858. (dolist (p-cell p-alist)
  20859. (org-element-put-property
  20860. split-ts
  20861. (funcall (if end 'car 'cdr) p-cell)
  20862. (org-element-property
  20863. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20864. ;; Eventually refresh `:raw-value'.
  20865. (org-element-put-property split-ts :raw-value nil)
  20866. (org-element-put-property
  20867. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20868. (defun org-timestamp-translate (timestamp &optional boundary)
  20869. "Apply `org-translate-time' on a TIMESTAMP object.
  20870. When optional argument BOUNDARY is non-nil, it is either the
  20871. symbol `start' or `end'. In this case, only translate the
  20872. starting or ending part of TIMESTAMP if it is a date or time
  20873. range. Otherwise, translate both parts."
  20874. (if (and (not boundary)
  20875. (memq (org-element-property :type timestamp)
  20876. '(active-range inactive-range)))
  20877. (concat
  20878. (org-translate-time
  20879. (org-element-property :raw-value
  20880. (org-timestamp-split-range timestamp)))
  20881. "--"
  20882. (org-translate-time
  20883. (org-element-property :raw-value
  20884. (org-timestamp-split-range timestamp t))))
  20885. (org-translate-time
  20886. (org-element-property
  20887. :raw-value
  20888. (if (not boundary) timestamp
  20889. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20890. ;;; Other stuff.
  20891. (defun org-toggle-fixed-width-section (arg)
  20892. "Toggle the fixed-width export.
  20893. If there is no active region, the QUOTE keyword at the current headline is
  20894. inserted or removed. When present, it causes the text between this headline
  20895. and the next to be exported as fixed-width text, and unmodified.
  20896. If there is an active region, this command adds or removes a colon as the
  20897. first character of this line. If the first character of a line is a colon,
  20898. this line is also exported in fixed-width font."
  20899. (interactive "P")
  20900. (let* ((cc 0)
  20901. (regionp (org-region-active-p))
  20902. (beg (if regionp (region-beginning) (point)))
  20903. (end (if regionp (region-end)))
  20904. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20905. (case-fold-search nil)
  20906. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20907. off)
  20908. (if regionp
  20909. (save-excursion
  20910. (goto-char beg)
  20911. (setq cc (current-column))
  20912. (beginning-of-line 1)
  20913. (setq off (looking-at re))
  20914. (while (> nlines 0)
  20915. (setq nlines (1- nlines))
  20916. (beginning-of-line 1)
  20917. (cond
  20918. (arg
  20919. (org-move-to-column cc t)
  20920. (insert ": \n")
  20921. (forward-line -1))
  20922. ((and off (looking-at re))
  20923. (replace-match "" t t nil 1))
  20924. ((not off) (org-move-to-column cc t) (insert ": ")))
  20925. (forward-line 1)))
  20926. (save-excursion
  20927. (org-back-to-heading)
  20928. (cond
  20929. ((looking-at (format org-heading-keyword-regexp-format
  20930. org-quote-string))
  20931. (goto-char (match-end 1))
  20932. (looking-at (concat " +" org-quote-string))
  20933. (replace-match "" t t)
  20934. (when (eolp) (insert " ")))
  20935. ((looking-at org-outline-regexp)
  20936. (goto-char (match-end 0))
  20937. (insert org-quote-string " ")))))))
  20938. (defun org-reftex-citation ()
  20939. "Use reftex-citation to insert a citation into the buffer.
  20940. This looks for a line like
  20941. #+BIBLIOGRAPHY: foo plain option:-d
  20942. and derives from it that foo.bib is the bibliography file relevant
  20943. for this document. It then installs the necessary environment for RefTeX
  20944. to work in this buffer and calls `reftex-citation' to insert a citation
  20945. into the buffer.
  20946. Export of such citations to both LaTeX and HTML is handled by the contributed
  20947. package ox-bibtex by Taru Karttunen."
  20948. (interactive)
  20949. (let ((reftex-docstruct-symbol 'rds)
  20950. (reftex-cite-format "\\cite{%l}")
  20951. rds bib)
  20952. (save-excursion
  20953. (save-restriction
  20954. (widen)
  20955. (let ((case-fold-search t)
  20956. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20957. (if (not (save-excursion
  20958. (or (re-search-forward re nil t)
  20959. (re-search-backward re nil t))))
  20960. (error "No bibliography defined in file")
  20961. (setq bib (concat (match-string 1) ".bib")
  20962. rds (list (list 'bib bib)))))))
  20963. (call-interactively 'reftex-citation)))
  20964. ;;;; Functions extending outline functionality
  20965. (defun org-beginning-of-line (&optional arg)
  20966. "Go to the beginning of the current line. If that is invisible, continue
  20967. to a visible line beginning. This makes the function of C-a more intuitive.
  20968. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20969. first attempt, and only move to after the tags when the cursor is already
  20970. beyond the end of the headline."
  20971. (interactive "P")
  20972. (let ((pos (point))
  20973. (special (if (consp org-special-ctrl-a/e)
  20974. (car org-special-ctrl-a/e)
  20975. org-special-ctrl-a/e))
  20976. deactivate-mark refpos)
  20977. (if (org-bound-and-true-p visual-line-mode)
  20978. (beginning-of-visual-line 1)
  20979. (beginning-of-line 1))
  20980. (if (and arg (fboundp 'move-beginning-of-line))
  20981. (call-interactively 'move-beginning-of-line)
  20982. (if (bobp)
  20983. nil
  20984. (backward-char 1)
  20985. (if (org-truely-invisible-p)
  20986. (while (and (not (bobp)) (org-truely-invisible-p))
  20987. (backward-char 1)
  20988. (beginning-of-line 1))
  20989. (forward-char 1))))
  20990. (when special
  20991. (cond
  20992. ((and (looking-at org-complex-heading-regexp)
  20993. (= (char-after (match-end 1)) ?\ ))
  20994. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20995. (point-at-eol)))
  20996. (goto-char
  20997. (if (eq special t)
  20998. (cond ((> pos refpos) refpos)
  20999. ((= pos (point)) refpos)
  21000. (t (point)))
  21001. (cond ((> pos (point)) (point))
  21002. ((not (eq last-command this-command)) (point))
  21003. (t refpos)))))
  21004. ((org-at-item-p)
  21005. ;; Being at an item and not looking at an the item means point
  21006. ;; was previously moved to beginning of a visual line, which
  21007. ;; doesn't contain the item. Therefore, do nothing special,
  21008. ;; just stay here.
  21009. (when (looking-at org-list-full-item-re)
  21010. ;; Set special position at first white space character after
  21011. ;; bullet, and check-box, if any.
  21012. (let ((after-bullet
  21013. (let ((box (match-end 3)))
  21014. (if (not box) (match-end 1)
  21015. (let ((after (char-after box)))
  21016. (if (and after (= after ? )) (1+ box) box))))))
  21017. ;; Special case: Move point to special position when
  21018. ;; currently after it or at beginning of line.
  21019. (if (eq special t)
  21020. (when (or (> pos after-bullet) (= (point) pos))
  21021. (goto-char after-bullet))
  21022. ;; Reversed case: Move point to special position when
  21023. ;; point was already at beginning of line and command is
  21024. ;; repeated.
  21025. (when (and (= (point) pos) (eq last-command this-command))
  21026. (goto-char after-bullet))))))))
  21027. (org-no-warnings
  21028. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21029. (setq disable-point-adjustment
  21030. (or (not (invisible-p (point)))
  21031. (not (invisible-p (max (point-min) (1- (point))))))))
  21032. (defun org-end-of-line (&optional arg)
  21033. "Go to the end of the line.
  21034. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21035. tags on the first attempt, and only move to after the tags when
  21036. the cursor is already beyond the end of the headline."
  21037. (interactive "P")
  21038. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21039. org-special-ctrl-a/e))
  21040. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21041. 'end-of-visual-line)
  21042. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21043. (t 'end-of-line)))
  21044. deactivate-mark)
  21045. (if (or (not special) arg) (call-interactively move-fun)
  21046. (let* ((element (save-excursion (beginning-of-line)
  21047. (org-element-at-point)))
  21048. (type (org-element-type element)))
  21049. (cond
  21050. ((memq type '(headline inlinetask))
  21051. (let ((pos (point)))
  21052. (beginning-of-line 1)
  21053. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21054. (if (eq special t)
  21055. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21056. (goto-char (match-beginning 1))
  21057. (goto-char (match-end 0)))
  21058. (if (or (< pos (match-end 0))
  21059. (not (eq this-command last-command)))
  21060. (goto-char (match-end 0))
  21061. (goto-char (match-beginning 1))))
  21062. (call-interactively move-fun))))
  21063. ((outline-invisible-p (line-end-position))
  21064. ;; If element is hidden, `move-end-of-line' would put point
  21065. ;; after it. Use `end-of-line' to stay on current line.
  21066. (call-interactively 'end-of-line))
  21067. (t (call-interactively move-fun)))))
  21068. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21069. (setq disable-point-adjustment
  21070. (or (not (invisible-p (point)))
  21071. (not (invisible-p (max (point-min) (1- (point))))))))
  21072. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21073. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21074. (defun org-backward-sentence (&optional arg)
  21075. "Go to beginning of sentence, or beginning of table field.
  21076. This will call `backward-sentence' or `org-table-beginning-of-field',
  21077. depending on context."
  21078. (interactive "P")
  21079. (cond
  21080. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21081. (t (call-interactively 'backward-sentence))))
  21082. (defun org-forward-sentence (&optional arg)
  21083. "Go to end of sentence, or end of table field.
  21084. This will call `forward-sentence' or `org-table-end-of-field',
  21085. depending on context."
  21086. (interactive "P")
  21087. (cond
  21088. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21089. (t (call-interactively 'forward-sentence))))
  21090. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21091. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21092. (defun org-kill-line (&optional arg)
  21093. "Kill line, to tags or end of line."
  21094. (interactive "P")
  21095. (cond
  21096. ((or (not org-special-ctrl-k)
  21097. (bolp)
  21098. (not (org-at-heading-p)))
  21099. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21100. org-ctrl-k-protect-subtree)
  21101. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21102. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21103. (user-error "C-k aborted as it would kill a hidden subtree")))
  21104. (call-interactively
  21105. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21106. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21107. (kill-region (point) (match-beginning 1))
  21108. (org-set-tags nil t))
  21109. (t (kill-region (point) (point-at-eol)))))
  21110. (define-key org-mode-map "\C-k" 'org-kill-line)
  21111. (defun org-yank (&optional arg)
  21112. "Yank. If the kill is a subtree, treat it specially.
  21113. This command will look at the current kill and check if is a single
  21114. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21115. cursor is at the beginning of a line or after the stars of a currently
  21116. empty headline, then the yank is handled specially. How exactly depends
  21117. on the value of the following variables, both set by default.
  21118. org-yank-folded-subtrees
  21119. When set, the subtree(s) will be folded after insertion, but only
  21120. if doing so would now swallow text after the yanked text.
  21121. org-yank-adjusted-subtrees
  21122. When set, the subtree will be promoted or demoted in order to
  21123. fit into the local outline tree structure, which means that the level
  21124. will be adjusted so that it becomes the smaller one of the two
  21125. *visible* surrounding headings.
  21126. Any prefix to this command will cause `yank' to be called directly with
  21127. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21128. will just
  21129. plainly yank the text as it is.
  21130. \[1] The test checks if the first non-white line is a heading
  21131. and if there are no other headings with fewer stars."
  21132. (interactive "P")
  21133. (org-yank-generic 'yank arg))
  21134. (defun org-yank-generic (command arg)
  21135. "Perform some yank-like command.
  21136. This function implements the behavior described in the `org-yank'
  21137. documentation. However, it has been generalized to work for any
  21138. interactive command with similar behavior."
  21139. ;; pretend to be command COMMAND
  21140. (setq this-command command)
  21141. (if arg
  21142. (call-interactively command)
  21143. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21144. (and (org-kill-is-subtree-p)
  21145. (or (bolp)
  21146. (and (looking-at "[ \t]*$")
  21147. (string-match
  21148. "\\`\\*+\\'"
  21149. (buffer-substring (point-at-bol) (point)))))))
  21150. swallowp)
  21151. (cond
  21152. ((and subtreep org-yank-folded-subtrees)
  21153. (let ((beg (point))
  21154. end)
  21155. (if (and subtreep org-yank-adjusted-subtrees)
  21156. (org-paste-subtree nil nil 'for-yank)
  21157. (call-interactively command))
  21158. (setq end (point))
  21159. (goto-char beg)
  21160. (when (and (bolp) subtreep
  21161. (not (setq swallowp
  21162. (org-yank-folding-would-swallow-text beg end))))
  21163. (org-with-limited-levels
  21164. (or (looking-at org-outline-regexp)
  21165. (re-search-forward org-outline-regexp-bol end t))
  21166. (while (and (< (point) end) (looking-at org-outline-regexp))
  21167. (hide-subtree)
  21168. (org-cycle-show-empty-lines 'folded)
  21169. (condition-case nil
  21170. (outline-forward-same-level 1)
  21171. (error (goto-char end))))))
  21172. (when swallowp
  21173. (message
  21174. "Inserted text not folded because that would swallow text"))
  21175. (goto-char end)
  21176. (skip-chars-forward " \t\n\r")
  21177. (beginning-of-line 1)
  21178. (push-mark beg 'nomsg)))
  21179. ((and subtreep org-yank-adjusted-subtrees)
  21180. (let ((beg (point-at-bol)))
  21181. (org-paste-subtree nil nil 'for-yank)
  21182. (push-mark beg 'nomsg)))
  21183. (t
  21184. (call-interactively command))))))
  21185. (defun org-yank-folding-would-swallow-text (beg end)
  21186. "Would hide-subtree at BEG swallow any text after END?"
  21187. (let (level)
  21188. (org-with-limited-levels
  21189. (save-excursion
  21190. (goto-char beg)
  21191. (when (or (looking-at org-outline-regexp)
  21192. (re-search-forward org-outline-regexp-bol end t))
  21193. (setq level (org-outline-level)))
  21194. (goto-char end)
  21195. (skip-chars-forward " \t\r\n\v\f")
  21196. (if (or (eobp)
  21197. (and (bolp) (looking-at org-outline-regexp)
  21198. (<= (org-outline-level) level)))
  21199. nil ; Nothing would be swallowed
  21200. t))))) ; something would swallow
  21201. (define-key org-mode-map "\C-y" 'org-yank)
  21202. (defun org-truely-invisible-p ()
  21203. "Check if point is at a character currently not visible.
  21204. This version does not only check the character property, but also
  21205. `visible-mode'."
  21206. ;; Early versions of noutline don't have `outline-invisible-p'.
  21207. (if (org-bound-and-true-p visible-mode)
  21208. nil
  21209. (outline-invisible-p)))
  21210. (defun org-invisible-p2 ()
  21211. "Check if point is at a character currently not visible."
  21212. (save-excursion
  21213. (if (and (eolp) (not (bobp))) (backward-char 1))
  21214. ;; Early versions of noutline don't have `outline-invisible-p'.
  21215. (outline-invisible-p)))
  21216. (defun org-back-to-heading (&optional invisible-ok)
  21217. "Call `outline-back-to-heading', but provide a better error message."
  21218. (condition-case nil
  21219. (outline-back-to-heading invisible-ok)
  21220. (error (error "Before first headline at position %d in buffer %s"
  21221. (point) (current-buffer)))))
  21222. (defun org-before-first-heading-p ()
  21223. "Before first heading?"
  21224. (save-excursion
  21225. (end-of-line)
  21226. (null (re-search-backward org-outline-regexp-bol nil t))))
  21227. (defun org-at-heading-p (&optional ignored)
  21228. (outline-on-heading-p t))
  21229. ;; Compatibility alias with Org versions < 7.8.03
  21230. (defalias 'org-on-heading-p 'org-at-heading-p)
  21231. (defun org-at-comment-p nil
  21232. "Is cursor in a line starting with a # character?"
  21233. (save-excursion
  21234. (beginning-of-line)
  21235. (looking-at "^#")))
  21236. (defun org-at-drawer-p nil
  21237. "Is cursor at a drawer keyword?"
  21238. (save-excursion
  21239. (move-beginning-of-line 1)
  21240. (looking-at org-drawer-regexp)))
  21241. (defun org-at-block-p nil
  21242. "Is cursor at a block keyword?"
  21243. (save-excursion
  21244. (move-beginning-of-line 1)
  21245. (looking-at org-block-regexp)))
  21246. (defun org-point-at-end-of-empty-headline ()
  21247. "If point is at the end of an empty headline, return t, else nil.
  21248. If the heading only contains a TODO keyword, it is still still considered
  21249. empty."
  21250. (and (looking-at "[ \t]*$")
  21251. (when org-todo-line-regexp
  21252. (save-excursion
  21253. (beginning-of-line 1)
  21254. (let ((case-fold-search nil))
  21255. (looking-at org-todo-line-regexp)
  21256. (string= (match-string 3) ""))))))
  21257. (defun org-at-heading-or-item-p ()
  21258. (or (org-at-heading-p) (org-at-item-p)))
  21259. (defun org-at-target-p ()
  21260. (or (org-in-regexp org-radio-target-regexp)
  21261. (org-in-regexp org-target-regexp)))
  21262. ;; Compatibility alias with Org versions < 7.8.03
  21263. (defalias 'org-on-target-p 'org-at-target-p)
  21264. (defun org-up-heading-all (arg)
  21265. "Move to the heading line of which the present line is a subheading.
  21266. This function considers both visible and invisible heading lines.
  21267. With argument, move up ARG levels."
  21268. (if (fboundp 'outline-up-heading-all)
  21269. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21270. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21271. (defun org-up-heading-safe ()
  21272. "Move to the heading line of which the present line is a subheading.
  21273. This version will not throw an error. It will return the level of the
  21274. headline found, or nil if no higher level is found.
  21275. Also, this function will be a lot faster than `outline-up-heading',
  21276. because it relies on stars being the outline starters. This can really
  21277. make a significant difference in outlines with very many siblings."
  21278. (let (start-level re)
  21279. (org-back-to-heading t)
  21280. (setq start-level (funcall outline-level))
  21281. (if (equal start-level 1)
  21282. nil
  21283. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21284. (if (re-search-backward re nil t)
  21285. (funcall outline-level)))))
  21286. (defun org-first-sibling-p ()
  21287. "Is this heading the first child of its parents?"
  21288. (interactive)
  21289. (let ((re org-outline-regexp-bol)
  21290. level l)
  21291. (unless (org-at-heading-p t)
  21292. (user-error "Not at a heading"))
  21293. (setq level (funcall outline-level))
  21294. (save-excursion
  21295. (if (not (re-search-backward re nil t))
  21296. t
  21297. (setq l (funcall outline-level))
  21298. (< l level)))))
  21299. (defun org-goto-sibling (&optional previous)
  21300. "Goto the next sibling, even if it is invisible.
  21301. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21302. when a sibling was found. When none is found, return nil and don't
  21303. move point."
  21304. (let ((fun (if previous 're-search-backward 're-search-forward))
  21305. (pos (point))
  21306. (re org-outline-regexp-bol)
  21307. level l)
  21308. (when (condition-case nil (org-back-to-heading t) (error nil))
  21309. (setq level (funcall outline-level))
  21310. (catch 'exit
  21311. (or previous (forward-char 1))
  21312. (while (funcall fun re nil t)
  21313. (setq l (funcall outline-level))
  21314. (when (< l level) (goto-char pos) (throw 'exit nil))
  21315. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21316. (goto-char pos)
  21317. nil))))
  21318. (defun org-show-siblings ()
  21319. "Show all siblings of the current headline."
  21320. (save-excursion
  21321. (while (org-goto-sibling) (org-flag-heading nil)))
  21322. (save-excursion
  21323. (while (org-goto-sibling 'previous)
  21324. (org-flag-heading nil))))
  21325. (defun org-goto-first-child ()
  21326. "Goto the first child, even if it is invisible.
  21327. Return t when a child was found. Otherwise don't move point and
  21328. return nil."
  21329. (let (level (pos (point)) (re org-outline-regexp-bol))
  21330. (when (condition-case nil (org-back-to-heading t) (error nil))
  21331. (setq level (outline-level))
  21332. (forward-char 1)
  21333. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21334. (progn (goto-char (match-beginning 0)) t)
  21335. (goto-char pos) nil))))
  21336. (defun org-show-hidden-entry ()
  21337. "Show an entry where even the heading is hidden."
  21338. (save-excursion
  21339. (org-show-entry)))
  21340. (defun org-flag-heading (flag &optional entry)
  21341. "Flag the current heading. FLAG non-nil means make invisible.
  21342. When ENTRY is non-nil, show the entire entry."
  21343. (save-excursion
  21344. (org-back-to-heading t)
  21345. ;; Check if we should show the entire entry
  21346. (if entry
  21347. (progn
  21348. (org-show-entry)
  21349. (save-excursion
  21350. (and (outline-next-heading)
  21351. (org-flag-heading nil))))
  21352. (outline-flag-region (max (point-min) (1- (point)))
  21353. (save-excursion (outline-end-of-heading) (point))
  21354. flag))))
  21355. (defun org-get-next-sibling ()
  21356. "Move to next heading of the same level, and return point.
  21357. If there is no such heading, return nil.
  21358. This is like outline-next-sibling, but invisible headings are ok."
  21359. (let ((level (funcall outline-level)))
  21360. (outline-next-heading)
  21361. (while (and (not (eobp)) (> (funcall outline-level) level))
  21362. (outline-next-heading))
  21363. (if (or (eobp) (< (funcall outline-level) level))
  21364. nil
  21365. (point))))
  21366. (defun org-get-last-sibling ()
  21367. "Move to previous heading of the same level, and return point.
  21368. If there is no such heading, return nil."
  21369. (let ((opoint (point))
  21370. (level (funcall outline-level)))
  21371. (outline-previous-heading)
  21372. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21373. (while (and (> (funcall outline-level) level)
  21374. (not (bobp)))
  21375. (outline-previous-heading))
  21376. (if (< (funcall outline-level) level)
  21377. nil
  21378. (point)))))
  21379. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21380. "Goto to the end of a subtree."
  21381. ;; This contains an exact copy of the original function, but it uses
  21382. ;; `org-back-to-heading', to make it work also in invisible
  21383. ;; trees. And is uses an invisible-ok argument.
  21384. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21385. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21386. ;; with many children and grandchildren etc, this can be much faster
  21387. ;; than the outline version.
  21388. (org-back-to-heading invisible-ok)
  21389. (let ((first t)
  21390. (level (funcall outline-level)))
  21391. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21392. ;; A true heading (not a plain list item), in Org-mode
  21393. ;; This means we can easily find the end by looking
  21394. ;; only for the right number of stars. Using a regexp to do
  21395. ;; this is so much faster than using a Lisp loop.
  21396. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21397. (forward-char 1)
  21398. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21399. ;; something else, do it the slow way
  21400. (while (and (not (eobp))
  21401. (or first (> (funcall outline-level) level)))
  21402. (setq first nil)
  21403. (outline-next-heading)))
  21404. (unless to-heading
  21405. (if (memq (preceding-char) '(?\n ?\^M))
  21406. (progn
  21407. ;; Go to end of line before heading
  21408. (forward-char -1)
  21409. (if (memq (preceding-char) '(?\n ?\^M))
  21410. ;; leave blank line before heading
  21411. (forward-char -1))))))
  21412. (point))
  21413. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21414. "Use Org version in org-mode, for dramatic speed-up."
  21415. (if (derived-mode-p 'org-mode)
  21416. (progn
  21417. (org-end-of-subtree nil t)
  21418. (unless (eobp) (backward-char 1)))
  21419. ad-do-it))
  21420. (defun org-end-of-meta-data-and-drawers ()
  21421. "Jump to the first text after meta data and drawers in the current entry.
  21422. This will move over empty lines, lines with planning time stamps,
  21423. clocking lines, and drawers."
  21424. (org-back-to-heading t)
  21425. (let ((end (save-excursion (outline-next-heading) (point)))
  21426. (re (concat "\\(" org-drawer-regexp "\\)"
  21427. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21428. (forward-line 1)
  21429. (while (re-search-forward re end t)
  21430. (if (not (match-end 1))
  21431. ;; empty or planning line
  21432. (forward-line 1)
  21433. ;; a drawer, find the end
  21434. (re-search-forward "^[ \t]*:END:" end 'move)
  21435. (forward-line 1)))
  21436. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21437. (point)))
  21438. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21439. "Move forward to the ARG'th subheading at same level as this one.
  21440. Stop at the first and last subheadings of a superior heading.
  21441. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21442. non-nil it will also look at invisible ones."
  21443. (interactive "p")
  21444. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21445. (if (and arg (< arg 0))
  21446. (goto-char (point-min))
  21447. (outline-next-heading))
  21448. (org-at-heading-p)
  21449. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21450. (f (if (and arg (< arg 0))
  21451. 're-search-backward
  21452. 're-search-forward))
  21453. (count (if arg (abs arg) 1))
  21454. (result (point)))
  21455. (while (and (prog1 (> count 0)
  21456. (forward-char (if (and arg (< arg 0)) -1 1)))
  21457. (funcall f org-outline-regexp-bol nil 'move))
  21458. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21459. (cond ((< l level) (setq count 0))
  21460. ((and (= l level)
  21461. (or invisible-ok
  21462. (progn
  21463. (goto-char (line-beginning-position))
  21464. (not (outline-invisible-p)))))
  21465. (setq count (1- count))
  21466. (when (eq l level)
  21467. (setq result (point)))))))
  21468. (goto-char result))
  21469. (beginning-of-line 1)))
  21470. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21471. "Move backward to the ARG'th subheading at same level as this one.
  21472. Stop at the first and last subheadings of a superior heading."
  21473. (interactive "p")
  21474. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21475. (defun org-next-block (arg &optional backward block-regexp)
  21476. "Jump to the next block.
  21477. With a prefix argument ARG, jump forward ARG many source blocks.
  21478. When BACKWARD is non-nil, jump to the previous block.
  21479. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21480. (interactive "p")
  21481. (let ((re (or block-regexp org-block-regexp))
  21482. (re-search-fn (or (and backward 're-search-backward)
  21483. 're-search-forward)))
  21484. (if (looking-at re) (forward-char 1))
  21485. (condition-case nil
  21486. (funcall re-search-fn re nil nil arg)
  21487. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21488. (goto-char (match-beginning 0)) (org-show-context)))
  21489. (defun org-previous-block (arg &optional block-regexp)
  21490. "Jump to the previous block.
  21491. With a prefix argument ARG, jump backward ARG many source blocks.
  21492. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21493. (interactive "p")
  21494. (org-next-block arg t block-regexp))
  21495. (defun org-forward-paragraph ()
  21496. "Move forward to beginning of next paragraph or equivalent.
  21497. The function moves point to the beginning of the next visible
  21498. structural element, which can be a paragraph, a table, a list
  21499. item, etc. It also provides some special moves for convenience:
  21500. - On an affiliated keyword, jump to the beginning of the
  21501. relative element.
  21502. - On an item or a footnote definition, move to the second
  21503. element inside, if any.
  21504. - On a table or a property drawer, jump after it.
  21505. - On a verse or source block, stop after blank lines."
  21506. (interactive)
  21507. (when (eobp) (user-error "Cannot move further down"))
  21508. (let* ((deactivate-mark nil)
  21509. (element (org-element-at-point))
  21510. (type (org-element-type element))
  21511. (post-affiliated (org-element-property :post-affiliated element))
  21512. (contents-begin (org-element-property :contents-begin element))
  21513. (contents-end (org-element-property :contents-end element))
  21514. (end (let ((end (org-element-property :end element)) (parent element))
  21515. (while (and (setq parent (org-element-property :parent parent))
  21516. (= (org-element-property :contents-end parent) end))
  21517. (setq end (org-element-property :end parent)))
  21518. end)))
  21519. (cond ((not element)
  21520. (skip-chars-forward " \r\t\n")
  21521. (or (eobp) (beginning-of-line)))
  21522. ;; On affiliated keywords, move to element's beginning.
  21523. ((and post-affiliated (< (point) post-affiliated))
  21524. (goto-char post-affiliated))
  21525. ;; At a table row, move to the end of the table. Similarly,
  21526. ;; at a node property, move to the end of the property
  21527. ;; drawer.
  21528. ((memq type '(node-property table-row))
  21529. (goto-char (org-element-property
  21530. :end (org-element-property :parent element))))
  21531. ((memq type '(property-drawer table)) (goto-char end))
  21532. ;; Consider blank lines as separators in verse and source
  21533. ;; blocks to ease editing.
  21534. ((memq type '(src-block verse-block))
  21535. (when (eq type 'src-block)
  21536. (setq contents-end
  21537. (save-excursion (goto-char end)
  21538. (skip-chars-backward " \r\t\n")
  21539. (line-beginning-position))))
  21540. (beginning-of-line)
  21541. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21542. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21543. (goto-char end)
  21544. (skip-chars-forward " \r\t\n")
  21545. (if (= (point) contents-end) (goto-char end)
  21546. (beginning-of-line))))
  21547. ;; With no contents, just skip element.
  21548. ((not contents-begin) (goto-char end))
  21549. ;; If contents are invisible, skip the element altogether.
  21550. ((outline-invisible-p (line-end-position))
  21551. (case type
  21552. (headline
  21553. (org-with-limited-levels (outline-next-visible-heading 1)))
  21554. ;; At a plain list, make sure we move to the next item
  21555. ;; instead of skipping the whole list.
  21556. (plain-list (forward-char)
  21557. (org-forward-paragraph))
  21558. (otherwise (goto-char end))))
  21559. ((>= (point) contents-end) (goto-char end))
  21560. ((>= (point) contents-begin)
  21561. ;; This can only happen on paragraphs and plain lists.
  21562. (case type
  21563. (paragraph (goto-char end))
  21564. ;; At a plain list, try to move to second element in
  21565. ;; first item, if possible.
  21566. (plain-list (end-of-line)
  21567. (org-forward-paragraph))))
  21568. ;; When contents start on the middle of a line (e.g. in
  21569. ;; items and footnote definitions), try to reach first
  21570. ;; element starting after current line.
  21571. ((> (line-end-position) contents-begin)
  21572. (end-of-line)
  21573. (org-forward-paragraph))
  21574. (t (goto-char contents-begin)))))
  21575. (defun org-backward-paragraph ()
  21576. "Move backward to start of previous paragraph or equivalent.
  21577. The function moves point to the beginning of the current
  21578. structural element, which can be a paragraph, a table, a list
  21579. item, etc., or to the beginning of the previous visible one if
  21580. point is already there. It also provides some special moves for
  21581. convenience:
  21582. - On an affiliated keyword, jump to the first one.
  21583. - On a table or a property drawer, move to its beginning.
  21584. - On a verse or source block, stop before blank lines."
  21585. (interactive)
  21586. (when (bobp) (user-error "Cannot move further up"))
  21587. (let* ((deactivate-mark nil)
  21588. (element (org-element-at-point))
  21589. (type (org-element-type element))
  21590. (contents-begin (org-element-property :contents-begin element))
  21591. (contents-end (org-element-property :contents-end element))
  21592. (post-affiliated (org-element-property :post-affiliated element))
  21593. (begin (org-element-property :begin element)))
  21594. (cond
  21595. ((not element) (goto-char (point-min)))
  21596. ((= (point) begin)
  21597. (backward-char)
  21598. (org-backward-paragraph))
  21599. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21600. ((memq type '(node-property table-row))
  21601. (goto-char (org-element-property
  21602. :post-affiliated (org-element-property :parent element))))
  21603. ((memq type '(property-drawer table)) (goto-char begin))
  21604. ((memq type '(src-block verse-block))
  21605. (when (eq type 'src-block)
  21606. (setq contents-begin
  21607. (save-excursion (goto-char begin) (forward-line) (point))))
  21608. (if (= (point) contents-begin) (goto-char post-affiliated)
  21609. ;; Inside a verse block, see blank lines as paragraph
  21610. ;; separators.
  21611. (let ((origin (point)))
  21612. (skip-chars-backward " \r\t\n" contents-begin)
  21613. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21614. (skip-chars-forward " \r\t\n" origin)
  21615. (if (= (point) origin) (goto-char contents-begin)
  21616. (beginning-of-line))))))
  21617. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21618. ((eq type 'paragraph)
  21619. (goto-char contents-begin)
  21620. ;; When at first paragraph in an item or a footnote definition,
  21621. ;; move directly to beginning of line.
  21622. (let ((parent-contents
  21623. (org-element-property
  21624. :contents-begin (org-element-property :parent element))))
  21625. (when (and parent-contents (= parent-contents contents-begin))
  21626. (beginning-of-line))))
  21627. ;; At the end of a greater element, move to the beginning of the
  21628. ;; last element within.
  21629. ((>= (point) contents-end)
  21630. (goto-char (1- contents-end))
  21631. (org-backward-paragraph))
  21632. (t (goto-char (or post-affiliated begin))))
  21633. ;; Ensure we never leave point invisible.
  21634. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21635. (defun org-forward-element ()
  21636. "Move forward by one element.
  21637. Move to the next element at the same level, when possible."
  21638. (interactive)
  21639. (cond ((eobp) (user-error "Cannot move further down"))
  21640. ((org-with-limited-levels (org-at-heading-p))
  21641. (let ((origin (point)))
  21642. (goto-char (org-end-of-subtree nil t))
  21643. (unless (org-with-limited-levels (org-at-heading-p))
  21644. (goto-char origin)
  21645. (user-error "Cannot move further down"))))
  21646. (t
  21647. (let* ((elem (org-element-at-point))
  21648. (end (org-element-property :end elem))
  21649. (parent (org-element-property :parent elem)))
  21650. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21651. (goto-char (org-element-property :end parent)))
  21652. ((integer-or-marker-p end) (goto-char end))
  21653. (t (message "No element at point")))))))
  21654. (defun org-backward-element ()
  21655. "Move backward by one element.
  21656. Move to the previous element at the same level, when possible."
  21657. (interactive)
  21658. (cond ((bobp) (user-error "Cannot move further up"))
  21659. ((org-with-limited-levels (org-at-heading-p))
  21660. ;; At a headline, move to the previous one, if any, or stay
  21661. ;; here.
  21662. (let ((origin (point)))
  21663. (org-with-limited-levels (org-backward-heading-same-level 1))
  21664. ;; When current headline has no sibling above, move to its
  21665. ;; parent.
  21666. (when (= (point) origin)
  21667. (or (org-with-limited-levels (org-up-heading-safe))
  21668. (progn (goto-char origin)
  21669. (user-error "Cannot move further up"))))))
  21670. (t
  21671. (let* ((trail (org-element-at-point 'keep-trail))
  21672. (elem (car trail))
  21673. (prev-elem (nth 1 trail))
  21674. (beg (org-element-property :begin elem)))
  21675. (cond
  21676. ;; Move to beginning of current element if point isn't
  21677. ;; there already.
  21678. ((null beg) (message "No element at point"))
  21679. ((/= (point) beg) (goto-char beg))
  21680. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21681. ((org-before-first-heading-p) (goto-char (point-min)))
  21682. (t (org-back-to-heading)))))))
  21683. (defun org-up-element ()
  21684. "Move to upper element."
  21685. (interactive)
  21686. (if (org-with-limited-levels (org-at-heading-p))
  21687. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21688. (let* ((elem (org-element-at-point))
  21689. (parent (org-element-property :parent elem)))
  21690. (if parent (goto-char (org-element-property :begin parent))
  21691. (if (org-with-limited-levels (org-before-first-heading-p))
  21692. (user-error "No surrounding element")
  21693. (org-with-limited-levels (org-back-to-heading)))))))
  21694. (defvar org-element-greater-elements)
  21695. (defun org-down-element ()
  21696. "Move to inner element."
  21697. (interactive)
  21698. (let ((element (org-element-at-point)))
  21699. (cond
  21700. ((memq (org-element-type element) '(plain-list table))
  21701. (goto-char (org-element-property :contents-begin element))
  21702. (forward-char))
  21703. ((memq (org-element-type element) org-element-greater-elements)
  21704. ;; If contents are hidden, first disclose them.
  21705. (when (outline-invisible-p (line-end-position)) (org-cycle))
  21706. (goto-char (or (org-element-property :contents-begin element)
  21707. (user-error "No content for this element"))))
  21708. (t (user-error "No inner element")))))
  21709. (defun org-drag-element-backward ()
  21710. "Move backward element at point."
  21711. (interactive)
  21712. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21713. (let* ((trail (org-element-at-point 'keep-trail))
  21714. (elem (car trail))
  21715. (prev-elem (nth 1 trail)))
  21716. ;; Error out if no previous element or previous element is
  21717. ;; a parent of the current one.
  21718. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21719. (user-error "Cannot drag element backward")
  21720. (let ((pos (point)))
  21721. (org-element-swap-A-B prev-elem elem)
  21722. (goto-char (+ (org-element-property :begin prev-elem)
  21723. (- pos (org-element-property :begin elem)))))))))
  21724. (defun org-drag-element-forward ()
  21725. "Move forward element at point."
  21726. (interactive)
  21727. (let* ((pos (point))
  21728. (elem (org-element-at-point)))
  21729. (when (= (point-max) (org-element-property :end elem))
  21730. (user-error "Cannot drag element forward"))
  21731. (goto-char (org-element-property :end elem))
  21732. (let ((next-elem (org-element-at-point)))
  21733. (when (or (org-element-nested-p elem next-elem)
  21734. (and (eq (org-element-type next-elem) 'headline)
  21735. (not (eq (org-element-type elem) 'headline))))
  21736. (goto-char pos)
  21737. (user-error "Cannot drag element forward"))
  21738. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21739. ;; body's length (without final blanks) and by the length of
  21740. ;; blanks between ELEM and NEXT-ELEM.
  21741. (let ((size-next (- (save-excursion
  21742. (goto-char (org-element-property :end next-elem))
  21743. (skip-chars-backward " \r\t\n")
  21744. (forward-line)
  21745. ;; Small correction if buffer doesn't end
  21746. ;; with a newline character.
  21747. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21748. (org-element-property :begin next-elem)))
  21749. (size-blank (- (org-element-property :end elem)
  21750. (save-excursion
  21751. (goto-char (org-element-property :end elem))
  21752. (skip-chars-backward " \r\t\n")
  21753. (forward-line)
  21754. (point)))))
  21755. (org-element-swap-A-B elem next-elem)
  21756. (goto-char (+ pos size-next size-blank))))))
  21757. (defun org-drag-line-forward (arg)
  21758. "Drag the line at point ARG lines forward."
  21759. (interactive "p")
  21760. (dotimes (n (abs arg))
  21761. (let ((c (current-column)))
  21762. (if (< 0 arg)
  21763. (progn
  21764. (beginning-of-line 2)
  21765. (transpose-lines 1)
  21766. (beginning-of-line 0))
  21767. (transpose-lines 1)
  21768. (beginning-of-line -1))
  21769. (org-move-to-column c))))
  21770. (defun org-drag-line-backward (arg)
  21771. "Drag the line at point ARG lines backward."
  21772. (interactive "p")
  21773. (org-drag-line-forward (- arg)))
  21774. (defun org-mark-element ()
  21775. "Put point at beginning of this element, mark at end.
  21776. Interactively, if this command is repeated or (in Transient Mark
  21777. mode) if the mark is active, it marks the next element after the
  21778. ones already marked."
  21779. (interactive)
  21780. (let (deactivate-mark)
  21781. (if (and (org-called-interactively-p 'any)
  21782. (or (and (eq last-command this-command) (mark t))
  21783. (and transient-mark-mode mark-active)))
  21784. (set-mark
  21785. (save-excursion
  21786. (goto-char (mark))
  21787. (goto-char (org-element-property :end (org-element-at-point)))))
  21788. (let ((element (org-element-at-point)))
  21789. (end-of-line)
  21790. (push-mark (org-element-property :end element) t t)
  21791. (goto-char (org-element-property :begin element))))))
  21792. (defun org-narrow-to-element ()
  21793. "Narrow buffer to current element."
  21794. (interactive)
  21795. (let ((elem (org-element-at-point)))
  21796. (cond
  21797. ((eq (car elem) 'headline)
  21798. (narrow-to-region
  21799. (org-element-property :begin elem)
  21800. (org-element-property :end elem)))
  21801. ((memq (car elem) org-element-greater-elements)
  21802. (narrow-to-region
  21803. (org-element-property :contents-begin elem)
  21804. (org-element-property :contents-end elem)))
  21805. (t
  21806. (narrow-to-region
  21807. (org-element-property :begin elem)
  21808. (org-element-property :end elem))))))
  21809. (defun org-transpose-element ()
  21810. "Transpose current and previous elements, keeping blank lines between.
  21811. Point is moved after both elements."
  21812. (interactive)
  21813. (org-skip-whitespace)
  21814. (let ((end (org-element-property :end (org-element-at-point))))
  21815. (org-drag-element-backward)
  21816. (goto-char end)))
  21817. (defun org-unindent-buffer ()
  21818. "Un-indent the visible part of the buffer.
  21819. Relative indentation (between items, inside blocks, etc.) isn't
  21820. modified."
  21821. (interactive)
  21822. (unless (eq major-mode 'org-mode)
  21823. (user-error "Cannot un-indent a buffer not in Org mode"))
  21824. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21825. unindent-tree ; For byte-compiler.
  21826. (unindent-tree
  21827. (function
  21828. (lambda (contents)
  21829. (mapc
  21830. (lambda (element)
  21831. (if (memq (org-element-type element) '(headline section))
  21832. (funcall unindent-tree (org-element-contents element))
  21833. (save-excursion
  21834. (save-restriction
  21835. (narrow-to-region
  21836. (org-element-property :begin element)
  21837. (org-element-property :end element))
  21838. (org-do-remove-indentation)))))
  21839. (reverse contents))))))
  21840. (funcall unindent-tree (org-element-contents parse-tree))))
  21841. (defun org-show-subtree ()
  21842. "Show everything after this heading at deeper levels."
  21843. (interactive)
  21844. (outline-flag-region
  21845. (point)
  21846. (save-excursion
  21847. (org-end-of-subtree t t))
  21848. nil))
  21849. (defun org-show-entry ()
  21850. "Show the body directly following this heading.
  21851. Show the heading too, if it is currently invisible."
  21852. (interactive)
  21853. (save-excursion
  21854. (condition-case nil
  21855. (progn
  21856. (org-back-to-heading t)
  21857. (outline-flag-region
  21858. (max (point-min) (1- (point)))
  21859. (save-excursion
  21860. (if (re-search-forward
  21861. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21862. (match-beginning 1)
  21863. (point-max)))
  21864. nil)
  21865. (org-cycle-hide-drawers 'children))
  21866. (error nil))))
  21867. (defun org-make-options-regexp (kwds &optional extra)
  21868. "Make a regular expression for keyword lines."
  21869. (concat
  21870. "^#\\+\\("
  21871. (mapconcat 'regexp-quote kwds "\\|")
  21872. (if extra (concat "\\|" extra))
  21873. "\\):[ \t]*\\(.*\\)"))
  21874. ;; Make isearch reveal the necessary context
  21875. (defun org-isearch-end ()
  21876. "Reveal context after isearch exits."
  21877. (when isearch-success ; only if search was successful
  21878. (if (featurep 'xemacs)
  21879. ;; Under XEmacs, the hook is run in the correct place,
  21880. ;; we directly show the context.
  21881. (org-show-context 'isearch)
  21882. ;; In Emacs the hook runs *before* restoring the overlays.
  21883. ;; So we have to use a one-time post-command-hook to do this.
  21884. ;; (Emacs 22 has a special variable, see function `org-mode')
  21885. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21886. isearch-mode-end-hook-quit)
  21887. ;; Only when the isearch was not quitted.
  21888. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21889. 'append 'local)))
  21890. (org-fix-ellipsis-at-bol)))
  21891. (defun org-isearch-post-command ()
  21892. "Remove self from hook, and show context."
  21893. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21894. (org-show-context 'isearch))
  21895. ;;;; Integration with and fixes for other packages
  21896. ;;; Imenu support
  21897. (defvar org-imenu-markers nil
  21898. "All markers currently used by Imenu.")
  21899. (make-variable-buffer-local 'org-imenu-markers)
  21900. (defun org-imenu-new-marker (&optional pos)
  21901. "Return a new marker for use by Imenu, and remember the marker."
  21902. (let ((m (make-marker)))
  21903. (move-marker m (or pos (point)))
  21904. (push m org-imenu-markers)
  21905. m))
  21906. (defun org-imenu-get-tree ()
  21907. "Produce the index for Imenu."
  21908. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21909. (setq org-imenu-markers nil)
  21910. (let* ((n org-imenu-depth)
  21911. (re (concat "^" (org-get-limited-outline-regexp)))
  21912. (subs (make-vector (1+ n) nil))
  21913. (last-level 0)
  21914. m level head0 head)
  21915. (save-excursion
  21916. (save-restriction
  21917. (widen)
  21918. (goto-char (point-max))
  21919. (while (re-search-backward re nil t)
  21920. (setq level (org-reduced-level (funcall outline-level)))
  21921. (when (and (<= level n)
  21922. (looking-at org-complex-heading-regexp)
  21923. (setq head0 (org-match-string-no-properties 4)))
  21924. (setq head (org-link-display-format head0)
  21925. m (org-imenu-new-marker))
  21926. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21927. (if (>= level last-level)
  21928. (push (cons head m) (aref subs level))
  21929. (push (cons head (aref subs (1+ level))) (aref subs level))
  21930. (loop for i from (1+ level) to n do (aset subs i nil)))
  21931. (setq last-level level)))))
  21932. (aref subs 1)))
  21933. (eval-after-load "imenu"
  21934. '(progn
  21935. (add-hook 'imenu-after-jump-hook
  21936. (lambda ()
  21937. (if (derived-mode-p 'org-mode)
  21938. (org-show-context 'org-goto))))))
  21939. (defun org-link-display-format (link)
  21940. "Replace a link with its the description.
  21941. If there is no description, use the link target."
  21942. (save-match-data
  21943. (if (string-match org-bracket-link-analytic-regexp link)
  21944. (replace-match (if (match-end 5)
  21945. (match-string 5 link)
  21946. (concat (match-string 1 link)
  21947. (match-string 3 link)))
  21948. nil t link)
  21949. link)))
  21950. (defun org-toggle-link-display ()
  21951. "Toggle the literal or descriptive display of links."
  21952. (interactive)
  21953. (if org-descriptive-links
  21954. (progn (org-remove-from-invisibility-spec '(org-link))
  21955. (org-restart-font-lock)
  21956. (setq org-descriptive-links nil))
  21957. (progn (add-to-invisibility-spec '(org-link))
  21958. (org-restart-font-lock)
  21959. (setq org-descriptive-links t))))
  21960. ;; Speedbar support
  21961. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21962. "Overlay marking the agenda restriction line in speedbar.")
  21963. (overlay-put org-speedbar-restriction-lock-overlay
  21964. 'face 'org-agenda-restriction-lock)
  21965. (overlay-put org-speedbar-restriction-lock-overlay
  21966. 'help-echo "Agendas are currently limited to this item.")
  21967. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21968. (defun org-speedbar-set-agenda-restriction ()
  21969. "Restrict future agenda commands to the location at point in speedbar.
  21970. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21971. (interactive)
  21972. (require 'org-agenda)
  21973. (let (p m tp np dir txt)
  21974. (cond
  21975. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21976. 'org-imenu t))
  21977. (setq m (get-text-property p 'org-imenu-marker))
  21978. (with-current-buffer (marker-buffer m)
  21979. (goto-char m)
  21980. (org-agenda-set-restriction-lock 'subtree)))
  21981. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21982. 'speedbar-function 'speedbar-find-file))
  21983. (setq tp (previous-single-property-change
  21984. (1+ p) 'speedbar-function)
  21985. np (next-single-property-change
  21986. tp 'speedbar-function)
  21987. dir (speedbar-line-directory)
  21988. txt (buffer-substring-no-properties (or tp (point-min))
  21989. (or np (point-max))))
  21990. (with-current-buffer (find-file-noselect
  21991. (let ((default-directory dir))
  21992. (expand-file-name txt)))
  21993. (unless (derived-mode-p 'org-mode)
  21994. (user-error "Cannot restrict to non-Org-mode file"))
  21995. (org-agenda-set-restriction-lock 'file)))
  21996. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21997. (move-overlay org-speedbar-restriction-lock-overlay
  21998. (point-at-bol) (point-at-eol))
  21999. (setq current-prefix-arg nil)
  22000. (org-agenda-maybe-redo)))
  22001. (defvar speedbar-file-key-map)
  22002. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22003. (eval-after-load "speedbar"
  22004. '(progn
  22005. (speedbar-add-supported-extension ".org")
  22006. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22007. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22008. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22009. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22010. (add-hook 'speedbar-visiting-tag-hook
  22011. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22012. ;;; Fixes and Hacks for problems with other packages
  22013. ;; Make flyspell not check words in links, to not mess up our keymap
  22014. (defvar org-element-affiliated-keywords) ; From org-element.el
  22015. (defvar org-element-block-name-alist) ; From org-element.el
  22016. (defun org-mode-flyspell-verify ()
  22017. "Don't let flyspell put overlays at active buttons, or on
  22018. {todo,all-time,additional-option-like}-keywords."
  22019. (require 'org-element) ; For `org-element-affiliated-keywords'
  22020. (let ((pos (max (1- (point)) (point-min)))
  22021. (word (thing-at-point 'word)))
  22022. (and (not (get-text-property pos 'keymap))
  22023. (not (get-text-property pos 'org-no-flyspell))
  22024. (not (member word org-todo-keywords-1))
  22025. (not (member word org-all-time-keywords))
  22026. (not (member word org-options-keywords))
  22027. (not (member word (mapcar 'car org-startup-options)))
  22028. (not (member-ignore-case word org-element-affiliated-keywords))
  22029. (not (member-ignore-case word (org-get-export-keywords)))
  22030. (not (member-ignore-case
  22031. word (mapcar 'car org-element-block-name-alist)))
  22032. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  22033. (not (org-in-src-block-p)))))
  22034. (defun org-remove-flyspell-overlays-in (beg end)
  22035. "Remove flyspell overlays in region."
  22036. (and (org-bound-and-true-p flyspell-mode)
  22037. (fboundp 'flyspell-delete-region-overlays)
  22038. (flyspell-delete-region-overlays beg end))
  22039. (add-text-properties beg end '(org-no-flyspell t)))
  22040. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22041. (eval-after-load "bookmark"
  22042. '(if (boundp 'bookmark-after-jump-hook)
  22043. ;; We can use the hook
  22044. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22045. ;; Hook not available, use advice
  22046. (defadvice bookmark-jump (after org-make-visible activate)
  22047. "Make the position visible."
  22048. (org-bookmark-jump-unhide))))
  22049. ;; Make sure saveplace shows the location if it was hidden
  22050. (eval-after-load "saveplace"
  22051. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22052. "Make the position visible."
  22053. (org-bookmark-jump-unhide)))
  22054. ;; Make sure ecb shows the location if it was hidden
  22055. (eval-after-load "ecb"
  22056. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22057. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22058. (if (derived-mode-p 'org-mode)
  22059. (org-show-context))))
  22060. (defun org-bookmark-jump-unhide ()
  22061. "Unhide the current position, to show the bookmark location."
  22062. (and (derived-mode-p 'org-mode)
  22063. (or (outline-invisible-p)
  22064. (save-excursion (goto-char (max (point-min) (1- (point))))
  22065. (outline-invisible-p)))
  22066. (org-show-context 'bookmark-jump)))
  22067. ;; Make session.el ignore our circular variable
  22068. (defvar session-globals-exclude)
  22069. (eval-after-load "session"
  22070. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22071. ;;;; Finish up
  22072. (provide 'org)
  22073. (run-hooks 'org-load-hook)
  22074. ;;; org.el ends here