org.el 906 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914
  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-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (setq exported-file
  183. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  184. (message "%s %s"
  185. (if compile
  186. (progn (byte-compile-file exported-file 'load)
  187. "Compiled and loaded")
  188. (progn (load-file exported-file) "Loaded"))
  189. exported-file)))
  190. (defcustom org-babel-load-languages '((emacs-lisp . t))
  191. "Languages which can be evaluated in Org-mode buffers.
  192. This list can be used to load support for any of the languages
  193. below, note that each language will depend on a different set of
  194. system executables and/or Emacs modes. When a language is
  195. \"loaded\", then code blocks in that language can be evaluated
  196. with `org-babel-execute-src-block' bound by default to C-c
  197. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  198. be set to remove code block evaluation from the C-c C-c
  199. keybinding. By default only Emacs Lisp (which has no
  200. requirements) is loaded."
  201. :group 'org-babel
  202. :set 'org-babel-do-load-languages
  203. :version "24.1"
  204. :type '(alist :tag "Babel Languages"
  205. :key-type
  206. (choice
  207. (const :tag "Awk" awk)
  208. (const :tag "C" C)
  209. (const :tag "R" R)
  210. (const :tag "Asymptote" asymptote)
  211. (const :tag "Calc" calc)
  212. (const :tag "Clojure" clojure)
  213. (const :tag "CSS" css)
  214. (const :tag "Ditaa" ditaa)
  215. (const :tag "Dot" dot)
  216. (const :tag "Emacs Lisp" emacs-lisp)
  217. (const :tag "Fortran" fortran)
  218. (const :tag "Gnuplot" gnuplot)
  219. (const :tag "Haskell" haskell)
  220. (const :tag "IO" io)
  221. (const :tag "Java" java)
  222. (const :tag "Javascript" js)
  223. (const :tag "LaTeX" latex)
  224. (const :tag "Ledger" ledger)
  225. (const :tag "Lilypond" lilypond)
  226. (const :tag "Lisp" lisp)
  227. (const :tag "Makefile" makefile)
  228. (const :tag "Maxima" maxima)
  229. (const :tag "Matlab" matlab)
  230. (const :tag "Mscgen" mscgen)
  231. (const :tag "Ocaml" ocaml)
  232. (const :tag "Octave" octave)
  233. (const :tag "Org" org)
  234. (const :tag "Perl" perl)
  235. (const :tag "Pico Lisp" picolisp)
  236. (const :tag "PlantUML" plantuml)
  237. (const :tag "Python" python)
  238. (const :tag "Ruby" ruby)
  239. (const :tag "Sass" sass)
  240. (const :tag "Scala" scala)
  241. (const :tag "Scheme" scheme)
  242. (const :tag "Screen" screen)
  243. (const :tag "Shell Script" sh)
  244. (const :tag "Shen" shen)
  245. (const :tag "Sql" sql)
  246. (const :tag "Sqlite" sqlite))
  247. :value-type (boolean :tag "Activate" :value t)))
  248. ;;;; Customization variables
  249. (defcustom org-clone-delete-id nil
  250. "Remove ID property of clones of a subtree.
  251. When non-nil, clones of a subtree don't inherit the ID property.
  252. Otherwise they inherit the ID property with a new unique
  253. identifier."
  254. :type 'boolean
  255. :version "24.1"
  256. :group 'org-id)
  257. ;;; Version
  258. (org-check-version)
  259. ;;;###autoload
  260. (defun org-version (&optional here full message)
  261. "Show the org-mode version in the echo area.
  262. With prefix argument HERE, insert it at point.
  263. When FULL is non-nil, use a verbose version string.
  264. When MESSAGE is non-nil, display a message with the version."
  265. (interactive "P")
  266. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  267. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  268. (load-suffixes (list ".el"))
  269. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  270. (org-trash (or
  271. (and (fboundp 'org-release) (fboundp 'org-git-version))
  272. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  273. (load-suffixes save-load-suffixes)
  274. (org-version (org-release))
  275. (git-version (org-git-version))
  276. (version (format "Org-mode version %s (%s @ %s)"
  277. org-version
  278. git-version
  279. (if org-install-dir
  280. (if (string= org-dir org-install-dir)
  281. org-install-dir
  282. (concat "mixed installation! " org-install-dir " and " org-dir))
  283. "org-loaddefs.el can not be found!")))
  284. (version1 (if full version org-version)))
  285. (if (org-called-interactively-p 'interactive)
  286. (if here
  287. (insert version)
  288. (message version))
  289. (if message (message version1))
  290. version1)))
  291. (defconst org-version (org-version))
  292. ;;; Compatibility constants
  293. ;;; The custom variables
  294. (defgroup org nil
  295. "Outline-based notes management and organizer."
  296. :tag "Org"
  297. :group 'outlines
  298. :group 'calendar)
  299. (defcustom org-mode-hook nil
  300. "Mode hook for Org-mode, run after the mode was turned on."
  301. :group 'org
  302. :type 'hook)
  303. (defcustom org-load-hook nil
  304. "Hook that is run after org.el has been loaded."
  305. :group 'org
  306. :type 'hook)
  307. (defcustom org-log-buffer-setup-hook nil
  308. "Hook that is run after an Org log buffer is created."
  309. :group 'org
  310. :version "24.1"
  311. :type 'hook)
  312. (defvar org-modules) ; defined below
  313. (defvar org-modules-loaded nil
  314. "Have the modules been loaded already?")
  315. (defun org-load-modules-maybe (&optional force)
  316. "Load all extensions listed in `org-modules'."
  317. (when (or force (not org-modules-loaded))
  318. (mapc (lambda (ext)
  319. (condition-case nil (require ext)
  320. (error (message "Problems while trying to load feature `%s'" ext))))
  321. org-modules)
  322. (setq org-modules-loaded t)))
  323. (defun org-set-modules (var value)
  324. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  325. (set var value)
  326. (when (featurep 'org)
  327. (org-load-modules-maybe 'force)))
  328. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  329. "Modules that should always be loaded together with org.el.
  330. If a description starts with <C>, the file is not part of Emacs
  331. and loading it will require that you have downloaded and properly
  332. installed the Org mode distribution.
  333. You can also use this system to load external packages (i.e. neither Org
  334. core modules, nor modules from the CONTRIB directory). Just add symbols
  335. to the end of the list. If the package is called org-xyz.el, then you need
  336. to add the symbol `xyz', and the package must have a call to:
  337. \(provide 'org-xyz)
  338. For export specific modules, see also `org-export-backends'."
  339. :group 'org
  340. :set 'org-set-modules
  341. :version "24.4"
  342. :package-version '(Org . "8.0")
  343. :type
  344. '(set :greedy t
  345. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  346. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  347. (const :tag " crypt: Encryption of subtrees" org-crypt)
  348. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  349. (const :tag " docview: Links to doc-view buffers" org-docview)
  350. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  351. (const :tag " habit: Track your consistency with habits" org-habit)
  352. (const :tag " id: Global IDs for identifying entries" org-id)
  353. (const :tag " info: Links to Info nodes" org-info)
  354. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  355. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  356. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  357. (const :tag " mouse: Additional mouse support" org-mouse)
  358. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  359. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  360. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  361. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  362. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  363. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  364. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  365. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  366. (const :tag "C collector: Collect properties into tables" org-collector)
  367. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  368. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  369. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  370. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  371. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  372. (const :tag "C eval: Include command output as text" org-eval)
  373. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  374. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  375. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  376. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  377. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  378. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  379. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  380. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  381. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export--registered-backends) ; From ox.el.
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (declare-function org-export-backend-name "ox" (backend))
  402. (defcustom org-export-backends '(ascii html icalendar latex)
  403. "List of export back-ends that should be always available.
  404. If a description starts with <C>, the file is not part of Emacs
  405. and loading it will require that you have downloaded and properly
  406. installed the Org mode distribution.
  407. Unlike to `org-modules', libraries in this list will not be
  408. loaded along with Org, but only once the export framework is
  409. needed.
  410. This variable needs to be set before org.el is loaded. If you
  411. need to make a change while Emacs is running, use the customize
  412. interface or run the following code, where VAL stands for the new
  413. value of the variable, after updating it:
  414. \(progn
  415. \(setq org-export--registered-backends
  416. \(org-remove-if-not
  417. \(lambda (backend)
  418. \(let ((name (org-export-backend-name backend)))
  419. \(or (memq name val)
  420. \(catch 'parentp
  421. \(dolist (b val)
  422. \(and (org-export-derived-backend-p b name)
  423. \(throw 'parentp t)))))))
  424. org-export--registered-backends))
  425. \(let ((new-list (mapcar 'org-export-backend-name
  426. org-export--registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default 'org-export-backends new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export--registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (let ((name (org-export-backend-name backend)))
  450. (or (memq name val)
  451. (catch 'parentp
  452. (dolist (b val)
  453. (and (org-export-derived-backend-p b name)
  454. (throw 'parentp t)))))))
  455. org-export--registered-backends))
  456. ;; Now build NEW-LIST of both new back-ends and required
  457. ;; parents.
  458. (let ((new-list (mapcar 'org-export-backend-name
  459. org-export--registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. If you want to control how Org exports those characters,
  592. see `org-export-with-sub-superscripts'.
  593. When this option is turned on, you can use TeX-like syntax for
  594. sub- and superscripts within the buffer. Several characters after
  595. \"_\" or \"^\" will be considered as a single item - so grouping
  596. with {} is normally not needed. For example, the following things
  597. will be parsed as single sub- or superscripts:
  598. 10^24 or 10^tau several digits will be considered 1 item.
  599. 10^-12 or 10^-tau a leading sign with digits or a word
  600. x^2-y^3 will be read as x^2 - y^3, because items are
  601. terminated by almost any nonword/nondigit char.
  602. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  603. Still, ambiguity is possible. So when in doubt, use {} to enclose
  604. the sub/superscript. If you set this variable to the symbol `{}',
  605. the braces are *required* in order to trigger interpretations as
  606. sub/superscript. This can be helpful in documents that need \"_\"
  607. frequently in plain text."
  608. :group 'org-startup
  609. :version "24.1"
  610. :type '(choice
  611. (const :tag "Always interpret" t)
  612. (const :tag "Only with braces" {})
  613. (const :tag "Never interpret" nil)))
  614. (defcustom org-startup-with-beamer-mode nil
  615. "Non-nil means turn on `org-beamer-mode' on startup.
  616. This can also be configured on a per-file basis by adding one of
  617. the following lines anywhere in the buffer:
  618. #+STARTUP: beamer"
  619. :group 'org-startup
  620. :version "24.1"
  621. :type 'boolean)
  622. (defcustom org-startup-align-all-tables nil
  623. "Non-nil means align all tables when visiting a file.
  624. This is useful when the column width in tables is forced with <N> cookies
  625. in table fields. Such tables will look correct only after the first re-align.
  626. This can also be configured on a per-file basis by adding one of
  627. the following lines anywhere in the buffer:
  628. #+STARTUP: align
  629. #+STARTUP: noalign"
  630. :group 'org-startup
  631. :type 'boolean)
  632. (defcustom org-startup-with-inline-images nil
  633. "Non-nil means show inline images when loading a new Org file.
  634. This can also be configured on a per-file basis by adding one of
  635. the following lines anywhere in the buffer:
  636. #+STARTUP: inlineimages
  637. #+STARTUP: noinlineimages"
  638. :group 'org-startup
  639. :version "24.1"
  640. :type 'boolean)
  641. (defcustom org-startup-with-latex-preview nil
  642. "Non-nil means preview LaTeX fragments when loading a new Org file.
  643. This can also be configured on a per-file basis by adding one of
  644. the followinglines anywhere in the buffer:
  645. #+STARTUP: latexpreview
  646. #+STARTUP: nolatexpreview"
  647. :group 'org-startup
  648. :version "24.4"
  649. :package-version '(Org . "8.0")
  650. :type 'boolean)
  651. (defcustom org-insert-mode-line-in-empty-file nil
  652. "Non-nil means insert the first line setting Org-mode in empty files.
  653. When the function `org-mode' is called interactively in an empty file, this
  654. normally means that the file name does not automatically trigger Org-mode.
  655. To ensure that the file will always be in Org-mode in the future, a
  656. line enforcing Org-mode will be inserted into the buffer, if this option
  657. has been set."
  658. :group 'org-startup
  659. :type 'boolean)
  660. (defcustom org-replace-disputed-keys nil
  661. "Non-nil means use alternative key bindings for some keys.
  662. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  663. These keys are also used by other packages like shift-selection-mode'
  664. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  665. If you want to use Org-mode together with one of these other modes,
  666. or more generally if you would like to move some Org-mode commands to
  667. other keys, set this variable and configure the keys with the variable
  668. `org-disputed-keys'.
  669. This option is only relevant at load-time of Org-mode, and must be set
  670. *before* org.el is loaded. Changing it requires a restart of Emacs to
  671. become effective."
  672. :group 'org-startup
  673. :type 'boolean)
  674. (defcustom org-use-extra-keys nil
  675. "Non-nil means use extra key sequence definitions for certain commands.
  676. This happens automatically if you run XEmacs or if `window-system'
  677. is nil. This variable lets you do the same manually. You must
  678. set it before loading org.
  679. Example: on Carbon Emacs 22 running graphically, with an external
  680. keyboard on a Powerbook, the default way of setting M-left might
  681. not work for either Alt or ESC. Setting this variable will make
  682. it work for ESC."
  683. :group 'org-startup
  684. :type 'boolean)
  685. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  686. (defcustom org-disputed-keys
  687. '(([(shift up)] . [(meta p)])
  688. ([(shift down)] . [(meta n)])
  689. ([(shift left)] . [(meta -)])
  690. ([(shift right)] . [(meta +)])
  691. ([(control shift right)] . [(meta shift +)])
  692. ([(control shift left)] . [(meta shift -)]))
  693. "Keys for which Org-mode and other modes compete.
  694. This is an alist, cars are the default keys, second element specifies
  695. the alternative to use when `org-replace-disputed-keys' is t.
  696. Keys can be specified in any syntax supported by `define-key'.
  697. The value of this option takes effect only at Org-mode's startup,
  698. therefore you'll have to restart Emacs to apply it after changing."
  699. :group 'org-startup
  700. :type 'alist)
  701. (defun org-key (key)
  702. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  703. Or return the original if not disputed.
  704. Also apply the translations defined in `org-xemacs-key-equivalents'."
  705. (when org-replace-disputed-keys
  706. (let* ((nkey (key-description key))
  707. (x (org-find-if (lambda (x)
  708. (equal (key-description (car x)) nkey))
  709. org-disputed-keys)))
  710. (setq key (if x (cdr x) key))))
  711. (when (featurep 'xemacs)
  712. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  713. key)
  714. (defun org-find-if (predicate seq)
  715. (catch 'exit
  716. (while seq
  717. (if (funcall predicate (car seq))
  718. (throw 'exit (car seq))
  719. (pop seq)))))
  720. (defun org-defkey (keymap key def)
  721. "Define a key, possibly translated, as returned by `org-key'."
  722. (define-key keymap (org-key key) def))
  723. (defcustom org-ellipsis nil
  724. "The ellipsis to use in the Org-mode outline.
  725. When nil, just use the standard three dots. When a string, use that instead,
  726. When a face, use the standard 3 dots, but with the specified face.
  727. The change affects only Org-mode (which will then use its own display table).
  728. Changing this requires executing `M-x org-mode' in a buffer to become
  729. effective."
  730. :group 'org-startup
  731. :type '(choice (const :tag "Default" nil)
  732. (face :tag "Face" :value org-warning)
  733. (string :tag "String" :value "...#")))
  734. (defvar org-display-table nil
  735. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  736. (defgroup org-keywords nil
  737. "Keywords in Org-mode."
  738. :tag "Org Keywords"
  739. :group 'org)
  740. (defcustom org-deadline-string "DEADLINE:"
  741. "String to mark deadline entries.
  742. A deadline is this string, followed by a time stamp. Should be a word,
  743. terminated by a colon. You can insert a schedule keyword and
  744. a timestamp with \\[org-deadline].
  745. Changes become only effective after restarting Emacs."
  746. :group 'org-keywords
  747. :type 'string)
  748. (defcustom org-scheduled-string "SCHEDULED:"
  749. "String to mark scheduled TODO entries.
  750. A schedule is this string, followed by a time stamp. Should be a word,
  751. terminated by a colon. You can insert a schedule keyword and
  752. a timestamp with \\[org-schedule].
  753. Changes become only effective after restarting Emacs."
  754. :group 'org-keywords
  755. :type 'string)
  756. (defcustom org-closed-string "CLOSED:"
  757. "String used as the prefix for timestamps logging closing a TODO entry."
  758. :group 'org-keywords
  759. :type 'string)
  760. (defcustom org-clock-string "CLOCK:"
  761. "String used as prefix for timestamps clocking work hours on an item."
  762. :group 'org-keywords
  763. :type 'string)
  764. (defcustom org-closed-keep-when-no-todo nil
  765. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  766. :group 'org-todo
  767. :group 'org-keywords
  768. :version "24.4"
  769. :package-version '(Org . "8.0")
  770. :type 'boolean)
  771. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  772. org-scheduled-string "\\|"
  773. org-deadline-string "\\|"
  774. org-closed-string "\\|"
  775. org-clock-string "\\)")
  776. "Matches a line with planning or clock info.")
  777. (defcustom org-comment-string "COMMENT"
  778. "Entries starting with this keyword will never be exported.
  779. An entry can be toggled between COMMENT and normal with
  780. \\[org-toggle-comment].
  781. Changes become only effective after restarting Emacs."
  782. :group 'org-keywords
  783. :type 'string)
  784. (defcustom org-quote-string "QUOTE"
  785. "Entries starting with this keyword will be exported in fixed-width font.
  786. Quoting applies only to the text in the entry following the headline, and does
  787. not extend beyond the next headline, even if that is lower level.
  788. An entry can be toggled between QUOTE and normal with
  789. \\[org-toggle-fixed-width-section]."
  790. :group 'org-keywords
  791. :type 'string)
  792. (defconst org-repeat-re
  793. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  794. "Regular expression for specifying repeated events.
  795. After a match, group 1 contains the repeat expression.")
  796. (defgroup org-structure nil
  797. "Options concerning the general structure of Org-mode files."
  798. :tag "Org Structure"
  799. :group 'org)
  800. (defgroup org-reveal-location nil
  801. "Options about how to make context of a location visible."
  802. :tag "Org Reveal Location"
  803. :group 'org-structure)
  804. (defconst org-context-choice
  805. '(choice
  806. (const :tag "Always" t)
  807. (const :tag "Never" nil)
  808. (repeat :greedy t :tag "Individual contexts"
  809. (cons
  810. (choice :tag "Context"
  811. (const agenda)
  812. (const org-goto)
  813. (const occur-tree)
  814. (const tags-tree)
  815. (const link-search)
  816. (const mark-goto)
  817. (const bookmark-jump)
  818. (const isearch)
  819. (const default))
  820. (boolean))))
  821. "Contexts for the reveal options.")
  822. (defcustom org-show-hierarchy-above '((default . t))
  823. "Non-nil means show full hierarchy when revealing a location.
  824. Org-mode often shows locations in an org-mode file which might have
  825. been invisible before. When this is set, the hierarchy of headings
  826. above the exposed location is shown.
  827. Turning this off for example for sparse trees makes them very compact.
  828. Instead of t, this can also be an alist specifying this option for different
  829. contexts. Valid contexts are
  830. agenda when exposing an entry from the agenda
  831. org-goto when using the command `org-goto' on key C-c C-j
  832. occur-tree when using the command `org-occur' on key C-c /
  833. tags-tree when constructing a sparse tree based on tags matches
  834. link-search when exposing search matches associated with a link
  835. mark-goto when exposing the jump goal of a mark
  836. bookmark-jump when exposing a bookmark location
  837. isearch when exiting from an incremental search
  838. default default for all contexts not set explicitly"
  839. :group 'org-reveal-location
  840. :type org-context-choice)
  841. (defcustom org-show-following-heading '((default . nil))
  842. "Non-nil means show following heading when revealing a location.
  843. Org-mode often shows locations in an org-mode file which might have
  844. been invisible before. When this is set, the heading following the
  845. match is shown.
  846. Turning this off for example for sparse trees makes them very compact,
  847. but makes it harder to edit the location of the match. In such a case,
  848. use the command \\[org-reveal] to show more context.
  849. Instead of t, this can also be an alist specifying this option for different
  850. contexts. See `org-show-hierarchy-above' for valid contexts."
  851. :group 'org-reveal-location
  852. :type org-context-choice)
  853. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  854. "Non-nil means show all sibling heading when revealing a location.
  855. Org-mode often shows locations in an org-mode file which might have
  856. been invisible before. When this is set, the sibling of the current entry
  857. heading are all made visible. If `org-show-hierarchy-above' is t,
  858. the same happens on each level of the hierarchy above the current entry.
  859. By default this is on for the isearch context, off for all other contexts.
  860. Turning this off for example for sparse trees makes them very compact,
  861. but makes it harder to edit the location of the match. In such a case,
  862. use the command \\[org-reveal] to show more context.
  863. Instead of t, this can also be an alist specifying this option for different
  864. contexts. See `org-show-hierarchy-above' for valid contexts."
  865. :group 'org-reveal-location
  866. :type org-context-choice
  867. :version "24.4"
  868. :package-version '(Org . "8.0"))
  869. (defcustom org-show-entry-below '((default . nil))
  870. "Non-nil means show the entry below a headline when revealing a location.
  871. Org-mode often shows locations in an org-mode file which might have
  872. been invisible before. When this is set, the text below the headline that is
  873. exposed is also shown.
  874. By default this is off for all contexts.
  875. Instead of t, this can also be an alist specifying this option for different
  876. contexts. See `org-show-hierarchy-above' for valid contexts."
  877. :group 'org-reveal-location
  878. :type org-context-choice)
  879. (defcustom org-indirect-buffer-display 'other-window
  880. "How should indirect tree buffers be displayed?
  881. This applies to indirect buffers created with the commands
  882. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  883. Valid values are:
  884. current-window Display in the current window
  885. other-window Just display in another window.
  886. dedicated-frame Create one new frame, and re-use it each time.
  887. new-frame Make a new frame each time. Note that in this case
  888. previously-made indirect buffers are kept, and you need to
  889. kill these buffers yourself."
  890. :group 'org-structure
  891. :group 'org-agenda-windows
  892. :type '(choice
  893. (const :tag "In current window" current-window)
  894. (const :tag "In current frame, other window" other-window)
  895. (const :tag "Each time a new frame" new-frame)
  896. (const :tag "One dedicated frame" dedicated-frame)))
  897. (defcustom org-use-speed-commands nil
  898. "Non-nil means activate single letter commands at beginning of a headline.
  899. This may also be a function to test for appropriate locations where speed
  900. commands should be active."
  901. :group 'org-structure
  902. :type '(choice
  903. (const :tag "Never" nil)
  904. (const :tag "At beginning of headline stars" t)
  905. (function)))
  906. (defcustom org-speed-commands-user nil
  907. "Alist of additional speed commands.
  908. This list will be checked before `org-speed-commands-default'
  909. when the variable `org-use-speed-commands' is non-nil
  910. and when the cursor is at the beginning of a headline.
  911. The car if each entry is a string with a single letter, which must
  912. be assigned to `self-insert-command' in the global map.
  913. The cdr is either a command to be called interactively, a function
  914. to be called, or a form to be evaluated.
  915. An entry that is just a list with a single string will be interpreted
  916. as a descriptive headline that will be added when listing the speed
  917. commands in the Help buffer using the `?' speed command."
  918. :group 'org-structure
  919. :type '(repeat :value ("k" . ignore)
  920. (choice :value ("k" . ignore)
  921. (list :tag "Descriptive Headline" (string :tag "Headline"))
  922. (cons :tag "Letter and Command"
  923. (string :tag "Command letter")
  924. (choice
  925. (function)
  926. (sexp))))))
  927. (defcustom org-bookmark-names-plist
  928. '(:last-capture "org-capture-last-stored"
  929. :last-refile "org-refile-last-stored"
  930. :last-capture-marker "org-capture-last-stored-marker")
  931. "Names for bookmarks automatically set by some Org commands.
  932. This can provide strings as names for a number of bookmakrs Org sets
  933. automatically. The following keys are currently implemented:
  934. :last-capture
  935. :last-capture-marker
  936. :last-refile
  937. When a key does not show up in the property list, the corresponding bookmark
  938. is not set."
  939. :group 'org-structure
  940. :type 'plist)
  941. (defgroup org-cycle nil
  942. "Options concerning visibility cycling in Org-mode."
  943. :tag "Org Cycle"
  944. :group 'org-structure)
  945. (defcustom org-cycle-skip-children-state-if-no-children t
  946. "Non-nil means skip CHILDREN state in entries that don't have any."
  947. :group 'org-cycle
  948. :type 'boolean)
  949. (defcustom org-cycle-max-level nil
  950. "Maximum level which should still be subject to visibility cycling.
  951. Levels higher than this will, for cycling, be treated as text, not a headline.
  952. When `org-odd-levels-only' is set, a value of N in this variable actually
  953. means 2N-1 stars as the limiting headline.
  954. When nil, cycle all levels.
  955. Note that the limiting level of cycling is also influenced by
  956. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  957. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  958. than its value."
  959. :group 'org-cycle
  960. :type '(choice
  961. (const :tag "No limit" nil)
  962. (integer :tag "Maximum level")))
  963. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  964. "Names of drawers. Drawers are not opened by cycling on the headline above.
  965. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  966. this:
  967. :DRAWERNAME:
  968. .....
  969. :END:
  970. The drawer \"PROPERTIES\" is special for capturing properties through
  971. the property API.
  972. Drawers can be defined on the per-file basis with a line like:
  973. #+DRAWERS: HIDDEN STATE PROPERTIES"
  974. :group 'org-structure
  975. :group 'org-cycle
  976. :type '(repeat (string :tag "Drawer Name")))
  977. (defcustom org-hide-block-startup nil
  978. "Non-nil means entering Org-mode will fold all blocks.
  979. This can also be set in on a per-file basis with
  980. #+STARTUP: hideblocks
  981. #+STARTUP: showblocks"
  982. :group 'org-startup
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-global-at-bob nil
  986. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  987. This makes it possible to do global cycling without having to use S-TAB or
  988. \\[universal-argument] TAB. For this special case to work, the first line
  989. of the buffer must not be a headline -- it may be empty or some other text.
  990. When used in this way, `org-cycle-hook' is disabled temporarily to make
  991. sure the cursor stays at the beginning of the buffer. When this option is
  992. nil, don't do anything special at the beginning of the buffer."
  993. :group 'org-cycle
  994. :type 'boolean)
  995. (defcustom org-cycle-level-after-item/entry-creation t
  996. "Non-nil means cycle entry level or item indentation in new empty entries.
  997. When the cursor is at the end of an empty headline, i.e., with only stars
  998. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  999. and then all possible ancestor states, before returning to the original state.
  1000. This makes data entry extremely fast: M-RET to create a new headline,
  1001. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1002. When the cursor is at the end of an empty plain list item, one TAB will
  1003. make it a subitem, two or more tabs will back up to make this an item
  1004. higher up in the item hierarchy."
  1005. :group 'org-cycle
  1006. :type 'boolean)
  1007. (defcustom org-cycle-emulate-tab t
  1008. "Where should `org-cycle' emulate TAB.
  1009. nil Never
  1010. white Only in completely white lines
  1011. whitestart Only at the beginning of lines, before the first non-white char
  1012. t Everywhere except in headlines
  1013. exc-hl-bol Everywhere except at the start of a headline
  1014. If TAB is used in a place where it does not emulate TAB, the current subtree
  1015. visibility is cycled."
  1016. :group 'org-cycle
  1017. :type '(choice (const :tag "Never" nil)
  1018. (const :tag "Only in completely white lines" white)
  1019. (const :tag "Before first char in a line" whitestart)
  1020. (const :tag "Everywhere except in headlines" t)
  1021. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1022. (defcustom org-cycle-separator-lines 2
  1023. "Number of empty lines needed to keep an empty line between collapsed trees.
  1024. If you leave an empty line between the end of a subtree and the following
  1025. headline, this empty line is hidden when the subtree is folded.
  1026. Org-mode will leave (exactly) one empty line visible if the number of
  1027. empty lines is equal or larger to the number given in this variable.
  1028. So the default 2 means at least 2 empty lines after the end of a subtree
  1029. are needed to produce free space between a collapsed subtree and the
  1030. following headline.
  1031. If the number is negative, and the number of empty lines is at least -N,
  1032. all empty lines are shown.
  1033. Special case: when 0, never leave empty lines in collapsed view."
  1034. :group 'org-cycle
  1035. :type 'integer)
  1036. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1037. (defcustom org-pre-cycle-hook nil
  1038. "Hook that is run before visibility cycling is happening.
  1039. The function(s) in this hook must accept a single argument which indicates
  1040. the new state that will be set right after running this hook. The
  1041. argument is a symbol. Before a global state change, it can have the values
  1042. `overview', `content', or `all'. Before a local state change, it can have
  1043. the values `folded', `children', or `subtree'."
  1044. :group 'org-cycle
  1045. :type 'hook)
  1046. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1047. org-cycle-hide-drawers
  1048. org-cycle-hide-inline-tasks
  1049. org-cycle-show-empty-lines
  1050. org-optimize-window-after-visibility-change)
  1051. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1052. The function(s) in this hook must accept a single argument which indicates
  1053. the new state that was set by the most recent `org-cycle' command. The
  1054. argument is a symbol. After a global state change, it can have the values
  1055. `overview', `contents', or `all'. After a local state change, it can have
  1056. the values `folded', `children', or `subtree'."
  1057. :group 'org-cycle
  1058. :type 'hook)
  1059. (defgroup org-edit-structure nil
  1060. "Options concerning structure editing in Org-mode."
  1061. :tag "Org Edit Structure"
  1062. :group 'org-structure)
  1063. (defcustom org-odd-levels-only nil
  1064. "Non-nil means skip even levels and only use odd levels for the outline.
  1065. This has the effect that two stars are being added/taken away in
  1066. promotion/demotion commands. It also influences how levels are
  1067. handled by the exporters.
  1068. Changing it requires restart of `font-lock-mode' to become effective
  1069. for fontification also in regions already fontified.
  1070. You may also set this on a per-file basis by adding one of the following
  1071. lines to the buffer:
  1072. #+STARTUP: odd
  1073. #+STARTUP: oddeven"
  1074. :group 'org-edit-structure
  1075. :group 'org-appearance
  1076. :type 'boolean)
  1077. (defcustom org-adapt-indentation t
  1078. "Non-nil means adapt indentation to outline node level.
  1079. When this variable is set, Org assumes that you write outlines by
  1080. indenting text in each node to align with the headline (after the stars).
  1081. The following issues are influenced by this variable:
  1082. - When this is set and the *entire* text in an entry is indented, the
  1083. indentation is increased by one space in a demotion command, and
  1084. decreased by one in a promotion command. If any line in the entry
  1085. body starts with text at column 0, indentation is not changed at all.
  1086. - Property drawers and planning information is inserted indented when
  1087. this variable s set. When nil, they will not be indented.
  1088. - TAB indents a line relative to context. The lines below a headline
  1089. will be indented when this variable is set.
  1090. Note that this is all about true indentation, by adding and removing
  1091. space characters. See also `org-indent.el' which does level-dependent
  1092. indentation in a virtual way, i.e. at display time in Emacs."
  1093. :group 'org-edit-structure
  1094. :type 'boolean)
  1095. (defcustom org-special-ctrl-a/e nil
  1096. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1097. When t, `C-a' will bring back the cursor to the beginning of the
  1098. headline text, i.e. after the stars and after a possible TODO
  1099. keyword. In an item, this will be the position after bullet and
  1100. check-box, if any. When the cursor is already at that position,
  1101. another `C-a' will bring it to the beginning of the line.
  1102. `C-e' will jump to the end of the headline, ignoring the presence
  1103. of tags in the headline. A second `C-e' will then jump to the
  1104. true end of the line, after any tags. This also means that, when
  1105. this variable is non-nil, `C-e' also will never jump beyond the
  1106. end of the heading of a folded section, i.e. not after the
  1107. ellipses.
  1108. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1109. normally, going to the true line boundary first. Only a directly
  1110. following, identical keypress will bring the cursor to the
  1111. special positions.
  1112. This may also be a cons cell where the behavior for `C-a' and
  1113. `C-e' is set separately."
  1114. :group 'org-edit-structure
  1115. :type '(choice
  1116. (const :tag "off" nil)
  1117. (const :tag "on: after stars/bullet and before tags first" t)
  1118. (const :tag "reversed: true line boundary first" reversed)
  1119. (cons :tag "Set C-a and C-e separately"
  1120. (choice :tag "Special C-a"
  1121. (const :tag "off" nil)
  1122. (const :tag "on: after stars/bullet first" t)
  1123. (const :tag "reversed: before stars/bullet first" reversed))
  1124. (choice :tag "Special C-e"
  1125. (const :tag "off" nil)
  1126. (const :tag "on: before tags first" t)
  1127. (const :tag "reversed: after tags first" reversed)))))
  1128. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1129. (defcustom org-special-ctrl-k nil
  1130. "Non-nil means `C-k' will behave specially in headlines.
  1131. When nil, `C-k' will call the default `kill-line' command.
  1132. When t, the following will happen while the cursor is in the headline:
  1133. - When the cursor is at the beginning of a headline, kill the entire
  1134. line and possible the folded subtree below the line.
  1135. - When in the middle of the headline text, kill the headline up to the tags.
  1136. - When after the headline text, kill the tags."
  1137. :group 'org-edit-structure
  1138. :type 'boolean)
  1139. (defcustom org-ctrl-k-protect-subtree nil
  1140. "Non-nil means, do not delete a hidden subtree with C-k.
  1141. When set to the symbol `error', simply throw an error when C-k is
  1142. used to kill (part-of) a headline that has hidden text behind it.
  1143. Any other non-nil value will result in a query to the user, if it is
  1144. OK to kill that hidden subtree. When nil, kill without remorse."
  1145. :group 'org-edit-structure
  1146. :version "24.1"
  1147. :type '(choice
  1148. (const :tag "Do not protect hidden subtrees" nil)
  1149. (const :tag "Protect hidden subtrees with a security query" t)
  1150. (const :tag "Never kill a hidden subtree with C-k" error)))
  1151. (defcustom org-special-ctrl-o t
  1152. "Non-nil means, make `C-o' insert a row in tables."
  1153. :group 'org-edit-structure
  1154. :type 'boolean)
  1155. (defcustom org-catch-invisible-edits nil
  1156. "Check if in invisible region before inserting or deleting a character.
  1157. Valid values are:
  1158. nil Do not check, so just do invisible edits.
  1159. error Throw an error and do nothing.
  1160. show Make point visible, and do the requested edit.
  1161. show-and-error Make point visible, then throw an error and abort the edit.
  1162. smart Make point visible, and do insertion/deletion if it is
  1163. adjacent to visible text and the change feels predictable.
  1164. Never delete a previously invisible character or add in the
  1165. middle or right after an invisible region. Basically, this
  1166. allows insertion and backward-delete right before ellipses.
  1167. FIXME: maybe in this case we should not even show?"
  1168. :group 'org-edit-structure
  1169. :version "24.1"
  1170. :type '(choice
  1171. (const :tag "Do not check" nil)
  1172. (const :tag "Throw error when trying to edit" error)
  1173. (const :tag "Unhide, but do not do the edit" show-and-error)
  1174. (const :tag "Show invisible part and do the edit" show)
  1175. (const :tag "Be smart and do the right thing" smart)))
  1176. (defcustom org-yank-folded-subtrees t
  1177. "Non-nil means when yanking subtrees, fold them.
  1178. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1179. it starts with a heading and all other headings in it are either children
  1180. or siblings, then fold all the subtrees. However, do this only if no
  1181. text after the yank would be swallowed into a folded tree by this action."
  1182. :group 'org-edit-structure
  1183. :type 'boolean)
  1184. (defcustom org-yank-adjusted-subtrees nil
  1185. "Non-nil means when yanking subtrees, adjust the level.
  1186. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1187. this function for details."
  1188. :group 'org-edit-structure
  1189. :type 'boolean)
  1190. (defcustom org-M-RET-may-split-line '((default . t))
  1191. "Non-nil means M-RET will split the line at the cursor position.
  1192. When nil, it will go to the end of the line before making a
  1193. new line.
  1194. You may also set this option in a different way for different
  1195. contexts. Valid contexts are:
  1196. headline when creating a new headline
  1197. item when creating a new item
  1198. table in a table field
  1199. default the value to be used for all contexts not explicitly
  1200. customized"
  1201. :group 'org-structure
  1202. :group 'org-table
  1203. :type '(choice
  1204. (const :tag "Always" t)
  1205. (const :tag "Never" nil)
  1206. (repeat :greedy t :tag "Individual contexts"
  1207. (cons
  1208. (choice :tag "Context"
  1209. (const headline)
  1210. (const item)
  1211. (const table)
  1212. (const default))
  1213. (boolean)))))
  1214. (defcustom org-insert-heading-respect-content nil
  1215. "Non-nil means insert new headings after the current subtree.
  1216. When nil, the new heading is created directly after the current line.
  1217. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1218. this variable on for the duration of the command."
  1219. :group 'org-structure
  1220. :type 'boolean)
  1221. (defcustom org-blank-before-new-entry '((heading . auto)
  1222. (plain-list-item . auto))
  1223. "Should `org-insert-heading' leave a blank line before new heading/item?
  1224. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1225. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1226. which case Org will look at the surrounding headings/items and try to
  1227. make an intelligent decision whether to insert a blank line or not.
  1228. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1229. the setting here is ignored and no empty line is inserted to avoid breaking
  1230. the list structure."
  1231. :group 'org-edit-structure
  1232. :type '(list
  1233. (cons (const heading)
  1234. (choice (const :tag "Never" nil)
  1235. (const :tag "Always" t)
  1236. (const :tag "Auto" auto)))
  1237. (cons (const plain-list-item)
  1238. (choice (const :tag "Never" nil)
  1239. (const :tag "Always" t)
  1240. (const :tag "Auto" auto)))))
  1241. (defcustom org-insert-heading-hook nil
  1242. "Hook being run after inserting a new heading."
  1243. :group 'org-edit-structure
  1244. :type 'hook)
  1245. (defcustom org-enable-fixed-width-editor t
  1246. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1247. This currently only means they are never auto-wrapped.
  1248. When nil, such lines will be treated like ordinary lines.
  1249. See also the QUOTE keyword."
  1250. :group 'org-edit-structure
  1251. :type 'boolean)
  1252. (defcustom org-goto-auto-isearch t
  1253. "Non-nil means typing characters in `org-goto' starts incremental search.
  1254. When nil, you can use these keybindings to navigate the buffer:
  1255. q Quit the org-goto interface
  1256. n Go to the next visible heading
  1257. p Go to the previous visible heading
  1258. f Go one heading forward on same level
  1259. b Go one heading backward on same level
  1260. u Go one heading up"
  1261. :group 'org-edit-structure
  1262. :type 'boolean)
  1263. (defgroup org-sparse-trees nil
  1264. "Options concerning sparse trees in Org-mode."
  1265. :tag "Org Sparse Trees"
  1266. :group 'org-structure)
  1267. (defcustom org-highlight-sparse-tree-matches t
  1268. "Non-nil means highlight all matches that define a sparse tree.
  1269. The highlights will automatically disappear the next time the buffer is
  1270. changed by an edit command."
  1271. :group 'org-sparse-trees
  1272. :type 'boolean)
  1273. (defcustom org-remove-highlights-with-change t
  1274. "Non-nil means any change to the buffer will remove temporary highlights.
  1275. Such highlights are created by `org-occur' and `org-clock-display'.
  1276. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1277. The highlights created by `org-preview-latex-fragment' always need
  1278. `C-c C-c' to be removed."
  1279. :group 'org-sparse-trees
  1280. :group 'org-time
  1281. :type 'boolean)
  1282. (defcustom org-occur-hook '(org-first-headline-recenter)
  1283. "Hook that is run after `org-occur' has constructed a sparse tree.
  1284. This can be used to recenter the window to show as much of the structure
  1285. as possible."
  1286. :group 'org-sparse-trees
  1287. :type 'hook)
  1288. (defgroup org-imenu-and-speedbar nil
  1289. "Options concerning imenu and speedbar in Org-mode."
  1290. :tag "Org Imenu and Speedbar"
  1291. :group 'org-structure)
  1292. (defcustom org-imenu-depth 2
  1293. "The maximum level for Imenu access to Org-mode headlines.
  1294. This also applied for speedbar access."
  1295. :group 'org-imenu-and-speedbar
  1296. :type 'integer)
  1297. (defgroup org-table nil
  1298. "Options concerning tables in Org-mode."
  1299. :tag "Org Table"
  1300. :group 'org)
  1301. (defcustom org-enable-table-editor 'optimized
  1302. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1303. When nil, such lines will be treated like ordinary lines.
  1304. When equal to the symbol `optimized', the table editor will be optimized to
  1305. do the following:
  1306. - Automatic overwrite mode in front of whitespace in table fields.
  1307. This makes the structure of the table stay in tact as long as the edited
  1308. field does not exceed the column width.
  1309. - Minimize the number of realigns. Normally, the table is aligned each time
  1310. TAB or RET are pressed to move to another field. With optimization this
  1311. happens only if changes to a field might have changed the column width.
  1312. Optimization requires replacing the functions `self-insert-command',
  1313. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1314. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1315. very good at guessing when a re-align will be necessary, but you can always
  1316. force one with \\[org-ctrl-c-ctrl-c].
  1317. If you would like to use the optimized version in Org-mode, but the
  1318. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1319. This variable can be used to turn on and off the table editor during a session,
  1320. but in order to toggle optimization, a restart is required.
  1321. See also the variable `org-table-auto-blank-field'."
  1322. :group 'org-table
  1323. :type '(choice
  1324. (const :tag "off" nil)
  1325. (const :tag "on" t)
  1326. (const :tag "on, optimized" optimized)))
  1327. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1328. (version<= emacs-version "24.1"))
  1329. "Non-nil means cluster self-insert commands for undo when possible.
  1330. If this is set, then, like in the Emacs command loop, 20 consecutive
  1331. characters will be undone together.
  1332. This is configurable, because there is some impact on typing performance."
  1333. :group 'org-table
  1334. :type 'boolean)
  1335. (defcustom org-table-tab-recognizes-table.el t
  1336. "Non-nil means TAB will automatically notice a table.el table.
  1337. When it sees such a table, it moves point into it and - if necessary -
  1338. calls `table-recognize-table'."
  1339. :group 'org-table-editing
  1340. :type 'boolean)
  1341. (defgroup org-link nil
  1342. "Options concerning links in Org-mode."
  1343. :tag "Org Link"
  1344. :group 'org)
  1345. (defvar org-link-abbrev-alist-local nil
  1346. "Buffer-local version of `org-link-abbrev-alist', which see.
  1347. The value of this is taken from the #+LINK lines.")
  1348. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1349. (defcustom org-link-abbrev-alist nil
  1350. "Alist of link abbreviations.
  1351. The car of each element is a string, to be replaced at the start of a link.
  1352. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1353. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1354. [[linkkey:tag][description]]
  1355. The 'linkkey' must be a word word, starting with a letter, followed
  1356. by letters, numbers, '-' or '_'.
  1357. If REPLACE is a string, the tag will simply be appended to create the link.
  1358. If the string contains \"%s\", the tag will be inserted there. If the string
  1359. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1360. at that point (see the function `url-hexify-string'). If the string contains
  1361. the specifier \"%(my-function)\", then the custom function `my-function' will
  1362. be invoked: this function takes the tag as its only argument and must return
  1363. a string.
  1364. REPLACE may also be a function that will be called with the tag as the
  1365. only argument to create the link, which should be returned as a string.
  1366. See the manual for examples."
  1367. :group 'org-link
  1368. :type '(repeat
  1369. (cons
  1370. (string :tag "Protocol")
  1371. (choice
  1372. (string :tag "Format")
  1373. (function)))))
  1374. (defcustom org-descriptive-links t
  1375. "Non-nil means Org will display descriptive links.
  1376. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1377. \"Org Website\", hiding the link itself and just displaying its
  1378. description. When set to `nil', Org will display the full links
  1379. literally.
  1380. You can interactively set the value of this variable by calling
  1381. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1382. :group 'org-link
  1383. :type 'boolean)
  1384. (defcustom org-link-file-path-type 'adaptive
  1385. "How the path name in file links should be stored.
  1386. Valid values are:
  1387. relative Relative to the current directory, i.e. the directory of the file
  1388. into which the link is being inserted.
  1389. absolute Absolute path, if possible with ~ for home directory.
  1390. noabbrev Absolute path, no abbreviation of home directory.
  1391. adaptive Use relative path for files in the current directory and sub-
  1392. directories of it. For other files, use an absolute path."
  1393. :group 'org-link
  1394. :type '(choice
  1395. (const relative)
  1396. (const absolute)
  1397. (const noabbrev)
  1398. (const adaptive)))
  1399. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1400. "Types of links that should be activated in Org-mode files.
  1401. This is a list of symbols, each leading to the activation of a certain link
  1402. type. In principle, it does not hurt to turn on most link types - there may
  1403. be a small gain when turning off unused link types. The types are:
  1404. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1405. angle Links in angular brackets that may contain whitespace like
  1406. <bbdb:Carsten Dominik>.
  1407. plain Plain links in normal text, no whitespace, like http://google.com.
  1408. radio Text that is matched by a radio target, see manual for details.
  1409. tag Tag settings in a headline (link to tag search).
  1410. date Time stamps (link to calendar).
  1411. footnote Footnote labels.
  1412. Changing this variable requires a restart of Emacs to become effective."
  1413. :group 'org-link
  1414. :type '(set :greedy t
  1415. (const :tag "Double bracket links" bracket)
  1416. (const :tag "Angular bracket links" angle)
  1417. (const :tag "Plain text links" plain)
  1418. (const :tag "Radio target matches" radio)
  1419. (const :tag "Tags" tag)
  1420. (const :tag "Timestamps" date)
  1421. (const :tag "Footnotes" footnote)))
  1422. (defcustom org-make-link-description-function nil
  1423. "Function to use for generating link descriptions from links.
  1424. When nil, the link location will be used. This function must take
  1425. two parameters: the first one is the link, the second one is the
  1426. description generated by `org-insert-link'. The function should
  1427. return the description to use."
  1428. :group 'org-link
  1429. :type '(choice (const nil) (function)))
  1430. (defgroup org-link-store nil
  1431. "Options concerning storing links in Org-mode."
  1432. :tag "Org Store Link"
  1433. :group 'org-link)
  1434. (defcustom org-url-hexify-p t
  1435. "When non-nil, hexify URL when creating a link."
  1436. :type 'boolean
  1437. :version "24.3"
  1438. :group 'org-link-store)
  1439. (defcustom org-email-link-description-format "Email %c: %.30s"
  1440. "Format of the description part of a link to an email or usenet message.
  1441. The following %-escapes will be replaced by corresponding information:
  1442. %F full \"From\" field
  1443. %f name, taken from \"From\" field, address if no name
  1444. %T full \"To\" field
  1445. %t first name in \"To\" field, address if no name
  1446. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1447. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1448. %s subject
  1449. %d date
  1450. %m message-id.
  1451. You may use normal field width specification between the % and the letter.
  1452. This is for example useful to limit the length of the subject.
  1453. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1454. :group 'org-link-store
  1455. :type 'string)
  1456. (defcustom org-from-is-user-regexp
  1457. (let (r1 r2)
  1458. (when (and user-mail-address (not (string= user-mail-address "")))
  1459. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1460. (when (and user-full-name (not (string= user-full-name "")))
  1461. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1462. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1463. "Regexp matched against the \"From:\" header of an email or usenet message.
  1464. It should match if the message is from the user him/herself."
  1465. :group 'org-link-store
  1466. :type 'regexp)
  1467. (defcustom org-context-in-file-links t
  1468. "Non-nil means file links from `org-store-link' contain context.
  1469. A search string will be added to the file name with :: as separator and
  1470. used to find the context when the link is activated by the command
  1471. `org-open-at-point'. When this option is t, the entire active region
  1472. will be placed in the search string of the file link. If set to a
  1473. positive integer, only the first n lines of context will be stored.
  1474. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1475. negates this setting for the duration of the command."
  1476. :group 'org-link-store
  1477. :type '(choice boolean integer))
  1478. (defcustom org-keep-stored-link-after-insertion nil
  1479. "Non-nil means keep link in list for entire session.
  1480. The command `org-store-link' adds a link pointing to the current
  1481. location to an internal list. These links accumulate during a session.
  1482. The command `org-insert-link' can be used to insert links into any
  1483. Org-mode file (offering completion for all stored links). When this
  1484. option is nil, every link which has been inserted once using \\[org-insert-link]
  1485. will be removed from the list, to make completing the unused links
  1486. more efficient."
  1487. :group 'org-link-store
  1488. :type 'boolean)
  1489. (defgroup org-link-follow nil
  1490. "Options concerning following links in Org-mode."
  1491. :tag "Org Follow Link"
  1492. :group 'org-link)
  1493. (defcustom org-link-translation-function nil
  1494. "Function to translate links with different syntax to Org syntax.
  1495. This can be used to translate links created for example by the Planner
  1496. or emacs-wiki packages to Org syntax.
  1497. The function must accept two parameters, a TYPE containing the link
  1498. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1499. which is everything after the link protocol. It should return a cons
  1500. with possibly modified values of type and path.
  1501. Org contains a function for this, so if you set this variable to
  1502. `org-translate-link-from-planner', you should be able follow many
  1503. links created by planner."
  1504. :group 'org-link-follow
  1505. :type '(choice (const nil) (function)))
  1506. (defcustom org-follow-link-hook nil
  1507. "Hook that is run after a link has been followed."
  1508. :group 'org-link-follow
  1509. :type 'hook)
  1510. (defcustom org-tab-follows-link nil
  1511. "Non-nil means on links TAB will follow the link.
  1512. Needs to be set before org.el is loaded.
  1513. This really should not be used, it does not make sense, and the
  1514. implementation is bad."
  1515. :group 'org-link-follow
  1516. :type 'boolean)
  1517. (defcustom org-return-follows-link nil
  1518. "Non-nil means on links RET will follow the link.
  1519. In tables, the special behavior of RET has precedence."
  1520. :group 'org-link-follow
  1521. :type 'boolean)
  1522. (defcustom org-mouse-1-follows-link
  1523. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1524. "Non-nil means mouse-1 on a link will follow the link.
  1525. A longer mouse click will still set point. Does not work on XEmacs.
  1526. Needs to be set before org.el is loaded."
  1527. :group 'org-link-follow
  1528. :version "24.4"
  1529. :package-version '(Org . "8.3")
  1530. :type '(choice
  1531. (const :tag "A double click follows the link" 'double)
  1532. (const :tag "Unconditionally follow the link with mouse-1" t)
  1533. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1534. (defcustom org-mark-ring-length 4
  1535. "Number of different positions to be recorded in the ring.
  1536. Changing this requires a restart of Emacs to work correctly."
  1537. :group 'org-link-follow
  1538. :type 'integer)
  1539. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1540. "Non-nil means internal links in Org files must exactly match a headline.
  1541. When nil, the link search tries to match a phrase with all words
  1542. in the search text."
  1543. :group 'org-link-follow
  1544. :version "24.1"
  1545. :type '(choice
  1546. (const :tag "Use fuzzy text search" nil)
  1547. (const :tag "Match only exact headline" t)
  1548. (const :tag "Match exact headline or query to create it"
  1549. query-to-create)))
  1550. (defcustom org-link-frame-setup
  1551. '((vm . vm-visit-folder-other-frame)
  1552. (vm-imap . vm-visit-imap-folder-other-frame)
  1553. (gnus . org-gnus-no-new-news)
  1554. (file . find-file-other-window)
  1555. (wl . wl-other-frame))
  1556. "Setup the frame configuration for following links.
  1557. When following a link with Emacs, it may often be useful to display
  1558. this link in another window or frame. This variable can be used to
  1559. set this up for the different types of links.
  1560. For VM, use any of
  1561. `vm-visit-folder'
  1562. `vm-visit-folder-other-window'
  1563. `vm-visit-folder-other-frame'
  1564. For Gnus, use any of
  1565. `gnus'
  1566. `gnus-other-frame'
  1567. `org-gnus-no-new-news'
  1568. For FILE, use any of
  1569. `find-file'
  1570. `find-file-other-window'
  1571. `find-file-other-frame'
  1572. For Wanderlust use any of
  1573. `wl'
  1574. `wl-other-frame'
  1575. For the calendar, use the variable `calendar-setup'.
  1576. For BBDB, it is currently only possible to display the matches in
  1577. another window."
  1578. :group 'org-link-follow
  1579. :type '(list
  1580. (cons (const vm)
  1581. (choice
  1582. (const vm-visit-folder)
  1583. (const vm-visit-folder-other-window)
  1584. (const vm-visit-folder-other-frame)))
  1585. (cons (const vm-imap)
  1586. (choice
  1587. (const vm-visit-imap-folder)
  1588. (const vm-visit-imap-folder-other-window)
  1589. (const vm-visit-imap-folder-other-frame)))
  1590. (cons (const gnus)
  1591. (choice
  1592. (const gnus)
  1593. (const gnus-other-frame)
  1594. (const org-gnus-no-new-news)))
  1595. (cons (const file)
  1596. (choice
  1597. (const find-file)
  1598. (const find-file-other-window)
  1599. (const find-file-other-frame)))
  1600. (cons (const wl)
  1601. (choice
  1602. (const wl)
  1603. (const wl-other-frame)))))
  1604. (defcustom org-display-internal-link-with-indirect-buffer nil
  1605. "Non-nil means use indirect buffer to display infile links.
  1606. Activating internal links (from one location in a file to another location
  1607. in the same file) normally just jumps to the location. When the link is
  1608. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1609. is displayed in
  1610. another window. When this option is set, the other window actually displays
  1611. an indirect buffer clone of the current buffer, to avoid any visibility
  1612. changes to the current buffer."
  1613. :group 'org-link-follow
  1614. :type 'boolean)
  1615. (defcustom org-open-non-existing-files nil
  1616. "Non-nil means `org-open-file' will open non-existing files.
  1617. When nil, an error will be generated.
  1618. This variable applies only to external applications because they
  1619. might choke on non-existing files. If the link is to a file that
  1620. will be opened in Emacs, the variable is ignored."
  1621. :group 'org-link-follow
  1622. :type 'boolean)
  1623. (defcustom org-open-directory-means-index-dot-org nil
  1624. "Non-nil means a link to a directory really means to index.org.
  1625. When nil, following a directory link will run dired or open a finder/explorer
  1626. window on that directory."
  1627. :group 'org-link-follow
  1628. :type 'boolean)
  1629. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1630. "Function and arguments to call for following mailto links.
  1631. This is a list with the first element being a Lisp function, and the
  1632. remaining elements being arguments to the function. In string arguments,
  1633. %a will be replaced by the address, and %s will be replaced by the subject
  1634. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1635. :group 'org-link-follow
  1636. :type '(choice
  1637. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1638. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1639. (const :tag "message-mail" (message-mail "%a" "%s"))
  1640. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1641. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1642. "Non-nil means ask for confirmation before executing shell links.
  1643. Shell links can be dangerous: just think about a link
  1644. [[shell:rm -rf ~/*][Google Search]]
  1645. This link would show up in your Org-mode document as \"Google Search\",
  1646. but really it would remove your entire home directory.
  1647. Therefore we advise against setting this variable to nil.
  1648. Just change it to `y-or-n-p' if you want to confirm with a
  1649. single keystroke rather than having to type \"yes\"."
  1650. :group 'org-link-follow
  1651. :type '(choice
  1652. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1653. (const :tag "with y-or-n (faster)" y-or-n-p)
  1654. (const :tag "no confirmation (dangerous)" nil)))
  1655. (put 'org-confirm-shell-link-function
  1656. 'safe-local-variable
  1657. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1658. (defcustom org-confirm-shell-link-not-regexp ""
  1659. "A regexp to skip confirmation for shell links."
  1660. :group 'org-link-follow
  1661. :version "24.1"
  1662. :type 'regexp)
  1663. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1664. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1665. Elisp links can be dangerous: just think about a link
  1666. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1667. This link would show up in your Org-mode document as \"Google Search\",
  1668. but really it would remove your entire home directory.
  1669. Therefore we advise against setting this variable to nil.
  1670. Just change it to `y-or-n-p' if you want to confirm with a
  1671. single keystroke rather than having to type \"yes\"."
  1672. :group 'org-link-follow
  1673. :type '(choice
  1674. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1675. (const :tag "with y-or-n (faster)" y-or-n-p)
  1676. (const :tag "no confirmation (dangerous)" nil)))
  1677. (put 'org-confirm-shell-link-function
  1678. 'safe-local-variable
  1679. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1680. (defcustom org-confirm-elisp-link-not-regexp ""
  1681. "A regexp to skip confirmation for Elisp links."
  1682. :group 'org-link-follow
  1683. :version "24.1"
  1684. :type 'regexp)
  1685. (defconst org-file-apps-defaults-gnu
  1686. '((remote . emacs)
  1687. (system . mailcap)
  1688. (t . mailcap))
  1689. "Default file applications on a UNIX or GNU/Linux system.
  1690. See `org-file-apps'.")
  1691. (defconst org-file-apps-defaults-macosx
  1692. '((remote . emacs)
  1693. (t . "open %s")
  1694. (system . "open %s")
  1695. ("ps.gz" . "gv %s")
  1696. ("eps.gz" . "gv %s")
  1697. ("dvi" . "xdvi %s")
  1698. ("fig" . "xfig %s"))
  1699. "Default file applications on a MacOS X system.
  1700. The system \"open\" is known as a default, but we use X11 applications
  1701. for some files for which the OS does not have a good default.
  1702. See `org-file-apps'.")
  1703. (defconst org-file-apps-defaults-windowsnt
  1704. (list
  1705. '(remote . emacs)
  1706. (cons t
  1707. (list (if (featurep 'xemacs)
  1708. 'mswindows-shell-execute
  1709. 'w32-shell-execute)
  1710. "open" 'file))
  1711. (cons 'system
  1712. (list (if (featurep 'xemacs)
  1713. 'mswindows-shell-execute
  1714. 'w32-shell-execute)
  1715. "open" 'file)))
  1716. "Default file applications on a Windows NT system.
  1717. The system \"open\" is used for most files.
  1718. See `org-file-apps'.")
  1719. (defcustom org-file-apps
  1720. '((auto-mode . emacs)
  1721. ("\\.mm\\'" . default)
  1722. ("\\.x?html?\\'" . default)
  1723. ("\\.pdf\\'" . default))
  1724. "External applications for opening `file:path' items in a document.
  1725. Org-mode uses system defaults for different file types, but
  1726. you can use this variable to set the application for a given file
  1727. extension. The entries in this list are cons cells where the car identifies
  1728. files and the cdr the corresponding command. Possible values for the
  1729. file identifier are
  1730. \"string\" A string as a file identifier can be interpreted in different
  1731. ways, depending on its contents:
  1732. - Alphanumeric characters only:
  1733. Match links with this file extension.
  1734. Example: (\"pdf\" . \"evince %s\")
  1735. to open PDFs with evince.
  1736. - Regular expression: Match links where the
  1737. filename matches the regexp. If you want to
  1738. use groups here, use shy groups.
  1739. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1740. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1741. to open *.html and *.xhtml with firefox.
  1742. - Regular expression which contains (non-shy) groups:
  1743. Match links where the whole link, including \"::\", and
  1744. anything after that, matches the regexp.
  1745. In a custom command string, %1, %2, etc. are replaced with
  1746. the parts of the link that were matched by the groups.
  1747. For backwards compatibility, if a command string is given
  1748. that does not use any of the group matches, this case is
  1749. handled identically to the second one (i.e. match against
  1750. file name only).
  1751. In a custom lisp form, you can access the group matches with
  1752. (match-string n link).
  1753. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1754. to open [[file:document.pdf::5]] with evince at page 5.
  1755. `directory' Matches a directory
  1756. `remote' Matches a remote file, accessible through tramp or efs.
  1757. Remote files most likely should be visited through Emacs
  1758. because external applications cannot handle such paths.
  1759. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1760. so all files Emacs knows how to handle. Using this with
  1761. command `emacs' will open most files in Emacs. Beware that this
  1762. will also open html files inside Emacs, unless you add
  1763. (\"html\" . default) to the list as well.
  1764. t Default for files not matched by any of the other options.
  1765. `system' The system command to open files, like `open' on Windows
  1766. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1767. that will be selected if you call `C-c C-o' with a double
  1768. \\[universal-argument] \\[universal-argument] prefix.
  1769. Possible values for the command are:
  1770. `emacs' The file will be visited by the current Emacs process.
  1771. `default' Use the default application for this file type, which is the
  1772. association for t in the list, most likely in the system-specific
  1773. part.
  1774. This can be used to overrule an unwanted setting in the
  1775. system-specific variable.
  1776. `system' Use the system command for opening files, like \"open\".
  1777. This command is specified by the entry whose car is `system'.
  1778. Most likely, the system-specific version of this variable
  1779. does define this command, but you can overrule/replace it
  1780. here.
  1781. string A command to be executed by a shell; %s will be replaced
  1782. by the path to the file.
  1783. sexp A Lisp form which will be evaluated. The file path will
  1784. be available in the Lisp variable `file'.
  1785. For more examples, see the system specific constants
  1786. `org-file-apps-defaults-macosx'
  1787. `org-file-apps-defaults-windowsnt'
  1788. `org-file-apps-defaults-gnu'."
  1789. :group 'org-link-follow
  1790. :type '(repeat
  1791. (cons (choice :value ""
  1792. (string :tag "Extension")
  1793. (const :tag "System command to open files" system)
  1794. (const :tag "Default for unrecognized files" t)
  1795. (const :tag "Remote file" remote)
  1796. (const :tag "Links to a directory" directory)
  1797. (const :tag "Any files that have Emacs modes"
  1798. auto-mode))
  1799. (choice :value ""
  1800. (const :tag "Visit with Emacs" emacs)
  1801. (const :tag "Use default" default)
  1802. (const :tag "Use the system command" system)
  1803. (string :tag "Command")
  1804. (sexp :tag "Lisp form")))))
  1805. (defcustom org-doi-server-url "http://dx.doi.org/"
  1806. "The URL of the DOI server."
  1807. :type 'string
  1808. :version "24.3"
  1809. :group 'org-link-follow)
  1810. (defgroup org-refile nil
  1811. "Options concerning refiling entries in Org-mode."
  1812. :tag "Org Refile"
  1813. :group 'org)
  1814. (defcustom org-directory "~/org"
  1815. "Directory with org files.
  1816. This is just a default location to look for Org files. There is no need
  1817. at all to put your files into this directory. It is only used in the
  1818. following situations:
  1819. 1. When a capture template specifies a target file that is not an
  1820. absolute path. The path will then be interpreted relative to
  1821. `org-directory'
  1822. 2. When a capture note is filed away in an interactive way (when exiting the
  1823. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1824. with `org-directory' as the default path."
  1825. :group 'org-refile
  1826. :group 'org-capture
  1827. :type 'directory)
  1828. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1829. "Default target for storing notes.
  1830. Used as a fall back file for org-capture.el, for templates that
  1831. do not specify a target file."
  1832. :group 'org-refile
  1833. :group 'org-capture
  1834. :type '(choice
  1835. (const :tag "Default from remember-data-file" nil)
  1836. file))
  1837. (defcustom org-goto-interface 'outline
  1838. "The default interface to be used for `org-goto'.
  1839. Allowed values are:
  1840. outline The interface shows an outline of the relevant file
  1841. and the correct heading is found by moving through
  1842. the outline or by searching with incremental search.
  1843. outline-path-completion Headlines in the current buffer are offered via
  1844. completion. This is the interface also used by
  1845. the refile command."
  1846. :group 'org-refile
  1847. :type '(choice
  1848. (const :tag "Outline" outline)
  1849. (const :tag "Outline-path-completion" outline-path-completion)))
  1850. (defcustom org-goto-max-level 5
  1851. "Maximum target level when running `org-goto' with refile interface."
  1852. :group 'org-refile
  1853. :type 'integer)
  1854. (defcustom org-reverse-note-order nil
  1855. "Non-nil means store new notes at the beginning of a file or entry.
  1856. When nil, new notes will be filed to the end of a file or entry.
  1857. This can also be a list with cons cells of regular expressions that
  1858. are matched against file names, and values."
  1859. :group 'org-capture
  1860. :group 'org-refile
  1861. :type '(choice
  1862. (const :tag "Reverse always" t)
  1863. (const :tag "Reverse never" nil)
  1864. (repeat :tag "By file name regexp"
  1865. (cons regexp boolean))))
  1866. (defcustom org-log-refile nil
  1867. "Information to record when a task is refiled.
  1868. Possible values are:
  1869. nil Don't add anything
  1870. time Add a time stamp to the task
  1871. note Prompt for a note and add it with template `org-log-note-headings'
  1872. This option can also be set with on a per-file-basis with
  1873. #+STARTUP: nologrefile
  1874. #+STARTUP: logrefile
  1875. #+STARTUP: lognoterefile
  1876. You can have local logging settings for a subtree by setting the LOGGING
  1877. property to one or more of these keywords.
  1878. When bulk-refiling from the agenda, the value `note' is forbidden and
  1879. will temporarily be changed to `time'."
  1880. :group 'org-refile
  1881. :group 'org-progress
  1882. :version "24.1"
  1883. :type '(choice
  1884. (const :tag "No logging" nil)
  1885. (const :tag "Record timestamp" time)
  1886. (const :tag "Record timestamp with note." note)))
  1887. (defcustom org-refile-targets nil
  1888. "Targets for refiling entries with \\[org-refile].
  1889. This is a list of cons cells. Each cell contains:
  1890. - a specification of the files to be considered, either a list of files,
  1891. or a symbol whose function or variable value will be used to retrieve
  1892. a file name or a list of file names. If you use `org-agenda-files' for
  1893. that, all agenda files will be scanned for targets. Nil means consider
  1894. headings in the current buffer.
  1895. - A specification of how to find candidate refile targets. This may be
  1896. any of:
  1897. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1898. This tag has to be present in all target headlines, inheritance will
  1899. not be considered.
  1900. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1901. todo keyword.
  1902. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1903. headlines that are refiling targets.
  1904. - a cons cell (:level . N). Any headline of level N is considered a target.
  1905. Note that, when `org-odd-levels-only' is set, level corresponds to
  1906. order in hierarchy, not to the number of stars.
  1907. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1908. Note that, when `org-odd-levels-only' is set, level corresponds to
  1909. order in hierarchy, not to the number of stars.
  1910. Each element of this list generates a set of possible targets.
  1911. The union of these sets is presented (with completion) to
  1912. the user by `org-refile'.
  1913. You can set the variable `org-refile-target-verify-function' to a function
  1914. to verify each headline found by the simple criteria above.
  1915. When this variable is nil, all top-level headlines in the current buffer
  1916. are used, equivalent to the value `((nil . (:level . 1))'."
  1917. :group 'org-refile
  1918. :type '(repeat
  1919. (cons
  1920. (choice :value org-agenda-files
  1921. (const :tag "All agenda files" org-agenda-files)
  1922. (const :tag "Current buffer" nil)
  1923. (function) (variable) (file))
  1924. (choice :tag "Identify target headline by"
  1925. (cons :tag "Specific tag" (const :value :tag) (string))
  1926. (cons :tag "TODO keyword" (const :value :todo) (string))
  1927. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1928. (cons :tag "Level number" (const :value :level) (integer))
  1929. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1930. (defcustom org-refile-target-verify-function nil
  1931. "Function to verify if the headline at point should be a refile target.
  1932. The function will be called without arguments, with point at the
  1933. beginning of the headline. It should return t and leave point
  1934. where it is if the headline is a valid target for refiling.
  1935. If the target should not be selected, the function must return nil.
  1936. In addition to this, it may move point to a place from where the search
  1937. should be continued. For example, the function may decide that the entire
  1938. subtree of the current entry should be excluded and move point to the end
  1939. of the subtree."
  1940. :group 'org-refile
  1941. :type '(choice
  1942. (const nil)
  1943. (function)))
  1944. (defcustom org-refile-use-cache nil
  1945. "Non-nil means cache refile targets to speed up the process.
  1946. The cache for a particular file will be updated automatically when
  1947. the buffer has been killed, or when any of the marker used for flagging
  1948. refile targets no longer points at a live buffer.
  1949. If you have added new entries to a buffer that might themselves be targets,
  1950. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1951. find that easier, `C-u C-u C-u C-c C-w'."
  1952. :group 'org-refile
  1953. :version "24.1"
  1954. :type 'boolean)
  1955. (defcustom org-refile-use-outline-path nil
  1956. "Non-nil means provide refile targets as paths.
  1957. So a level 3 headline will be available as level1/level2/level3.
  1958. When the value is `file', also include the file name (without directory)
  1959. into the path. In this case, you can also stop the completion after
  1960. the file name, to get entries inserted as top level in the file.
  1961. When `full-file-path', include the full file path."
  1962. :group 'org-refile
  1963. :type '(choice
  1964. (const :tag "Not" nil)
  1965. (const :tag "Yes" t)
  1966. (const :tag "Start with file name" file)
  1967. (const :tag "Start with full file path" full-file-path)))
  1968. (defcustom org-outline-path-complete-in-steps t
  1969. "Non-nil means complete the outline path in hierarchical steps.
  1970. When Org-mode uses the refile interface to select an outline path
  1971. \(see variable `org-refile-use-outline-path'), the completion of
  1972. the path can be done is a single go, or if can be done in steps down
  1973. the headline hierarchy. Going in steps is probably the best if you
  1974. do not use a special completion package like `ido' or `icicles'.
  1975. However, when using these packages, going in one step can be very
  1976. fast, while still showing the whole path to the entry."
  1977. :group 'org-refile
  1978. :type 'boolean)
  1979. (defcustom org-refile-allow-creating-parent-nodes nil
  1980. "Non-nil means allow to create new nodes as refile targets.
  1981. New nodes are then created by adding \"/new node name\" to the completion
  1982. of an existing node. When the value of this variable is `confirm',
  1983. new node creation must be confirmed by the user (recommended)
  1984. When nil, the completion must match an existing entry.
  1985. Note that, if the new heading is not seen by the criteria
  1986. listed in `org-refile-targets', multiple instances of the same
  1987. heading would be created by trying again to file under the new
  1988. heading."
  1989. :group 'org-refile
  1990. :type '(choice
  1991. (const :tag "Never" nil)
  1992. (const :tag "Always" t)
  1993. (const :tag "Prompt for confirmation" confirm)))
  1994. (defcustom org-refile-active-region-within-subtree nil
  1995. "Non-nil means also refile active region within a subtree.
  1996. By default `org-refile' doesn't allow refiling regions if they
  1997. don't contain a set of subtrees, but it might be convenient to
  1998. do so sometimes: in that case, the first line of the region is
  1999. converted to a headline before refiling."
  2000. :group 'org-refile
  2001. :version "24.1"
  2002. :type 'boolean)
  2003. (defgroup org-todo nil
  2004. "Options concerning TODO items in Org-mode."
  2005. :tag "Org TODO"
  2006. :group 'org)
  2007. (defgroup org-progress nil
  2008. "Options concerning Progress logging in Org-mode."
  2009. :tag "Org Progress"
  2010. :group 'org-time)
  2011. (defvar org-todo-interpretation-widgets
  2012. '((:tag "Sequence (cycling hits every state)" sequence)
  2013. (:tag "Type (cycling directly to DONE)" type))
  2014. "The available interpretation symbols for customizing `org-todo-keywords'.
  2015. Interested libraries should add to this list.")
  2016. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2017. "List of TODO entry keyword sequences and their interpretation.
  2018. \\<org-mode-map>This is a list of sequences.
  2019. Each sequence starts with a symbol, either `sequence' or `type',
  2020. indicating if the keywords should be interpreted as a sequence of
  2021. action steps, or as different types of TODO items. The first
  2022. keywords are states requiring action - these states will select a headline
  2023. for inclusion into the global TODO list Org-mode produces. If one of
  2024. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2025. signify that no further action is necessary. If \"|\" is not found,
  2026. the last keyword is treated as the only DONE state of the sequence.
  2027. The command \\[org-todo] cycles an entry through these states, and one
  2028. additional state where no keyword is present. For details about this
  2029. cycling, see the manual.
  2030. TODO keywords and interpretation can also be set on a per-file basis with
  2031. the special #+SEQ_TODO and #+TYP_TODO lines.
  2032. Each keyword can optionally specify a character for fast state selection
  2033. \(in combination with the variable `org-use-fast-todo-selection')
  2034. and specifiers for state change logging, using the same syntax that
  2035. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2036. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2037. indicates to record a time stamp each time this state is selected.
  2038. Each keyword may also specify if a timestamp or a note should be
  2039. recorded when entering or leaving the state, by adding additional
  2040. characters in the parenthesis after the keyword. This looks like this:
  2041. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2042. record only the time of the state change. With X and Y being either
  2043. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2044. Y when leaving the state if and only if the *target* state does not
  2045. define X. You may omit any of the fast-selection key or X or /Y,
  2046. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2047. For backward compatibility, this variable may also be just a list
  2048. of keywords. In this case the interpretation (sequence or type) will be
  2049. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2050. :group 'org-todo
  2051. :group 'org-keywords
  2052. :type '(choice
  2053. (repeat :tag "Old syntax, just keywords"
  2054. (string :tag "Keyword"))
  2055. (repeat :tag "New syntax"
  2056. (cons
  2057. (choice
  2058. :tag "Interpretation"
  2059. ;;Quick and dirty way to see
  2060. ;;`org-todo-interpretations'. This takes the
  2061. ;;place of item arguments
  2062. :convert-widget
  2063. (lambda (widget)
  2064. (widget-put widget
  2065. :args (mapcar
  2066. #'(lambda (x)
  2067. (widget-convert
  2068. (cons 'const x)))
  2069. org-todo-interpretation-widgets))
  2070. widget))
  2071. (repeat
  2072. (string :tag "Keyword"))))))
  2073. (defvar org-todo-keywords-1 nil
  2074. "All TODO and DONE keywords active in a buffer.")
  2075. (make-variable-buffer-local 'org-todo-keywords-1)
  2076. (defvar org-todo-keywords-for-agenda nil)
  2077. (defvar org-done-keywords-for-agenda nil)
  2078. (defvar org-drawers-for-agenda nil)
  2079. (defvar org-todo-keyword-alist-for-agenda nil)
  2080. (defvar org-tag-alist-for-agenda nil
  2081. "Alist of all tags from all agenda files.")
  2082. (defvar org-tag-groups-alist-for-agenda nil
  2083. "Alist of all groups tags from all current agenda files.")
  2084. (defvar org-tag-groups-alist nil)
  2085. (make-variable-buffer-local 'org-tag-groups-alist)
  2086. (defvar org-agenda-contributing-files nil)
  2087. (defvar org-not-done-keywords nil)
  2088. (make-variable-buffer-local 'org-not-done-keywords)
  2089. (defvar org-done-keywords nil)
  2090. (make-variable-buffer-local 'org-done-keywords)
  2091. (defvar org-todo-heads nil)
  2092. (make-variable-buffer-local 'org-todo-heads)
  2093. (defvar org-todo-sets nil)
  2094. (make-variable-buffer-local 'org-todo-sets)
  2095. (defvar org-todo-log-states nil)
  2096. (make-variable-buffer-local 'org-todo-log-states)
  2097. (defvar org-todo-kwd-alist nil)
  2098. (make-variable-buffer-local 'org-todo-kwd-alist)
  2099. (defvar org-todo-key-alist nil)
  2100. (make-variable-buffer-local 'org-todo-key-alist)
  2101. (defvar org-todo-key-trigger nil)
  2102. (make-variable-buffer-local 'org-todo-key-trigger)
  2103. (defcustom org-todo-interpretation 'sequence
  2104. "Controls how TODO keywords are interpreted.
  2105. This variable is in principle obsolete and is only used for
  2106. backward compatibility, if the interpretation of todo keywords is
  2107. not given already in `org-todo-keywords'. See that variable for
  2108. more information."
  2109. :group 'org-todo
  2110. :group 'org-keywords
  2111. :type '(choice (const sequence)
  2112. (const type)))
  2113. (defcustom org-use-fast-todo-selection t
  2114. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2115. This variable describes if and under what circumstances the cycling
  2116. mechanism for TODO keywords will be replaced by a single-key, direct
  2117. selection scheme.
  2118. When nil, fast selection is never used.
  2119. When the symbol `prefix', it will be used when `org-todo' is called
  2120. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2121. `C-u t' in an agenda buffer.
  2122. When t, fast selection is used by default. In this case, the prefix
  2123. argument forces cycling instead.
  2124. In all cases, the special interface is only used if access keys have
  2125. actually been assigned by the user, i.e. if keywords in the configuration
  2126. are followed by a letter in parenthesis, like TODO(t)."
  2127. :group 'org-todo
  2128. :type '(choice
  2129. (const :tag "Never" nil)
  2130. (const :tag "By default" t)
  2131. (const :tag "Only with C-u C-c C-t" prefix)))
  2132. (defcustom org-provide-todo-statistics t
  2133. "Non-nil means update todo statistics after insert and toggle.
  2134. ALL-HEADLINES means update todo statistics by including headlines
  2135. with no TODO keyword as well, counting them as not done.
  2136. A list of TODO keywords means the same, but skip keywords that are
  2137. not in this list.
  2138. When this is set, todo statistics is updated in the parent of the
  2139. current entry each time a todo state is changed."
  2140. :group 'org-todo
  2141. :type '(choice
  2142. (const :tag "Yes, only for TODO entries" t)
  2143. (const :tag "Yes, including all entries" 'all-headlines)
  2144. (repeat :tag "Yes, for TODOs in this list"
  2145. (string :tag "TODO keyword"))
  2146. (other :tag "No TODO statistics" nil)))
  2147. (defcustom org-hierarchical-todo-statistics t
  2148. "Non-nil means TODO statistics covers just direct children.
  2149. When nil, all entries in the subtree are considered.
  2150. This has only an effect if `org-provide-todo-statistics' is set.
  2151. To set this to nil for only a single subtree, use a COOKIE_DATA
  2152. property and include the word \"recursive\" into the value."
  2153. :group 'org-todo
  2154. :type 'boolean)
  2155. (defcustom org-after-todo-state-change-hook nil
  2156. "Hook which is run after the state of a TODO item was changed.
  2157. The new state (a string with a TODO keyword, or nil) is available in the
  2158. Lisp variable `org-state'."
  2159. :group 'org-todo
  2160. :type 'hook)
  2161. (defvar org-blocker-hook nil
  2162. "Hook for functions that are allowed to block a state change.
  2163. Functions in this hook should not modify the buffer.
  2164. Each function gets as its single argument a property list,
  2165. see `org-trigger-hook' for more information about this list.
  2166. If any of the functions in this hook returns nil, the state change
  2167. is blocked.")
  2168. (defvar org-trigger-hook nil
  2169. "Hook for functions that are triggered by a state change.
  2170. Each function gets as its single argument a property list with at
  2171. least the following elements:
  2172. (:type type-of-change :position pos-at-entry-start
  2173. :from old-state :to new-state)
  2174. Depending on the type, more properties may be present.
  2175. This mechanism is currently implemented for:
  2176. TODO state changes
  2177. ------------------
  2178. :type todo-state-change
  2179. :from previous state (keyword as a string), or nil, or a symbol
  2180. 'todo' or 'done', to indicate the general type of state.
  2181. :to new state, like in :from")
  2182. (defcustom org-enforce-todo-dependencies nil
  2183. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2184. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2185. be blocked if any prior sibling is not yet done.
  2186. Finally, if the parent is blocked because of ordered siblings of its own,
  2187. the child will also be blocked."
  2188. :set (lambda (var val)
  2189. (set var val)
  2190. (if val
  2191. (add-hook 'org-blocker-hook
  2192. 'org-block-todo-from-children-or-siblings-or-parent)
  2193. (remove-hook 'org-blocker-hook
  2194. 'org-block-todo-from-children-or-siblings-or-parent)))
  2195. :group 'org-todo
  2196. :type 'boolean)
  2197. (defcustom org-enforce-todo-checkbox-dependencies nil
  2198. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2199. When this is nil, checkboxes have no influence on switching TODO states.
  2200. When non-nil, you first need to check off all check boxes before the TODO
  2201. entry can be switched to DONE.
  2202. This variable needs to be set before org.el is loaded, and you need to
  2203. restart Emacs after a change to make the change effective. The only way
  2204. to change is while Emacs is running is through the customize interface."
  2205. :set (lambda (var val)
  2206. (set var val)
  2207. (if val
  2208. (add-hook 'org-blocker-hook
  2209. 'org-block-todo-from-checkboxes)
  2210. (remove-hook 'org-blocker-hook
  2211. 'org-block-todo-from-checkboxes)))
  2212. :group 'org-todo
  2213. :type 'boolean)
  2214. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2215. "Non-nil means inserting a TODO heading is treated as state change.
  2216. So when the command \\[org-insert-todo-heading] is used, state change
  2217. logging will apply if appropriate. When nil, the new TODO item will
  2218. be inserted directly, and no logging will take place."
  2219. :group 'org-todo
  2220. :type 'boolean)
  2221. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2222. "Non-nil means switching TODO states with S-cursor counts as state change.
  2223. This is the default behavior. However, setting this to nil allows a
  2224. convenient way to select a TODO state and bypass any logging associated
  2225. with that."
  2226. :group 'org-todo
  2227. :type 'boolean)
  2228. (defcustom org-todo-state-tags-triggers nil
  2229. "Tag changes that should be triggered by TODO state changes.
  2230. This is a list. Each entry is
  2231. (state-change (tag . flag) .......)
  2232. State-change can be a string with a state, and empty string to indicate the
  2233. state that has no TODO keyword, or it can be one of the symbols `todo'
  2234. or `done', meaning any not-done or done state, respectively."
  2235. :group 'org-todo
  2236. :group 'org-tags
  2237. :type '(repeat
  2238. (cons (choice :tag "When changing to"
  2239. (const :tag "Not-done state" todo)
  2240. (const :tag "Done state" done)
  2241. (string :tag "State"))
  2242. (repeat
  2243. (cons :tag "Tag action"
  2244. (string :tag "Tag")
  2245. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2246. (defcustom org-log-done nil
  2247. "Information to record when a task moves to the DONE state.
  2248. Possible values are:
  2249. nil Don't add anything, just change the keyword
  2250. time Add a time stamp to the task
  2251. note Prompt for a note and add it with template `org-log-note-headings'
  2252. This option can also be set with on a per-file-basis with
  2253. #+STARTUP: nologdone
  2254. #+STARTUP: logdone
  2255. #+STARTUP: lognotedone
  2256. You can have local logging settings for a subtree by setting the LOGGING
  2257. property to one or more of these keywords."
  2258. :group 'org-todo
  2259. :group 'org-progress
  2260. :type '(choice
  2261. (const :tag "No logging" nil)
  2262. (const :tag "Record CLOSED timestamp" time)
  2263. (const :tag "Record CLOSED timestamp with note." note)))
  2264. ;; Normalize old uses of org-log-done.
  2265. (cond
  2266. ((eq org-log-done t) (setq org-log-done 'time))
  2267. ((and (listp org-log-done) (memq 'done org-log-done))
  2268. (setq org-log-done 'note)))
  2269. (defcustom org-log-reschedule nil
  2270. "Information to record when the scheduling date of a tasks is modified.
  2271. Possible values are:
  2272. nil Don't add anything, just change the date
  2273. time Add a time stamp to the task
  2274. note Prompt for a note and add it with template `org-log-note-headings'
  2275. This option can also be set with on a per-file-basis with
  2276. #+STARTUP: nologreschedule
  2277. #+STARTUP: logreschedule
  2278. #+STARTUP: lognotereschedule"
  2279. :group 'org-todo
  2280. :group 'org-progress
  2281. :type '(choice
  2282. (const :tag "No logging" nil)
  2283. (const :tag "Record timestamp" time)
  2284. (const :tag "Record timestamp with note." note)))
  2285. (defcustom org-log-redeadline nil
  2286. "Information to record when the deadline date of a tasks is modified.
  2287. Possible values are:
  2288. nil Don't add anything, just change the date
  2289. time Add a time stamp to the task
  2290. note Prompt for a note and add it with template `org-log-note-headings'
  2291. This option can also be set with on a per-file-basis with
  2292. #+STARTUP: nologredeadline
  2293. #+STARTUP: logredeadline
  2294. #+STARTUP: lognoteredeadline
  2295. You can have local logging settings for a subtree by setting the LOGGING
  2296. property to one or more of these keywords."
  2297. :group 'org-todo
  2298. :group 'org-progress
  2299. :type '(choice
  2300. (const :tag "No logging" nil)
  2301. (const :tag "Record timestamp" time)
  2302. (const :tag "Record timestamp with note." note)))
  2303. (defcustom org-log-note-clock-out nil
  2304. "Non-nil means record a note when clocking out of an item.
  2305. This can also be configured on a per-file basis by adding one of
  2306. the following lines anywhere in the buffer:
  2307. #+STARTUP: lognoteclock-out
  2308. #+STARTUP: nolognoteclock-out"
  2309. :group 'org-todo
  2310. :group 'org-progress
  2311. :type 'boolean)
  2312. (defcustom org-log-done-with-time t
  2313. "Non-nil means the CLOSED time stamp will contain date and time.
  2314. When nil, only the date will be recorded."
  2315. :group 'org-progress
  2316. :type 'boolean)
  2317. (defcustom org-log-note-headings
  2318. '((done . "CLOSING NOTE %t")
  2319. (state . "State %-12s from %-12S %t")
  2320. (note . "Note taken on %t")
  2321. (reschedule . "Rescheduled from %S on %t")
  2322. (delschedule . "Not scheduled, was %S on %t")
  2323. (redeadline . "New deadline from %S on %t")
  2324. (deldeadline . "Removed deadline, was %S on %t")
  2325. (refile . "Refiled on %t")
  2326. (clock-out . ""))
  2327. "Headings for notes added to entries.
  2328. The value is an alist, with the car being a symbol indicating the note
  2329. context, and the cdr is the heading to be used. The heading may also be the
  2330. empty string.
  2331. %t in the heading will be replaced by a time stamp.
  2332. %T will be an active time stamp instead the default inactive one
  2333. %d will be replaced by a short-format time stamp.
  2334. %D will be replaced by an active short-format time stamp.
  2335. %s will be replaced by the new TODO state, in double quotes.
  2336. %S will be replaced by the old TODO state, in double quotes.
  2337. %u will be replaced by the user name.
  2338. %U will be replaced by the full user name.
  2339. In fact, it is not a good idea to change the `state' entry, because
  2340. agenda log mode depends on the format of these entries."
  2341. :group 'org-todo
  2342. :group 'org-progress
  2343. :type '(list :greedy t
  2344. (cons (const :tag "Heading when closing an item" done) string)
  2345. (cons (const :tag
  2346. "Heading when changing todo state (todo sequence only)"
  2347. state) string)
  2348. (cons (const :tag "Heading when just taking a note" note) string)
  2349. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2350. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2351. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2352. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2353. (cons (const :tag "Heading when refiling" refile) string)
  2354. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2355. (unless (assq 'note org-log-note-headings)
  2356. (push '(note . "%t") org-log-note-headings))
  2357. (defcustom org-log-into-drawer nil
  2358. "Non-nil means insert state change notes and time stamps into a drawer.
  2359. When nil, state changes notes will be inserted after the headline and
  2360. any scheduling and clock lines, but not inside a drawer.
  2361. The value of this variable should be the name of the drawer to use.
  2362. LOGBOOK is proposed as the default drawer for this purpose, you can
  2363. also set this to a string to define the drawer of your choice.
  2364. A value of t is also allowed, representing \"LOGBOOK\".
  2365. A value of t or nil can also be set with on a per-file-basis with
  2366. #+STARTUP: logdrawer
  2367. #+STARTUP: nologdrawer
  2368. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2369. will be ignored.
  2370. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2371. a subtree."
  2372. :group 'org-todo
  2373. :group 'org-progress
  2374. :type '(choice
  2375. (const :tag "Not into a drawer" nil)
  2376. (const :tag "LOGBOOK" t)
  2377. (string :tag "Other")))
  2378. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2379. (defun org-log-into-drawer ()
  2380. "Return the value of `org-log-into-drawer', but let properties overrule.
  2381. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2382. used instead of the default value."
  2383. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2384. (cond
  2385. ((not p) org-log-into-drawer)
  2386. ((equal p "nil") nil)
  2387. ((equal p "t") "LOGBOOK")
  2388. (t p))))
  2389. (defcustom org-log-state-notes-insert-after-drawers nil
  2390. "Non-nil means insert state change notes after any drawers in entry.
  2391. Only the drawers that *immediately* follow the headline and the
  2392. deadline/scheduled line are skipped.
  2393. When nil, insert notes right after the heading and perhaps the line
  2394. with deadline/scheduling if present.
  2395. This variable will have no effect if `org-log-into-drawer' is
  2396. set."
  2397. :group 'org-todo
  2398. :group 'org-progress
  2399. :type 'boolean)
  2400. (defcustom org-log-states-order-reversed t
  2401. "Non-nil means the latest state note will be directly after heading.
  2402. When nil, the state change notes will be ordered according to time.
  2403. This option can also be set with on a per-file-basis with
  2404. #+STARTUP: logstatesreversed
  2405. #+STARTUP: nologstatesreversed"
  2406. :group 'org-todo
  2407. :group 'org-progress
  2408. :type 'boolean)
  2409. (defcustom org-todo-repeat-to-state nil
  2410. "The TODO state to which a repeater should return the repeating task.
  2411. By default this is the first task in a TODO sequence, or the previous state
  2412. in a TODO_TYP set. But you can specify another task here.
  2413. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2414. :group 'org-todo
  2415. :version "24.1"
  2416. :type '(choice (const :tag "Head of sequence" nil)
  2417. (string :tag "Specific state")))
  2418. (defcustom org-log-repeat 'time
  2419. "Non-nil means record moving through the DONE state when triggering repeat.
  2420. An auto-repeating task is immediately switched back to TODO when
  2421. marked DONE. If you are not logging state changes (by adding \"@\"
  2422. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2423. record a closing note, there will be no record of the task moving
  2424. through DONE. This variable forces taking a note anyway.
  2425. nil Don't force a record
  2426. time Record a time stamp
  2427. note Prompt for a note and add it with template `org-log-note-headings'
  2428. This option can also be set with on a per-file-basis with
  2429. #+STARTUP: nologrepeat
  2430. #+STARTUP: logrepeat
  2431. #+STARTUP: lognoterepeat
  2432. You can have local logging settings for a subtree by setting the LOGGING
  2433. property to one or more of these keywords."
  2434. :group 'org-todo
  2435. :group 'org-progress
  2436. :type '(choice
  2437. (const :tag "Don't force a record" nil)
  2438. (const :tag "Force recording the DONE state" time)
  2439. (const :tag "Force recording a note with the DONE state" note)))
  2440. (defgroup org-priorities nil
  2441. "Priorities in Org-mode."
  2442. :tag "Org Priorities"
  2443. :group 'org-todo)
  2444. (defcustom org-enable-priority-commands t
  2445. "Non-nil means priority commands are active.
  2446. When nil, these commands will be disabled, so that you never accidentally
  2447. set a priority."
  2448. :group 'org-priorities
  2449. :type 'boolean)
  2450. (defcustom org-highest-priority ?A
  2451. "The highest priority of TODO items. A character like ?A, ?B etc.
  2452. Must have a smaller ASCII number than `org-lowest-priority'."
  2453. :group 'org-priorities
  2454. :type 'character)
  2455. (defcustom org-lowest-priority ?C
  2456. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2457. Must have a larger ASCII number than `org-highest-priority'."
  2458. :group 'org-priorities
  2459. :type 'character)
  2460. (defcustom org-default-priority ?B
  2461. "The default priority of TODO items.
  2462. This is the priority an item gets if no explicit priority is given.
  2463. When starting to cycle on an empty priority the first step in the cycle
  2464. depends on `org-priority-start-cycle-with-default'. The resulting first
  2465. step priority must not exceed the range from `org-highest-priority' to
  2466. `org-lowest-priority' which means that `org-default-priority' has to be
  2467. in this range exclusive or inclusive the range boundaries. Else the
  2468. first step refuses to set the default and the second will fall back
  2469. to (depending on the command used) the highest or lowest priority."
  2470. :group 'org-priorities
  2471. :type 'character)
  2472. (defcustom org-priority-start-cycle-with-default t
  2473. "Non-nil means start with default priority when starting to cycle.
  2474. When this is nil, the first step in the cycle will be (depending on the
  2475. command used) one higher or lower than the default priority.
  2476. See also `org-default-priority'."
  2477. :group 'org-priorities
  2478. :type 'boolean)
  2479. (defcustom org-get-priority-function nil
  2480. "Function to extract the priority from a string.
  2481. The string is normally the headline. If this is nil Org computes the
  2482. priority from the priority cookie like [#A] in the headline. It returns
  2483. an integer, increasing by 1000 for each priority level.
  2484. The user can set a different function here, which should take a string
  2485. as an argument and return the numeric priority."
  2486. :group 'org-priorities
  2487. :version "24.1"
  2488. :type '(choice
  2489. (const nil)
  2490. (function)))
  2491. (defgroup org-time nil
  2492. "Options concerning time stamps and deadlines in Org-mode."
  2493. :tag "Org Time"
  2494. :group 'org)
  2495. (defcustom org-insert-labeled-timestamps-at-point nil
  2496. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2497. When nil, these labeled time stamps are forces into the second line of an
  2498. entry, just after the headline. When scheduling from the global TODO list,
  2499. the time stamp will always be forced into the second line."
  2500. :group 'org-time
  2501. :type 'boolean)
  2502. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2503. "Formats for `format-time-string' which are used for time stamps.
  2504. It is not recommended to change this constant.")
  2505. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2506. "Number of minutes to round time stamps to.
  2507. These are two values, the first applies when first creating a time stamp.
  2508. The second applies when changing it with the commands `S-up' and `S-down'.
  2509. When changing the time stamp, this means that it will change in steps
  2510. of N minutes, as given by the second value.
  2511. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2512. numbers should be factors of 60, so for example 5, 10, 15.
  2513. When this is larger than 1, you can still force an exact time stamp by using
  2514. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2515. and by using a prefix arg to `S-up/down' to specify the exact number
  2516. of minutes to shift."
  2517. :group 'org-time
  2518. :get #'(lambda (var) ; Make sure both elements are there
  2519. (if (integerp (default-value var))
  2520. (list (default-value var) 5)
  2521. (default-value var)))
  2522. :type '(list
  2523. (integer :tag "when inserting times")
  2524. (integer :tag "when modifying times")))
  2525. ;; Normalize old customizations of this variable.
  2526. (when (integerp org-time-stamp-rounding-minutes)
  2527. (setq org-time-stamp-rounding-minutes
  2528. (list org-time-stamp-rounding-minutes
  2529. org-time-stamp-rounding-minutes)))
  2530. (defcustom org-display-custom-times nil
  2531. "Non-nil means overlay custom formats over all time stamps.
  2532. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2533. To turn this on on a per-file basis, insert anywhere in the file:
  2534. #+STARTUP: customtime"
  2535. :group 'org-time
  2536. :set 'set-default
  2537. :type 'sexp)
  2538. (make-variable-buffer-local 'org-display-custom-times)
  2539. (defcustom org-time-stamp-custom-formats
  2540. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2541. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2542. These are overlaid over the default ISO format if the variable
  2543. `org-display-custom-times' is set. Time like %H:%M should be at the
  2544. end of the second format. The custom formats are also honored by export
  2545. commands, if custom time display is turned on at the time of export."
  2546. :group 'org-time
  2547. :type 'sexp)
  2548. (defun org-time-stamp-format (&optional long inactive)
  2549. "Get the right format for a time string."
  2550. (let ((f (if long (cdr org-time-stamp-formats)
  2551. (car org-time-stamp-formats))))
  2552. (if inactive
  2553. (concat "[" (substring f 1 -1) "]")
  2554. f)))
  2555. (defcustom org-time-clocksum-format
  2556. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2557. "The format string used when creating CLOCKSUM lines.
  2558. This is also used when Org mode generates a time duration.
  2559. The value can be a single format string containing two
  2560. %-sequences, which will be filled with the number of hours and
  2561. minutes in that order.
  2562. Alternatively, the value can be a plist associating any of the
  2563. keys :years, :months, :weeks, :days, :hours or :minutes with
  2564. format strings. The time duration is formatted using only the
  2565. time components that are needed and concatenating the results.
  2566. If a time unit in absent, it falls back to the next smallest
  2567. unit.
  2568. The keys :require-years, :require-months, :require-days,
  2569. :require-weeks, :require-hours, :require-minutes are also
  2570. meaningful. A non-nil value for these keys indicates that the
  2571. corresponding time component should always be included, even if
  2572. its value is 0.
  2573. For example,
  2574. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2575. :require-minutes t)
  2576. means durations longer than a day will be expressed in days,
  2577. hours and minutes, and durations less than a day will always be
  2578. expressed in hours and minutes (even for durations less than an
  2579. hour).
  2580. The value
  2581. \(:days \"%dd\" :minutes \"%dm\")
  2582. means durations longer than a day will be expressed in days and
  2583. minutes, and durations less than a day will be expressed entirely
  2584. in minutes (even for durations longer than an hour)."
  2585. :group 'org-time
  2586. :group 'org-clock
  2587. :version "24.4"
  2588. :package-version '(Org . "8.0")
  2589. :type '(choice (string :tag "Format string")
  2590. (set :tag "Plist"
  2591. (group :inline t (const :tag "Years" :years)
  2592. (string :tag "Format string"))
  2593. (group :inline t
  2594. (const :tag "Always show years" :require-years)
  2595. (const t))
  2596. (group :inline t (const :tag "Months" :months)
  2597. (string :tag "Format string"))
  2598. (group :inline t
  2599. (const :tag "Always show months" :require-months)
  2600. (const t))
  2601. (group :inline t (const :tag "Weeks" :weeks)
  2602. (string :tag "Format string"))
  2603. (group :inline t
  2604. (const :tag "Always show weeks" :require-weeks)
  2605. (const t))
  2606. (group :inline t (const :tag "Days" :days)
  2607. (string :tag "Format string"))
  2608. (group :inline t
  2609. (const :tag "Always show days" :require-days)
  2610. (const t))
  2611. (group :inline t (const :tag "Hours" :hours)
  2612. (string :tag "Format string"))
  2613. (group :inline t
  2614. (const :tag "Always show hours" :require-hours)
  2615. (const t))
  2616. (group :inline t (const :tag "Minutes" :minutes)
  2617. (string :tag "Format string"))
  2618. (group :inline t
  2619. (const :tag "Always show minutes" :require-minutes)
  2620. (const t)))))
  2621. (defcustom org-time-clocksum-use-fractional nil
  2622. "When non-nil, \\[org-clock-display] uses fractional times.
  2623. See `org-time-clocksum-format' for more on time clock formats."
  2624. :group 'org-time
  2625. :group 'org-clock
  2626. :version "24.3"
  2627. :type 'boolean)
  2628. (defcustom org-time-clocksum-use-effort-durations nil
  2629. "When non-nil, \\[org-clock-display] uses effort durations.
  2630. E.g. by default, one day is considered to be a 8 hours effort,
  2631. so a task that has been clocked for 16 hours will be displayed
  2632. as during 2 days in the clock display or in the clocktable.
  2633. See `org-effort-durations' on how to set effort durations
  2634. and `org-time-clocksum-format' for more on time clock formats."
  2635. :group 'org-time
  2636. :group 'org-clock
  2637. :version "24.4"
  2638. :package-version '(Org . "8.0")
  2639. :type 'boolean)
  2640. (defcustom org-time-clocksum-fractional-format "%.2f"
  2641. "The format string used when creating CLOCKSUM lines,
  2642. or when Org mode generates a time duration, if
  2643. `org-time-clocksum-use-fractional' is enabled.
  2644. The value can be a single format string containing one
  2645. %-sequence, which will be filled with the number of hours as
  2646. a float.
  2647. Alternatively, the value can be a plist associating any of the
  2648. keys :years, :months, :weeks, :days, :hours or :minutes with
  2649. a format string. The time duration is formatted using the
  2650. largest time unit which gives a non-zero integer part. If all
  2651. specified formats have zero integer part, the smallest time unit
  2652. is used."
  2653. :group 'org-time
  2654. :type '(choice (string :tag "Format string")
  2655. (set (group :inline t (const :tag "Years" :years)
  2656. (string :tag "Format string"))
  2657. (group :inline t (const :tag "Months" :months)
  2658. (string :tag "Format string"))
  2659. (group :inline t (const :tag "Weeks" :weeks)
  2660. (string :tag "Format string"))
  2661. (group :inline t (const :tag "Days" :days)
  2662. (string :tag "Format string"))
  2663. (group :inline t (const :tag "Hours" :hours)
  2664. (string :tag "Format string"))
  2665. (group :inline t (const :tag "Minutes" :minutes)
  2666. (string :tag "Format string")))))
  2667. (defcustom org-deadline-warning-days 14
  2668. "Number of days before expiration during which a deadline becomes active.
  2669. This variable governs the display in sparse trees and in the agenda.
  2670. When 0 or negative, it means use this number (the absolute value of it)
  2671. even if a deadline has a different individual lead time specified.
  2672. Custom commands can set this variable in the options section."
  2673. :group 'org-time
  2674. :group 'org-agenda-daily/weekly
  2675. :type 'integer)
  2676. (defcustom org-scheduled-delay-days 0
  2677. "Number of days before a scheduled item becomes active.
  2678. This variable governs the display in sparse trees and in the agenda.
  2679. The default value (i.e. 0) means: don't delay scheduled item.
  2680. When negative, it means use this number (the absolute value of it)
  2681. even if a scheduled item has a different individual delay time
  2682. specified.
  2683. Custom commands can set this variable in the options section."
  2684. :group 'org-time
  2685. :group 'org-agenda-daily/weekly
  2686. :version "24.4"
  2687. :package-version '(Org . "8.0")
  2688. :type 'integer)
  2689. (defcustom org-read-date-prefer-future t
  2690. "Non-nil means assume future for incomplete date input from user.
  2691. This affects the following situations:
  2692. 1. The user gives a month but not a year.
  2693. For example, if it is April and you enter \"feb 2\", this will be read
  2694. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2695. 2. The user gives a day, but no month.
  2696. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2697. read this as the third of *next* month. However, if you enter \"17\",
  2698. it will be considered as *this* month.
  2699. If you set this variable to the symbol `time', then also the following
  2700. will work:
  2701. 3. If the user gives a time.
  2702. If the time is before now, it will be interpreted as tomorrow.
  2703. Currently none of this works for ISO week specifications.
  2704. When this option is nil, the current day, month and year will always be
  2705. used as defaults.
  2706. See also `org-agenda-jump-prefer-future'."
  2707. :group 'org-time
  2708. :type '(choice
  2709. (const :tag "Never" nil)
  2710. (const :tag "Check month and day" t)
  2711. (const :tag "Check month, day, and time" time)))
  2712. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2713. "Should the agenda jump command prefer the future for incomplete dates?
  2714. The default is to do the same as configured in `org-read-date-prefer-future'.
  2715. But you can also set a deviating value here.
  2716. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2717. :group 'org-agenda
  2718. :group 'org-time
  2719. :version "24.1"
  2720. :type '(choice
  2721. (const :tag "Use org-read-date-prefer-future"
  2722. org-read-date-prefer-future)
  2723. (const :tag "Never" nil)
  2724. (const :tag "Always" t)))
  2725. (defcustom org-read-date-force-compatible-dates t
  2726. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2727. Depending on the system Emacs is running on, certain dates cannot
  2728. be represented with the type used internally to represent time.
  2729. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2730. correctly. Some systems allow for earlier dates, some for later,
  2731. some for both. One way to find out it to insert any date into an
  2732. Org buffer, putting the cursor on the year and hitting S-up and
  2733. S-down to test the range.
  2734. When this variable is set to t, the date/time prompt will not let
  2735. you specify dates outside the 1970-2037 range, so it is certain that
  2736. these dates will work in whatever version of Emacs you are
  2737. running, and also that you can move a file from one Emacs implementation
  2738. to another. WHenever Org is forcing the year for you, it will display
  2739. a message and beep.
  2740. When this variable is nil, Org will check if the date is
  2741. representable in the specific Emacs implementation you are using.
  2742. If not, it will force a year, usually the current year, and beep
  2743. to remind you. Currently this setting is not recommended because
  2744. the likelihood that you will open your Org files in an Emacs that
  2745. has limited date range is not negligible.
  2746. A workaround for this problem is to use diary sexp dates for time
  2747. stamps outside of this range."
  2748. :group 'org-time
  2749. :version "24.1"
  2750. :type 'boolean)
  2751. (defcustom org-read-date-display-live t
  2752. "Non-nil means display current interpretation of date prompt live.
  2753. This display will be in an overlay, in the minibuffer."
  2754. :group 'org-time
  2755. :type 'boolean)
  2756. (defcustom org-read-date-popup-calendar t
  2757. "Non-nil means pop up a calendar when prompting for a date.
  2758. In the calendar, the date can be selected with mouse-1. However, the
  2759. minibuffer will also be active, and you can simply enter the date as well.
  2760. When nil, only the minibuffer will be available."
  2761. :group 'org-time
  2762. :type 'boolean)
  2763. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2764. 'org-read-date-popup-calendar)
  2765. (make-obsolete-variable
  2766. 'org-read-date-minibuffer-setup-hook
  2767. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2768. (defcustom org-read-date-minibuffer-setup-hook nil
  2769. "Hook to be used to set up keys for the date/time interface.
  2770. Add key definitions to `minibuffer-local-map', which will be a
  2771. temporary copy.
  2772. WARNING: This option is obsolete, you should use
  2773. `org-read-date-minibuffer-local-map' to set up keys."
  2774. :group 'org-time
  2775. :type 'hook)
  2776. (defcustom org-extend-today-until 0
  2777. "The hour when your day really ends. Must be an integer.
  2778. This has influence for the following applications:
  2779. - When switching the agenda to \"today\". It it is still earlier than
  2780. the time given here, the day recognized as TODAY is actually yesterday.
  2781. - When a date is read from the user and it is still before the time given
  2782. here, the current date and time will be assumed to be yesterday, 23:59.
  2783. Also, timestamps inserted in capture templates follow this rule.
  2784. IMPORTANT: This is a feature whose implementation is and likely will
  2785. remain incomplete. Really, it is only here because past midnight seems to
  2786. be the favorite working time of John Wiegley :-)"
  2787. :group 'org-time
  2788. :type 'integer)
  2789. (defcustom org-use-effective-time nil
  2790. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2791. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2792. \"effective time\" of any timestamps between midnight and 8am will be
  2793. 23:59 of the previous day."
  2794. :group 'org-time
  2795. :version "24.1"
  2796. :type 'boolean)
  2797. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2798. "When non-nil, use the last clock out time for `org-todo'.
  2799. Note that this option has precedence over the combined use of
  2800. `org-use-effective-time' and `org-extend-today-until'."
  2801. :group 'org-time
  2802. :version "24.4"
  2803. :package-version '(Org . "8.0")
  2804. :type 'boolean)
  2805. (defcustom org-edit-timestamp-down-means-later nil
  2806. "Non-nil means S-down will increase the time in a time stamp.
  2807. When nil, S-up will increase."
  2808. :group 'org-time
  2809. :type 'boolean)
  2810. (defcustom org-calendar-follow-timestamp-change t
  2811. "Non-nil means make the calendar window follow timestamp changes.
  2812. When a timestamp is modified and the calendar window is visible, it will be
  2813. moved to the new date."
  2814. :group 'org-time
  2815. :type 'boolean)
  2816. (defgroup org-tags nil
  2817. "Options concerning tags in Org-mode."
  2818. :tag "Org Tags"
  2819. :group 'org)
  2820. (defcustom org-tag-alist nil
  2821. "List of tags allowed in Org-mode files.
  2822. When this list is nil, Org-mode will base TAG input on what is already in the
  2823. buffer.
  2824. The value of this variable is an alist, the car of each entry must be a
  2825. keyword as a string, the cdr may be a character that is used to select
  2826. that tag through the fast-tag-selection interface.
  2827. See the manual for details."
  2828. :group 'org-tags
  2829. :type '(repeat
  2830. (choice
  2831. (cons (string :tag "Tag name")
  2832. (character :tag "Access char"))
  2833. (list :tag "Start radio group"
  2834. (const :startgroup)
  2835. (option (string :tag "Group description")))
  2836. (list :tag "Group tags delimiter"
  2837. (const :grouptags))
  2838. (list :tag "End radio group"
  2839. (const :endgroup)
  2840. (option (string :tag "Group description")))
  2841. (const :tag "New line" (:newline)))))
  2842. (defcustom org-tag-persistent-alist nil
  2843. "List of tags that will always appear in all Org-mode files.
  2844. This is in addition to any in buffer settings or customizations
  2845. of `org-tag-alist'.
  2846. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2847. The value of this variable is an alist, the car of each entry must be a
  2848. keyword as a string, the cdr may be a character that is used to select
  2849. that tag through the fast-tag-selection interface.
  2850. See the manual for details.
  2851. To disable these tags on a per-file basis, insert anywhere in the file:
  2852. #+STARTUP: noptag"
  2853. :group 'org-tags
  2854. :type '(repeat
  2855. (choice
  2856. (cons (string :tag "Tag name")
  2857. (character :tag "Access char"))
  2858. (const :tag "Start radio group" (:startgroup))
  2859. (const :tag "Group tags delimiter" (:grouptags))
  2860. (const :tag "End radio group" (:endgroup))
  2861. (const :tag "New line" (:newline)))))
  2862. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2863. "If non-nil, always offer completion for all tags of all agenda files.
  2864. Instead of customizing this variable directly, you might want to
  2865. set it locally for capture buffers, because there no list of
  2866. tags in that file can be created dynamically (there are none).
  2867. (add-hook 'org-capture-mode-hook
  2868. (lambda ()
  2869. (set (make-local-variable
  2870. 'org-complete-tags-always-offer-all-agenda-tags)
  2871. t)))"
  2872. :group 'org-tags
  2873. :version "24.1"
  2874. :type 'boolean)
  2875. (defvar org-file-tags nil
  2876. "List of tags that can be inherited by all entries in the file.
  2877. The tags will be inherited if the variable `org-use-tag-inheritance'
  2878. says they should be.
  2879. This variable is populated from #+FILETAGS lines.")
  2880. (defcustom org-use-fast-tag-selection 'auto
  2881. "Non-nil means use fast tag selection scheme.
  2882. This is a special interface to select and deselect tags with single keys.
  2883. When nil, fast selection is never used.
  2884. When the symbol `auto', fast selection is used if and only if selection
  2885. characters for tags have been configured, either through the variable
  2886. `org-tag-alist' or through a #+TAGS line in the buffer.
  2887. When t, fast selection is always used and selection keys are assigned
  2888. automatically if necessary."
  2889. :group 'org-tags
  2890. :type '(choice
  2891. (const :tag "Always" t)
  2892. (const :tag "Never" nil)
  2893. (const :tag "When selection characters are configured" auto)))
  2894. (defcustom org-fast-tag-selection-single-key nil
  2895. "Non-nil means fast tag selection exits after first change.
  2896. When nil, you have to press RET to exit it.
  2897. During fast tag selection, you can toggle this flag with `C-c'.
  2898. This variable can also have the value `expert'. In this case, the window
  2899. displaying the tags menu is not even shown, until you press C-c again."
  2900. :group 'org-tags
  2901. :type '(choice
  2902. (const :tag "No" nil)
  2903. (const :tag "Yes" t)
  2904. (const :tag "Expert" expert)))
  2905. (defvar org-fast-tag-selection-include-todo nil
  2906. "Non-nil means fast tags selection interface will also offer TODO states.
  2907. This is an undocumented feature, you should not rely on it.")
  2908. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2909. "The column to which tags should be indented in a headline.
  2910. If this number is positive, it specifies the column. If it is negative,
  2911. it means that the tags should be flushright to that column. For example,
  2912. -80 works well for a normal 80 character screen.
  2913. When 0, place tags directly after headline text, with only one space in
  2914. between."
  2915. :group 'org-tags
  2916. :type 'integer)
  2917. (defcustom org-auto-align-tags t
  2918. "Non-nil keeps tags aligned when modifying headlines.
  2919. Some operations (i.e. demoting) change the length of a headline and
  2920. therefore shift the tags around. With this option turned on, after
  2921. each such operation the tags are again aligned to `org-tags-column'."
  2922. :group 'org-tags
  2923. :type 'boolean)
  2924. (defcustom org-use-tag-inheritance t
  2925. "Non-nil means tags in levels apply also for sublevels.
  2926. When nil, only the tags directly given in a specific line apply there.
  2927. This may also be a list of tags that should be inherited, or a regexp that
  2928. matches tags that should be inherited. Additional control is possible
  2929. with the variable `org-tags-exclude-from-inheritance' which gives an
  2930. explicit list of tags to be excluded from inheritance, even if the value of
  2931. `org-use-tag-inheritance' would select it for inheritance.
  2932. If this option is t, a match early-on in a tree can lead to a large
  2933. number of matches in the subtree when constructing the agenda or creating
  2934. a sparse tree. If you only want to see the first match in a tree during
  2935. a search, check out the variable `org-tags-match-list-sublevels'."
  2936. :group 'org-tags
  2937. :type '(choice
  2938. (const :tag "Not" nil)
  2939. (const :tag "Always" t)
  2940. (repeat :tag "Specific tags" (string :tag "Tag"))
  2941. (regexp :tag "Tags matched by regexp")))
  2942. (defcustom org-tags-exclude-from-inheritance nil
  2943. "List of tags that should never be inherited.
  2944. This is a way to exclude a few tags from inheritance. For way to do
  2945. the opposite, to actively allow inheritance for selected tags,
  2946. see the variable `org-use-tag-inheritance'."
  2947. :group 'org-tags
  2948. :type '(repeat (string :tag "Tag")))
  2949. (defun org-tag-inherit-p (tag)
  2950. "Check if TAG is one that should be inherited."
  2951. (cond
  2952. ((member tag org-tags-exclude-from-inheritance) nil)
  2953. ((eq org-use-tag-inheritance t) t)
  2954. ((not org-use-tag-inheritance) nil)
  2955. ((stringp org-use-tag-inheritance)
  2956. (string-match org-use-tag-inheritance tag))
  2957. ((listp org-use-tag-inheritance)
  2958. (member tag org-use-tag-inheritance))
  2959. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2960. (defcustom org-tags-match-list-sublevels t
  2961. "Non-nil means list also sublevels of headlines matching a search.
  2962. This variable applies to tags/property searches, and also to stuck
  2963. projects because this search is based on a tags match as well.
  2964. When set to the symbol `indented', sublevels are indented with
  2965. leading dots.
  2966. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2967. the sublevels of a headline matching a tag search often also match
  2968. the same search. Listing all of them can create very long lists.
  2969. Setting this variable to nil causes subtrees of a match to be skipped.
  2970. This variable is semi-obsolete and probably should always be true. It
  2971. is better to limit inheritance to certain tags using the variables
  2972. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2973. :group 'org-tags
  2974. :type '(choice
  2975. (const :tag "No, don't list them" nil)
  2976. (const :tag "Yes, do list them" t)
  2977. (const :tag "List them, indented with leading dots" indented)))
  2978. (defcustom org-tags-sort-function nil
  2979. "When set, tags are sorted using this function as a comparator."
  2980. :group 'org-tags
  2981. :type '(choice
  2982. (const :tag "No sorting" nil)
  2983. (const :tag "Alphabetical" string<)
  2984. (const :tag "Reverse alphabetical" string>)
  2985. (function :tag "Custom function" nil)))
  2986. (defvar org-tags-history nil
  2987. "History of minibuffer reads for tags.")
  2988. (defvar org-last-tags-completion-table nil
  2989. "The last used completion table for tags.")
  2990. (defvar org-after-tags-change-hook nil
  2991. "Hook that is run after the tags in a line have changed.")
  2992. (defgroup org-properties nil
  2993. "Options concerning properties in Org-mode."
  2994. :tag "Org Properties"
  2995. :group 'org)
  2996. (defcustom org-property-format "%-10s %s"
  2997. "How property key/value pairs should be formatted by `indent-line'.
  2998. When `indent-line' hits a property definition, it will format the line
  2999. according to this format, mainly to make sure that the values are
  3000. lined-up with respect to each other."
  3001. :group 'org-properties
  3002. :type 'string)
  3003. (defcustom org-properties-postprocess-alist nil
  3004. "Alist of properties and functions to adjust inserted values.
  3005. Elements of this alist must be of the form
  3006. ([string] [function])
  3007. where [string] must be a property name and [function] must be a
  3008. lambda expression: this lambda expression must take one argument,
  3009. the value to adjust, and return the new value as a string.
  3010. For example, this element will allow the property \"Remaining\"
  3011. to be updated wrt the relation between the \"Effort\" property
  3012. and the clock summary:
  3013. ((\"Remaining\" (lambda(value)
  3014. (let ((clocksum (org-clock-sum-current-item))
  3015. (effort (org-duration-string-to-minutes
  3016. (org-entry-get (point) \"Effort\"))))
  3017. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3018. :group 'org-properties
  3019. :version "24.1"
  3020. :type '(alist :key-type (string :tag "Property")
  3021. :value-type (function :tag "Function")))
  3022. (defcustom org-use-property-inheritance nil
  3023. "Non-nil means properties apply also for sublevels.
  3024. This setting is chiefly used during property searches. Turning it on can
  3025. cause significant overhead when doing a search, which is why it is not
  3026. on by default.
  3027. When nil, only the properties directly given in the current entry count.
  3028. When t, every property is inherited. The value may also be a list of
  3029. properties that should have inheritance, or a regular expression matching
  3030. properties that should be inherited.
  3031. However, note that some special properties use inheritance under special
  3032. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3033. and the properties ending in \"_ALL\" when they are used as descriptor
  3034. for valid values of a property.
  3035. Note for programmers:
  3036. When querying an entry with `org-entry-get', you can control if inheritance
  3037. should be used. By default, `org-entry-get' looks only at the local
  3038. properties. You can request inheritance by setting the inherit argument
  3039. to t (to force inheritance) or to `selective' (to respect the setting
  3040. in this variable)."
  3041. :group 'org-properties
  3042. :type '(choice
  3043. (const :tag "Not" nil)
  3044. (const :tag "Always" t)
  3045. (repeat :tag "Specific properties" (string :tag "Property"))
  3046. (regexp :tag "Properties matched by regexp")))
  3047. (defun org-property-inherit-p (property)
  3048. "Check if PROPERTY is one that should be inherited."
  3049. (cond
  3050. ((eq org-use-property-inheritance t) t)
  3051. ((not org-use-property-inheritance) nil)
  3052. ((stringp org-use-property-inheritance)
  3053. (string-match org-use-property-inheritance property))
  3054. ((listp org-use-property-inheritance)
  3055. (member property org-use-property-inheritance))
  3056. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3057. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3058. "The default column format, if no other format has been defined.
  3059. This variable can be set on the per-file basis by inserting a line
  3060. #+COLUMNS: %25ITEM ....."
  3061. :group 'org-properties
  3062. :type 'string)
  3063. (defcustom org-columns-ellipses ".."
  3064. "The ellipses to be used when a field in column view is truncated.
  3065. When this is the empty string, as many characters as possible are shown,
  3066. but then there will be no visual indication that the field has been truncated.
  3067. When this is a string of length N, the last N characters of a truncated
  3068. field are replaced by this string. If the column is narrower than the
  3069. ellipses string, only part of the ellipses string will be shown."
  3070. :group 'org-properties
  3071. :type 'string)
  3072. (defcustom org-columns-modify-value-for-display-function nil
  3073. "Function that modifies values for display in column view.
  3074. For example, it can be used to cut out a certain part from a time stamp.
  3075. The function must take 2 arguments:
  3076. column-title The title of the column (*not* the property name)
  3077. value The value that should be modified.
  3078. The function should return the value that should be displayed,
  3079. or nil if the normal value should be used."
  3080. :group 'org-properties
  3081. :type '(choice (const nil) (function)))
  3082. (defcustom org-effort-property "Effort"
  3083. "The property that is being used to keep track of effort estimates.
  3084. Effort estimates given in this property need to have the format H:MM."
  3085. :group 'org-properties
  3086. :group 'org-progress
  3087. :type '(string :tag "Property"))
  3088. (defconst org-global-properties-fixed
  3089. '(("VISIBILITY_ALL" . "folded children content all")
  3090. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3091. "List of property/value pairs that can be inherited by any entry.
  3092. These are fixed values, for the preset properties. The user variable
  3093. that can be used to add to this list is `org-global-properties'.
  3094. The entries in this list are cons cells where the car is a property
  3095. name and cdr is a string with the value. If the value represents
  3096. multiple items like an \"_ALL\" property, separate the items by
  3097. spaces.")
  3098. (defcustom org-global-properties nil
  3099. "List of property/value pairs that can be inherited by any entry.
  3100. This list will be combined with the constant `org-global-properties-fixed'.
  3101. The entries in this list are cons cells where the car is a property
  3102. name and cdr is a string with the value.
  3103. You can set buffer-local values for the same purpose in the variable
  3104. `org-file-properties' this by adding lines like
  3105. #+PROPERTY: NAME VALUE"
  3106. :group 'org-properties
  3107. :type '(repeat
  3108. (cons (string :tag "Property")
  3109. (string :tag "Value"))))
  3110. (defvar org-file-properties nil
  3111. "List of property/value pairs that can be inherited by any entry.
  3112. Valid for the current buffer.
  3113. This variable is populated from #+PROPERTY lines.")
  3114. (make-variable-buffer-local 'org-file-properties)
  3115. (defgroup org-agenda nil
  3116. "Options concerning agenda views in Org-mode."
  3117. :tag "Org Agenda"
  3118. :group 'org)
  3119. (defvar org-category nil
  3120. "Variable used by org files to set a category for agenda display.
  3121. Such files should use a file variable to set it, for example
  3122. # -*- mode: org; org-category: \"ELisp\"
  3123. or contain a special line
  3124. #+CATEGORY: ELisp
  3125. If the file does not specify a category, then file's base name
  3126. is used instead.")
  3127. (make-variable-buffer-local 'org-category)
  3128. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3129. (defcustom org-agenda-files nil
  3130. "The files to be used for agenda display.
  3131. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3132. \\[org-remove-file]. You can also use customize to edit the list.
  3133. If an entry is a directory, all files in that directory that are matched by
  3134. `org-agenda-file-regexp' will be part of the file list.
  3135. If the value of the variable is not a list but a single file name, then
  3136. the list of agenda files is actually stored and maintained in that file, one
  3137. agenda file per line. In this file paths can be given relative to
  3138. `org-directory'. Tilde expansion and environment variable substitution
  3139. are also made."
  3140. :group 'org-agenda
  3141. :type '(choice
  3142. (repeat :tag "List of files and directories" file)
  3143. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3144. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3145. "Regular expression to match files for `org-agenda-files'.
  3146. If any element in the list in that variable contains a directory instead
  3147. of a normal file, all files in that directory that are matched by this
  3148. regular expression will be included."
  3149. :group 'org-agenda
  3150. :type 'regexp)
  3151. (defcustom org-agenda-text-search-extra-files nil
  3152. "List of extra files to be searched by text search commands.
  3153. These files will be searched in addition to the agenda files by the
  3154. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3155. Note that these files will only be searched for text search commands,
  3156. not for the other agenda views like todo lists, tag searches or the weekly
  3157. agenda. This variable is intended to list notes and possibly archive files
  3158. that should also be searched by these two commands.
  3159. In fact, if the first element in the list is the symbol `agenda-archives',
  3160. then all archive files of all agenda files will be added to the search
  3161. scope."
  3162. :group 'org-agenda
  3163. :type '(set :greedy t
  3164. (const :tag "Agenda Archives" agenda-archives)
  3165. (repeat :inline t (file))))
  3166. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3167. 'org-agenda-text-search-extra-files)
  3168. (defcustom org-agenda-skip-unavailable-files nil
  3169. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3170. A nil value means to remove them, after a query, from the list."
  3171. :group 'org-agenda
  3172. :type 'boolean)
  3173. (defcustom org-calendar-to-agenda-key [?c]
  3174. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3175. The command `org-calendar-goto-agenda' will be bound to this key. The
  3176. default is the character `c' because then `c' can be used to switch back and
  3177. forth between agenda and calendar."
  3178. :group 'org-agenda
  3179. :type 'sexp)
  3180. (defcustom org-calendar-insert-diary-entry-key [?i]
  3181. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3182. This option is irrelevant until `org-agenda-diary-file' has been configured
  3183. to point to an Org-mode file. When that is the case, the command
  3184. `org-agenda-diary-entry' will be bound to the key given here, by default
  3185. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3186. if you want to continue doing this, you need to change this to a different
  3187. key."
  3188. :group 'org-agenda
  3189. :type 'sexp)
  3190. (defcustom org-agenda-diary-file 'diary-file
  3191. "File to which to add new entries with the `i' key in agenda and calendar.
  3192. When this is the symbol `diary-file', the functionality in the Emacs
  3193. calendar will be used to add entries to the `diary-file'. But when this
  3194. points to a file, `org-agenda-diary-entry' will be used instead."
  3195. :group 'org-agenda
  3196. :type '(choice
  3197. (const :tag "The standard Emacs diary file" diary-file)
  3198. (file :tag "Special Org file diary entries")))
  3199. (eval-after-load "calendar"
  3200. '(progn
  3201. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3202. 'org-calendar-goto-agenda)
  3203. (add-hook 'calendar-mode-hook
  3204. (lambda ()
  3205. (unless (eq org-agenda-diary-file 'diary-file)
  3206. (define-key calendar-mode-map
  3207. org-calendar-insert-diary-entry-key
  3208. 'org-agenda-diary-entry))))))
  3209. (defgroup org-latex nil
  3210. "Options for embedding LaTeX code into Org-mode."
  3211. :tag "Org LaTeX"
  3212. :group 'org)
  3213. (defcustom org-format-latex-options
  3214. '(:foreground default :background default :scale 1.0
  3215. :html-foreground "Black" :html-background "Transparent"
  3216. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3217. "Options for creating images from LaTeX fragments.
  3218. This is a property list with the following properties:
  3219. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3220. `default' means use the foreground of the default face.
  3221. `auto' means use the foreground from the text face.
  3222. :background the background color, or \"Transparent\".
  3223. `default' means use the background of the default face.
  3224. `auto' means use the background from the text face.
  3225. :scale a scaling factor for the size of the images, to get more pixels
  3226. :html-foreground, :html-background, :html-scale
  3227. the same numbers for HTML export.
  3228. :matchers a list indicating which matchers should be used to
  3229. find LaTeX fragments. Valid members of this list are:
  3230. \"begin\" find environments
  3231. \"$1\" find single characters surrounded by $.$
  3232. \"$\" find math expressions surrounded by $...$
  3233. \"$$\" find math expressions surrounded by $$....$$
  3234. \"\\(\" find math expressions surrounded by \\(...\\)
  3235. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3236. :group 'org-latex
  3237. :type 'plist)
  3238. (defcustom org-format-latex-signal-error t
  3239. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3240. When nil, just push out a message."
  3241. :group 'org-latex
  3242. :version "24.1"
  3243. :type 'boolean)
  3244. (defcustom org-latex-to-mathml-jar-file nil
  3245. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3246. Use this to specify additional executable file say a jar file.
  3247. When using MathToWeb as the converter, specify the full-path to
  3248. your mathtoweb.jar file."
  3249. :group 'org-latex
  3250. :version "24.1"
  3251. :type '(choice
  3252. (const :tag "None" nil)
  3253. (file :tag "JAR file" :must-match t)))
  3254. (defcustom org-latex-to-mathml-convert-command nil
  3255. "Command to convert LaTeX fragments to MathML.
  3256. Replace format-specifiers in the command as noted below and use
  3257. `shell-command' to convert LaTeX to MathML.
  3258. %j: Executable file in fully expanded form as specified by
  3259. `org-latex-to-mathml-jar-file'.
  3260. %I: Input LaTeX file in fully expanded form
  3261. %o: Output MathML file
  3262. This command is used by `org-create-math-formula'.
  3263. When using MathToWeb as the converter, set this to
  3264. \"java -jar %j -unicode -force -df %o %I\"."
  3265. :group 'org-latex
  3266. :version "24.1"
  3267. :type '(choice
  3268. (const :tag "None" nil)
  3269. (string :tag "\nShell command")))
  3270. (defcustom org-latex-create-formula-image-program 'dvipng
  3271. "Program to convert LaTeX fragments with.
  3272. dvipng Process the LaTeX fragments to dvi file, then convert
  3273. dvi files to png files using dvipng.
  3274. This will also include processing of non-math environments.
  3275. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3276. to convert pdf files to png files"
  3277. :group 'org-latex
  3278. :version "24.1"
  3279. :type '(choice
  3280. (const :tag "dvipng" dvipng)
  3281. (const :tag "imagemagick" imagemagick)))
  3282. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3283. "Path to store latex preview images.
  3284. A relative path here creates many directories relative to the
  3285. processed org files paths. An absolute path puts all preview
  3286. images at the same place."
  3287. :group 'org-latex
  3288. :version "24.3"
  3289. :type 'string)
  3290. (defun org-format-latex-mathml-available-p ()
  3291. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3292. (save-match-data
  3293. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3294. org-latex-to-mathml-convert-command)
  3295. (let ((executable (car (split-string
  3296. org-latex-to-mathml-convert-command))))
  3297. (when (executable-find executable)
  3298. (if (string-match
  3299. "%j" org-latex-to-mathml-convert-command)
  3300. (file-readable-p org-latex-to-mathml-jar-file)
  3301. t))))))
  3302. (defcustom org-format-latex-header "\\documentclass{article}
  3303. \\usepackage[usenames]{color}
  3304. \[PACKAGES]
  3305. \[DEFAULT-PACKAGES]
  3306. \\pagestyle{empty} % do not remove
  3307. % The settings below are copied from fullpage.sty
  3308. \\setlength{\\textwidth}{\\paperwidth}
  3309. \\addtolength{\\textwidth}{-3cm}
  3310. \\setlength{\\oddsidemargin}{1.5cm}
  3311. \\addtolength{\\oddsidemargin}{-2.54cm}
  3312. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3313. \\setlength{\\textheight}{\\paperheight}
  3314. \\addtolength{\\textheight}{-\\headheight}
  3315. \\addtolength{\\textheight}{-\\headsep}
  3316. \\addtolength{\\textheight}{-\\footskip}
  3317. \\addtolength{\\textheight}{-3cm}
  3318. \\setlength{\\topmargin}{1.5cm}
  3319. \\addtolength{\\topmargin}{-2.54cm}"
  3320. "The document header used for processing LaTeX fragments.
  3321. It is imperative that this header make sure that no page number
  3322. appears on the page. The package defined in the variables
  3323. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3324. will either replace the placeholder \"[PACKAGES]\" in this
  3325. header, or they will be appended."
  3326. :group 'org-latex
  3327. :type 'string)
  3328. (defun org-set-packages-alist (var val)
  3329. "Set the packages alist and make sure it has 3 elements per entry."
  3330. (set var (mapcar (lambda (x)
  3331. (if (and (consp x) (= (length x) 2))
  3332. (list (car x) (nth 1 x) t)
  3333. x))
  3334. val)))
  3335. (defun org-get-packages-alist (var)
  3336. "Get the packages alist and make sure it has 3 elements per entry."
  3337. (mapcar (lambda (x)
  3338. (if (and (consp x) (= (length x) 2))
  3339. (list (car x) (nth 1 x) t)
  3340. x))
  3341. (default-value var)))
  3342. (defcustom org-latex-default-packages-alist
  3343. '(("AUTO" "inputenc" t)
  3344. ("T1" "fontenc" t)
  3345. ("" "fixltx2e" nil)
  3346. ("" "graphicx" t)
  3347. ("" "longtable" nil)
  3348. ("" "float" nil)
  3349. ("" "wrapfig" nil)
  3350. ("" "rotating" nil)
  3351. ("normalem" "ulem" t)
  3352. ("" "amsmath" t)
  3353. ("" "textcomp" t)
  3354. ("" "marvosym" t)
  3355. ("" "wasysym" t)
  3356. ("" "amssymb" t)
  3357. ("" "hyperref" nil)
  3358. "\\tolerance=1000")
  3359. "Alist of default packages to be inserted in the header.
  3360. Change this only if one of the packages here causes an
  3361. incompatibility with another package you are using.
  3362. The packages in this list are needed by one part or another of
  3363. Org mode to function properly:
  3364. - inputenc, fontenc: for basic font and character selection
  3365. - fixltx2e: Important patches of LaTeX itself
  3366. - graphicx: for including images
  3367. - longtable: For multipage tables
  3368. - float, wrapfig: for figure placement
  3369. - rotating: for sideways figures and tables
  3370. - ulem: for underline and strike-through
  3371. - amsmath: for subscript and superscript and math environments
  3372. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3373. for interpreting the entities in `org-entities'. You can skip
  3374. some of these packages if you don't use any of their symbols.
  3375. - hyperref: for cross references
  3376. Therefore you should not modify this variable unless you know
  3377. what you are doing. The one reason to change it anyway is that
  3378. you might be loading some other package that conflicts with one
  3379. of the default packages. Each cell is of the format
  3380. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3381. the package also needs to be included when compiling LaTeX
  3382. snippets into images for inclusion into non-LaTeX output."
  3383. :group 'org-latex
  3384. :group 'org-export-latex
  3385. :set 'org-set-packages-alist
  3386. :get 'org-get-packages-alist
  3387. :version "24.1"
  3388. :type '(repeat
  3389. (choice
  3390. (list :tag "options/package pair"
  3391. (string :tag "options")
  3392. (string :tag "package")
  3393. (boolean :tag "Snippet"))
  3394. (string :tag "A line of LaTeX"))))
  3395. (defcustom org-latex-packages-alist nil
  3396. "Alist of packages to be inserted in every LaTeX header.
  3397. These will be inserted after `org-latex-default-packages-alist'.
  3398. Each cell is of the format:
  3399. \(\"options\" \"package\" snippet-flag)
  3400. SNIPPET-FLAG, when t, indicates that this package is also needed
  3401. when turning LaTeX snippets into images for inclusion into
  3402. non-LaTeX output.
  3403. Make sure that you only list packages here which:
  3404. - you want in every file
  3405. - do not conflict with the setup in `org-format-latex-header'.
  3406. - do not conflict with the default packages in
  3407. `org-latex-default-packages-alist'."
  3408. :group 'org-latex
  3409. :group 'org-export-latex
  3410. :set 'org-set-packages-alist
  3411. :get 'org-get-packages-alist
  3412. :type '(repeat
  3413. (choice
  3414. (list :tag "options/package pair"
  3415. (string :tag "options")
  3416. (string :tag "package")
  3417. (boolean :tag "Snippet"))
  3418. (string :tag "A line of LaTeX"))))
  3419. (defgroup org-appearance nil
  3420. "Settings for Org-mode appearance."
  3421. :tag "Org Appearance"
  3422. :group 'org)
  3423. (defcustom org-level-color-stars-only nil
  3424. "Non-nil means fontify only the stars in each headline.
  3425. When nil, the entire headline is fontified.
  3426. Changing it requires restart of `font-lock-mode' to become effective
  3427. also in regions already fontified."
  3428. :group 'org-appearance
  3429. :type 'boolean)
  3430. (defcustom org-hide-leading-stars nil
  3431. "Non-nil means hide the first N-1 stars in a headline.
  3432. This works by using the face `org-hide' for these stars. This
  3433. face is white for a light background, and black for a dark
  3434. background. You may have to customize the face `org-hide' to
  3435. make this work.
  3436. Changing it requires restart of `font-lock-mode' to become effective
  3437. also in regions already fontified.
  3438. You may also set this on a per-file basis by adding one of the following
  3439. lines to the buffer:
  3440. #+STARTUP: hidestars
  3441. #+STARTUP: showstars"
  3442. :group 'org-appearance
  3443. :type 'boolean)
  3444. (defcustom org-hidden-keywords nil
  3445. "List of symbols corresponding to keywords to be hidden the org buffer.
  3446. For example, a value '(title) for this list will make the document's title
  3447. appear in the buffer without the initial #+TITLE: keyword."
  3448. :group 'org-appearance
  3449. :version "24.1"
  3450. :type '(set (const :tag "#+AUTHOR" author)
  3451. (const :tag "#+DATE" date)
  3452. (const :tag "#+EMAIL" email)
  3453. (const :tag "#+TITLE" title)))
  3454. (defcustom org-custom-properties nil
  3455. "List of properties (as strings) with a special meaning.
  3456. The default use of these custom properties is to let the user
  3457. hide them with `org-toggle-custom-properties-visibility'."
  3458. :group 'org-properties
  3459. :group 'org-appearance
  3460. :version "24.3"
  3461. :type '(repeat (string :tag "Property Name")))
  3462. (defcustom org-fontify-done-headline nil
  3463. "Non-nil means change the face of a headline if it is marked DONE.
  3464. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3465. When this is non-nil, the headline after the keyword is set to the
  3466. `org-headline-done' as an additional indication."
  3467. :group 'org-appearance
  3468. :type 'boolean)
  3469. (defcustom org-fontify-emphasized-text t
  3470. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3471. Changing this variable requires a restart of Emacs to take effect."
  3472. :group 'org-appearance
  3473. :type 'boolean)
  3474. (defcustom org-fontify-whole-heading-line nil
  3475. "Non-nil means fontify the whole line for headings.
  3476. This is useful when setting a background color for the
  3477. org-level-* faces."
  3478. :group 'org-appearance
  3479. :type 'boolean)
  3480. (defcustom org-highlight-latex-and-related nil
  3481. "Non-nil means highlight LaTeX related syntax in the buffer.
  3482. When non nil, the value should be a list containing any of the
  3483. following symbols:
  3484. `latex' Highlight LaTeX snippets and environments.
  3485. `script' Highlight subscript and superscript.
  3486. `entities' Highlight entities."
  3487. :group 'org-appearance
  3488. :version "24.4"
  3489. :package-version '(Org . "8.0")
  3490. :type '(choice
  3491. (const :tag "No highlighting" nil)
  3492. (set :greedy t :tag "Highlight"
  3493. (const :tag "LaTeX snippets and environments" latex)
  3494. (const :tag "Subscript and superscript" script)
  3495. (const :tag "Entities" entities))))
  3496. (defcustom org-hide-emphasis-markers nil
  3497. "Non-nil mean font-lock should hide the emphasis marker characters."
  3498. :group 'org-appearance
  3499. :type 'boolean)
  3500. (defcustom org-pretty-entities nil
  3501. "Non-nil means show entities as UTF8 characters.
  3502. When nil, the \\name form remains in the buffer."
  3503. :group 'org-appearance
  3504. :version "24.1"
  3505. :type 'boolean)
  3506. (defcustom org-pretty-entities-include-sub-superscripts t
  3507. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3508. :group 'org-appearance
  3509. :version "24.1"
  3510. :type 'boolean)
  3511. (defvar org-emph-re nil
  3512. "Regular expression for matching emphasis.
  3513. After a match, the match groups contain these elements:
  3514. 0 The match of the full regular expression, including the characters
  3515. before and after the proper match
  3516. 1 The character before the proper match, or empty at beginning of line
  3517. 2 The proper match, including the leading and trailing markers
  3518. 3 The leading marker like * or /, indicating the type of highlighting
  3519. 4 The text between the emphasis markers, not including the markers
  3520. 5 The character after the match, empty at the end of a line")
  3521. (defvar org-verbatim-re nil
  3522. "Regular expression for matching verbatim text.")
  3523. (defvar org-emphasis-regexp-components) ; defined just below
  3524. (defvar org-emphasis-alist) ; defined just below
  3525. (defun org-set-emph-re (var val)
  3526. "Set variable and compute the emphasis regular expression."
  3527. (set var val)
  3528. (when (and (boundp 'org-emphasis-alist)
  3529. (boundp 'org-emphasis-regexp-components)
  3530. org-emphasis-alist org-emphasis-regexp-components)
  3531. (let* ((e org-emphasis-regexp-components)
  3532. (pre (car e))
  3533. (post (nth 1 e))
  3534. (border (nth 2 e))
  3535. (body (nth 3 e))
  3536. (nl (nth 4 e))
  3537. (body1 (concat body "*?"))
  3538. (markers (mapconcat 'car org-emphasis-alist ""))
  3539. (vmarkers (mapconcat
  3540. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3541. org-emphasis-alist "")))
  3542. ;; make sure special characters appear at the right position in the class
  3543. (if (string-match "\\^" markers)
  3544. (setq markers (concat (replace-match "" t t markers) "^")))
  3545. (if (string-match "-" markers)
  3546. (setq markers (concat (replace-match "" t t markers) "-")))
  3547. (if (string-match "\\^" vmarkers)
  3548. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3549. (if (string-match "-" vmarkers)
  3550. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3551. (if (> nl 0)
  3552. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3553. (int-to-string nl) "\\}")))
  3554. ;; Make the regexp
  3555. (setq org-emph-re
  3556. (concat "\\([" pre "]\\|^\\)"
  3557. "\\("
  3558. "\\([" markers "]\\)"
  3559. "\\("
  3560. "[^" border "]\\|"
  3561. "[^" border "]"
  3562. body1
  3563. "[^" border "]"
  3564. "\\)"
  3565. "\\3\\)"
  3566. "\\([" post "]\\|$\\)"))
  3567. (setq org-verbatim-re
  3568. (concat "\\([" pre "]\\|^\\)"
  3569. "\\("
  3570. "\\([" vmarkers "]\\)"
  3571. "\\("
  3572. "[^" border "]\\|"
  3573. "[^" border "]"
  3574. body1
  3575. "[^" border "]"
  3576. "\\)"
  3577. "\\3\\)"
  3578. "\\([" post "]\\|$\\)")))))
  3579. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3580. ;; set this option proved cumbersome. See this message/thread:
  3581. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3582. (defvar org-emphasis-regexp-components
  3583. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3584. "Components used to build the regular expression for emphasis.
  3585. This is a list with five entries. Terminology: In an emphasis string
  3586. like \" *strong word* \", we call the initial space PREMATCH, the final
  3587. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3588. and \"trong wor\" is the body. The different components in this variable
  3589. specify what is allowed/forbidden in each part:
  3590. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3591. post Chars allowed as postmatch. End of line will be allowed too.
  3592. border The chars *forbidden* as border characters.
  3593. body-regexp A regexp like \".\" to match a body character. Don't use
  3594. non-shy groups here, and don't allow newline here.
  3595. newline The maximum number of newlines allowed in an emphasis exp.
  3596. You need to reload Org or to restart Emacs after customizing this.")
  3597. (defcustom org-emphasis-alist
  3598. `(("*" bold)
  3599. ("/" italic)
  3600. ("_" underline)
  3601. ("=" org-code verbatim)
  3602. ("~" org-verbatim verbatim)
  3603. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3604. "Alist of characters and faces to emphasize text.
  3605. Text starting and ending with a special character will be emphasized,
  3606. for example *bold*, _underlined_ and /italic/. This variable sets the
  3607. marker characters and the face to be used by font-lock for highlighting
  3608. in Org-mode Emacs buffers.
  3609. You need to reload Org or to restart Emacs after customizing this."
  3610. :group 'org-appearance
  3611. :set 'org-set-emph-re
  3612. :version "24.4"
  3613. :package-version '(Org . "8.0")
  3614. :type '(repeat
  3615. (list
  3616. (string :tag "Marker character")
  3617. (choice
  3618. (face :tag "Font-lock-face")
  3619. (plist :tag "Face property list"))
  3620. (option (const verbatim)))))
  3621. (defvar org-protecting-blocks
  3622. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3623. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3624. This is needed for font-lock setup.")
  3625. ;;; Miscellaneous options
  3626. (defgroup org-completion nil
  3627. "Completion in Org-mode."
  3628. :tag "Org Completion"
  3629. :group 'org)
  3630. (defcustom org-completion-use-ido nil
  3631. "Non-nil means use ido completion wherever possible.
  3632. Note that `ido-mode' must be active for this variable to be relevant.
  3633. If you decide to turn this variable on, you might well want to turn off
  3634. `org-outline-path-complete-in-steps'.
  3635. See also `org-completion-use-iswitchb'."
  3636. :group 'org-completion
  3637. :type 'boolean)
  3638. (defcustom org-completion-use-iswitchb nil
  3639. "Non-nil means use iswitchb completion wherever possible.
  3640. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3641. If you decide to turn this variable on, you might well want to turn off
  3642. `org-outline-path-complete-in-steps'.
  3643. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3644. :group 'org-completion
  3645. :type 'boolean)
  3646. (defcustom org-completion-fallback-command 'hippie-expand
  3647. "The expansion command called by \\[pcomplete] in normal context.
  3648. Normal means, no org-mode-specific context."
  3649. :group 'org-completion
  3650. :type 'function)
  3651. ;;; Functions and variables from their packages
  3652. ;; Declared here to avoid compiler warnings
  3653. ;; XEmacs only
  3654. (defvar outline-mode-menu-heading)
  3655. (defvar outline-mode-menu-show)
  3656. (defvar outline-mode-menu-hide)
  3657. (defvar zmacs-regions) ; XEmacs regions
  3658. ;; Emacs only
  3659. (defvar mark-active)
  3660. ;; Various packages
  3661. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3662. (declare-function calendar-forward-day "cal-move" (arg))
  3663. (declare-function calendar-goto-date "cal-move" (date))
  3664. (declare-function calendar-goto-today "cal-move" ())
  3665. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3666. (defvar calc-embedded-close-formula)
  3667. (defvar calc-embedded-open-formula)
  3668. (declare-function cdlatex-tab "ext:cdlatex" ())
  3669. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3670. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3671. (defvar font-lock-unfontify-region-function)
  3672. (declare-function iswitchb-read-buffer "iswitchb"
  3673. (prompt &optional default require-match start matches-set))
  3674. (defvar iswitchb-temp-buflist)
  3675. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3676. (defvar org-agenda-tags-todo-honor-ignore-options)
  3677. (declare-function org-agenda-skip "org-agenda" ())
  3678. (declare-function
  3679. org-agenda-format-item "org-agenda"
  3680. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3681. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3682. (declare-function org-agenda-change-all-lines "org-agenda"
  3683. (newhead hdmarker &optional fixface just-this))
  3684. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3685. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3686. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3687. (beg end))
  3688. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3689. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3690. "org-agenda" (&optional end))
  3691. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3692. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3693. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3694. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3695. (declare-function org-indent-mode "org-indent" (&optional arg))
  3696. (declare-function parse-time-string "parse-time" (string))
  3697. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3698. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3699. (defvar remember-data-file)
  3700. (defvar texmathp-why)
  3701. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3702. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3703. (defvar org-latex-regexps)
  3704. ;;; Autoload and prepare some org modules
  3705. ;; Some table stuff that needs to be defined here, because it is used
  3706. ;; by the functions setting up org-mode or checking for table context.
  3707. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3708. "Detect an org-type or table-type table.")
  3709. (defconst org-table-line-regexp "^[ \t]*|"
  3710. "Detect an org-type table line.")
  3711. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3712. "Detect an org-type table line.")
  3713. (defconst org-table-hline-regexp "^[ \t]*|-"
  3714. "Detect an org-type table hline.")
  3715. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3716. "Detect a table-type table hline.")
  3717. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3718. "Detect the first line outside a table when searching from within it.
  3719. This works for both table types.")
  3720. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3721. "Detect a #+TBLFM line.")
  3722. ;;;###autoload
  3723. (defun turn-on-orgtbl ()
  3724. "Unconditionally turn on `orgtbl-mode'."
  3725. (require 'org-table)
  3726. (orgtbl-mode 1))
  3727. (defun org-at-table-p (&optional table-type)
  3728. "Return t if the cursor is inside an org-type table.
  3729. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3730. (if org-enable-table-editor
  3731. (save-excursion
  3732. (beginning-of-line 1)
  3733. (looking-at (if table-type org-table-any-line-regexp
  3734. org-table-line-regexp)))
  3735. nil))
  3736. (defsubst org-table-p () (org-at-table-p))
  3737. (defun org-at-table.el-p ()
  3738. "Return t if and only if we are at a table.el table."
  3739. (and (org-at-table-p 'any)
  3740. (save-excursion
  3741. (goto-char (org-table-begin 'any))
  3742. (looking-at org-table1-hline-regexp))))
  3743. (defun org-table-recognize-table.el ()
  3744. "If there is a table.el table nearby, recognize it and move into it."
  3745. (if org-table-tab-recognizes-table.el
  3746. (if (org-at-table.el-p)
  3747. (progn
  3748. (beginning-of-line 1)
  3749. (if (looking-at org-table-dataline-regexp)
  3750. nil
  3751. (if (looking-at org-table1-hline-regexp)
  3752. (progn
  3753. (beginning-of-line 2)
  3754. (if (looking-at org-table-any-border-regexp)
  3755. (beginning-of-line -1)))))
  3756. (if (re-search-forward "|" (org-table-end t) t)
  3757. (progn
  3758. (require 'table)
  3759. (if (table--at-cell-p (point))
  3760. t
  3761. (message "recognizing table.el table...")
  3762. (table-recognize-table)
  3763. (message "recognizing table.el table...done")))
  3764. (error "This should not happen"))
  3765. t)
  3766. nil)
  3767. nil))
  3768. (defun org-at-table-hline-p ()
  3769. "Return t if the cursor is inside a hline in a table."
  3770. (if org-enable-table-editor
  3771. (save-excursion
  3772. (beginning-of-line 1)
  3773. (looking-at org-table-hline-regexp))
  3774. nil))
  3775. (defun org-table-map-tables (function &optional quietly)
  3776. "Apply FUNCTION to the start of all tables in the buffer."
  3777. (save-excursion
  3778. (save-restriction
  3779. (widen)
  3780. (goto-char (point-min))
  3781. (while (re-search-forward org-table-any-line-regexp nil t)
  3782. (unless quietly
  3783. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3784. (beginning-of-line 1)
  3785. (when (and (looking-at org-table-line-regexp)
  3786. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3787. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3788. (save-excursion (funcall function))
  3789. (or (looking-at org-table-line-regexp)
  3790. (forward-char 1)))
  3791. (re-search-forward org-table-any-border-regexp nil 1))))
  3792. (unless quietly (message "Mapping tables: done")))
  3793. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3794. (declare-function org-clock-update-mode-line "org-clock" ())
  3795. (declare-function org-resolve-clocks "org-clock"
  3796. (&optional also-non-dangling-p prompt last-valid))
  3797. (defun org-at-TBLFM-p (&optional pos)
  3798. "Return t when point (or POS) is in #+TBLFM line."
  3799. (save-excursion
  3800. (let ((pos pos)))
  3801. (goto-char (or pos (point)))
  3802. (beginning-of-line 1)
  3803. (looking-at org-TBLFM-regexp)))
  3804. (defvar org-clock-start-time)
  3805. (defvar org-clock-marker (make-marker)
  3806. "Marker recording the last clock-in.")
  3807. (defvar org-clock-hd-marker (make-marker)
  3808. "Marker recording the last clock-in, but the headline position.")
  3809. (defvar org-clock-heading ""
  3810. "The heading of the current clock entry.")
  3811. (defun org-clock-is-active ()
  3812. "Return the buffer where the clock is currently running.
  3813. Return nil if no clock is running."
  3814. (marker-buffer org-clock-marker))
  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. ;; Declare Column View Code
  3984. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3985. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3986. (declare-function org-columns-compute "org-colview" (property))
  3987. ;; Declare ID code
  3988. (declare-function org-id-store-link "org-id")
  3989. (declare-function org-id-locations-load "org-id")
  3990. (declare-function org-id-locations-save "org-id")
  3991. (defvar org-id-track-globally)
  3992. ;;; Variables for pre-computed regular expressions, all buffer local
  3993. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3994. "Matches first line of a hidden block.")
  3995. (make-variable-buffer-local 'org-drawer-regexp)
  3996. (defvar org-todo-regexp nil
  3997. "Matches any of the TODO state keywords.")
  3998. (make-variable-buffer-local 'org-todo-regexp)
  3999. (defvar org-not-done-regexp nil
  4000. "Matches any of the TODO state keywords except the last one.")
  4001. (make-variable-buffer-local 'org-not-done-regexp)
  4002. (defvar org-not-done-heading-regexp nil
  4003. "Matches a TODO headline that is not done.")
  4004. (make-variable-buffer-local 'org-not-done-regexp)
  4005. (defvar org-todo-line-regexp nil
  4006. "Matches a headline and puts TODO state into group 2 if present.")
  4007. (make-variable-buffer-local 'org-todo-line-regexp)
  4008. (defvar org-complex-heading-regexp nil
  4009. "Matches a headline and puts everything into groups:
  4010. group 1: the stars
  4011. group 2: The todo keyword, maybe
  4012. group 3: Priority cookie
  4013. group 4: True headline
  4014. group 5: Tags")
  4015. (make-variable-buffer-local 'org-complex-heading-regexp)
  4016. (defvar org-complex-heading-regexp-format nil
  4017. "Printf format to make regexp to match an exact headline.
  4018. This regexp will match the headline of any node which has the
  4019. exact headline text that is put into the format, but may have any
  4020. TODO state, priority and tags.")
  4021. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4022. (defvar org-todo-line-tags-regexp nil
  4023. "Matches a headline and puts TODO state into group 2 if present.
  4024. Also put tags into group 4 if tags are present.")
  4025. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4026. (defvar org-ds-keyword-length 12
  4027. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4028. (make-variable-buffer-local 'org-ds-keyword-length)
  4029. (defvar org-deadline-regexp nil
  4030. "Matches the DEADLINE keyword.")
  4031. (make-variable-buffer-local 'org-deadline-regexp)
  4032. (defvar org-deadline-time-regexp nil
  4033. "Matches the DEADLINE keyword together with a time stamp.")
  4034. (make-variable-buffer-local 'org-deadline-time-regexp)
  4035. (defvar org-deadline-time-hour-regexp nil
  4036. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4037. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4038. (defvar org-deadline-line-regexp nil
  4039. "Matches the DEADLINE keyword and the rest of the line.")
  4040. (make-variable-buffer-local 'org-deadline-line-regexp)
  4041. (defvar org-scheduled-regexp nil
  4042. "Matches the SCHEDULED keyword.")
  4043. (make-variable-buffer-local 'org-scheduled-regexp)
  4044. (defvar org-scheduled-time-regexp nil
  4045. "Matches the SCHEDULED keyword together with a time stamp.")
  4046. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4047. (defvar org-scheduled-time-hour-regexp nil
  4048. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4049. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4050. (defvar org-closed-time-regexp nil
  4051. "Matches the CLOSED keyword together with a time stamp.")
  4052. (make-variable-buffer-local 'org-closed-time-regexp)
  4053. (defvar org-keyword-time-regexp nil
  4054. "Matches any of the 4 keywords, together with the time stamp.")
  4055. (make-variable-buffer-local 'org-keyword-time-regexp)
  4056. (defvar org-keyword-time-not-clock-regexp nil
  4057. "Matches any of the 3 keywords, together with the time stamp.")
  4058. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4059. (defvar org-maybe-keyword-time-regexp nil
  4060. "Matches a timestamp, possibly preceded by a keyword.")
  4061. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4062. (defvar org-all-time-keywords nil
  4063. "List of time keywords.")
  4064. (make-variable-buffer-local 'org-all-time-keywords)
  4065. (defconst org-plain-time-of-day-regexp
  4066. (concat
  4067. "\\(\\<[012]?[0-9]"
  4068. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4069. "\\(--?"
  4070. "\\(\\<[012]?[0-9]"
  4071. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4072. "\\)?")
  4073. "Regular expression to match a plain time or time range.
  4074. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4075. groups carry important information:
  4076. 0 the full match
  4077. 1 the first time, range or not
  4078. 8 the second time, if it is a range.")
  4079. (defconst org-plain-time-extension-regexp
  4080. (concat
  4081. "\\(\\<[012]?[0-9]"
  4082. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4083. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4084. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4085. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4086. groups carry important information:
  4087. 0 the full match
  4088. 7 hours of duration
  4089. 9 minutes of duration")
  4090. (defconst org-stamp-time-of-day-regexp
  4091. (concat
  4092. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4093. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4094. "\\(--?"
  4095. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4096. "Regular expression to match a timestamp time or time range.
  4097. After a match, the following groups carry important information:
  4098. 0 the full match
  4099. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4100. 2 the first time, range or not
  4101. 4 the second time, if it is a range.")
  4102. (defconst org-startup-options
  4103. '(("fold" org-startup-folded t)
  4104. ("overview" org-startup-folded t)
  4105. ("nofold" org-startup-folded nil)
  4106. ("showall" org-startup-folded nil)
  4107. ("showeverything" org-startup-folded showeverything)
  4108. ("content" org-startup-folded content)
  4109. ("indent" org-startup-indented t)
  4110. ("noindent" org-startup-indented nil)
  4111. ("hidestars" org-hide-leading-stars t)
  4112. ("showstars" org-hide-leading-stars nil)
  4113. ("odd" org-odd-levels-only t)
  4114. ("oddeven" org-odd-levels-only nil)
  4115. ("align" org-startup-align-all-tables t)
  4116. ("noalign" org-startup-align-all-tables nil)
  4117. ("inlineimages" org-startup-with-inline-images t)
  4118. ("noinlineimages" org-startup-with-inline-images nil)
  4119. ("latexpreview" org-startup-with-latex-preview t)
  4120. ("nolatexpreview" org-startup-with-latex-preview nil)
  4121. ("customtime" org-display-custom-times t)
  4122. ("logdone" org-log-done time)
  4123. ("lognotedone" org-log-done note)
  4124. ("nologdone" org-log-done nil)
  4125. ("lognoteclock-out" org-log-note-clock-out t)
  4126. ("nolognoteclock-out" org-log-note-clock-out nil)
  4127. ("logrepeat" org-log-repeat state)
  4128. ("lognoterepeat" org-log-repeat note)
  4129. ("logdrawer" org-log-into-drawer t)
  4130. ("nologdrawer" org-log-into-drawer nil)
  4131. ("logstatesreversed" org-log-states-order-reversed t)
  4132. ("nologstatesreversed" org-log-states-order-reversed nil)
  4133. ("nologrepeat" org-log-repeat nil)
  4134. ("logreschedule" org-log-reschedule time)
  4135. ("lognotereschedule" org-log-reschedule note)
  4136. ("nologreschedule" org-log-reschedule nil)
  4137. ("logredeadline" org-log-redeadline time)
  4138. ("lognoteredeadline" org-log-redeadline note)
  4139. ("nologredeadline" org-log-redeadline nil)
  4140. ("logrefile" org-log-refile time)
  4141. ("lognoterefile" org-log-refile note)
  4142. ("nologrefile" org-log-refile nil)
  4143. ("fninline" org-footnote-define-inline t)
  4144. ("nofninline" org-footnote-define-inline nil)
  4145. ("fnlocal" org-footnote-section nil)
  4146. ("fnauto" org-footnote-auto-label t)
  4147. ("fnprompt" org-footnote-auto-label nil)
  4148. ("fnconfirm" org-footnote-auto-label confirm)
  4149. ("fnplain" org-footnote-auto-label plain)
  4150. ("fnadjust" org-footnote-auto-adjust t)
  4151. ("nofnadjust" org-footnote-auto-adjust nil)
  4152. ("constcgs" constants-unit-system cgs)
  4153. ("constSI" constants-unit-system SI)
  4154. ("noptag" org-tag-persistent-alist nil)
  4155. ("hideblocks" org-hide-block-startup t)
  4156. ("nohideblocks" org-hide-block-startup nil)
  4157. ("beamer" org-startup-with-beamer-mode t)
  4158. ("entitiespretty" org-pretty-entities t)
  4159. ("entitiesplain" org-pretty-entities nil))
  4160. "Variable associated with STARTUP options for org-mode.
  4161. Each element is a list of three items: the startup options (as written
  4162. in the #+STARTUP line), the corresponding variable, and the value to set
  4163. this variable to if the option is found. An optional forth element PUSH
  4164. means to push this value onto the list in the variable.")
  4165. (defun org-update-property-plist (key val props)
  4166. "Update PROPS with KEY and VAL."
  4167. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4168. (key (if appending (substring key 0 (- (length key) 1)) key))
  4169. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4170. (previous (cdr (assoc key props))))
  4171. (if appending
  4172. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4173. (cons (cons key val) remainder))))
  4174. (defconst org-block-regexp
  4175. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4176. "Regular expression for hiding blocks.")
  4177. (defconst org-heading-keyword-regexp-format
  4178. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4179. "Printf format for a regexp matching a headline with some keyword.
  4180. This regexp will match the headline of any node which has the
  4181. exact keyword that is put into the format. The keyword isn't in
  4182. any group by default, but the stars and the body are.")
  4183. (defconst org-heading-keyword-maybe-regexp-format
  4184. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4185. "Printf format for a regexp matching a headline, possibly with some keyword.
  4186. This regexp can match any headline with the specified keyword, or
  4187. without a keyword. The keyword isn't in any group by default,
  4188. but the stars and the body are.")
  4189. (defcustom org-group-tags t
  4190. "When non-nil (the default), use group tags.
  4191. This can be turned on/off through `org-toggle-tags-groups'."
  4192. :group 'org-tags
  4193. :group 'org-startup
  4194. :type 'boolean)
  4195. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4196. (defun org-toggle-tags-groups ()
  4197. "Toggle support for group tags.
  4198. Support for group tags is controlled by the option
  4199. `org-group-tags', which is non-nil by default."
  4200. (interactive)
  4201. (setq org-group-tags (not org-group-tags))
  4202. (cond ((and (derived-mode-p 'org-agenda-mode)
  4203. org-group-tags)
  4204. (org-agenda-redo))
  4205. ((derived-mode-p 'org-mode)
  4206. (let ((org-inhibit-startup t)) (org-mode))))
  4207. (message "Groups tags support has been turned %s"
  4208. (if org-group-tags "on" "off")))
  4209. (defun org-set-regexps-and-options-for-tags ()
  4210. "Precompute variables used for tags."
  4211. (when (derived-mode-p 'org-mode)
  4212. (org-set-local 'org-file-tags nil)
  4213. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4214. (splitre "[ \t]+")
  4215. (start 0)
  4216. tags ftags key value)
  4217. (save-excursion
  4218. (save-restriction
  4219. (widen)
  4220. (goto-char (point-min))
  4221. (while (re-search-forward re nil t)
  4222. (setq key (upcase (org-match-string-no-properties 1))
  4223. value (org-match-string-no-properties 2))
  4224. (if (stringp value) (setq value (org-trim value)))
  4225. (cond
  4226. ((equal key "TAGS")
  4227. (setq tags (append tags (if tags '("\\n") nil)
  4228. (org-split-string value splitre))))
  4229. ((equal key "FILETAGS")
  4230. (when (string-match "\\S-" value)
  4231. (setq ftags
  4232. (append
  4233. ftags
  4234. (apply 'append
  4235. (mapcar (lambda (x) (org-split-string x ":"))
  4236. (org-split-string value)))))))))))
  4237. ;; Process the file tags.
  4238. (and ftags (org-set-local 'org-file-tags
  4239. (mapcar 'org-add-prop-inherited ftags)))
  4240. (org-set-local 'org-tag-groups-alist nil)
  4241. ;; Process the tags.
  4242. (when (and (not tags) org-tag-alist)
  4243. (setq tags
  4244. (mapcar
  4245. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4246. ((eq (car tg) :endgroup) "}")
  4247. ((eq (car tg) :grouptags) ":")
  4248. ((eq (car tg) :newline) "\n")
  4249. (t (concat (car tg)
  4250. (if (characterp (cdr tg))
  4251. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4252. org-tag-alist)))
  4253. (let (e tgs g)
  4254. (while (setq e (pop tags))
  4255. (cond
  4256. ((equal e "{")
  4257. (progn (push '(:startgroup) tgs)
  4258. (when (equal (nth 1 tags) ":")
  4259. (push (list (replace-regexp-in-string
  4260. "(.+)$" "" (nth 0 tags)))
  4261. org-tag-groups-alist)
  4262. (setq g 0))))
  4263. ((equal e ":") (push '(:grouptags) tgs))
  4264. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4265. ((equal e "\\n") (push '(:newline) tgs))
  4266. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4267. (push (cons (match-string 1 e)
  4268. (string-to-char (match-string 2 e))) tgs)
  4269. (if (and g (> g 0))
  4270. (setcar org-tag-groups-alist
  4271. (append (car org-tag-groups-alist)
  4272. (list (match-string 1 e)))))
  4273. (if g (setq g (1+ g))))
  4274. (t (push (list e) tgs)
  4275. (if (and g (> g 0))
  4276. (setcar org-tag-groups-alist
  4277. (append (car org-tag-groups-alist) (list e))))
  4278. (if g (setq g (1+ g))))))
  4279. (org-set-local 'org-tag-alist nil)
  4280. (while (setq e (pop tgs))
  4281. (or (and (stringp (car e))
  4282. (assoc (car e) org-tag-alist))
  4283. (push e org-tag-alist)))
  4284. ;; Return a list with tag variables
  4285. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4286. (defvar org-ota nil)
  4287. (defun org-set-regexps-and-options ()
  4288. "Precompute regular expressions used in the current buffer."
  4289. (when (derived-mode-p 'org-mode)
  4290. (org-set-local 'org-todo-kwd-alist nil)
  4291. (org-set-local 'org-todo-key-alist nil)
  4292. (org-set-local 'org-todo-key-trigger nil)
  4293. (org-set-local 'org-todo-keywords-1 nil)
  4294. (org-set-local 'org-done-keywords nil)
  4295. (org-set-local 'org-todo-heads nil)
  4296. (org-set-local 'org-todo-sets nil)
  4297. (org-set-local 'org-todo-log-states nil)
  4298. (org-set-local 'org-file-properties nil)
  4299. (let ((re (org-make-options-regexp
  4300. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4301. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4302. "SETUPFILE" "OPTIONS")
  4303. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4304. (splitre "[ \t]+")
  4305. (scripts org-use-sub-superscripts)
  4306. kwds kws0 kwsa key log value cat arch const links hw dws
  4307. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4308. (start 0))
  4309. (save-excursion
  4310. (save-restriction
  4311. (widen)
  4312. (goto-char (point-min))
  4313. (while
  4314. (or (and
  4315. ext-setup-or-nil
  4316. (not org-ota)
  4317. (let (ret)
  4318. (with-temp-buffer
  4319. (insert ext-setup-or-nil)
  4320. (let ((major-mode 'org-mode) org-ota)
  4321. (setq ret (save-match-data
  4322. (org-set-regexps-and-options-for-tags)))))
  4323. ;; Append setupfile tags to existing tags
  4324. (setq org-ota t)
  4325. (setq org-file-tags
  4326. (delq nil (append org-file-tags (nth 0 ret)))
  4327. org-tag-alist
  4328. (delq nil (append org-tag-alist (nth 1 ret)))
  4329. org-tag-groups-alist
  4330. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4331. (and ext-setup-or-nil
  4332. (string-match re ext-setup-or-nil start)
  4333. (setq start (match-end 0)))
  4334. (and (setq ext-setup-or-nil nil start 0)
  4335. (re-search-forward re nil t)))
  4336. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4337. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4338. (if (stringp value) (setq value (org-trim value)))
  4339. (cond
  4340. ((equal key "CATEGORY")
  4341. (setq cat value))
  4342. ((member key '("SEQ_TODO" "TODO"))
  4343. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4344. ((equal key "TYP_TODO")
  4345. (push (cons 'type (org-split-string value splitre)) kwds))
  4346. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4347. ;; general TODO-like setup
  4348. (push (cons (intern (downcase (match-string 1 key)))
  4349. (org-split-string value splitre)) kwds))
  4350. ((equal key "COLUMNS")
  4351. (org-set-local 'org-columns-default-format value))
  4352. ((equal key "LINK")
  4353. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4354. (push (cons (match-string 1 value)
  4355. (org-trim (match-string 2 value)))
  4356. links)))
  4357. ((equal key "PRIORITIES")
  4358. (setq prio (org-split-string value " +")))
  4359. ((equal key "PROPERTY")
  4360. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4361. (setq props (org-update-property-plist (match-string 1 value)
  4362. (match-string 2 value)
  4363. props))))
  4364. ((equal key "DRAWERS")
  4365. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4366. ((equal key "CONSTANTS")
  4367. (org-table-set-constants))
  4368. ((equal key "STARTUP")
  4369. (let ((opts (org-split-string value splitre))
  4370. l var val)
  4371. (while (setq l (pop opts))
  4372. (when (setq l (assoc l org-startup-options))
  4373. (setq var (nth 1 l) val (nth 2 l))
  4374. (if (not (nth 3 l))
  4375. (set (make-local-variable var) val)
  4376. (if (not (listp (symbol-value var)))
  4377. (set (make-local-variable var) nil))
  4378. (set (make-local-variable var) (symbol-value var))
  4379. (add-to-list var val))))))
  4380. ((equal key "ARCHIVE")
  4381. (setq arch value)
  4382. (remove-text-properties 0 (length arch)
  4383. '(face t fontified t) arch))
  4384. ((equal key "OPTIONS")
  4385. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4386. (setq scripts (read (match-string 2 value)))))
  4387. ((and (equal key "SETUPFILE")
  4388. ;; Prevent checking in Gnus messages
  4389. (not buffer-read-only))
  4390. (setq setup-contents (org-file-contents
  4391. (expand-file-name
  4392. (org-remove-double-quotes value))
  4393. 'noerror))
  4394. (if (not ext-setup-or-nil)
  4395. (setq ext-setup-or-nil setup-contents start 0)
  4396. (setq ext-setup-or-nil
  4397. (concat (substring ext-setup-or-nil 0 start)
  4398. "\n" setup-contents "\n"
  4399. (substring ext-setup-or-nil start)))))))
  4400. ;; search for property blocks
  4401. (goto-char (point-min))
  4402. (while (re-search-forward org-block-regexp nil t)
  4403. (when (equal "PROPERTY" (upcase (match-string 1)))
  4404. (setq value (replace-regexp-in-string
  4405. "[\n\r]" " " (match-string 4)))
  4406. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4407. (setq props (org-update-property-plist (match-string 1 value)
  4408. (match-string 2 value)
  4409. props)))))))
  4410. (org-set-local 'org-use-sub-superscripts scripts)
  4411. (when cat
  4412. (org-set-local 'org-category (intern cat))
  4413. (push (cons "CATEGORY" cat) props))
  4414. (when prio
  4415. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4416. (setq prio (mapcar 'string-to-char prio))
  4417. (org-set-local 'org-highest-priority (nth 0 prio))
  4418. (org-set-local 'org-lowest-priority (nth 1 prio))
  4419. (org-set-local 'org-default-priority (nth 2 prio)))
  4420. (and props (org-set-local 'org-file-properties (nreverse props)))
  4421. (and drawers (org-set-local 'org-drawers drawers))
  4422. (and arch (org-set-local 'org-archive-location arch))
  4423. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4424. ;; Process the TODO keywords
  4425. (unless kwds
  4426. ;; Use the global values as if they had been given locally.
  4427. (setq kwds (default-value 'org-todo-keywords))
  4428. (if (stringp (car kwds))
  4429. (setq kwds (list (cons org-todo-interpretation
  4430. (default-value 'org-todo-keywords)))))
  4431. (setq kwds (reverse kwds)))
  4432. (setq kwds (nreverse kwds))
  4433. (let (inter kws kw)
  4434. (while (setq kws (pop kwds))
  4435. (let ((kws (or
  4436. (run-hook-with-args-until-success
  4437. 'org-todo-setup-filter-hook kws)
  4438. kws)))
  4439. (setq inter (pop kws) sep (member "|" kws)
  4440. kws0 (delete "|" (copy-sequence kws))
  4441. kwsa nil
  4442. kws1 (mapcar
  4443. (lambda (x)
  4444. ;; 1 2
  4445. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4446. (progn
  4447. (setq kw (match-string 1 x)
  4448. key (and (match-end 2) (match-string 2 x))
  4449. log (org-extract-log-state-settings x))
  4450. (push (cons kw (and key (string-to-char key))) kwsa)
  4451. (and log (push log org-todo-log-states))
  4452. kw)
  4453. (error "Invalid TODO keyword %s" x)))
  4454. kws0)
  4455. kwsa (if kwsa (append '((:startgroup))
  4456. (nreverse kwsa)
  4457. '((:endgroup))))
  4458. hw (car kws1)
  4459. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4460. tail (list inter hw (car dws) (org-last dws))))
  4461. (add-to-list 'org-todo-heads hw 'append)
  4462. (push kws1 org-todo-sets)
  4463. (setq org-done-keywords (append org-done-keywords dws nil))
  4464. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4465. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4466. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4467. (setq org-todo-sets (nreverse org-todo-sets)
  4468. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4469. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4470. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4471. ;; Compute the regular expressions and other local variables.
  4472. ;; Using `org-outline-regexp-bol' would complicate them much,
  4473. ;; because of the fixed white space at the end of that string.
  4474. (if (not org-done-keywords)
  4475. (setq org-done-keywords (and org-todo-keywords-1
  4476. (list (org-last org-todo-keywords-1)))))
  4477. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4478. (length org-scheduled-string)
  4479. (length org-clock-string)
  4480. (length org-closed-string)))
  4481. org-drawer-regexp
  4482. (concat "^[ \t]*:\\("
  4483. (mapconcat 'regexp-quote org-drawers "\\|")
  4484. "\\):[ \t]*$")
  4485. org-not-done-keywords
  4486. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4487. org-todo-regexp
  4488. (concat "\\("
  4489. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4490. "\\)")
  4491. org-not-done-regexp
  4492. (concat "\\("
  4493. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4494. "\\)")
  4495. org-not-done-heading-regexp
  4496. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4497. org-todo-line-regexp
  4498. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4499. org-complex-heading-regexp
  4500. (concat "^\\(\\*+\\)"
  4501. "\\(?: +" org-todo-regexp "\\)?"
  4502. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4503. "\\(?: +\\(.*?\\)\\)??"
  4504. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4505. "[ \t]*$")
  4506. org-complex-heading-regexp-format
  4507. (concat "^\\(\\*+\\)"
  4508. "\\(?: +" org-todo-regexp "\\)?"
  4509. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4510. "\\(?: +"
  4511. ;; Stats cookies can be stuck to body.
  4512. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4513. "\\(%s\\)"
  4514. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4515. "\\)"
  4516. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4517. "[ \t]*$")
  4518. org-todo-line-tags-regexp
  4519. (concat "^\\(\\*+\\)"
  4520. "\\(?: +" org-todo-regexp "\\)?"
  4521. "\\(?: +\\(.*?\\)\\)??"
  4522. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4523. "[ \t]*$")
  4524. org-deadline-regexp (concat "\\<" org-deadline-string)
  4525. org-deadline-time-regexp
  4526. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4527. org-deadline-time-hour-regexp
  4528. (concat "\\<" org-deadline-string
  4529. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4530. org-deadline-line-regexp
  4531. (concat "\\<\\(" org-deadline-string "\\).*")
  4532. org-scheduled-regexp
  4533. (concat "\\<" org-scheduled-string)
  4534. org-scheduled-time-regexp
  4535. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4536. org-scheduled-time-hour-regexp
  4537. (concat "\\<" org-scheduled-string
  4538. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4539. org-closed-time-regexp
  4540. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4541. org-keyword-time-regexp
  4542. (concat "\\<\\(" org-scheduled-string
  4543. "\\|" org-deadline-string
  4544. "\\|" org-closed-string
  4545. "\\|" org-clock-string "\\)"
  4546. " *[[<]\\([^]>]+\\)[]>]")
  4547. org-keyword-time-not-clock-regexp
  4548. (concat "\\<\\(" org-scheduled-string
  4549. "\\|" org-deadline-string
  4550. "\\|" org-closed-string
  4551. "\\)"
  4552. " *[[<]\\([^]>]+\\)[]>]")
  4553. org-maybe-keyword-time-regexp
  4554. (concat "\\(\\<\\(" org-scheduled-string
  4555. "\\|" org-deadline-string
  4556. "\\|" org-closed-string
  4557. "\\|" org-clock-string "\\)\\)?"
  4558. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4559. org-all-time-keywords
  4560. (mapcar (lambda (w) (substring w 0 -1))
  4561. (list org-scheduled-string org-deadline-string
  4562. org-clock-string org-closed-string)))
  4563. (setq org-ota nil)
  4564. (org-compute-latex-and-related-regexp))))
  4565. (defun org-file-contents (file &optional noerror)
  4566. "Return the contents of FILE, as a string."
  4567. (if (or (not file)
  4568. (not (file-readable-p file)))
  4569. (if noerror
  4570. (message "Cannot read file \"%s\"" file)
  4571. (error "Cannot read file \"%s\"" file))
  4572. (with-temp-buffer
  4573. (insert-file-contents file)
  4574. (buffer-string))))
  4575. (defun org-extract-log-state-settings (x)
  4576. "Extract the log state setting from a TODO keyword string.
  4577. This will extract info from a string like \"WAIT(w@/!)\"."
  4578. (let (kw key log1 log2)
  4579. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4580. (setq kw (match-string 1 x)
  4581. key (and (match-end 2) (match-string 2 x))
  4582. log1 (and (match-end 3) (match-string 3 x))
  4583. log2 (and (match-end 4) (match-string 4 x)))
  4584. (and (or log1 log2)
  4585. (list kw
  4586. (and log1 (if (equal log1 "!") 'time 'note))
  4587. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4588. (defun org-remove-keyword-keys (list)
  4589. "Remove a pair of parenthesis at the end of each string in LIST."
  4590. (mapcar (lambda (x)
  4591. (if (string-match "(.*)$" x)
  4592. (substring x 0 (match-beginning 0))
  4593. x))
  4594. list))
  4595. (defun org-assign-fast-keys (alist)
  4596. "Assign fast keys to a keyword-key alist.
  4597. Respect keys that are already there."
  4598. (let (new e (alt ?0))
  4599. (while (setq e (pop alist))
  4600. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4601. (cdr e)) ;; Key already assigned.
  4602. (push e new)
  4603. (let ((clist (string-to-list (downcase (car e))))
  4604. (used (append new alist)))
  4605. (when (= (car clist) ?@)
  4606. (pop clist))
  4607. (while (and clist (rassoc (car clist) used))
  4608. (pop clist))
  4609. (unless clist
  4610. (while (rassoc alt used)
  4611. (incf alt)))
  4612. (push (cons (car e) (or (car clist) alt)) new))))
  4613. (nreverse new)))
  4614. ;;; Some variables used in various places
  4615. (defvar org-window-configuration nil
  4616. "Used in various places to store a window configuration.")
  4617. (defvar org-selected-window nil
  4618. "Used in various places to store a window configuration.")
  4619. (defvar org-finish-function nil
  4620. "Function to be called when `C-c C-c' is used.
  4621. This is for getting out of special buffers like capture.")
  4622. ;; FIXME: Occasionally check by commenting these, to make sure
  4623. ;; no other functions uses these, forgetting to let-bind them.
  4624. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4625. (defvar org-last-state)
  4626. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4627. ;; Defined somewhere in this file, but used before definition.
  4628. (defvar org-entities) ;; defined in org-entities.el
  4629. (defvar org-struct-menu)
  4630. (defvar org-org-menu)
  4631. (defvar org-tbl-menu)
  4632. ;;;; Define the Org-mode
  4633. ;; We use a before-change function to check if a table might need
  4634. ;; an update.
  4635. (defvar org-table-may-need-update t
  4636. "Indicates that a table might need an update.
  4637. This variable is set by `org-before-change-function'.
  4638. `org-table-align' sets it back to nil.")
  4639. (defun org-before-change-function (beg end)
  4640. "Every change indicates that a table might need an update."
  4641. (setq org-table-may-need-update t))
  4642. (defvar org-mode-map)
  4643. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4644. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4645. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4646. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4647. (defvar org-table-buffer-is-an nil)
  4648. (defvar bidi-paragraph-direction)
  4649. (defvar buffer-face-mode-face)
  4650. (require 'outline)
  4651. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4652. (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"))
  4653. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4654. ;; Other stuff we need.
  4655. (require 'time-date)
  4656. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4657. (require 'easymenu)
  4658. (require 'overlay)
  4659. ;; (require 'org-macs) moved higher up in the file before it is first used
  4660. (require 'org-entities)
  4661. ;; (require 'org-compat) moved higher up in the file before it is first used
  4662. (require 'org-faces)
  4663. (require 'org-list)
  4664. (require 'org-pcomplete)
  4665. (require 'org-src)
  4666. (require 'org-footnote)
  4667. (require 'org-macro)
  4668. ;; babel
  4669. (require 'ob)
  4670. ;;;###autoload
  4671. (define-derived-mode org-mode outline-mode "Org"
  4672. "Outline-based notes management and organizer, alias
  4673. \"Carsten's outline-mode for keeping track of everything.\"
  4674. Org-mode develops organizational tasks around a NOTES file which
  4675. contains information about projects as plain text. Org-mode is
  4676. implemented on top of outline-mode, which is ideal to keep the content
  4677. of large files well structured. It supports ToDo items, deadlines and
  4678. time stamps, which magically appear in the diary listing of the Emacs
  4679. calendar. Tables are easily created with a built-in table editor.
  4680. Plain text URL-like links connect to websites, emails (VM), Usenet
  4681. messages (Gnus), BBDB entries, and any files related to the project.
  4682. For printing and sharing of notes, an Org-mode file (or a part of it)
  4683. can be exported as a structured ASCII or HTML file.
  4684. The following commands are available:
  4685. \\{org-mode-map}"
  4686. ;; Get rid of Outline menus, they are not needed
  4687. ;; Need to do this here because define-derived-mode sets up
  4688. ;; the keymap so late. Still, it is a waste to call this each time
  4689. ;; we switch another buffer into org-mode.
  4690. (if (featurep 'xemacs)
  4691. (when (boundp 'outline-mode-menu-heading)
  4692. ;; Assume this is Greg's port, it uses easymenu
  4693. (easy-menu-remove outline-mode-menu-heading)
  4694. (easy-menu-remove outline-mode-menu-show)
  4695. (easy-menu-remove outline-mode-menu-hide))
  4696. (define-key org-mode-map [menu-bar headings] 'undefined)
  4697. (define-key org-mode-map [menu-bar hide] 'undefined)
  4698. (define-key org-mode-map [menu-bar show] 'undefined))
  4699. (org-load-modules-maybe)
  4700. (easy-menu-add org-org-menu)
  4701. (easy-menu-add org-tbl-menu)
  4702. (org-install-agenda-files-menu)
  4703. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4704. (add-to-invisibility-spec '(org-cwidth))
  4705. (add-to-invisibility-spec '(org-hide-block . t))
  4706. (when (featurep 'xemacs)
  4707. (org-set-local 'line-move-ignore-invisible t))
  4708. (org-set-local 'outline-regexp org-outline-regexp)
  4709. (org-set-local 'outline-level 'org-outline-level)
  4710. (setq bidi-paragraph-direction 'left-to-right)
  4711. (when (and org-ellipsis
  4712. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4713. (fboundp 'make-glyph-code))
  4714. (unless org-display-table
  4715. (setq org-display-table (make-display-table)))
  4716. (set-display-table-slot
  4717. org-display-table 4
  4718. (vconcat (mapcar
  4719. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4720. org-ellipsis)))
  4721. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4722. (setq buffer-display-table org-display-table))
  4723. (org-set-regexps-and-options-for-tags)
  4724. (org-set-regexps-and-options)
  4725. (org-set-font-lock-defaults)
  4726. (when (and org-tag-faces (not org-tags-special-faces-re))
  4727. ;; tag faces set outside customize.... force initialization.
  4728. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4729. ;; Calc embedded
  4730. (org-set-local 'calc-embedded-open-mode "# ")
  4731. ;; Modify a few syntax entries
  4732. (modify-syntax-entry ?@ "w")
  4733. (modify-syntax-entry ?\" "\"")
  4734. (if org-startup-truncated (setq truncate-lines t))
  4735. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4736. (org-set-local 'font-lock-unfontify-region-function
  4737. 'org-unfontify-region)
  4738. ;; Activate before-change-function
  4739. (org-set-local 'org-table-may-need-update t)
  4740. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4741. 'local)
  4742. ;; Check for running clock before killing a buffer
  4743. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4744. ;; Initialize macros templates.
  4745. (org-macro-initialize-templates)
  4746. ;; Initialize radio targets.
  4747. (org-update-radio-target-regexp)
  4748. ;; Indentation.
  4749. (org-set-local 'indent-line-function 'org-indent-line)
  4750. (org-set-local 'indent-region-function 'org-indent-region)
  4751. ;; Filling and auto-filling.
  4752. (org-setup-filling)
  4753. ;; Comments.
  4754. (org-setup-comments-handling)
  4755. ;; Beginning/end of defun
  4756. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4757. (org-set-local 'end-of-defun-function 'org-forward-element)
  4758. ;; Next error for sparse trees
  4759. (org-set-local 'next-error-function 'org-occur-next-match)
  4760. ;; Make sure dependence stuff works reliably, even for users who set it
  4761. ;; too late :-(
  4762. (if org-enforce-todo-dependencies
  4763. (add-hook 'org-blocker-hook
  4764. 'org-block-todo-from-children-or-siblings-or-parent)
  4765. (remove-hook 'org-blocker-hook
  4766. 'org-block-todo-from-children-or-siblings-or-parent))
  4767. (if org-enforce-todo-checkbox-dependencies
  4768. (add-hook 'org-blocker-hook
  4769. 'org-block-todo-from-checkboxes)
  4770. (remove-hook 'org-blocker-hook
  4771. 'org-block-todo-from-checkboxes))
  4772. ;; Align options lines
  4773. (org-set-local
  4774. 'align-mode-rules-list
  4775. '((org-in-buffer-settings
  4776. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4777. (modes . '(org-mode)))))
  4778. ;; Imenu
  4779. (org-set-local 'imenu-create-index-function
  4780. 'org-imenu-get-tree)
  4781. ;; Make isearch reveal context
  4782. (if (or (featurep 'xemacs)
  4783. (not (boundp 'outline-isearch-open-invisible-function)))
  4784. ;; Emacs 21 and XEmacs make use of the hook
  4785. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4786. ;; Emacs 22 deals with this through a special variable
  4787. (org-set-local 'outline-isearch-open-invisible-function
  4788. (lambda (&rest ignore) (org-show-context 'isearch)))
  4789. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4790. ;; Setup the pcomplete hooks
  4791. (set (make-local-variable 'pcomplete-command-completion-function)
  4792. 'org-pcomplete-initial)
  4793. (set (make-local-variable 'pcomplete-command-name-function)
  4794. 'org-command-at-point)
  4795. (set (make-local-variable 'pcomplete-default-completion-function)
  4796. 'ignore)
  4797. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4798. 'org-parse-arguments)
  4799. (set (make-local-variable 'pcomplete-termination-string) "")
  4800. (when (>= emacs-major-version 23)
  4801. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4802. ;; If empty file that did not turn on org-mode automatically, make it to.
  4803. (if (and org-insert-mode-line-in-empty-file
  4804. (org-called-interactively-p 'any)
  4805. (= (point-min) (point-max)))
  4806. (insert "# -*- mode: org -*-\n\n"))
  4807. (unless org-inhibit-startup
  4808. (org-unmodified
  4809. (and org-startup-with-beamer-mode (org-beamer-mode))
  4810. (when org-startup-align-all-tables
  4811. (org-table-map-tables 'org-table-align 'quietly))
  4812. (when org-startup-with-inline-images
  4813. (org-display-inline-images))
  4814. (when org-startup-with-latex-preview
  4815. (org-preview-latex-fragment))
  4816. (unless org-inhibit-startup-visibility-stuff
  4817. (org-set-startup-visibility))))
  4818. ;; Try to set org-hide correctly
  4819. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4820. ;; Update `customize-package-emacs-version-alist'
  4821. (add-to-list 'customize-package-emacs-version-alist
  4822. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4823. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4824. ("8.0" . "24.4")))
  4825. (defvar org-mode-transpose-word-syntax-table
  4826. (let ((st (make-syntax-table)))
  4827. (mapc (lambda(c) (modify-syntax-entry
  4828. (string-to-char (car c)) "w p" st))
  4829. org-emphasis-alist)
  4830. st))
  4831. (when (fboundp 'abbrev-table-put)
  4832. (abbrev-table-put org-mode-abbrev-table
  4833. :parents (list text-mode-abbrev-table)))
  4834. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4835. (defsubst org-fix-ellipsis-at-bol ()
  4836. (save-excursion (goto-char (window-start)) (recenter 0)))
  4837. (defun org-find-invisible-foreground ()
  4838. (let ((candidates (remove
  4839. "unspecified-bg"
  4840. (nconc
  4841. (list (face-background 'default)
  4842. (face-background 'org-default))
  4843. (mapcar
  4844. (lambda (alist)
  4845. (when (boundp alist)
  4846. (cdr (assoc 'background-color (symbol-value alist)))))
  4847. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4848. (list (face-foreground 'org-hide))))))
  4849. (car (remove nil candidates))))
  4850. (defun org-current-time (&optional rounding-minutes past)
  4851. "Current time, possibly rounded to ROUNDING-MINUTES.
  4852. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4853. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4854. the rounding returns a past time."
  4855. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4856. (car org-time-stamp-rounding-minutes)))
  4857. (time (decode-time)) res)
  4858. (if (< r 1)
  4859. (current-time)
  4860. (setq res
  4861. (apply 'encode-time
  4862. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4863. (nthcdr 2 time))))
  4864. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4865. (seconds-to-time (- (org-float-time res) (* r 60)))
  4866. res))))
  4867. (defun org-today ()
  4868. "Return today date, considering `org-extend-today-until'."
  4869. (time-to-days
  4870. (time-subtract (current-time)
  4871. (list 0 (* 3600 org-extend-today-until) 0))))
  4872. ;;;; Font-Lock stuff, including the activators
  4873. (defvar org-mouse-map (make-sparse-keymap))
  4874. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4875. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4876. (when org-mouse-1-follows-link
  4877. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4878. (when org-tab-follows-link
  4879. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4880. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4881. (require 'font-lock)
  4882. (defconst org-non-link-chars "]\t\n\r<>")
  4883. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4884. "shell" "elisp" "doi" "message"))
  4885. (defvar org-link-types-re nil
  4886. "Matches a link that has a url-like prefix like \"http:\"")
  4887. (defvar org-link-re-with-space nil
  4888. "Matches a link with spaces, optional angular brackets around it.")
  4889. (defvar org-link-re-with-space2 nil
  4890. "Matches a link with spaces, optional angular brackets around it.")
  4891. (defvar org-link-re-with-space3 nil
  4892. "Matches a link with spaces, only for internal part in bracket links.")
  4893. (defvar org-angle-link-re nil
  4894. "Matches link with angular brackets, spaces are allowed.")
  4895. (defvar org-plain-link-re nil
  4896. "Matches plain link, without spaces.")
  4897. (defvar org-bracket-link-regexp nil
  4898. "Matches a link in double brackets.")
  4899. (defvar org-bracket-link-analytic-regexp nil
  4900. "Regular expression used to analyze links.
  4901. Here is what the match groups contain after a match:
  4902. 1: http:
  4903. 2: http
  4904. 3: path
  4905. 4: [desc]
  4906. 5: desc")
  4907. (defvar org-bracket-link-analytic-regexp++ nil
  4908. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4909. (defvar org-any-link-re nil
  4910. "Regular expression matching any link.")
  4911. (defconst org-match-sexp-depth 3
  4912. "Number of stacked braces for sub/superscript matching.")
  4913. (defun org-create-multibrace-regexp (left right n)
  4914. "Create a regular expression which will match a balanced sexp.
  4915. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4916. as single character strings.
  4917. The regexp returned will match the entire expression including the
  4918. delimiters. It will also define a single group which contains the
  4919. match except for the outermost delimiters. The maximum depth of
  4920. stacked delimiters is N. Escaping delimiters is not possible."
  4921. (let* ((nothing (concat "[^" left right "]*?"))
  4922. (or "\\|")
  4923. (re nothing)
  4924. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4925. (while (> n 1)
  4926. (setq n (1- n)
  4927. re (concat re or next)
  4928. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4929. (concat left "\\(" re "\\)" right)))
  4930. (defvar org-match-substring-regexp
  4931. (concat
  4932. "\\(\\S-\\)\\([_^]\\)\\("
  4933. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4934. "\\|"
  4935. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4936. "\\|"
  4937. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4938. "The regular expression matching a sub- or superscript.")
  4939. (defvar org-match-substring-with-braces-regexp
  4940. (concat
  4941. "\\(\\S-\\)\\([_^]\\)\\("
  4942. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4943. "\\)")
  4944. "The regular expression matching a sub- or superscript, forcing braces.")
  4945. (defun org-make-link-regexps ()
  4946. "Update the link regular expressions.
  4947. This should be called after the variable `org-link-types' has changed."
  4948. (setq org-link-types-re
  4949. (concat
  4950. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4951. org-link-re-with-space
  4952. (concat
  4953. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4954. "\\([^" org-non-link-chars " ]"
  4955. "[^" org-non-link-chars "]*"
  4956. "[^" org-non-link-chars " ]\\)>?")
  4957. org-link-re-with-space2
  4958. (concat
  4959. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4960. "\\([^" org-non-link-chars " ]"
  4961. "[^\t\n\r]*"
  4962. "[^" org-non-link-chars " ]\\)>?")
  4963. org-link-re-with-space3
  4964. (concat
  4965. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4966. "\\([^" org-non-link-chars " ]"
  4967. "[^\t\n\r]*\\)")
  4968. org-angle-link-re
  4969. (concat
  4970. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4971. "\\([^" org-non-link-chars " ]"
  4972. "[^" org-non-link-chars "]*"
  4973. "\\)>")
  4974. org-plain-link-re
  4975. (concat
  4976. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4977. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4978. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4979. org-bracket-link-regexp
  4980. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4981. org-bracket-link-analytic-regexp
  4982. (concat
  4983. "\\[\\["
  4984. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4985. "\\([^]]+\\)"
  4986. "\\]"
  4987. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4988. "\\]")
  4989. org-bracket-link-analytic-regexp++
  4990. (concat
  4991. "\\[\\["
  4992. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4993. "\\([^]]+\\)"
  4994. "\\]"
  4995. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4996. "\\]")
  4997. org-any-link-re
  4998. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4999. org-angle-link-re "\\)\\|\\("
  5000. org-plain-link-re "\\)")))
  5001. (org-make-link-regexps)
  5002. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5003. "Regular expression for fast time stamp matching.")
  5004. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5005. "Regular expression for fast time stamp matching.")
  5006. (defconst org-ts-regexp0
  5007. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5008. "Regular expression matching time strings for analysis.
  5009. This one does not require the space after the date, so it can be used
  5010. on a string that terminates immediately after the date.")
  5011. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5012. "Regular expression matching time strings for analysis.")
  5013. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5014. "Regular expression matching time stamps, with groups.")
  5015. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5016. "Regular expression matching time stamps (also [..]), with groups.")
  5017. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5018. "Regular expression matching a time stamp range.")
  5019. (defconst org-tr-regexp-both
  5020. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5021. "Regular expression matching a time stamp range.")
  5022. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5023. org-ts-regexp "\\)?")
  5024. "Regular expression matching a time stamp or time stamp range.")
  5025. (defconst org-tsr-regexp-both
  5026. (concat org-ts-regexp-both "\\(--?-?"
  5027. org-ts-regexp-both "\\)?")
  5028. "Regular expression matching a time stamp or time stamp range.
  5029. The time stamps may be either active or inactive.")
  5030. (defvar org-emph-face nil)
  5031. (defun org-do-emphasis-faces (limit)
  5032. "Run through the buffer and add overlays to emphasized strings."
  5033. (let (rtn a)
  5034. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5035. (if (not (= (char-after (match-beginning 3))
  5036. (char-after (match-beginning 4))))
  5037. (progn
  5038. (setq rtn t)
  5039. (setq a (assoc (match-string 3) org-emphasis-alist))
  5040. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5041. 'face
  5042. (nth 1 a))
  5043. (and (nth 2 a)
  5044. (org-remove-flyspell-overlays-in
  5045. (match-beginning 0) (match-end 0)))
  5046. (add-text-properties (match-beginning 2) (match-end 2)
  5047. '(font-lock-multiline t org-emphasis t))
  5048. (when org-hide-emphasis-markers
  5049. (add-text-properties (match-end 4) (match-beginning 5)
  5050. '(invisible org-link))
  5051. (add-text-properties (match-beginning 3) (match-end 3)
  5052. '(invisible org-link)))))
  5053. (backward-char 1))
  5054. rtn))
  5055. (defun org-emphasize (&optional char)
  5056. "Insert or change an emphasis, i.e. a font like bold or italic.
  5057. If there is an active region, change that region to a new emphasis.
  5058. If there is no region, just insert the marker characters and position
  5059. the cursor between them.
  5060. CHAR should be the marker character. If it is a space, it means to
  5061. remove the emphasis of the selected region.
  5062. If CHAR is not given (for example in an interactive call) it will be
  5063. prompted for."
  5064. (interactive)
  5065. (let ((erc org-emphasis-regexp-components)
  5066. (prompt "")
  5067. (string "") beg end move c s)
  5068. (if (org-region-active-p)
  5069. (setq beg (region-beginning) end (region-end)
  5070. string (buffer-substring beg end))
  5071. (setq move t))
  5072. (unless char
  5073. (message "Emphasis marker or tag: [%s]"
  5074. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5075. (setq char (read-char-exclusive)))
  5076. (if (equal char ?\ )
  5077. (setq s "" move nil)
  5078. (unless (assoc (char-to-string char) org-emphasis-alist)
  5079. (user-error "No such emphasis marker: \"%c\"" char))
  5080. (setq s (char-to-string char)))
  5081. (while (and (> (length string) 1)
  5082. (equal (substring string 0 1) (substring string -1))
  5083. (assoc (substring string 0 1) org-emphasis-alist))
  5084. (setq string (substring string 1 -1)))
  5085. (setq string (concat s string s))
  5086. (if beg (delete-region beg end))
  5087. (unless (or (bolp)
  5088. (string-match (concat "[" (nth 0 erc) "\n]")
  5089. (char-to-string (char-before (point)))))
  5090. (insert " "))
  5091. (unless (or (eobp)
  5092. (string-match (concat "[" (nth 1 erc) "\n]")
  5093. (char-to-string (char-after (point)))))
  5094. (insert " ") (backward-char 1))
  5095. (insert string)
  5096. (and move (backward-char 1))))
  5097. (defconst org-nonsticky-props
  5098. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5099. (defsubst org-rear-nonsticky-at (pos)
  5100. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5101. (defun org-activate-plain-links (limit)
  5102. "Run through the buffer and add overlays to links."
  5103. (let (f hl)
  5104. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5105. (not (org-in-src-block-p)))
  5106. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5107. (setq f (get-text-property (match-beginning 0) 'face))
  5108. (setq hl (org-match-string-no-properties 0))
  5109. (if (or (eq f 'org-tag)
  5110. (and (listp f) (memq 'org-tag f)))
  5111. nil
  5112. (add-text-properties (match-beginning 0) (match-end 0)
  5113. (list 'mouse-face 'highlight
  5114. 'face 'org-link
  5115. 'htmlize-link `(:uri ,hl)
  5116. 'keymap org-mouse-map))
  5117. (org-rear-nonsticky-at (match-end 0)))
  5118. t)))
  5119. (defun org-activate-code (limit)
  5120. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5121. (progn
  5122. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5123. (remove-text-properties (match-beginning 0) (match-end 0)
  5124. '(display t invisible t intangible t))
  5125. t)))
  5126. (defcustom org-src-fontify-natively nil
  5127. "When non-nil, fontify code in code blocks."
  5128. :type 'boolean
  5129. :version "24.1"
  5130. :group 'org-appearance
  5131. :group 'org-babel)
  5132. (defcustom org-allow-promoting-top-level-subtree nil
  5133. "When non-nil, allow promoting a top level subtree.
  5134. The leading star of the top level headline will be replaced
  5135. by a #."
  5136. :type 'boolean
  5137. :version "24.1"
  5138. :group 'org-appearance)
  5139. (defun org-fontify-meta-lines-and-blocks (limit)
  5140. (condition-case nil
  5141. (org-fontify-meta-lines-and-blocks-1 limit)
  5142. (error (message "org-mode fontification error"))))
  5143. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5144. "Fontify #+ lines and blocks."
  5145. (let ((case-fold-search t))
  5146. (if (re-search-forward
  5147. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5148. limit t)
  5149. (let ((beg (match-beginning 0))
  5150. (block-start (match-end 0))
  5151. (block-end nil)
  5152. (lang (match-string 7))
  5153. (beg1 (line-beginning-position 2))
  5154. (dc1 (downcase (match-string 2)))
  5155. (dc3 (downcase (match-string 3)))
  5156. end end1 quoting block-type ovl)
  5157. (cond
  5158. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5159. ;; a single line of backend-specific content
  5160. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5161. (remove-text-properties (match-beginning 0) (match-end 0)
  5162. '(display t invisible t intangible t))
  5163. (add-text-properties (match-beginning 1) (match-end 3)
  5164. '(font-lock-fontified t face org-meta-line))
  5165. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5166. '(font-lock-fontified t face org-block))
  5167. ; for backend-specific code
  5168. t)
  5169. ((and (match-end 4) (equal dc3 "+begin"))
  5170. ;; Truly a block
  5171. (setq block-type (downcase (match-string 5))
  5172. quoting (member block-type org-protecting-blocks))
  5173. (when (re-search-forward
  5174. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5175. nil t) ;; on purpose, we look further than LIMIT
  5176. (setq end (min (point-max) (match-end 0))
  5177. end1 (min (point-max) (1- (match-beginning 0))))
  5178. (setq block-end (match-beginning 0))
  5179. (when quoting
  5180. (remove-text-properties beg end
  5181. '(display t invisible t intangible t)))
  5182. (add-text-properties
  5183. beg end
  5184. '(font-lock-fontified t font-lock-multiline t))
  5185. (add-text-properties beg beg1 '(face org-meta-line))
  5186. (add-text-properties end1 (min (point-max) (1+ end))
  5187. '(face org-meta-line)) ; for end_src
  5188. (cond
  5189. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5190. (org-src-font-lock-fontify-block lang block-start block-end)
  5191. ;; remove old background overlays
  5192. (mapc (lambda (ov)
  5193. (if (eq (overlay-get ov 'face) 'org-block-background)
  5194. (delete-overlay ov)))
  5195. (overlays-at (/ (+ beg1 block-end) 2)))
  5196. ;; add a background overlay
  5197. (setq ovl (make-overlay beg1 block-end))
  5198. (overlay-put ovl 'face 'org-block-background)
  5199. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5200. (quoting
  5201. (add-text-properties beg1 (min (point-max) (1+ end1))
  5202. '(face org-block))) ; end of source block
  5203. ((not org-fontify-quote-and-verse-blocks))
  5204. ((string= block-type "quote")
  5205. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5206. ((string= block-type "verse")
  5207. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5208. (add-text-properties beg beg1 '(face org-block-begin-line))
  5209. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5210. '(face org-block-end-line))
  5211. t))
  5212. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5213. (add-text-properties
  5214. beg (match-end 3)
  5215. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5216. '(font-lock-fontified t invisible t)
  5217. '(font-lock-fontified t face org-document-info-keyword)))
  5218. (add-text-properties
  5219. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5220. (if (string-equal dc1 "+title:")
  5221. '(font-lock-fontified t face org-document-title)
  5222. '(font-lock-fontified t face org-document-info))))
  5223. ((or (equal dc1 "+results")
  5224. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5225. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5226. "+call:" "+header:" "+headers:" "+name:"))
  5227. (and (match-end 4) (equal dc3 "+attr")))
  5228. (add-text-properties
  5229. beg (match-end 0)
  5230. '(font-lock-fontified t face org-meta-line))
  5231. t)
  5232. ((member dc3 '(" " ""))
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face font-lock-comment-face)))
  5236. ((not (member (char-after beg) '(?\ ?\t)))
  5237. ;; just any other in-buffer setting, but not indented
  5238. (add-text-properties
  5239. beg (match-end 0)
  5240. '(font-lock-fontified t face org-meta-line))
  5241. t)
  5242. (t nil))))))
  5243. (defun org-activate-angle-links (limit)
  5244. "Run through the buffer and add overlays to links."
  5245. (if (and (re-search-forward org-angle-link-re limit t)
  5246. (not (org-in-src-block-p)))
  5247. (progn
  5248. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5249. (add-text-properties (match-beginning 0) (match-end 0)
  5250. (list 'mouse-face 'highlight
  5251. 'keymap org-mouse-map))
  5252. (org-rear-nonsticky-at (match-end 0))
  5253. t)))
  5254. (defun org-activate-footnote-links (limit)
  5255. "Run through the buffer and add overlays to footnotes."
  5256. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5257. (when fn
  5258. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5259. (org-remove-flyspell-overlays-in beg end)
  5260. (add-text-properties beg end
  5261. (list 'mouse-face 'highlight
  5262. 'keymap org-mouse-map
  5263. 'help-echo
  5264. (if (= (point-at-bol) beg)
  5265. "Footnote definition"
  5266. "Footnote reference")
  5267. 'font-lock-fontified t
  5268. 'font-lock-multiline t
  5269. 'face 'org-footnote))))))
  5270. (defun org-activate-bracket-links (limit)
  5271. "Run through the buffer and add overlays to bracketed links."
  5272. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5273. (not (org-in-src-block-p)))
  5274. (let* ((hl (org-match-string-no-properties 1))
  5275. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5276. (ip (org-maybe-intangible
  5277. (list 'invisible 'org-link
  5278. 'keymap org-mouse-map 'mouse-face 'highlight
  5279. 'font-lock-multiline t 'help-echo help
  5280. 'htmlize-link `(:uri ,hl))))
  5281. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5282. 'font-lock-multiline t 'help-echo help
  5283. 'htmlize-link `(:uri ,hl))))
  5284. ;; We need to remove the invisible property here. Table narrowing
  5285. ;; may have made some of this invisible.
  5286. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5287. (remove-text-properties (match-beginning 0) (match-end 0)
  5288. '(invisible nil))
  5289. (if (match-end 3)
  5290. (progn
  5291. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5292. (org-rear-nonsticky-at (match-beginning 3))
  5293. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5294. (org-rear-nonsticky-at (match-end 3))
  5295. (add-text-properties (match-end 3) (match-end 0) ip)
  5296. (org-rear-nonsticky-at (match-end 0)))
  5297. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5298. (org-rear-nonsticky-at (match-beginning 1))
  5299. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5300. (org-rear-nonsticky-at (match-end 1))
  5301. (add-text-properties (match-end 1) (match-end 0) ip)
  5302. (org-rear-nonsticky-at (match-end 0)))
  5303. t)))
  5304. (defun org-activate-dates (limit)
  5305. "Run through the buffer and add overlays to dates."
  5306. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5307. (not (equal (char-before (match-beginning 0)) 91)))
  5308. (progn
  5309. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5310. (add-text-properties (match-beginning 0) (match-end 0)
  5311. (list 'mouse-face 'highlight
  5312. 'keymap org-mouse-map))
  5313. (org-rear-nonsticky-at (match-end 0))
  5314. (when org-display-custom-times
  5315. (if (match-end 3)
  5316. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5317. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5318. t)))
  5319. (defvar org-target-link-regexp nil
  5320. "Regular expression matching radio targets in plain text.")
  5321. (make-variable-buffer-local 'org-target-link-regexp)
  5322. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5323. "Regular expression matching a link target.")
  5324. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5325. "Regular expression matching a radio target.")
  5326. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5327. "Regular expression matching any target.")
  5328. (defun org-activate-target-links (limit)
  5329. "Run through the buffer and add overlays to target matches."
  5330. (when org-target-link-regexp
  5331. (let ((case-fold-search t))
  5332. (if (re-search-forward org-target-link-regexp limit t)
  5333. (progn
  5334. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5335. (add-text-properties (match-beginning 0) (match-end 0)
  5336. (list 'mouse-face 'highlight
  5337. 'keymap org-mouse-map
  5338. 'help-echo "Radio target link"
  5339. 'org-linked-text t))
  5340. (org-rear-nonsticky-at (match-end 0))
  5341. t)))))
  5342. (defun org-update-radio-target-regexp ()
  5343. "Find all radio targets in this file and update the regular expression."
  5344. (interactive)
  5345. (when (memq 'radio org-activate-links)
  5346. (setq org-target-link-regexp
  5347. (org-make-target-link-regexp (org-all-targets 'radio)))
  5348. (org-restart-font-lock)))
  5349. (defun org-hide-wide-columns (limit)
  5350. (let (s e)
  5351. (setq s (text-property-any (point) (or limit (point-max))
  5352. 'org-cwidth t))
  5353. (when s
  5354. (setq e (next-single-property-change s 'org-cwidth))
  5355. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5356. (goto-char e)
  5357. t)))
  5358. (defvar org-latex-and-related-regexp nil
  5359. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5360. (defvar org-match-substring-regexp)
  5361. (defvar org-match-substring-with-braces-regexp)
  5362. (defun org-compute-latex-and-related-regexp ()
  5363. "Compute regular expression for LaTeX, entities and sub/superscript.
  5364. Result depends on variable `org-highlight-latex-and-related'."
  5365. (org-set-local
  5366. 'org-latex-and-related-regexp
  5367. (let* ((re-sub
  5368. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5369. ((eq org-use-sub-superscripts '{})
  5370. (list org-match-substring-with-braces-regexp))
  5371. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5372. (re-latex
  5373. (when (memq 'latex org-highlight-latex-and-related)
  5374. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5375. (delq nil
  5376. (mapcar (lambda (x)
  5377. (and (member (car x) matchers) (nth 1 x)))
  5378. org-latex-regexps)))))
  5379. (re-entities
  5380. (when (memq 'entities org-highlight-latex-and-related)
  5381. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5382. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5383. (defun org-do-latex-and-related (limit)
  5384. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5385. LIMIT bounds the search for syntax to highlight. Stop at first
  5386. highlighted object, if any. Return t if some highlighting was
  5387. done, nil otherwise."
  5388. (when (org-string-nw-p org-latex-and-related-regexp)
  5389. (catch 'found
  5390. (while (re-search-forward org-latex-and-related-regexp limit t)
  5391. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5392. 'face))
  5393. '(org-code org-verbatim underline))
  5394. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5395. '(?_ ?^))
  5396. 1
  5397. 0)))
  5398. (font-lock-prepend-text-property
  5399. (+ offset (match-beginning 0)) (match-end 0)
  5400. 'face 'org-latex-and-related)
  5401. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5402. '(font-lock-multiline t)))
  5403. (throw 'found t)))
  5404. nil)))
  5405. (defun org-restart-font-lock ()
  5406. "Restart `font-lock-mode', to force refontification."
  5407. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5408. (font-lock-mode -1)
  5409. (font-lock-mode 1)))
  5410. (defun org-all-targets (&optional radio)
  5411. "Return a list of all targets in this file.
  5412. When optional argument RADIO is non-nil, only find radio
  5413. targets."
  5414. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5415. (save-excursion
  5416. (goto-char (point-min))
  5417. (while (re-search-forward re nil t)
  5418. ;; Make sure point is really within the object.
  5419. (backward-char)
  5420. (let ((obj (org-element-context)))
  5421. (when (memq (org-element-type obj) '(radio-target target))
  5422. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5423. rtn)))
  5424. (defun org-make-target-link-regexp (targets)
  5425. "Make regular expression matching all strings in TARGETS.
  5426. The regular expression finds the targets also if there is a line break
  5427. between words."
  5428. (and targets
  5429. (concat
  5430. "\\<\\("
  5431. (mapconcat
  5432. (lambda (x)
  5433. (setq x (regexp-quote x))
  5434. (while (string-match " +" x)
  5435. (setq x (replace-match "\\s-+" t t x)))
  5436. x)
  5437. targets
  5438. "\\|")
  5439. "\\)\\>")))
  5440. (defun org-activate-tags (limit)
  5441. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5442. (progn
  5443. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5444. (add-text-properties (match-beginning 1) (match-end 1)
  5445. (list 'mouse-face 'highlight
  5446. 'keymap org-mouse-map))
  5447. (org-rear-nonsticky-at (match-end 1))
  5448. t)))
  5449. (defun org-outline-level ()
  5450. "Compute the outline level of the heading at point.
  5451. If this is called at a normal headline, the level is the number of stars.
  5452. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5453. (save-excursion
  5454. (if (not (condition-case nil
  5455. (org-back-to-heading t)
  5456. (error nil)))
  5457. 0
  5458. (looking-at org-outline-regexp)
  5459. (1- (- (match-end 0) (match-beginning 0))))))
  5460. (defvar org-font-lock-keywords nil)
  5461. (defsubst org-re-property (property &optional literal)
  5462. "Return a regexp matching a PROPERTY line.
  5463. Match group 3 will be set to the value if it exists."
  5464. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5465. (if literal property (regexp-quote property))
  5466. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5467. (defconst org-property-re
  5468. (org-re-property ".*?" 'literal)
  5469. "Regular expression matching a property line.
  5470. There are four matching groups:
  5471. 1: :PROPKEY: including the leading and trailing colon,
  5472. 2: PROPKEY without the leading and trailing colon,
  5473. 3: PROPVAL without leading or trailing spaces,
  5474. 4: the indentation of the current line,
  5475. 5: trailing whitespace.")
  5476. (defvar org-font-lock-hook nil
  5477. "Functions to be called for special font lock stuff.")
  5478. (defvar org-font-lock-set-keywords-hook nil
  5479. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5480. This is called after `org-font-lock-extra-keywords' is defined, but before
  5481. it is installed to be used by font lock. This can be useful if something
  5482. needs to be inserted at a specific position in the font-lock sequence.")
  5483. (defun org-font-lock-hook (limit)
  5484. "Run `org-font-lock-hook' within LIMIT."
  5485. (run-hook-with-args 'org-font-lock-hook limit))
  5486. (defun org-set-font-lock-defaults ()
  5487. "Set font lock defaults for the current buffer."
  5488. (let* ((em org-fontify-emphasized-text)
  5489. (lk org-activate-links)
  5490. (org-font-lock-extra-keywords
  5491. (list
  5492. ;; Call the hook
  5493. '(org-font-lock-hook)
  5494. ;; Headlines
  5495. `(,(if org-fontify-whole-heading-line
  5496. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5497. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5498. (1 (org-get-level-face 1))
  5499. (2 (org-get-level-face 2))
  5500. (3 (org-get-level-face 3)))
  5501. ;; Table lines
  5502. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5503. (1 'org-table t))
  5504. ;; Table internals
  5505. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5506. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5507. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5508. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5509. ;; Drawers
  5510. (list org-drawer-regexp '(0 'org-special-keyword t))
  5511. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5512. ;; Properties
  5513. (list org-property-re
  5514. '(1 'org-special-keyword t)
  5515. '(3 'org-property-value t))
  5516. ;; Links
  5517. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5518. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5519. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5520. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5521. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5522. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5523. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5524. ;; Targets.
  5525. (list org-any-target-regexp '(0 'org-target t))
  5526. ;; Diary sexps.
  5527. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5528. ;; Macro
  5529. '("{{{.+}}}" (0 'org-macro t))
  5530. '(org-hide-wide-columns (0 nil append))
  5531. ;; TODO keyword
  5532. (list (format org-heading-keyword-regexp-format
  5533. org-todo-regexp)
  5534. '(2 (org-get-todo-face 2) t))
  5535. ;; DONE
  5536. (if org-fontify-done-headline
  5537. (list (format org-heading-keyword-regexp-format
  5538. (concat
  5539. "\\(?:"
  5540. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5541. "\\)"))
  5542. '(2 'org-headline-done t))
  5543. nil)
  5544. ;; Priorities
  5545. '(org-font-lock-add-priority-faces)
  5546. ;; Tags
  5547. '(org-font-lock-add-tag-faces)
  5548. ;; Tags groups
  5549. (if (and org-group-tags org-tag-groups-alist)
  5550. (list (concat org-outline-regexp-bol ".+\\(:"
  5551. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5552. ":\\).*$")
  5553. '(1 'org-tag-group prepend)))
  5554. ;; Special keywords
  5555. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5556. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5557. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5558. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5559. ;; Emphasis
  5560. (if em
  5561. (if (featurep 'xemacs)
  5562. '(org-do-emphasis-faces (0 nil append))
  5563. '(org-do-emphasis-faces)))
  5564. ;; Checkboxes
  5565. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5566. 1 'org-checkbox prepend)
  5567. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5568. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5569. (0 (org-get-checkbox-statistics-face) t)))
  5570. ;; Description list items
  5571. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5572. 1 'org-list-dt prepend)
  5573. ;; ARCHIVEd headings
  5574. (list (concat
  5575. org-outline-regexp-bol
  5576. "\\(.*:" org-archive-tag ":.*\\)")
  5577. '(1 'org-archived prepend))
  5578. ;; Specials
  5579. '(org-do-latex-and-related)
  5580. '(org-fontify-entities)
  5581. '(org-raise-scripts)
  5582. ;; Code
  5583. '(org-activate-code (1 'org-code t))
  5584. ;; COMMENT
  5585. (list (format org-heading-keyword-regexp-format
  5586. (concat "\\("
  5587. org-comment-string "\\|" org-quote-string
  5588. "\\)"))
  5589. '(2 'org-special-keyword t))
  5590. ;; Blocks and meta lines
  5591. '(org-fontify-meta-lines-and-blocks))))
  5592. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5593. (run-hooks 'org-font-lock-set-keywords-hook)
  5594. ;; Now set the full font-lock-keywords
  5595. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5596. (org-set-local 'font-lock-defaults
  5597. '(org-font-lock-keywords t nil nil backward-paragraph))
  5598. (kill-local-variable 'font-lock-keywords) nil))
  5599. (defun org-toggle-pretty-entities ()
  5600. "Toggle the composition display of entities as UTF8 characters."
  5601. (interactive)
  5602. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5603. (org-restart-font-lock)
  5604. (if org-pretty-entities
  5605. (message "Entities are now displayed as UTF8 characters")
  5606. (save-restriction
  5607. (widen)
  5608. (org-decompose-region (point-min) (point-max))
  5609. (message "Entities are now displayed as plain text"))))
  5610. (defvar org-custom-properties-overlays nil
  5611. "List of overlays used for custom properties.")
  5612. (make-variable-buffer-local 'org-custom-properties-overlays)
  5613. (defun org-toggle-custom-properties-visibility ()
  5614. "Display or hide properties in `org-custom-properties'."
  5615. (interactive)
  5616. (if org-custom-properties-overlays
  5617. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5618. (setq org-custom-properties-overlays nil))
  5619. (unless (not org-custom-properties)
  5620. (save-excursion
  5621. (save-restriction
  5622. (widen)
  5623. (goto-char (point-min))
  5624. (while (re-search-forward org-property-re nil t)
  5625. (mapc (lambda(p)
  5626. (when (equal p (substring (match-string 1) 1 -1))
  5627. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5628. (overlay-put o 'invisible t)
  5629. (overlay-put o 'org-custom-property t)
  5630. (push o org-custom-properties-overlays))))
  5631. org-custom-properties)))))))
  5632. (defun org-fontify-entities (limit)
  5633. "Find an entity to fontify."
  5634. (let (ee)
  5635. (when org-pretty-entities
  5636. (catch 'match
  5637. (while (re-search-forward
  5638. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5639. limit t)
  5640. (if (and (not (org-in-indented-comment-line))
  5641. (setq ee (org-entity-get (match-string 1)))
  5642. (= (length (nth 6 ee)) 1))
  5643. (let*
  5644. ((end (if (equal (match-string 2) "{}")
  5645. (match-end 2)
  5646. (match-end 1))))
  5647. (add-text-properties
  5648. (match-beginning 0) end
  5649. (list 'font-lock-fontified t))
  5650. (compose-region (match-beginning 0) end
  5651. (nth 6 ee) nil)
  5652. (backward-char 1)
  5653. (throw 'match t))))
  5654. nil))))
  5655. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5656. "Fontify string S like in Org-mode."
  5657. (with-temp-buffer
  5658. (insert s)
  5659. (let ((org-odd-levels-only odd-levels))
  5660. (org-mode)
  5661. (font-lock-fontify-buffer)
  5662. (buffer-string))))
  5663. (defvar org-m nil)
  5664. (defvar org-l nil)
  5665. (defvar org-f nil)
  5666. (defun org-get-level-face (n)
  5667. "Get the right face for match N in font-lock matching of headlines."
  5668. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5669. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5670. (if org-cycle-level-faces
  5671. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5672. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5673. (cond
  5674. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5675. ((eq n 2) org-f)
  5676. (t (if org-level-color-stars-only nil org-f))))
  5677. (defun org-get-todo-face (kwd)
  5678. "Get the right face for a TODO keyword KWD.
  5679. If KWD is a number, get the corresponding match group."
  5680. (if (numberp kwd) (setq kwd (match-string kwd)))
  5681. (or (org-face-from-face-or-color
  5682. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5683. (and (member kwd org-done-keywords) 'org-done)
  5684. 'org-todo))
  5685. (defun org-face-from-face-or-color (context inherit face-or-color)
  5686. "Create a face list that inherits INHERIT, but sets the foreground color.
  5687. When FACE-OR-COLOR is not a string, just return it."
  5688. (if (stringp face-or-color)
  5689. (list :inherit inherit
  5690. (cdr (assoc context org-faces-easy-properties))
  5691. face-or-color)
  5692. face-or-color))
  5693. (defun org-font-lock-add-tag-faces (limit)
  5694. "Add the special tag faces."
  5695. (when (and org-tag-faces org-tags-special-faces-re)
  5696. (while (re-search-forward org-tags-special-faces-re limit t)
  5697. (add-text-properties (match-beginning 1) (match-end 1)
  5698. (list 'face (org-get-tag-face 1)
  5699. 'font-lock-fontified t))
  5700. (backward-char 1))))
  5701. (defun org-font-lock-add-priority-faces (limit)
  5702. "Add the special priority faces."
  5703. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5704. (when (save-match-data (org-at-heading-p))
  5705. (add-text-properties
  5706. (match-beginning 0) (match-end 0)
  5707. (list 'face (or (org-face-from-face-or-color
  5708. 'priority 'org-priority
  5709. (cdr (assoc (char-after (match-beginning 1))
  5710. org-priority-faces)))
  5711. 'org-priority)
  5712. 'font-lock-fontified t)))))
  5713. (defun org-get-tag-face (kwd)
  5714. "Get the right face for a TODO keyword KWD.
  5715. If KWD is a number, get the corresponding match group."
  5716. (if (numberp kwd) (setq kwd (match-string kwd)))
  5717. (or (org-face-from-face-or-color
  5718. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5719. 'org-tag))
  5720. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5721. "Remove fontification and activation overlays from links."
  5722. (font-lock-default-unfontify-region beg end)
  5723. (let* ((buffer-undo-list t)
  5724. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5725. (inhibit-modification-hooks t)
  5726. deactivate-mark buffer-file-name buffer-file-truename)
  5727. (org-decompose-region beg end)
  5728. (remove-text-properties beg end
  5729. '(mouse-face t keymap t org-linked-text t
  5730. invisible t intangible t
  5731. org-no-flyspell t org-emphasis t))
  5732. (org-remove-font-lock-display-properties beg end)))
  5733. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5734. ((raise 0.3) (height 0.7))
  5735. ((raise -0.5))
  5736. ((raise 0.5)))
  5737. "Display properties for showing superscripts and subscripts.")
  5738. (defun org-remove-font-lock-display-properties (beg end)
  5739. "Remove specific display properties that have been added by font lock.
  5740. The will remove the raise properties that are used to show superscripts
  5741. and subscripts."
  5742. (let (next prop)
  5743. (while (< beg end)
  5744. (setq next (next-single-property-change beg 'display nil end)
  5745. prop (get-text-property beg 'display))
  5746. (if (member prop org-script-display)
  5747. (put-text-property beg next 'display nil))
  5748. (setq beg next))))
  5749. (defun org-raise-scripts (limit)
  5750. "Add raise properties to sub/superscripts."
  5751. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5752. (if (re-search-forward
  5753. (if (eq org-use-sub-superscripts t)
  5754. org-match-substring-regexp
  5755. org-match-substring-with-braces-regexp)
  5756. limit t)
  5757. (let* ((pos (point)) table-p comment-p
  5758. (mpos (match-beginning 3))
  5759. (emph-p (get-text-property mpos 'org-emphasis))
  5760. (link-p (get-text-property mpos 'mouse-face))
  5761. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5762. (goto-char (point-at-bol))
  5763. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5764. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5765. (goto-char pos)
  5766. ;; Handle a_b^c
  5767. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5768. (if (or comment-p emph-p link-p keyw-p)
  5769. t
  5770. (put-text-property (match-beginning 3) (match-end 0)
  5771. 'display
  5772. (if (equal (char-after (match-beginning 2)) ?^)
  5773. (nth (if table-p 3 1) org-script-display)
  5774. (nth (if table-p 2 0) org-script-display)))
  5775. (add-text-properties (match-beginning 2) (match-end 2)
  5776. (list 'invisible t
  5777. 'org-dwidth t 'org-dwidth-n 1))
  5778. (if (and (eq (char-after (match-beginning 3)) ?{)
  5779. (eq (char-before (match-end 3)) ?}))
  5780. (progn
  5781. (add-text-properties
  5782. (match-beginning 3) (1+ (match-beginning 3))
  5783. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5784. (add-text-properties
  5785. (1- (match-end 3)) (match-end 3)
  5786. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5787. t)))))
  5788. ;;;; Visibility cycling, including org-goto and indirect buffer
  5789. ;;; Cycling
  5790. (defvar org-cycle-global-status nil)
  5791. (make-variable-buffer-local 'org-cycle-global-status)
  5792. (put 'org-cycle-global-status 'org-state t)
  5793. (defvar org-cycle-subtree-status nil)
  5794. (make-variable-buffer-local 'org-cycle-subtree-status)
  5795. (put 'org-cycle-subtree-status 'org-state t)
  5796. (defvar org-inlinetask-min-level)
  5797. (defun org-unlogged-message (&rest args)
  5798. "Display a message, but avoid logging it in the *Messages* buffer."
  5799. (let ((message-log-max nil))
  5800. (apply 'message args)))
  5801. ;;;###autoload
  5802. (defun org-cycle (&optional arg)
  5803. "TAB-action and visibility cycling for Org-mode.
  5804. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5805. is outline visibility cycling, but it also invokes other actions
  5806. in special contexts.
  5807. - When this function is called with a prefix argument, rotate the entire
  5808. buffer through 3 states (global cycling)
  5809. 1. OVERVIEW: Show only top-level headlines.
  5810. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5811. 3. SHOW ALL: Show everything.
  5812. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5813. determined by the variable `org-startup-folded', and by any VISIBILITY
  5814. properties in the buffer.
  5815. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5816. including any drawers.
  5817. - When inside a table, re-align the table and move to the next field.
  5818. - When point is at the beginning of a headline, rotate the subtree started
  5819. by this line through 3 different states (local cycling)
  5820. 1. FOLDED: Only the main headline is shown.
  5821. 2. CHILDREN: The main headline and the direct children are shown.
  5822. From this state, you can move to one of the children
  5823. and zoom in further.
  5824. 3. SUBTREE: Show the entire subtree, including body text.
  5825. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5826. - When point is at the beginning of an empty headline and the variable
  5827. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5828. of the headline by demoting and promoting it to likely levels. This
  5829. speeds up creation document structure by pressing TAB once or several
  5830. times right after creating a new headline.
  5831. - When there is a numeric prefix, go up to a heading with level ARG, do
  5832. a `show-subtree' and return to the previous cursor position. If ARG
  5833. is negative, go up that many levels.
  5834. - When point is not at the beginning of a headline, execute the global
  5835. binding for TAB, which is re-indenting the line. See the option
  5836. `org-cycle-emulate-tab' for details.
  5837. - Special case: if point is at the beginning of the buffer and there is
  5838. no headline in line 1, this function will act as if called with prefix arg
  5839. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5840. But only if also the variable `org-cycle-global-at-bob' is t."
  5841. (interactive "P")
  5842. (org-load-modules-maybe)
  5843. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5844. (and org-cycle-level-after-item/entry-creation
  5845. (or (org-cycle-level)
  5846. (org-cycle-item-indentation))))
  5847. (let* ((limit-level
  5848. (or org-cycle-max-level
  5849. (and (boundp 'org-inlinetask-min-level)
  5850. org-inlinetask-min-level
  5851. (1- org-inlinetask-min-level))))
  5852. (nstars (and limit-level
  5853. (if org-odd-levels-only
  5854. (and limit-level (1- (* limit-level 2)))
  5855. limit-level)))
  5856. (org-outline-regexp
  5857. (if (not (derived-mode-p 'org-mode))
  5858. outline-regexp
  5859. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5860. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5861. (not (looking-at org-outline-regexp))))
  5862. (org-cycle-hook
  5863. (if bob-special
  5864. (delq 'org-optimize-window-after-visibility-change
  5865. (copy-sequence org-cycle-hook))
  5866. org-cycle-hook))
  5867. (pos (point)))
  5868. (if (or bob-special (equal arg '(4)))
  5869. ;; special case: use global cycling
  5870. (setq arg t))
  5871. (cond
  5872. ((equal arg '(16))
  5873. (setq last-command 'dummy)
  5874. (org-set-startup-visibility)
  5875. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5876. ((equal arg '(64))
  5877. (show-all)
  5878. (org-unlogged-message "Entire buffer visible, including drawers"))
  5879. ;; Table: enter it or move to the next field.
  5880. ((org-at-table-p 'any)
  5881. (if (org-at-table.el-p)
  5882. (message "Use C-c ' to edit table.el tables")
  5883. (if arg (org-table-edit-field t)
  5884. (org-table-justify-field-maybe)
  5885. (call-interactively 'org-table-next-field))))
  5886. ((run-hook-with-args-until-success
  5887. 'org-tab-after-check-for-table-hook))
  5888. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5889. ((eq arg t) (org-cycle-internal-global))
  5890. ;; Drawers: delegate to `org-flag-drawer'.
  5891. ((and org-drawers org-drawer-regexp
  5892. (save-excursion
  5893. (beginning-of-line 1)
  5894. (looking-at org-drawer-regexp)))
  5895. (org-flag-drawer ; toggle block visibility
  5896. (not (get-char-property (match-end 0) 'invisible))))
  5897. ;; Show-subtree, ARG levels up from here.
  5898. ((integerp arg)
  5899. (save-excursion
  5900. (org-back-to-heading)
  5901. (outline-up-heading (if (< arg 0) (- arg)
  5902. (- (funcall outline-level) arg)))
  5903. (org-show-subtree)))
  5904. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5905. ((and (featurep 'org-inlinetask)
  5906. (org-inlinetask-at-task-p)
  5907. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5908. (org-inlinetask-toggle-visibility))
  5909. ((org-try-cdlatex-tab))
  5910. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5911. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5912. (save-excursion (beginning-of-line 1)
  5913. (looking-at org-outline-regexp)))
  5914. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5915. (org-cycle-internal-local))
  5916. ;; From there: TAB emulation and template completion.
  5917. (buffer-read-only (org-back-to-heading))
  5918. ((run-hook-with-args-until-success
  5919. 'org-tab-after-check-for-cycling-hook))
  5920. ((org-try-structure-completion))
  5921. ((run-hook-with-args-until-success
  5922. 'org-tab-before-tab-emulation-hook))
  5923. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5924. (or (not (bolp))
  5925. (not (looking-at org-outline-regexp))))
  5926. (call-interactively (global-key-binding "\t")))
  5927. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5928. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5929. (or (and (eq org-cycle-emulate-tab 'white)
  5930. (= (match-end 0) (point-at-eol)))
  5931. (and (eq org-cycle-emulate-tab 'whitestart)
  5932. (>= (match-end 0) pos))))
  5933. t
  5934. (eq org-cycle-emulate-tab t))
  5935. (call-interactively (global-key-binding "\t")))
  5936. (t (save-excursion
  5937. (org-back-to-heading)
  5938. (org-cycle)))))))
  5939. (defun org-cycle-internal-global ()
  5940. "Do the global cycling action."
  5941. ;; Hack to avoid display of messages for .org attachments in Gnus
  5942. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5943. (cond
  5944. ((and (eq last-command this-command)
  5945. (eq org-cycle-global-status 'overview))
  5946. ;; We just created the overview - now do table of contents
  5947. ;; This can be slow in very large buffers, so indicate action
  5948. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5949. (unless ga (org-unlogged-message "CONTENTS..."))
  5950. (org-content)
  5951. (unless ga (org-unlogged-message "CONTENTS...done"))
  5952. (setq org-cycle-global-status 'contents)
  5953. (run-hook-with-args 'org-cycle-hook 'contents))
  5954. ((and (eq last-command this-command)
  5955. (eq org-cycle-global-status 'contents))
  5956. ;; We just showed the table of contents - now show everything
  5957. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5958. (show-all)
  5959. (unless ga (org-unlogged-message "SHOW ALL"))
  5960. (setq org-cycle-global-status 'all)
  5961. (run-hook-with-args 'org-cycle-hook 'all))
  5962. (t
  5963. ;; Default action: go to overview
  5964. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5965. (org-overview)
  5966. (unless ga (org-unlogged-message "OVERVIEW"))
  5967. (setq org-cycle-global-status 'overview)
  5968. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5969. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  5970. (defun org-cycle-internal-local ()
  5971. "Do the local cycling action."
  5972. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5973. ;; First, determine end of headline (EOH), end of subtree or item
  5974. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5975. (save-excursion
  5976. (if (org-at-item-p)
  5977. (progn
  5978. (beginning-of-line)
  5979. (setq struct (org-list-struct))
  5980. (setq eoh (point-at-eol))
  5981. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5982. (setq has-children (org-list-has-child-p (point) struct)))
  5983. (org-back-to-heading)
  5984. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5985. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5986. (setq has-children
  5987. (or (save-excursion
  5988. (let ((level (funcall outline-level)))
  5989. (outline-next-heading)
  5990. (and (org-at-heading-p t)
  5991. (> (funcall outline-level) level))))
  5992. (save-excursion
  5993. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5994. ;; Determine end invisible part of buffer (EOL)
  5995. (beginning-of-line 2)
  5996. ;; XEmacs doesn't have `next-single-char-property-change'
  5997. (if (featurep 'xemacs)
  5998. (while (and (not (eobp)) ;; this is like `next-line'
  5999. (get-char-property (1- (point)) 'invisible))
  6000. (beginning-of-line 2))
  6001. (while (and (not (eobp)) ;; this is like `next-line'
  6002. (get-char-property (1- (point)) 'invisible))
  6003. (goto-char (next-single-char-property-change (point) 'invisible))
  6004. (and (eolp) (beginning-of-line 2))))
  6005. (setq eol (point)))
  6006. ;; Find out what to do next and set `this-command'
  6007. (cond
  6008. ((= eos eoh)
  6009. ;; Nothing is hidden behind this heading
  6010. (unless (org-before-first-heading-p)
  6011. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6012. (org-unlogged-message "EMPTY ENTRY")
  6013. (setq org-cycle-subtree-status nil)
  6014. (save-excursion
  6015. (goto-char eos)
  6016. (outline-next-heading)
  6017. (if (outline-invisible-p) (org-flag-heading nil))))
  6018. ((and (or (>= eol eos)
  6019. (not (string-match "\\S-" (buffer-substring eol eos))))
  6020. (or has-children
  6021. (not (setq children-skipped
  6022. org-cycle-skip-children-state-if-no-children))))
  6023. ;; Entire subtree is hidden in one line: children view
  6024. (unless (org-before-first-heading-p)
  6025. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6026. (if (org-at-item-p)
  6027. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6028. (org-show-entry)
  6029. (org-with-limited-levels (show-children))
  6030. ;; FIXME: This slows down the func way too much.
  6031. ;; How keep drawers hidden in subtree anyway?
  6032. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6033. ;; (org-cycle-hide-drawers 'subtree))
  6034. ;; Fold every list in subtree to top-level items.
  6035. (when (eq org-cycle-include-plain-lists 'integrate)
  6036. (save-excursion
  6037. (org-back-to-heading)
  6038. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6039. (beginning-of-line 1)
  6040. (let* ((struct (org-list-struct))
  6041. (prevs (org-list-prevs-alist struct))
  6042. (end (org-list-get-bottom-point struct)))
  6043. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6044. (org-list-get-all-items (point) struct prevs))
  6045. (goto-char (if (< end eos) end eos)))))))
  6046. (org-unlogged-message "CHILDREN")
  6047. (save-excursion
  6048. (goto-char eos)
  6049. (outline-next-heading)
  6050. (if (outline-invisible-p) (org-flag-heading nil)))
  6051. (setq org-cycle-subtree-status 'children)
  6052. (unless (org-before-first-heading-p)
  6053. (run-hook-with-args 'org-cycle-hook 'children)))
  6054. ((or children-skipped
  6055. (and (eq last-command this-command)
  6056. (eq org-cycle-subtree-status 'children)))
  6057. ;; We just showed the children, or no children are there,
  6058. ;; now show everything.
  6059. (unless (org-before-first-heading-p)
  6060. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6061. (outline-flag-region eoh eos nil)
  6062. (org-unlogged-message
  6063. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6064. (setq org-cycle-subtree-status 'subtree)
  6065. (unless (org-before-first-heading-p)
  6066. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6067. (t
  6068. ;; Default action: hide the subtree.
  6069. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6070. (outline-flag-region eoh eos t)
  6071. (org-unlogged-message "FOLDED")
  6072. (setq org-cycle-subtree-status 'folded)
  6073. (unless (org-before-first-heading-p)
  6074. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6075. ;;;###autoload
  6076. (defun org-global-cycle (&optional arg)
  6077. "Cycle the global visibility. For details see `org-cycle'.
  6078. With \\[universal-argument] prefix arg, switch to startup visibility.
  6079. With a numeric prefix, show all headlines up to that level."
  6080. (interactive "P")
  6081. (let ((org-cycle-include-plain-lists
  6082. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6083. (cond
  6084. ((integerp arg)
  6085. (show-all)
  6086. (hide-sublevels arg)
  6087. (setq org-cycle-global-status 'contents))
  6088. ((equal arg '(4))
  6089. (org-set-startup-visibility)
  6090. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6091. (t
  6092. (org-cycle '(4))))))
  6093. (defun org-set-startup-visibility ()
  6094. "Set the visibility required by startup options and properties."
  6095. (cond
  6096. ((eq org-startup-folded t)
  6097. (org-cycle '(4)))
  6098. ((eq org-startup-folded 'content)
  6099. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6100. (org-cycle '(4)) (org-cycle '(4)))))
  6101. (unless (eq org-startup-folded 'showeverything)
  6102. (if org-hide-block-startup (org-hide-block-all))
  6103. (org-set-visibility-according-to-property 'no-cleanup)
  6104. (org-cycle-hide-archived-subtrees 'all)
  6105. (org-cycle-hide-drawers 'all)
  6106. (org-cycle-show-empty-lines t)))
  6107. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6108. "Switch subtree visibilities according to :VISIBILITY: property."
  6109. (interactive)
  6110. (let (org-show-entry-below state)
  6111. (save-excursion
  6112. (goto-char (point-min))
  6113. (while (re-search-forward
  6114. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6115. nil t)
  6116. (setq state (match-string 1))
  6117. (save-excursion
  6118. (org-back-to-heading t)
  6119. (hide-subtree)
  6120. (org-reveal)
  6121. (cond
  6122. ((equal state '("fold" "folded"))
  6123. (hide-subtree))
  6124. ((equal state "children")
  6125. (org-show-hidden-entry)
  6126. (show-children))
  6127. ((equal state "content")
  6128. (save-excursion
  6129. (save-restriction
  6130. (org-narrow-to-subtree)
  6131. (org-content))))
  6132. ((member state '("all" "showall"))
  6133. (show-subtree)))))
  6134. (unless no-cleanup
  6135. (org-cycle-hide-archived-subtrees 'all)
  6136. (org-cycle-hide-drawers 'all)
  6137. (org-cycle-show-empty-lines 'all)))))
  6138. ;; This function uses outline-regexp instead of the more fundamental
  6139. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6140. ;; buffers, where outline-regexp is needed.
  6141. (defun org-overview ()
  6142. "Switch to overview mode, showing only top-level headlines.
  6143. Really, this shows all headlines with level equal or greater than the level
  6144. of the first headline in the buffer. This is important, because if the
  6145. first headline is not level one, then (hide-sublevels 1) gives confusing
  6146. results."
  6147. (interactive)
  6148. (let ((pos (point))
  6149. (level (save-excursion
  6150. (goto-char (point-min))
  6151. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6152. (progn
  6153. (goto-char (match-beginning 0))
  6154. (funcall outline-level))))))
  6155. (and level (hide-sublevels level))
  6156. (recenter '(4))
  6157. (goto-char pos)))
  6158. (defun org-content (&optional arg)
  6159. "Show all headlines in the buffer, like a table of contents.
  6160. With numerical argument N, show content up to level N."
  6161. (interactive "P")
  6162. (save-excursion
  6163. ;; Visit all headings and show their offspring
  6164. (and (integerp arg) (org-overview))
  6165. (goto-char (point-max))
  6166. (catch 'exit
  6167. (while (and (progn (condition-case nil
  6168. (outline-previous-visible-heading 1)
  6169. (error (goto-char (point-min))))
  6170. t)
  6171. (looking-at org-outline-regexp))
  6172. (if (integerp arg)
  6173. (show-children (1- arg))
  6174. (show-branches))
  6175. (if (bobp) (throw 'exit nil))))))
  6176. (defun org-optimize-window-after-visibility-change (state)
  6177. "Adjust the window after a change in outline visibility.
  6178. This function is the default value of the hook `org-cycle-hook'."
  6179. (when (get-buffer-window (current-buffer))
  6180. (cond
  6181. ((eq state 'content) nil)
  6182. ((eq state 'all) nil)
  6183. ((eq state 'folded) nil)
  6184. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6185. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6186. (defun org-remove-empty-overlays-at (pos)
  6187. "Remove outline overlays that do not contain non-white stuff."
  6188. (mapc
  6189. (lambda (o)
  6190. (and (eq 'outline (overlay-get o 'invisible))
  6191. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6192. (overlay-end o))))
  6193. (delete-overlay o)))
  6194. (overlays-at pos)))
  6195. (defun org-clean-visibility-after-subtree-move ()
  6196. "Fix visibility issues after moving a subtree."
  6197. ;; First, find a reasonable region to look at:
  6198. ;; Start two siblings above, end three below
  6199. (let* ((beg (save-excursion
  6200. (and (org-get-last-sibling)
  6201. (org-get-last-sibling))
  6202. (point)))
  6203. (end (save-excursion
  6204. (and (org-get-next-sibling)
  6205. (org-get-next-sibling)
  6206. (org-get-next-sibling))
  6207. (if (org-at-heading-p)
  6208. (point-at-eol)
  6209. (point))))
  6210. (level (looking-at "\\*+"))
  6211. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6212. (save-excursion
  6213. (save-restriction
  6214. (narrow-to-region beg end)
  6215. (when re
  6216. ;; Properly fold already folded siblings
  6217. (goto-char (point-min))
  6218. (while (re-search-forward re nil t)
  6219. (if (and (not (outline-invisible-p))
  6220. (save-excursion
  6221. (goto-char (point-at-eol)) (outline-invisible-p)))
  6222. (hide-entry))))
  6223. (org-cycle-show-empty-lines 'overview)
  6224. (org-cycle-hide-drawers 'overview)))))
  6225. (defun org-cycle-show-empty-lines (state)
  6226. "Show empty lines above all visible headlines.
  6227. The region to be covered depends on STATE when called through
  6228. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6229. entire buffer covered. Note that an empty line is only shown if there
  6230. are at least `org-cycle-separator-lines' empty lines before the headline."
  6231. (when (not (= org-cycle-separator-lines 0))
  6232. (save-excursion
  6233. (let* ((n (abs org-cycle-separator-lines))
  6234. (re (cond
  6235. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6236. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6237. (t (let ((ns (number-to-string (- n 2))))
  6238. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6239. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6240. beg end b e)
  6241. (cond
  6242. ((memq state '(overview contents t))
  6243. (setq beg (point-min) end (point-max)))
  6244. ((memq state '(children folded))
  6245. (setq beg (point) end (progn (org-end-of-subtree t t)
  6246. (beginning-of-line 2)
  6247. (point)))))
  6248. (when beg
  6249. (goto-char beg)
  6250. (while (re-search-forward re end t)
  6251. (unless (get-char-property (match-end 1) 'invisible)
  6252. (setq e (match-end 1))
  6253. (if (< org-cycle-separator-lines 0)
  6254. (setq b (save-excursion
  6255. (goto-char (match-beginning 0))
  6256. (org-back-over-empty-lines)
  6257. (if (save-excursion
  6258. (goto-char (max (point-min) (1- (point))))
  6259. (org-at-heading-p))
  6260. (1- (point))
  6261. (point))))
  6262. (setq b (match-beginning 1)))
  6263. (outline-flag-region b e nil)))))))
  6264. ;; Never hide empty lines at the end of the file.
  6265. (save-excursion
  6266. (goto-char (point-max))
  6267. (outline-previous-heading)
  6268. (outline-end-of-heading)
  6269. (if (and (looking-at "[ \t\n]+")
  6270. (= (match-end 0) (point-max)))
  6271. (outline-flag-region (point) (match-end 0) nil))))
  6272. (defun org-show-empty-lines-in-parent ()
  6273. "Move to the parent and re-show empty lines before visible headlines."
  6274. (save-excursion
  6275. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6276. (org-cycle-show-empty-lines context))))
  6277. (defun org-files-list ()
  6278. "Return `org-agenda-files' list, plus all open org-mode files.
  6279. This is useful for operations that need to scan all of a user's
  6280. open and agenda-wise Org files."
  6281. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6282. (dolist (buf (buffer-list))
  6283. (with-current-buffer buf
  6284. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6285. (let ((file (expand-file-name (buffer-file-name))))
  6286. (unless (member file files)
  6287. (push file files))))))
  6288. files))
  6289. (defsubst org-entry-beginning-position ()
  6290. "Return the beginning position of the current entry."
  6291. (save-excursion (outline-back-to-heading t) (point)))
  6292. (defsubst org-entry-end-position ()
  6293. "Return the end position of the current entry."
  6294. (save-excursion (outline-next-heading) (point)))
  6295. (defun org-cycle-hide-drawers (state)
  6296. "Re-hide all drawers after a visibility state change."
  6297. (when (and (derived-mode-p 'org-mode)
  6298. (not (memq state '(overview folded contents))))
  6299. (save-excursion
  6300. (let* ((globalp (memq state '(contents all)))
  6301. (beg (if globalp (point-min) (point)))
  6302. (end (if globalp (point-max)
  6303. (if (eq state 'children)
  6304. (save-excursion (outline-next-heading) (point))
  6305. (org-end-of-subtree t)))))
  6306. (goto-char beg)
  6307. (while (re-search-forward org-drawer-regexp end t)
  6308. (org-flag-drawer t))))))
  6309. (defun org-cycle-hide-inline-tasks (state)
  6310. "Re-hide inline tasks when switching to 'contents or 'children
  6311. visibility state."
  6312. (case state
  6313. (contents
  6314. (when (org-bound-and-true-p org-inlinetask-min-level)
  6315. (hide-sublevels (1- org-inlinetask-min-level))))
  6316. (children
  6317. (when (featurep 'org-inlinetask)
  6318. (save-excursion
  6319. (while (and (outline-next-heading)
  6320. (org-inlinetask-at-task-p))
  6321. (org-inlinetask-toggle-visibility)
  6322. (org-inlinetask-goto-end)))))))
  6323. (defun org-flag-drawer (flag)
  6324. "When FLAG is non-nil, hide the drawer we are within.
  6325. Otherwise make it visible."
  6326. (save-excursion
  6327. (beginning-of-line 1)
  6328. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6329. (let ((b (match-end 0)))
  6330. (if (re-search-forward
  6331. "^[ \t]*:END:"
  6332. (save-excursion (outline-next-heading) (point)) t)
  6333. (outline-flag-region b (point-at-eol) flag)
  6334. (user-error ":END: line missing at position %s" b))))))
  6335. (defun org-subtree-end-visible-p ()
  6336. "Is the end of the current subtree visible?"
  6337. (pos-visible-in-window-p
  6338. (save-excursion (org-end-of-subtree t) (point))))
  6339. (defun org-first-headline-recenter (&optional N)
  6340. "Move cursor to the first headline and recenter the headline.
  6341. Optional argument N means put the headline into the Nth line of the window."
  6342. (goto-char (point-min))
  6343. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6344. (beginning-of-line)
  6345. (recenter (prefix-numeric-value N))))
  6346. ;;; Saving and restoring visibility
  6347. (defun org-outline-overlay-data (&optional use-markers)
  6348. "Return a list of the locations of all outline overlays.
  6349. These are overlays with the `invisible' property value `outline'.
  6350. The return value is a list of cons cells, with start and stop
  6351. positions for each overlay.
  6352. If USE-MARKERS is set, return the positions as markers."
  6353. (let (beg end)
  6354. (save-excursion
  6355. (save-restriction
  6356. (widen)
  6357. (delq nil
  6358. (mapcar (lambda (o)
  6359. (when (eq (overlay-get o 'invisible) 'outline)
  6360. (setq beg (overlay-start o)
  6361. end (overlay-end o))
  6362. (and beg end (> end beg)
  6363. (if use-markers
  6364. (cons (move-marker (make-marker) beg)
  6365. (move-marker (make-marker) end))
  6366. (cons beg end)))))
  6367. (overlays-in (point-min) (point-max))))))))
  6368. (defun org-set-outline-overlay-data (data)
  6369. "Create visibility overlays for all positions in DATA.
  6370. DATA should have been made by `org-outline-overlay-data'."
  6371. (let (o)
  6372. (save-excursion
  6373. (save-restriction
  6374. (widen)
  6375. (show-all)
  6376. (mapc (lambda (c)
  6377. (outline-flag-region (car c) (cdr c) t))
  6378. data)))))
  6379. ;;; Folding of blocks
  6380. (defvar org-hide-block-overlays nil
  6381. "Overlays hiding blocks.")
  6382. (make-variable-buffer-local 'org-hide-block-overlays)
  6383. (defun org-block-map (function &optional start end)
  6384. "Call FUNCTION at the head of all source blocks in the current buffer.
  6385. Optional arguments START and END can be used to limit the range."
  6386. (let ((start (or start (point-min)))
  6387. (end (or end (point-max))))
  6388. (save-excursion
  6389. (goto-char start)
  6390. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6391. (save-excursion
  6392. (save-match-data
  6393. (goto-char (match-beginning 0))
  6394. (funcall function)))))))
  6395. (defun org-hide-block-toggle-all ()
  6396. "Toggle the visibility of all blocks in the current buffer."
  6397. (org-block-map #'org-hide-block-toggle))
  6398. (defun org-hide-block-all ()
  6399. "Fold all blocks in the current buffer."
  6400. (interactive)
  6401. (org-show-block-all)
  6402. (org-block-map #'org-hide-block-toggle-maybe))
  6403. (defun org-show-block-all ()
  6404. "Unfold all blocks in the current buffer."
  6405. (interactive)
  6406. (mapc 'delete-overlay org-hide-block-overlays)
  6407. (setq org-hide-block-overlays nil))
  6408. (defun org-hide-block-toggle-maybe ()
  6409. "Toggle visibility of block at point."
  6410. (interactive)
  6411. (let ((case-fold-search t))
  6412. (if (save-excursion
  6413. (beginning-of-line 1)
  6414. (looking-at org-block-regexp))
  6415. (progn (org-hide-block-toggle)
  6416. t) ;; to signal that we took action
  6417. nil))) ;; to signal that we did not
  6418. (defun org-hide-block-toggle (&optional force)
  6419. "Toggle the visibility of the current block."
  6420. (interactive)
  6421. (save-excursion
  6422. (beginning-of-line)
  6423. (if (re-search-forward org-block-regexp nil t)
  6424. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6425. (end (match-end 0)) ;; end of entire body
  6426. ov)
  6427. (if (memq t (mapcar (lambda (overlay)
  6428. (eq (overlay-get overlay 'invisible)
  6429. 'org-hide-block))
  6430. (overlays-at start)))
  6431. (if (or (not force) (eq force 'off))
  6432. (mapc (lambda (ov)
  6433. (when (member ov org-hide-block-overlays)
  6434. (setq org-hide-block-overlays
  6435. (delq ov org-hide-block-overlays)))
  6436. (when (eq (overlay-get ov 'invisible)
  6437. 'org-hide-block)
  6438. (delete-overlay ov)))
  6439. (overlays-at start)))
  6440. (setq ov (make-overlay start end))
  6441. (overlay-put ov 'invisible 'org-hide-block)
  6442. ;; make the block accessible to isearch
  6443. (overlay-put
  6444. ov 'isearch-open-invisible
  6445. (lambda (ov)
  6446. (when (member ov org-hide-block-overlays)
  6447. (setq org-hide-block-overlays
  6448. (delq ov org-hide-block-overlays)))
  6449. (when (eq (overlay-get ov 'invisible)
  6450. 'org-hide-block)
  6451. (delete-overlay ov))))
  6452. (push ov org-hide-block-overlays)))
  6453. (user-error "Not looking at a source block"))))
  6454. ;; org-tab-after-check-for-cycling-hook
  6455. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6456. ;; Remove overlays when changing major mode
  6457. (add-hook 'org-mode-hook
  6458. (lambda () (org-add-hook 'change-major-mode-hook
  6459. 'org-show-block-all 'append 'local)))
  6460. ;;; Org-goto
  6461. (defvar org-goto-window-configuration nil)
  6462. (defvar org-goto-marker nil)
  6463. (defvar org-goto-map)
  6464. (defun org-goto-map ()
  6465. "Set the keymap `org-goto'."
  6466. (setq org-goto-map
  6467. (let ((map (make-sparse-keymap)))
  6468. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6469. mouse-drag-region universal-argument org-occur))
  6470. cmd)
  6471. (while (setq cmd (pop cmds))
  6472. (substitute-key-definition cmd cmd map global-map)))
  6473. (suppress-keymap map)
  6474. (org-defkey map "\C-m" 'org-goto-ret)
  6475. (org-defkey map [(return)] 'org-goto-ret)
  6476. (org-defkey map [(left)] 'org-goto-left)
  6477. (org-defkey map [(right)] 'org-goto-right)
  6478. (org-defkey map [(control ?g)] 'org-goto-quit)
  6479. (org-defkey map "\C-i" 'org-cycle)
  6480. (org-defkey map [(tab)] 'org-cycle)
  6481. (org-defkey map [(down)] 'outline-next-visible-heading)
  6482. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6483. (if org-goto-auto-isearch
  6484. (if (fboundp 'define-key-after)
  6485. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6486. nil)
  6487. (org-defkey map "q" 'org-goto-quit)
  6488. (org-defkey map "n" 'outline-next-visible-heading)
  6489. (org-defkey map "p" 'outline-previous-visible-heading)
  6490. (org-defkey map "f" 'outline-forward-same-level)
  6491. (org-defkey map "b" 'outline-backward-same-level)
  6492. (org-defkey map "u" 'outline-up-heading))
  6493. (org-defkey map "/" 'org-occur)
  6494. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6495. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6496. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6497. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6498. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6499. map)))
  6500. (defconst org-goto-help
  6501. "Browse buffer copy, to find location or copy text.%s
  6502. RET=jump to location C-g=quit and return to previous location
  6503. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6504. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6505. (defun org-goto (&optional alternative-interface)
  6506. "Look up a different location in the current file, keeping current visibility.
  6507. When you want look-up or go to a different location in a
  6508. document, the fastest way is often to fold the entire buffer and
  6509. then dive into the tree. This method has the disadvantage, that
  6510. the previous location will be folded, which may not be what you
  6511. want.
  6512. This command works around this by showing a copy of the current
  6513. buffer in an indirect buffer, in overview mode. You can dive
  6514. into the tree in that copy, use org-occur and incremental search
  6515. to find a location. When pressing RET or `Q', the command
  6516. returns to the original buffer in which the visibility is still
  6517. unchanged. After RET it will also jump to the location selected
  6518. in the indirect buffer and expose the headline hierarchy above.
  6519. With a prefix argument, use the alternative interface: e.g. if
  6520. `org-goto-interface' is 'outline use 'outline-path-completion."
  6521. (interactive "P")
  6522. (org-goto-map)
  6523. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6524. (org-refile-use-outline-path t)
  6525. (org-refile-target-verify-function nil)
  6526. (interface
  6527. (if (not alternative-interface)
  6528. org-goto-interface
  6529. (if (eq org-goto-interface 'outline)
  6530. 'outline-path-completion
  6531. 'outline)))
  6532. (org-goto-start-pos (point))
  6533. (selected-point
  6534. (if (eq interface 'outline)
  6535. (car (org-get-location (current-buffer) org-goto-help))
  6536. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6537. (org-refile-check-position pa)
  6538. (nth 3 pa)))))
  6539. (if selected-point
  6540. (progn
  6541. (org-mark-ring-push org-goto-start-pos)
  6542. (goto-char selected-point)
  6543. (if (or (outline-invisible-p) (org-invisible-p2))
  6544. (org-show-context 'org-goto)))
  6545. (message "Quit"))))
  6546. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6547. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6548. (defvar org-goto-local-auto-isearch-map) ; defined below
  6549. (defun org-get-location (buf help)
  6550. "Let the user select a location in the Org-mode buffer BUF.
  6551. This function uses a recursive edit. It returns the selected position
  6552. or nil."
  6553. (org-no-popups
  6554. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6555. (isearch-hide-immediately nil)
  6556. (isearch-search-fun-function
  6557. (lambda () 'org-goto-local-search-headings))
  6558. (org-goto-selected-point org-goto-exit-command))
  6559. (save-excursion
  6560. (save-window-excursion
  6561. (delete-other-windows)
  6562. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6563. (org-pop-to-buffer-same-window
  6564. (condition-case nil
  6565. (make-indirect-buffer (current-buffer) "*org-goto*")
  6566. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6567. (with-output-to-temp-buffer "*Org Help*"
  6568. (princ (format help (if org-goto-auto-isearch
  6569. " Just type for auto-isearch."
  6570. " n/p/f/b/u to navigate, q to quit."))))
  6571. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6572. (setq buffer-read-only nil)
  6573. (let ((org-startup-truncated t)
  6574. (org-startup-folded nil)
  6575. (org-startup-align-all-tables nil))
  6576. (org-mode)
  6577. (org-overview))
  6578. (setq buffer-read-only t)
  6579. (if (and (boundp 'org-goto-start-pos)
  6580. (integer-or-marker-p org-goto-start-pos))
  6581. (let ((org-show-hierarchy-above t)
  6582. (org-show-siblings t)
  6583. (org-show-following-heading t))
  6584. (goto-char org-goto-start-pos)
  6585. (and (outline-invisible-p) (org-show-context)))
  6586. (goto-char (point-min)))
  6587. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6588. (message "Select location and press RET")
  6589. (use-local-map org-goto-map)
  6590. (recursive-edit)))
  6591. (kill-buffer "*org-goto*")
  6592. (cons org-goto-selected-point org-goto-exit-command))))
  6593. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6594. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6595. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6596. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6597. (defun org-goto-local-search-headings (string bound noerror)
  6598. "Search and make sure that any matches are in headlines."
  6599. (catch 'return
  6600. (while (if isearch-forward
  6601. (search-forward string bound noerror)
  6602. (search-backward string bound noerror))
  6603. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6604. (and (member :headline context)
  6605. (not (member :tags context))))
  6606. (throw 'return (point))))))
  6607. (defun org-goto-local-auto-isearch ()
  6608. "Start isearch."
  6609. (interactive)
  6610. (goto-char (point-min))
  6611. (let ((keys (this-command-keys)))
  6612. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6613. (isearch-mode t)
  6614. (isearch-process-search-char (string-to-char keys)))))
  6615. (defun org-goto-ret (&optional arg)
  6616. "Finish `org-goto' by going to the new location."
  6617. (interactive "P")
  6618. (setq org-goto-selected-point (point)
  6619. org-goto-exit-command 'return)
  6620. (throw 'exit nil))
  6621. (defun org-goto-left ()
  6622. "Finish `org-goto' by going to the new location."
  6623. (interactive)
  6624. (if (org-at-heading-p)
  6625. (progn
  6626. (beginning-of-line 1)
  6627. (setq org-goto-selected-point (point)
  6628. org-goto-exit-command 'left)
  6629. (throw 'exit nil))
  6630. (user-error "Not on a heading")))
  6631. (defun org-goto-right ()
  6632. "Finish `org-goto' by going to the new location."
  6633. (interactive)
  6634. (if (org-at-heading-p)
  6635. (progn
  6636. (setq org-goto-selected-point (point)
  6637. org-goto-exit-command 'right)
  6638. (throw 'exit nil))
  6639. (user-error "Not on a heading")))
  6640. (defun org-goto-quit ()
  6641. "Finish `org-goto' without cursor motion."
  6642. (interactive)
  6643. (setq org-goto-selected-point nil)
  6644. (setq org-goto-exit-command 'quit)
  6645. (throw 'exit nil))
  6646. ;;; Indirect buffer display of subtrees
  6647. (defvar org-indirect-dedicated-frame nil
  6648. "This is the frame being used for indirect tree display.")
  6649. (defvar org-last-indirect-buffer nil)
  6650. (defun org-tree-to-indirect-buffer (&optional arg)
  6651. "Create indirect buffer and narrow it to current subtree.
  6652. With a numerical prefix ARG, go up to this level and then take that tree.
  6653. If ARG is negative, go up that many levels.
  6654. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6655. indirect buffer previously made with this command, to avoid proliferation of
  6656. indirect buffers. However, when you call the command with a \
  6657. \\[universal-argument] prefix, or
  6658. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6659. is kept so that you can work with several indirect buffers at the same time.
  6660. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6661. \\[universal-argument] prefix also
  6662. requests that a new frame be made for the new buffer, so that the dedicated
  6663. frame is not changed."
  6664. (interactive "P")
  6665. (let ((cbuf (current-buffer))
  6666. (cwin (selected-window))
  6667. (pos (point))
  6668. beg end level heading ibuf)
  6669. (save-excursion
  6670. (org-back-to-heading t)
  6671. (when (numberp arg)
  6672. (setq level (org-outline-level))
  6673. (if (< arg 0) (setq arg (+ level arg)))
  6674. (while (> (setq level (org-outline-level)) arg)
  6675. (org-up-heading-safe)))
  6676. (setq beg (point)
  6677. heading (org-get-heading))
  6678. (org-end-of-subtree t t)
  6679. (if (org-at-heading-p) (backward-char 1))
  6680. (setq end (point)))
  6681. (if (and (buffer-live-p org-last-indirect-buffer)
  6682. (not (eq org-indirect-buffer-display 'new-frame))
  6683. (not arg))
  6684. (kill-buffer org-last-indirect-buffer))
  6685. (setq ibuf (org-get-indirect-buffer cbuf)
  6686. org-last-indirect-buffer ibuf)
  6687. (cond
  6688. ((or (eq org-indirect-buffer-display 'new-frame)
  6689. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6690. (select-frame (make-frame))
  6691. (delete-other-windows)
  6692. (org-pop-to-buffer-same-window ibuf)
  6693. (org-set-frame-title heading))
  6694. ((eq org-indirect-buffer-display 'dedicated-frame)
  6695. (raise-frame
  6696. (select-frame (or (and org-indirect-dedicated-frame
  6697. (frame-live-p org-indirect-dedicated-frame)
  6698. org-indirect-dedicated-frame)
  6699. (setq org-indirect-dedicated-frame (make-frame)))))
  6700. (delete-other-windows)
  6701. (org-pop-to-buffer-same-window ibuf)
  6702. (org-set-frame-title (concat "Indirect: " heading)))
  6703. ((eq org-indirect-buffer-display 'current-window)
  6704. (org-pop-to-buffer-same-window ibuf))
  6705. ((eq org-indirect-buffer-display 'other-window)
  6706. (pop-to-buffer ibuf))
  6707. (t (error "Invalid value")))
  6708. (if (featurep 'xemacs)
  6709. (save-excursion (org-mode) (turn-on-font-lock)))
  6710. (narrow-to-region beg end)
  6711. (show-all)
  6712. (goto-char pos)
  6713. (run-hook-with-args 'org-cycle-hook 'all)
  6714. (and (window-live-p cwin) (select-window cwin))))
  6715. (defun org-get-indirect-buffer (&optional buffer)
  6716. (setq buffer (or buffer (current-buffer)))
  6717. (let ((n 1) (base (buffer-name buffer)) bname)
  6718. (while (buffer-live-p
  6719. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6720. (setq n (1+ n)))
  6721. (condition-case nil
  6722. (make-indirect-buffer buffer bname 'clone)
  6723. (error (make-indirect-buffer buffer bname)))))
  6724. (defun org-set-frame-title (title)
  6725. "Set the title of the current frame to the string TITLE."
  6726. ;; FIXME: how to name a single frame in XEmacs???
  6727. (unless (featurep 'xemacs)
  6728. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6729. ;;;; Structure editing
  6730. ;;; Inserting headlines
  6731. (defun org-previous-line-empty-p (&optional next)
  6732. "Is the previous line a blank line?
  6733. When NEXT is non-nil, check the next line instead."
  6734. (save-excursion
  6735. (and (not (bobp))
  6736. (or (beginning-of-line (if next 2 0)) t)
  6737. (save-match-data
  6738. (looking-at "[ \t]*$")))))
  6739. (defun org-insert-heading (&optional arg invisible-ok)
  6740. "Insert a new heading or item with same depth at point.
  6741. If point is in a plain list and ARG is nil, create a new list item.
  6742. With one universal prefix argument, insert a heading even in lists.
  6743. With two universal prefix arguments, insert the heading at the end
  6744. of the parent subtree.
  6745. If point is at the beginning of a headline, insert a sibling before
  6746. the current headline. If point is not at the beginning, split the line
  6747. and create a new headline with the text in the current line after point
  6748. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6749. If point is at the beginning of a normal line, turn this line into
  6750. a heading.
  6751. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6752. This is important for non-interactive uses of the command."
  6753. (interactive "P")
  6754. (if (org-called-interactively-p 'any) (org-reveal))
  6755. (let ((itemp (org-in-item-p))
  6756. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6757. (respect-content (or org-insert-heading-respect-content
  6758. (equal arg '(16))))
  6759. (initial-content "")
  6760. (adjust-empty-lines t))
  6761. (cond
  6762. ((or (= (buffer-size) 0)
  6763. (and (not (save-excursion
  6764. (and (ignore-errors (org-back-to-heading invisible-ok))
  6765. (org-at-heading-p))))
  6766. (or arg (not itemp))))
  6767. ;; At beginning of buffer or so high up that only a heading
  6768. ;; makes sense.
  6769. (insert
  6770. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6771. (if (org-in-src-block-p) ",* " "* "))
  6772. (run-hooks 'org-insert-heading-hook))
  6773. ((and itemp (not (equal arg '(4))))
  6774. ;; Insert an item
  6775. (org-insert-item))
  6776. (t
  6777. ;; Insert a heading
  6778. (save-restriction
  6779. (widen)
  6780. (let* ((level nil)
  6781. (on-heading (org-at-heading-p))
  6782. (empty-line-p (if on-heading
  6783. (org-previous-line-empty-p)
  6784. ;; We will decide later
  6785. nil))
  6786. ;; Get a level string to fall back on
  6787. (fix-level
  6788. (save-excursion
  6789. (org-back-to-heading t)
  6790. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6791. (looking-at org-outline-regexp)
  6792. (make-string (1- (length (match-string 0))) ?*)))
  6793. (stars
  6794. (save-excursion
  6795. (condition-case nil
  6796. (progn
  6797. (org-back-to-heading invisible-ok)
  6798. (when (and (not on-heading)
  6799. (featurep 'org-inlinetask)
  6800. (integerp org-inlinetask-min-level)
  6801. (>= (length (match-string 0))
  6802. org-inlinetask-min-level))
  6803. ;; Find a heading level before the inline task
  6804. (while (and (setq level (org-up-heading-safe))
  6805. (>= level org-inlinetask-min-level)))
  6806. (if (org-at-heading-p)
  6807. (org-back-to-heading invisible-ok)
  6808. (error "This should not happen")))
  6809. (unless (and (save-excursion
  6810. (save-match-data
  6811. (org-backward-heading-same-level
  6812. 1 invisible-ok))
  6813. (= (point) (match-beginning 0)))
  6814. (not (org-previous-line-empty-p t)))
  6815. (setq empty-line-p (or empty-line-p
  6816. (org-previous-line-empty-p))))
  6817. (match-string 0))
  6818. (error (or fix-level "* ")))))
  6819. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6820. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6821. pos hide-previous previous-pos)
  6822. ;; If we insert after content, move there and clean up whitespace
  6823. (when respect-content
  6824. (org-end-of-subtree nil t)
  6825. (skip-chars-backward " \r\n")
  6826. (and (looking-at "[ \t]+") (replace-match ""))
  6827. (unless (eobp) (forward-char 1))
  6828. (when (looking-at "^\\*")
  6829. (unless (bobp) (backward-char 1))
  6830. (insert "\n")))
  6831. ;; If we are splitting, grab the text that should be moved to the new headline
  6832. (when may-split
  6833. (if (org-on-heading-p)
  6834. ;; This is a heading, we split intelligently (keeping tags)
  6835. (let ((pos (point)))
  6836. (goto-char (point-at-bol))
  6837. (unless (looking-at org-complex-heading-regexp)
  6838. (error "This should not happen"))
  6839. (when (and (match-beginning 4)
  6840. (> pos (match-beginning 4))
  6841. (< pos (match-end 4)))
  6842. (setq initial-content (buffer-substring pos (match-end 4)))
  6843. (goto-char pos)
  6844. (delete-region (point) (match-end 4))
  6845. (if (looking-at "[ \t]*$")
  6846. (replace-match "")
  6847. (insert (make-string (length initial-content) ?\ )))
  6848. (setq initial-content (org-trim initial-content)))
  6849. (goto-char pos))
  6850. ;; a normal line
  6851. (unless (bolp)
  6852. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6853. (delete-region (point) (point-at-eol))
  6854. (setq initial-content (org-trim initial-content)))))
  6855. ;; If we are at the beginning of the line, insert before it. Else after
  6856. (cond
  6857. ((and (bolp) (looking-at "[ \t]*$")))
  6858. ((and (bolp) (not (looking-at "[ \t]*$")))
  6859. (open-line 1))
  6860. (t
  6861. (goto-char (point-at-eol))
  6862. (insert "\n")))
  6863. ;; Insert the new heading
  6864. (insert stars)
  6865. (just-one-space)
  6866. (insert initial-content)
  6867. (when adjust-empty-lines
  6868. (if (or (not blank)
  6869. (and blank (not (org-previous-line-empty-p))))
  6870. (org-N-empty-lines-before-current (if blank 1 0))))
  6871. (run-hooks 'org-insert-heading-hook)))))))
  6872. (defun org-N-empty-lines-before-current (N)
  6873. "Make the number of empty lines before current exactly N.
  6874. So this will delete or add empty lines."
  6875. (save-excursion
  6876. (goto-char (point-at-bol))
  6877. (if (looking-back "\\s-+" nil 'greedy)
  6878. (replace-match ""))
  6879. (or (bobp) (insert "\n"))
  6880. (while (> N 0)
  6881. (insert "\n")
  6882. (setq N (1- N)))))
  6883. (defun org-get-heading (&optional no-tags no-todo)
  6884. "Return the heading of the current entry, without the stars.
  6885. When NO-TAGS is non-nil, don't include tags.
  6886. When NO-TODO is non-nil, don't include TODO keywords."
  6887. (save-excursion
  6888. (org-back-to-heading t)
  6889. (cond
  6890. ((and no-tags no-todo)
  6891. (looking-at org-complex-heading-regexp)
  6892. (match-string 4))
  6893. (no-tags
  6894. (looking-at (concat org-outline-regexp
  6895. "\\(.*?\\)"
  6896. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6897. (match-string 1))
  6898. (no-todo
  6899. (looking-at org-todo-line-regexp)
  6900. (match-string 3))
  6901. (t (looking-at org-heading-regexp)
  6902. (match-string 2)))))
  6903. (defvar orgstruct-mode) ; defined below
  6904. (defun org-heading-components ()
  6905. "Return the components of the current heading.
  6906. This is a list with the following elements:
  6907. - the level as an integer
  6908. - the reduced level, different if `org-odd-levels-only' is set.
  6909. - the TODO keyword, or nil
  6910. - the priority character, like ?A, or nil if no priority is given
  6911. - the headline text itself, or the tags string if no headline text
  6912. - the tags string, or nil."
  6913. (save-excursion
  6914. (org-back-to-heading t)
  6915. (if (let (case-fold-search)
  6916. (looking-at
  6917. (if orgstruct-mode
  6918. org-heading-regexp
  6919. org-complex-heading-regexp)))
  6920. (if orgstruct-mode
  6921. (list (length (match-string 1))
  6922. (org-reduced-level (length (match-string 1)))
  6923. nil
  6924. nil
  6925. (match-string 2)
  6926. nil)
  6927. (list (length (match-string 1))
  6928. (org-reduced-level (length (match-string 1)))
  6929. (org-match-string-no-properties 2)
  6930. (and (match-end 3) (aref (match-string 3) 2))
  6931. (org-match-string-no-properties 4)
  6932. (org-match-string-no-properties 5))))))
  6933. (defun org-get-entry ()
  6934. "Get the entry text, after heading, entire subtree."
  6935. (save-excursion
  6936. (org-back-to-heading t)
  6937. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6938. (defun org-insert-heading-after-current ()
  6939. "Insert a new heading with same level as current, after current subtree."
  6940. (interactive)
  6941. (org-back-to-heading)
  6942. (org-insert-heading)
  6943. (org-move-subtree-down)
  6944. (end-of-line 1))
  6945. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6946. "Insert heading with `org-insert-heading-respect-content' set to t."
  6947. (interactive "P")
  6948. (let ((org-insert-heading-respect-content t))
  6949. (org-insert-heading '(4) invisible-ok)))
  6950. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6951. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6952. (interactive "P")
  6953. (let ((org-insert-heading-respect-content t))
  6954. (org-insert-todo-heading force-state '(4))))
  6955. (defun org-insert-todo-heading (arg &optional force-heading)
  6956. "Insert a new heading with the same level and TODO state as current heading.
  6957. If the heading has no TODO state, or if the state is DONE, use the first
  6958. state (TODO by default). Also one prefix arg, force first state. With two
  6959. prefix args, force inserting at the end of the parent subtree."
  6960. (interactive "P")
  6961. (when (or force-heading (not (org-insert-item 'checkbox)))
  6962. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6963. force-heading))
  6964. (save-excursion
  6965. (org-back-to-heading)
  6966. (outline-previous-heading)
  6967. (looking-at org-todo-line-regexp))
  6968. (let*
  6969. ((new-mark-x
  6970. (if (or arg
  6971. (not (match-beginning 2))
  6972. (member (match-string 2) org-done-keywords))
  6973. (car org-todo-keywords-1)
  6974. (match-string 2)))
  6975. (new-mark
  6976. (or
  6977. (run-hook-with-args-until-success
  6978. 'org-todo-get-default-hook new-mark-x nil)
  6979. new-mark-x)))
  6980. (beginning-of-line 1)
  6981. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6982. (if org-treat-insert-todo-heading-as-state-change
  6983. (org-todo new-mark)
  6984. (insert new-mark " "))))
  6985. (when org-provide-todo-statistics
  6986. (org-update-parent-todo-statistics))))
  6987. (defun org-insert-subheading (arg)
  6988. "Insert a new subheading and demote it.
  6989. Works for outline headings and for plain lists alike."
  6990. (interactive "P")
  6991. (org-insert-heading arg)
  6992. (cond
  6993. ((org-at-heading-p) (org-do-demote))
  6994. ((org-at-item-p) (org-indent-item))))
  6995. (defun org-insert-todo-subheading (arg)
  6996. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6997. Works for outline headings and for plain lists alike."
  6998. (interactive "P")
  6999. (org-insert-todo-heading arg)
  7000. (cond
  7001. ((org-at-heading-p) (org-do-demote))
  7002. ((org-at-item-p) (org-indent-item))))
  7003. ;;; Promotion and Demotion
  7004. (defvar org-after-demote-entry-hook nil
  7005. "Hook run after an entry has been demoted.
  7006. The cursor will be at the beginning of the entry.
  7007. When a subtree is being demoted, the hook will be called for each node.")
  7008. (defvar org-after-promote-entry-hook nil
  7009. "Hook run after an entry has been promoted.
  7010. The cursor will be at the beginning of the entry.
  7011. When a subtree is being promoted, the hook will be called for each node.")
  7012. (defun org-promote-subtree ()
  7013. "Promote the entire subtree.
  7014. See also `org-promote'."
  7015. (interactive)
  7016. (save-excursion
  7017. (org-with-limited-levels (org-map-tree 'org-promote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-demote-subtree ()
  7020. "Demote the entire subtree. See `org-demote'.
  7021. See also `org-promote'."
  7022. (interactive)
  7023. (save-excursion
  7024. (org-with-limited-levels (org-map-tree 'org-demote)))
  7025. (org-fix-position-after-promote))
  7026. (defun org-do-promote ()
  7027. "Promote the current heading higher up the tree.
  7028. If the region is active in `transient-mark-mode', promote all headings
  7029. in the region."
  7030. (interactive)
  7031. (save-excursion
  7032. (if (org-region-active-p)
  7033. (org-map-region 'org-promote (region-beginning) (region-end))
  7034. (org-promote)))
  7035. (org-fix-position-after-promote))
  7036. (defun org-do-demote ()
  7037. "Demote the current heading lower down the tree.
  7038. If the region is active in `transient-mark-mode', demote all headings
  7039. in the region."
  7040. (interactive)
  7041. (save-excursion
  7042. (if (org-region-active-p)
  7043. (org-map-region 'org-demote (region-beginning) (region-end))
  7044. (org-demote)))
  7045. (org-fix-position-after-promote))
  7046. (defun org-fix-position-after-promote ()
  7047. "Make sure that after pro/demotion cursor position is right."
  7048. (let ((pos (point)))
  7049. (when (save-excursion
  7050. (beginning-of-line 1)
  7051. (looking-at org-todo-line-regexp)
  7052. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7053. (cond ((eobp) (insert " "))
  7054. ((eolp) (insert " "))
  7055. ((equal (char-after) ?\ ) (forward-char 1))))))
  7056. (defun org-current-level ()
  7057. "Return the level of the current entry, or nil if before the first headline.
  7058. The level is the number of stars at the beginning of the headline."
  7059. (save-excursion
  7060. (org-with-limited-levels
  7061. (if (ignore-errors (org-back-to-heading t))
  7062. (funcall outline-level)))))
  7063. (defun org-get-previous-line-level ()
  7064. "Return the outline depth of the last headline before the current line.
  7065. Returns 0 for the first headline in the buffer, and nil if before the
  7066. first headline."
  7067. (let ((current-level (org-current-level))
  7068. (prev-level (when (> (line-number-at-pos) 1)
  7069. (save-excursion
  7070. (beginning-of-line 0)
  7071. (org-current-level)))))
  7072. (cond ((null current-level) nil) ; Before first headline
  7073. ((null prev-level) 0) ; At first headline
  7074. (prev-level))))
  7075. (defun org-reduced-level (l)
  7076. "Compute the effective level of a heading.
  7077. This takes into account the setting of `org-odd-levels-only'."
  7078. (cond
  7079. ((zerop l) 0)
  7080. (org-odd-levels-only (1+ (floor (/ l 2))))
  7081. (t l)))
  7082. (defun org-level-increment ()
  7083. "Return the number of stars that will be added or removed at a
  7084. time to headlines when structure editing, based on the value of
  7085. `org-odd-levels-only'."
  7086. (if org-odd-levels-only 2 1))
  7087. (defun org-get-valid-level (level &optional change)
  7088. "Rectify a level change under the influence of `org-odd-levels-only'
  7089. LEVEL is a current level, CHANGE is by how much the level should be
  7090. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7091. even level numbers will become the next higher odd number."
  7092. (if org-odd-levels-only
  7093. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7094. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7095. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7096. (max 1 (+ level (or change 0)))))
  7097. (if (boundp 'define-obsolete-function-alias)
  7098. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7099. (define-obsolete-function-alias 'org-get-legal-level
  7100. 'org-get-valid-level)
  7101. (define-obsolete-function-alias 'org-get-legal-level
  7102. 'org-get-valid-level "23.1")))
  7103. (defun org-promote ()
  7104. "Promote the current heading higher up the tree.
  7105. If the region is active in `transient-mark-mode', promote all headings
  7106. in the region."
  7107. (org-back-to-heading t)
  7108. (let* ((level (save-match-data (funcall outline-level)))
  7109. (after-change-functions (remove 'flyspell-after-change-function
  7110. after-change-functions))
  7111. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7112. (diff (abs (- level (length up-head) -1))))
  7113. (cond ((and (= level 1) org-called-with-limited-levels
  7114. org-allow-promoting-top-level-subtree)
  7115. (replace-match "# " nil t))
  7116. ((= level 1)
  7117. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7118. (t (replace-match up-head nil t)))
  7119. ;; Fixup tag positioning
  7120. (unless (= level 1)
  7121. (and org-auto-align-tags (org-set-tags nil t))
  7122. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7123. (run-hooks 'org-after-promote-entry-hook)))
  7124. (defun org-demote ()
  7125. "Demote the current heading lower down the tree.
  7126. If the region is active in `transient-mark-mode', demote all headings
  7127. in the region."
  7128. (org-back-to-heading t)
  7129. (let* ((level (save-match-data (funcall outline-level)))
  7130. (after-change-functions (remove 'flyspell-after-change-function
  7131. after-change-functions))
  7132. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7133. (diff (abs (- level (length down-head) -1))))
  7134. (replace-match down-head nil t)
  7135. ;; Fixup tag positioning
  7136. (and org-auto-align-tags (org-set-tags nil t))
  7137. (if org-adapt-indentation (org-fixup-indentation diff))
  7138. (run-hooks 'org-after-demote-entry-hook)))
  7139. (defun org-cycle-level ()
  7140. "Cycle the level of an empty headline through possible states.
  7141. This goes first to child, then to parent, level, then up the hierarchy.
  7142. After top level, it switches back to sibling level."
  7143. (interactive)
  7144. (let ((org-adapt-indentation nil))
  7145. (when (org-point-at-end-of-empty-headline)
  7146. (setq this-command 'org-cycle-level) ; Only needed for caching
  7147. (let ((cur-level (org-current-level))
  7148. (prev-level (org-get-previous-line-level)))
  7149. (cond
  7150. ;; If first headline in file, promote to top-level.
  7151. ((= prev-level 0)
  7152. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7153. do (org-do-promote)))
  7154. ;; If same level as prev, demote one.
  7155. ((= prev-level cur-level)
  7156. (org-do-demote))
  7157. ;; If parent is top-level, promote to top level if not already.
  7158. ((= prev-level 1)
  7159. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7160. do (org-do-promote)))
  7161. ;; If top-level, return to prev-level.
  7162. ((= cur-level 1)
  7163. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7164. do (org-do-demote)))
  7165. ;; If less than prev-level, promote one.
  7166. ((< cur-level prev-level)
  7167. (org-do-promote))
  7168. ;; If deeper than prev-level, promote until higher than
  7169. ;; prev-level.
  7170. ((> cur-level prev-level)
  7171. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7172. do (org-do-promote))))
  7173. t))))
  7174. (defun org-map-tree (fun)
  7175. "Call FUN for every heading underneath the current one."
  7176. (org-back-to-heading)
  7177. (let ((level (funcall outline-level)))
  7178. (save-excursion
  7179. (funcall fun)
  7180. (while (and (progn
  7181. (outline-next-heading)
  7182. (> (funcall outline-level) level))
  7183. (not (eobp)))
  7184. (funcall fun)))))
  7185. (defun org-map-region (fun beg end)
  7186. "Call FUN for every heading between BEG and END."
  7187. (let ((org-ignore-region t))
  7188. (save-excursion
  7189. (setq end (copy-marker end))
  7190. (goto-char beg)
  7191. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7192. (< (point) end))
  7193. (funcall fun))
  7194. (while (and (progn
  7195. (outline-next-heading)
  7196. (< (point) end))
  7197. (not (eobp)))
  7198. (funcall fun)))))
  7199. (defvar org-property-end-re) ; silence byte-compiler
  7200. (defun org-fixup-indentation (diff)
  7201. "Change the indentation in the current entry by DIFF.
  7202. However, if any line in the current entry has no indentation, or if it
  7203. would end up with no indentation after the change, nothing at all is done."
  7204. (save-excursion
  7205. (let ((end (save-excursion (outline-next-heading)
  7206. (point-marker)))
  7207. (prohibit (if (> diff 0)
  7208. "^\\S-"
  7209. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7210. col)
  7211. (unless (save-excursion (end-of-line 1)
  7212. (re-search-forward prohibit end t))
  7213. (while (and (< (point) end)
  7214. (re-search-forward "^[ \t]+" end t))
  7215. (goto-char (match-end 0))
  7216. (setq col (current-column))
  7217. (if (< diff 0) (replace-match ""))
  7218. (org-indent-to-column (+ diff col))))
  7219. (move-marker end nil))))
  7220. (defun org-convert-to-odd-levels ()
  7221. "Convert an org-mode file with all levels allowed to one with odd levels.
  7222. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7223. level 5 etc."
  7224. (interactive)
  7225. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7226. (let ((outline-level 'org-outline-level)
  7227. (org-odd-levels-only nil) n)
  7228. (save-excursion
  7229. (goto-char (point-min))
  7230. (while (re-search-forward "^\\*\\*+ " nil t)
  7231. (setq n (- (length (match-string 0)) 2))
  7232. (while (>= (setq n (1- n)) 0)
  7233. (org-demote))
  7234. (end-of-line 1))))))
  7235. (defun org-convert-to-oddeven-levels ()
  7236. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7237. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7238. file contains a section with an even level, conversion would
  7239. destroy the structure of the file. An error is signaled in this
  7240. case."
  7241. (interactive)
  7242. (goto-char (point-min))
  7243. ;; First check if there are no even levels
  7244. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7245. (org-show-context t)
  7246. (error "Not all levels are odd in this file. Conversion not possible"))
  7247. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7248. (let ((outline-regexp org-outline-regexp)
  7249. (outline-level 'org-outline-level)
  7250. (org-odd-levels-only nil) n)
  7251. (save-excursion
  7252. (goto-char (point-min))
  7253. (while (re-search-forward "^\\*\\*+ " nil t)
  7254. (setq n (/ (1- (length (match-string 0))) 2))
  7255. (while (>= (setq n (1- n)) 0)
  7256. (org-promote))
  7257. (end-of-line 1))))))
  7258. (defun org-tr-level (n)
  7259. "Make N odd if required."
  7260. (if org-odd-levels-only (1+ (/ n 2)) n))
  7261. ;;; Vertical tree motion, cutting and pasting of subtrees
  7262. (defun org-move-subtree-up (&optional arg)
  7263. "Move the current subtree up past ARG headlines of the same level."
  7264. (interactive "p")
  7265. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7266. (defun org-move-subtree-down (&optional arg)
  7267. "Move the current subtree down past ARG headlines of the same level."
  7268. (interactive "p")
  7269. (setq arg (prefix-numeric-value arg))
  7270. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7271. 'org-get-last-sibling))
  7272. (ins-point (make-marker))
  7273. (cnt (abs arg))
  7274. (col (current-column))
  7275. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7276. ;; Select the tree
  7277. (org-back-to-heading)
  7278. (setq beg0 (point))
  7279. (save-excursion
  7280. (setq ne-beg (org-back-over-empty-lines))
  7281. (setq beg (point)))
  7282. (save-match-data
  7283. (save-excursion (outline-end-of-heading)
  7284. (setq folded (outline-invisible-p)))
  7285. (outline-end-of-subtree))
  7286. (outline-next-heading)
  7287. (setq ne-end (org-back-over-empty-lines))
  7288. (setq end (point))
  7289. (goto-char beg0)
  7290. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7291. ;; include less whitespace
  7292. (save-excursion
  7293. (goto-char beg)
  7294. (forward-line (- ne-beg ne-end))
  7295. (setq beg (point))))
  7296. ;; Find insertion point, with error handling
  7297. (while (> cnt 0)
  7298. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7299. (progn (goto-char beg0)
  7300. (user-error "Cannot move past superior level or buffer limit")))
  7301. (setq cnt (1- cnt)))
  7302. (if (> arg 0)
  7303. ;; Moving forward - still need to move over subtree
  7304. (progn (org-end-of-subtree t t)
  7305. (save-excursion
  7306. (org-back-over-empty-lines)
  7307. (or (bolp) (newline)))))
  7308. (setq ne-ins (org-back-over-empty-lines))
  7309. (move-marker ins-point (point))
  7310. (setq txt (buffer-substring beg end))
  7311. (org-save-markers-in-region beg end)
  7312. (delete-region beg end)
  7313. (org-remove-empty-overlays-at beg)
  7314. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7315. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7316. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7317. (let ((bbb (point)))
  7318. (insert-before-markers txt)
  7319. (org-reinstall-markers-in-region bbb)
  7320. (move-marker ins-point bbb))
  7321. (or (bolp) (insert "\n"))
  7322. (setq ins-end (point))
  7323. (goto-char ins-point)
  7324. (org-skip-whitespace)
  7325. (when (and (< arg 0)
  7326. (org-first-sibling-p)
  7327. (> ne-ins ne-beg))
  7328. ;; Move whitespace back to beginning
  7329. (save-excursion
  7330. (goto-char ins-end)
  7331. (let ((kill-whole-line t))
  7332. (kill-line (- ne-ins ne-beg)) (point)))
  7333. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7334. (move-marker ins-point nil)
  7335. (if folded
  7336. (hide-subtree)
  7337. (org-show-entry)
  7338. (show-children)
  7339. (org-cycle-hide-drawers 'children))
  7340. (org-clean-visibility-after-subtree-move)
  7341. ;; move back to the initial column we were at
  7342. (move-to-column col)))
  7343. (defvar org-subtree-clip ""
  7344. "Clipboard for cut and paste of subtrees.
  7345. This is actually only a copy of the kill, because we use the normal kill
  7346. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7347. (defvar org-subtree-clip-folded nil
  7348. "Was the last copied subtree folded?
  7349. This is used to fold the tree back after pasting.")
  7350. (defun org-cut-subtree (&optional n)
  7351. "Cut the current subtree into the clipboard.
  7352. With prefix arg N, cut this many sequential subtrees.
  7353. This is a short-hand for marking the subtree and then cutting it."
  7354. (interactive "p")
  7355. (org-copy-subtree n 'cut))
  7356. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7357. "Copy the current subtree it in the clipboard.
  7358. With prefix arg N, copy this many sequential subtrees.
  7359. This is a short-hand for marking the subtree and then copying it.
  7360. If CUT is non-nil, actually cut the subtree.
  7361. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7362. of some markers in the region, even if CUT is non-nil. This is
  7363. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7364. (interactive "p")
  7365. (let (beg end folded (beg0 (point)))
  7366. (if (org-called-interactively-p 'any)
  7367. (org-back-to-heading nil) ; take what looks like a subtree
  7368. (org-back-to-heading t)) ; take what is really there
  7369. (setq beg (point))
  7370. (skip-chars-forward " \t\r\n")
  7371. (save-match-data
  7372. (if nosubtrees
  7373. (outline-next-heading)
  7374. (save-excursion (outline-end-of-heading)
  7375. (setq folded (outline-invisible-p)))
  7376. (condition-case nil
  7377. (org-forward-heading-same-level (1- n) t)
  7378. (error nil))
  7379. (org-end-of-subtree t t)))
  7380. (setq end (point))
  7381. (goto-char beg0)
  7382. (when (> end beg)
  7383. (setq org-subtree-clip-folded folded)
  7384. (when (or cut force-store-markers)
  7385. (org-save-markers-in-region beg end))
  7386. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7387. (setq org-subtree-clip (current-kill 0))
  7388. (message "%s: Subtree(s) with %d characters"
  7389. (if cut "Cut" "Copied")
  7390. (length org-subtree-clip)))))
  7391. (defun org-paste-subtree (&optional level tree for-yank)
  7392. "Paste the clipboard as a subtree, with modification of headline level.
  7393. The entire subtree is promoted or demoted in order to match a new headline
  7394. level.
  7395. If the cursor is at the beginning of a headline, the same level as
  7396. that headline is used to paste the tree.
  7397. If not, the new level is derived from the *visible* headings
  7398. before and after the insertion point, and taken to be the inferior headline
  7399. level of the two. So if the previous visible heading is level 3 and the
  7400. next is level 4 (or vice versa), level 4 will be used for insertion.
  7401. This makes sure that the subtree remains an independent subtree and does
  7402. not swallow low level entries.
  7403. You can also force a different level, either by using a numeric prefix
  7404. argument, or by inserting the heading marker by hand. For example, if the
  7405. cursor is after \"*****\", then the tree will be shifted to level 5.
  7406. If optional TREE is given, use this text instead of the kill ring.
  7407. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7408. move back over whitespace before inserting, and move point to the end of
  7409. the inserted text when done."
  7410. (interactive "P")
  7411. (setq tree (or tree (and kill-ring (current-kill 0))))
  7412. (unless (org-kill-is-subtree-p tree)
  7413. (user-error "%s"
  7414. (substitute-command-keys
  7415. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7416. (org-with-limited-levels
  7417. (let* ((visp (not (outline-invisible-p)))
  7418. (txt tree)
  7419. (^re_ "\\(\\*+\\)[ \t]*")
  7420. (old-level (if (string-match org-outline-regexp-bol txt)
  7421. (- (match-end 0) (match-beginning 0) 1)
  7422. -1))
  7423. (force-level (cond (level (prefix-numeric-value level))
  7424. ((and (looking-at "[ \t]*$")
  7425. (string-match
  7426. "^\\*+$" (buffer-substring
  7427. (point-at-bol) (point))))
  7428. (- (match-end 1) (match-beginning 1)))
  7429. ((and (bolp)
  7430. (looking-at org-outline-regexp))
  7431. (- (match-end 0) (point) 1))))
  7432. (previous-level (save-excursion
  7433. (condition-case nil
  7434. (progn
  7435. (outline-previous-visible-heading 1)
  7436. (if (looking-at ^re_)
  7437. (- (match-end 0) (match-beginning 0) 1)
  7438. 1))
  7439. (error 1))))
  7440. (next-level (save-excursion
  7441. (condition-case nil
  7442. (progn
  7443. (or (looking-at org-outline-regexp)
  7444. (outline-next-visible-heading 1))
  7445. (if (looking-at ^re_)
  7446. (- (match-end 0) (match-beginning 0) 1)
  7447. 1))
  7448. (error 1))))
  7449. (new-level (or force-level (max previous-level next-level)))
  7450. (shift (if (or (= old-level -1)
  7451. (= new-level -1)
  7452. (= old-level new-level))
  7453. 0
  7454. (- new-level old-level)))
  7455. (delta (if (> shift 0) -1 1))
  7456. (func (if (> shift 0) 'org-demote 'org-promote))
  7457. (org-odd-levels-only nil)
  7458. beg end newend)
  7459. ;; Remove the forced level indicator
  7460. (if force-level
  7461. (delete-region (point-at-bol) (point)))
  7462. ;; Paste
  7463. (beginning-of-line (if (bolp) 1 2))
  7464. (setq beg (point))
  7465. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7466. (insert-before-markers txt)
  7467. (unless (string-match "\n\\'" txt) (insert "\n"))
  7468. (setq newend (point))
  7469. (org-reinstall-markers-in-region beg)
  7470. (setq end (point))
  7471. (goto-char beg)
  7472. (skip-chars-forward " \t\n\r")
  7473. (setq beg (point))
  7474. (if (and (outline-invisible-p) visp)
  7475. (save-excursion (outline-show-heading)))
  7476. ;; Shift if necessary
  7477. (unless (= shift 0)
  7478. (save-restriction
  7479. (narrow-to-region beg end)
  7480. (while (not (= shift 0))
  7481. (org-map-region func (point-min) (point-max))
  7482. (setq shift (+ delta shift)))
  7483. (goto-char (point-min))
  7484. (setq newend (point-max))))
  7485. (when (or (org-called-interactively-p 'interactive) for-yank)
  7486. (message "Clipboard pasted as level %d subtree" new-level))
  7487. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7488. kill-ring
  7489. (eq org-subtree-clip (current-kill 0))
  7490. org-subtree-clip-folded)
  7491. ;; The tree was folded before it was killed/copied
  7492. (hide-subtree))
  7493. (and for-yank (goto-char newend)))))
  7494. (defun org-kill-is-subtree-p (&optional txt)
  7495. "Check if the current kill is an outline subtree, or a set of trees.
  7496. Returns nil if kill does not start with a headline, or if the first
  7497. headline level is not the largest headline level in the tree.
  7498. So this will actually accept several entries of equal levels as well,
  7499. which is OK for `org-paste-subtree'.
  7500. If optional TXT is given, check this string instead of the current kill."
  7501. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7502. (re (org-get-limited-outline-regexp))
  7503. (^re (concat "^" re))
  7504. (start-level (and kill
  7505. (string-match
  7506. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7507. kill)
  7508. (- (match-end 2) (match-beginning 2) 1)))
  7509. (start (1+ (or (match-beginning 2) -1))))
  7510. (if (not start-level)
  7511. (progn
  7512. nil) ;; does not even start with a heading
  7513. (catch 'exit
  7514. (while (setq start (string-match ^re kill (1+ start)))
  7515. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7516. (throw 'exit nil)))
  7517. t))))
  7518. (defvar org-markers-to-move nil
  7519. "Markers that should be moved with a cut-and-paste operation.
  7520. Those markers are stored together with their positions relative to
  7521. the start of the region.")
  7522. (defun org-save-markers-in-region (beg end)
  7523. "Check markers in region.
  7524. If these markers are between BEG and END, record their position relative
  7525. to BEG, so that after moving the block of text, we can put the markers back
  7526. into place.
  7527. This function gets called just before an entry or tree gets cut from the
  7528. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7529. called immediately, to move the markers with the entries."
  7530. (setq org-markers-to-move nil)
  7531. (when (featurep 'org-clock)
  7532. (org-clock-save-markers-for-cut-and-paste beg end))
  7533. (when (featurep 'org-agenda)
  7534. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7535. (defun org-check-and-save-marker (marker beg end)
  7536. "Check if MARKER is between BEG and END.
  7537. If yes, remember the marker and the distance to BEG."
  7538. (when (and (marker-buffer marker)
  7539. (equal (marker-buffer marker) (current-buffer)))
  7540. (if (and (>= marker beg) (< marker end))
  7541. (push (cons marker (- marker beg)) org-markers-to-move))))
  7542. (defun org-reinstall-markers-in-region (beg)
  7543. "Move all remembered markers to their position relative to BEG."
  7544. (mapc (lambda (x)
  7545. (move-marker (car x) (+ beg (cdr x))))
  7546. org-markers-to-move)
  7547. (setq org-markers-to-move nil))
  7548. (defun org-narrow-to-subtree ()
  7549. "Narrow buffer to the current subtree."
  7550. (interactive)
  7551. (save-excursion
  7552. (save-match-data
  7553. (org-with-limited-levels
  7554. (narrow-to-region
  7555. (progn (org-back-to-heading t) (point))
  7556. (progn (org-end-of-subtree t t)
  7557. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7558. (point)))))))
  7559. (defun org-narrow-to-block ()
  7560. "Narrow buffer to the current block."
  7561. (interactive)
  7562. (let* ((case-fold-search t)
  7563. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7564. "^[ \t]*#\\+end_.*")))
  7565. (if blockp
  7566. (narrow-to-region (car blockp) (cdr blockp))
  7567. (user-error "Not in a block"))))
  7568. (eval-when-compile
  7569. (defvar org-property-drawer-re))
  7570. (defvar org-property-start-re) ;; defined below
  7571. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7572. "Clone the task (subtree) at point N times.
  7573. The clones will be inserted as siblings.
  7574. In interactive use, the user will be prompted for the number of
  7575. clones to be produced. If the entry has a timestamp, the user
  7576. will also be prompted for a time shift, which may be a repeater
  7577. as used in time stamps, for example `+3d'. To disable this,
  7578. you can call the function with a universal prefix argument.
  7579. When a valid repeater is given and the entry contains any time
  7580. stamps, the clones will become a sequence in time, with time
  7581. stamps in the subtree shifted for each clone produced. If SHIFT
  7582. is nil or the empty string, time stamps will be left alone. The
  7583. ID property of the original subtree is removed.
  7584. If the original subtree did contain time stamps with a repeater,
  7585. the following will happen:
  7586. - the repeater will be removed in each clone
  7587. - an additional clone will be produced, with the current, unshifted
  7588. date(s) in the entry.
  7589. - the original entry will be placed *after* all the clones, with
  7590. repeater intact.
  7591. - the start days in the repeater in the original entry will be shifted
  7592. to past the last clone.
  7593. In this way you can spell out a number of instances of a repeating task,
  7594. and still retain the repeater to cover future instances of the task."
  7595. (interactive "nNumber of clones to produce: ")
  7596. (let ((shift
  7597. (or shift
  7598. (if (and (not (equal current-prefix-arg '(4)))
  7599. (save-excursion
  7600. (re-search-forward org-ts-regexp-both
  7601. (save-excursion
  7602. (org-end-of-subtree t)
  7603. (point)) t)))
  7604. (read-from-minibuffer
  7605. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7606. ""))) ;; No time shift
  7607. (n-no-remove -1)
  7608. (drawer-re org-drawer-regexp)
  7609. beg end template task idprop
  7610. shift-n shift-what doshift nmin nmax)
  7611. (if (not (and (integerp n) (> n 0)))
  7612. (error "Invalid number of replications %s" n))
  7613. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7614. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7615. shift)))
  7616. (error "Invalid shift specification %s" shift))
  7617. (when doshift
  7618. (setq shift-n (string-to-number (match-string 1 shift))
  7619. shift-what (cdr (assoc (match-string 2 shift)
  7620. '(("d" . day) ("w" . week)
  7621. ("m" . month) ("y" . year))))))
  7622. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7623. (setq nmin 1 nmax n)
  7624. (org-back-to-heading t)
  7625. (setq beg (point))
  7626. (setq idprop (org-entry-get nil "ID"))
  7627. (org-end-of-subtree t t)
  7628. (or (bolp) (insert "\n"))
  7629. (setq end (point))
  7630. (setq template (buffer-substring beg end))
  7631. (when (and doshift
  7632. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7633. (delete-region beg end)
  7634. (setq end beg)
  7635. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7636. (goto-char end)
  7637. (loop for n from nmin to nmax do
  7638. ;; prepare clone
  7639. (with-temp-buffer
  7640. (insert template)
  7641. (org-mode)
  7642. (goto-char (point-min))
  7643. (org-show-subtree)
  7644. (and idprop (if org-clone-delete-id
  7645. (org-entry-delete nil "ID")
  7646. (org-id-get-create t)))
  7647. (unless (= n 0)
  7648. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7649. (kill-whole-line))
  7650. (goto-char (point-min))
  7651. (while (re-search-forward drawer-re nil t)
  7652. (mapc (lambda (d)
  7653. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7654. (goto-char (point-min))
  7655. (when doshift
  7656. (while (re-search-forward org-ts-regexp-both nil t)
  7657. (org-timestamp-change (* n shift-n) shift-what))
  7658. (unless (= n n-no-remove)
  7659. (goto-char (point-min))
  7660. (while (re-search-forward org-ts-regexp nil t)
  7661. (save-excursion
  7662. (goto-char (match-beginning 0))
  7663. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7664. (delete-region (match-beginning 1) (match-end 1)))))))
  7665. (setq task (buffer-string)))
  7666. (insert task))
  7667. (goto-char beg)))
  7668. ;;; Outline Sorting
  7669. (defun org-sort (with-case)
  7670. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7671. Optional argument WITH-CASE means sort case-sensitively."
  7672. (interactive "P")
  7673. (cond
  7674. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7675. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7676. (t
  7677. (org-call-with-arg 'org-sort-entries with-case))))
  7678. (defun org-sort-remove-invisible (s)
  7679. "Remove invisible links from string S."
  7680. (remove-text-properties 0 (length s) org-rm-props s)
  7681. (while (string-match org-bracket-link-regexp s)
  7682. (setq s (replace-match (if (match-end 2)
  7683. (match-string 3 s)
  7684. (match-string 1 s)) t t s)))
  7685. (let ((st (format " %s " s)))
  7686. (while (string-match org-emph-re st)
  7687. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7688. (setq s (substring st 1 -1)))
  7689. s)
  7690. (defvar org-priority-regexp) ; defined later in the file
  7691. (defvar org-after-sorting-entries-or-items-hook nil
  7692. "Hook that is run after a bunch of entries or items have been sorted.
  7693. When children are sorted, the cursor is in the parent line when this
  7694. hook gets called. When a region or a plain list is sorted, the cursor
  7695. will be in the first entry of the sorted region/list.")
  7696. (defun org-sort-entries
  7697. (&optional with-case sorting-type getkey-func compare-func property)
  7698. "Sort entries on a certain level of an outline tree.
  7699. If there is an active region, the entries in the region are sorted.
  7700. Else, if the cursor is before the first entry, sort the top-level items.
  7701. Else, the children of the entry at point are sorted.
  7702. Sorting can be alphabetically, numerically, by date/time as given by
  7703. a time stamp, by a property, by priority order, or by a custom function.
  7704. The command prompts for the sorting type unless it has been given to the
  7705. function through the SORTING-TYPE argument, which needs to be a character,
  7706. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7707. precise meaning of each character:
  7708. n Numerically, by converting the beginning of the entry/item to a number.
  7709. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7710. o By order of TODO keywords.
  7711. t By date/time, either the first active time stamp in the entry, or, if
  7712. none exist, by the first inactive one.
  7713. s By the scheduled date/time.
  7714. d By deadline date/time.
  7715. c By creation time, which is assumed to be the first inactive time stamp
  7716. at the beginning of a line.
  7717. p By priority according to the cookie.
  7718. r By the value of a property.
  7719. Capital letters will reverse the sort order.
  7720. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7721. called with point at the beginning of the record. It must return either
  7722. a string or a number that should serve as the sorting key for that record.
  7723. Comparing entries ignores case by default. However, with an optional argument
  7724. WITH-CASE, the sorting considers case as well.
  7725. Sorting is done against the visible part of the headlines, it ignores hidden
  7726. links."
  7727. (interactive "P")
  7728. (let ((case-func (if with-case 'identity 'downcase))
  7729. (cmstr
  7730. ;; The clock marker is lost when using `sort-subr', let's
  7731. ;; store the clocking string.
  7732. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7733. (save-excursion
  7734. (goto-char org-clock-marker)
  7735. (looking-back "^.*") (match-string-no-properties 0))))
  7736. start beg end stars re re2
  7737. txt what tmp)
  7738. ;; Find beginning and end of region to sort
  7739. (cond
  7740. ((org-region-active-p)
  7741. ;; we will sort the region
  7742. (setq end (region-end)
  7743. what "region")
  7744. (goto-char (region-beginning))
  7745. (if (not (org-at-heading-p)) (outline-next-heading))
  7746. (setq start (point)))
  7747. ((or (org-at-heading-p)
  7748. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7749. ;; we will sort the children of the current headline
  7750. (org-back-to-heading)
  7751. (setq start (point)
  7752. end (progn (org-end-of-subtree t t)
  7753. (or (bolp) (insert "\n"))
  7754. (org-back-over-empty-lines)
  7755. (point))
  7756. what "children")
  7757. (goto-char start)
  7758. (show-subtree)
  7759. (outline-next-heading))
  7760. (t
  7761. ;; we will sort the top-level entries in this file
  7762. (goto-char (point-min))
  7763. (or (org-at-heading-p) (outline-next-heading))
  7764. (setq start (point))
  7765. (goto-char (point-max))
  7766. (beginning-of-line 1)
  7767. (when (looking-at ".*?\\S-")
  7768. ;; File ends in a non-white line
  7769. (end-of-line 1)
  7770. (insert "\n"))
  7771. (setq end (point-max))
  7772. (setq what "top-level")
  7773. (goto-char start)
  7774. (show-all)))
  7775. (setq beg (point))
  7776. (if (>= beg end) (user-error "Nothing to sort"))
  7777. (looking-at "\\(\\*+\\)")
  7778. (setq stars (match-string 1)
  7779. re (concat "^" (regexp-quote stars) " +")
  7780. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7781. txt (buffer-substring beg end))
  7782. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7783. (if (and (not (equal stars "*")) (string-match re2 txt))
  7784. (user-error "Region to sort contains a level above the first entry"))
  7785. (unless sorting-type
  7786. (message
  7787. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7788. [t]ime [s]cheduled [d]eadline [c]reated
  7789. A/N/P/R/O/F/T/S/D/C means reversed:"
  7790. what)
  7791. (setq sorting-type (read-char-exclusive))
  7792. (unless getkey-func
  7793. (and (= (downcase sorting-type) ?f)
  7794. (setq getkey-func
  7795. (org-icompleting-read "Sort using function: "
  7796. obarray 'fboundp t nil nil))
  7797. (setq getkey-func (intern getkey-func))))
  7798. (and (= (downcase sorting-type) ?r)
  7799. (not property)
  7800. (setq property
  7801. (org-icompleting-read "Property: "
  7802. (mapcar 'list (org-buffer-property-keys t))
  7803. nil t))))
  7804. (message "Sorting entries...")
  7805. (save-restriction
  7806. (narrow-to-region start end)
  7807. (let ((dcst (downcase sorting-type))
  7808. (case-fold-search nil)
  7809. (now (current-time)))
  7810. (sort-subr
  7811. (/= dcst sorting-type)
  7812. ;; This function moves to the beginning character of the "record" to
  7813. ;; be sorted.
  7814. (lambda nil
  7815. (if (re-search-forward re nil t)
  7816. (goto-char (match-beginning 0))
  7817. (goto-char (point-max))))
  7818. ;; This function moves to the last character of the "record" being
  7819. ;; sorted.
  7820. (lambda nil
  7821. (save-match-data
  7822. (condition-case nil
  7823. (outline-forward-same-level 1)
  7824. (error
  7825. (goto-char (point-max))))))
  7826. ;; This function returns the value that gets sorted against.
  7827. (lambda nil
  7828. (cond
  7829. ((= dcst ?n)
  7830. (if (looking-at org-complex-heading-regexp)
  7831. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7832. nil))
  7833. ((= dcst ?a)
  7834. (if (looking-at org-complex-heading-regexp)
  7835. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7836. nil))
  7837. ((= dcst ?t)
  7838. (let ((end (save-excursion (outline-next-heading) (point))))
  7839. (if (or (re-search-forward org-ts-regexp end t)
  7840. (re-search-forward org-ts-regexp-both end t))
  7841. (org-time-string-to-seconds (match-string 0))
  7842. (org-float-time now))))
  7843. ((= dcst ?c)
  7844. (let ((end (save-excursion (outline-next-heading) (point))))
  7845. (if (re-search-forward
  7846. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7847. end t)
  7848. (org-time-string-to-seconds (match-string 0))
  7849. (org-float-time now))))
  7850. ((= dcst ?s)
  7851. (let ((end (save-excursion (outline-next-heading) (point))))
  7852. (if (re-search-forward org-scheduled-time-regexp end t)
  7853. (org-time-string-to-seconds (match-string 1))
  7854. (org-float-time now))))
  7855. ((= dcst ?d)
  7856. (let ((end (save-excursion (outline-next-heading) (point))))
  7857. (if (re-search-forward org-deadline-time-regexp end t)
  7858. (org-time-string-to-seconds (match-string 1))
  7859. (org-float-time now))))
  7860. ((= dcst ?p)
  7861. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7862. (string-to-char (match-string 2))
  7863. org-default-priority))
  7864. ((= dcst ?r)
  7865. (or (org-entry-get nil property) ""))
  7866. ((= dcst ?o)
  7867. (if (looking-at org-complex-heading-regexp)
  7868. (- 9999 (length (member (match-string 2)
  7869. org-todo-keywords-1)))))
  7870. ((= dcst ?f)
  7871. (if getkey-func
  7872. (progn
  7873. (setq tmp (funcall getkey-func))
  7874. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7875. tmp)
  7876. (error "Invalid key function `%s'" getkey-func)))
  7877. (t (error "Invalid sorting type `%c'" sorting-type))))
  7878. nil
  7879. (cond
  7880. ((= dcst ?a) 'string<)
  7881. ((= dcst ?f) compare-func)
  7882. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7883. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7884. ;; Reset the clock marker if needed
  7885. (when cmstr
  7886. (save-excursion
  7887. (goto-char start)
  7888. (search-forward cmstr nil t)
  7889. (move-marker org-clock-marker (point))))
  7890. (message "Sorting entries...done")))
  7891. (defun org-do-sort (table what &optional with-case sorting-type)
  7892. "Sort TABLE of WHAT according to SORTING-TYPE.
  7893. The user will be prompted for the SORTING-TYPE if the call to this
  7894. function does not specify it. WHAT is only for the prompt, to indicate
  7895. what is being sorted. The sorting key will be extracted from
  7896. the car of the elements of the table.
  7897. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7898. (unless sorting-type
  7899. (message
  7900. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7901. what)
  7902. (setq sorting-type (read-char-exclusive)))
  7903. (let ((dcst (downcase sorting-type))
  7904. extractfun comparefun)
  7905. ;; Define the appropriate functions
  7906. (cond
  7907. ((= dcst ?n)
  7908. (setq extractfun 'string-to-number
  7909. comparefun (if (= dcst sorting-type) '< '>)))
  7910. ((= dcst ?a)
  7911. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7912. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7913. comparefun (if (= dcst sorting-type)
  7914. 'string<
  7915. (lambda (a b) (and (not (string< a b))
  7916. (not (string= a b)))))))
  7917. ((= dcst ?t)
  7918. (setq extractfun
  7919. (lambda (x)
  7920. (if (or (string-match org-ts-regexp x)
  7921. (string-match org-ts-regexp-both x))
  7922. (org-float-time
  7923. (org-time-string-to-time (match-string 0 x)))
  7924. 0))
  7925. comparefun (if (= dcst sorting-type) '< '>)))
  7926. (t (error "Invalid sorting type `%c'" sorting-type)))
  7927. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7928. table)
  7929. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7930. ;;; The orgstruct minor mode
  7931. ;; Define a minor mode which can be used in other modes in order to
  7932. ;; integrate the org-mode structure editing commands.
  7933. ;; This is really a hack, because the org-mode structure commands use
  7934. ;; keys which normally belong to the major mode. Here is how it
  7935. ;; works: The minor mode defines all the keys necessary to operate the
  7936. ;; structure commands, but wraps the commands into a function which
  7937. ;; tests if the cursor is currently at a headline or a plain list
  7938. ;; item. If that is the case, the structure command is used,
  7939. ;; temporarily setting many Org-mode variables like regular
  7940. ;; expressions for filling etc. However, when any of those keys is
  7941. ;; used at a different location, function uses `key-binding' to look
  7942. ;; up if the key has an associated command in another currently active
  7943. ;; keymap (minor modes, major mode, global), and executes that
  7944. ;; command. There might be problems if any of the keys is otherwise
  7945. ;; used as a prefix key.
  7946. (defcustom orgstruct-heading-prefix-regexp ""
  7947. "Regexp that matches the custom prefix of Org headlines in
  7948. orgstruct(++)-mode."
  7949. :group 'org
  7950. :version "24.4"
  7951. :package-version '(Org . "8.3")
  7952. :type 'regexp)
  7953. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7954. (defcustom orgstruct-setup-hook nil
  7955. "Hook run after orgstruct-mode-map is filled."
  7956. :group 'org
  7957. :version "24.4"
  7958. :package-version '(Org . "8.0")
  7959. :type 'hook)
  7960. (defvar orgstruct-initialized nil)
  7961. (defvar org-local-vars nil
  7962. "List of local variables, for use by `orgstruct-mode'.")
  7963. ;;;###autoload
  7964. (define-minor-mode orgstruct-mode
  7965. "Toggle the minor mode `orgstruct-mode'.
  7966. This mode is for using Org-mode structure commands in other
  7967. modes. The following keys behave as if Org-mode were active, if
  7968. the cursor is on a headline, or on a plain list item (both as
  7969. defined by Org-mode)."
  7970. nil " OrgStruct" (make-sparse-keymap)
  7971. (funcall (if orgstruct-mode
  7972. 'add-to-invisibility-spec
  7973. 'remove-from-invisibility-spec)
  7974. '(outline . t))
  7975. (when orgstruct-mode
  7976. (org-load-modules-maybe)
  7977. (unless orgstruct-initialized
  7978. (orgstruct-setup)
  7979. (setq orgstruct-initialized t))))
  7980. ;;;###autoload
  7981. (defun turn-on-orgstruct ()
  7982. "Unconditionally turn on `orgstruct-mode'."
  7983. (orgstruct-mode 1))
  7984. (defvar org-fb-vars nil)
  7985. (make-variable-buffer-local 'org-fb-vars)
  7986. (defun orgstruct++-mode (&optional arg)
  7987. "Toggle `orgstruct-mode', the enhanced version of it.
  7988. In addition to setting orgstruct-mode, this also exports all
  7989. indentation and autofilling variables from org-mode into the
  7990. buffer. It will also recognize item context in multiline items."
  7991. (interactive "P")
  7992. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7993. (if (< arg 1)
  7994. (progn (orgstruct-mode -1)
  7995. (mapc (lambda(v)
  7996. (org-set-local (car v)
  7997. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7998. org-fb-vars))
  7999. (orgstruct-mode 1)
  8000. (setq org-fb-vars nil)
  8001. (unless org-local-vars
  8002. (setq org-local-vars (org-get-local-variables)))
  8003. (let (var val)
  8004. (mapc
  8005. (lambda (x)
  8006. (when (string-match
  8007. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8008. (symbol-name (car x)))
  8009. (setq var (car x) val (nth 1 x))
  8010. (push (list var `(quote ,(eval var))) org-fb-vars)
  8011. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8012. org-local-vars)
  8013. (org-set-local 'orgstruct-is-++ t))))
  8014. (defvar orgstruct-is-++ nil
  8015. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8016. (make-variable-buffer-local 'orgstruct-is-++)
  8017. ;;;###autoload
  8018. (defun turn-on-orgstruct++ ()
  8019. "Unconditionally turn on `orgstruct++-mode'."
  8020. (orgstruct++-mode 1))
  8021. (defun orgstruct-error ()
  8022. "Error when there is no default binding for a structure key."
  8023. (interactive)
  8024. (funcall (if (fboundp 'user-error)
  8025. 'user-error
  8026. 'error)
  8027. "This key has no function outside structure elements"))
  8028. (defun orgstruct-setup ()
  8029. "Setup orgstruct keymap."
  8030. (dolist (cell '((org-demote . t)
  8031. (org-metaleft . t)
  8032. (org-metaright . t)
  8033. (org-promote . t)
  8034. (org-shiftmetaleft . t)
  8035. (org-shiftmetaright . t)
  8036. org-backward-element
  8037. org-backward-heading-same-level
  8038. org-ctrl-c-ret
  8039. org-ctrl-c-minus
  8040. org-ctrl-c-star
  8041. org-cycle
  8042. org-forward-heading-same-level
  8043. org-insert-heading
  8044. org-insert-heading-respect-content
  8045. org-kill-note-or-show-branches
  8046. org-mark-subtree
  8047. org-meta-return
  8048. org-metadown
  8049. org-metaup
  8050. org-narrow-to-subtree
  8051. org-promote-subtree
  8052. org-reveal
  8053. org-shiftdown
  8054. org-shiftleft
  8055. org-shiftmetadown
  8056. org-shiftmetaup
  8057. org-shiftright
  8058. org-shifttab
  8059. org-shifttab
  8060. org-shiftup
  8061. org-show-subtree
  8062. org-sort
  8063. org-up-element
  8064. outline-demote
  8065. outline-next-visible-heading
  8066. outline-previous-visible-heading
  8067. outline-promote
  8068. outline-up-heading
  8069. show-children))
  8070. (let ((f (or (car-safe cell) cell))
  8071. (disable-when-heading-prefix (cdr-safe cell)))
  8072. (when (fboundp f)
  8073. (let ((new-bindings))
  8074. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8075. (where-is-internal f outline-mode-map)))
  8076. (push binding new-bindings)
  8077. ;; TODO use local-function-key-map
  8078. (dolist (rep '(("<tab>" . "TAB")
  8079. ("<return>" . "RET")
  8080. ("<escape>" . "ESC")
  8081. ("<delete>" . "DEL")))
  8082. (setq binding (read-kbd-macro
  8083. (let ((case-fold-search))
  8084. (replace-regexp-in-string
  8085. (regexp-quote (cdr rep))
  8086. (car rep)
  8087. (key-description binding)))))
  8088. (pushnew binding new-bindings :test 'equal)))
  8089. (dolist (binding new-bindings)
  8090. (let ((key (lookup-key orgstruct-mode-map binding)))
  8091. (when (or (not key) (numberp key))
  8092. (condition-case nil
  8093. (org-defkey orgstruct-mode-map
  8094. binding
  8095. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8096. (error nil)))))))))
  8097. (run-hooks 'orgstruct-setup-hook))
  8098. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8099. "Create a function for binding in the structure minor mode.
  8100. FUN is the command to call inside a table. KEY is the key that
  8101. should be checked in for a command to execute outside of tables.
  8102. Non-nil `disable-when-heading-prefix' means to disable the command
  8103. if `orgstruct-heading-prefix-regexp' is not empty."
  8104. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8105. (let ((nname name)
  8106. (i 0))
  8107. (while (fboundp (intern nname))
  8108. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8109. (setq name (intern nname)))
  8110. (eval
  8111. (let ((bindings '((org-heading-regexp
  8112. (concat "^"
  8113. orgstruct-heading-prefix-regexp
  8114. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8115. (org-outline-regexp
  8116. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8117. (org-outline-regexp-bol
  8118. (concat "^" org-outline-regexp))
  8119. (outline-regexp org-outline-regexp)
  8120. (outline-heading-end-regexp "\n")
  8121. (outline-level 'org-outline-level)
  8122. (outline-heading-alist))))
  8123. `(defun ,name (arg)
  8124. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8125. "Outside of structure, run the binding of `"
  8126. (key-description key) "'."
  8127. (when disable-when-heading-prefix
  8128. (concat
  8129. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8130. "back to the default binding due to limitations of Org's implementation of\n"
  8131. "`" (symbol-name fun) "'.")))
  8132. (interactive "p")
  8133. (let* ((disable
  8134. ,(and disable-when-heading-prefix
  8135. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8136. (fallback
  8137. (or disable
  8138. (not
  8139. (let* ,bindings
  8140. (org-context-p 'headline 'item
  8141. ,(when (memq fun
  8142. '(org-insert-heading
  8143. org-insert-heading-respect-content
  8144. org-meta-return))
  8145. '(when orgstruct-is-++
  8146. 'item-body))))))))
  8147. (if fallback
  8148. (let* ((orgstruct-mode)
  8149. (binding
  8150. (loop with key = ,key
  8151. for rep in
  8152. '(nil
  8153. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8154. ("<\\([^>]*\\)return>" . "\\1RET")
  8155. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8156. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8157. do
  8158. (when rep
  8159. (setq key (read-kbd-macro
  8160. (let ((case-fold-search))
  8161. (replace-regexp-in-string
  8162. (car rep)
  8163. (cdr rep)
  8164. (key-description key))))))
  8165. thereis (key-binding key))))
  8166. (if (keymapp binding)
  8167. (set-temporary-overlay-map binding)
  8168. (let ((func (or binding
  8169. (unless disable
  8170. 'orgstruct-error))))
  8171. (when func
  8172. (call-interactively func)))))
  8173. (org-run-like-in-org-mode
  8174. (lambda ()
  8175. (interactive)
  8176. (let* ,bindings
  8177. (call-interactively ',fun)))))))))
  8178. name))
  8179. (defun org-contextualize-keys (alist contexts)
  8180. "Return valid elements in ALIST depending on CONTEXTS.
  8181. `org-agenda-custom-commands' or `org-capture-templates' are the
  8182. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8183. or `org-capture-templates-contexts' are the associated contexts
  8184. definitions."
  8185. (let ((contexts
  8186. ;; normalize contexts
  8187. (mapcar
  8188. (lambda(c) (cond ((listp (cadr c))
  8189. (list (car c) (car c) (cadr c)))
  8190. ((string= "" (cadr c))
  8191. (list (car c) (car c) (caddr c)))
  8192. (t c))) contexts))
  8193. (a alist) c r s)
  8194. ;; loop over all commands or templates
  8195. (while (setq c (pop a))
  8196. (let (vrules repl)
  8197. (cond
  8198. ((not (assoc (car c) contexts))
  8199. (push c r))
  8200. ((and (assoc (car c) contexts)
  8201. (setq vrules (org-contextualize-validate-key
  8202. (car c) contexts)))
  8203. (mapc (lambda (vr)
  8204. (when (not (equal (car vr) (cadr vr)))
  8205. (setq repl vr))) vrules)
  8206. (if (not repl) (push c r)
  8207. (push (cadr repl) s)
  8208. (push
  8209. (cons (car c)
  8210. (cdr (or (assoc (cadr repl) alist)
  8211. (error "Undefined key `%s' as contextual replacement for `%s'"
  8212. (cadr repl) (car c)))))
  8213. r))))))
  8214. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8215. (delq
  8216. nil
  8217. (delete-dups
  8218. (mapcar (lambda (x)
  8219. (let ((tpl (car x)))
  8220. (when (not (delq
  8221. nil
  8222. (mapcar (lambda(y)
  8223. (equal y tpl)) s))) x)))
  8224. (reverse r))))))
  8225. (defun org-contextualize-validate-key (key contexts)
  8226. "Check CONTEXTS for agenda or capture KEY."
  8227. (let (r rr res)
  8228. (while (setq r (pop contexts))
  8229. (mapc
  8230. (lambda (rr)
  8231. (when
  8232. (and (equal key (car r))
  8233. (if (functionp rr) (funcall rr)
  8234. (or (and (eq (car rr) 'in-file)
  8235. (buffer-file-name)
  8236. (string-match (cdr rr) (buffer-file-name)))
  8237. (and (eq (car rr) 'in-mode)
  8238. (string-match (cdr rr) (symbol-name major-mode)))
  8239. (and (eq (car rr) 'in-buffer)
  8240. (string-match (cdr rr) (buffer-name)))
  8241. (when (and (eq (car rr) 'not-in-file)
  8242. (buffer-file-name))
  8243. (not (string-match (cdr rr) (buffer-file-name))))
  8244. (when (eq (car rr) 'not-in-mode)
  8245. (not (string-match (cdr rr) (symbol-name major-mode))))
  8246. (when (eq (car rr) 'not-in-buffer)
  8247. (not (string-match (cdr rr) (buffer-name)))))))
  8248. (push r res)))
  8249. (car (last r))))
  8250. (delete-dups (delq nil res))))
  8251. (defun org-context-p (&rest contexts)
  8252. "Check if local context is any of CONTEXTS.
  8253. Possible values in the list of contexts are `table', `headline', and `item'."
  8254. (let ((pos (point)))
  8255. (goto-char (point-at-bol))
  8256. (prog1 (or (and (memq 'table contexts)
  8257. (looking-at "[ \t]*|"))
  8258. (and (memq 'headline contexts)
  8259. (looking-at org-outline-regexp))
  8260. (and (memq 'item contexts)
  8261. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8262. (and (memq 'item-body contexts)
  8263. (org-in-item-p)))
  8264. (goto-char pos))))
  8265. (defun org-get-local-variables ()
  8266. "Return a list of all local variables in an Org mode buffer."
  8267. (let (varlist)
  8268. (with-current-buffer (get-buffer-create "*Org tmp*")
  8269. (erase-buffer)
  8270. (org-mode)
  8271. (setq varlist (buffer-local-variables)))
  8272. (kill-buffer "*Org tmp*")
  8273. (delq nil
  8274. (mapcar
  8275. (lambda (x)
  8276. (setq x
  8277. (if (symbolp x)
  8278. (list x)
  8279. (list (car x) (cdr x))))
  8280. (if (and (not (get (car x) 'org-state))
  8281. (string-match
  8282. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8283. (symbol-name (car x))))
  8284. x nil))
  8285. varlist))))
  8286. (defun org-clone-local-variables (from-buffer &optional regexp)
  8287. "Clone local variables from FROM-BUFFER.
  8288. Optional argument REGEXP selects variables to clone."
  8289. (mapc
  8290. (lambda (pair)
  8291. (and (symbolp (car pair))
  8292. (or (null regexp)
  8293. (string-match regexp (symbol-name (car pair))))
  8294. (set (make-local-variable (car pair))
  8295. (cdr pair))))
  8296. (buffer-local-variables from-buffer)))
  8297. ;;;###autoload
  8298. (defun org-run-like-in-org-mode (cmd)
  8299. "Run a command, pretending that the current buffer is in Org-mode.
  8300. This will temporarily bind local variables that are typically bound in
  8301. Org-mode to the values they have in Org-mode, and then interactively
  8302. call CMD."
  8303. (org-load-modules-maybe)
  8304. (unless org-local-vars
  8305. (setq org-local-vars (org-get-local-variables)))
  8306. (let (binds)
  8307. (dolist (var org-local-vars)
  8308. (when (or (not (boundp (car var)))
  8309. (eq (symbol-value (car var))
  8310. (default-value (car var))))
  8311. (push (list (car var) `(quote ,(cadr var))) binds)))
  8312. (eval `(let ,binds
  8313. (call-interactively (quote ,cmd))))))
  8314. ;;;; Archiving
  8315. (defun org-get-category (&optional pos force-refresh)
  8316. "Get the category applying to position POS."
  8317. (save-match-data
  8318. (if force-refresh (org-refresh-category-properties))
  8319. (let ((pos (or pos (point))))
  8320. (or (get-text-property pos 'org-category)
  8321. (progn (org-refresh-category-properties)
  8322. (get-text-property pos 'org-category))))))
  8323. (defun org-refresh-category-properties ()
  8324. "Refresh category text properties in the buffer."
  8325. (let ((case-fold-search t)
  8326. (inhibit-read-only t)
  8327. (def-cat (cond
  8328. ((null org-category)
  8329. (if buffer-file-name
  8330. (file-name-sans-extension
  8331. (file-name-nondirectory buffer-file-name))
  8332. "???"))
  8333. ((symbolp org-category) (symbol-name org-category))
  8334. (t org-category)))
  8335. beg end cat pos optionp)
  8336. (org-with-silent-modifications
  8337. (save-excursion
  8338. (save-restriction
  8339. (widen)
  8340. (goto-char (point-min))
  8341. (put-text-property (point) (point-max) 'org-category def-cat)
  8342. (while (re-search-forward
  8343. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8344. (setq pos (match-end 0)
  8345. optionp (equal (char-after (match-beginning 0)) ?#)
  8346. cat (org-trim (match-string 2)))
  8347. (if optionp
  8348. (setq beg (point-at-bol) end (point-max))
  8349. (org-back-to-heading t)
  8350. (setq beg (point) end (org-end-of-subtree t t)))
  8351. (put-text-property beg end 'org-category cat)
  8352. (put-text-property beg end 'org-category-position beg)
  8353. (goto-char pos)))))))
  8354. (defun org-refresh-properties (dprop tprop)
  8355. "Refresh buffer text properties.
  8356. DPROP is the drawer property and TPROP is the corresponding text
  8357. property to set."
  8358. (let ((case-fold-search t)
  8359. (inhibit-read-only t) p)
  8360. (org-with-silent-modifications
  8361. (save-excursion
  8362. (save-restriction
  8363. (widen)
  8364. (goto-char (point-min))
  8365. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8366. (setq p (org-match-string-no-properties 1))
  8367. (save-excursion
  8368. (org-back-to-heading t)
  8369. (put-text-property
  8370. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8371. ;;;; Link Stuff
  8372. ;;; Link abbreviations
  8373. (defun org-link-expand-abbrev (link)
  8374. "Apply replacements as defined in `org-link-abbrev-alist'."
  8375. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8376. (let* ((key (match-string 1 link))
  8377. (as (or (assoc key org-link-abbrev-alist-local)
  8378. (assoc key org-link-abbrev-alist)))
  8379. (tag (and (match-end 2) (match-string 3 link)))
  8380. rpl)
  8381. (if (not as)
  8382. link
  8383. (setq rpl (cdr as))
  8384. (cond
  8385. ((symbolp rpl) (funcall rpl tag))
  8386. ((string-match "%(\\([^)]+\\))" rpl)
  8387. (replace-match
  8388. (save-match-data
  8389. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8390. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8391. ((string-match "%h" rpl)
  8392. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8393. (t (concat rpl tag)))))
  8394. link))
  8395. ;;; Storing and inserting links
  8396. (defvar org-insert-link-history nil
  8397. "Minibuffer history for links inserted with `org-insert-link'.")
  8398. (defvar org-stored-links nil
  8399. "Contains the links stored with `org-store-link'.")
  8400. (defvar org-store-link-plist nil
  8401. "Plist with info about the most recently link created with `org-store-link'.")
  8402. (defvar org-link-protocols nil
  8403. "Link protocols added to Org-mode using `org-add-link-type'.")
  8404. (defvar org-store-link-functions nil
  8405. "List of functions that are called to create and store a link.
  8406. Each function will be called in turn until one returns a non-nil
  8407. value. Each function should check if it is responsible for creating
  8408. this link (for example by looking at the major mode).
  8409. If not, it must exit and return nil.
  8410. If yes, it should return a non-nil value after a calling
  8411. `org-store-link-props' with a list of properties and values.
  8412. Special properties are:
  8413. :type The link prefix, like \"http\". This must be given.
  8414. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8415. This is obligatory as well.
  8416. :description Optional default description for the second pair
  8417. of brackets in an Org-mode link. The user can still change
  8418. this when inserting this link into an Org-mode buffer.
  8419. In addition to these, any additional properties can be specified
  8420. and then used in capture templates.")
  8421. (defun org-add-link-type (type &optional follow export)
  8422. "Add TYPE to the list of `org-link-types'.
  8423. Re-compute all regular expressions depending on `org-link-types'
  8424. FOLLOW and EXPORT are two functions.
  8425. FOLLOW should take the link path as the single argument and do whatever
  8426. is necessary to follow the link, for example find a file or display
  8427. a mail message.
  8428. EXPORT should format the link path for export to one of the export formats.
  8429. It should be a function accepting three arguments:
  8430. path the path of the link, the text after the prefix (like \"http:\")
  8431. desc the description of the link, if any, or a description added by
  8432. org-export-normalize-links if there is none
  8433. format the export format, a symbol like `html' or `latex' or `ascii'..
  8434. The function may use the FORMAT information to return different values
  8435. depending on the format. The return value will be put literally into
  8436. the exported file. If the return value is nil, this means Org should
  8437. do what it normally does with links which do not have EXPORT defined.
  8438. Org-mode has a built-in default for exporting links. If you are happy with
  8439. this default, there is no need to define an export function for the link
  8440. type. For a simple example of an export function, see `org-bbdb.el'."
  8441. (add-to-list 'org-link-types type t)
  8442. (org-make-link-regexps)
  8443. (if (assoc type org-link-protocols)
  8444. (setcdr (assoc type org-link-protocols) (list follow export))
  8445. (push (list type follow export) org-link-protocols)))
  8446. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8447. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8448. ;;;###autoload
  8449. (defun org-store-link (arg)
  8450. "\\<org-mode-map>Store an org-link to the current location.
  8451. This link is added to `org-stored-links' and can later be inserted
  8452. into an org-buffer with \\[org-insert-link].
  8453. For some link types, a prefix arg is interpreted.
  8454. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8455. For file links, arg negates `org-context-in-file-links'.
  8456. A double prefix arg force skipping storing functions that are not
  8457. part of Org's core.
  8458. A triple prefix arg force storing a link for each line in the
  8459. active region."
  8460. (interactive "P")
  8461. (org-load-modules-maybe)
  8462. (if (and (equal arg '(64)) (org-region-active-p))
  8463. (save-excursion
  8464. (let ((end (region-end)))
  8465. (goto-char (region-beginning))
  8466. (set-mark (point))
  8467. (while (< (point-at-eol) end)
  8468. (move-end-of-line 1) (activate-mark)
  8469. (let (current-prefix-arg)
  8470. (call-interactively 'org-store-link))
  8471. (move-beginning-of-line 2)
  8472. (set-mark (point)))))
  8473. (org-with-limited-levels
  8474. (setq org-store-link-plist nil)
  8475. (let (link cpltxt desc description search
  8476. txt custom-id agenda-link sfuns sfunsn)
  8477. (cond
  8478. ;; Store a link using an external link type
  8479. ((and (not (equal arg '(16)))
  8480. (setq sfuns
  8481. (delq
  8482. nil (mapcar (lambda (f)
  8483. (let (fs) (if (funcall f) (push f fs))))
  8484. org-store-link-functions))
  8485. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8486. (or (and (cdr sfuns)
  8487. (funcall (intern
  8488. (completing-read
  8489. "Which function for creating the link? "
  8490. sfunsn t (car sfunsn)))))
  8491. (funcall (caar sfuns)))
  8492. (setq link (plist-get org-store-link-plist :link)
  8493. desc (or (plist-get org-store-link-plist
  8494. :description) link))))
  8495. ;; Store a link from a source code buffer
  8496. ((org-src-edit-buffer-p)
  8497. (let (label gc)
  8498. (while (or (not label)
  8499. (save-excursion
  8500. (save-restriction
  8501. (widen)
  8502. (goto-char (point-min))
  8503. (re-search-forward
  8504. (regexp-quote (format org-coderef-label-format label))
  8505. nil t))))
  8506. (when label (message "Label exists already") (sit-for 2))
  8507. (setq label (read-string "Code line label: " label)))
  8508. (end-of-line 1)
  8509. (setq link (format org-coderef-label-format label))
  8510. (setq gc (- 79 (length link)))
  8511. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8512. (insert link)
  8513. (setq link (concat "(" label ")") desc nil)))
  8514. ;; We are in the agenda, link to referenced location
  8515. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8516. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8517. (get-text-property (point) 'org-marker))))
  8518. (when m
  8519. (org-with-point-at m
  8520. (setq agenda-link
  8521. (if (org-called-interactively-p 'any)
  8522. (call-interactively 'org-store-link)
  8523. (org-store-link nil)))))))
  8524. ((eq major-mode 'calendar-mode)
  8525. (let ((cd (calendar-cursor-to-date)))
  8526. (setq link
  8527. (format-time-string
  8528. (car org-time-stamp-formats)
  8529. (apply 'encode-time
  8530. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8531. nil nil nil))))
  8532. (org-store-link-props :type "calendar" :date cd)))
  8533. ((eq major-mode 'help-mode)
  8534. (setq link (concat "help:" (save-excursion
  8535. (goto-char (point-min))
  8536. (looking-at "^[^ ]+")
  8537. (match-string 0))))
  8538. (org-store-link-props :type "help"))
  8539. ((eq major-mode 'w3-mode)
  8540. (setq cpltxt (if (and (buffer-name)
  8541. (not (string-match "Untitled" (buffer-name))))
  8542. (buffer-name)
  8543. (url-view-url t))
  8544. link (url-view-url t))
  8545. (org-store-link-props :type "w3" :url (url-view-url t)))
  8546. ((eq major-mode 'image-mode)
  8547. (setq cpltxt (concat "file:"
  8548. (abbreviate-file-name buffer-file-name))
  8549. link cpltxt)
  8550. (org-store-link-props :type "image" :file buffer-file-name))
  8551. ;; In dired, store a link to the file of the current line
  8552. ((eq major-mode 'dired-mode)
  8553. (let ((file (dired-get-filename nil t)))
  8554. (setq file (if file
  8555. (abbreviate-file-name
  8556. (expand-file-name (dired-get-filename nil t)))
  8557. ;; otherwise, no file so use current directory.
  8558. default-directory))
  8559. (setq cpltxt (concat "file:" file)
  8560. link cpltxt)))
  8561. ((setq search (run-hook-with-args-until-success
  8562. 'org-create-file-search-functions))
  8563. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8564. "::" search))
  8565. (setq cpltxt (or description link)))
  8566. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8567. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8568. (cond
  8569. ;; Store a link using the target at point
  8570. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8571. (setq cpltxt
  8572. (concat "file:"
  8573. (abbreviate-file-name
  8574. (buffer-file-name (buffer-base-buffer)))
  8575. "::" (match-string 1))
  8576. link cpltxt))
  8577. ((and (featurep 'org-id)
  8578. (or (eq org-id-link-to-org-use-id t)
  8579. (and (org-called-interactively-p 'any)
  8580. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8581. (and (eq org-id-link-to-org-use-id
  8582. 'create-if-interactive-and-no-custom-id)
  8583. (not custom-id))))
  8584. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8585. ;; Store a link using the ID at point
  8586. (setq link (condition-case nil
  8587. (prog1 (org-id-store-link)
  8588. (setq desc (plist-get org-store-link-plist
  8589. :description)))
  8590. (error
  8591. ;; Probably before first headline, link only to file
  8592. (concat "file:"
  8593. (abbreviate-file-name
  8594. (buffer-file-name (buffer-base-buffer))))))))
  8595. (t
  8596. ;; Just link to current headline
  8597. (setq cpltxt (concat "file:"
  8598. (abbreviate-file-name
  8599. (buffer-file-name (buffer-base-buffer)))))
  8600. ;; Add a context search string
  8601. (when (org-xor org-context-in-file-links arg)
  8602. (let* ((ee (org-element-at-point))
  8603. (et (org-element-type ee))
  8604. (ev (plist-get (cadr ee) :value))
  8605. (ek (plist-get (cadr ee) :key))
  8606. (eok (and (stringp ek) (string-match "name" ek))))
  8607. (setq txt (cond
  8608. ((org-at-heading-p) nil)
  8609. ((and (eq et 'keyword) eok) ev)
  8610. ((org-region-active-p)
  8611. (buffer-substring (region-beginning) (region-end)))))
  8612. (when (or (null txt) (string-match "\\S-" txt))
  8613. (setq cpltxt
  8614. (concat cpltxt "::"
  8615. (condition-case nil
  8616. (org-make-org-heading-search-string txt)
  8617. (error "")))
  8618. desc (or (and (eq et 'keyword) eok ev)
  8619. (nth 4 (ignore-errors (org-heading-components)))
  8620. "NONE")))))
  8621. (if (string-match "::\\'" cpltxt)
  8622. (setq cpltxt (substring cpltxt 0 -2)))
  8623. (setq link cpltxt))))
  8624. ((buffer-file-name (buffer-base-buffer))
  8625. ;; Just link to this file here.
  8626. (setq cpltxt (concat "file:"
  8627. (abbreviate-file-name
  8628. (buffer-file-name (buffer-base-buffer)))))
  8629. ;; Add a context string.
  8630. (when (org-xor org-context-in-file-links arg)
  8631. (setq txt (if (org-region-active-p)
  8632. (buffer-substring (region-beginning) (region-end))
  8633. (buffer-substring (point-at-bol) (point-at-eol))))
  8634. ;; Only use search option if there is some text.
  8635. (when (string-match "\\S-" txt)
  8636. (setq cpltxt
  8637. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8638. desc "NONE")))
  8639. (setq link cpltxt))
  8640. ((org-called-interactively-p 'interactive)
  8641. (user-error "No method for storing a link from this buffer"))
  8642. (t (setq link nil)))
  8643. ;; We're done setting link and desc, clean up
  8644. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8645. (setq link (or link cpltxt)
  8646. desc (or desc cpltxt))
  8647. (cond ((equal desc "NONE") (setq desc nil))
  8648. ((string-match org-bracket-link-analytic-regexp desc)
  8649. (let ((d0 (match-string 3 desc))
  8650. (p0 (match-string 5 desc)))
  8651. (setq desc
  8652. (replace-regexp-in-string
  8653. org-bracket-link-regexp
  8654. (concat (or p0 d0)
  8655. (if (equal (length (match-string 0 desc))
  8656. (length desc)) "*" "")) desc)))))
  8657. ;; Return the link
  8658. (if (not (and (or (org-called-interactively-p 'any)
  8659. executing-kbd-macro) link))
  8660. (or agenda-link (and link (org-make-link-string link desc)))
  8661. (push (list link desc) org-stored-links)
  8662. (message "Stored: %s" (or desc link))
  8663. (when custom-id
  8664. (setq link (concat "file:" (abbreviate-file-name
  8665. (buffer-file-name)) "::#" custom-id))
  8666. (push (list link desc) org-stored-links)))))))
  8667. (defun org-store-link-props (&rest plist)
  8668. "Store link properties, extract names and addresses."
  8669. (let (x adr)
  8670. (when (setq x (plist-get plist :from))
  8671. (setq adr (mail-extract-address-components x))
  8672. (setq plist (plist-put plist :fromname (car adr)))
  8673. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8674. (when (setq x (plist-get plist :to))
  8675. (setq adr (mail-extract-address-components x))
  8676. (setq plist (plist-put plist :toname (car adr)))
  8677. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8678. (let ((from (plist-get plist :from))
  8679. (to (plist-get plist :to)))
  8680. (when (and from to org-from-is-user-regexp)
  8681. (setq plist
  8682. (plist-put plist :fromto
  8683. (if (string-match org-from-is-user-regexp from)
  8684. (concat "to %t")
  8685. (concat "from %f"))))))
  8686. (setq org-store-link-plist plist))
  8687. (defun org-add-link-props (&rest plist)
  8688. "Add these properties to the link property list."
  8689. (let (key value)
  8690. (while plist
  8691. (setq key (pop plist) value (pop plist))
  8692. (setq org-store-link-plist
  8693. (plist-put org-store-link-plist key value)))))
  8694. (defun org-email-link-description (&optional fmt)
  8695. "Return the description part of an email link.
  8696. This takes information from `org-store-link-plist' and formats it
  8697. according to FMT (default from `org-email-link-description-format')."
  8698. (setq fmt (or fmt org-email-link-description-format))
  8699. (let* ((p org-store-link-plist)
  8700. (to (plist-get p :toaddress))
  8701. (from (plist-get p :fromaddress))
  8702. (table
  8703. (list
  8704. (cons "%c" (plist-get p :fromto))
  8705. (cons "%F" (plist-get p :from))
  8706. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8707. (cons "%T" (plist-get p :to))
  8708. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8709. (cons "%s" (plist-get p :subject))
  8710. (cons "%d" (plist-get p :date))
  8711. (cons "%m" (plist-get p :message-id)))))
  8712. (when (string-match "%c" fmt)
  8713. ;; Check if the user wrote this message
  8714. (if (and org-from-is-user-regexp from to
  8715. (save-match-data (string-match org-from-is-user-regexp from)))
  8716. (setq fmt (replace-match "to %t" t t fmt))
  8717. (setq fmt (replace-match "from %f" t t fmt))))
  8718. (org-replace-escapes fmt table)))
  8719. (defun org-make-org-heading-search-string (&optional string)
  8720. "Make search string for the current headline or STRING."
  8721. (let ((s (or string
  8722. (and (derived-mode-p 'org-mode)
  8723. (save-excursion
  8724. (org-back-to-heading t)
  8725. (org-element-property :raw-value (org-element-at-point))))))
  8726. (lines org-context-in-file-links))
  8727. (or string (setq s (concat "*" s))) ; Add * for headlines
  8728. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8729. (when (and string (integerp lines) (> lines 0))
  8730. (let ((slines (org-split-string s "\n")))
  8731. (when (< lines (length slines))
  8732. (setq s (mapconcat
  8733. 'identity
  8734. (reverse (nthcdr (- (length slines) lines)
  8735. (reverse slines))) "\n")))))
  8736. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8737. (defun org-make-link-string (link &optional description)
  8738. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8739. (unless (string-match "\\S-" link)
  8740. (error "Empty link"))
  8741. (when (and description
  8742. (stringp description)
  8743. (not (string-match "\\S-" description)))
  8744. (setq description nil))
  8745. (when (stringp description)
  8746. ;; Remove brackets from the description, they are fatal.
  8747. (while (string-match "\\[" description)
  8748. (setq description (replace-match "{" t t description)))
  8749. (while (string-match "\\]" description)
  8750. (setq description (replace-match "}" t t description))))
  8751. (when (equal link description)
  8752. ;; No description needed, it is identical
  8753. (setq description nil))
  8754. (when (and (not description)
  8755. (not (string-match (org-image-file-name-regexp) link))
  8756. (not (equal link (org-link-escape link))))
  8757. (setq description (org-extract-attributes link)))
  8758. (setq link
  8759. (cond ((string-match (org-image-file-name-regexp) link) link)
  8760. ((string-match org-link-types-re link)
  8761. (concat (match-string 1 link)
  8762. (org-link-escape (substring link (match-end 1)))))
  8763. (t (org-link-escape link))))
  8764. (concat "[[" link "]"
  8765. (if description (concat "[" description "]") "")
  8766. "]"))
  8767. (defconst org-link-escape-chars
  8768. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8769. "List of characters that should be escaped in link.
  8770. This is the list that is used for internal purposes.")
  8771. (defconst org-link-escape-chars-browser
  8772. '(?\ ?\")
  8773. "List of escapes for characters that are problematic in links.
  8774. This is the list that is used before handing over to the browser.")
  8775. (defun org-link-escape (text &optional table merge)
  8776. "Return percent escaped representation of TEXT.
  8777. TEXT is a string with the text to escape.
  8778. Optional argument TABLE is a list with characters that should be
  8779. escaped. When nil, `org-link-escape-chars' is used.
  8780. If optional argument MERGE is set, merge TABLE into
  8781. `org-link-escape-chars'."
  8782. (cond
  8783. ((and table merge)
  8784. (mapc (lambda (defchr)
  8785. (unless (member defchr table)
  8786. (setq table (cons defchr table)))) org-link-escape-chars))
  8787. ((null table)
  8788. (setq table org-link-escape-chars)))
  8789. (mapconcat
  8790. (lambda (char)
  8791. (if (or (member char table)
  8792. (and (or (< char 32) (= char 37) (> char 126))
  8793. org-url-hexify-p))
  8794. (mapconcat (lambda (sequence-element)
  8795. (format "%%%.2X" sequence-element))
  8796. (or (encode-coding-char char 'utf-8)
  8797. (error "Unable to percent escape character: %s"
  8798. (char-to-string char))) "")
  8799. (char-to-string char))) text ""))
  8800. (defun org-link-unescape (str)
  8801. "Unhex hexified Unicode strings as returned from the JavaScript function
  8802. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8803. (unless (and (null str) (string= "" str))
  8804. (let ((pos 0) (case-fold-search t) unhexed)
  8805. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8806. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8807. (setq str (replace-match unhexed t t str))
  8808. (setq pos (+ pos (length unhexed))))))
  8809. str)
  8810. (defun org-link-unescape-compound (hex)
  8811. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8812. Note: this function also decodes single byte encodings like
  8813. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8814. (save-match-data
  8815. (let* ((bytes (cdr (split-string hex "%")))
  8816. (ret "")
  8817. (eat 0)
  8818. (sum 0))
  8819. (while bytes
  8820. (let* ((val (string-to-number (pop bytes) 16))
  8821. (shift-xor
  8822. (if (= 0 eat)
  8823. (cond
  8824. ((>= val 252) (cons 6 252))
  8825. ((>= val 248) (cons 5 248))
  8826. ((>= val 240) (cons 4 240))
  8827. ((>= val 224) (cons 3 224))
  8828. ((>= val 192) (cons 2 192))
  8829. (t (cons 0 0)))
  8830. (cons 6 128))))
  8831. (if (>= val 192) (setq eat (car shift-xor)))
  8832. (setq val (logxor val (cdr shift-xor)))
  8833. (setq sum (+ (lsh sum (car shift-xor)) val))
  8834. (if (> eat 0) (setq eat (- eat 1)))
  8835. (cond
  8836. ((= 0 eat) ;multi byte
  8837. (setq ret (concat ret (org-char-to-string sum)))
  8838. (setq sum 0))
  8839. ((not bytes) ; single byte(s)
  8840. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8841. )) ;; end (while bytes
  8842. ret )))
  8843. (defun org-link-unescape-single-byte-sequence (hex)
  8844. "Unhexify hex-encoded single byte character sequences."
  8845. (mapconcat (lambda (byte)
  8846. (char-to-string (string-to-number byte 16)))
  8847. (cdr (split-string hex "%")) ""))
  8848. (defun org-xor (a b)
  8849. "Exclusive or."
  8850. (if a (not b) b))
  8851. (defun org-fixup-message-id-for-http (s)
  8852. "Replace special characters in a message id, so it can be used in an http query."
  8853. (when (string-match "%" s)
  8854. (setq s (mapconcat (lambda (c)
  8855. (if (eq c ?%)
  8856. "%25"
  8857. (char-to-string c)))
  8858. s "")))
  8859. (while (string-match "<" s)
  8860. (setq s (replace-match "%3C" t t s)))
  8861. (while (string-match ">" s)
  8862. (setq s (replace-match "%3E" t t s)))
  8863. (while (string-match "@" s)
  8864. (setq s (replace-match "%40" t t s)))
  8865. s)
  8866. (defun org-link-prettify (link)
  8867. "Return a human-readable representation of LINK.
  8868. The car of LINK must be a raw link the cdr of LINK must be either
  8869. a link description or nil."
  8870. (let ((desc (or (cadr link) "<no description>")))
  8871. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8872. "<" (car link) ">")))
  8873. ;;;###autoload
  8874. (defun org-insert-link-global ()
  8875. "Insert a link like Org-mode does.
  8876. This command can be called in any mode to insert a link in Org-mode syntax."
  8877. (interactive)
  8878. (org-load-modules-maybe)
  8879. (org-run-like-in-org-mode 'org-insert-link))
  8880. (defun org-insert-all-links (&optional keep)
  8881. "Insert all links in `org-stored-links'."
  8882. (interactive "P")
  8883. (let ((links (copy-sequence org-stored-links)) l)
  8884. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8885. (insert "- ")
  8886. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8887. (insert "\n"))))
  8888. (defun org-link-fontify-links-to-this-file ()
  8889. "Fontify links to the current file in `org-stored-links'."
  8890. (let ((f (buffer-file-name)) a b)
  8891. (setq a (mapcar (lambda(l)
  8892. (let ((ll (car l)))
  8893. (when (and (string-match "^file:\\(.+\\)::" ll)
  8894. (equal f (expand-file-name (match-string 1 ll))))
  8895. ll)))
  8896. org-stored-links))
  8897. (when (featurep 'org-id)
  8898. (setq b (mapcar (lambda(l)
  8899. (let ((ll (car l)))
  8900. (when (and (string-match "^id:\\(.+\\)$" ll)
  8901. (equal f (expand-file-name
  8902. (or (org-id-find-id-file
  8903. (match-string 1 ll)) ""))))
  8904. ll)))
  8905. org-stored-links)))
  8906. (mapcar (lambda(l)
  8907. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8908. (delq nil (append a b)))))
  8909. (defvar org-link-links-in-this-file nil)
  8910. (defun org-insert-link (&optional complete-file link-location default-description)
  8911. "Insert a link. At the prompt, enter the link.
  8912. Completion can be used to insert any of the link protocol prefixes like
  8913. http or ftp in use.
  8914. The history can be used to select a link previously stored with
  8915. `org-store-link'. When the empty string is entered (i.e. if you just
  8916. press RET at the prompt), the link defaults to the most recently
  8917. stored link. As SPC triggers completion in the minibuffer, you need to
  8918. use M-SPC or C-q SPC to force the insertion of a space character.
  8919. You will also be prompted for a description, and if one is given, it will
  8920. be displayed in the buffer instead of the link.
  8921. If there is already a link at point, this command will allow you to edit link
  8922. and description parts.
  8923. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8924. be selected using completion. The path to the file will be relative to the
  8925. current directory if the file is in the current directory or a subdirectory.
  8926. Otherwise, the link will be the absolute path as completed in the minibuffer
  8927. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8928. option `org-link-file-path-type'.
  8929. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8930. the current directory or below.
  8931. With three \\[universal-argument] prefixes, negate the meaning of
  8932. `org-keep-stored-link-after-insertion'.
  8933. If `org-make-link-description-function' is non-nil, this function will be
  8934. called with the link target, and the result will be the default
  8935. link description.
  8936. If the LINK-LOCATION parameter is non-nil, this value will be
  8937. used as the link location instead of reading one interactively.
  8938. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8939. be used as the default description."
  8940. (interactive "P")
  8941. (let* ((wcf (current-window-configuration))
  8942. (origbuf (current-buffer))
  8943. (region (if (org-region-active-p)
  8944. (buffer-substring (region-beginning) (region-end))))
  8945. (remove (and region (list (region-beginning) (region-end))))
  8946. (desc region)
  8947. tmphist ; byte-compile incorrectly complains about this
  8948. (link link-location)
  8949. (abbrevs org-link-abbrev-alist-local)
  8950. entry file all-prefixes auto-desc)
  8951. (cond
  8952. (link-location) ; specified by arg, just use it.
  8953. ((org-in-regexp org-bracket-link-regexp 1)
  8954. ;; We do have a link at point, and we are going to edit it.
  8955. (setq remove (list (match-beginning 0) (match-end 0)))
  8956. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8957. (setq link (read-string "Link: "
  8958. (org-link-unescape
  8959. (org-match-string-no-properties 1)))))
  8960. ((or (org-in-regexp org-angle-link-re)
  8961. (org-in-regexp org-plain-link-re))
  8962. ;; Convert to bracket link
  8963. (setq remove (list (match-beginning 0) (match-end 0))
  8964. link (read-string "Link: "
  8965. (org-remove-angle-brackets (match-string 0)))))
  8966. ((member complete-file '((4) (16)))
  8967. ;; Completing read for file names.
  8968. (setq link (org-file-complete-link complete-file)))
  8969. (t
  8970. ;; Read link, with completion for stored links.
  8971. (org-link-fontify-links-to-this-file)
  8972. (org-switch-to-buffer-other-window "*Org Links*")
  8973. (with-current-buffer "*Org Links*"
  8974. (erase-buffer)
  8975. (insert "Insert a link.
  8976. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8977. (when org-stored-links
  8978. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8979. (insert (mapconcat 'org-link-prettify
  8980. (reverse org-stored-links) "\n")))
  8981. (goto-char (point-min)))
  8982. (let ((cw (selected-window)))
  8983. (select-window (get-buffer-window "*Org Links*" 'visible))
  8984. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8985. (unless (pos-visible-in-window-p (point-max))
  8986. (org-fit-window-to-buffer))
  8987. (and (window-live-p cw) (select-window cw)))
  8988. ;; Fake a link history, containing the stored links.
  8989. (setq tmphist (append (mapcar 'car org-stored-links)
  8990. org-insert-link-history))
  8991. (setq all-prefixes (append (mapcar 'car abbrevs)
  8992. (mapcar 'car org-link-abbrev-alist)
  8993. org-link-types))
  8994. (unwind-protect
  8995. (progn
  8996. (setq link
  8997. (org-completing-read
  8998. "Link: "
  8999. (append
  9000. (mapcar (lambda (x) (concat x ":"))
  9001. all-prefixes)
  9002. (mapcar 'car org-stored-links))
  9003. nil nil nil
  9004. 'tmphist
  9005. (caar org-stored-links)))
  9006. (if (not (string-match "\\S-" link))
  9007. (user-error "No link selected"))
  9008. (mapc (lambda(l)
  9009. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9010. org-stored-links)
  9011. (if (or (member link all-prefixes)
  9012. (and (equal ":" (substring link -1))
  9013. (member (substring link 0 -1) all-prefixes)
  9014. (setq link (substring link 0 -1))))
  9015. (setq link (with-current-buffer origbuf
  9016. (org-link-try-special-completion link)))))
  9017. (set-window-configuration wcf)
  9018. (kill-buffer "*Org Links*"))
  9019. (setq entry (assoc link org-stored-links))
  9020. (or entry (push link org-insert-link-history))
  9021. (setq desc (or desc (nth 1 entry)))))
  9022. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9023. (not org-keep-stored-link-after-insertion))
  9024. (setq org-stored-links (delq (assoc link org-stored-links)
  9025. org-stored-links)))
  9026. (if (and (string-match org-plain-link-re link)
  9027. (not (string-match org-ts-regexp link)))
  9028. ;; URL-like link, normalize the use of angular brackets.
  9029. (setq link (org-remove-angle-brackets link)))
  9030. ;; Check if we are linking to the current file with a search
  9031. ;; option If yes, simplify the link by using only the search
  9032. ;; option.
  9033. (when (and buffer-file-name
  9034. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9035. (let* ((path (match-string 1 link))
  9036. (case-fold-search nil)
  9037. (search (match-string 2 link)))
  9038. (save-match-data
  9039. (if (equal (file-truename buffer-file-name) (file-truename path))
  9040. ;; We are linking to this same file, with a search option
  9041. (setq link search)))))
  9042. ;; Check if we can/should use a relative path. If yes, simplify the link
  9043. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9044. (let* ((type (match-string 1 link))
  9045. (path (match-string 2 link))
  9046. (origpath path)
  9047. (case-fold-search nil))
  9048. (cond
  9049. ((or (eq org-link-file-path-type 'absolute)
  9050. (equal complete-file '(16)))
  9051. (setq path (abbreviate-file-name (expand-file-name path))))
  9052. ((eq org-link-file-path-type 'noabbrev)
  9053. (setq path (expand-file-name path)))
  9054. ((eq org-link-file-path-type 'relative)
  9055. (setq path (file-relative-name path)))
  9056. (t
  9057. (save-match-data
  9058. (if (string-match (concat "^" (regexp-quote
  9059. (expand-file-name
  9060. (file-name-as-directory
  9061. default-directory))))
  9062. (expand-file-name path))
  9063. ;; We are linking a file with relative path name.
  9064. (setq path (substring (expand-file-name path)
  9065. (match-end 0)))
  9066. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9067. (setq link (concat type path))
  9068. (if (equal desc origpath)
  9069. (setq desc path))))
  9070. (if org-make-link-description-function
  9071. (setq desc
  9072. (or (condition-case nil
  9073. (funcall org-make-link-description-function link desc)
  9074. (error (progn (message "Can't get link description from `%s'"
  9075. (symbol-name org-make-link-description-function))
  9076. (sit-for 2) nil)))
  9077. (read-string "Description: " default-description)))
  9078. (if default-description (setq desc default-description)
  9079. (setq desc (or (and auto-desc desc)
  9080. (read-string "Description: " desc)))))
  9081. (unless (string-match "\\S-" desc) (setq desc nil))
  9082. (if remove (apply 'delete-region remove))
  9083. (insert (org-make-link-string link desc))))
  9084. (defun org-link-try-special-completion (type)
  9085. "If there is completion support for link type TYPE, offer it."
  9086. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9087. (if (functionp fun)
  9088. (funcall fun)
  9089. (read-string "Link (no completion support): " (concat type ":")))))
  9090. (defun org-file-complete-link (&optional arg)
  9091. "Create a file link using completion."
  9092. (let (file link)
  9093. (setq file (org-iread-file-name "File: "))
  9094. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9095. (pwd1 (file-name-as-directory (abbreviate-file-name
  9096. (expand-file-name ".")))))
  9097. (cond
  9098. ((equal arg '(16))
  9099. (setq link (concat
  9100. "file:"
  9101. (abbreviate-file-name (expand-file-name file)))))
  9102. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9103. (setq link (concat "file:" (match-string 1 file))))
  9104. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9105. (expand-file-name file))
  9106. (setq link (concat
  9107. "file:" (match-string 1 (expand-file-name file)))))
  9108. (t (setq link (concat "file:" file)))))
  9109. link))
  9110. (defun org-iread-file-name (&rest args)
  9111. "Read-file-name using `ido-mode' speedup if available.
  9112. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9113. See `read-file-name' for a description of parameters."
  9114. (org-without-partial-completion
  9115. (if (and org-completion-use-ido
  9116. (fboundp 'ido-read-file-name)
  9117. (boundp 'ido-mode) ido-mode
  9118. (listp (second args)))
  9119. (let ((ido-enter-matching-directory nil))
  9120. (apply 'ido-read-file-name args))
  9121. (apply 'read-file-name args))))
  9122. (defun org-completing-read (&rest args)
  9123. "Completing-read with SPACE being a normal character."
  9124. (let ((enable-recursive-minibuffers t)
  9125. (minibuffer-local-completion-map
  9126. (copy-keymap minibuffer-local-completion-map)))
  9127. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9128. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9129. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9130. (apply 'org-icompleting-read args)))
  9131. (defun org-completing-read-no-i (&rest args)
  9132. (let (org-completion-use-ido org-completion-use-iswitchb)
  9133. (apply 'org-completing-read args)))
  9134. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9135. "Use iswitch as a completing-read replacement to choose from choices.
  9136. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9137. from."
  9138. (let* ((iswitchb-use-virtual-buffers nil)
  9139. (iswitchb-make-buflist-hook
  9140. (lambda ()
  9141. (setq iswitchb-temp-buflist choices))))
  9142. (iswitchb-read-buffer prompt)))
  9143. (defun org-icompleting-read (&rest args)
  9144. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9145. (org-without-partial-completion
  9146. (if (and org-completion-use-ido
  9147. (fboundp 'ido-completing-read)
  9148. (boundp 'ido-mode) ido-mode
  9149. (listp (second args)))
  9150. (let ((ido-enter-matching-directory nil))
  9151. (apply 'ido-completing-read (concat (car args))
  9152. (if (consp (car (nth 1 args)))
  9153. (mapcar 'car (nth 1 args))
  9154. (nth 1 args))
  9155. (cddr args)))
  9156. (if (and org-completion-use-iswitchb
  9157. (boundp 'iswitchb-mode) iswitchb-mode
  9158. (listp (second args)))
  9159. (apply 'org-iswitchb-completing-read (concat (car args))
  9160. (if (consp (car (nth 1 args)))
  9161. (mapcar 'car (nth 1 args))
  9162. (nth 1 args))
  9163. (cddr args))
  9164. (apply 'completing-read args)))))
  9165. (defun org-extract-attributes (s)
  9166. "Extract the attributes cookie from a string and set as text property."
  9167. (let (a attr (start 0) key value)
  9168. (save-match-data
  9169. (when (string-match "{{\\([^}]+\\)}}$" s)
  9170. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9171. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9172. (setq key (match-string 1 a) value (match-string 2 a)
  9173. start (match-end 0)
  9174. attr (plist-put attr (intern key) value))))
  9175. (org-add-props s nil 'org-attr attr))
  9176. s))
  9177. ;;; Opening/following a link
  9178. (defvar org-link-search-failed nil)
  9179. (defvar org-open-link-functions nil
  9180. "Hook for functions finding a plain text link.
  9181. These functions must take a single argument, the link content.
  9182. They will be called for links that look like [[link text][description]]
  9183. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9184. like a filename (e.g. \"./blue.png\").
  9185. These functions will be called *before* Org attempts to resolve the
  9186. link by doing text searches in the current buffer - so if you want a
  9187. link \"[[target]]\" to still find \"<<target>>\", your function should
  9188. handle this as a special case.
  9189. When the function does handle the link, it must return a non-nil value.
  9190. If it decides that it is not responsible for this link, it must return
  9191. nil to indicate that that Org-mode can continue with other options
  9192. like exact and fuzzy text search.")
  9193. (defun org-next-link (&optional search-backward)
  9194. "Move forward to the next link.
  9195. If the link is in hidden text, expose it."
  9196. (interactive "P")
  9197. (when (and org-link-search-failed (eq this-command last-command))
  9198. (goto-char (point-min))
  9199. (message "Link search wrapped back to beginning of buffer"))
  9200. (setq org-link-search-failed nil)
  9201. (let* ((pos (point))
  9202. (ct (org-context))
  9203. (a (assoc :link ct))
  9204. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9205. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9206. ((looking-at org-any-link-re)
  9207. ;; Don't stay stuck at link without an org-link face
  9208. (forward-char (if search-backward -1 1))))
  9209. (if (funcall srch-fun org-any-link-re nil t)
  9210. (progn
  9211. (goto-char (match-beginning 0))
  9212. (if (outline-invisible-p) (org-show-context)))
  9213. (goto-char pos)
  9214. (setq org-link-search-failed t)
  9215. (message "No further link found"))))
  9216. (defun org-previous-link ()
  9217. "Move backward to the previous link.
  9218. If the link is in hidden text, expose it."
  9219. (interactive)
  9220. (funcall 'org-next-link t))
  9221. (defun org-translate-link (s)
  9222. "Translate a link string if a translation function has been defined."
  9223. (if (and org-link-translation-function
  9224. (fboundp org-link-translation-function)
  9225. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9226. (progn
  9227. (setq s (funcall org-link-translation-function
  9228. (match-string 1 s) (match-string 2 s)))
  9229. (concat (car s) ":" (cdr s)))
  9230. s))
  9231. (defun org-translate-link-from-planner (type path)
  9232. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9233. This is still an experimental function, your mileage may vary."
  9234. (cond
  9235. ((member type '("http" "https" "news" "ftp"))
  9236. ;; standard Internet links are the same.
  9237. nil)
  9238. ((and (equal type "irc") (string-match "^//" path))
  9239. ;; Planner has two / at the beginning of an irc link, we have 1.
  9240. ;; We should have zero, actually....
  9241. (setq path (substring path 1)))
  9242. ((and (equal type "lisp") (string-match "^/" path))
  9243. ;; Planner has a slash, we do not.
  9244. (setq type "elisp" path (substring path 1)))
  9245. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9246. ;; A typical message link. Planner has the id after the final slash,
  9247. ;; we separate it with a hash mark
  9248. (setq path (concat (match-string 1 path) "#"
  9249. (org-remove-angle-brackets (match-string 2 path))))))
  9250. (cons type path))
  9251. (defun org-find-file-at-mouse (ev)
  9252. "Open file link or URL at mouse."
  9253. (interactive "e")
  9254. (mouse-set-point ev)
  9255. (org-open-at-point 'in-emacs))
  9256. (defun org-open-at-mouse (ev)
  9257. "Open file link or URL at mouse.
  9258. See the docstring of `org-open-file' for details."
  9259. (interactive "e")
  9260. (mouse-set-point ev)
  9261. (if (eq major-mode 'org-agenda-mode)
  9262. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9263. (org-open-at-point))
  9264. (defvar org-window-config-before-follow-link nil
  9265. "The window configuration before following a link.
  9266. This is saved in case the need arises to restore it.")
  9267. (defvar org-open-link-marker (make-marker)
  9268. "Marker pointing to the location where `org-open-at-point; was called.")
  9269. ;;;###autoload
  9270. (defun org-open-at-point-global ()
  9271. "Follow a link like Org-mode does.
  9272. This command can be called in any mode to follow a link that has
  9273. Org-mode syntax."
  9274. (interactive)
  9275. (org-run-like-in-org-mode 'org-open-at-point))
  9276. ;;;###autoload
  9277. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9278. "Open a link in the string S, as if it was in Org-mode."
  9279. (interactive "sLink: \nP")
  9280. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9281. (with-temp-buffer
  9282. (let ((org-inhibit-startup (not reference-buffer)))
  9283. (org-mode)
  9284. (insert s)
  9285. (goto-char (point-min))
  9286. (when reference-buffer
  9287. (setq org-link-abbrev-alist-local
  9288. (with-current-buffer reference-buffer
  9289. org-link-abbrev-alist-local)))
  9290. (org-open-at-point arg reference-buffer)))))
  9291. (defvar org-open-at-point-functions nil
  9292. "Hook that is run when following a link at point.
  9293. Functions in this hook must return t if they identify and follow
  9294. a link at point. If they don't find anything interesting at point,
  9295. they must return nil.")
  9296. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9297. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9298. (defun org-open-at-point (&optional arg reference-buffer)
  9299. "Open link at or after point.
  9300. If there is no link at point, this function will search forward up to
  9301. the end of the current line.
  9302. Normally, files will be opened by an appropriate application. If the
  9303. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9304. With a double prefix argument, try to open outside of Emacs, in the
  9305. application the system uses for this file type."
  9306. (interactive "P")
  9307. ;; if in a code block, then open the block's results
  9308. (unless (call-interactively #'org-babel-open-src-block-result)
  9309. (org-load-modules-maybe)
  9310. (move-marker org-open-link-marker (point))
  9311. (setq org-window-config-before-follow-link (current-window-configuration))
  9312. (org-remove-occur-highlights nil nil t)
  9313. (cond
  9314. ((and (org-at-heading-p)
  9315. (not (org-at-timestamp-p t))
  9316. (not (org-in-regexp
  9317. (concat org-plain-link-re "\\|"
  9318. org-bracket-link-regexp "\\|"
  9319. org-angle-link-re "\\|"
  9320. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9321. (not (get-text-property (point) 'org-linked-text)))
  9322. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9323. (lk0 (car lkall))
  9324. (lk (if (stringp lk0) (list lk0) lk0))
  9325. (lkend (cdr lkall)))
  9326. (mapcar (lambda(l)
  9327. (search-forward l nil lkend)
  9328. (goto-char (match-beginning 0))
  9329. (org-open-at-point))
  9330. lk))
  9331. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9332. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9333. ((and (org-at-timestamp-p t)
  9334. (not (org-in-regexp org-bracket-link-regexp)))
  9335. (org-follow-timestamp-link))
  9336. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9337. (not (org-in-regexp org-any-link-re)))
  9338. (org-footnote-action))
  9339. (t
  9340. (let (type path link line search (pos (point)))
  9341. (catch 'match
  9342. (save-excursion
  9343. (skip-chars-forward "^]\n\r")
  9344. (when (org-in-regexp org-bracket-link-regexp 1)
  9345. (setq link (org-extract-attributes
  9346. (org-link-unescape (org-match-string-no-properties 1))))
  9347. (while (string-match " *\n *" link)
  9348. (setq link (replace-match " " t t link)))
  9349. (setq link (org-link-expand-abbrev link))
  9350. (cond
  9351. ((or (file-name-absolute-p link)
  9352. (string-match "^\\.\\.?/" link))
  9353. (setq type "file" path link))
  9354. ((string-match org-link-re-with-space3 link)
  9355. (setq type (match-string 1 link) path (match-string 2 link)))
  9356. ((string-match "^help:+\\(.+\\)" link)
  9357. (setq type "help" path (match-string 1 link)))
  9358. (t (setq type "thisfile" path link)))
  9359. (throw 'match t)))
  9360. (when (get-text-property (point) 'org-linked-text)
  9361. (setq type "thisfile"
  9362. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9363. (1+ (point)) (point))
  9364. path (buffer-substring
  9365. (or (previous-single-property-change pos 'org-linked-text)
  9366. (point-min))
  9367. (or (next-single-property-change pos 'org-linked-text)
  9368. (point-max)))
  9369. ;; Ensure we will search for a <<<radio>>> link, not
  9370. ;; a simple reference like <<ref>>
  9371. path (concat "<" path))
  9372. (throw 'match t))
  9373. (save-excursion
  9374. (when (or (org-in-regexp org-angle-link-re)
  9375. (let ((match (org-in-regexp org-plain-link-re)))
  9376. ;; Check a plain link is not within a bracket link
  9377. (and match
  9378. (save-excursion
  9379. (progn
  9380. (goto-char (car match))
  9381. (not (org-in-regexp org-bracket-link-regexp))))))
  9382. (let ((line_ending (save-excursion (end-of-line) (point))))
  9383. ;; We are in a line before a plain or bracket link
  9384. (or (re-search-forward org-plain-link-re line_ending t)
  9385. (re-search-forward org-bracket-link-regexp line_ending t))))
  9386. (setq type (match-string 1)
  9387. path (org-link-unescape (match-string 2)))
  9388. (throw 'match t)))
  9389. (save-excursion
  9390. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9391. (setq type "tags"
  9392. path (match-string 1))
  9393. (while (string-match ":" path)
  9394. (setq path (replace-match "+" t t path)))
  9395. (throw 'match t)))
  9396. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9397. (setq type "tree-match"
  9398. path (match-string 1))
  9399. (throw 'match t)))
  9400. (unless path
  9401. (user-error "No link found"))
  9402. ;; switch back to reference buffer
  9403. ;; needed when if called in a temporary buffer through
  9404. ;; org-open-link-from-string
  9405. (with-current-buffer (or reference-buffer (current-buffer))
  9406. ;; Remove any trailing spaces in path
  9407. (if (string-match " +\\'" path)
  9408. (setq path (replace-match "" t t path)))
  9409. (if (and org-link-translation-function
  9410. (fboundp org-link-translation-function))
  9411. ;; Check if we need to translate the link
  9412. (let ((tmp (funcall org-link-translation-function type path)))
  9413. (setq type (car tmp) path (cdr tmp))))
  9414. (cond
  9415. ((assoc type org-link-protocols)
  9416. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9417. ((equal type "help")
  9418. (let ((f-or-v (intern path)))
  9419. (cond ((fboundp f-or-v)
  9420. (describe-function f-or-v))
  9421. ((boundp f-or-v)
  9422. (describe-variable f-or-v))
  9423. (t (error "Not a known function or variable")))))
  9424. ((equal type "mailto")
  9425. (let ((cmd (car org-link-mailto-program))
  9426. (args (cdr org-link-mailto-program)) args1
  9427. (address path) (subject "") a)
  9428. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9429. (setq address (match-string 1 path)
  9430. subject (org-link-escape (match-string 2 path))))
  9431. (while args
  9432. (cond
  9433. ((not (stringp (car args))) (push (pop args) args1))
  9434. (t (setq a (pop args))
  9435. (if (string-match "%a" a)
  9436. (setq a (replace-match address t t a)))
  9437. (if (string-match "%s" a)
  9438. (setq a (replace-match subject t t a)))
  9439. (push a args1))))
  9440. (apply cmd (nreverse args1))))
  9441. ((member type '("http" "https" "ftp" "news"))
  9442. (browse-url
  9443. (concat type ":"
  9444. (if (org-string-match-p
  9445. (concat "[[:nonascii:]"
  9446. org-link-escape-chars-browser "]")
  9447. path)
  9448. (org-link-escape path org-link-escape-chars-browser)
  9449. path))))
  9450. ((string= type "doi")
  9451. (browse-url
  9452. (concat org-doi-server-url
  9453. (if (org-string-match-p
  9454. (concat "[[:nonascii:]"
  9455. org-link-escape-chars-browser "]")
  9456. path)
  9457. (org-link-escape path org-link-escape-chars-browser)
  9458. path))))
  9459. ((member type '("message"))
  9460. (browse-url (concat type ":" path)))
  9461. ((string= type "tags")
  9462. (org-tags-view arg path))
  9463. ((string= type "tree-match")
  9464. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9465. ((string= type "file")
  9466. (if (string-match "::\\([0-9]+\\)\\'" path)
  9467. (setq line (string-to-number (match-string 1 path))
  9468. path (substring path 0 (match-beginning 0)))
  9469. (if (string-match "::\\(.+\\)\\'" path)
  9470. (setq search (match-string 1 path)
  9471. path (substring path 0 (match-beginning 0)))))
  9472. (if (string-match "[*?{]" (file-name-nondirectory path))
  9473. (dired path)
  9474. (org-open-file path arg line search)))
  9475. ((string= type "shell")
  9476. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9477. (cmd path))
  9478. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9479. (string-match org-confirm-shell-link-not-regexp cmd))
  9480. (not org-confirm-shell-link-function)
  9481. (funcall org-confirm-shell-link-function
  9482. (format "Execute \"%s\" in shell? "
  9483. (org-add-props cmd nil
  9484. 'face 'org-warning))))
  9485. (progn
  9486. (message "Executing %s" cmd)
  9487. (shell-command cmd buf)
  9488. (if (featurep 'midnight)
  9489. (setq clean-buffer-list-kill-buffer-names
  9490. (cons buf clean-buffer-list-kill-buffer-names))))
  9491. (error "Abort"))))
  9492. ((string= type "elisp")
  9493. (let ((cmd path))
  9494. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9495. (string-match org-confirm-elisp-link-not-regexp cmd))
  9496. (not org-confirm-elisp-link-function)
  9497. (funcall org-confirm-elisp-link-function
  9498. (format "Execute \"%s\" as elisp? "
  9499. (org-add-props cmd nil
  9500. 'face 'org-warning))))
  9501. (message "%s => %s" cmd
  9502. (if (equal (string-to-char cmd) ?\()
  9503. (eval (read cmd))
  9504. (call-interactively (read cmd))))
  9505. (error "Abort"))))
  9506. ((and (string= type "thisfile")
  9507. (or (run-hook-with-args-until-success
  9508. 'org-open-link-functions path)
  9509. (and link
  9510. (string-match "^id:" link)
  9511. (or (featurep 'org-id) (require 'org-id))
  9512. (progn
  9513. (funcall (nth 1 (assoc "id" org-link-protocols))
  9514. (substring path 3))
  9515. t)))))
  9516. ((string= type "thisfile")
  9517. (if arg
  9518. (switch-to-buffer-other-window
  9519. (org-get-buffer-for-internal-link (current-buffer)))
  9520. (org-mark-ring-push))
  9521. (let ((cmd `(org-link-search
  9522. ,path
  9523. ,(cond ((equal arg '(4)) ''occur)
  9524. ((equal arg '(16)) ''org-occur))
  9525. ,pos)))
  9526. (condition-case nil (let ((org-link-search-inhibit-query t))
  9527. (eval cmd))
  9528. (error (progn (widen) (eval cmd))))))
  9529. (t (browse-url-at-point)))))))
  9530. (move-marker org-open-link-marker nil)
  9531. (run-hook-with-args 'org-follow-link-hook)))
  9532. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9533. "Offer links in the current entry and return the selected link.
  9534. If there is only one link, return it.
  9535. If NTH is an integer, return the NTH link found.
  9536. If ZERO is a string, check also this string for a link, and if
  9537. there is one, return it."
  9538. (with-current-buffer buffer
  9539. (save-excursion
  9540. (save-restriction
  9541. (widen)
  9542. (goto-char marker)
  9543. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9544. "\\(" org-angle-link-re "\\)\\|"
  9545. "\\(" org-plain-link-re "\\)"))
  9546. (cnt ?0)
  9547. (in-emacs (if (integerp nth) nil nth))
  9548. have-zero end links link c)
  9549. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9550. (push (match-string 0 zero) links)
  9551. (setq cnt (1- cnt) have-zero t))
  9552. (save-excursion
  9553. (org-back-to-heading t)
  9554. (setq end (save-excursion (outline-next-heading) (point)))
  9555. (while (re-search-forward re end t)
  9556. (push (match-string 0) links))
  9557. (setq links (org-uniquify (reverse links))))
  9558. (cond
  9559. ((null links)
  9560. (message "No links"))
  9561. ((equal (length links) 1)
  9562. (setq link (car links)))
  9563. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9564. (setq link (nth (if have-zero nth (1- nth)) links)))
  9565. (t ; we have to select a link
  9566. (save-excursion
  9567. (save-window-excursion
  9568. (delete-other-windows)
  9569. (with-output-to-temp-buffer "*Select Link*"
  9570. (mapc (lambda (l)
  9571. (if (not (string-match org-bracket-link-regexp l))
  9572. (princ (format "[%c] %s\n" (incf cnt)
  9573. (org-remove-angle-brackets l)))
  9574. (if (match-end 3)
  9575. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9576. (match-string 3 l) (match-string 1 l)))
  9577. (princ (format "[%c] %s\n" (incf cnt)
  9578. (match-string 1 l))))))
  9579. links))
  9580. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9581. (message "Select link to open, RET to open all:")
  9582. (setq c (read-char-exclusive))
  9583. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9584. (when (equal c ?q) (error "Abort"))
  9585. (if (equal c ?\C-m)
  9586. (setq link links)
  9587. (setq nth (- c ?0))
  9588. (if have-zero (setq nth (1+ nth)))
  9589. (unless (and (integerp nth) (>= (length links) nth))
  9590. (user-error "Invalid link selection"))
  9591. (setq link (nth (1- nth) links)))))
  9592. (cons link end))))))
  9593. ;; Add special file links that specify the way of opening
  9594. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9595. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9596. (defun org-open-file-with-system (path)
  9597. "Open file at PATH using the system way of opening it."
  9598. (org-open-file path 'system))
  9599. (defun org-open-file-with-emacs (path)
  9600. "Open file at PATH in Emacs."
  9601. (org-open-file path 'emacs))
  9602. ;;; File search
  9603. (defvar org-create-file-search-functions nil
  9604. "List of functions to construct the right search string for a file link.
  9605. These functions are called in turn with point at the location to
  9606. which the link should point.
  9607. A function in the hook should first test if it would like to
  9608. handle this file type, for example by checking the `major-mode'
  9609. or the file extension. If it decides not to handle this file, it
  9610. should just return nil to give other functions a chance. If it
  9611. does handle the file, it must return the search string to be used
  9612. when following the link. The search string will be part of the
  9613. file link, given after a double colon, and `org-open-at-point'
  9614. will automatically search for it. If special measures must be
  9615. taken to make the search successful, another function should be
  9616. added to the companion hook `org-execute-file-search-functions',
  9617. which see.
  9618. A function in this hook may also use `setq' to set the variable
  9619. `description' to provide a suggestion for the descriptive text to
  9620. be used for this link when it gets inserted into an Org-mode
  9621. buffer with \\[org-insert-link].")
  9622. (defvar org-execute-file-search-functions nil
  9623. "List of functions to execute a file search triggered by a link.
  9624. Functions added to this hook must accept a single argument, the
  9625. search string that was part of the file link, the part after the
  9626. double colon. The function must first check if it would like to
  9627. handle this search, for example by checking the `major-mode' or
  9628. the file extension. If it decides not to handle this search, it
  9629. should just return nil to give other functions a chance. If it
  9630. does handle the search, it must return a non-nil value to keep
  9631. other functions from trying.
  9632. Each function can access the current prefix argument through the
  9633. variable `current-prefix-arg'. Note that a single prefix is used
  9634. to force opening a link in Emacs, so it may be good to only use a
  9635. numeric or double prefix to guide the search function.
  9636. In case this is needed, a function in this hook can also restore
  9637. the window configuration before `org-open-at-point' was called using:
  9638. (set-window-configuration org-window-config-before-follow-link)")
  9639. (defun org-link-search (s &optional type avoid-pos stealth)
  9640. "Search for a link search option.
  9641. If S is surrounded by forward slashes, it is interpreted as a
  9642. regular expression. In org-mode files, this will create an `org-occur'
  9643. sparse tree. In ordinary files, `occur' will be used to list matches.
  9644. If the current buffer is in `dired-mode', grep will be used to search
  9645. in all files. If AVOID-POS is given, ignore matches near that position.
  9646. When optional argument STEALTH is non-nil, do not modify
  9647. visibility around point, thus ignoring
  9648. `org-show-hierarchy-above', `org-show-following-heading' and
  9649. `org-show-siblings' variables."
  9650. (let ((case-fold-search t)
  9651. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9652. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9653. (append '(("") (" ") ("\t") ("\n"))
  9654. org-emphasis-alist)
  9655. "\\|") "\\)"))
  9656. (pos (point))
  9657. (pre nil) (post nil)
  9658. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9659. (cond
  9660. ;; First check if there are any special search functions
  9661. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9662. ;; Now try the builtin stuff
  9663. ((and (equal (string-to-char s0) ?#)
  9664. (> (length s0) 1)
  9665. (save-excursion
  9666. (goto-char (point-min))
  9667. (and
  9668. (re-search-forward
  9669. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9670. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9671. (setq type 'dedicated
  9672. pos (match-beginning 0))))
  9673. ;; There is an exact target for this
  9674. (goto-char pos)
  9675. (org-back-to-heading t)))
  9676. ((save-excursion
  9677. (goto-char (point-min))
  9678. (and
  9679. (re-search-forward
  9680. (concat "<<" (regexp-quote s0) ">>") nil t)
  9681. (setq type 'dedicated
  9682. pos (match-beginning 0))))
  9683. ;; There is an exact target for this
  9684. (goto-char pos))
  9685. ((save-excursion
  9686. (goto-char (point-min))
  9687. (and
  9688. (re-search-forward
  9689. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9690. (setq type 'dedicated pos (match-beginning 0))))
  9691. ;; Found an element with a matching #+name affiliated keyword.
  9692. (goto-char pos))
  9693. ((and (string-match "^(\\(.*\\))$" s0)
  9694. (save-excursion
  9695. (goto-char (point-min))
  9696. (and
  9697. (re-search-forward
  9698. (concat "[^[]" (regexp-quote
  9699. (format org-coderef-label-format
  9700. (match-string 1 s0))))
  9701. nil t)
  9702. (setq type 'dedicated
  9703. pos (1+ (match-beginning 0))))))
  9704. ;; There is a coderef target for this
  9705. (goto-char pos))
  9706. ((string-match "^/\\(.*\\)/$" s)
  9707. ;; A regular expression
  9708. (cond
  9709. ((derived-mode-p 'org-mode)
  9710. (org-occur (match-string 1 s)))
  9711. (t (org-do-occur (match-string 1 s)))))
  9712. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9713. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9714. (goto-char (point-min))
  9715. (cond
  9716. ((let (case-fold-search)
  9717. (re-search-forward (format org-complex-heading-regexp-format
  9718. (regexp-quote s))
  9719. nil t))
  9720. ;; OK, found a match
  9721. (setq type 'dedicated)
  9722. (goto-char (match-beginning 0)))
  9723. ((and (not org-link-search-inhibit-query)
  9724. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9725. (y-or-n-p "No match - create this as a new heading? "))
  9726. (goto-char (point-max))
  9727. (or (bolp) (newline))
  9728. (insert "* " s "\n")
  9729. (beginning-of-line 0))
  9730. (t
  9731. (goto-char pos)
  9732. (error "No match"))))
  9733. (t
  9734. ;; A normal search string
  9735. (when (equal (string-to-char s) ?*)
  9736. ;; Anchor on headlines, post may include tags.
  9737. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9738. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9739. s (substring s 1)))
  9740. (remove-text-properties
  9741. 0 (length s)
  9742. '(face nil mouse-face nil keymap nil fontified nil) s)
  9743. ;; Make a series of regular expressions to find a match
  9744. (setq words (org-split-string s "[ \n\r\t]+")
  9745. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9746. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9747. "\\)" markers)
  9748. re2a_ (concat "\\(" (mapconcat 'downcase words
  9749. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9750. re2a (concat "[ \t\r\n]" re2a_)
  9751. re4_ (concat "\\(" (mapconcat 'downcase words
  9752. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9753. re4 (concat "[^a-zA-Z_]" re4_)
  9754. re1 (concat pre re2 post)
  9755. re3 (concat pre (if pre re4_ re4) post)
  9756. re5 (concat pre ".*" re4)
  9757. re2 (concat pre re2)
  9758. re2a (concat pre (if pre re2a_ re2a))
  9759. re4 (concat pre (if pre re4_ re4))
  9760. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9761. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9762. re5 "\\)"))
  9763. (cond
  9764. ((eq type 'org-occur) (org-occur reall))
  9765. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9766. (t (goto-char (point-min))
  9767. (setq type 'fuzzy)
  9768. (if (or (and (org-search-not-self 1 re0 nil t)
  9769. (setq type 'dedicated))
  9770. (org-search-not-self 1 re1 nil t)
  9771. (org-search-not-self 1 re2 nil t)
  9772. (org-search-not-self 1 re2a nil t)
  9773. (org-search-not-self 1 re3 nil t)
  9774. (org-search-not-self 1 re4 nil t)
  9775. (org-search-not-self 1 re5 nil t))
  9776. (goto-char (match-beginning 1))
  9777. (goto-char pos)
  9778. (error "No match"))))))
  9779. (and (derived-mode-p 'org-mode)
  9780. (not stealth)
  9781. (org-show-context 'link-search))
  9782. type))
  9783. (defun org-search-not-self (group &rest args)
  9784. "Execute `re-search-forward', but only accept matches that do not
  9785. enclose the position of `org-open-link-marker'."
  9786. (let ((m org-open-link-marker))
  9787. (catch 'exit
  9788. (while (apply 're-search-forward args)
  9789. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9790. (goto-char (match-end group))
  9791. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9792. (> (match-beginning 0) (marker-position m))
  9793. (< (match-end 0) (marker-position m)))
  9794. (save-match-data
  9795. (or (not (org-in-regexp
  9796. org-bracket-link-analytic-regexp 1))
  9797. (not (match-end 4)) ; no description
  9798. (and (<= (match-beginning 4) (point))
  9799. (>= (match-end 4) (point))))))
  9800. (throw 'exit (point))))))))
  9801. (defun org-get-buffer-for-internal-link (buffer)
  9802. "Return a buffer to be used for displaying the link target of internal links."
  9803. (cond
  9804. ((not org-display-internal-link-with-indirect-buffer)
  9805. buffer)
  9806. ((string-match "(Clone)$" (buffer-name buffer))
  9807. (message "Buffer is already a clone, not making another one")
  9808. ;; we also do not modify visibility in this case
  9809. buffer)
  9810. (t ; make a new indirect buffer for displaying the link
  9811. (let* ((bn (buffer-name buffer))
  9812. (ibn (concat bn "(Clone)"))
  9813. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9814. (with-current-buffer ib (org-overview))
  9815. ib))))
  9816. (defun org-do-occur (regexp &optional cleanup)
  9817. "Call the Emacs command `occur'.
  9818. If CLEANUP is non-nil, remove the printout of the regular expression
  9819. in the *Occur* buffer. This is useful if the regex is long and not useful
  9820. to read."
  9821. (occur regexp)
  9822. (when cleanup
  9823. (let ((cwin (selected-window)) win beg end)
  9824. (when (setq win (get-buffer-window "*Occur*"))
  9825. (select-window win))
  9826. (goto-char (point-min))
  9827. (when (re-search-forward "match[a-z]+" nil t)
  9828. (setq beg (match-end 0))
  9829. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9830. (setq end (1- (match-beginning 0)))))
  9831. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9832. (goto-char (point-min))
  9833. (select-window cwin))))
  9834. ;;; The mark ring for links jumps
  9835. (defvar org-mark-ring nil
  9836. "Mark ring for positions before jumps in Org-mode.")
  9837. (defvar org-mark-ring-last-goto nil
  9838. "Last position in the mark ring used to go back.")
  9839. ;; Fill and close the ring
  9840. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9841. (loop for i from 1 to org-mark-ring-length do
  9842. (push (make-marker) org-mark-ring))
  9843. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9844. org-mark-ring)
  9845. (defun org-mark-ring-push (&optional pos buffer)
  9846. "Put the current position or POS into the mark ring and rotate it."
  9847. (interactive)
  9848. (setq pos (or pos (point)))
  9849. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9850. (move-marker (car org-mark-ring)
  9851. (or pos (point))
  9852. (or buffer (current-buffer)))
  9853. (message "%s"
  9854. (substitute-command-keys
  9855. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9856. (defun org-mark-ring-goto (&optional n)
  9857. "Jump to the previous position in the mark ring.
  9858. With prefix arg N, jump back that many stored positions. When
  9859. called several times in succession, walk through the entire ring.
  9860. Org-mode commands jumping to a different position in the current file,
  9861. or to another Org-mode file, automatically push the old position
  9862. onto the ring."
  9863. (interactive "p")
  9864. (let (p m)
  9865. (if (eq last-command this-command)
  9866. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9867. (setq p org-mark-ring))
  9868. (setq org-mark-ring-last-goto p)
  9869. (setq m (car p))
  9870. (org-pop-to-buffer-same-window (marker-buffer m))
  9871. (goto-char m)
  9872. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9873. (defun org-remove-angle-brackets (s)
  9874. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9875. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9876. s)
  9877. (defun org-add-angle-brackets (s)
  9878. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9879. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9880. s)
  9881. (defun org-remove-double-quotes (s)
  9882. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9883. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9884. s)
  9885. ;;; Following specific links
  9886. (defun org-follow-timestamp-link ()
  9887. "Open an agenda view for the time-stamp date/range at point."
  9888. (cond
  9889. ((org-at-date-range-p t)
  9890. (let ((org-agenda-start-on-weekday)
  9891. (t1 (match-string 1))
  9892. (t2 (match-string 2)) tt1 tt2)
  9893. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9894. tt2 (time-to-days (org-time-string-to-time t2)))
  9895. (let ((org-agenda-buffer-tmp-name
  9896. (format "*Org Agenda(a:%s)"
  9897. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9898. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9899. ((org-at-timestamp-p t)
  9900. (let ((org-agenda-buffer-tmp-name
  9901. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9902. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9903. (substring (match-string 1) 0 10)))
  9904. 1)))
  9905. (t (error "This should not happen"))))
  9906. ;;; Following file links
  9907. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9908. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9909. (declare-function mailcap-mime-info
  9910. "mailcap" (string &optional request no-decode))
  9911. (defvar org-wait nil)
  9912. (defun org-open-file (path &optional in-emacs line search)
  9913. "Open the file at PATH.
  9914. First, this expands any special file name abbreviations. Then the
  9915. configuration variable `org-file-apps' is checked if it contains an
  9916. entry for this file type, and if yes, the corresponding command is launched.
  9917. If no application is found, Emacs simply visits the file.
  9918. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9919. With a double \\[universal-argument] \\[universal-argument] \
  9920. prefix arg, Org tries to avoid opening in Emacs
  9921. and to use an external application to visit the file.
  9922. Optional LINE specifies a line to go to, optional SEARCH a string
  9923. to search for. If LINE or SEARCH is given, the file will be
  9924. opened in Emacs, unless an entry from org-file-apps that makes
  9925. use of groups in a regexp matches.
  9926. If you want to change the way frames are used when following a
  9927. link, please customize `org-link-frame-setup'.
  9928. If the file does not exist, an error is thrown."
  9929. (let* ((file (if (equal path "")
  9930. buffer-file-name
  9931. (substitute-in-file-name (expand-file-name path))))
  9932. (file-apps (append org-file-apps (org-default-apps)))
  9933. (apps (org-remove-if
  9934. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9935. (apps-dlink (org-remove-if-not
  9936. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9937. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9938. (dirp (if remp nil (file-directory-p file)))
  9939. (file (if (and dirp org-open-directory-means-index-dot-org)
  9940. (concat (file-name-as-directory file) "index.org")
  9941. file))
  9942. (a-m-a-p (assq 'auto-mode apps))
  9943. (dfile (downcase file))
  9944. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9945. (link (cond ((and (eq line nil)
  9946. (eq search nil))
  9947. file)
  9948. (line
  9949. (concat file "::" (number-to-string line)))
  9950. (search
  9951. (concat file "::" search))))
  9952. (dlink (downcase link))
  9953. (old-buffer (current-buffer))
  9954. (old-pos (point))
  9955. (old-mode major-mode)
  9956. ext cmd link-match-data)
  9957. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9958. (setq ext (match-string 1 dfile))
  9959. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9960. (setq ext (match-string 1 dfile))))
  9961. (cond
  9962. ((member in-emacs '((16) system))
  9963. (setq cmd (cdr (assoc 'system apps))))
  9964. (in-emacs (setq cmd 'emacs))
  9965. (t
  9966. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9967. (and dirp (cdr (assoc 'directory apps)))
  9968. ; first, try matching against apps-dlink
  9969. ; if we get a match here, store the match data for later
  9970. (let ((match (assoc-default dlink apps-dlink
  9971. 'string-match)))
  9972. (if match
  9973. (progn (setq link-match-data (match-data))
  9974. match)
  9975. (progn (setq in-emacs (or in-emacs line search))
  9976. nil))) ; if we have no match in apps-dlink,
  9977. ; always open the file in emacs if line or search
  9978. ; is given (for backwards compatibility)
  9979. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9980. 'string-match)
  9981. (cdr (assoc ext apps))
  9982. (cdr (assoc t apps))))))
  9983. (when (eq cmd 'system)
  9984. (setq cmd (cdr (assoc 'system apps))))
  9985. (when (eq cmd 'default)
  9986. (setq cmd (cdr (assoc t apps))))
  9987. (when (eq cmd 'mailcap)
  9988. (require 'mailcap)
  9989. (mailcap-parse-mailcaps)
  9990. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9991. (command (mailcap-mime-info mime-type)))
  9992. (if (stringp command)
  9993. (setq cmd command)
  9994. (setq cmd 'emacs))))
  9995. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9996. (not (file-exists-p file))
  9997. (not org-open-non-existing-files))
  9998. (user-error "No such file: %s" file))
  9999. (cond
  10000. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10001. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10002. (while (string-match "['\"]%s['\"]" cmd)
  10003. (setq cmd (replace-match "%s" t t cmd)))
  10004. (while (string-match "%s" cmd)
  10005. (setq cmd (replace-match
  10006. (save-match-data
  10007. (shell-quote-argument
  10008. (convert-standard-filename file)))
  10009. t t cmd)))
  10010. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10011. (save-match-data
  10012. (let ((match-index 1)
  10013. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10014. (set-match-data link-match-data)
  10015. (while (<= match-index number-of-groups)
  10016. (let ((regex (concat "%" (number-to-string match-index)))
  10017. (replace-with (match-string match-index dlink)))
  10018. (while (string-match regex cmd)
  10019. (setq cmd (replace-match replace-with t t cmd))))
  10020. (setq match-index (+ match-index 1)))))
  10021. (save-window-excursion
  10022. (message "Running %s...done" cmd)
  10023. (start-process-shell-command cmd nil cmd)
  10024. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10025. ((or (stringp cmd)
  10026. (eq cmd 'emacs))
  10027. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10028. (widen)
  10029. (if line (org-goto-line line)
  10030. (if search (org-link-search search))))
  10031. ((consp cmd)
  10032. (let ((file (convert-standard-filename file)))
  10033. (save-match-data
  10034. (set-match-data link-match-data)
  10035. (eval cmd))))
  10036. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10037. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10038. (or (not (equal old-buffer (current-buffer)))
  10039. (not (equal old-pos (point))))
  10040. (org-mark-ring-push old-pos old-buffer))))
  10041. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10042. "This function returns non-nil if `entry' uses a regular
  10043. expression which should be matched against the whole link by
  10044. org-open-file.
  10045. It assumes that is the case when the entry uses a regular
  10046. expression which has at least one grouping construct and the
  10047. action is either a lisp form or a command string containing
  10048. '%1', i.e. using at least one subexpression match as a
  10049. parameter."
  10050. (let ((selector (car entry))
  10051. (action (cdr entry)))
  10052. (if (stringp selector)
  10053. (and (> (regexp-opt-depth selector) 0)
  10054. (or (and (stringp action)
  10055. (string-match "%[0-9]" action))
  10056. (consp action)))
  10057. nil)))
  10058. (defun org-default-apps ()
  10059. "Return the default applications for this operating system."
  10060. (cond
  10061. ((eq system-type 'darwin)
  10062. org-file-apps-defaults-macosx)
  10063. ((eq system-type 'windows-nt)
  10064. org-file-apps-defaults-windowsnt)
  10065. (t org-file-apps-defaults-gnu)))
  10066. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10067. "Convert extensions to regular expressions in the cars of LIST.
  10068. Also, weed out any non-string entries, because the return value is used
  10069. only for regexp matching.
  10070. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10071. point to the symbol `emacs', indicating that the file should
  10072. be opened in Emacs."
  10073. (append
  10074. (delq nil
  10075. (mapcar (lambda (x)
  10076. (if (not (stringp (car x)))
  10077. nil
  10078. (if (string-match "\\W" (car x))
  10079. x
  10080. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10081. list))
  10082. (if add-auto-mode
  10083. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10084. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10085. (defun org-file-remote-p (file)
  10086. "Test whether FILE specifies a location on a remote system.
  10087. Return non-nil if the location is indeed remote.
  10088. For example, the filename \"/user@host:/foo\" specifies a location
  10089. on the system \"/user@host:\"."
  10090. (cond ((fboundp 'file-remote-p)
  10091. (file-remote-p file))
  10092. ((fboundp 'tramp-handle-file-remote-p)
  10093. (tramp-handle-file-remote-p file))
  10094. ((and (boundp 'ange-ftp-name-format)
  10095. (string-match (car ange-ftp-name-format) file))
  10096. t)))
  10097. ;;;; Refiling
  10098. (defun org-get-org-file ()
  10099. "Read a filename, with default directory `org-directory'."
  10100. (let ((default (or org-default-notes-file remember-data-file)))
  10101. (read-file-name (format "File name [%s]: " default)
  10102. (file-name-as-directory org-directory)
  10103. default)))
  10104. (defun org-notes-order-reversed-p ()
  10105. "Check if the current file should receive notes in reversed order."
  10106. (cond
  10107. ((not org-reverse-note-order) nil)
  10108. ((eq t org-reverse-note-order) t)
  10109. ((not (listp org-reverse-note-order)) nil)
  10110. (t (catch 'exit
  10111. (let ((all org-reverse-note-order)
  10112. entry)
  10113. (while (setq entry (pop all))
  10114. (if (string-match (car entry) buffer-file-name)
  10115. (throw 'exit (cdr entry))))
  10116. nil)))))
  10117. (defvar org-refile-target-table nil
  10118. "The list of refile targets, created by `org-refile'.")
  10119. (defvar org-agenda-new-buffers nil
  10120. "Buffers created to visit agenda files.")
  10121. (defvar org-refile-cache nil
  10122. "Cache for refile targets.")
  10123. (defvar org-refile-markers nil
  10124. "All the markers used for caching refile locations.")
  10125. (defun org-refile-marker (pos)
  10126. "Get a new refile marker, but only if caching is in use."
  10127. (if (not org-refile-use-cache)
  10128. pos
  10129. (let ((m (make-marker)))
  10130. (move-marker m pos)
  10131. (push m org-refile-markers)
  10132. m)))
  10133. (defun org-refile-cache-clear ()
  10134. "Clear the refile cache and disable all the markers."
  10135. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10136. (setq org-refile-markers nil)
  10137. (setq org-refile-cache nil)
  10138. (message "Refile cache has been cleared"))
  10139. (defun org-refile-cache-check-set (set)
  10140. "Check if all the markers in the cache still have live buffers."
  10141. (let (marker)
  10142. (catch 'exit
  10143. (while (and set (setq marker (nth 3 (pop set))))
  10144. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10145. (when (and marker (null (marker-buffer marker)))
  10146. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10147. (sit-for 3)
  10148. (throw 'exit nil)))
  10149. t)))
  10150. (defun org-refile-cache-put (set &rest identifiers)
  10151. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10152. (let* ((key (sha1 (prin1-to-string identifiers)))
  10153. (entry (assoc key org-refile-cache)))
  10154. (if entry
  10155. (setcdr entry set)
  10156. (push (cons key set) org-refile-cache))))
  10157. (defun org-refile-cache-get (&rest identifiers)
  10158. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10159. (cond
  10160. ((not org-refile-cache) nil)
  10161. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10162. (t
  10163. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10164. org-refile-cache))))
  10165. (and set (org-refile-cache-check-set set) set)))))
  10166. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10167. "Produce a table with refile targets."
  10168. (let ((case-fold-search nil)
  10169. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10170. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10171. targets tgs txt re files f desc descre fast-path-p level pos0)
  10172. (message "Getting targets...")
  10173. (with-current-buffer (or default-buffer (current-buffer))
  10174. (while (setq entry (pop entries))
  10175. (setq files (car entry) desc (cdr entry))
  10176. (setq fast-path-p nil)
  10177. (cond
  10178. ((null files) (setq files (list (current-buffer))))
  10179. ((eq files 'org-agenda-files)
  10180. (setq files (org-agenda-files 'unrestricted)))
  10181. ((and (symbolp files) (fboundp files))
  10182. (setq files (funcall files)))
  10183. ((and (symbolp files) (boundp files))
  10184. (setq files (symbol-value files))))
  10185. (if (stringp files) (setq files (list files)))
  10186. (cond
  10187. ((eq (car desc) :tag)
  10188. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10189. ((eq (car desc) :todo)
  10190. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10191. ((eq (car desc) :regexp)
  10192. (setq descre (cdr desc)))
  10193. ((eq (car desc) :level)
  10194. (setq descre (concat "^\\*\\{" (number-to-string
  10195. (if org-odd-levels-only
  10196. (1- (* 2 (cdr desc)))
  10197. (cdr desc)))
  10198. "\\}[ \t]")))
  10199. ((eq (car desc) :maxlevel)
  10200. (setq fast-path-p t)
  10201. (setq descre (concat "^\\*\\{1," (number-to-string
  10202. (if org-odd-levels-only
  10203. (1- (* 2 (cdr desc)))
  10204. (cdr desc)))
  10205. "\\}[ \t]")))
  10206. (t (error "Bad refiling target description %s" desc)))
  10207. (while (setq f (pop files))
  10208. (with-current-buffer
  10209. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10210. (or
  10211. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10212. (progn
  10213. (if (bufferp f) (setq f (buffer-file-name
  10214. (buffer-base-buffer f))))
  10215. (setq f (and f (expand-file-name f)))
  10216. (if (eq org-refile-use-outline-path 'file)
  10217. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10218. (save-excursion
  10219. (save-restriction
  10220. (widen)
  10221. (goto-char (point-min))
  10222. (while (re-search-forward descre nil t)
  10223. (goto-char (setq pos0 (point-at-bol)))
  10224. (catch 'next
  10225. (when org-refile-target-verify-function
  10226. (save-match-data
  10227. (or (funcall org-refile-target-verify-function)
  10228. (throw 'next t))))
  10229. (when (and (looking-at org-complex-heading-regexp)
  10230. (not (member (match-string 4) excluded-entries))
  10231. (match-string 4))
  10232. (setq level (org-reduced-level
  10233. (- (match-end 1) (match-beginning 1)))
  10234. txt (org-link-display-format (match-string 4))
  10235. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10236. re (format org-complex-heading-regexp-format
  10237. (regexp-quote (match-string 4))))
  10238. (when org-refile-use-outline-path
  10239. (setq txt (mapconcat
  10240. 'org-protect-slash
  10241. (append
  10242. (if (eq org-refile-use-outline-path
  10243. 'file)
  10244. (list (file-name-nondirectory
  10245. (buffer-file-name
  10246. (buffer-base-buffer))))
  10247. (if (eq org-refile-use-outline-path
  10248. 'full-file-path)
  10249. (list (buffer-file-name
  10250. (buffer-base-buffer)))))
  10251. (org-get-outline-path fast-path-p
  10252. level txt)
  10253. (list txt))
  10254. "/")))
  10255. (push (list txt f re (org-refile-marker (point)))
  10256. tgs)))
  10257. (when (= (point) pos0)
  10258. ;; verification function has not moved point
  10259. (goto-char (point-at-eol))))))))
  10260. (when org-refile-use-cache
  10261. (org-refile-cache-put tgs (buffer-file-name) descre))
  10262. (setq targets (append tgs targets))))))
  10263. (message "Getting targets...done")
  10264. (nreverse targets)))
  10265. (defun org-protect-slash (s)
  10266. (while (string-match "/" s)
  10267. (setq s (replace-match "\\" t t s)))
  10268. s)
  10269. (defvar org-olpa (make-vector 20 nil))
  10270. (defun org-get-outline-path (&optional fastp level heading)
  10271. "Return the outline path to the current entry, as a list.
  10272. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10273. routine which makes outline path derivations for an entire file,
  10274. avoiding backtracing. Refile target collection makes use of that."
  10275. (if fastp
  10276. (progn
  10277. (if (> level 19)
  10278. (error "Outline path failure, more than 19 levels"))
  10279. (loop for i from level upto 19 do
  10280. (aset org-olpa i nil))
  10281. (prog1
  10282. (delq nil (append org-olpa nil))
  10283. (aset org-olpa level heading)))
  10284. (let (rtn case-fold-search)
  10285. (save-excursion
  10286. (save-restriction
  10287. (widen)
  10288. (while (org-up-heading-safe)
  10289. (when (looking-at org-complex-heading-regexp)
  10290. (push (org-trim
  10291. (replace-regexp-in-string
  10292. ;; Remove statistical/checkboxes cookies
  10293. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10294. (org-match-string-no-properties 4)))
  10295. rtn)))
  10296. rtn)))))
  10297. (defun org-format-outline-path (path &optional width prefix separator)
  10298. "Format the outline path PATH for display.
  10299. WIDTH is the maximum number of characters that is available.
  10300. PREFIX is a prefix to be included in the returned string,
  10301. such as the file name.
  10302. SEPARATOR is inserted between the different parts of the path,
  10303. the default is \"/\"."
  10304. (setq width (or width 79))
  10305. (if prefix (setq width (- width (length prefix))))
  10306. (if (not path)
  10307. (or prefix "")
  10308. (let* ((nsteps (length path))
  10309. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10310. (maxwidth (if (<= total-width width)
  10311. 10000 ;; everything fits
  10312. ;; we need to shorten the level headings
  10313. (/ (- width nsteps) nsteps)))
  10314. (org-odd-levels-only nil)
  10315. (n 0)
  10316. (total (1+ (length prefix))))
  10317. (setq maxwidth (max maxwidth 10))
  10318. (concat prefix
  10319. (if prefix (or separator "/"))
  10320. (mapconcat
  10321. (lambda (h)
  10322. (setq n (1+ n))
  10323. (if (and (= n nsteps) (< maxwidth 10000))
  10324. (setq maxwidth (- total-width total)))
  10325. (if (< (length h) maxwidth)
  10326. (progn (setq total (+ total (length h) 1)) h)
  10327. (setq h (substring h 0 (- maxwidth 2))
  10328. total (+ total maxwidth 1))
  10329. (if (string-match "[ \t]+\\'" h)
  10330. (setq h (substring h 0 (match-beginning 0))))
  10331. (setq h (concat h "..")))
  10332. (org-add-props h nil 'face
  10333. (nth (% (1- n) org-n-level-faces)
  10334. org-level-faces))
  10335. h)
  10336. path (or separator "/"))))))
  10337. (defun org-display-outline-path (&optional file current separator just-return-string)
  10338. "Display the current outline path in the echo area.
  10339. If FILE is non-nil, prepend the output with the file name.
  10340. If CURRENT is non-nil, append the current heading to the output.
  10341. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10342. the different parts of the path and defaults to \"/\".
  10343. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10344. (interactive "P")
  10345. (let* (case-fold-search
  10346. (bfn (buffer-file-name (buffer-base-buffer)))
  10347. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10348. res)
  10349. (if current (setq path (append path
  10350. (save-excursion
  10351. (org-back-to-heading t)
  10352. (if (looking-at org-complex-heading-regexp)
  10353. (list (match-string 4)))))))
  10354. (setq res
  10355. (org-format-outline-path
  10356. path
  10357. (1- (frame-width))
  10358. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10359. separator))
  10360. (if just-return-string
  10361. (org-no-properties res)
  10362. (org-unlogged-message "%s" res))))
  10363. (defvar org-refile-history nil
  10364. "History for refiling operations.")
  10365. (defvar org-after-refile-insert-hook nil
  10366. "Hook run after `org-refile' has inserted its stuff at the new location.
  10367. Note that this is still *before* the stuff will be removed from
  10368. the *old* location.")
  10369. (defvar org-capture-last-stored-marker)
  10370. (defvar org-refile-keep nil
  10371. "Non-nil means `org-refile' will copy instead of refile.")
  10372. (defun org-copy ()
  10373. "Like `org-refile', but copy."
  10374. (interactive)
  10375. (let ((org-refile-keep t))
  10376. (funcall 'org-refile nil nil nil "Copy")))
  10377. (defun org-refile (&optional goto default-buffer rfloc msg)
  10378. "Move the entry or entries at point to another heading.
  10379. The list of target headings is compiled using the information in
  10380. `org-refile-targets', which see.
  10381. At the target location, the entry is filed as a subitem of the target
  10382. heading. Depending on `org-reverse-note-order', the new subitem will
  10383. either be the first or the last subitem.
  10384. If there is an active region, all entries in that region will be moved.
  10385. However, the region must fulfill the requirement that the first heading
  10386. is the first one sets the top-level of the moved text - at most siblings
  10387. below it are allowed.
  10388. With prefix arg GOTO, the command will only visit the target location
  10389. and not actually move anything.
  10390. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10391. go to the location where the last refiling operation has put the subtree.
  10392. With a numeric prefix argument of `2', refile to the running clock.
  10393. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10394. being set to `t' and copy to the target location, don't move it.
  10395. Beware that keeping refiled entries may result in duplicated ID
  10396. properties.
  10397. RFLOC can be a refile location obtained in a different way.
  10398. MSG is a string to replace \"Refile\" in the default prompt with
  10399. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10400. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10401. If you are using target caching (see `org-refile-use-cache'),
  10402. you have to clear the target cache in order to find new targets.
  10403. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10404. prefix argument (`C-u C-u C-u C-c C-w')."
  10405. (interactive "P")
  10406. (if (member goto '(0 (64)))
  10407. (org-refile-cache-clear)
  10408. (let* ((actionmsg (or msg "Refile"))
  10409. (cbuf (current-buffer))
  10410. (regionp (org-region-active-p))
  10411. (region-start (and regionp (region-beginning)))
  10412. (region-end (and regionp (region-end)))
  10413. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10414. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10415. pos it nbuf file re level reversed)
  10416. (setq last-command nil)
  10417. (when regionp
  10418. (goto-char region-start)
  10419. (or (bolp) (goto-char (point-at-bol)))
  10420. (setq region-start (point))
  10421. (unless (or (org-kill-is-subtree-p
  10422. (buffer-substring region-start region-end))
  10423. (prog1 org-refile-active-region-within-subtree
  10424. (let ((s (point-at-eol)))
  10425. (org-toggle-heading)
  10426. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10427. (user-error "The region is not a (sequence of) subtree(s)")))
  10428. (if (equal goto '(16))
  10429. (org-refile-goto-last-stored)
  10430. (when (or
  10431. (and (equal goto 2)
  10432. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10433. (prog1
  10434. (setq it (list (or org-clock-heading "running clock")
  10435. (buffer-file-name
  10436. (marker-buffer org-clock-hd-marker))
  10437. ""
  10438. (marker-position org-clock-hd-marker)))
  10439. (setq goto nil)))
  10440. (setq it (or rfloc
  10441. (let (heading-text)
  10442. (save-excursion
  10443. (unless goto
  10444. (org-back-to-heading t)
  10445. (setq heading-text
  10446. (nth 4 (org-heading-components))))
  10447. (org-refile-get-location
  10448. (cond (goto "Goto")
  10449. (regionp (concat actionmsg " region to"))
  10450. (t (concat actionmsg " subtree \""
  10451. heading-text "\" to")))
  10452. default-buffer
  10453. (and (not (equal '(4) goto))
  10454. org-refile-allow-creating-parent-nodes)
  10455. goto))))))
  10456. (setq file (nth 1 it)
  10457. re (nth 2 it)
  10458. pos (nth 3 it))
  10459. (if (and (not goto)
  10460. pos
  10461. (equal (buffer-file-name) file)
  10462. (if regionp
  10463. (and (>= pos region-start)
  10464. (<= pos region-end))
  10465. (and (>= pos (point))
  10466. (< pos (save-excursion
  10467. (org-end-of-subtree t t))))))
  10468. (error "Cannot refile to position inside the tree or region"))
  10469. (setq nbuf (or (find-buffer-visiting file)
  10470. (find-file-noselect file)))
  10471. (if (and goto (not (equal goto 3)))
  10472. (progn
  10473. (org-pop-to-buffer-same-window nbuf)
  10474. (goto-char pos)
  10475. (org-show-context 'org-goto))
  10476. (if regionp
  10477. (progn
  10478. (org-kill-new (buffer-substring region-start region-end))
  10479. (org-save-markers-in-region region-start region-end))
  10480. (org-copy-subtree 1 nil t))
  10481. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10482. (find-file-noselect file)))
  10483. (setq reversed (org-notes-order-reversed-p))
  10484. (save-excursion
  10485. (save-restriction
  10486. (widen)
  10487. (if pos
  10488. (progn
  10489. (goto-char pos)
  10490. (looking-at org-outline-regexp)
  10491. (setq level (org-get-valid-level (funcall outline-level) 1))
  10492. (goto-char
  10493. (if reversed
  10494. (or (outline-next-heading) (point-max))
  10495. (or (save-excursion (org-get-next-sibling))
  10496. (org-end-of-subtree t t)
  10497. (point-max)))))
  10498. (setq level 1)
  10499. (if (not reversed)
  10500. (goto-char (point-max))
  10501. (goto-char (point-min))
  10502. (or (outline-next-heading) (goto-char (point-max)))))
  10503. (if (not (bolp)) (newline))
  10504. (org-paste-subtree level)
  10505. (when org-log-refile
  10506. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10507. (unless (eq org-log-refile 'note)
  10508. (save-excursion (org-add-log-note))))
  10509. (and org-auto-align-tags
  10510. (let ((org-loop-over-headlines-in-active-region nil))
  10511. (org-set-tags nil t)))
  10512. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10513. :last-refile)))
  10514. (when bookmark-name
  10515. (with-demoted-errors
  10516. (bookmark-set bookmark-name))))
  10517. ;; If we are refiling for capture, make sure that the
  10518. ;; last-capture pointers point here
  10519. (when (org-bound-and-true-p org-refile-for-capture)
  10520. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10521. :last-capture-marker)))
  10522. (when bookmark-name
  10523. (with-demoted-errors
  10524. (bookmark-set bookmark-name))))
  10525. (move-marker org-capture-last-stored-marker (point)))
  10526. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10527. (run-hooks 'org-after-refile-insert-hook))))
  10528. (unless org-refile-keep
  10529. (if regionp
  10530. (delete-region (point) (+ (point) (- region-end region-start)))
  10531. (delete-region
  10532. (and (org-back-to-heading t) (point))
  10533. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10534. (when (featurep 'org-inlinetask)
  10535. (org-inlinetask-remove-END-maybe))
  10536. (setq org-markers-to-move nil)
  10537. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10538. (defun org-refile-goto-last-stored ()
  10539. "Go to the location where the last refile was stored."
  10540. (interactive)
  10541. (bookmark-jump "org-refile-last-stored")
  10542. (message "This is the location of the last refile"))
  10543. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10544. no-exclude)
  10545. "Prompt the user for a refile location, using PROMPT.
  10546. PROMPT should not be suffixed with a colon and a space, because
  10547. this function appends the default value from
  10548. `org-refile-history' automatically, if that is not empty.
  10549. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10550. this is used for the GOTO interface."
  10551. (let ((org-refile-targets org-refile-targets)
  10552. (org-refile-use-outline-path org-refile-use-outline-path)
  10553. excluded-entries)
  10554. (when (and (derived-mode-p 'org-mode)
  10555. (not org-refile-use-cache)
  10556. (not no-exclude))
  10557. (org-map-tree
  10558. (lambda()
  10559. (setq excluded-entries
  10560. (append excluded-entries (list (org-get-heading t t)))))))
  10561. (setq org-refile-target-table
  10562. (org-refile-get-targets default-buffer excluded-entries)))
  10563. (unless org-refile-target-table
  10564. (user-error "No refile targets"))
  10565. (let* ((cbuf (current-buffer))
  10566. (partial-completion-mode nil)
  10567. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10568. (cfunc (if (and org-refile-use-outline-path
  10569. org-outline-path-complete-in-steps)
  10570. 'org-olpath-completing-read
  10571. 'org-icompleting-read))
  10572. (extra (if org-refile-use-outline-path "/" ""))
  10573. (cbnex (concat (buffer-name) extra))
  10574. (filename (and cfn (expand-file-name cfn)))
  10575. (tbl (mapcar
  10576. (lambda (x)
  10577. (if (and (not (member org-refile-use-outline-path
  10578. '(file full-file-path)))
  10579. (not (equal filename (nth 1 x))))
  10580. (cons (concat (car x) extra " ("
  10581. (file-name-nondirectory (nth 1 x)) ")")
  10582. (cdr x))
  10583. (cons (concat (car x) extra) (cdr x))))
  10584. org-refile-target-table))
  10585. (completion-ignore-case t)
  10586. cdef
  10587. (prompt (concat prompt
  10588. (or (and (car org-refile-history)
  10589. (concat " (default " (car org-refile-history) ")"))
  10590. (and (assoc cbnex tbl) (setq cdef cbnex)
  10591. (concat " (default " cbnex ")"))) ": "))
  10592. pa answ parent-target child parent old-hist)
  10593. (setq old-hist org-refile-history)
  10594. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10595. nil 'org-refile-history (or cdef (car org-refile-history))))
  10596. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10597. (org-refile-check-position pa)
  10598. (if pa
  10599. (progn
  10600. (when (or (not org-refile-history)
  10601. (not (eq old-hist org-refile-history))
  10602. (not (equal (car pa) (car org-refile-history))))
  10603. (setq org-refile-history
  10604. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10605. org-refile-history
  10606. (cdr org-refile-history))))
  10607. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10608. (pop org-refile-history)))
  10609. pa)
  10610. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10611. (progn
  10612. (setq parent (match-string 1 answ)
  10613. child (match-string 2 answ))
  10614. (setq parent-target (or (assoc parent tbl)
  10615. (assoc (concat parent "/") tbl)))
  10616. (when (and parent-target
  10617. (or (eq new-nodes t)
  10618. (and (eq new-nodes 'confirm)
  10619. (y-or-n-p (format "Create new node \"%s\"? "
  10620. child)))))
  10621. (org-refile-new-child parent-target child)))
  10622. (user-error "Invalid target location")))))
  10623. (declare-function org-string-nw-p "org-macs" (s))
  10624. (defun org-refile-check-position (refile-pointer)
  10625. "Check if the refile pointer matches the headline to which it points."
  10626. (let* ((file (nth 1 refile-pointer))
  10627. (re (nth 2 refile-pointer))
  10628. (pos (nth 3 refile-pointer))
  10629. buffer)
  10630. (if (and (not (markerp pos)) (not file))
  10631. (user-error "Please save the buffer to a file before refiling")
  10632. (when (org-string-nw-p re)
  10633. (setq buffer (if (markerp pos)
  10634. (marker-buffer pos)
  10635. (or (find-buffer-visiting file)
  10636. (find-file-noselect file))))
  10637. (with-current-buffer buffer
  10638. (save-excursion
  10639. (save-restriction
  10640. (widen)
  10641. (goto-char pos)
  10642. (beginning-of-line 1)
  10643. (unless (org-looking-at-p re)
  10644. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10645. (defun org-refile-new-child (parent-target child)
  10646. "Use refile target PARENT-TARGET to add new CHILD below it."
  10647. (unless parent-target
  10648. (error "Cannot find parent for new node"))
  10649. (let ((file (nth 1 parent-target))
  10650. (pos (nth 3 parent-target))
  10651. level)
  10652. (with-current-buffer (or (find-buffer-visiting file)
  10653. (find-file-noselect file))
  10654. (save-excursion
  10655. (save-restriction
  10656. (widen)
  10657. (if pos
  10658. (goto-char pos)
  10659. (goto-char (point-max))
  10660. (if (not (bolp)) (newline)))
  10661. (when (looking-at org-outline-regexp)
  10662. (setq level (funcall outline-level))
  10663. (org-end-of-subtree t t))
  10664. (org-back-over-empty-lines)
  10665. (insert "\n" (make-string
  10666. (if pos (org-get-valid-level level 1) 1) ?*)
  10667. " " child "\n")
  10668. (beginning-of-line 0)
  10669. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10670. (defun org-olpath-completing-read (prompt collection &rest args)
  10671. "Read an outline path like a file name."
  10672. (let ((thetable collection)
  10673. (org-completion-use-ido nil) ; does not work with ido.
  10674. (org-completion-use-iswitchb nil)) ; or iswitchb
  10675. (apply
  10676. 'org-icompleting-read prompt
  10677. (lambda (string predicate &optional flag)
  10678. (let (rtn r f (l (length string)))
  10679. (cond
  10680. ((eq flag nil)
  10681. ;; try completion
  10682. (try-completion string thetable))
  10683. ((eq flag t)
  10684. ;; all-completions
  10685. (setq rtn (all-completions string thetable predicate))
  10686. (mapcar
  10687. (lambda (x)
  10688. (setq r (substring x l))
  10689. (if (string-match " ([^)]*)$" x)
  10690. (setq f (match-string 0 x))
  10691. (setq f ""))
  10692. (if (string-match "/" r)
  10693. (concat string (substring r 0 (match-end 0)) f)
  10694. x))
  10695. rtn))
  10696. ((eq flag 'lambda)
  10697. ;; exact match?
  10698. (assoc string thetable)))))
  10699. args)))
  10700. ;;;; Dynamic blocks
  10701. (defun org-find-dblock (name)
  10702. "Find the first dynamic block with name NAME in the buffer.
  10703. If not found, stay at current position and return nil."
  10704. (let ((case-fold-search t) pos)
  10705. (save-excursion
  10706. (goto-char (point-min))
  10707. (setq pos (and (re-search-forward
  10708. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10709. (match-beginning 0))))
  10710. (if pos (goto-char pos))
  10711. pos))
  10712. (defconst org-dblock-start-re
  10713. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10714. "Matches the start line of a dynamic block, with parameters.")
  10715. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10716. "Matches the end of a dynamic block.")
  10717. (defun org-create-dblock (plist)
  10718. "Create a dynamic block section, with parameters taken from PLIST.
  10719. PLIST must contain a :name entry which is used as name of the block."
  10720. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10721. (end-of-line 1)
  10722. (newline))
  10723. (let ((col (current-column))
  10724. (name (plist-get plist :name)))
  10725. (insert "#+BEGIN: " name)
  10726. (while plist
  10727. (if (eq (car plist) :name)
  10728. (setq plist (cddr plist))
  10729. (insert " " (prin1-to-string (pop plist)))))
  10730. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10731. (beginning-of-line -2)))
  10732. (defun org-prepare-dblock ()
  10733. "Prepare dynamic block for refresh.
  10734. This empties the block, puts the cursor at the insert position and returns
  10735. the property list including an extra property :name with the block name."
  10736. (unless (looking-at org-dblock-start-re)
  10737. (user-error "Not at a dynamic block"))
  10738. (let* ((begdel (1+ (match-end 0)))
  10739. (name (org-no-properties (match-string 1)))
  10740. (params (append (list :name name)
  10741. (read (concat "(" (match-string 3) ")")))))
  10742. (save-excursion
  10743. (beginning-of-line 1)
  10744. (skip-chars-forward " \t")
  10745. (setq params (plist-put params :indentation-column (current-column))))
  10746. (unless (re-search-forward org-dblock-end-re nil t)
  10747. (error "Dynamic block not terminated"))
  10748. (setq params
  10749. (append params
  10750. (list :content (buffer-substring
  10751. begdel (match-beginning 0)))))
  10752. (delete-region begdel (match-beginning 0))
  10753. (goto-char begdel)
  10754. (open-line 1)
  10755. params))
  10756. (defun org-map-dblocks (&optional command)
  10757. "Apply COMMAND to all dynamic blocks in the current buffer.
  10758. If COMMAND is not given, use `org-update-dblock'."
  10759. (let ((cmd (or command 'org-update-dblock)))
  10760. (save-excursion
  10761. (goto-char (point-min))
  10762. (while (re-search-forward org-dblock-start-re nil t)
  10763. (goto-char (match-beginning 0))
  10764. (save-excursion
  10765. (condition-case nil
  10766. (funcall cmd)
  10767. (error (message "Error during update of dynamic block"))))
  10768. (unless (re-search-forward org-dblock-end-re nil t)
  10769. (error "Dynamic block not terminated"))))))
  10770. (defun org-dblock-update (&optional arg)
  10771. "User command for updating dynamic blocks.
  10772. Update the dynamic block at point. With prefix ARG, update all dynamic
  10773. blocks in the buffer."
  10774. (interactive "P")
  10775. (if arg
  10776. (org-update-all-dblocks)
  10777. (or (looking-at org-dblock-start-re)
  10778. (org-beginning-of-dblock))
  10779. (org-update-dblock)))
  10780. (defun org-update-dblock ()
  10781. "Update the dynamic block at point.
  10782. This means to empty the block, parse for parameters and then call
  10783. the correct writing function."
  10784. (interactive)
  10785. (save-window-excursion
  10786. (let* ((pos (point))
  10787. (line (org-current-line))
  10788. (params (org-prepare-dblock))
  10789. (name (plist-get params :name))
  10790. (indent (plist-get params :indentation-column))
  10791. (cmd (intern (concat "org-dblock-write:" name))))
  10792. (message "Updating dynamic block `%s' at line %d..." name line)
  10793. (funcall cmd params)
  10794. (message "Updating dynamic block `%s' at line %d...done" name line)
  10795. (goto-char pos)
  10796. (when (and indent (> indent 0))
  10797. (setq indent (make-string indent ?\ ))
  10798. (save-excursion
  10799. (org-beginning-of-dblock)
  10800. (forward-line 1)
  10801. (while (not (looking-at org-dblock-end-re))
  10802. (insert indent)
  10803. (beginning-of-line 2))
  10804. (when (looking-at org-dblock-end-re)
  10805. (and (looking-at "[ \t]+")
  10806. (replace-match ""))
  10807. (insert indent)))))))
  10808. (defun org-beginning-of-dblock ()
  10809. "Find the beginning of the dynamic block at point.
  10810. Error if there is no such block at point."
  10811. (let ((pos (point))
  10812. beg)
  10813. (end-of-line 1)
  10814. (if (and (re-search-backward org-dblock-start-re nil t)
  10815. (setq beg (match-beginning 0))
  10816. (re-search-forward org-dblock-end-re nil t)
  10817. (> (match-end 0) pos))
  10818. (goto-char beg)
  10819. (goto-char pos)
  10820. (error "Not in a dynamic block"))))
  10821. (defun org-update-all-dblocks ()
  10822. "Update all dynamic blocks in the buffer.
  10823. This function can be used in a hook."
  10824. (interactive)
  10825. (when (derived-mode-p 'org-mode)
  10826. (org-map-dblocks 'org-update-dblock)))
  10827. ;;;; Completion
  10828. (declare-function org-export-backend-name "org-export" (cl-x))
  10829. (declare-function org-export-backend-options "org-export" (cl-x))
  10830. (defun org-get-export-keywords ()
  10831. "Return a list of all currently understood export keywords.
  10832. Export keywords include options, block names, attributes and
  10833. keywords relative to each registered export back-end."
  10834. (let (keywords)
  10835. (dolist (backend
  10836. (org-bound-and-true-p org-export--registered-backends)
  10837. (delq nil keywords))
  10838. ;; Back-end name (for keywords, like #+LATEX:)
  10839. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10840. (dolist (option-entry (org-export-backend-options backend))
  10841. ;; Back-end options.
  10842. (push (nth 1 option-entry) keywords)))))
  10843. (defconst org-options-keywords
  10844. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10845. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10846. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10847. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10848. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10849. (defcustom org-structure-template-alist
  10850. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10851. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10852. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10853. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10854. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10855. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10856. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10857. "<literal style=\"latex\">\n?\n</literal>")
  10858. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10859. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10860. "<literal style=\"html\">\n?\n</literal>")
  10861. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10862. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10863. ("A" "#+ASCII: " "")
  10864. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10865. ("I" "#+INCLUDE: %file ?"
  10866. "<include file=%file markup=\"?\">"))
  10867. "Structure completion elements.
  10868. This is a list of abbreviation keys and values. The value gets inserted
  10869. if you type `<' followed by the key and then press the completion key,
  10870. usually `M-TAB'. %file will be replaced by a file name after prompting
  10871. for the file using completion. The cursor will be placed at the position
  10872. of the `?` in the template.
  10873. There are two templates for each key, the first uses the original Org syntax,
  10874. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10875. the default when the /org-mtags.el/ module has been loaded. See also the
  10876. variable `org-mtags-prefer-muse-templates'."
  10877. :group 'org-completion
  10878. :type '(repeat
  10879. (list
  10880. (string :tag "Key")
  10881. (string :tag "Template")
  10882. (string :tag "Muse Template"))))
  10883. (defun org-try-structure-completion ()
  10884. "Try to complete a structure template before point.
  10885. This looks for strings like \"<e\" on an otherwise empty line and
  10886. expands them."
  10887. (let ((l (buffer-substring (point-at-bol) (point)))
  10888. a)
  10889. (when (and (looking-at "[ \t]*$")
  10890. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10891. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10892. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10893. (match-beginning 1)) a)
  10894. t)))
  10895. (defun org-complete-expand-structure-template (start cell)
  10896. "Expand a structure template."
  10897. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10898. (rpl (nth (if musep 2 1) cell))
  10899. (ind ""))
  10900. (delete-region start (point))
  10901. (when (string-match "\\`#\\+" rpl)
  10902. (cond
  10903. ((bolp))
  10904. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10905. (setq ind (buffer-substring (point-at-bol) (point))))
  10906. (t (newline))))
  10907. (setq start (point))
  10908. (if (string-match "%file" rpl)
  10909. (setq rpl (replace-match
  10910. (concat
  10911. "\""
  10912. (save-match-data
  10913. (abbreviate-file-name (read-file-name "Include file: ")))
  10914. "\"")
  10915. t t rpl)))
  10916. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10917. (concat "\n" ind)))
  10918. (insert rpl)
  10919. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10920. ;;;; TODO, DEADLINE, Comments
  10921. (defun org-toggle-comment ()
  10922. "Change the COMMENT state of an entry."
  10923. (interactive)
  10924. (save-excursion
  10925. (org-back-to-heading)
  10926. (let (case-fold-search)
  10927. (cond
  10928. ((looking-at (format org-heading-keyword-regexp-format
  10929. org-comment-string))
  10930. (goto-char (match-end 1))
  10931. (looking-at (concat " +" org-comment-string))
  10932. (replace-match "" t t)
  10933. (when (eolp) (insert " ")))
  10934. ((looking-at org-outline-regexp)
  10935. (goto-char (match-end 0))
  10936. (insert org-comment-string " "))))))
  10937. (defvar org-last-todo-state-is-todo nil
  10938. "This is non-nil when the last TODO state change led to a TODO state.
  10939. If the last change removed the TODO tag or switched to DONE, then
  10940. this is nil.")
  10941. (defvar org-setting-tags nil) ; dynamically skipped
  10942. (defvar org-todo-setup-filter-hook nil
  10943. "Hook for functions that pre-filter todo specs.
  10944. Each function takes a todo spec and returns either nil or the spec
  10945. transformed into canonical form." )
  10946. (defvar org-todo-get-default-hook nil
  10947. "Hook for functions that get a default item for todo.
  10948. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10949. nil or a string to be used for the todo mark." )
  10950. (defvar org-agenda-headline-snapshot-before-repeat)
  10951. (defun org-current-effective-time ()
  10952. "Return current time adjusted for `org-extend-today-until' variable."
  10953. (let* ((ct (org-current-time))
  10954. (dct (decode-time ct))
  10955. (ct1
  10956. (cond
  10957. (org-use-last-clock-out-time-as-effective-time
  10958. (or (org-clock-get-last-clock-out-time) ct))
  10959. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10960. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10961. (t ct))))
  10962. ct1))
  10963. (defun org-todo-yesterday (&optional arg)
  10964. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10965. (interactive "P")
  10966. (if (eq major-mode 'org-agenda-mode)
  10967. (apply 'org-agenda-todo-yesterday arg)
  10968. (let* ((hour (third (decode-time
  10969. (org-current-time))))
  10970. (org-extend-today-until (1+ hour)))
  10971. (org-todo arg))))
  10972. (defvar org-block-entry-blocking ""
  10973. "First entry preventing the TODO state change.")
  10974. (defun org-todo (&optional arg)
  10975. "Change the TODO state of an item.
  10976. The state of an item is given by a keyword at the start of the heading,
  10977. like
  10978. *** TODO Write paper
  10979. *** DONE Call mom
  10980. The different keywords are specified in the variable `org-todo-keywords'.
  10981. By default the available states are \"TODO\" and \"DONE\".
  10982. So for this example: when the item starts with TODO, it is changed to DONE.
  10983. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10984. DONE are present, add TODO at the beginning of the heading.
  10985. With \\[universal-argument] prefix arg, use completion to determine the new \
  10986. state.
  10987. With numeric prefix arg, switch to that state.
  10988. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10989. keywords (nextset).
  10990. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10991. With a numeric prefix arg of 0, inhibit note taking for the change.
  10992. For calling through lisp, arg is also interpreted in the following way:
  10993. 'none -> empty state
  10994. \"\"(empty string) -> switch to empty state
  10995. 'done -> switch to DONE
  10996. 'nextset -> switch to the next set of keywords
  10997. 'previousset -> switch to the previous set of keywords
  10998. \"WAITING\" -> switch to the specified keyword, but only if it
  10999. really is a member of `org-todo-keywords'."
  11000. (interactive "P")
  11001. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11002. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11003. 'region-start-level 'region))
  11004. org-loop-over-headlines-in-active-region)
  11005. (org-map-entries
  11006. `(org-todo ,arg)
  11007. org-loop-over-headlines-in-active-region
  11008. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11009. (if (equal arg '(16)) (setq arg 'nextset))
  11010. (let ((org-blocker-hook org-blocker-hook)
  11011. commentp
  11012. case-fold-search)
  11013. (when (equal arg '(64))
  11014. (setq arg nil org-blocker-hook nil))
  11015. (when (and org-blocker-hook
  11016. (or org-inhibit-blocking
  11017. (org-entry-get nil "NOBLOCKING")))
  11018. (setq org-blocker-hook nil))
  11019. (save-excursion
  11020. (catch 'exit
  11021. (org-back-to-heading t)
  11022. (when (looking-at (concat "^\\*+ " org-comment-string))
  11023. (org-toggle-comment)
  11024. (setq commentp t))
  11025. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11026. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11027. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11028. (let* ((match-data (match-data))
  11029. (startpos (point-at-bol))
  11030. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11031. (org-log-done org-log-done)
  11032. (org-log-repeat org-log-repeat)
  11033. (org-todo-log-states org-todo-log-states)
  11034. (org-inhibit-logging
  11035. (if (equal arg 0)
  11036. (progn (setq arg nil) 'note) org-inhibit-logging))
  11037. (this (match-string 1))
  11038. (hl-pos (match-beginning 0))
  11039. (head (org-get-todo-sequence-head this))
  11040. (ass (assoc head org-todo-kwd-alist))
  11041. (interpret (nth 1 ass))
  11042. (done-word (nth 3 ass))
  11043. (final-done-word (nth 4 ass))
  11044. (org-last-state (or this ""))
  11045. (completion-ignore-case t)
  11046. (member (member this org-todo-keywords-1))
  11047. (tail (cdr member))
  11048. (org-state (cond
  11049. ((and org-todo-key-trigger
  11050. (or (and (equal arg '(4))
  11051. (eq org-use-fast-todo-selection 'prefix))
  11052. (and (not arg) org-use-fast-todo-selection
  11053. (not (eq org-use-fast-todo-selection
  11054. 'prefix)))))
  11055. ;; Use fast selection
  11056. (org-fast-todo-selection))
  11057. ((and (equal arg '(4))
  11058. (or (not org-use-fast-todo-selection)
  11059. (not org-todo-key-trigger)))
  11060. ;; Read a state with completion
  11061. (org-icompleting-read
  11062. "State: " (mapcar 'list org-todo-keywords-1)
  11063. nil t))
  11064. ((eq arg 'right)
  11065. (if this
  11066. (if tail (car tail) nil)
  11067. (car org-todo-keywords-1)))
  11068. ((eq arg 'left)
  11069. (if (equal member org-todo-keywords-1)
  11070. nil
  11071. (if this
  11072. (nth (- (length org-todo-keywords-1)
  11073. (length tail) 2)
  11074. org-todo-keywords-1)
  11075. (org-last org-todo-keywords-1))))
  11076. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11077. (setq arg nil))) ; hack to fall back to cycling
  11078. (arg
  11079. ;; user or caller requests a specific state
  11080. (cond
  11081. ((equal arg "") nil)
  11082. ((eq arg 'none) nil)
  11083. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11084. ((eq arg 'nextset)
  11085. (or (car (cdr (member head org-todo-heads)))
  11086. (car org-todo-heads)))
  11087. ((eq arg 'previousset)
  11088. (let ((org-todo-heads (reverse org-todo-heads)))
  11089. (or (car (cdr (member head org-todo-heads)))
  11090. (car org-todo-heads))))
  11091. ((car (member arg org-todo-keywords-1)))
  11092. ((stringp arg)
  11093. (user-error "State `%s' not valid in this file" arg))
  11094. ((nth (1- (prefix-numeric-value arg))
  11095. org-todo-keywords-1))))
  11096. ((null member) (or head (car org-todo-keywords-1)))
  11097. ((equal this final-done-word) nil) ;; -> make empty
  11098. ((null tail) nil) ;; -> first entry
  11099. ((memq interpret '(type priority))
  11100. (if (eq this-command last-command)
  11101. (car tail)
  11102. (if (> (length tail) 0)
  11103. (or done-word (car org-done-keywords))
  11104. nil)))
  11105. (t
  11106. (car tail))))
  11107. (org-state (or
  11108. (run-hook-with-args-until-success
  11109. 'org-todo-get-default-hook org-state org-last-state)
  11110. org-state))
  11111. (next (if org-state (concat " " org-state " ") " "))
  11112. (change-plist (list :type 'todo-state-change :from this :to org-state
  11113. :position startpos))
  11114. dolog now-done-p)
  11115. (when org-blocker-hook
  11116. (setq org-last-todo-state-is-todo
  11117. (not (member this org-done-keywords)))
  11118. (unless (save-excursion
  11119. (save-match-data
  11120. (org-with-wide-buffer
  11121. (run-hook-with-args-until-failure
  11122. 'org-blocker-hook change-plist))))
  11123. (if (org-called-interactively-p 'interactive)
  11124. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11125. this org-state org-block-entry-blocking)
  11126. ;; fail silently
  11127. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11128. this org-state org-block-entry-blocking)
  11129. (throw 'exit nil))))
  11130. (store-match-data match-data)
  11131. (replace-match next t t)
  11132. (unless (pos-visible-in-window-p hl-pos)
  11133. (message "TODO state changed to %s" (org-trim next)))
  11134. (unless head
  11135. (setq head (org-get-todo-sequence-head org-state)
  11136. ass (assoc head org-todo-kwd-alist)
  11137. interpret (nth 1 ass)
  11138. done-word (nth 3 ass)
  11139. final-done-word (nth 4 ass)))
  11140. (when (memq arg '(nextset previousset))
  11141. (message "Keyword-Set %d/%d: %s"
  11142. (- (length org-todo-sets) -1
  11143. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11144. (length org-todo-sets)
  11145. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11146. (setq org-last-todo-state-is-todo
  11147. (not (member org-state org-done-keywords)))
  11148. (setq now-done-p (and (member org-state org-done-keywords)
  11149. (not (member this org-done-keywords))))
  11150. (and logging (org-local-logging logging))
  11151. (when (and (or org-todo-log-states org-log-done)
  11152. (not (eq org-inhibit-logging t))
  11153. (not (memq arg '(nextset previousset))))
  11154. ;; we need to look at recording a time and note
  11155. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11156. (nth 2 (assoc this org-todo-log-states))))
  11157. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11158. (setq dolog 'time))
  11159. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11160. (and org-state
  11161. (member org-state org-not-done-keywords)
  11162. (not (member this org-not-done-keywords))))
  11163. ;; This is now a todo state and was not one before
  11164. ;; If there was a CLOSED time stamp, get rid of it.
  11165. (org-add-planning-info nil nil 'closed))
  11166. (when (and now-done-p org-log-done)
  11167. ;; It is now done, and it was not done before
  11168. (org-add-planning-info 'closed (org-current-effective-time))
  11169. (if (and (not dolog) (eq 'note org-log-done))
  11170. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11171. (when (and org-state dolog)
  11172. ;; This is a non-nil state, and we need to log it
  11173. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11174. ;; Fixup tag positioning
  11175. (org-todo-trigger-tag-changes org-state)
  11176. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11177. (when org-provide-todo-statistics
  11178. (org-update-parent-todo-statistics))
  11179. (run-hooks 'org-after-todo-state-change-hook)
  11180. (if (and arg (not (member org-state org-done-keywords)))
  11181. (setq head (org-get-todo-sequence-head org-state)))
  11182. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11183. ;; Do we need to trigger a repeat?
  11184. (when now-done-p
  11185. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11186. ;; This is for the agenda, take a snapshot of the headline.
  11187. (save-match-data
  11188. (setq org-agenda-headline-snapshot-before-repeat
  11189. (org-get-heading))))
  11190. (org-auto-repeat-maybe org-state))
  11191. ;; Fixup cursor location if close to the keyword
  11192. (if (and (outline-on-heading-p)
  11193. (not (bolp))
  11194. (save-excursion (beginning-of-line 1)
  11195. (looking-at org-todo-line-regexp))
  11196. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11197. (progn
  11198. (goto-char (or (match-end 2) (match-end 1)))
  11199. (and (looking-at " ") (just-one-space))))
  11200. (when org-trigger-hook
  11201. (save-excursion
  11202. (run-hook-with-args 'org-trigger-hook change-plist)))
  11203. (when commentp (org-toggle-comment))))))))
  11204. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11205. "Block turning an entry into a TODO, using the hierarchy.
  11206. This checks whether the current task should be blocked from state
  11207. changes. Such blocking occurs when:
  11208. 1. The task has children which are not all in a completed state.
  11209. 2. A task has a parent with the property :ORDERED:, and there
  11210. are siblings prior to the current task with incomplete
  11211. status.
  11212. 3. The parent of the task is blocked because it has siblings that should
  11213. be done first, or is child of a block grandparent TODO entry."
  11214. (if (not org-enforce-todo-dependencies)
  11215. t ; if locally turned off don't block
  11216. (catch 'dont-block
  11217. ;; If this is not a todo state change, or if this entry is already DONE,
  11218. ;; do not block
  11219. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11220. (member (plist-get change-plist :from)
  11221. (cons 'done org-done-keywords))
  11222. (member (plist-get change-plist :to)
  11223. (cons 'todo org-not-done-keywords))
  11224. (not (plist-get change-plist :to)))
  11225. (throw 'dont-block t))
  11226. ;; If this task has children, and any are undone, it's blocked
  11227. (save-excursion
  11228. (org-back-to-heading t)
  11229. (let ((this-level (funcall outline-level)))
  11230. (outline-next-heading)
  11231. (let ((child-level (funcall outline-level)))
  11232. (while (and (not (eobp))
  11233. (> child-level this-level))
  11234. ;; this todo has children, check whether they are all
  11235. ;; completed
  11236. (if (and (not (org-entry-is-done-p))
  11237. (org-entry-is-todo-p))
  11238. (progn (setq org-block-entry-blocking (org-get-heading))
  11239. (throw 'dont-block nil)))
  11240. (outline-next-heading)
  11241. (setq child-level (funcall outline-level))))))
  11242. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11243. ;; any previous siblings are undone, it's blocked
  11244. (save-excursion
  11245. (org-back-to-heading t)
  11246. (let* ((pos (point))
  11247. (parent-pos (and (org-up-heading-safe) (point))))
  11248. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11249. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11250. (forward-line 1)
  11251. (re-search-forward org-not-done-heading-regexp pos t))
  11252. (setq org-block-entry-blocking (match-string 0))
  11253. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11254. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11255. (while t
  11256. (goto-char parent-pos)
  11257. (if (not (looking-at org-not-done-heading-regexp))
  11258. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11259. (setq pos (point))
  11260. (setq parent-pos (and (org-up-heading-safe) (point)))
  11261. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11262. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11263. (forward-line 1)
  11264. (re-search-forward org-not-done-heading-regexp pos t)
  11265. (setq org-block-entry-blocking (org-get-heading)))
  11266. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11267. (defcustom org-track-ordered-property-with-tag nil
  11268. "Should the ORDERED property also be shown as a tag?
  11269. The ORDERED property decides if an entry should require subtasks to be
  11270. completed in sequence. Since a property is not very visible, setting
  11271. this option means that toggling the ORDERED property with the command
  11272. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11273. not relevant for the behavior, but it makes things more visible.
  11274. Note that toggling the tag with tags commands will not change the property
  11275. and therefore not influence behavior!
  11276. This can be t, meaning the tag ORDERED should be used, It can also be a
  11277. string to select a different tag for this task."
  11278. :group 'org-todo
  11279. :type '(choice
  11280. (const :tag "No tracking" nil)
  11281. (const :tag "Track with ORDERED tag" t)
  11282. (string :tag "Use other tag")))
  11283. (defun org-toggle-ordered-property ()
  11284. "Toggle the ORDERED property of the current entry.
  11285. For better visibility, you can track the value of this property with a tag.
  11286. See variable `org-track-ordered-property-with-tag'."
  11287. (interactive)
  11288. (let* ((t1 org-track-ordered-property-with-tag)
  11289. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11290. (save-excursion
  11291. (org-back-to-heading)
  11292. (if (org-entry-get nil "ORDERED")
  11293. (progn
  11294. (org-delete-property "ORDERED" "PROPERTIES")
  11295. (and tag (org-toggle-tag tag 'off))
  11296. (message "Subtasks can be completed in arbitrary order"))
  11297. (org-entry-put nil "ORDERED" "t")
  11298. (and tag (org-toggle-tag tag 'on))
  11299. (message "Subtasks must be completed in sequence")))))
  11300. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11301. (defun org-block-todo-from-checkboxes (change-plist)
  11302. "Block turning an entry into a TODO, using checkboxes.
  11303. This checks whether the current task should be blocked from state
  11304. changes because there are unchecked boxes in this entry."
  11305. (if (not org-enforce-todo-checkbox-dependencies)
  11306. t ; if locally turned off don't block
  11307. (catch 'dont-block
  11308. ;; If this is not a todo state change, or if this entry is already DONE,
  11309. ;; do not block
  11310. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11311. (member (plist-get change-plist :from)
  11312. (cons 'done org-done-keywords))
  11313. (member (plist-get change-plist :to)
  11314. (cons 'todo org-not-done-keywords))
  11315. (not (plist-get change-plist :to)))
  11316. (throw 'dont-block t))
  11317. ;; If this task has checkboxes that are not checked, it's blocked
  11318. (save-excursion
  11319. (org-back-to-heading t)
  11320. (let ((beg (point)) end)
  11321. (outline-next-heading)
  11322. (setq end (point))
  11323. (goto-char beg)
  11324. (if (org-list-search-forward
  11325. (concat (org-item-beginning-re)
  11326. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11327. "\\[[- ]\\]")
  11328. end t)
  11329. (progn
  11330. (if (boundp 'org-blocked-by-checkboxes)
  11331. (setq org-blocked-by-checkboxes t))
  11332. (throw 'dont-block nil)))))
  11333. t))) ; do not block
  11334. (defun org-entry-blocked-p ()
  11335. "Is the current entry blocked?"
  11336. (org-with-silent-modifications
  11337. (if (org-entry-get nil "NOBLOCKING")
  11338. nil ;; Never block this entry
  11339. (not (run-hook-with-args-until-failure
  11340. 'org-blocker-hook
  11341. (list :type 'todo-state-change
  11342. :position (point)
  11343. :from 'todo
  11344. :to 'done))))))
  11345. (defun org-update-statistics-cookies (all)
  11346. "Update the statistics cookie, either from TODO or from checkboxes.
  11347. This should be called with the cursor in a line with a statistics cookie."
  11348. (interactive "P")
  11349. (if all
  11350. (progn
  11351. (org-update-checkbox-count 'all)
  11352. (org-map-entries 'org-update-parent-todo-statistics))
  11353. (if (not (org-at-heading-p))
  11354. (org-update-checkbox-count)
  11355. (let ((pos (point-marker))
  11356. end l1 l2)
  11357. (ignore-errors (org-back-to-heading t))
  11358. (if (not (org-at-heading-p))
  11359. (org-update-checkbox-count)
  11360. (setq l1 (org-outline-level))
  11361. (setq end (save-excursion
  11362. (outline-next-heading)
  11363. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11364. (point)))
  11365. (if (and (save-excursion
  11366. (re-search-forward
  11367. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11368. (not (save-excursion (re-search-forward
  11369. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11370. (org-update-checkbox-count)
  11371. (if (and l2 (> l2 l1))
  11372. (progn
  11373. (goto-char end)
  11374. (org-update-parent-todo-statistics))
  11375. (goto-char pos)
  11376. (beginning-of-line 1)
  11377. (while (re-search-forward
  11378. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11379. (point-at-eol) t)
  11380. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11381. (goto-char pos)
  11382. (move-marker pos nil)))))
  11383. (defvar org-entry-property-inherited-from) ;; defined below
  11384. (defun org-update-parent-todo-statistics ()
  11385. "Update any statistics cookie in the parent of the current headline.
  11386. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11387. the entire subtree and this will travel up the hierarchy and update
  11388. statistics everywhere."
  11389. (let* ((prop (save-excursion (org-up-heading-safe)
  11390. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11391. (recursive (or (not org-hierarchical-todo-statistics)
  11392. (and prop (string-match "\\<recursive\\>" prop))))
  11393. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11394. 0))
  11395. (first t)
  11396. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11397. level ltoggle l1 new ndel
  11398. (cnt-all 0) (cnt-done 0) is-percent kwd
  11399. checkbox-beg ov ovs ove cookie-present)
  11400. (catch 'exit
  11401. (save-excursion
  11402. (beginning-of-line 1)
  11403. (setq ltoggle (funcall outline-level))
  11404. ;; Three situations are to consider:
  11405. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11406. ;; to the top-level ancestor on the headline;
  11407. ;; 2. If parent has "recursive" property, repeat up to the
  11408. ;; headline setting that property, taking inheritance into
  11409. ;; account;
  11410. ;; 3. Else, move up to direct parent and proceed only once.
  11411. (while (and (setq level (org-up-heading-safe))
  11412. (or recursive first)
  11413. (>= (point) lim))
  11414. (setq first nil cookie-present nil)
  11415. (unless (and level
  11416. (not (string-match
  11417. "\\<checkbox\\>"
  11418. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11419. "")))))
  11420. (throw 'exit nil))
  11421. (while (re-search-forward box-re (point-at-eol) t)
  11422. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11423. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11424. (save-match-data
  11425. (unless (outline-next-heading) (throw 'exit nil))
  11426. (while (and (looking-at org-complex-heading-regexp)
  11427. (> (setq l1 (length (match-string 1))) level))
  11428. (setq kwd (and (or recursive (= l1 ltoggle))
  11429. (match-string 2)))
  11430. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11431. (and (listp org-provide-todo-statistics)
  11432. (or (member kwd org-provide-todo-statistics)
  11433. (member kwd org-done-keywords))))
  11434. (setq cnt-all (1+ cnt-all))
  11435. (if (eq org-provide-todo-statistics t)
  11436. (and kwd (setq cnt-all (1+ cnt-all)))))
  11437. (and (member kwd org-done-keywords)
  11438. (setq cnt-done (1+ cnt-done)))
  11439. (outline-next-heading)))
  11440. (setq new
  11441. (if is-percent
  11442. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11443. (format "[%d/%d]" cnt-done cnt-all))
  11444. ndel (- (match-end 0) checkbox-beg))
  11445. ;; handle overlays when updating cookie from column view
  11446. (when (setq ov (car (overlays-at checkbox-beg)))
  11447. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11448. (delete-overlay ov))
  11449. (goto-char checkbox-beg)
  11450. (insert new)
  11451. (delete-region (point) (+ (point) ndel))
  11452. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11453. (when ov (move-overlay ov ovs ove)))
  11454. (when cookie-present
  11455. (run-hook-with-args 'org-after-todo-statistics-hook
  11456. cnt-done (- cnt-all cnt-done))))))
  11457. (run-hooks 'org-todo-statistics-hook)))
  11458. (defvar org-after-todo-statistics-hook nil
  11459. "Hook that is called after a TODO statistics cookie has been updated.
  11460. Each function is called with two arguments: the number of not-done entries
  11461. and the number of done entries.
  11462. For example, the following function, when added to this hook, will switch
  11463. an entry to DONE when all children are done, and back to TODO when new
  11464. entries are set to a TODO status. Note that this hook is only called
  11465. when there is a statistics cookie in the headline!
  11466. (defun org-summary-todo (n-done n-not-done)
  11467. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11468. (let (org-log-done org-log-states) ; turn off logging
  11469. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11470. ")
  11471. (defvar org-todo-statistics-hook nil
  11472. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11473. This hook runs even if there is no statistics cookie present, in which case
  11474. `org-after-todo-statistics-hook' would not run.")
  11475. (defun org-todo-trigger-tag-changes (state)
  11476. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11477. (let ((l org-todo-state-tags-triggers)
  11478. changes)
  11479. (when (or (not state) (equal state ""))
  11480. (setq changes (append changes (cdr (assoc "" l)))))
  11481. (when (and (stringp state) (> (length state) 0))
  11482. (setq changes (append changes (cdr (assoc state l)))))
  11483. (when (member state org-not-done-keywords)
  11484. (setq changes (append changes (cdr (assoc 'todo l)))))
  11485. (when (member state org-done-keywords)
  11486. (setq changes (append changes (cdr (assoc 'done l)))))
  11487. (dolist (c changes)
  11488. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11489. (defun org-local-logging (value)
  11490. "Get logging settings from a property VALUE."
  11491. (let* (words w a)
  11492. ;; directly set the variables, they are already local.
  11493. (setq org-log-done nil
  11494. org-log-repeat nil
  11495. org-todo-log-states nil)
  11496. (setq words (org-split-string value))
  11497. (while (setq w (pop words))
  11498. (cond
  11499. ((setq a (assoc w org-startup-options))
  11500. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11501. (set (nth 1 a) (nth 2 a))))
  11502. ((setq a (org-extract-log-state-settings w))
  11503. (and (member (car a) org-todo-keywords-1)
  11504. (push a org-todo-log-states)))))))
  11505. (defun org-get-todo-sequence-head (kwd)
  11506. "Return the head of the TODO sequence to which KWD belongs.
  11507. If KWD is not set, check if there is a text property remembering the
  11508. right sequence."
  11509. (let (p)
  11510. (cond
  11511. ((not kwd)
  11512. (or (get-text-property (point-at-bol) 'org-todo-head)
  11513. (progn
  11514. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11515. nil (point-at-eol)))
  11516. (get-text-property p 'org-todo-head))))
  11517. ((not (member kwd org-todo-keywords-1))
  11518. (car org-todo-keywords-1))
  11519. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11520. (defun org-fast-todo-selection ()
  11521. "Fast TODO keyword selection with single keys.
  11522. Returns the new TODO keyword, or nil if no state change should occur."
  11523. (let* ((fulltable org-todo-key-alist)
  11524. (done-keywords org-done-keywords) ;; needed for the faces.
  11525. (maxlen (apply 'max (mapcar
  11526. (lambda (x)
  11527. (if (stringp (car x)) (string-width (car x)) 0))
  11528. fulltable)))
  11529. (expert nil)
  11530. (fwidth (+ maxlen 3 1 3))
  11531. (ncol (/ (- (window-width) 4) fwidth))
  11532. tg cnt e c tbl
  11533. groups ingroup)
  11534. (save-excursion
  11535. (save-window-excursion
  11536. (if expert
  11537. (set-buffer (get-buffer-create " *Org todo*"))
  11538. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11539. (erase-buffer)
  11540. (org-set-local 'org-done-keywords done-keywords)
  11541. (setq tbl fulltable cnt 0)
  11542. (while (setq e (pop tbl))
  11543. (cond
  11544. ((equal e '(:startgroup))
  11545. (push '() groups) (setq ingroup t)
  11546. (when (not (= cnt 0))
  11547. (setq cnt 0)
  11548. (insert "\n"))
  11549. (insert "{ "))
  11550. ((equal e '(:endgroup))
  11551. (setq ingroup nil cnt 0)
  11552. (insert "}\n"))
  11553. ((equal e '(:newline))
  11554. (when (not (= cnt 0))
  11555. (setq cnt 0)
  11556. (insert "\n")
  11557. (setq e (car tbl))
  11558. (while (equal (car tbl) '(:newline))
  11559. (insert "\n")
  11560. (setq tbl (cdr tbl)))))
  11561. (t
  11562. (setq tg (car e) c (cdr e))
  11563. (if ingroup (push tg (car groups)))
  11564. (setq tg (org-add-props tg nil 'face
  11565. (org-get-todo-face tg)))
  11566. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11567. (insert "[" c "] " tg (make-string
  11568. (- fwidth 4 (length tg)) ?\ ))
  11569. (when (= (setq cnt (1+ cnt)) ncol)
  11570. (insert "\n")
  11571. (if ingroup (insert " "))
  11572. (setq cnt 0)))))
  11573. (insert "\n")
  11574. (goto-char (point-min))
  11575. (if (not expert) (org-fit-window-to-buffer))
  11576. (message "[a-z..]:Set [SPC]:clear")
  11577. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11578. (cond
  11579. ((or (= c ?\C-g)
  11580. (and (= c ?q) (not (rassoc c fulltable))))
  11581. (setq quit-flag t))
  11582. ((= c ?\ ) nil)
  11583. ((setq e (rassoc c fulltable) tg (car e))
  11584. tg)
  11585. (t (setq quit-flag t)))))))
  11586. (defun org-entry-is-todo-p ()
  11587. (member (org-get-todo-state) org-not-done-keywords))
  11588. (defun org-entry-is-done-p ()
  11589. (member (org-get-todo-state) org-done-keywords))
  11590. (defun org-get-todo-state ()
  11591. "Return the TODO keyword of the current subtree."
  11592. (save-excursion
  11593. (org-back-to-heading t)
  11594. (and (looking-at org-todo-line-regexp)
  11595. (match-end 2)
  11596. (match-string 2))))
  11597. (defun org-at-date-range-p (&optional inactive-ok)
  11598. "Is the cursor inside a date range?"
  11599. (interactive)
  11600. (save-excursion
  11601. (catch 'exit
  11602. (let ((pos (point)))
  11603. (skip-chars-backward "^[<\r\n")
  11604. (skip-chars-backward "<[")
  11605. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11606. (>= (match-end 0) pos)
  11607. (throw 'exit t))
  11608. (skip-chars-backward "^<[\r\n")
  11609. (skip-chars-backward "<[")
  11610. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11611. (>= (match-end 0) pos)
  11612. (throw 'exit t)))
  11613. nil)))
  11614. (defun org-get-repeat (&optional tagline)
  11615. "Check if there is a deadline/schedule with repeater in this entry."
  11616. (save-match-data
  11617. (save-excursion
  11618. (org-back-to-heading t)
  11619. (and (re-search-forward (if tagline
  11620. (concat tagline "\\s-*" org-repeat-re)
  11621. org-repeat-re)
  11622. (org-entry-end-position) t)
  11623. (match-string-no-properties 1)))))
  11624. (defvar org-last-changed-timestamp)
  11625. (defvar org-last-inserted-timestamp)
  11626. (defvar org-log-post-message)
  11627. (defvar org-log-note-purpose)
  11628. (defvar org-log-note-how)
  11629. (defvar org-log-note-extra)
  11630. (defun org-auto-repeat-maybe (done-word)
  11631. "Check if the current headline contains a repeated deadline/schedule.
  11632. If yes, set TODO state back to what it was and change the base date
  11633. of repeating deadline/scheduled time stamps to new date.
  11634. This function is run automatically after each state change to a DONE state."
  11635. ;; last-state is dynamically scoped into this function
  11636. (let* ((repeat (org-get-repeat))
  11637. (aa (assoc org-last-state org-todo-kwd-alist))
  11638. (interpret (nth 1 aa))
  11639. (head (nth 2 aa))
  11640. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11641. (msg "Entry repeats: ")
  11642. (org-log-done nil)
  11643. (org-todo-log-states nil)
  11644. re type n what ts time to-state)
  11645. (when repeat
  11646. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11647. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11648. org-todo-repeat-to-state))
  11649. (unless (and to-state (member to-state org-todo-keywords-1))
  11650. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11651. (org-todo to-state)
  11652. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11653. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11654. (org-time-stamp-format t t))))
  11655. (when org-log-repeat
  11656. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11657. (memq 'org-add-log-note post-command-hook))
  11658. ;; OK, we are already setup for some record
  11659. (if (eq org-log-repeat 'note)
  11660. ;; make sure we take a note, not only a time stamp
  11661. (setq org-log-note-how 'note))
  11662. ;; Set up for taking a record
  11663. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11664. org-last-state
  11665. 'findpos org-log-repeat)))
  11666. (org-back-to-heading t)
  11667. (org-add-planning-info nil nil 'closed)
  11668. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11669. org-deadline-time-regexp "\\)\\|\\("
  11670. org-ts-regexp "\\)"))
  11671. (while (re-search-forward
  11672. re (save-excursion (outline-next-heading) (point)) t)
  11673. (setq type (if (match-end 1) org-scheduled-string
  11674. (if (match-end 3) org-deadline-string "Plain:"))
  11675. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11676. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11677. (setq n (string-to-number (match-string 2 ts))
  11678. what (match-string 3 ts))
  11679. (if (equal what "w") (setq n (* n 7) what "d"))
  11680. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11681. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11682. ;; Preparation, see if we need to modify the start date for the change
  11683. (when (match-end 1)
  11684. (setq time (save-match-data (org-time-string-to-time ts)))
  11685. (cond
  11686. ((equal (match-string 1 ts) ".")
  11687. ;; Shift starting date to today
  11688. (org-timestamp-change
  11689. (- (org-today) (time-to-days time))
  11690. 'day))
  11691. ((equal (match-string 1 ts) "+")
  11692. (let ((nshiftmax 10) (nshift 0))
  11693. (while (or (= nshift 0)
  11694. (<= (time-to-days time)
  11695. (time-to-days (current-time))))
  11696. (when (= (incf nshift) nshiftmax)
  11697. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11698. (error "Abort")))
  11699. (org-timestamp-change n (cdr (assoc what whata)))
  11700. (org-at-timestamp-p t)
  11701. (setq ts (match-string 1))
  11702. (setq time (save-match-data (org-time-string-to-time ts)))))
  11703. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11704. ;; rematch, so that we have everything in place for the real shift
  11705. (org-at-timestamp-p t)
  11706. (setq ts (match-string 1))
  11707. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11708. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11709. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11710. (setq org-log-post-message msg)
  11711. (message "%s" msg))))
  11712. (defun org-show-todo-tree (arg)
  11713. "Make a compact tree which shows all headlines marked with TODO.
  11714. The tree will show the lines where the regexp matches, and all higher
  11715. headlines above the match.
  11716. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11717. With a numeric prefix N, construct a sparse tree for the Nth element
  11718. of `org-todo-keywords-1'."
  11719. (interactive "P")
  11720. (let ((case-fold-search nil)
  11721. (kwd-re
  11722. (cond ((null arg) org-not-done-regexp)
  11723. ((equal arg '(4))
  11724. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11725. (mapcar 'list org-todo-keywords-1))))
  11726. (concat "\\("
  11727. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11728. "\\)\\>")))
  11729. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11730. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11731. org-todo-keywords-1)))
  11732. (t (user-error "Invalid prefix argument: %s" arg)))))
  11733. (message "%d TODO entries found"
  11734. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11735. (defun org-deadline (arg &optional time)
  11736. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11737. With one universal prefix argument, remove any deadline from the item.
  11738. With two universal prefix arguments, prompt for a warning delay.
  11739. With argument TIME, set the deadline at the corresponding date. TIME
  11740. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11741. (interactive "P")
  11742. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11743. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11744. 'region-start-level 'region))
  11745. org-loop-over-headlines-in-active-region)
  11746. (org-map-entries
  11747. `(org-deadline ',arg ,time)
  11748. org-loop-over-headlines-in-active-region
  11749. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11750. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11751. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11752. (repeater (and old-date
  11753. (string-match
  11754. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11755. old-date)
  11756. (match-string 1 old-date))))
  11757. (cond
  11758. ((equal arg '(4))
  11759. (when (and old-date org-log-redeadline)
  11760. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11761. org-log-redeadline))
  11762. (org-remove-timestamp-with-keyword org-deadline-string)
  11763. (message "Item no longer has a deadline."))
  11764. ((equal arg '(16))
  11765. (save-excursion
  11766. (org-back-to-heading t)
  11767. (if (re-search-forward
  11768. org-deadline-time-regexp
  11769. (save-excursion (outline-next-heading) (point)) t)
  11770. (let* ((rpl0 (match-string 1))
  11771. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11772. (replace-match
  11773. (concat org-deadline-string
  11774. " <" rpl
  11775. (format " -%dd"
  11776. (abs
  11777. (- (time-to-days
  11778. (save-match-data
  11779. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11780. (time-to-days old-date-time))))
  11781. ">") t t))
  11782. (user-error "No deadline information to update"))))
  11783. (t
  11784. (org-add-planning-info 'deadline time 'closed)
  11785. (when (and old-date org-log-redeadline
  11786. (not (equal old-date
  11787. (substring org-last-inserted-timestamp 1 -1))))
  11788. (org-add-log-setup 'redeadline nil old-date 'findpos
  11789. org-log-redeadline))
  11790. (when repeater
  11791. (save-excursion
  11792. (org-back-to-heading t)
  11793. (when (re-search-forward (concat org-deadline-string " "
  11794. org-last-inserted-timestamp)
  11795. (save-excursion
  11796. (outline-next-heading) (point)) t)
  11797. (goto-char (1- (match-end 0)))
  11798. (insert " " repeater)
  11799. (setq org-last-inserted-timestamp
  11800. (concat (substring org-last-inserted-timestamp 0 -1)
  11801. " " repeater
  11802. (substring org-last-inserted-timestamp -1))))))
  11803. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11804. (defun org-schedule (arg &optional time)
  11805. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11806. With one universal prefix argument, remove any scheduling date from the item.
  11807. With two universal prefix arguments, prompt for a delay cookie.
  11808. With argument TIME, scheduled at the corresponding date. TIME can
  11809. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11810. (interactive "P")
  11811. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11812. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11813. 'region-start-level 'region))
  11814. org-loop-over-headlines-in-active-region)
  11815. (org-map-entries
  11816. `(org-schedule ',arg ,time)
  11817. org-loop-over-headlines-in-active-region
  11818. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11819. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11820. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11821. (repeater (and old-date
  11822. (string-match
  11823. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11824. old-date)
  11825. (match-string 1 old-date))))
  11826. (cond
  11827. ((equal arg '(4))
  11828. (progn
  11829. (when (and old-date org-log-reschedule)
  11830. (org-add-log-setup 'delschedule nil old-date 'findpos
  11831. org-log-reschedule))
  11832. (org-remove-timestamp-with-keyword org-scheduled-string)
  11833. (message "Item is no longer scheduled.")))
  11834. ((equal arg '(16))
  11835. (save-excursion
  11836. (org-back-to-heading t)
  11837. (if (re-search-forward
  11838. org-scheduled-time-regexp
  11839. (save-excursion (outline-next-heading) (point)) t)
  11840. (let* ((rpl0 (match-string 1))
  11841. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11842. (replace-match
  11843. (concat org-scheduled-string
  11844. " <" rpl
  11845. (format " -%dd"
  11846. (abs
  11847. (- (time-to-days
  11848. (save-match-data
  11849. (org-read-date nil t nil "Delay until" old-date-time)))
  11850. (time-to-days old-date-time))))
  11851. ">") t t))
  11852. (user-error "No scheduled information to update"))))
  11853. (t
  11854. (org-add-planning-info 'scheduled time 'closed)
  11855. (when (and old-date org-log-reschedule
  11856. (not (equal old-date
  11857. (substring org-last-inserted-timestamp 1 -1))))
  11858. (org-add-log-setup 'reschedule nil old-date 'findpos
  11859. org-log-reschedule))
  11860. (when repeater
  11861. (save-excursion
  11862. (org-back-to-heading t)
  11863. (when (re-search-forward (concat org-scheduled-string " "
  11864. org-last-inserted-timestamp)
  11865. (save-excursion
  11866. (outline-next-heading) (point)) t)
  11867. (goto-char (1- (match-end 0)))
  11868. (insert " " repeater)
  11869. (setq org-last-inserted-timestamp
  11870. (concat (substring org-last-inserted-timestamp 0 -1)
  11871. " " repeater
  11872. (substring org-last-inserted-timestamp -1))))))
  11873. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11874. (defun org-get-scheduled-time (pom &optional inherit)
  11875. "Get the scheduled time as a time tuple, of a format suitable
  11876. for calling org-schedule with, or if there is no scheduling,
  11877. returns nil."
  11878. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11879. (when time
  11880. (apply 'encode-time (org-parse-time-string time)))))
  11881. (defun org-get-deadline-time (pom &optional inherit)
  11882. "Get the deadline as a time tuple, of a format suitable for
  11883. calling org-deadline with, or if there is no scheduling, returns
  11884. nil."
  11885. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11886. (when time
  11887. (apply 'encode-time (org-parse-time-string time)))))
  11888. (defun org-remove-timestamp-with-keyword (keyword)
  11889. "Remove all time stamps with KEYWORD in the current entry."
  11890. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11891. beg)
  11892. (save-excursion
  11893. (org-back-to-heading t)
  11894. (setq beg (point))
  11895. (outline-next-heading)
  11896. (while (re-search-backward re beg t)
  11897. (replace-match "")
  11898. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11899. (equal (char-before) ?\ ))
  11900. (backward-delete-char 1)
  11901. (if (string-match "^[ \t]*$" (buffer-substring
  11902. (point-at-bol) (point-at-eol)))
  11903. (delete-region (point-at-bol)
  11904. (min (point-max) (1+ (point-at-eol))))))))))
  11905. (defvar org-time-was-given) ; dynamically scoped parameter
  11906. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11907. (defun org-add-planning-info (what &optional time &rest remove)
  11908. "Insert new timestamp with keyword in the line directly after the headline.
  11909. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11910. If non is given, the user is prompted for a date.
  11911. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11912. be removed."
  11913. (interactive)
  11914. (let (org-time-was-given org-end-time-was-given ts
  11915. end default-time default-input)
  11916. (catch 'exit
  11917. (when (and (memq what '(scheduled deadline))
  11918. (or (not time)
  11919. (and (stringp time)
  11920. (string-match "^[-+]+[0-9]" time))))
  11921. ;; Try to get a default date/time from existing timestamp
  11922. (save-excursion
  11923. (org-back-to-heading t)
  11924. (setq end (save-excursion (outline-next-heading) (point)))
  11925. (when (re-search-forward (if (eq what 'scheduled)
  11926. org-scheduled-time-regexp
  11927. org-deadline-time-regexp)
  11928. end t)
  11929. (setq ts (match-string 1)
  11930. default-time
  11931. (apply 'encode-time (org-parse-time-string ts))
  11932. default-input (and ts (org-get-compact-tod ts))))))
  11933. (when what
  11934. (setq time
  11935. (if (stringp time)
  11936. ;; This is a string (relative or absolute), set proper date
  11937. (apply 'encode-time
  11938. (org-read-date-analyze
  11939. time default-time (decode-time default-time)))
  11940. ;; If necessary, get the time from the user
  11941. (or time (org-read-date nil 'to-time nil nil
  11942. default-time default-input)))))
  11943. (when (and org-insert-labeled-timestamps-at-point
  11944. (member what '(scheduled deadline)))
  11945. (insert
  11946. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11947. (org-insert-time-stamp time org-time-was-given
  11948. nil nil nil (list org-end-time-was-given))
  11949. (setq what nil))
  11950. (save-excursion
  11951. (save-restriction
  11952. (let (col list elt ts buffer-invisibility-spec)
  11953. (org-back-to-heading t)
  11954. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11955. (goto-char (match-end 1))
  11956. (setq col (current-column))
  11957. (goto-char (match-end 0))
  11958. (if (eobp) (insert "\n") (forward-char 1))
  11959. (when (and (not what)
  11960. (not (looking-at
  11961. (concat "[ \t]*"
  11962. org-keyword-time-not-clock-regexp))))
  11963. ;; Nothing to add, nothing to remove...... :-)
  11964. (throw 'exit nil))
  11965. (if (and (not (looking-at org-outline-regexp))
  11966. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11967. "[^\r\n]*"))
  11968. (not (equal (match-string 1) org-clock-string)))
  11969. (narrow-to-region (match-beginning 0) (match-end 0))
  11970. (insert-before-markers "\n")
  11971. (backward-char 1)
  11972. (narrow-to-region (point) (point))
  11973. (and org-adapt-indentation (org-indent-to-column col)))
  11974. ;; Check if we have to remove something.
  11975. (setq list (cons what remove))
  11976. (while list
  11977. (setq elt (pop list))
  11978. (when (or (and (eq elt 'scheduled)
  11979. (re-search-forward org-scheduled-time-regexp nil t))
  11980. (and (eq elt 'deadline)
  11981. (re-search-forward org-deadline-time-regexp nil t))
  11982. (and (eq elt 'closed)
  11983. (re-search-forward org-closed-time-regexp nil t)))
  11984. (replace-match "")
  11985. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11986. (and (looking-at "[ \t]+") (replace-match ""))
  11987. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11988. (when what
  11989. (insert
  11990. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11991. (cond ((eq what 'scheduled) org-scheduled-string)
  11992. ((eq what 'deadline) org-deadline-string)
  11993. ((eq what 'closed) org-closed-string))
  11994. " ")
  11995. (setq ts (org-insert-time-stamp
  11996. time
  11997. (or org-time-was-given
  11998. (and (eq what 'closed) org-log-done-with-time))
  11999. (eq what 'closed)
  12000. nil nil (list org-end-time-was-given)))
  12001. (insert
  12002. (if (not (or (bolp) (eq (char-before) ?\ )
  12003. (memq (char-after) '(32 10))
  12004. (eobp))) " " ""))
  12005. (end-of-line 1))
  12006. (goto-char (point-min))
  12007. (widen)
  12008. (if (and (looking-at "[ \t]*\n")
  12009. (equal (char-before) ?\n))
  12010. (delete-region (1- (point)) (point-at-eol)))
  12011. ts))))))
  12012. (defvar org-log-note-marker (make-marker))
  12013. (defvar org-log-note-purpose nil)
  12014. (defvar org-log-note-state nil)
  12015. (defvar org-log-note-previous-state nil)
  12016. (defvar org-log-note-how nil)
  12017. (defvar org-log-note-extra nil)
  12018. (defvar org-log-note-window-configuration nil)
  12019. (defvar org-log-note-return-to (make-marker))
  12020. (defvar org-log-note-effective-time nil
  12021. "Remembered current time so that dynamically scoped
  12022. `org-extend-today-until' affects tha timestamps in state change
  12023. log")
  12024. (defvar org-log-post-message nil
  12025. "Message to be displayed after a log note has been stored.
  12026. The auto-repeater uses this.")
  12027. (defun org-add-note ()
  12028. "Add a note to the current entry.
  12029. This is done in the same way as adding a state change note."
  12030. (interactive)
  12031. (org-add-log-setup 'note nil nil 'findpos nil))
  12032. (defvar org-property-end-re)
  12033. (defun org-add-log-setup (&optional purpose state prev-state
  12034. findpos how extra)
  12035. "Set up the post command hook to take a note.
  12036. If this is about to TODO state change, the new state is expected in STATE.
  12037. When FINDPOS is non-nil, find the correct position for the note in
  12038. the current entry. If not, assume that it can be inserted at point.
  12039. HOW is an indicator what kind of note should be created.
  12040. EXTRA is additional text that will be inserted into the notes buffer."
  12041. (let* ((org-log-into-drawer (org-log-into-drawer))
  12042. (drawer (cond ((stringp org-log-into-drawer)
  12043. org-log-into-drawer)
  12044. (org-log-into-drawer "LOGBOOK"))))
  12045. (save-restriction
  12046. (save-excursion
  12047. (when findpos
  12048. (org-back-to-heading t)
  12049. (narrow-to-region (point) (save-excursion
  12050. (outline-next-heading) (point)))
  12051. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12052. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12053. "[^\r\n]*\\)?"))
  12054. (goto-char (match-end 0))
  12055. (cond
  12056. (drawer
  12057. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12058. nil t)
  12059. (progn
  12060. (goto-char (match-end 0))
  12061. (or org-log-states-order-reversed
  12062. (and (re-search-forward org-property-end-re nil t)
  12063. (goto-char (1- (match-beginning 0))))))
  12064. (insert "\n:" drawer ":\n:END:")
  12065. (beginning-of-line 0)
  12066. (org-indent-line)
  12067. (beginning-of-line 2)
  12068. (org-indent-line)
  12069. (end-of-line 0)))
  12070. ((and org-log-state-notes-insert-after-drawers
  12071. (save-excursion
  12072. (forward-line) (looking-at org-drawer-regexp)))
  12073. (forward-line)
  12074. (while (looking-at org-drawer-regexp)
  12075. (goto-char (match-end 0))
  12076. (re-search-forward org-property-end-re (point-max) t)
  12077. (forward-line))
  12078. (forward-line -1)))
  12079. (unless org-log-states-order-reversed
  12080. (and (= (char-after) ?\n) (forward-char 1))
  12081. (org-skip-over-state-notes)
  12082. (skip-chars-backward " \t\n\r")))
  12083. (move-marker org-log-note-marker (point))
  12084. (setq org-log-note-purpose purpose
  12085. org-log-note-state state
  12086. org-log-note-previous-state prev-state
  12087. org-log-note-how how
  12088. org-log-note-extra extra
  12089. org-log-note-effective-time (org-current-effective-time))
  12090. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12091. (defun org-skip-over-state-notes ()
  12092. "Skip past the list of State notes in an entry."
  12093. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12094. (when (ignore-errors (goto-char (org-in-item-p)))
  12095. (let* ((struct (org-list-struct))
  12096. (prevs (org-list-prevs-alist struct)))
  12097. (while (looking-at "[ \t]*- State")
  12098. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12099. (org-list-get-item-end (point) struct)))))))
  12100. (defun org-add-log-note (&optional purpose)
  12101. "Pop up a window for taking a note, and add this note later at point."
  12102. (remove-hook 'post-command-hook 'org-add-log-note)
  12103. (setq org-log-note-window-configuration (current-window-configuration))
  12104. (delete-other-windows)
  12105. (move-marker org-log-note-return-to (point))
  12106. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12107. (goto-char org-log-note-marker)
  12108. (org-switch-to-buffer-other-window "*Org Note*")
  12109. (erase-buffer)
  12110. (if (memq org-log-note-how '(time state))
  12111. (let (current-prefix-arg) (org-store-log-note))
  12112. (let ((org-inhibit-startup t)) (org-mode))
  12113. (insert (format "# Insert note for %s.
  12114. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12115. (cond
  12116. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12117. ((eq org-log-note-purpose 'done) "closed todo item")
  12118. ((eq org-log-note-purpose 'state)
  12119. (format "state change from \"%s\" to \"%s\""
  12120. (or org-log-note-previous-state "")
  12121. (or org-log-note-state "")))
  12122. ((eq org-log-note-purpose 'reschedule)
  12123. "rescheduling")
  12124. ((eq org-log-note-purpose 'delschedule)
  12125. "no longer scheduled")
  12126. ((eq org-log-note-purpose 'redeadline)
  12127. "changing deadline")
  12128. ((eq org-log-note-purpose 'deldeadline)
  12129. "removing deadline")
  12130. ((eq org-log-note-purpose 'refile)
  12131. "refiling")
  12132. ((eq org-log-note-purpose 'note)
  12133. "this entry")
  12134. (t (error "This should not happen")))))
  12135. (if org-log-note-extra (insert org-log-note-extra))
  12136. (org-set-local 'org-finish-function 'org-store-log-note)
  12137. (run-hooks 'org-log-buffer-setup-hook)))
  12138. (defvar org-note-abort nil) ; dynamically scoped
  12139. (defun org-store-log-note ()
  12140. "Finish taking a log note, and insert it to where it belongs."
  12141. (let ((txt (buffer-string)))
  12142. (kill-buffer (current-buffer))
  12143. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12144. lines ind bul)
  12145. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12146. (setq txt (replace-match "" t t txt)))
  12147. (if (string-match "\\s-+\\'" txt)
  12148. (setq txt (replace-match "" t t txt)))
  12149. (setq lines (org-split-string txt "\n"))
  12150. (when (and note (string-match "\\S-" note))
  12151. (setq note
  12152. (org-replace-escapes
  12153. note
  12154. (list (cons "%u" (user-login-name))
  12155. (cons "%U" user-full-name)
  12156. (cons "%t" (format-time-string
  12157. (org-time-stamp-format 'long 'inactive)
  12158. org-log-note-effective-time))
  12159. (cons "%T" (format-time-string
  12160. (org-time-stamp-format 'long nil)
  12161. org-log-note-effective-time))
  12162. (cons "%d" (format-time-string
  12163. (org-time-stamp-format nil 'inactive)
  12164. org-log-note-effective-time))
  12165. (cons "%D" (format-time-string
  12166. (org-time-stamp-format nil nil)
  12167. org-log-note-effective-time))
  12168. (cons "%s" (if org-log-note-state
  12169. (concat "\"" org-log-note-state "\"")
  12170. ""))
  12171. (cons "%S" (if org-log-note-previous-state
  12172. (concat "\"" org-log-note-previous-state "\"")
  12173. "\"\"")))))
  12174. (if lines (setq note (concat note " \\\\")))
  12175. (push note lines))
  12176. (when (or current-prefix-arg org-note-abort)
  12177. (when org-log-into-drawer
  12178. (org-remove-empty-drawer-at
  12179. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12180. org-log-note-marker))
  12181. (setq lines nil))
  12182. (when lines
  12183. (with-current-buffer (marker-buffer org-log-note-marker)
  12184. (save-excursion
  12185. (goto-char org-log-note-marker)
  12186. (move-marker org-log-note-marker nil)
  12187. (end-of-line 1)
  12188. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12189. (setq ind (save-excursion
  12190. (if (ignore-errors (goto-char (org-in-item-p)))
  12191. (let ((struct (org-list-struct)))
  12192. (org-list-get-ind
  12193. (org-list-get-top-point struct) struct))
  12194. (skip-chars-backward " \r\t\n")
  12195. (cond
  12196. ((and (org-at-heading-p)
  12197. org-adapt-indentation)
  12198. (1+ (org-current-level)))
  12199. ((org-at-heading-p) 0)
  12200. (t (org-get-indentation))))))
  12201. (setq bul (org-list-bullet-string "-"))
  12202. (org-indent-line-to ind)
  12203. (insert bul (pop lines))
  12204. (let ((ind-body (+ (length bul) ind)))
  12205. (while lines
  12206. (insert "\n")
  12207. (org-indent-line-to ind-body)
  12208. (insert (pop lines))))
  12209. (message "Note stored")
  12210. (org-back-to-heading t)
  12211. (org-cycle-hide-drawers 'children))
  12212. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12213. ;; from within `org-add-log-note' because `buffer-undo-list'
  12214. ;; is then modified outside of `org-with-remote-undo'.
  12215. (when (eq this-command 'org-agenda-todo)
  12216. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12217. ;; Don't add undo information when called from `org-agenda-todo'
  12218. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12219. (set-window-configuration org-log-note-window-configuration)
  12220. (with-current-buffer (marker-buffer org-log-note-return-to)
  12221. (goto-char org-log-note-return-to))
  12222. (move-marker org-log-note-return-to nil)
  12223. (and org-log-post-message (message "%s" org-log-post-message))))
  12224. (defun org-remove-empty-drawer-at (drawer pos)
  12225. "Remove an empty drawer DRAWER at position POS.
  12226. POS may also be a marker."
  12227. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12228. (save-excursion
  12229. (save-restriction
  12230. (widen)
  12231. (goto-char pos)
  12232. (if (org-in-regexp
  12233. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12234. (replace-match ""))))))
  12235. (defvar org-ts-type nil)
  12236. (defun org-sparse-tree (&optional arg type)
  12237. "Create a sparse tree, prompt for the details.
  12238. This command can create sparse trees. You first need to select the type
  12239. of match used to create the tree:
  12240. t Show all TODO entries.
  12241. T Show entries with a specific TODO keyword.
  12242. m Show entries selected by a tags/property match.
  12243. p Enter a property name and its value (both with completion on existing
  12244. names/values) and show entries with that property.
  12245. r Show entries matching a regular expression (`/' can be used as well).
  12246. b Show deadlines and scheduled items before a date.
  12247. a Show deadlines and scheduled items after a date.
  12248. d Show deadlines due within `org-deadline-warning-days'.
  12249. D Show deadlines and scheduled items between a date range."
  12250. (interactive "P")
  12251. (let (ans kwd value ts-type)
  12252. (setq type (or type org-sparse-tree-default-date-type))
  12253. (setq org-ts-type type)
  12254. (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"
  12255. (cond ((eq type 'all) "all timestamps")
  12256. ((eq type 'scheduled) "only scheduled")
  12257. ((eq type 'deadline) "only deadline")
  12258. ((eq type 'active) "only active timestamps")
  12259. ((eq type 'inactive) "only inactive timestamps")
  12260. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12261. ((eq type 'closed) "with a closed time-stamp")
  12262. (t "scheduled/deadline")))
  12263. (setq ans (read-char-exclusive))
  12264. (cond
  12265. ((equal ans ?c)
  12266. (org-sparse-tree
  12267. arg (cadr (member type '(scheduled-or-deadline
  12268. all scheduled deadline active inactive closed)))))
  12269. ((equal ans ?d)
  12270. (call-interactively 'org-check-deadlines))
  12271. ((equal ans ?b)
  12272. (call-interactively 'org-check-before-date))
  12273. ((equal ans ?a)
  12274. (call-interactively 'org-check-after-date))
  12275. ((equal ans ?D)
  12276. (call-interactively 'org-check-dates-range))
  12277. ((equal ans ?t)
  12278. (call-interactively 'org-show-todo-tree))
  12279. ((equal ans ?T)
  12280. (org-show-todo-tree '(4)))
  12281. ((member ans '(?T ?m))
  12282. (call-interactively 'org-match-sparse-tree))
  12283. ((member ans '(?p ?P))
  12284. (setq kwd (org-icompleting-read "Property: "
  12285. (mapcar 'list (org-buffer-property-keys))))
  12286. (setq value (org-icompleting-read "Value: "
  12287. (mapcar 'list (org-property-values kwd))))
  12288. (unless (string-match "\\`{.*}\\'" value)
  12289. (setq value (concat "\"" value "\"")))
  12290. (org-match-sparse-tree arg (concat kwd "=" value)))
  12291. ((member ans '(?r ?R ?/))
  12292. (call-interactively 'org-occur))
  12293. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12294. (defvar org-occur-highlights nil
  12295. "List of overlays used for occur matches.")
  12296. (make-variable-buffer-local 'org-occur-highlights)
  12297. (defvar org-occur-parameters nil
  12298. "Parameters of the active org-occur calls.
  12299. This is a list, each call to org-occur pushes as cons cell,
  12300. containing the regular expression and the callback, onto the list.
  12301. The list can contain several entries if `org-occur' has been called
  12302. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12303. will only contain one set of parameters. When the highlights are
  12304. removed (for example with `C-c C-c', or with the next edit (depending
  12305. on `org-remove-highlights-with-change'), this variable is emptied
  12306. as well.")
  12307. (make-variable-buffer-local 'org-occur-parameters)
  12308. (defun org-occur (regexp &optional keep-previous callback)
  12309. "Make a compact tree which shows all matches of REGEXP.
  12310. The tree will show the lines where the regexp matches, and all higher
  12311. headlines above the match. It will also show the heading after the match,
  12312. to make sure editing the matching entry is easy.
  12313. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12314. call to `org-occur' will be kept, to allow stacking of calls to this
  12315. command.
  12316. If CALLBACK is non-nil, it is a function which is called to confirm
  12317. that the match should indeed be shown."
  12318. (interactive "sRegexp: \nP")
  12319. (when (equal regexp "")
  12320. (user-error "Regexp cannot be empty"))
  12321. (unless keep-previous
  12322. (org-remove-occur-highlights nil nil t))
  12323. (push (cons regexp callback) org-occur-parameters)
  12324. (let ((cnt 0))
  12325. (save-excursion
  12326. (goto-char (point-min))
  12327. (if (or (not keep-previous) ; do not want to keep
  12328. (not org-occur-highlights)) ; no previous matches
  12329. ;; hide everything
  12330. (org-overview))
  12331. (while (re-search-forward regexp nil t)
  12332. (when (or (not callback)
  12333. (save-match-data (funcall callback)))
  12334. (setq cnt (1+ cnt))
  12335. (when org-highlight-sparse-tree-matches
  12336. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12337. (org-show-context 'occur-tree))))
  12338. (when org-remove-highlights-with-change
  12339. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12340. nil 'local))
  12341. (unless org-sparse-tree-open-archived-trees
  12342. (org-hide-archived-subtrees (point-min) (point-max)))
  12343. (run-hooks 'org-occur-hook)
  12344. (if (org-called-interactively-p 'interactive)
  12345. (message "%d match(es) for regexp %s" cnt regexp))
  12346. cnt))
  12347. (defun org-occur-next-match (&optional n reset)
  12348. "Function for `next-error-function' to find sparse tree matches.
  12349. N is the number of matches to move, when negative move backwards.
  12350. RESET is entirely ignored - this function always goes back to the
  12351. starting point when no match is found."
  12352. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12353. (search-func (if (< n 0)
  12354. 'previous-single-char-property-change
  12355. 'next-single-char-property-change))
  12356. (n (abs n))
  12357. (pos (point))
  12358. p1)
  12359. (catch 'exit
  12360. (while (setq p1 (funcall search-func (point) 'org-type))
  12361. (when (equal p1 limit)
  12362. (goto-char pos)
  12363. (error "No more matches"))
  12364. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12365. (setq n (1- n))
  12366. (when (= n 0)
  12367. (goto-char p1)
  12368. (throw 'exit (point))))
  12369. (goto-char p1))
  12370. (goto-char p1)
  12371. (error "No more matches"))))
  12372. (defun org-show-context (&optional key)
  12373. "Make sure point and context are visible.
  12374. How much context is shown depends upon the variables
  12375. `org-show-hierarchy-above', `org-show-following-heading',
  12376. `org-show-entry-below' and `org-show-siblings'."
  12377. (let ((heading-p (org-at-heading-p t))
  12378. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12379. (following-p (org-get-alist-option org-show-following-heading key))
  12380. (entry-p (org-get-alist-option org-show-entry-below key))
  12381. (siblings-p (org-get-alist-option org-show-siblings key)))
  12382. ;; Show heading or entry text
  12383. (if (and heading-p (not entry-p))
  12384. (org-flag-heading nil) ; only show the heading
  12385. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12386. (org-show-hidden-entry))) ; show entire entry
  12387. (when following-p
  12388. ;; Show next sibling, or heading below text
  12389. (save-excursion
  12390. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12391. (org-flag-heading nil))))
  12392. (when siblings-p (org-show-siblings))
  12393. (when hierarchy-p
  12394. ;; show all higher headings, possibly with siblings
  12395. (save-excursion
  12396. (while (and (condition-case nil
  12397. (progn (org-up-heading-all 1) t)
  12398. (error nil))
  12399. (not (bobp)))
  12400. (org-flag-heading nil)
  12401. (when siblings-p (org-show-siblings)))))
  12402. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12403. (defvar org-reveal-start-hook nil
  12404. "Hook run before revealing a location.")
  12405. (defun org-reveal (&optional siblings)
  12406. "Show current entry, hierarchy above it, and the following headline.
  12407. This can be used to show a consistent set of context around locations
  12408. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12409. not t for the search context.
  12410. With optional argument SIBLINGS, on each level of the hierarchy all
  12411. siblings are shown. This repairs the tree structure to what it would
  12412. look like when opened with hierarchical calls to `org-cycle'.
  12413. With double optional argument \\[universal-argument] \\[universal-argument], \
  12414. go to the parent and show the
  12415. entire tree."
  12416. (interactive "P")
  12417. (run-hooks 'org-reveal-start-hook)
  12418. (let ((org-show-hierarchy-above t)
  12419. (org-show-following-heading t)
  12420. (org-show-siblings (if siblings t org-show-siblings)))
  12421. (org-show-context nil))
  12422. (when (equal siblings '(16))
  12423. (save-excursion
  12424. (when (org-up-heading-safe)
  12425. (org-show-subtree)
  12426. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12427. (defun org-highlight-new-match (beg end)
  12428. "Highlight from BEG to END and mark the highlight is an occur headline."
  12429. (let ((ov (make-overlay beg end)))
  12430. (overlay-put ov 'face 'secondary-selection)
  12431. (overlay-put ov 'org-type 'org-occur)
  12432. (push ov org-occur-highlights)))
  12433. (defun org-remove-occur-highlights (&optional beg end noremove)
  12434. "Remove the occur highlights from the buffer.
  12435. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12436. from the `before-change-functions' in the current buffer."
  12437. (interactive)
  12438. (unless org-inhibit-highlight-removal
  12439. (mapc 'delete-overlay org-occur-highlights)
  12440. (setq org-occur-highlights nil)
  12441. (setq org-occur-parameters nil)
  12442. (unless noremove
  12443. (remove-hook 'before-change-functions
  12444. 'org-remove-occur-highlights 'local))))
  12445. ;;;; Priorities
  12446. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12447. "Regular expression matching the priority indicator.")
  12448. (defvar org-remove-priority-next-time nil)
  12449. (defun org-priority-up ()
  12450. "Increase the priority of the current item."
  12451. (interactive)
  12452. (org-priority 'up))
  12453. (defun org-priority-down ()
  12454. "Decrease the priority of the current item."
  12455. (interactive)
  12456. (org-priority 'down))
  12457. (defun org-priority (&optional action show)
  12458. "Change the priority of an item.
  12459. ACTION can be `set', `up', `down', or a character."
  12460. (interactive "P")
  12461. (if (equal action '(4))
  12462. (org-show-priority)
  12463. (unless org-enable-priority-commands
  12464. (user-error "Priority commands are disabled"))
  12465. (setq action (or action 'set))
  12466. (let (current new news have remove)
  12467. (save-excursion
  12468. (org-back-to-heading t)
  12469. (if (looking-at org-priority-regexp)
  12470. (setq current (string-to-char (match-string 2))
  12471. have t))
  12472. (cond
  12473. ((eq action 'remove)
  12474. (setq remove t new ?\ ))
  12475. ((or (eq action 'set)
  12476. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12477. (if (not (eq action 'set))
  12478. (setq new action)
  12479. (message "Priority %c-%c, SPC to remove: "
  12480. org-highest-priority org-lowest-priority)
  12481. (save-match-data
  12482. (setq new (read-char-exclusive))))
  12483. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12484. (= (upcase org-lowest-priority) org-lowest-priority))
  12485. (setq new (upcase new)))
  12486. (cond ((equal new ?\ ) (setq remove t))
  12487. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12488. (user-error "Priority must be between `%c' and `%c'"
  12489. org-highest-priority org-lowest-priority))))
  12490. ((eq action 'up)
  12491. (setq new (if have
  12492. (1- current) ; normal cycling
  12493. ;; last priority was empty
  12494. (if (eq last-command this-command)
  12495. org-lowest-priority ; wrap around empty to lowest
  12496. ;; default
  12497. (if org-priority-start-cycle-with-default
  12498. org-default-priority
  12499. (1- org-default-priority))))))
  12500. ((eq action 'down)
  12501. (setq new (if have
  12502. (1+ current) ; normal cycling
  12503. ;; last priority was empty
  12504. (if (eq last-command this-command)
  12505. org-highest-priority ; wrap around empty to highest
  12506. ;; default
  12507. (if org-priority-start-cycle-with-default
  12508. org-default-priority
  12509. (1+ org-default-priority))))))
  12510. (t (user-error "Invalid action")))
  12511. (if (or (< (upcase new) org-highest-priority)
  12512. (> (upcase new) org-lowest-priority))
  12513. (if (and (memq action '(up down))
  12514. (not have) (not (eq last-command this-command)))
  12515. ;; `new' is from default priority
  12516. (error
  12517. "The default can not be set, see `org-default-priority' why")
  12518. ;; normal cycling: `new' is beyond highest/lowest priority
  12519. ;; and is wrapped around to the empty priority
  12520. (setq remove t)))
  12521. (setq news (format "%c" new))
  12522. (if have
  12523. (if remove
  12524. (replace-match "" t t nil 1)
  12525. (replace-match news t t nil 2))
  12526. (if remove
  12527. (user-error "No priority cookie found in line")
  12528. (let ((case-fold-search nil))
  12529. (looking-at org-todo-line-regexp))
  12530. (if (match-end 2)
  12531. (progn
  12532. (goto-char (match-end 2))
  12533. (insert " [#" news "]"))
  12534. (goto-char (match-beginning 3))
  12535. (insert "[#" news "] "))))
  12536. (org-preserve-lc (org-set-tags nil 'align)))
  12537. (if remove
  12538. (message "Priority removed")
  12539. (message "Priority of current item set to %s" news)))))
  12540. (defun org-show-priority ()
  12541. "Show the priority of the current item.
  12542. This priority is composed of the main priority given with the [#A] cookies,
  12543. and by additional input from the age of a schedules or deadline entry."
  12544. (interactive)
  12545. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12546. (org-get-at-bol 'priority)
  12547. (save-excursion
  12548. (save-match-data
  12549. (beginning-of-line)
  12550. (and (looking-at org-heading-regexp)
  12551. (org-get-priority (match-string 0))))))))
  12552. (message "Priority is %d" (if pri pri -1000))))
  12553. (defun org-get-priority (s)
  12554. "Find priority cookie and return priority."
  12555. (save-match-data
  12556. (if (functionp org-get-priority-function)
  12557. (funcall org-get-priority-function)
  12558. (if (not (string-match org-priority-regexp s))
  12559. (* 1000 (- org-lowest-priority org-default-priority))
  12560. (* 1000 (- org-lowest-priority
  12561. (string-to-char (match-string 2 s))))))))
  12562. ;;;; Tags
  12563. (defvar org-agenda-archives-mode)
  12564. (defvar org-map-continue-from nil
  12565. "Position from where mapping should continue.
  12566. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12567. (defvar org-scanner-tags nil
  12568. "The current tag list while the tags scanner is running.")
  12569. (defvar org-trust-scanner-tags nil
  12570. "Should `org-get-tags-at' use the tags for the scanner.
  12571. This is for internal dynamical scoping only.
  12572. When this is non-nil, the function `org-get-tags-at' will return the value
  12573. of `org-scanner-tags' instead of building the list by itself. This
  12574. can lead to large speed-ups when the tags scanner is used in a file with
  12575. many entries, and when the list of tags is retrieved, for example to
  12576. obtain a list of properties. Building the tags list for each entry in such
  12577. a file becomes an N^2 operation - but with this variable set, it scales
  12578. as N.")
  12579. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12580. "Sca headline tags with inheritance and produce output ACTION.
  12581. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12582. or `agenda' to produce an entry list for an agenda view. It can also be
  12583. a Lisp form or a function that should be called at each matched headline, in
  12584. this case the return value is a list of all return values from these calls.
  12585. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12586. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12587. only lines with a not-done TODO keyword are included in the output.
  12588. This should be the same variable that was scoped into
  12589. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12590. START-LEVEL can be a string with asterisks, reducing the scope to
  12591. headlines matching this string."
  12592. (require 'org-agenda)
  12593. (let* ((re (concat "^"
  12594. (if start-level
  12595. ;; Get the correct level to match
  12596. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12597. org-outline-regexp)
  12598. " *\\(\\<\\("
  12599. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12600. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12601. (props (list 'face 'default
  12602. 'done-face 'org-agenda-done
  12603. 'undone-face 'default
  12604. 'mouse-face 'highlight
  12605. 'org-not-done-regexp org-not-done-regexp
  12606. 'org-todo-regexp org-todo-regexp
  12607. 'org-complex-heading-regexp org-complex-heading-regexp
  12608. 'help-echo
  12609. (format "mouse-2 or RET jump to org file %s"
  12610. (abbreviate-file-name
  12611. (or (buffer-file-name (buffer-base-buffer))
  12612. (buffer-name (buffer-base-buffer)))))))
  12613. (org-map-continue-from nil)
  12614. lspos tags tags-list
  12615. (tags-alist (list (cons 0 org-file-tags)))
  12616. (llast 0) rtn rtn1 level category i txt
  12617. todo marker entry priority)
  12618. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12619. (setq action (list 'lambda nil action)))
  12620. (save-excursion
  12621. (goto-char (point-min))
  12622. (when (eq action 'sparse-tree)
  12623. (org-overview)
  12624. (org-remove-occur-highlights))
  12625. (while (let (case-fold-search)
  12626. (re-search-forward re nil t))
  12627. (setq org-map-continue-from nil)
  12628. (catch :skip
  12629. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12630. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12631. (goto-char (setq lspos (match-beginning 0)))
  12632. (setq level (org-reduced-level (org-outline-level))
  12633. category (org-get-category))
  12634. (setq i llast llast level)
  12635. ;; remove tag lists from same and sublevels
  12636. (while (>= i level)
  12637. (when (setq entry (assoc i tags-alist))
  12638. (setq tags-alist (delete entry tags-alist)))
  12639. (setq i (1- i)))
  12640. ;; add the next tags
  12641. (when tags
  12642. (setq tags (org-split-string tags ":")
  12643. tags-alist
  12644. (cons (cons level tags) tags-alist)))
  12645. ;; compile tags for current headline
  12646. (setq tags-list
  12647. (if org-use-tag-inheritance
  12648. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12649. tags)
  12650. org-scanner-tags tags-list)
  12651. (when org-use-tag-inheritance
  12652. (setcdr (car tags-alist)
  12653. (mapcar (lambda (x)
  12654. (setq x (copy-sequence x))
  12655. (org-add-prop-inherited x))
  12656. (cdar tags-alist))))
  12657. (when (and tags org-use-tag-inheritance
  12658. (or (not (eq t org-use-tag-inheritance))
  12659. org-tags-exclude-from-inheritance))
  12660. ;; selective inheritance, remove uninherited ones
  12661. (setcdr (car tags-alist)
  12662. (org-remove-uninherited-tags (cdar tags-alist))))
  12663. (when (and
  12664. ;; eval matcher only when the todo condition is OK
  12665. (and (or (not todo-only) (member todo org-not-done-keywords))
  12666. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12667. (eval matcher)))
  12668. ;; Call the skipper, but return t if it does not skip,
  12669. ;; so that the `and' form continues evaluating
  12670. (progn
  12671. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12672. t)
  12673. ;; Check if timestamps are deselecting this entry
  12674. (or (not todo-only)
  12675. (and (member todo org-not-done-keywords)
  12676. (or (not org-agenda-tags-todo-honor-ignore-options)
  12677. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12678. ;; select this headline
  12679. (cond
  12680. ((eq action 'sparse-tree)
  12681. (and org-highlight-sparse-tree-matches
  12682. (org-get-heading) (match-end 0)
  12683. (org-highlight-new-match
  12684. (match-beginning 1) (match-end 1)))
  12685. (org-show-context 'tags-tree))
  12686. ((eq action 'agenda)
  12687. (setq txt (org-agenda-format-item
  12688. ""
  12689. (concat
  12690. (if (eq org-tags-match-list-sublevels 'indented)
  12691. (make-string (1- level) ?.) "")
  12692. (org-get-heading))
  12693. level category
  12694. tags-list)
  12695. priority (org-get-priority txt))
  12696. (goto-char lspos)
  12697. (setq marker (org-agenda-new-marker))
  12698. (org-add-props txt props
  12699. 'org-marker marker 'org-hd-marker marker 'org-category category
  12700. 'todo-state todo
  12701. 'priority priority 'type "tagsmatch")
  12702. (push txt rtn))
  12703. ((functionp action)
  12704. (setq org-map-continue-from nil)
  12705. (save-excursion
  12706. (setq rtn1 (funcall action))
  12707. (push rtn1 rtn)))
  12708. (t (user-error "Invalid action")))
  12709. ;; if we are to skip sublevels, jump to end of subtree
  12710. (unless org-tags-match-list-sublevels
  12711. (org-end-of-subtree t)
  12712. (backward-char 1))))
  12713. ;; Get the correct position from where to continue
  12714. (if org-map-continue-from
  12715. (goto-char org-map-continue-from)
  12716. (and (= (point) lspos) (end-of-line 1)))))
  12717. (when (and (eq action 'sparse-tree)
  12718. (not org-sparse-tree-open-archived-trees))
  12719. (org-hide-archived-subtrees (point-min) (point-max)))
  12720. (nreverse rtn)))
  12721. (defun org-remove-uninherited-tags (tags)
  12722. "Remove all tags that are not inherited from the list TAGS."
  12723. (cond
  12724. ((eq org-use-tag-inheritance t)
  12725. (if org-tags-exclude-from-inheritance
  12726. (org-delete-all org-tags-exclude-from-inheritance tags)
  12727. tags))
  12728. ((not org-use-tag-inheritance) nil)
  12729. ((stringp org-use-tag-inheritance)
  12730. (delq nil (mapcar
  12731. (lambda (x)
  12732. (if (and (string-match org-use-tag-inheritance x)
  12733. (not (member x org-tags-exclude-from-inheritance)))
  12734. x nil))
  12735. tags)))
  12736. ((listp org-use-tag-inheritance)
  12737. (delq nil (mapcar
  12738. (lambda (x)
  12739. (if (member x org-use-tag-inheritance) x nil))
  12740. tags)))))
  12741. (defun org-match-sparse-tree (&optional todo-only match)
  12742. "Create a sparse tree according to tags string MATCH.
  12743. MATCH can contain positive and negative selection of tags, like
  12744. \"+WORK+URGENT-WITHBOSS\".
  12745. If optional argument TODO-ONLY is non-nil, only select lines that are
  12746. also TODO lines."
  12747. (interactive "P")
  12748. (org-agenda-prepare-buffers (list (current-buffer)))
  12749. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12750. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12751. (defvar org-cached-props nil)
  12752. (defun org-cached-entry-get (pom property)
  12753. (if (or (eq t org-use-property-inheritance)
  12754. (and (stringp org-use-property-inheritance)
  12755. (string-match org-use-property-inheritance property))
  12756. (and (listp org-use-property-inheritance)
  12757. (member property org-use-property-inheritance)))
  12758. ;; Caching is not possible, check it directly
  12759. (org-entry-get pom property 'inherit)
  12760. ;; Get all properties, so that we can do complicated checks easily
  12761. (cdr (assoc property (or org-cached-props
  12762. (setq org-cached-props
  12763. (org-entry-properties pom)))))))
  12764. (defun org-global-tags-completion-table (&optional files)
  12765. "Return the list of all tags in all agenda buffer/files.
  12766. Optional FILES argument is a list of files which can be used
  12767. instead of the agenda files."
  12768. (save-excursion
  12769. (org-uniquify
  12770. (delq nil
  12771. (apply 'append
  12772. (mapcar
  12773. (lambda (file)
  12774. (set-buffer (find-file-noselect file))
  12775. (append (org-get-buffer-tags)
  12776. (mapcar (lambda (x) (if (stringp (car-safe x))
  12777. (list (car-safe x)) nil))
  12778. org-tag-alist)))
  12779. (if (and files (car files))
  12780. files
  12781. (org-agenda-files))))))))
  12782. (defun org-make-tags-matcher (match)
  12783. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12784. The variable `todo-only' is scoped dynamically into this function.
  12785. It will be set to t if the matcher restricts matching to TODO entries,
  12786. otherwise will not be touched.
  12787. Returns a cons of the selection string MATCH and the constructed
  12788. lisp form implementing the matcher. The matcher is to be evaluated
  12789. at an Org entry, with point on the headline, and returns t if the
  12790. entry matches the selection string MATCH. The returned lisp form
  12791. references two variables with information about the entry, which
  12792. must be bound around the form's evaluation: todo, the TODO keyword
  12793. at the entry (or nil of none); and tags-list, the list of all tags
  12794. at the entry including inherited ones. Additionally, the category
  12795. of the entry (if any) must be specified as the text property
  12796. 'org-category on the headline.
  12797. See also `org-scan-tags'.
  12798. "
  12799. (declare (special todo-only))
  12800. (unless (boundp 'todo-only)
  12801. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12802. (unless match
  12803. ;; Get a new match request, with completion against the global
  12804. ;; tags table and the local tags in current buffer
  12805. (let ((org-last-tags-completion-table
  12806. (org-uniquify
  12807. (delq nil (append (org-get-buffer-tags)
  12808. (org-global-tags-completion-table))))))
  12809. (setq match (org-completing-read-no-i
  12810. "Match: " 'org-tags-completion-function nil nil nil
  12811. 'org-tags-history))))
  12812. ;; Parse the string and create a lisp form
  12813. (let ((match0 match)
  12814. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12815. minus tag mm
  12816. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12817. orterms term orlist re-p str-p level-p level-op time-p
  12818. prop-p pn pv po gv rest (start 0) (ss 0))
  12819. ;; Expand group tags
  12820. (setq match (org-tags-expand match))
  12821. ;; Check if there is a TODO part of this match, which would be the
  12822. ;; part after a "/". TO make sure that this slash is not part of
  12823. ;; a property value to be matched against, we also check that there
  12824. ;; is no " after that slash.
  12825. ;; First, find the last slash
  12826. (while (string-match "/+" match ss)
  12827. (setq start (match-beginning 0) ss (match-end 0)))
  12828. (if (and (string-match "/+" match start)
  12829. (not (save-match-data (string-match "\"" match start))))
  12830. ;; match contains also a todo-matching request
  12831. (progn
  12832. (setq tagsmatch (substring match 0 (match-beginning 0))
  12833. todomatch (substring match (match-end 0)))
  12834. (if (string-match "^!" todomatch)
  12835. (setq todo-only t todomatch (substring todomatch 1)))
  12836. (if (string-match "^\\s-*$" todomatch)
  12837. (setq todomatch nil)))
  12838. ;; only matching tags
  12839. (setq tagsmatch match todomatch nil))
  12840. ;; Make the tags matcher
  12841. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12842. (setq tagsmatcher t)
  12843. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12844. (while (setq term (pop orterms))
  12845. (while (and (equal (substring term -1) "\\") orterms)
  12846. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12847. (while (string-match re term)
  12848. (setq rest (substring term (match-end 0))
  12849. minus (and (match-end 1)
  12850. (equal (match-string 1 term) "-"))
  12851. tag (save-match-data (replace-regexp-in-string
  12852. "\\\\-" "-"
  12853. (match-string 2 term)))
  12854. re-p (equal (string-to-char tag) ?{)
  12855. level-p (match-end 4)
  12856. prop-p (match-end 5)
  12857. mm (cond
  12858. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12859. (level-p
  12860. (setq level-op (org-op-to-function (match-string 3 term)))
  12861. `(,level-op level ,(string-to-number
  12862. (match-string 4 term))))
  12863. (prop-p
  12864. (setq pn (match-string 5 term)
  12865. po (match-string 6 term)
  12866. pv (match-string 7 term)
  12867. re-p (equal (string-to-char pv) ?{)
  12868. str-p (equal (string-to-char pv) ?\")
  12869. time-p (save-match-data
  12870. (string-match "^\"[[<].*[]>]\"$" pv))
  12871. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12872. (if time-p (setq pv (org-matcher-time pv)))
  12873. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12874. (cond
  12875. ((equal pn "CATEGORY")
  12876. (setq gv '(get-text-property (point) 'org-category)))
  12877. ((equal pn "TODO")
  12878. (setq gv 'todo))
  12879. (t
  12880. (setq gv `(org-cached-entry-get nil ,pn))))
  12881. (if re-p
  12882. (if (eq po 'org<>)
  12883. `(not (string-match ,pv (or ,gv "")))
  12884. `(string-match ,pv (or ,gv "")))
  12885. (if str-p
  12886. `(,po (or ,gv "") ,pv)
  12887. `(,po (string-to-number (or ,gv ""))
  12888. ,(string-to-number pv) ))))
  12889. (t `(member ,tag tags-list)))
  12890. mm (if minus (list 'not mm) mm)
  12891. term rest)
  12892. (push mm tagsmatcher))
  12893. (push (if (> (length tagsmatcher) 1)
  12894. (cons 'and tagsmatcher)
  12895. (car tagsmatcher))
  12896. orlist)
  12897. (setq tagsmatcher nil))
  12898. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12899. (setq tagsmatcher
  12900. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12901. ;; Make the todo matcher
  12902. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12903. (setq todomatcher t)
  12904. (setq orterms (org-split-string todomatch "|") orlist nil)
  12905. (while (setq term (pop orterms))
  12906. (while (string-match re term)
  12907. (setq minus (and (match-end 1)
  12908. (equal (match-string 1 term) "-"))
  12909. kwd (match-string 2 term)
  12910. re-p (equal (string-to-char kwd) ?{)
  12911. term (substring term (match-end 0))
  12912. mm (if re-p
  12913. `(string-match ,(substring kwd 1 -1) todo)
  12914. (list 'equal 'todo kwd))
  12915. mm (if minus (list 'not mm) mm))
  12916. (push mm todomatcher))
  12917. (push (if (> (length todomatcher) 1)
  12918. (cons 'and todomatcher)
  12919. (car todomatcher))
  12920. orlist)
  12921. (setq todomatcher nil))
  12922. (setq todomatcher (if (> (length orlist) 1)
  12923. (cons 'or orlist) (car orlist))))
  12924. ;; Return the string and lisp forms of the matcher
  12925. (setq matcher (if todomatcher
  12926. (list 'and tagsmatcher todomatcher)
  12927. tagsmatcher))
  12928. (when todo-only
  12929. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12930. matcher)))
  12931. (cons match0 matcher)))
  12932. (defun org-tags-expand (match &optional single-as-list downcased)
  12933. "Expand group tags in MATCH.
  12934. This replaces every group tag in MATCH with a regexp tag search.
  12935. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12936. will be replaced like this:
  12937. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12938. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12939. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12940. Replacing by a regexp preserves the structure of the match.
  12941. E.g., this expansion
  12942. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12943. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12944. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12945. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12946. assumed to be a single group tag, and the function will return
  12947. the list of tags in this group.
  12948. When DOWNCASE is non-nil, expand downcased TAGS."
  12949. (if org-group-tags
  12950. (let* ((case-fold-search t)
  12951. (stable org-mode-syntax-table)
  12952. (tal (or org-tag-groups-alist-for-agenda
  12953. org-tag-groups-alist))
  12954. (tal (if downcased
  12955. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12956. (tml (mapcar 'car tal))
  12957. (rtnmatch match) rpl)
  12958. ;; @ and _ are allowed as word-components in tags
  12959. (modify-syntax-entry ?@ "w" stable)
  12960. (modify-syntax-entry ?_ "w" stable)
  12961. (while (and tml
  12962. (with-syntax-table stable
  12963. (string-match
  12964. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12965. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12966. (let* ((dir (match-string 1 rtnmatch))
  12967. (tag (match-string 2 rtnmatch))
  12968. (tag (if downcased (downcase tag) tag)))
  12969. (setq tml (delete tag tml))
  12970. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12971. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12972. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12973. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12974. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12975. (if single-as-list
  12976. (or (reverse rpl) (list rtnmatch))
  12977. rtnmatch))
  12978. (if single-as-list (list (if downcased (downcase match) match))
  12979. match)))
  12980. (defun org-op-to-function (op &optional stringp)
  12981. "Turn an operator into the appropriate function."
  12982. (setq op
  12983. (cond
  12984. ((equal op "<" ) '(< string< org-time<))
  12985. ((equal op ">" ) '(> org-string> org-time>))
  12986. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12987. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12988. ((member op '("=" "==")) '(= string= org-time=))
  12989. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12990. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12991. (defun org<> (a b) (not (= a b)))
  12992. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12993. (defun org-string>= (a b) (not (string< a b)))
  12994. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12995. (defun org-string<> (a b) (not (string= a b)))
  12996. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12997. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12998. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12999. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13000. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13001. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13002. (defun org-2ft (s)
  13003. "Convert S to a floating point time.
  13004. If S is already a number, just return it. If it is a string, parse
  13005. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13006. (cond
  13007. ((numberp s) s)
  13008. ((stringp s)
  13009. (condition-case nil
  13010. (float-time (apply 'encode-time (org-parse-time-string s)))
  13011. (error 0.)))
  13012. (t 0.)))
  13013. (defun org-time-today ()
  13014. "Time in seconds today at 0:00.
  13015. Returns the float number of seconds since the beginning of the
  13016. epoch to the beginning of today (00:00)."
  13017. (float-time (apply 'encode-time
  13018. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13019. (defun org-matcher-time (s)
  13020. "Interpret a time comparison value."
  13021. (save-match-data
  13022. (cond
  13023. ((string= s "<now>") (float-time))
  13024. ((string= s "<today>") (org-time-today))
  13025. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13026. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13027. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13028. (+ (org-time-today)
  13029. (* (string-to-number (match-string 1 s))
  13030. (cdr (assoc (match-string 2 s)
  13031. '(("d" . 86400.0) ("w" . 604800.0)
  13032. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13033. (t (org-2ft s)))))
  13034. (defun org-match-any-p (re list)
  13035. "Does re match any element of list?"
  13036. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13037. (delq nil list))
  13038. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13039. (defvar org-tags-overlay (make-overlay 1 1))
  13040. (org-detach-overlay org-tags-overlay)
  13041. (defun org-get-local-tags-at (&optional pos)
  13042. "Get a list of tags defined in the current headline."
  13043. (org-get-tags-at pos 'local))
  13044. (defun org-get-local-tags ()
  13045. "Get a list of tags defined in the current headline."
  13046. (org-get-tags-at nil 'local))
  13047. (defun org-get-tags-at (&optional pos local)
  13048. "Get a list of all headline tags applicable at POS.
  13049. POS defaults to point. If tags are inherited, the list contains
  13050. the targets in the same sequence as the headlines appear, i.e.
  13051. the tags of the current headline come last.
  13052. When LOCAL is non-nil, only return tags from the current headline,
  13053. ignore inherited ones."
  13054. (interactive)
  13055. (if (and org-trust-scanner-tags
  13056. (or (not pos) (equal pos (point)))
  13057. (not local))
  13058. org-scanner-tags
  13059. (let (tags ltags lastpos parent)
  13060. (save-excursion
  13061. (save-restriction
  13062. (widen)
  13063. (goto-char (or pos (point)))
  13064. (save-match-data
  13065. (catch 'done
  13066. (condition-case nil
  13067. (progn
  13068. (org-back-to-heading t)
  13069. (while (not (equal lastpos (point)))
  13070. (setq lastpos (point))
  13071. (when (looking-at
  13072. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13073. (setq ltags (org-split-string
  13074. (org-match-string-no-properties 1) ":"))
  13075. (when parent
  13076. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13077. (setq tags (append
  13078. (if parent
  13079. (org-remove-uninherited-tags ltags)
  13080. ltags)
  13081. tags)))
  13082. (or org-use-tag-inheritance (throw 'done t))
  13083. (if local (throw 'done t))
  13084. (or (org-up-heading-safe) (error nil))
  13085. (setq parent t)))
  13086. (error nil)))))
  13087. (if local
  13088. tags
  13089. (reverse (delete-dups
  13090. (reverse (append
  13091. (org-remove-uninherited-tags
  13092. org-file-tags) tags)))))))))
  13093. (defun org-add-prop-inherited (s)
  13094. (add-text-properties 0 (length s) '(inherited t) s)
  13095. s)
  13096. (defun org-toggle-tag (tag &optional onoff)
  13097. "Toggle the tag TAG for the current line.
  13098. If ONOFF is `on' or `off', don't toggle but set to this state."
  13099. (let (res current)
  13100. (save-excursion
  13101. (org-back-to-heading t)
  13102. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13103. (point-at-eol) t)
  13104. (progn
  13105. (setq current (match-string 1))
  13106. (replace-match ""))
  13107. (setq current ""))
  13108. (setq current (nreverse (org-split-string current ":")))
  13109. (cond
  13110. ((eq onoff 'on)
  13111. (setq res t)
  13112. (or (member tag current) (push tag current)))
  13113. ((eq onoff 'off)
  13114. (or (not (member tag current)) (setq current (delete tag current))))
  13115. (t (if (member tag current)
  13116. (setq current (delete tag current))
  13117. (setq res t)
  13118. (push tag current))))
  13119. (end-of-line 1)
  13120. (if current
  13121. (progn
  13122. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13123. (org-set-tags nil t))
  13124. (delete-horizontal-space))
  13125. (run-hooks 'org-after-tags-change-hook))
  13126. res))
  13127. (defun org-align-tags-here (to-col)
  13128. ;; Assumes that this is a headline
  13129. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13130. (beginning-of-line 1)
  13131. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13132. (< pos (match-beginning 2)))
  13133. (progn
  13134. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13135. (goto-char (match-beginning 1))
  13136. (insert " ")
  13137. (delete-region (point) (1+ (match-beginning 2)))
  13138. (setq ncol (max (current-column)
  13139. (1+ col)
  13140. (if (> to-col 0)
  13141. to-col
  13142. (- (abs to-col) tags-l))))
  13143. (setq p (point))
  13144. (insert (make-string (- ncol (current-column)) ?\ ))
  13145. (setq ncol (current-column))
  13146. (when indent-tabs-mode (tabify p (point-at-eol)))
  13147. (org-move-to-column (min ncol col) t nil t))
  13148. (goto-char pos))))
  13149. (defun org-set-tags-command (&optional arg just-align)
  13150. "Call the set-tags command for the current entry."
  13151. (interactive "P")
  13152. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13153. (org-set-tags arg just-align)
  13154. (save-excursion
  13155. (unless (and (org-region-active-p)
  13156. org-loop-over-headlines-in-active-region)
  13157. (org-back-to-heading t))
  13158. (org-set-tags arg just-align))))
  13159. (defun org-set-tags-to (data)
  13160. "Set the tags of the current entry to DATA, replacing the current tags.
  13161. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13162. If DATA is nil or the empty string, any tags will be removed."
  13163. (interactive "sTags: ")
  13164. (setq data
  13165. (cond
  13166. ((eq data nil) "")
  13167. ((equal data "") "")
  13168. ((stringp data)
  13169. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13170. ":"))
  13171. ((listp data)
  13172. (concat ":" (mapconcat 'identity data ":") ":"))))
  13173. (when data
  13174. (save-excursion
  13175. (org-back-to-heading t)
  13176. (when (looking-at org-complex-heading-regexp)
  13177. (if (match-end 5)
  13178. (progn
  13179. (goto-char (match-beginning 5))
  13180. (insert data)
  13181. (delete-region (point) (point-at-eol))
  13182. (org-set-tags nil 'align))
  13183. (goto-char (point-at-eol))
  13184. (insert " " data)
  13185. (org-set-tags nil 'align)))
  13186. (beginning-of-line 1)
  13187. (if (looking-at ".*?\\([ \t]+\\)$")
  13188. (delete-region (match-beginning 1) (match-end 1))))))
  13189. (defun org-align-all-tags ()
  13190. "Align the tags i all headings."
  13191. (interactive)
  13192. (save-excursion
  13193. (or (ignore-errors (org-back-to-heading t))
  13194. (outline-next-heading))
  13195. (if (org-at-heading-p)
  13196. (org-set-tags t)
  13197. (message "No headings"))))
  13198. (defvar org-indent-indentation-per-level)
  13199. (defun org-set-tags (&optional arg just-align)
  13200. "Set the tags for the current headline.
  13201. With prefix ARG, realign all tags in headings in the current buffer."
  13202. (interactive "P")
  13203. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13204. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13205. 'region-start-level 'region))
  13206. org-loop-over-headlines-in-active-region)
  13207. (org-map-entries
  13208. ;; We don't use ARG and JUST-ALIGN here these args are not
  13209. ;; useful when looping over headlines
  13210. `(org-set-tags)
  13211. org-loop-over-headlines-in-active-region
  13212. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13213. (let* ((re org-outline-regexp-bol)
  13214. (current (unless arg (org-get-tags-string)))
  13215. (col (current-column))
  13216. (org-setting-tags t)
  13217. table current-tags inherited-tags ; computed below when needed
  13218. tags p0 c0 c1 rpl di tc level)
  13219. (if arg
  13220. (save-excursion
  13221. (goto-char (point-min))
  13222. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13223. (while (re-search-forward re nil t)
  13224. (org-set-tags nil t)
  13225. (end-of-line 1)))
  13226. (message "All tags realigned to column %d" org-tags-column))
  13227. (if just-align
  13228. (setq tags current)
  13229. ;; Get a new set of tags from the user
  13230. (save-excursion
  13231. (setq table (append org-tag-persistent-alist
  13232. (or org-tag-alist (org-get-buffer-tags))
  13233. (and
  13234. org-complete-tags-always-offer-all-agenda-tags
  13235. (org-global-tags-completion-table
  13236. (org-agenda-files))))
  13237. org-last-tags-completion-table table
  13238. current-tags (org-split-string current ":")
  13239. inherited-tags (nreverse
  13240. (nthcdr (length current-tags)
  13241. (nreverse (org-get-tags-at))))
  13242. tags
  13243. (if (or (eq t org-use-fast-tag-selection)
  13244. (and org-use-fast-tag-selection
  13245. (delq nil (mapcar 'cdr table))))
  13246. (org-fast-tag-selection
  13247. current-tags inherited-tags table
  13248. (if org-fast-tag-selection-include-todo
  13249. org-todo-key-alist))
  13250. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13251. (org-trim
  13252. (org-icompleting-read "Tags: "
  13253. 'org-tags-completion-function
  13254. nil nil current 'org-tags-history))))))
  13255. (while (string-match "[-+&]+" tags)
  13256. ;; No boolean logic, just a list
  13257. (setq tags (replace-match ":" t t tags))))
  13258. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13259. (if org-tags-sort-function
  13260. (setq tags (mapconcat 'identity
  13261. (sort (org-split-string
  13262. tags (org-re "[^[:alnum:]_@#%]+"))
  13263. org-tags-sort-function) ":")))
  13264. (if (string-match "\\`[\t ]*\\'" tags)
  13265. (setq tags "")
  13266. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13267. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13268. ;; Insert new tags at the correct column
  13269. (beginning-of-line 1)
  13270. (setq level (or (and (looking-at org-outline-regexp)
  13271. (- (match-end 0) (point) 1))
  13272. 1))
  13273. (cond
  13274. ((and (equal current "") (equal tags "")))
  13275. ((re-search-forward
  13276. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13277. (point-at-eol) t)
  13278. (if (equal tags "")
  13279. (setq rpl "")
  13280. (goto-char (match-beginning 0))
  13281. (setq c0 (current-column)
  13282. ;; compute offset for the case of org-indent-mode active
  13283. di (if org-indent-mode
  13284. (* (1- org-indent-indentation-per-level) (1- level))
  13285. 0)
  13286. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13287. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13288. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13289. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13290. (replace-match rpl t t)
  13291. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13292. tags)
  13293. (t (error "Tags alignment failed")))
  13294. (org-move-to-column col)
  13295. (unless just-align
  13296. (run-hooks 'org-after-tags-change-hook))))))
  13297. (defun org-change-tag-in-region (beg end tag off)
  13298. "Add or remove TAG for each entry in the region.
  13299. This works in the agenda, and also in an org-mode buffer."
  13300. (interactive
  13301. (list (region-beginning) (region-end)
  13302. (let ((org-last-tags-completion-table
  13303. (if (derived-mode-p 'org-mode)
  13304. (org-uniquify
  13305. (delq nil (append (org-get-buffer-tags)
  13306. (org-global-tags-completion-table))))
  13307. (org-global-tags-completion-table))))
  13308. (org-icompleting-read
  13309. "Tag: " 'org-tags-completion-function nil nil nil
  13310. 'org-tags-history))
  13311. (progn
  13312. (message "[s]et or [r]emove? ")
  13313. (equal (read-char-exclusive) ?r))))
  13314. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13315. (let ((agendap (equal major-mode 'org-agenda-mode))
  13316. l1 l2 m buf pos newhead (cnt 0))
  13317. (goto-char end)
  13318. (setq l2 (1- (org-current-line)))
  13319. (goto-char beg)
  13320. (setq l1 (org-current-line))
  13321. (loop for l from l1 to l2 do
  13322. (org-goto-line l)
  13323. (setq m (get-text-property (point) 'org-hd-marker))
  13324. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13325. (and agendap m))
  13326. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13327. pos (if agendap m (point)))
  13328. (with-current-buffer buf
  13329. (save-excursion
  13330. (save-restriction
  13331. (goto-char pos)
  13332. (setq cnt (1+ cnt))
  13333. (org-toggle-tag tag (if off 'off 'on))
  13334. (setq newhead (org-get-heading)))))
  13335. (and agendap (org-agenda-change-all-lines newhead m))))
  13336. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13337. (defun org-tags-completion-function (string predicate &optional flag)
  13338. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13339. (confirm (lambda (x) (stringp (car x)))))
  13340. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13341. (setq s1 (match-string 1 string)
  13342. s2 (match-string 2 string))
  13343. (setq s1 "" s2 string))
  13344. (cond
  13345. ((eq flag nil)
  13346. ;; try completion
  13347. (setq rtn (try-completion s2 ctable confirm))
  13348. (if (stringp rtn)
  13349. (setq rtn
  13350. (concat s1 s2 (substring rtn (length s2))
  13351. (if (and org-add-colon-after-tag-completion
  13352. (assoc rtn ctable))
  13353. ":" ""))))
  13354. rtn)
  13355. ((eq flag t)
  13356. ;; all-completions
  13357. (all-completions s2 ctable confirm))
  13358. ((eq flag 'lambda)
  13359. ;; exact match?
  13360. (assoc s2 ctable)))))
  13361. (defun org-fast-tag-insert (kwd tags face &optional end)
  13362. "Insert KDW, and the TAGS, the latter with face FACE.
  13363. Also insert END."
  13364. (insert (format "%-12s" (concat kwd ":"))
  13365. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13366. (or end "")))
  13367. (defun org-fast-tag-show-exit (flag)
  13368. (save-excursion
  13369. (org-goto-line 3)
  13370. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13371. (replace-match ""))
  13372. (when flag
  13373. (end-of-line 1)
  13374. (org-move-to-column (- (window-width) 19) t)
  13375. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13376. (defun org-set-current-tags-overlay (current prefix)
  13377. "Add an overlay to CURRENT tag with PREFIX."
  13378. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13379. (if (featurep 'xemacs)
  13380. (org-overlay-display org-tags-overlay (concat prefix s)
  13381. 'secondary-selection)
  13382. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13383. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13384. (defvar org-last-tag-selection-key nil)
  13385. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13386. "Fast tag selection with single keys.
  13387. CURRENT is the current list of tags in the headline, INHERITED is the
  13388. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13389. possibly with grouping information. TODO-TABLE is a similar table with
  13390. TODO keywords, should these have keys assigned to them.
  13391. If the keys are nil, a-z are automatically assigned.
  13392. Returns the new tags string, or nil to not change the current settings."
  13393. (let* ((fulltable (append table todo-table))
  13394. (maxlen (apply 'max (mapcar
  13395. (lambda (x)
  13396. (if (stringp (car x)) (string-width (car x)) 0))
  13397. fulltable)))
  13398. (buf (current-buffer))
  13399. (expert (eq org-fast-tag-selection-single-key 'expert))
  13400. (buffer-tags nil)
  13401. (fwidth (+ maxlen 3 1 3))
  13402. (ncol (/ (- (window-width) 4) fwidth))
  13403. (i-face 'org-done)
  13404. (c-face 'org-todo)
  13405. tg cnt e c char c1 c2 ntable tbl rtn
  13406. ov-start ov-end ov-prefix
  13407. (exit-after-next org-fast-tag-selection-single-key)
  13408. (done-keywords org-done-keywords)
  13409. groups ingroup)
  13410. (save-excursion
  13411. (beginning-of-line 1)
  13412. (if (looking-at
  13413. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13414. (setq ov-start (match-beginning 1)
  13415. ov-end (match-end 1)
  13416. ov-prefix "")
  13417. (setq ov-start (1- (point-at-eol))
  13418. ov-end (1+ ov-start))
  13419. (skip-chars-forward "^\n\r")
  13420. (setq ov-prefix
  13421. (concat
  13422. (buffer-substring (1- (point)) (point))
  13423. (if (> (current-column) org-tags-column)
  13424. " "
  13425. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13426. (move-overlay org-tags-overlay ov-start ov-end)
  13427. (save-window-excursion
  13428. (if expert
  13429. (set-buffer (get-buffer-create " *Org tags*"))
  13430. (delete-other-windows)
  13431. (split-window-vertically)
  13432. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13433. (erase-buffer)
  13434. (org-set-local 'org-done-keywords done-keywords)
  13435. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13436. (org-fast-tag-insert "Current" current c-face "\n\n")
  13437. (org-fast-tag-show-exit exit-after-next)
  13438. (org-set-current-tags-overlay current ov-prefix)
  13439. (setq tbl fulltable char ?a cnt 0)
  13440. (while (setq e (pop tbl))
  13441. (cond
  13442. ((equal (car e) :startgroup)
  13443. (push '() groups) (setq ingroup t)
  13444. (when (not (= cnt 0))
  13445. (setq cnt 0)
  13446. (insert "\n"))
  13447. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13448. ((equal (car e) :endgroup)
  13449. (setq ingroup nil cnt 0)
  13450. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13451. ((equal e '(:newline))
  13452. (when (not (= cnt 0))
  13453. (setq cnt 0)
  13454. (insert "\n")
  13455. (setq e (car tbl))
  13456. (while (equal (car tbl) '(:newline))
  13457. (insert "\n")
  13458. (setq tbl (cdr tbl)))))
  13459. ((equal e '(:grouptags)) nil)
  13460. (t
  13461. (setq tg (copy-sequence (car e)) c2 nil)
  13462. (if (cdr e)
  13463. (setq c (cdr e))
  13464. ;; automatically assign a character.
  13465. (setq c1 (string-to-char
  13466. (downcase (substring
  13467. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13468. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13469. (while (or (rassoc char ntable) (rassoc char table))
  13470. (setq char (1+ char)))
  13471. (setq c2 c1))
  13472. (setq c (or c2 char)))
  13473. (if ingroup (push tg (car groups)))
  13474. (setq tg (org-add-props tg nil 'face
  13475. (cond
  13476. ((not (assoc tg table))
  13477. (org-get-todo-face tg))
  13478. ((member tg current) c-face)
  13479. ((member tg inherited) i-face))))
  13480. (if (equal (caar tbl) :grouptags)
  13481. (org-add-props tg nil 'face 'org-tag-group))
  13482. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13483. (insert "[" c "] " tg (make-string
  13484. (- fwidth 4 (length tg)) ?\ ))
  13485. (push (cons tg c) ntable)
  13486. (when (= (setq cnt (1+ cnt)) ncol)
  13487. (insert "\n")
  13488. (if ingroup (insert " "))
  13489. (setq cnt 0)))))
  13490. (setq ntable (nreverse ntable))
  13491. (insert "\n")
  13492. (goto-char (point-min))
  13493. (if (not expert) (org-fit-window-to-buffer))
  13494. (setq rtn
  13495. (catch 'exit
  13496. (while t
  13497. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13498. (if (not groups) "no " "")
  13499. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13500. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13501. (setq org-last-tag-selection-key c)
  13502. (cond
  13503. ((= c ?\r) (throw 'exit t))
  13504. ((= c ?!)
  13505. (setq groups (not groups))
  13506. (goto-char (point-min))
  13507. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13508. ((= c ?\C-c)
  13509. (if (not expert)
  13510. (org-fast-tag-show-exit
  13511. (setq exit-after-next (not exit-after-next)))
  13512. (setq expert nil)
  13513. (delete-other-windows)
  13514. (set-window-buffer (split-window-vertically) " *Org tags*")
  13515. (org-switch-to-buffer-other-window " *Org tags*")
  13516. (org-fit-window-to-buffer)))
  13517. ((or (= c ?\C-g)
  13518. (and (= c ?q) (not (rassoc c ntable))))
  13519. (org-detach-overlay org-tags-overlay)
  13520. (setq quit-flag t))
  13521. ((= c ?\ )
  13522. (setq current nil)
  13523. (if exit-after-next (setq exit-after-next 'now)))
  13524. ((= c ?\t)
  13525. (condition-case nil
  13526. (setq tg (org-icompleting-read
  13527. "Tag: "
  13528. (or buffer-tags
  13529. (with-current-buffer buf
  13530. (org-get-buffer-tags)))))
  13531. (quit (setq tg "")))
  13532. (when (string-match "\\S-" tg)
  13533. (add-to-list 'buffer-tags (list tg))
  13534. (if (member tg current)
  13535. (setq current (delete tg current))
  13536. (push tg current)))
  13537. (if exit-after-next (setq exit-after-next 'now)))
  13538. ((setq e (rassoc c todo-table) tg (car e))
  13539. (with-current-buffer buf
  13540. (save-excursion (org-todo tg)))
  13541. (if exit-after-next (setq exit-after-next 'now)))
  13542. ((setq e (rassoc c ntable) tg (car e))
  13543. (if (member tg current)
  13544. (setq current (delete tg current))
  13545. (loop for g in groups do
  13546. (if (member tg g)
  13547. (mapc (lambda (x)
  13548. (setq current (delete x current)))
  13549. g)))
  13550. (push tg current))
  13551. (if exit-after-next (setq exit-after-next 'now))))
  13552. ;; Create a sorted list
  13553. (setq current
  13554. (sort current
  13555. (lambda (a b)
  13556. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13557. (if (eq exit-after-next 'now) (throw 'exit t))
  13558. (goto-char (point-min))
  13559. (beginning-of-line 2)
  13560. (delete-region (point) (point-at-eol))
  13561. (org-fast-tag-insert "Current" current c-face)
  13562. (org-set-current-tags-overlay current ov-prefix)
  13563. (while (re-search-forward
  13564. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13565. (setq tg (match-string 1))
  13566. (add-text-properties
  13567. (match-beginning 1) (match-end 1)
  13568. (list 'face
  13569. (cond
  13570. ((member tg current) c-face)
  13571. ((member tg inherited) i-face)
  13572. (t (get-text-property (match-beginning 1) 'face))))))
  13573. (goto-char (point-min)))))
  13574. (org-detach-overlay org-tags-overlay)
  13575. (if rtn
  13576. (mapconcat 'identity current ":")
  13577. nil))))
  13578. (defun org-get-tags-string ()
  13579. "Get the TAGS string in the current headline."
  13580. (unless (org-at-heading-p t)
  13581. (user-error "Not on a heading"))
  13582. (save-excursion
  13583. (beginning-of-line 1)
  13584. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13585. (org-match-string-no-properties 1)
  13586. "")))
  13587. (defun org-get-tags ()
  13588. "Get the list of tags specified in the current headline."
  13589. (org-split-string (org-get-tags-string) ":"))
  13590. (defun org-get-buffer-tags ()
  13591. "Get a table of all tags used in the buffer, for completion."
  13592. (let (tags)
  13593. (save-excursion
  13594. (goto-char (point-min))
  13595. (while (re-search-forward
  13596. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13597. (when (equal (char-after (point-at-bol 0)) ?*)
  13598. (mapc (lambda (x) (add-to-list 'tags x))
  13599. (org-split-string (org-match-string-no-properties 1) ":")))))
  13600. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13601. (mapcar 'list tags)))
  13602. ;;;; The mapping API
  13603. (defun org-map-entries (func &optional match scope &rest skip)
  13604. "Call FUNC at each headline selected by MATCH in SCOPE.
  13605. FUNC is a function or a lisp form. The function will be called without
  13606. arguments, with the cursor positioned at the beginning of the headline.
  13607. The return values of all calls to the function will be collected and
  13608. returned as a list.
  13609. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13610. does not need to preserve point. After evaluation, the cursor will be
  13611. moved to the end of the line (presumably of the headline of the
  13612. processed entry) and search continues from there. Under some
  13613. circumstances, this may not produce the wanted results. For example,
  13614. if you have removed (e.g. archived) the current (sub)tree it could
  13615. mean that the next entry will be skipped entirely. In such cases, you
  13616. can specify the position from where search should continue by making
  13617. FUNC set the variable `org-map-continue-from' to the desired buffer
  13618. position.
  13619. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13620. Only headlines that are matched by this query will be considered during
  13621. the iteration. When MATCH is nil or t, all headlines will be
  13622. visited by the iteration.
  13623. SCOPE determines the scope of this command. It can be any of:
  13624. nil The current buffer, respecting the restriction if any
  13625. tree The subtree started with the entry at point
  13626. region The entries within the active region, if any
  13627. region-start-level
  13628. The entries within the active region, but only those at
  13629. the same level than the first one.
  13630. file The current buffer, without restriction
  13631. file-with-archives
  13632. The current buffer, and any archives associated with it
  13633. agenda All agenda files
  13634. agenda-with-archives
  13635. All agenda files with any archive files associated with them
  13636. \(file1 file2 ...)
  13637. If this is a list, all files in the list will be scanned
  13638. The remaining args are treated as settings for the skipping facilities of
  13639. the scanner. The following items can be given here:
  13640. archive skip trees with the archive tag
  13641. comment skip trees with the COMMENT keyword
  13642. function or Emacs Lisp form:
  13643. will be used as value for `org-agenda-skip-function', so
  13644. whenever the function returns a position, FUNC will not be
  13645. called for that entry and search will continue from the
  13646. position returned
  13647. If your function needs to retrieve the tags including inherited tags
  13648. at the *current* entry, you can use the value of the variable
  13649. `org-scanner-tags' which will be much faster than getting the value
  13650. with `org-get-tags-at'. If your function gets properties with
  13651. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13652. to t around the call to `org-entry-properties' to get the same speedup.
  13653. Note that if your function moves around to retrieve tags and properties at
  13654. a *different* entry, you cannot use these techniques."
  13655. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13656. (not (org-region-active-p)))
  13657. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13658. (org-agenda-skip-archived-trees (memq 'archive skip))
  13659. (org-agenda-skip-comment-trees (memq 'comment skip))
  13660. (org-agenda-skip-function
  13661. (car (org-delete-all '(comment archive) skip)))
  13662. (org-tags-match-list-sublevels t)
  13663. (start-level (eq scope 'region-start-level))
  13664. matcher file res
  13665. org-todo-keywords-for-agenda
  13666. org-done-keywords-for-agenda
  13667. org-todo-keyword-alist-for-agenda
  13668. org-drawers-for-agenda
  13669. org-tag-alist-for-agenda
  13670. todo-only)
  13671. (cond
  13672. ((eq match t) (setq matcher t))
  13673. ((eq match nil) (setq matcher t))
  13674. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13675. (save-window-excursion
  13676. (save-restriction
  13677. (cond ((eq scope 'tree)
  13678. (org-back-to-heading t)
  13679. (org-narrow-to-subtree)
  13680. (setq scope nil))
  13681. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13682. (org-region-active-p))
  13683. ;; If needed, set start-level to a string like "2"
  13684. (when start-level
  13685. (save-excursion
  13686. (goto-char (region-beginning))
  13687. (unless (org-at-heading-p) (outline-next-heading))
  13688. (setq start-level (org-current-level))))
  13689. (narrow-to-region (region-beginning)
  13690. (save-excursion
  13691. (goto-char (region-end))
  13692. (unless (and (bolp) (org-at-heading-p))
  13693. (outline-next-heading))
  13694. (point)))
  13695. (setq scope nil)))
  13696. (if (not scope)
  13697. (progn
  13698. (org-agenda-prepare-buffers
  13699. (list (buffer-file-name (current-buffer))))
  13700. (setq res (org-scan-tags func matcher todo-only start-level)))
  13701. ;; Get the right scope
  13702. (cond
  13703. ((and scope (listp scope) (symbolp (car scope)))
  13704. (setq scope (eval scope)))
  13705. ((eq scope 'agenda)
  13706. (setq scope (org-agenda-files t)))
  13707. ((eq scope 'agenda-with-archives)
  13708. (setq scope (org-agenda-files t))
  13709. (setq scope (org-add-archive-files scope)))
  13710. ((eq scope 'file)
  13711. (setq scope (list (buffer-file-name))))
  13712. ((eq scope 'file-with-archives)
  13713. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13714. (org-agenda-prepare-buffers scope)
  13715. (while (setq file (pop scope))
  13716. (with-current-buffer (org-find-base-buffer-visiting file)
  13717. (save-excursion
  13718. (save-restriction
  13719. (widen)
  13720. (goto-char (point-min))
  13721. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13722. res)))
  13723. ;;;; Properties
  13724. ;;; Setting and retrieving properties
  13725. (defconst org-special-properties
  13726. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13727. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13728. "The special properties valid in Org-mode.
  13729. These are properties that are not defined in the property drawer,
  13730. but in some other way.")
  13731. (defconst org-default-properties
  13732. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13733. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13734. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13735. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13736. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13737. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13738. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13739. "Some properties that are used by Org-mode for various purposes.
  13740. Being in this list makes sure that they are offered for completion.")
  13741. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13742. "Regular expression matching the first line of a property drawer.")
  13743. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13744. "Regular expression matching the last line of a property drawer.")
  13745. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13746. "Regular expression matching the first line of a property drawer.")
  13747. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13748. "Regular expression matching the first line of a property drawer.")
  13749. (defconst org-property-drawer-re
  13750. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13751. org-property-end-re "\\)\n?")
  13752. "Matches an entire property drawer.")
  13753. (defconst org-clock-drawer-re
  13754. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13755. org-property-end-re "\\)\n?")
  13756. "Matches an entire clock drawer.")
  13757. (defun org-property-action ()
  13758. "Do an action on properties."
  13759. (interactive)
  13760. (let (c)
  13761. (org-at-property-p)
  13762. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13763. (setq c (read-char-exclusive))
  13764. (cond
  13765. ((equal c ?s)
  13766. (call-interactively 'org-set-property))
  13767. ((equal c ?d)
  13768. (call-interactively 'org-delete-property))
  13769. ((equal c ?D)
  13770. (call-interactively 'org-delete-property-globally))
  13771. ((equal c ?c)
  13772. (call-interactively 'org-compute-property-at-point))
  13773. (t (user-error "No such property action %c" c)))))
  13774. (defun org-inc-effort ()
  13775. "Increment the value of the effort property in the current entry."
  13776. (interactive)
  13777. (org-set-effort nil t))
  13778. (defvar org-clock-effort) ;; Defined in org-clock.el
  13779. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13780. (defun org-set-effort (&optional value increment)
  13781. "Set the effort property of the current entry.
  13782. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13783. 10th allowed value.
  13784. When INCREMENT is non-nil, set the property to the next allowed value."
  13785. (interactive "P")
  13786. (if (equal value 0) (setq value 10))
  13787. (let* ((completion-ignore-case t)
  13788. (prop org-effort-property)
  13789. (cur (org-entry-get nil prop))
  13790. (allowed (org-property-get-allowed-values nil prop 'table))
  13791. (existing (mapcar 'list (org-property-values prop)))
  13792. (heading (nth 4 (org-heading-components)))
  13793. rpl
  13794. (val (cond
  13795. ((stringp value) value)
  13796. ((and allowed (integerp value))
  13797. (or (car (nth (1- value) allowed))
  13798. (car (org-last allowed))))
  13799. ((and allowed increment)
  13800. (or (caadr (member (list cur) allowed))
  13801. (user-error "Allowed effort values are not set")))
  13802. (allowed
  13803. (message "Select 1-9,0, [RET%s]: %s"
  13804. (if cur (concat "=" cur) "")
  13805. (mapconcat 'car allowed " "))
  13806. (setq rpl (read-char-exclusive))
  13807. (if (equal rpl ?\r)
  13808. cur
  13809. (setq rpl (- rpl ?0))
  13810. (if (equal rpl 0) (setq rpl 10))
  13811. (if (and (> rpl 0) (<= rpl (length allowed)))
  13812. (car (nth (1- rpl) allowed))
  13813. (org-completing-read "Effort: " allowed nil))))
  13814. (t
  13815. (let (org-completion-use-ido org-completion-use-iswitchb)
  13816. (org-completing-read
  13817. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13818. (concat "[" cur "]") "")
  13819. ": ")
  13820. existing nil nil "" nil cur))))))
  13821. (unless (equal (org-entry-get nil prop) val)
  13822. (org-entry-put nil prop val))
  13823. (save-excursion
  13824. (org-back-to-heading t)
  13825. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13826. (when (string= heading org-clock-current-task)
  13827. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13828. (org-clock-update-mode-line))
  13829. (message "%s is now %s" prop val)))
  13830. (defun org-at-property-p ()
  13831. "Is cursor inside a property drawer?"
  13832. (save-excursion
  13833. (when (equal 'node-property (car (org-element-at-point)))
  13834. (beginning-of-line 1)
  13835. (looking-at org-property-re))))
  13836. (defun org-get-property-block (&optional beg end force)
  13837. "Return the (beg . end) range of the body of the property drawer.
  13838. BEG and END are the beginning and end of the current subtree, or of
  13839. the part before the first headline. If they are not given, they will
  13840. be found. If the drawer does not exist and FORCE is non-nil, create
  13841. the drawer."
  13842. (catch 'exit
  13843. (save-excursion
  13844. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13845. (progn (org-back-to-heading t) (point))))
  13846. (end (or end (and (not (outline-next-heading)) (point-max))
  13847. (point))))
  13848. (goto-char beg)
  13849. (if (re-search-forward org-property-start-re end t)
  13850. (setq beg (1+ (match-end 0)))
  13851. (if force
  13852. (save-excursion
  13853. (org-insert-property-drawer)
  13854. (setq end (progn (outline-next-heading) (point))))
  13855. (throw 'exit nil))
  13856. (goto-char beg)
  13857. (if (re-search-forward org-property-start-re end t)
  13858. (setq beg (1+ (match-end 0)))))
  13859. (if (re-search-forward org-property-end-re end t)
  13860. (setq end (match-beginning 0))
  13861. (or force (throw 'exit nil))
  13862. (goto-char beg)
  13863. (setq end beg)
  13864. (org-indent-line)
  13865. (insert ":END:\n"))
  13866. (cons beg end)))))
  13867. (defun org-entry-properties (&optional pom which specific)
  13868. "Get all properties of the entry at point-or-marker POM.
  13869. This includes the TODO keyword, the tags, time strings for deadline,
  13870. scheduled, and clocking, and any additional properties defined in the
  13871. entry. The return value is an alist, keys may occur multiple times
  13872. if the property key was used several times.
  13873. POM may also be nil, in which case the current entry is used.
  13874. If WHICH is nil or `all', get all properties. If WHICH is
  13875. `special' or `standard', only get that subclass. If WHICH
  13876. is a string only get exactly this property. SPECIFIC can be a string, the
  13877. specific property we are interested in. Specifying it can speed
  13878. things up because then unnecessary parsing is avoided."
  13879. (setq which (or which 'all))
  13880. (org-with-point-at pom
  13881. (let ((clockstr (substring org-clock-string 0 -1))
  13882. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13883. (case-fold-search nil)
  13884. beg end range props sum-props key key1 value string clocksum clocksumt)
  13885. (save-excursion
  13886. (when (condition-case nil
  13887. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13888. (error nil))
  13889. (setq beg (point))
  13890. (setq sum-props (get-text-property (point) 'org-summaries))
  13891. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13892. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13893. (outline-next-heading)
  13894. (setq end (point))
  13895. (when (memq which '(all special))
  13896. ;; Get the special properties, like TODO and tags
  13897. (goto-char beg)
  13898. (when (and (or (not specific) (string= specific "TODO"))
  13899. (looking-at org-todo-line-regexp) (match-end 2))
  13900. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13901. (when (and (or (not specific) (string= specific "PRIORITY"))
  13902. (looking-at org-priority-regexp))
  13903. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13904. (when (or (not specific) (string= specific "FILE"))
  13905. (push (cons "FILE" buffer-file-name) props))
  13906. (when (and (or (not specific) (string= specific "TAGS"))
  13907. (setq value (org-get-tags-string))
  13908. (string-match "\\S-" value))
  13909. (push (cons "TAGS" value) props))
  13910. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13911. (setq value (org-get-tags-at)))
  13912. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13913. ":"))
  13914. props))
  13915. (when (or (not specific) (string= specific "BLOCKED"))
  13916. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13917. (when (or (not specific)
  13918. (member specific
  13919. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13920. "TIMESTAMP" "TIMESTAMP_IA")))
  13921. (catch 'match
  13922. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13923. (setq key (if (match-end 1)
  13924. (substring (org-match-string-no-properties 1)
  13925. 0 -1))
  13926. string (if (equal key clockstr)
  13927. (org-trim
  13928. (buffer-substring-no-properties
  13929. (match-beginning 3) (goto-char
  13930. (point-at-eol))))
  13931. (substring (org-match-string-no-properties 3)
  13932. 1 -1)))
  13933. ;; Get the correct property name from the key. This is
  13934. ;; necessary if the user has configured time keywords.
  13935. (setq key1 (concat key ":"))
  13936. (cond
  13937. ((not key)
  13938. (setq key
  13939. (if (= (char-after (match-beginning 3)) ?\[)
  13940. "TIMESTAMP_IA" "TIMESTAMP")))
  13941. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13942. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13943. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13944. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13945. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13946. (progn
  13947. (push (cons key string) props)
  13948. ;; no need to search further if match is found
  13949. (throw 'match t))
  13950. (when (or (equal key "CLOCK") (not (assoc key props)))
  13951. (push (cons key string) props)))))))
  13952. (when (memq which '(all standard))
  13953. ;; Get the standard properties, like :PROP: ...
  13954. (setq range (org-get-property-block beg end))
  13955. (when range
  13956. (goto-char (car range))
  13957. (while (re-search-forward org-property-re
  13958. (cdr range) t)
  13959. (setq key (org-match-string-no-properties 2)
  13960. value (org-trim (or (org-match-string-no-properties 3) "")))
  13961. (unless (member key excluded)
  13962. (push (cons key (or value "")) props)))))
  13963. (if clocksum
  13964. (push (cons "CLOCKSUM"
  13965. (org-columns-number-to-string (/ (float clocksum) 60.)
  13966. 'add_times))
  13967. props))
  13968. (if clocksumt
  13969. (push (cons "CLOCKSUM_T"
  13970. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13971. 'add_times))
  13972. props))
  13973. (unless (assoc "CATEGORY" props)
  13974. (push (cons "CATEGORY" (org-get-category)) props))
  13975. (append sum-props (nreverse props)))))))
  13976. (defun org-entry-get (pom property &optional inherit literal-nil)
  13977. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13978. If INHERIT is non-nil and the entry does not have the property,
  13979. then also check higher levels of the hierarchy.
  13980. If INHERIT is the symbol `selective', use inheritance only if the setting
  13981. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13982. If the property is present but empty, the return value is the empty string.
  13983. If the property is not present at all, nil is returned.
  13984. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13985. do not interpret it as the list atom nil. This is used for inheritance
  13986. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13987. (org-with-point-at pom
  13988. (if (and inherit (if (eq inherit 'selective)
  13989. (org-property-inherit-p property)
  13990. t))
  13991. (org-entry-get-with-inheritance property literal-nil)
  13992. (if (member property org-special-properties)
  13993. ;; We need a special property. Use `org-entry-properties' to
  13994. ;; retrieve it, but specify the wanted property
  13995. (cdr (assoc property (org-entry-properties nil 'special property)))
  13996. (let ((range (org-get-property-block)))
  13997. (when (and range (not (eq (car range) (cdr range))))
  13998. (let* ((props (list (or (assoc property org-file-properties)
  13999. (assoc property org-global-properties)
  14000. (assoc property org-global-properties-fixed))))
  14001. (ap (lambda (key)
  14002. (when (re-search-forward
  14003. (org-re-property key) (cdr range) t)
  14004. (setq props
  14005. (org-update-property-plist
  14006. key
  14007. (if (match-end 3)
  14008. (org-match-string-no-properties 3) "")
  14009. props)))))
  14010. val)
  14011. (goto-char (car range))
  14012. (funcall ap property)
  14013. (goto-char (car range))
  14014. (while (funcall ap (concat property "+")))
  14015. (setq val (cdr (assoc property props)))
  14016. (when val (if literal-nil val (org-not-nil val))))))))))
  14017. (defun org-property-or-variable-value (var &optional inherit)
  14018. "Check if there is a property fixing the value of VAR.
  14019. If yes, return this value. If not, return the current value of the variable."
  14020. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14021. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14022. (read prop)
  14023. (symbol-value var))))
  14024. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14025. "Delete the property PROPERTY from entry at point-or-marker POM.
  14026. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14027. an empty drawer to delete."
  14028. (org-with-point-at pom
  14029. (if (member property org-special-properties)
  14030. nil ; cannot delete these properties.
  14031. (let ((range (org-get-property-block)))
  14032. (if (and range
  14033. (goto-char (car range))
  14034. (re-search-forward
  14035. (org-re-property property)
  14036. (cdr range) t))
  14037. (progn
  14038. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14039. (and delete-empty-drawer
  14040. (org-remove-empty-drawer-at
  14041. delete-empty-drawer (car range)))
  14042. t)
  14043. nil)))))
  14044. ;; Multi-values properties are properties that contain multiple values
  14045. ;; These values are assumed to be single words, separated by whitespace.
  14046. (defun org-entry-add-to-multivalued-property (pom property value)
  14047. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14048. (let* ((old (org-entry-get pom property))
  14049. (values (and old (org-split-string old "[ \t]"))))
  14050. (setq value (org-entry-protect-space value))
  14051. (unless (member value values)
  14052. (setq values (append values (list value)))
  14053. (org-entry-put pom property
  14054. (mapconcat 'identity values " ")))))
  14055. (defun org-entry-remove-from-multivalued-property (pom property value)
  14056. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14057. (let* ((old (org-entry-get pom property))
  14058. (values (and old (org-split-string old "[ \t]"))))
  14059. (setq value (org-entry-protect-space value))
  14060. (when (member value values)
  14061. (setq values (delete value values))
  14062. (org-entry-put pom property
  14063. (mapconcat 'identity values " ")))))
  14064. (defun org-entry-member-in-multivalued-property (pom property value)
  14065. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14066. (let* ((old (org-entry-get pom property))
  14067. (values (and old (org-split-string old "[ \t]"))))
  14068. (setq value (org-entry-protect-space value))
  14069. (member value values)))
  14070. (defun org-entry-get-multivalued-property (pom property)
  14071. "Return a list of values in a multivalued property."
  14072. (let* ((value (org-entry-get pom property))
  14073. (values (and value (org-split-string value "[ \t]"))))
  14074. (mapcar 'org-entry-restore-space values)))
  14075. (defun org-entry-put-multivalued-property (pom property &rest values)
  14076. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14077. VALUES should be a list of strings. Spaces will be protected."
  14078. (org-entry-put pom property
  14079. (mapconcat 'org-entry-protect-space values " "))
  14080. (let* ((value (org-entry-get pom property))
  14081. (values (and value (org-split-string value "[ \t]"))))
  14082. (mapcar 'org-entry-restore-space values)))
  14083. (defun org-entry-protect-space (s)
  14084. "Protect spaces and newline in string S."
  14085. (while (string-match " " s)
  14086. (setq s (replace-match "%20" t t s)))
  14087. (while (string-match "\n" s)
  14088. (setq s (replace-match "%0A" t t s)))
  14089. s)
  14090. (defun org-entry-restore-space (s)
  14091. "Restore spaces and newline in string S."
  14092. (while (string-match "%20" s)
  14093. (setq s (replace-match " " t t s)))
  14094. (while (string-match "%0A" s)
  14095. (setq s (replace-match "\n" t t s)))
  14096. s)
  14097. (defvar org-entry-property-inherited-from (make-marker)
  14098. "Marker pointing to the entry from where a property was inherited.
  14099. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14100. location of the entry where the inheritance search matched. If there was
  14101. no match, the marker will point nowhere.
  14102. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14103. is set.")
  14104. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14105. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14106. The search will stop at the first ancestor which has the property defined.
  14107. If the value found is \"nil\", return nil to show that the property
  14108. should be considered as undefined (this is the meaning of nil here).
  14109. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14110. (move-marker org-entry-property-inherited-from nil)
  14111. (let (tmp)
  14112. (save-excursion
  14113. (save-restriction
  14114. (widen)
  14115. (catch 'ex
  14116. (while t
  14117. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14118. (or (ignore-errors (org-back-to-heading t))
  14119. (goto-char (point-min)))
  14120. (move-marker org-entry-property-inherited-from (point))
  14121. (throw 'ex tmp))
  14122. (or (ignore-errors (org-up-heading-safe))
  14123. (throw 'ex nil))))))
  14124. (setq tmp (or tmp
  14125. (cdr (assoc property org-file-properties))
  14126. (cdr (assoc property org-global-properties))
  14127. (cdr (assoc property org-global-properties-fixed))))
  14128. (if literal-nil tmp (org-not-nil tmp))))
  14129. (defvar org-property-changed-functions nil
  14130. "Hook called when the value of a property has changed.
  14131. Each hook function should accept two arguments, the name of the property
  14132. and the new value.")
  14133. (defun org-entry-put (pom property value)
  14134. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14135. (org-with-point-at pom
  14136. (org-back-to-heading t)
  14137. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14138. range)
  14139. (cond
  14140. ((equal property "TODO")
  14141. (when (and (stringp value) (string-match "\\S-" value)
  14142. (not (member value org-todo-keywords-1)))
  14143. (user-error "\"%s\" is not a valid TODO state" value))
  14144. (if (or (not value)
  14145. (not (string-match "\\S-" value)))
  14146. (setq value 'none))
  14147. (org-todo value)
  14148. (org-set-tags nil 'align))
  14149. ((equal property "PRIORITY")
  14150. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14151. (string-to-char value) ?\ ))
  14152. (org-set-tags nil 'align))
  14153. ((equal property "CLOCKSUM")
  14154. (if (not (re-search-forward
  14155. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14156. (error "Cannot find a clock log")
  14157. (goto-char (- (match-end 1) 2))
  14158. (cond
  14159. ((eq value 'earlier) (org-timestamp-down))
  14160. ((eq value 'later) (org-timestamp-up)))
  14161. (org-clock-sum-current-item)))
  14162. ((equal property "SCHEDULED")
  14163. (if (re-search-forward org-scheduled-time-regexp end t)
  14164. (cond
  14165. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14166. ((eq value 'later) (org-timestamp-change 1 'day))
  14167. (t (call-interactively 'org-schedule)))
  14168. (call-interactively 'org-schedule)))
  14169. ((equal property "DEADLINE")
  14170. (if (re-search-forward org-deadline-time-regexp end t)
  14171. (cond
  14172. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14173. ((eq value 'later) (org-timestamp-change 1 'day))
  14174. (t (call-interactively 'org-deadline)))
  14175. (call-interactively 'org-deadline)))
  14176. ((member property org-special-properties)
  14177. (error "The %s property can not yet be set with `org-entry-put'"
  14178. property))
  14179. (t ; a non-special property
  14180. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14181. (setq range (org-get-property-block beg end 'force))
  14182. (goto-char (car range))
  14183. (if (re-search-forward
  14184. (org-re-property property) (cdr range) t)
  14185. (progn
  14186. (delete-region (match-beginning 0) (match-end 0))
  14187. (goto-char (match-beginning 0)))
  14188. (goto-char (cdr range))
  14189. (insert "\n")
  14190. (backward-char 1)
  14191. (org-indent-line))
  14192. (insert ":" property ":")
  14193. (and value (insert " " value))
  14194. (org-indent-line)))))
  14195. (run-hook-with-args 'org-property-changed-functions property value)))
  14196. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14197. "Get all property keys in the current buffer.
  14198. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14199. like tags and TODO state.
  14200. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14201. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14202. and others.
  14203. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14204. formats in the current buffer."
  14205. (let (rtn range cfmt s p)
  14206. (save-excursion
  14207. (save-restriction
  14208. (widen)
  14209. (goto-char (point-min))
  14210. (while (re-search-forward org-property-start-re nil t)
  14211. (setq range (org-get-property-block))
  14212. (goto-char (car range))
  14213. (while (re-search-forward org-property-re
  14214. (cdr range) t)
  14215. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14216. (outline-next-heading))))
  14217. (when include-specials
  14218. (setq rtn (append org-special-properties rtn)))
  14219. (when include-defaults
  14220. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14221. (add-to-list 'rtn org-effort-property))
  14222. (when include-columns
  14223. (save-excursion
  14224. (save-restriction
  14225. (widen)
  14226. (goto-char (point-min))
  14227. (while (re-search-forward
  14228. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14229. nil t)
  14230. (setq cfmt (match-string 2) s 0)
  14231. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14232. cfmt s)
  14233. (setq s (match-end 0)
  14234. p (match-string 1 cfmt))
  14235. (unless (or (equal p "ITEM")
  14236. (member p org-special-properties))
  14237. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14238. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14239. (defun org-property-values (key)
  14240. "Return a list of all values of property KEY in the current buffer."
  14241. (save-excursion
  14242. (save-restriction
  14243. (widen)
  14244. (goto-char (point-min))
  14245. (let ((re (org-re-property key))
  14246. values)
  14247. (while (re-search-forward re nil t)
  14248. (add-to-list 'values (org-trim (match-string 3))))
  14249. (delete "" values)))))
  14250. (defun org-insert-property-drawer ()
  14251. "Insert a property drawer into the current entry."
  14252. (org-back-to-heading t)
  14253. (looking-at org-outline-regexp)
  14254. (let ((indent (if org-adapt-indentation
  14255. (- (match-end 0) (match-beginning 0))
  14256. 0))
  14257. (beg (point))
  14258. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14259. end hiddenp)
  14260. (outline-next-heading)
  14261. (setq end (point))
  14262. (goto-char beg)
  14263. (while (re-search-forward re end t))
  14264. (setq hiddenp (outline-invisible-p))
  14265. (end-of-line 1)
  14266. (and (equal (char-after) ?\n) (forward-char 1))
  14267. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14268. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14269. ;; just skip this line
  14270. (beginning-of-line 2)
  14271. ;; Drawer start, find the end
  14272. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14273. (beginning-of-line 1)))
  14274. (org-skip-over-state-notes)
  14275. (skip-chars-backward " \t\n\r")
  14276. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14277. (forward-char 1))
  14278. (goto-char (point-at-eol))
  14279. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14280. (beginning-of-line 0)
  14281. (org-indent-to-column indent)
  14282. (beginning-of-line 2)
  14283. (org-indent-to-column indent)
  14284. (beginning-of-line 0)
  14285. (if hiddenp
  14286. (save-excursion
  14287. (org-back-to-heading t)
  14288. (hide-entry))
  14289. (org-flag-drawer t))))
  14290. (defun org-insert-drawer (&optional arg drawer)
  14291. "Insert a drawer at point.
  14292. Optional argument DRAWER, when non-nil, is a string representing
  14293. drawer's name. Otherwise, the user is prompted for a name.
  14294. If a region is active, insert the drawer around that region
  14295. instead.
  14296. Point is left between drawer's boundaries."
  14297. (interactive "P")
  14298. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14299. "LOGBOOK"))
  14300. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14301. ;; internally by Org. They are meant to be inserted
  14302. ;; automatically.
  14303. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14304. ;; Remove system drawers from list. Note: For some reason,
  14305. ;; `org-completing-read' ignores the predicate while
  14306. ;; `completing-read' handles it fine.
  14307. (drawer (if arg "PROPERTIES"
  14308. (or drawer
  14309. (completing-read
  14310. "Drawer: " org-drawers
  14311. (lambda (d) (not (member d system-drawers))))))))
  14312. (cond
  14313. ;; With C-u, fall back on `org-insert-property-drawer'
  14314. (arg (org-insert-property-drawer))
  14315. ;; With an active region, insert a drawer at point.
  14316. ((not (org-region-active-p))
  14317. (progn
  14318. (unless (bolp) (insert "\n"))
  14319. (insert (format ":%s:\n\n:END:\n" drawer))
  14320. (forward-line -2)))
  14321. ;; Otherwise, insert the drawer at point
  14322. (t
  14323. (let ((rbeg (region-beginning))
  14324. (rend (copy-marker (region-end))))
  14325. (unwind-protect
  14326. (progn
  14327. (goto-char rbeg)
  14328. (beginning-of-line)
  14329. (when (save-excursion
  14330. (re-search-forward org-outline-regexp-bol rend t))
  14331. (user-error "Drawers cannot contain headlines"))
  14332. ;; Position point at the beginning of the first
  14333. ;; non-blank line in region. Insert drawer's opening
  14334. ;; there, then indent it.
  14335. (org-skip-whitespace)
  14336. (beginning-of-line)
  14337. (insert ":" drawer ":\n")
  14338. (forward-line -1)
  14339. (indent-for-tab-command)
  14340. ;; Move point to the beginning of the first blank line
  14341. ;; after the last non-blank line in region. Insert
  14342. ;; drawer's closing, then indent it.
  14343. (goto-char rend)
  14344. (skip-chars-backward " \r\t\n")
  14345. (insert "\n:END:")
  14346. (deactivate-mark t)
  14347. (indent-for-tab-command)
  14348. (unless (eolp) (insert "\n")))
  14349. ;; Clear marker, whatever the outcome of insertion is.
  14350. (set-marker rend nil)))))))
  14351. (defvar org-property-set-functions-alist nil
  14352. "Property set function alist.
  14353. Each entry should have the following format:
  14354. (PROPERTY . READ-FUNCTION)
  14355. The read function will be called with the same argument as
  14356. `org-completing-read'.")
  14357. (defun org-set-property-function (property)
  14358. "Get the function that should be used to set PROPERTY.
  14359. This is computed according to `org-property-set-functions-alist'."
  14360. (or (cdr (assoc property org-property-set-functions-alist))
  14361. 'org-completing-read))
  14362. (defun org-read-property-value (property)
  14363. "Read PROPERTY value from user."
  14364. (let* ((completion-ignore-case t)
  14365. (allowed (org-property-get-allowed-values nil property 'table))
  14366. (cur (org-entry-get nil property))
  14367. (prompt (concat property " value"
  14368. (if (and cur (string-match "\\S-" cur))
  14369. (concat " [" cur "]") "") ": "))
  14370. (set-function (org-set-property-function property))
  14371. (val (if allowed
  14372. (funcall set-function prompt allowed nil
  14373. (not (get-text-property 0 'org-unrestricted
  14374. (caar allowed))))
  14375. (let (org-completion-use-ido org-completion-use-iswitchb)
  14376. (funcall set-function prompt
  14377. (mapcar 'list (org-property-values property))
  14378. nil nil "" nil cur)))))
  14379. (if (equal val "")
  14380. cur
  14381. val)))
  14382. (defvar org-last-set-property nil)
  14383. (defvar org-last-set-property-value nil)
  14384. (defun org-read-property-name ()
  14385. "Read a property name."
  14386. (let* ((completion-ignore-case t)
  14387. (keys (org-buffer-property-keys nil t t))
  14388. (default-prop (or (save-excursion
  14389. (save-match-data
  14390. (beginning-of-line)
  14391. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14392. (null (string= (match-string 1) "END"))
  14393. (match-string 1))))
  14394. org-last-set-property))
  14395. (property (org-icompleting-read
  14396. (concat "Property"
  14397. (if default-prop (concat " [" default-prop "]") "")
  14398. ": ")
  14399. (mapcar 'list keys)
  14400. nil nil nil nil
  14401. default-prop)))
  14402. (if (member property keys)
  14403. property
  14404. (or (cdr (assoc (downcase property)
  14405. (mapcar (lambda (x) (cons (downcase x) x))
  14406. keys)))
  14407. property))))
  14408. (defun org-set-property-and-value (use-last)
  14409. "Allow to set [PROPERTY]: [value] direction from prompt.
  14410. When use-default, don't even ask, just use the last
  14411. \"[PROPERTY]: [value]\" string from the history."
  14412. (interactive "P")
  14413. (let* ((completion-ignore-case t)
  14414. (pv (or (and use-last org-last-set-property-value)
  14415. (org-completing-read
  14416. "Enter a \"[Property]: [value]\" pair: "
  14417. nil nil nil nil nil
  14418. org-last-set-property-value)))
  14419. prop val)
  14420. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14421. (setq prop (match-string 1 pv)
  14422. val (match-string 2 pv))
  14423. (org-set-property prop val))))
  14424. (defun org-set-property (property value)
  14425. "In the current entry, set PROPERTY to VALUE.
  14426. When called interactively, this will prompt for a property name, offering
  14427. completion on existing and default properties. And then it will prompt
  14428. for a value, offering completion either on allowed values (via an inherited
  14429. xxx_ALL property) or on existing values in other instances of this property
  14430. in the current file."
  14431. (interactive (list nil nil))
  14432. (let* ((property (or property (org-read-property-name)))
  14433. (value (or value (org-read-property-value property)))
  14434. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14435. (setq org-last-set-property property)
  14436. (setq org-last-set-property-value (concat property ": " value))
  14437. ;; Possibly postprocess the inserted value:
  14438. (when fn (setq value (funcall fn value)))
  14439. (unless (equal (org-entry-get nil property) value)
  14440. (org-entry-put nil property value))))
  14441. (defun org-delete-property (property &optional delete-empty-drawer)
  14442. "In the current entry, delete PROPERTY.
  14443. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14444. an empty drawer to delete."
  14445. (interactive
  14446. (let* ((completion-ignore-case t)
  14447. (prop (org-icompleting-read "Property: "
  14448. (org-entry-properties nil 'standard))))
  14449. (list prop)))
  14450. (message "Property %s %s" property
  14451. (if (org-entry-delete nil property delete-empty-drawer)
  14452. "deleted"
  14453. "was not present in the entry")))
  14454. (defun org-delete-property-globally (property)
  14455. "Remove PROPERTY globally, from all entries."
  14456. (interactive
  14457. (let* ((completion-ignore-case t)
  14458. (prop (org-icompleting-read
  14459. "Globally remove property: "
  14460. (mapcar 'list (org-buffer-property-keys)))))
  14461. (list prop)))
  14462. (save-excursion
  14463. (save-restriction
  14464. (widen)
  14465. (goto-char (point-min))
  14466. (let ((cnt 0))
  14467. (while (re-search-forward
  14468. (org-re-property property)
  14469. nil t)
  14470. (setq cnt (1+ cnt))
  14471. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14472. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14473. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14474. (defun org-compute-property-at-point ()
  14475. "Compute the property at point.
  14476. This looks for an enclosing column format, extracts the operator and
  14477. then applies it to the property in the column format's scope."
  14478. (interactive)
  14479. (unless (org-at-property-p)
  14480. (user-error "Not at a property"))
  14481. (let ((prop (org-match-string-no-properties 2)))
  14482. (org-columns-get-format-and-top-level)
  14483. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14484. (user-error "No operator defined for property %s" prop))
  14485. (org-columns-compute prop)))
  14486. (defvar org-property-allowed-value-functions nil
  14487. "Hook for functions supplying allowed values for a specific property.
  14488. The functions must take a single argument, the name of the property, and
  14489. return a flat list of allowed values. If \":ETC\" is one of
  14490. the values, this means that these values are intended as defaults for
  14491. completion, but that other values should be allowed too.
  14492. The functions must return nil if they are not responsible for this
  14493. property.")
  14494. (defun org-property-get-allowed-values (pom property &optional table)
  14495. "Get allowed values for the property PROPERTY.
  14496. When TABLE is non-nil, return an alist that can directly be used for
  14497. completion."
  14498. (let (vals)
  14499. (cond
  14500. ((equal property "TODO")
  14501. (setq vals (org-with-point-at pom
  14502. (append org-todo-keywords-1 '("")))))
  14503. ((equal property "PRIORITY")
  14504. (let ((n org-lowest-priority))
  14505. (while (>= n org-highest-priority)
  14506. (push (char-to-string n) vals)
  14507. (setq n (1- n)))))
  14508. ((member property org-special-properties))
  14509. ((setq vals (run-hook-with-args-until-success
  14510. 'org-property-allowed-value-functions property)))
  14511. (t
  14512. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14513. (when (and vals (string-match "\\S-" vals))
  14514. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14515. (setq vals (mapcar (lambda (x)
  14516. (cond ((stringp x) x)
  14517. ((numberp x) (number-to-string x))
  14518. ((symbolp x) (symbol-name x))
  14519. (t "???")))
  14520. vals)))))
  14521. (when (member ":ETC" vals)
  14522. (setq vals (remove ":ETC" vals))
  14523. (org-add-props (car vals) '(org-unrestricted t)))
  14524. (if table (mapcar 'list vals) vals)))
  14525. (defun org-property-previous-allowed-value (&optional previous)
  14526. "Switch to the next allowed value for this property."
  14527. (interactive)
  14528. (org-property-next-allowed-value t))
  14529. (defun org-property-next-allowed-value (&optional previous)
  14530. "Switch to the next allowed value for this property."
  14531. (interactive)
  14532. (unless (org-at-property-p)
  14533. (user-error "Not at a property"))
  14534. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14535. (key (match-string 2))
  14536. (value (match-string 3))
  14537. (allowed (or (org-property-get-allowed-values (point) key)
  14538. (and (member value '("[ ]" "[-]" "[X]"))
  14539. '("[ ]" "[X]"))))
  14540. (heading (save-match-data (nth 4 (org-heading-components))))
  14541. nval)
  14542. (unless allowed
  14543. (user-error "Allowed values for this property have not been defined"))
  14544. (if previous (setq allowed (reverse allowed)))
  14545. (if (member value allowed)
  14546. (setq nval (car (cdr (member value allowed)))))
  14547. (setq nval (or nval (car allowed)))
  14548. (if (equal nval value)
  14549. (user-error "Only one allowed value for this property"))
  14550. (org-at-property-p)
  14551. (replace-match (concat " :" key ": " nval) t t)
  14552. (org-indent-line)
  14553. (beginning-of-line 1)
  14554. (skip-chars-forward " \t")
  14555. (when (equal prop org-effort-property)
  14556. (save-excursion
  14557. (org-back-to-heading t)
  14558. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14559. (when (string= org-clock-current-task heading)
  14560. (setq org-clock-effort nval)
  14561. (org-clock-update-mode-line)))
  14562. (run-hook-with-args 'org-property-changed-functions key nval)))
  14563. (defun org-find-olp (path &optional this-buffer)
  14564. "Return a marker pointing to the entry at outline path OLP.
  14565. If anything goes wrong, throw an error.
  14566. You can wrap this call to catch the error like this:
  14567. (condition-case msg
  14568. (org-mobile-locate-entry (match-string 4))
  14569. (error (nth 1 msg)))
  14570. The return value will then be either a string with the error message,
  14571. or a marker if everything is OK.
  14572. If THIS-BUFFER is set, the outline path does not contain a file,
  14573. only headings."
  14574. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14575. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14576. (level 1)
  14577. (lmin 1)
  14578. (lmax 1)
  14579. limit re end found pos heading cnt flevel)
  14580. (unless buffer (error "File not found :%s" file))
  14581. (with-current-buffer buffer
  14582. (save-excursion
  14583. (save-restriction
  14584. (widen)
  14585. (setq limit (point-max))
  14586. (goto-char (point-min))
  14587. (while (setq heading (pop path))
  14588. (setq re (format org-complex-heading-regexp-format
  14589. (regexp-quote heading)))
  14590. (setq cnt 0 pos (point))
  14591. (while (re-search-forward re end t)
  14592. (setq level (- (match-end 1) (match-beginning 1)))
  14593. (if (and (>= level lmin) (<= level lmax))
  14594. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14595. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14596. lmax heading))
  14597. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14598. lmax heading))
  14599. (goto-char found)
  14600. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14601. (setq end (save-excursion (org-end-of-subtree t t))))
  14602. (when (org-at-heading-p)
  14603. (point-marker)))))))
  14604. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14605. "Find node HEADING in BUFFER.
  14606. Return a marker to the heading if it was found, or nil if not.
  14607. If POS-ONLY is set, return just the position instead of a marker.
  14608. The heading text must match exact, but it may have a TODO keyword,
  14609. a priority cookie and tags in the standard locations."
  14610. (with-current-buffer (or buffer (current-buffer))
  14611. (save-excursion
  14612. (save-restriction
  14613. (widen)
  14614. (goto-char (point-min))
  14615. (let (case-fold-search)
  14616. (if (re-search-forward
  14617. (format org-complex-heading-regexp-format
  14618. (regexp-quote heading)) nil t)
  14619. (if pos-only
  14620. (match-beginning 0)
  14621. (move-marker (make-marker) (match-beginning 0)))))))))
  14622. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14623. "Find Org node headline HEADING in all .org files in directory DIR.
  14624. When the target headline is found, return a marker to this location."
  14625. (let ((files (directory-files (or dir default-directory)
  14626. nil "\\`[^.#].*\\.org\\'"))
  14627. file visiting m buffer)
  14628. (catch 'found
  14629. (while (setq file (pop files))
  14630. (message "trying %s" file)
  14631. (setq visiting (org-find-base-buffer-visiting file))
  14632. (setq buffer (or visiting (find-file-noselect file)))
  14633. (setq m (org-find-exact-headline-in-buffer
  14634. heading buffer))
  14635. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14636. (and m (throw 'found m))))))
  14637. (defun org-find-entry-with-id (ident)
  14638. "Locate the entry that contains the ID property with exact value IDENT.
  14639. IDENT can be a string, a symbol or a number, this function will search for
  14640. the string representation of it.
  14641. Return the position where this entry starts, or nil if there is no such entry."
  14642. (interactive "sID: ")
  14643. (let ((id (cond
  14644. ((stringp ident) ident)
  14645. ((symbol-name ident) (symbol-name ident))
  14646. ((numberp ident) (number-to-string ident))
  14647. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14648. (case-fold-search nil))
  14649. (save-excursion
  14650. (save-restriction
  14651. (widen)
  14652. (goto-char (point-min))
  14653. (when (re-search-forward
  14654. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14655. nil t)
  14656. (org-back-to-heading t)
  14657. (point))))))
  14658. ;;;; Timestamps
  14659. (defvar org-last-changed-timestamp nil)
  14660. (defvar org-last-inserted-timestamp nil
  14661. "The last time stamp inserted with `org-insert-time-stamp'.")
  14662. (defvar org-ts-what) ; dynamically scoped parameter
  14663. (defun org-time-stamp (arg &optional inactive)
  14664. "Prompt for a date/time and insert a time stamp.
  14665. If the user specifies a time like HH:MM or if this command is
  14666. called with at least one prefix argument, the time stamp contains
  14667. the date and the time. Otherwise, only the date is be included.
  14668. All parts of a date not specified by the user is filled in from
  14669. the current date/time. So if you just press return without
  14670. typing anything, the time stamp will represent the current
  14671. date/time.
  14672. If there is already a timestamp at the cursor, it will be
  14673. modified.
  14674. With two universal prefix arguments, insert an active timestamp
  14675. with the current time without prompting the user.
  14676. When called from lisp, the timestamp is inactive if INACTIVE is
  14677. non-nil."
  14678. (interactive "P")
  14679. (let* ((ts nil)
  14680. (default-time
  14681. ;; Default time is either today, or, when entering a range,
  14682. ;; the range start.
  14683. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14684. (save-excursion
  14685. (re-search-backward
  14686. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14687. (- (point) 20) t)))
  14688. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14689. (current-time)))
  14690. (default-input (and ts (org-get-compact-tod ts)))
  14691. (repeater (save-excursion
  14692. (save-match-data
  14693. (beginning-of-line)
  14694. (when (re-search-forward
  14695. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14696. (save-excursion (progn (end-of-line) (point))) t)
  14697. (match-string 0)))))
  14698. org-time-was-given org-end-time-was-given time)
  14699. (cond
  14700. ((and (org-at-timestamp-p t)
  14701. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14702. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14703. (insert "--")
  14704. (setq time (let ((this-command this-command))
  14705. (org-read-date arg 'totime nil nil
  14706. default-time default-input inactive)))
  14707. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14708. ((org-at-timestamp-p t)
  14709. (setq time (let ((this-command this-command))
  14710. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14711. (when (org-at-timestamp-p t) ; just to get the match data
  14712. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14713. (replace-match "")
  14714. (setq org-last-changed-timestamp
  14715. (org-insert-time-stamp
  14716. time (or org-time-was-given arg)
  14717. inactive nil nil (list org-end-time-was-given)))
  14718. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14719. (setq org-last-changed-timestamp
  14720. (concat (substring org-last-inserted-timestamp 0 -1)
  14721. " " repeater ">"))))
  14722. (message "Timestamp updated"))
  14723. ((equal arg '(16))
  14724. (org-insert-time-stamp (current-time) t inactive))
  14725. (t
  14726. (setq time (let ((this-command this-command))
  14727. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14728. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14729. nil nil (list org-end-time-was-given))))))
  14730. ;; FIXME: can we use this for something else, like computing time differences?
  14731. (defun org-get-compact-tod (s)
  14732. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14733. (let* ((t1 (match-string 1 s))
  14734. (h1 (string-to-number (match-string 2 s)))
  14735. (m1 (string-to-number (match-string 3 s)))
  14736. (t2 (and (match-end 4) (match-string 5 s)))
  14737. (h2 (and t2 (string-to-number (match-string 6 s))))
  14738. (m2 (and t2 (string-to-number (match-string 7 s))))
  14739. dh dm)
  14740. (if (not t2)
  14741. t1
  14742. (setq dh (- h2 h1) dm (- m2 m1))
  14743. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14744. (concat t1 "+" (number-to-string dh)
  14745. (and (/= 0 dm) (format ":%02d" dm)))))))
  14746. (defun org-time-stamp-inactive (&optional arg)
  14747. "Insert an inactive time stamp.
  14748. An inactive time stamp is enclosed in square brackets instead of angle
  14749. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14750. does not link to the calendar and cannot be changed with the S-cursor keys.
  14751. So these are more for recording a certain time/date."
  14752. (interactive "P")
  14753. (org-time-stamp arg 'inactive))
  14754. (defvar org-date-ovl (make-overlay 1 1))
  14755. (overlay-put org-date-ovl 'face 'org-date-selected)
  14756. (org-detach-overlay org-date-ovl)
  14757. (defvar org-ans1) ; dynamically scoped parameter
  14758. (defvar org-ans2) ; dynamically scoped parameter
  14759. (defvar org-plain-time-of-day-regexp) ; defined below
  14760. (defvar org-overriding-default-time nil) ; dynamically scoped
  14761. (defvar org-read-date-overlay nil)
  14762. (defvar org-dcst nil) ; dynamically scoped
  14763. (defvar org-read-date-history nil)
  14764. (defvar org-read-date-final-answer nil)
  14765. (defvar org-read-date-analyze-futurep nil)
  14766. (defvar org-read-date-analyze-forced-year nil)
  14767. (defvar org-read-date-inactive)
  14768. (defvar org-read-date-minibuffer-local-map
  14769. (let* ((org-replace-disputed-keys nil)
  14770. (map (make-sparse-keymap)))
  14771. (set-keymap-parent map minibuffer-local-map)
  14772. (org-defkey map (kbd ".")
  14773. (lambda () (interactive)
  14774. ;; Are we at the beginning of the prompt?
  14775. (if (looking-back "^[^:]+: ")
  14776. (org-eval-in-calendar '(calendar-goto-today))
  14777. (insert "."))))
  14778. (org-defkey map (kbd "C-.")
  14779. (lambda () (interactive)
  14780. (org-eval-in-calendar '(calendar-goto-today))))
  14781. (org-defkey map [(meta shift left)]
  14782. (lambda () (interactive)
  14783. (org-eval-in-calendar '(calendar-backward-month 1))))
  14784. (org-defkey map [(meta shift right)]
  14785. (lambda () (interactive)
  14786. (org-eval-in-calendar '(calendar-forward-month 1))))
  14787. (org-defkey map [(meta shift up)]
  14788. (lambda () (interactive)
  14789. (org-eval-in-calendar '(calendar-backward-year 1))))
  14790. (org-defkey map [(meta shift down)]
  14791. (lambda () (interactive)
  14792. (org-eval-in-calendar '(calendar-forward-year 1))))
  14793. (org-defkey map [?\e (shift left)]
  14794. (lambda () (interactive)
  14795. (org-eval-in-calendar '(calendar-backward-month 1))))
  14796. (org-defkey map [?\e (shift right)]
  14797. (lambda () (interactive)
  14798. (org-eval-in-calendar '(calendar-forward-month 1))))
  14799. (org-defkey map [?\e (shift up)]
  14800. (lambda () (interactive)
  14801. (org-eval-in-calendar '(calendar-backward-year 1))))
  14802. (org-defkey map [?\e (shift down)]
  14803. (lambda () (interactive)
  14804. (org-eval-in-calendar '(calendar-forward-year 1))))
  14805. (org-defkey map [(shift up)]
  14806. (lambda () (interactive)
  14807. (org-eval-in-calendar '(calendar-backward-week 1))))
  14808. (org-defkey map [(shift down)]
  14809. (lambda () (interactive)
  14810. (org-eval-in-calendar '(calendar-forward-week 1))))
  14811. (org-defkey map [(shift left)]
  14812. (lambda () (interactive)
  14813. (org-eval-in-calendar '(calendar-backward-day 1))))
  14814. (org-defkey map [(shift right)]
  14815. (lambda () (interactive)
  14816. (org-eval-in-calendar '(calendar-forward-day 1))))
  14817. (org-defkey map "!"
  14818. (lambda () (interactive)
  14819. (org-eval-in-calendar '(diary-view-entries))
  14820. (message "")))
  14821. (org-defkey map ">"
  14822. (lambda () (interactive)
  14823. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14824. (org-defkey map "<"
  14825. (lambda () (interactive)
  14826. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14827. (org-defkey map "\C-v"
  14828. (lambda () (interactive)
  14829. (org-eval-in-calendar
  14830. '(calendar-scroll-left-three-months 1))))
  14831. (org-defkey map "\M-v"
  14832. (lambda () (interactive)
  14833. (org-eval-in-calendar
  14834. '(calendar-scroll-right-three-months 1))))
  14835. map)
  14836. "Keymap for minibuffer commands when using `org-read-date'.")
  14837. (defvar org-def)
  14838. (defvar org-defdecode)
  14839. (defvar org-with-time)
  14840. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14841. default-time default-input inactive)
  14842. "Read a date, possibly a time, and make things smooth for the user.
  14843. The prompt will suggest to enter an ISO date, but you can also enter anything
  14844. which will at least partially be understood by `parse-time-string'.
  14845. Unrecognized parts of the date will default to the current day, month, year,
  14846. hour and minute. If this command is called to replace a timestamp at point,
  14847. or to enter the second timestamp of a range, the default time is taken
  14848. from the existing stamp. Furthermore, the command prefers the future,
  14849. so if you are giving a date where the year is not given, and the day-month
  14850. combination is already past in the current year, it will assume you
  14851. mean next year. For details, see the manual. A few examples:
  14852. 3-2-5 --> 2003-02-05
  14853. feb 15 --> currentyear-02-15
  14854. 2/15 --> currentyear-02-15
  14855. sep 12 9 --> 2009-09-12
  14856. 12:45 --> today 12:45
  14857. 22 sept 0:34 --> currentyear-09-22 0:34
  14858. 12 --> currentyear-currentmonth-12
  14859. Fri --> nearest Friday after today
  14860. -Tue --> last Tuesday
  14861. etc.
  14862. Furthermore you can specify a relative date by giving, as the *first* thing
  14863. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14864. change in days weeks, months, years.
  14865. With a single plus or minus, the date is relative to today. With a double
  14866. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14867. +4d --> four days from today
  14868. +4 --> same as above
  14869. +2w --> two weeks from today
  14870. ++5 --> five days from default date
  14871. The function understands only English month and weekday abbreviations.
  14872. While prompting, a calendar is popped up - you can also select the
  14873. date with the mouse (button 1). The calendar shows a period of three
  14874. months. To scroll it to other months, use the keys `>' and `<'.
  14875. If you don't like the calendar, turn it off with
  14876. \(setq org-read-date-popup-calendar nil)
  14877. With optional argument TO-TIME, the date will immediately be converted
  14878. to an internal time.
  14879. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14880. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14881. still enter a time, and this function will inform the calling routine
  14882. about this change. The calling routine may then choose to change the
  14883. format used to insert the time stamp into the buffer to include the time.
  14884. With optional argument FROM-STRING, read from this string instead from
  14885. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14886. the time/date that is used for everything that is not specified by the
  14887. user."
  14888. (require 'parse-time)
  14889. (let* ((org-time-stamp-rounding-minutes
  14890. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14891. (org-dcst org-display-custom-times)
  14892. (ct (org-current-time))
  14893. (org-def (or org-overriding-default-time default-time ct))
  14894. (org-defdecode (decode-time org-def))
  14895. (dummy (progn
  14896. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14897. (setcar (nthcdr 2 org-defdecode) -1)
  14898. (setcar (nthcdr 1 org-defdecode) 59)
  14899. (setq org-def (apply 'encode-time org-defdecode)
  14900. org-defdecode (decode-time org-def)))))
  14901. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14902. (calendar-frame-setup nil)
  14903. (calendar-setup nil)
  14904. (calendar-move-hook nil)
  14905. (calendar-view-diary-initially-flag nil)
  14906. (calendar-view-holidays-initially-flag nil)
  14907. (timestr (format-time-string
  14908. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14909. (prompt (concat (if prompt (concat prompt " ") "")
  14910. (format "Date+time [%s]: " timestr)))
  14911. ans (org-ans0 "") org-ans1 org-ans2 final)
  14912. (cond
  14913. (from-string (setq ans from-string))
  14914. (org-read-date-popup-calendar
  14915. (save-excursion
  14916. (save-window-excursion
  14917. (calendar)
  14918. (org-eval-in-calendar '(setq cursor-type nil) t)
  14919. (unwind-protect
  14920. (progn
  14921. (calendar-forward-day (- (time-to-days org-def)
  14922. (calendar-absolute-from-gregorian
  14923. (calendar-current-date))))
  14924. (org-eval-in-calendar nil t)
  14925. (let* ((old-map (current-local-map))
  14926. (map (copy-keymap calendar-mode-map))
  14927. (minibuffer-local-map
  14928. (copy-keymap org-read-date-minibuffer-local-map)))
  14929. (org-defkey map (kbd "RET") 'org-calendar-select)
  14930. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14931. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14932. (unwind-protect
  14933. (progn
  14934. (use-local-map map)
  14935. (setq org-read-date-inactive inactive)
  14936. (add-hook 'post-command-hook 'org-read-date-display)
  14937. (setq org-ans0 (read-string prompt default-input
  14938. 'org-read-date-history nil))
  14939. ;; org-ans0: from prompt
  14940. ;; org-ans1: from mouse click
  14941. ;; org-ans2: from calendar motion
  14942. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14943. (remove-hook 'post-command-hook 'org-read-date-display)
  14944. (use-local-map old-map)
  14945. (when org-read-date-overlay
  14946. (delete-overlay org-read-date-overlay)
  14947. (setq org-read-date-overlay nil)))))
  14948. (bury-buffer "*Calendar*")))))
  14949. (t ; Naked prompt only
  14950. (unwind-protect
  14951. (setq ans (read-string prompt default-input
  14952. 'org-read-date-history timestr))
  14953. (when org-read-date-overlay
  14954. (delete-overlay org-read-date-overlay)
  14955. (setq org-read-date-overlay nil)))))
  14956. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14957. (when org-read-date-analyze-forced-year
  14958. (message "Year was forced into %s"
  14959. (if org-read-date-force-compatible-dates
  14960. "compatible range (1970-2037)"
  14961. "range representable on this machine"))
  14962. (ding))
  14963. ;; One round trip to get rid of 34th of August and stuff like that....
  14964. (setq final (decode-time (apply 'encode-time final)))
  14965. (setq org-read-date-final-answer ans)
  14966. (if to-time
  14967. (apply 'encode-time final)
  14968. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14969. (format "%04d-%02d-%02d %02d:%02d"
  14970. (nth 5 final) (nth 4 final) (nth 3 final)
  14971. (nth 2 final) (nth 1 final))
  14972. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14973. (defun org-read-date-display ()
  14974. "Display the current date prompt interpretation in the minibuffer."
  14975. (when org-read-date-display-live
  14976. (when org-read-date-overlay
  14977. (delete-overlay org-read-date-overlay))
  14978. (when (minibufferp (current-buffer))
  14979. (save-excursion
  14980. (end-of-line 1)
  14981. (while (not (equal (buffer-substring
  14982. (max (point-min) (- (point) 4)) (point))
  14983. " "))
  14984. (insert " ")))
  14985. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14986. " " (or org-ans1 org-ans2)))
  14987. (org-end-time-was-given nil)
  14988. (f (org-read-date-analyze ans org-def org-defdecode))
  14989. (fmts (if org-dcst
  14990. org-time-stamp-custom-formats
  14991. org-time-stamp-formats))
  14992. (fmt (if (or org-with-time
  14993. (and (boundp 'org-time-was-given) org-time-was-given))
  14994. (cdr fmts)
  14995. (car fmts)))
  14996. (txt (format-time-string fmt (apply 'encode-time f)))
  14997. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14998. (txt (concat "=> " txt)))
  14999. (when (and org-end-time-was-given
  15000. (string-match org-plain-time-of-day-regexp txt))
  15001. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15002. org-end-time-was-given
  15003. (substring txt (match-end 0)))))
  15004. (when org-read-date-analyze-futurep
  15005. (setq txt (concat txt " (=>F)")))
  15006. (setq org-read-date-overlay
  15007. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15008. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15009. (defun org-read-date-analyze (ans org-def org-defdecode)
  15010. "Analyze the combined answer of the date prompt."
  15011. ;; FIXME: cleanup and comment
  15012. (let ((nowdecode (decode-time (current-time)))
  15013. delta deltan deltaw deltadef year month day
  15014. hour minute second wday pm h2 m2 tl wday1
  15015. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15016. (setq org-read-date-analyze-futurep nil
  15017. org-read-date-analyze-forced-year nil)
  15018. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15019. (setq ans "+0"))
  15020. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15021. (setq ans (replace-match "" t t ans)
  15022. deltan (car delta)
  15023. deltaw (nth 1 delta)
  15024. deltadef (nth 2 delta)))
  15025. ;; Check if there is an iso week date in there. If yes, store the
  15026. ;; info and postpone interpreting it until the rest of the parsing
  15027. ;; is done.
  15028. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15029. (setq iso-year (if (match-end 1)
  15030. (org-small-year-to-year
  15031. (string-to-number (match-string 1 ans))))
  15032. iso-weekday (if (match-end 3)
  15033. (string-to-number (match-string 3 ans)))
  15034. iso-week (string-to-number (match-string 2 ans)))
  15035. (setq ans (replace-match "" t t ans)))
  15036. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15037. (when (string-match
  15038. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15039. (setq year (if (match-end 2)
  15040. (string-to-number (match-string 2 ans))
  15041. (progn (setq kill-year t)
  15042. (string-to-number (format-time-string "%Y"))))
  15043. month (string-to-number (match-string 3 ans))
  15044. day (string-to-number (match-string 4 ans)))
  15045. (if (< year 100) (setq year (+ 2000 year)))
  15046. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15047. t nil ans)))
  15048. ;; Help matching dotted european dates
  15049. (when (string-match
  15050. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15051. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15052. (setq kill-year t)
  15053. (string-to-number (format-time-string "%Y")))
  15054. day (string-to-number (match-string 1 ans))
  15055. month (string-to-number (match-string 2 ans))
  15056. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15057. t nil ans)))
  15058. ;; Help matching american dates, like 5/30 or 5/30/7
  15059. (when (string-match
  15060. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15061. (setq year (if (match-end 4)
  15062. (string-to-number (match-string 4 ans))
  15063. (progn (setq kill-year t)
  15064. (string-to-number (format-time-string "%Y"))))
  15065. month (string-to-number (match-string 1 ans))
  15066. day (string-to-number (match-string 2 ans)))
  15067. (if (< year 100) (setq year (+ 2000 year)))
  15068. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15069. t nil ans)))
  15070. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15071. ;; If there is a time with am/pm, and *no* time without it, we convert
  15072. ;; so that matching will be successful.
  15073. (loop for i from 1 to 2 do ; twice, for end time as well
  15074. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15075. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15076. (setq hour (string-to-number (match-string 1 ans))
  15077. minute (if (match-end 3)
  15078. (string-to-number (match-string 3 ans))
  15079. 0)
  15080. pm (equal ?p
  15081. (string-to-char (downcase (match-string 4 ans)))))
  15082. (if (and (= hour 12) (not pm))
  15083. (setq hour 0)
  15084. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15085. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15086. t t ans))))
  15087. ;; Check if a time range is given as a duration
  15088. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15089. (setq hour (string-to-number (match-string 1 ans))
  15090. h2 (+ hour (string-to-number (match-string 3 ans)))
  15091. minute (string-to-number (match-string 2 ans))
  15092. m2 (+ minute (if (match-end 5) (string-to-number
  15093. (match-string 5 ans))0)))
  15094. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15095. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15096. t t ans)))
  15097. ;; Check if there is a time range
  15098. (when (boundp 'org-end-time-was-given)
  15099. (setq org-time-was-given nil)
  15100. (when (and (string-match org-plain-time-of-day-regexp ans)
  15101. (match-end 8))
  15102. (setq org-end-time-was-given (match-string 8 ans))
  15103. (setq ans (concat (substring ans 0 (match-beginning 7))
  15104. (substring ans (match-end 7))))))
  15105. (setq tl (parse-time-string ans)
  15106. day (or (nth 3 tl) (nth 3 org-defdecode))
  15107. month (or (nth 4 tl)
  15108. (if (and org-read-date-prefer-future
  15109. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15110. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15111. (nth 4 org-defdecode)))
  15112. year (or (and (not kill-year) (nth 5 tl))
  15113. (if (and org-read-date-prefer-future
  15114. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15115. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15116. (nth 5 org-defdecode)))
  15117. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15118. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15119. second (or (nth 0 tl) 0)
  15120. wday (nth 6 tl))
  15121. (when (and (eq org-read-date-prefer-future 'time)
  15122. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15123. (equal day (nth 3 nowdecode))
  15124. (equal month (nth 4 nowdecode))
  15125. (equal year (nth 5 nowdecode))
  15126. (nth 2 tl)
  15127. (or (< (nth 2 tl) (nth 2 nowdecode))
  15128. (and (= (nth 2 tl) (nth 2 nowdecode))
  15129. (nth 1 tl)
  15130. (< (nth 1 tl) (nth 1 nowdecode)))))
  15131. (setq day (1+ day)
  15132. futurep t))
  15133. ;; Special date definitions below
  15134. (cond
  15135. (iso-week
  15136. ;; There was an iso week
  15137. (require 'cal-iso)
  15138. (setq futurep nil)
  15139. (setq year (or iso-year year)
  15140. day (or iso-weekday wday 1)
  15141. wday nil ; to make sure that the trigger below does not match
  15142. iso-date (calendar-gregorian-from-absolute
  15143. (calendar-absolute-from-iso
  15144. (list iso-week day year))))
  15145. ; FIXME: Should we also push ISO weeks into the future?
  15146. ; (when (and org-read-date-prefer-future
  15147. ; (not iso-year)
  15148. ; (< (calendar-absolute-from-gregorian iso-date)
  15149. ; (time-to-days (current-time))))
  15150. ; (setq year (1+ year)
  15151. ; iso-date (calendar-gregorian-from-absolute
  15152. ; (calendar-absolute-from-iso
  15153. ; (list iso-week day year)))))
  15154. (setq month (car iso-date)
  15155. year (nth 2 iso-date)
  15156. day (nth 1 iso-date)))
  15157. (deltan
  15158. (setq futurep nil)
  15159. (unless deltadef
  15160. (let ((now (decode-time (current-time))))
  15161. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15162. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15163. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15164. ((equal deltaw "m") (setq month (+ month deltan)))
  15165. ((equal deltaw "y") (setq year (+ year deltan)))))
  15166. ((and wday (not (nth 3 tl)))
  15167. ;; Weekday was given, but no day, so pick that day in the week
  15168. ;; on or after the derived date.
  15169. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15170. (unless (equal wday wday1)
  15171. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15172. (if (and (boundp 'org-time-was-given)
  15173. (nth 2 tl))
  15174. (setq org-time-was-given t))
  15175. (if (< year 100) (setq year (+ 2000 year)))
  15176. ;; Check of the date is representable
  15177. (if org-read-date-force-compatible-dates
  15178. (progn
  15179. (if (< year 1970)
  15180. (setq year 1970 org-read-date-analyze-forced-year t))
  15181. (if (> year 2037)
  15182. (setq year 2037 org-read-date-analyze-forced-year t)))
  15183. (condition-case nil
  15184. (ignore (encode-time second minute hour day month year))
  15185. (error
  15186. (setq year (nth 5 org-defdecode))
  15187. (setq org-read-date-analyze-forced-year t))))
  15188. (setq org-read-date-analyze-futurep futurep)
  15189. (list second minute hour day month year)))
  15190. (defvar parse-time-weekdays)
  15191. (defun org-read-date-get-relative (s today default)
  15192. "Check string S for special relative date string.
  15193. TODAY and DEFAULT are internal times, for today and for a default.
  15194. Return shift list (N what def-flag)
  15195. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15196. N is the number of WHATs to shift.
  15197. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15198. the DEFAULT date rather than TODAY."
  15199. (require 'parse-time)
  15200. (when (and
  15201. (string-match
  15202. (concat
  15203. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15204. "\\([0-9]+\\)?"
  15205. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15206. "\\([ \t]\\|$\\)") s)
  15207. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15208. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15209. (string-to-char (substring (match-string 1 s) -1))
  15210. ?+))
  15211. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15212. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15213. (what (if (match-end 3) (match-string 3 s) "d"))
  15214. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15215. (date (if rel default today))
  15216. (wday (nth 6 (decode-time date)))
  15217. delta)
  15218. (if wday1
  15219. (progn
  15220. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15221. (if (= delta 0) (setq delta 7))
  15222. (if (= dir ?-)
  15223. (progn
  15224. (setq delta (- delta 7))
  15225. (if (= delta 0) (setq delta -7))))
  15226. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15227. (list delta "d" rel))
  15228. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15229. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15230. "Turn a user-specified date into the internal representation.
  15231. The internal representation needed by the calendar is (month day year).
  15232. This is a wrapper to handle the brain-dead convention in calendar that
  15233. user function argument order change dependent on argument order."
  15234. (if (boundp 'calendar-date-style)
  15235. (cond
  15236. ((eq calendar-date-style 'american)
  15237. (list arg1 arg2 arg3))
  15238. ((eq calendar-date-style 'european)
  15239. (list arg2 arg1 arg3))
  15240. ((eq calendar-date-style 'iso)
  15241. (list arg2 arg3 arg1)))
  15242. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15243. (if (org-bound-and-true-p european-calendar-style)
  15244. (list arg2 arg1 arg3)
  15245. (list arg1 arg2 arg3)))))
  15246. (defun org-eval-in-calendar (form &optional keepdate)
  15247. "Eval FORM in the calendar window and return to current window.
  15248. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15249. otherwise stick to the current value of `org-ans2'."
  15250. (let ((sf (selected-frame))
  15251. (sw (selected-window)))
  15252. (select-window (get-buffer-window "*Calendar*" t))
  15253. (eval form)
  15254. (when (and (not keepdate) (calendar-cursor-to-date))
  15255. (let* ((date (calendar-cursor-to-date))
  15256. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15257. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15258. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15259. (select-window sw)
  15260. (org-select-frame-set-input-focus sf)))
  15261. (defun org-calendar-select ()
  15262. "Return to `org-read-date' with the date currently selected.
  15263. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15264. (interactive)
  15265. (when (calendar-cursor-to-date)
  15266. (let* ((date (calendar-cursor-to-date))
  15267. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15268. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15269. (if (active-minibuffer-window) (exit-minibuffer))))
  15270. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15271. "Insert a date stamp for the date given by the internal TIME.
  15272. WITH-HM means use the stamp format that includes the time of the day.
  15273. INACTIVE means use square brackets instead of angular ones, so that the
  15274. stamp will not contribute to the agenda.
  15275. PRE and POST are optional strings to be inserted before and after the
  15276. stamp.
  15277. The command returns the inserted time stamp."
  15278. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15279. stamp)
  15280. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15281. (insert-before-markers (or pre ""))
  15282. (when (listp extra)
  15283. (setq extra (car extra))
  15284. (if (and (stringp extra)
  15285. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15286. (setq extra (format "-%02d:%02d"
  15287. (string-to-number (match-string 1 extra))
  15288. (string-to-number (match-string 2 extra))))
  15289. (setq extra nil)))
  15290. (when extra
  15291. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15292. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15293. (insert-before-markers (or post ""))
  15294. (setq org-last-inserted-timestamp stamp)))
  15295. (defun org-toggle-time-stamp-overlays ()
  15296. "Toggle the use of custom time stamp formats."
  15297. (interactive)
  15298. (setq org-display-custom-times (not org-display-custom-times))
  15299. (unless org-display-custom-times
  15300. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15301. (while (setq p (next-single-property-change p 'display))
  15302. (if (and (get-text-property p 'display)
  15303. (eq (get-text-property p 'face) 'org-date))
  15304. (remove-text-properties
  15305. p (setq p (next-single-property-change p 'display))
  15306. '(display t))))
  15307. (set-buffer-modified-p bmp)))
  15308. (if (featurep 'xemacs)
  15309. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15310. (org-restart-font-lock)
  15311. (setq org-table-may-need-update t)
  15312. (if org-display-custom-times
  15313. (message "Time stamps are overlaid with custom format")
  15314. (message "Time stamp overlays removed")))
  15315. (defun org-display-custom-time (beg end)
  15316. "Overlay modified time stamp format over timestamp between BEG and END."
  15317. (let* ((ts (buffer-substring beg end))
  15318. t1 w1 with-hm tf time str w2 (off 0))
  15319. (save-match-data
  15320. (setq t1 (org-parse-time-string ts t))
  15321. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15322. (setq off (- (match-end 0) (match-beginning 0)))))
  15323. (setq end (- end off))
  15324. (setq w1 (- end beg)
  15325. with-hm (and (nth 1 t1) (nth 2 t1))
  15326. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15327. time (org-fix-decoded-time t1)
  15328. str (org-add-props
  15329. (format-time-string
  15330. (substring tf 1 -1) (apply 'encode-time time))
  15331. nil 'mouse-face 'highlight)
  15332. w2 (length str))
  15333. (if (not (= w2 w1))
  15334. (add-text-properties (1+ beg) (+ 2 beg)
  15335. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15336. (if (featurep 'xemacs)
  15337. (progn
  15338. (put-text-property beg end 'invisible t)
  15339. (put-text-property beg end 'end-glyph (make-glyph str)))
  15340. (put-text-property beg end 'display str))))
  15341. (defun org-translate-time (string)
  15342. "Translate all timestamps in STRING to custom format.
  15343. But do this only if the variable `org-display-custom-times' is set."
  15344. (when org-display-custom-times
  15345. (save-match-data
  15346. (let* ((start 0)
  15347. (re org-ts-regexp-both)
  15348. t1 with-hm inactive tf time str beg end)
  15349. (while (setq start (string-match re string start))
  15350. (setq beg (match-beginning 0)
  15351. end (match-end 0)
  15352. t1 (save-match-data
  15353. (org-parse-time-string (substring string beg end) t))
  15354. with-hm (and (nth 1 t1) (nth 2 t1))
  15355. inactive (equal (substring string beg (1+ beg)) "[")
  15356. tf (funcall (if with-hm 'cdr 'car)
  15357. org-time-stamp-custom-formats)
  15358. time (org-fix-decoded-time t1)
  15359. str (format-time-string
  15360. (concat
  15361. (if inactive "[" "<") (substring tf 1 -1)
  15362. (if inactive "]" ">"))
  15363. (apply 'encode-time time))
  15364. string (replace-match str t t string)
  15365. start (+ start (length str)))))))
  15366. string)
  15367. (defun org-fix-decoded-time (time)
  15368. "Set 0 instead of nil for the first 6 elements of time.
  15369. Don't touch the rest."
  15370. (let ((n 0))
  15371. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15372. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15373. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15374. "Difference between TIMESTAMP-STRING and now in days.
  15375. If SECONDS is non-nil, return the difference in seconds."
  15376. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15377. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15378. (funcall fdiff (current-time)))))
  15379. (defun org-deadline-close (timestamp-string &optional ndays)
  15380. "Is the time in TIMESTAMP-STRING close to the current date?"
  15381. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15382. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15383. (not (org-entry-is-done-p))))
  15384. (defun org-get-wdays (ts &optional delay zero-delay)
  15385. "Get the deadline lead time appropriate for timestring TS.
  15386. When DELAY is non-nil, get the delay time for scheduled items
  15387. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15388. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15389. don't try to find the delay cookie in the scheduled timestamp."
  15390. (let ((tv (if delay org-scheduled-delay-days
  15391. org-deadline-warning-days)))
  15392. (cond
  15393. ((or (and delay (< tv 0))
  15394. (and delay zero-delay (<= tv 0))
  15395. (and (not delay) (<= tv 0)))
  15396. ;; Enforce this value no matter what
  15397. (- tv))
  15398. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15399. ;; lead time is specified.
  15400. (floor (* (string-to-number (match-string 1 ts))
  15401. (cdr (assoc (match-string 2 ts)
  15402. '(("d" . 1) ("w" . 7)
  15403. ("m" . 30.4) ("y" . 365.25)
  15404. ("h" . 0.041667)))))))
  15405. ;; go for the default.
  15406. (t tv))))
  15407. (defun org-calendar-select-mouse (ev)
  15408. "Return to `org-read-date' with the date currently selected.
  15409. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15410. (interactive "e")
  15411. (mouse-set-point ev)
  15412. (when (calendar-cursor-to-date)
  15413. (let* ((date (calendar-cursor-to-date))
  15414. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15415. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15416. (if (active-minibuffer-window) (exit-minibuffer))))
  15417. (defun org-check-deadlines (ndays)
  15418. "Check if there are any deadlines due or past due.
  15419. A deadline is considered due if it happens within `org-deadline-warning-days'
  15420. days from today's date. If the deadline appears in an entry marked DONE,
  15421. it is not shown. The prefix arg NDAYS can be used to test that many
  15422. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15423. (interactive "P")
  15424. (let* ((org-warn-days
  15425. (cond
  15426. ((equal ndays '(4)) 100000)
  15427. (ndays (prefix-numeric-value ndays))
  15428. (t (abs org-deadline-warning-days))))
  15429. (case-fold-search nil)
  15430. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15431. (callback
  15432. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15433. (message "%d deadlines past-due or due within %d days"
  15434. (org-occur regexp nil callback)
  15435. org-warn-days)))
  15436. (defsubst org-re-timestamp (type)
  15437. "Return a regexp for timestamp TYPE.
  15438. Allowed values for TYPE are:
  15439. all: all timestamps
  15440. active: only active timestamps (<...>)
  15441. inactive: only inactive timestamps ([...])
  15442. scheduled: only scheduled timestamps
  15443. deadline: only deadline timestamps
  15444. closed: only closed time-stamps
  15445. When TYPE is nil, fall back on returning a regexp that matches
  15446. both scheduled and deadline timestamps."
  15447. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15448. ((eq type 'active) org-ts-regexp)
  15449. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15450. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15451. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15452. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15453. ((eq type 'scheduled-or-deadline)
  15454. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15455. (defun org-check-before-date (date)
  15456. "Check if there are deadlines or scheduled entries before DATE."
  15457. (interactive (list (org-read-date)))
  15458. (let ((case-fold-search nil)
  15459. (regexp (org-re-timestamp org-ts-type))
  15460. (callback
  15461. (lambda () (time-less-p
  15462. (org-time-string-to-time (match-string 1))
  15463. (org-time-string-to-time date)))))
  15464. (message "%d entries before %s"
  15465. (org-occur regexp nil callback) date)))
  15466. (defun org-check-after-date (date)
  15467. "Check if there are deadlines or scheduled entries after DATE."
  15468. (interactive (list (org-read-date)))
  15469. (let ((case-fold-search nil)
  15470. (regexp (org-re-timestamp org-ts-type))
  15471. (callback
  15472. (lambda () (not
  15473. (time-less-p
  15474. (org-time-string-to-time (match-string 1))
  15475. (org-time-string-to-time date))))))
  15476. (message "%d entries after %s"
  15477. (org-occur regexp nil callback) date)))
  15478. (defun org-check-dates-range (start-date end-date)
  15479. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15480. (interactive (list (org-read-date nil nil nil "Range starts")
  15481. (org-read-date nil nil nil "Range end")))
  15482. (let ((case-fold-search nil)
  15483. (regexp (org-re-timestamp org-ts-type))
  15484. (callback
  15485. (lambda ()
  15486. (let ((match (match-string 1)))
  15487. (and
  15488. (not (time-less-p
  15489. (org-time-string-to-time match)
  15490. (org-time-string-to-time start-date)))
  15491. (time-less-p
  15492. (org-time-string-to-time match)
  15493. (org-time-string-to-time end-date)))))))
  15494. (message "%d entries between %s and %s"
  15495. (org-occur regexp nil callback) start-date end-date)))
  15496. (defun org-evaluate-time-range (&optional to-buffer)
  15497. "Evaluate a time range by computing the difference between start and end.
  15498. Normally the result is just printed in the echo area, but with prefix arg
  15499. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15500. If the time range is actually in a table, the result is inserted into the
  15501. next column.
  15502. For time difference computation, a year is assumed to be exactly 365
  15503. days in order to avoid rounding problems."
  15504. (interactive "P")
  15505. (or
  15506. (org-clock-update-time-maybe)
  15507. (save-excursion
  15508. (unless (org-at-date-range-p t)
  15509. (goto-char (point-at-bol))
  15510. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15511. (if (not (org-at-date-range-p t))
  15512. (user-error "Not at a time-stamp range, and none found in current line")))
  15513. (let* ((ts1 (match-string 1))
  15514. (ts2 (match-string 2))
  15515. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15516. (match-end (match-end 0))
  15517. (time1 (org-time-string-to-time ts1))
  15518. (time2 (org-time-string-to-time ts2))
  15519. (t1 (org-float-time time1))
  15520. (t2 (org-float-time time2))
  15521. (diff (abs (- t2 t1)))
  15522. (negative (< (- t2 t1) 0))
  15523. ;; (ys (floor (* 365 24 60 60)))
  15524. (ds (* 24 60 60))
  15525. (hs (* 60 60))
  15526. (fy "%dy %dd %02d:%02d")
  15527. (fy1 "%dy %dd")
  15528. (fd "%dd %02d:%02d")
  15529. (fd1 "%dd")
  15530. (fh "%02d:%02d")
  15531. y d h m align)
  15532. (if havetime
  15533. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15534. y 0
  15535. d (floor (/ diff ds)) diff (mod diff ds)
  15536. h (floor (/ diff hs)) diff (mod diff hs)
  15537. m (floor (/ diff 60)))
  15538. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15539. y 0
  15540. d (floor (+ (/ diff ds) 0.5))
  15541. h 0 m 0))
  15542. (if (not to-buffer)
  15543. (message "%s" (org-make-tdiff-string y d h m))
  15544. (if (org-at-table-p)
  15545. (progn
  15546. (goto-char match-end)
  15547. (setq align t)
  15548. (and (looking-at " *|") (goto-char (match-end 0))))
  15549. (goto-char match-end))
  15550. (if (looking-at
  15551. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15552. (replace-match ""))
  15553. (if negative (insert " -"))
  15554. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15555. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15556. (insert " " (format fh h m))))
  15557. (if align (org-table-align))
  15558. (message "Time difference inserted")))))
  15559. (defun org-make-tdiff-string (y d h m)
  15560. (let ((fmt "")
  15561. (l nil))
  15562. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15563. l (push y l)))
  15564. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15565. l (push d l)))
  15566. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15567. l (push h l)))
  15568. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15569. l (push m l)))
  15570. (apply 'format fmt (nreverse l))))
  15571. (defun org-time-string-to-time (s &optional buffer pos)
  15572. "Convert a timestamp string into internal time."
  15573. (condition-case errdata
  15574. (apply 'encode-time (org-parse-time-string s))
  15575. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15576. s (if (not (and buffer pos))
  15577. ""
  15578. (format " at %d in buffer `%s'" pos buffer))
  15579. (cdr errdata)))))
  15580. (defun org-time-string-to-seconds (s)
  15581. "Convert a timestamp string to a number of seconds."
  15582. (org-float-time (org-time-string-to-time s)))
  15583. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15584. "Convert a time stamp to an absolute day number.
  15585. If there is a specifier for a cyclic time stamp, get the closest
  15586. date to DAYNR.
  15587. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15588. The variable `date' is bound by the calendar when this is called."
  15589. (cond
  15590. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15591. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15592. daynr
  15593. (+ daynr 1000)))
  15594. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15595. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15596. (time-to-days (current-time))) (match-string 0 s)
  15597. prefer show-all))
  15598. (t (time-to-days
  15599. (condition-case errdata
  15600. (apply 'encode-time (org-parse-time-string s))
  15601. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15602. s (if (not (and buffer pos))
  15603. ""
  15604. (format " at %d in buffer `%s'" pos buffer))
  15605. (cdr errdata))))))))
  15606. (defun org-days-to-iso-week (days)
  15607. "Return the iso week number."
  15608. (require 'cal-iso)
  15609. (car (calendar-iso-from-absolute days)))
  15610. (defun org-small-year-to-year (year)
  15611. "Convert 2-digit years into 4-digit years.
  15612. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15613. The year 2000 cannot be abbreviated. Any year larger than 99
  15614. is returned unchanged."
  15615. (if (< year 38)
  15616. (setq year (+ 2000 year))
  15617. (if (< year 100)
  15618. (setq year (+ 1900 year))))
  15619. year)
  15620. (defun org-time-from-absolute (d)
  15621. "Return the time corresponding to date D.
  15622. D may be an absolute day number, or a calendar-type list (month day year)."
  15623. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15624. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15625. (defun org-calendar-holiday ()
  15626. "List of holidays, for Diary display in Org-mode."
  15627. (require 'holidays)
  15628. (let ((hl (funcall
  15629. (if (fboundp 'calendar-check-holidays)
  15630. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15631. (if hl (mapconcat 'identity hl "; "))))
  15632. (defun org-diary-sexp-entry (sexp entry date)
  15633. "Process a SEXP diary ENTRY for DATE."
  15634. (require 'diary-lib)
  15635. (let ((result (if calendar-debug-sexp
  15636. (let ((stack-trace-on-error t))
  15637. (eval (car (read-from-string sexp))))
  15638. (condition-case nil
  15639. (eval (car (read-from-string sexp)))
  15640. (error
  15641. (beep)
  15642. (message "Bad sexp at line %d in %s: %s"
  15643. (org-current-line)
  15644. (buffer-file-name) sexp)
  15645. (sleep-for 2))))))
  15646. (cond ((stringp result) (split-string result "; "))
  15647. ((and (consp result)
  15648. (not (consp (cdr result)))
  15649. (stringp (cdr result))) (cdr result))
  15650. ((and (consp result)
  15651. (stringp (car result))) result)
  15652. (result entry))))
  15653. (defun org-diary-to-ical-string (frombuf)
  15654. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15655. This uses the icalendar.el library."
  15656. (let* ((tmpdir (if (featurep 'xemacs)
  15657. (temp-directory)
  15658. temporary-file-directory))
  15659. (tmpfile (make-temp-name
  15660. (expand-file-name "orgics" tmpdir)))
  15661. buf rtn b e)
  15662. (with-current-buffer frombuf
  15663. (icalendar-export-region (point-min) (point-max) tmpfile)
  15664. (setq buf (find-buffer-visiting tmpfile))
  15665. (set-buffer buf)
  15666. (goto-char (point-min))
  15667. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15668. (setq b (match-beginning 0)))
  15669. (goto-char (point-max))
  15670. (if (re-search-backward "^END:VEVENT" nil t)
  15671. (setq e (match-end 0)))
  15672. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15673. (kill-buffer buf)
  15674. (delete-file tmpfile)
  15675. rtn))
  15676. (defun org-closest-date (start current change prefer show-all)
  15677. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15678. When PREFER is `past', return a date that is either CURRENT or past.
  15679. When PREFER is `future', return a date that is either CURRENT or future.
  15680. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15681. ;; Make the proper lists from the dates
  15682. (catch 'exit
  15683. (let ((a1 '(("h" . hour)
  15684. ("d" . day)
  15685. ("w" . week)
  15686. ("m" . month)
  15687. ("y" . year)))
  15688. (shour (nth 2 (org-parse-time-string start)))
  15689. dn dw sday cday n1 n2 n0
  15690. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15691. (setq start (org-date-to-gregorian start)
  15692. current (org-date-to-gregorian
  15693. (if show-all
  15694. current
  15695. (time-to-days (current-time))))
  15696. sday (calendar-absolute-from-gregorian start)
  15697. cday (calendar-absolute-from-gregorian current))
  15698. (if (<= cday sday) (throw 'exit sday))
  15699. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15700. (setq dn (string-to-number (match-string 1 change))
  15701. dw (cdr (assoc (match-string 2 change) a1)))
  15702. (user-error "Invalid change specifier: %s" change))
  15703. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15704. (cond
  15705. ((eq dw 'hour)
  15706. (let ((missing-hours
  15707. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15708. dn)))
  15709. (setq n1 (if (zerop missing-hours) cday
  15710. (- cday (1+ (floor (/ missing-hours 24)))))
  15711. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15712. ((eq dw 'day)
  15713. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15714. n2 (+ n1 dn)))
  15715. ((eq dw 'year)
  15716. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15717. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15718. (setq date1 (list m d y1)
  15719. n1 (calendar-absolute-from-gregorian date1)
  15720. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15721. n2 (calendar-absolute-from-gregorian date2)))
  15722. ((eq dw 'month)
  15723. ;; approx number of month between the two dates
  15724. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15725. ;; How often does dn fit in there?
  15726. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15727. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15728. m (+ m nm)
  15729. ny (floor (/ m 12))
  15730. y (+ y ny)
  15731. m (- m (* ny 12)))
  15732. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15733. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15734. (setq m2 (+ m dn) y2 y)
  15735. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15736. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15737. (while (<= n2 cday)
  15738. (setq n1 n2 m m2 y y2)
  15739. (setq m2 (+ m dn) y2 y)
  15740. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15741. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15742. ;; Make sure n1 is the earlier date
  15743. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15744. (if show-all
  15745. (cond
  15746. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15747. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15748. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15749. (cond
  15750. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15751. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15752. (t (if (= cday n1) n1 n2)))))))
  15753. (defun org-date-to-gregorian (date)
  15754. "Turn any specification of DATE into a Gregorian date for the calendar."
  15755. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15756. ((and (listp date) (= (length date) 3)) date)
  15757. ((stringp date)
  15758. (setq date (org-parse-time-string date))
  15759. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15760. ((listp date)
  15761. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15762. (defun org-parse-time-string (s &optional nodefault)
  15763. "Parse the standard Org-mode time string.
  15764. This should be a lot faster than the normal `parse-time-string'.
  15765. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15766. hour and minute fields will be nil if not given."
  15767. (cond ((string-match org-ts-regexp0 s)
  15768. (list 0
  15769. (if (or (match-beginning 8) (not nodefault))
  15770. (string-to-number (or (match-string 8 s) "0")))
  15771. (if (or (match-beginning 7) (not nodefault))
  15772. (string-to-number (or (match-string 7 s) "0")))
  15773. (string-to-number (match-string 4 s))
  15774. (string-to-number (match-string 3 s))
  15775. (string-to-number (match-string 2 s))
  15776. nil nil nil))
  15777. ((string-match "^<[^>]+>$" s)
  15778. (decode-time (seconds-to-time (org-matcher-time s))))
  15779. (t (error "Not a standard Org-mode time string: %s" s))))
  15780. (defun org-timestamp-up (&optional arg)
  15781. "Increase the date item at the cursor by one.
  15782. If the cursor is on the year, change the year. If it is on the month,
  15783. the day or the time, change that.
  15784. With prefix ARG, change by that many units."
  15785. (interactive "p")
  15786. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15787. (defun org-timestamp-down (&optional arg)
  15788. "Decrease the date item at the cursor by one.
  15789. If the cursor is on the year, change the year. If it is on the month,
  15790. the day or the time, change that.
  15791. With prefix ARG, change by that many units."
  15792. (interactive "p")
  15793. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15794. (defun org-timestamp-up-day (&optional arg)
  15795. "Increase the date in the time stamp by one day.
  15796. With prefix ARG, change that many days."
  15797. (interactive "p")
  15798. (if (and (not (org-at-timestamp-p t))
  15799. (org-at-heading-p))
  15800. (org-todo 'up)
  15801. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15802. (defun org-timestamp-down-day (&optional arg)
  15803. "Decrease the date in the time stamp by one day.
  15804. With prefix ARG, change that many days."
  15805. (interactive "p")
  15806. (if (and (not (org-at-timestamp-p t))
  15807. (org-at-heading-p))
  15808. (org-todo 'down)
  15809. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15810. (defun org-at-timestamp-p (&optional inactive-ok)
  15811. "Determine if the cursor is in or at a timestamp."
  15812. (interactive)
  15813. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15814. (pos (point))
  15815. (ans (or (looking-at tsr)
  15816. (save-excursion
  15817. (skip-chars-backward "^[<\n\r\t")
  15818. (if (> (point) (point-min)) (backward-char 1))
  15819. (and (looking-at tsr)
  15820. (> (- (match-end 0) pos) -1))))))
  15821. (and ans
  15822. (boundp 'org-ts-what)
  15823. (setq org-ts-what
  15824. (cond
  15825. ((= pos (match-beginning 0)) 'bracket)
  15826. ;; Point is considered to be "on the bracket" whether
  15827. ;; it's really on it or right after it.
  15828. ((= pos (1- (match-end 0))) 'bracket)
  15829. ((= pos (match-end 0)) 'after)
  15830. ((org-pos-in-match-range pos 2) 'year)
  15831. ((org-pos-in-match-range pos 3) 'month)
  15832. ((org-pos-in-match-range pos 7) 'hour)
  15833. ((org-pos-in-match-range pos 8) 'minute)
  15834. ((or (org-pos-in-match-range pos 4)
  15835. (org-pos-in-match-range pos 5)) 'day)
  15836. ((and (> pos (or (match-end 8) (match-end 5)))
  15837. (< pos (match-end 0)))
  15838. (- pos (or (match-end 8) (match-end 5))))
  15839. (t 'day))))
  15840. ans))
  15841. (defun org-toggle-timestamp-type ()
  15842. "Toggle the type (<active> or [inactive]) of a time stamp."
  15843. (interactive)
  15844. (when (org-at-timestamp-p t)
  15845. (let ((beg (match-beginning 0)) (end (match-end 0))
  15846. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15847. (save-excursion
  15848. (goto-char beg)
  15849. (while (re-search-forward "[][<>]" end t)
  15850. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15851. t t)))
  15852. (message "Timestamp is now %sactive"
  15853. (if (equal (char-after beg) ?<) "" "in")))))
  15854. (defun org-at-clock-log-p nil
  15855. "Is the cursor on the clock log line?"
  15856. (save-excursion
  15857. (move-beginning-of-line 1)
  15858. (looking-at "^[ \t]*CLOCK:")))
  15859. (defvar org-clock-history) ; defined in org-clock.el
  15860. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15861. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15862. "Change the date in the time stamp at point.
  15863. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15864. `year', `minute', `second'. If WHAT is not given, the cursor position
  15865. in the timestamp determines what will be changed.
  15866. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15867. (let ((origin (point)) origin-cat
  15868. with-hm inactive
  15869. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15870. org-ts-what
  15871. extra rem
  15872. ts time time0 fixnext clrgx)
  15873. (if (not (org-at-timestamp-p t))
  15874. (user-error "Not at a timestamp"))
  15875. (if (and (not what) (eq org-ts-what 'bracket))
  15876. (org-toggle-timestamp-type)
  15877. ;; Point isn't on brackets. Remember the part of the time-stamp
  15878. ;; the point was in. Indeed, size of time-stamps may change,
  15879. ;; but point must be kept in the same category nonetheless.
  15880. (setq origin-cat org-ts-what)
  15881. (if (and (not what) (not (eq org-ts-what 'day))
  15882. org-display-custom-times
  15883. (get-text-property (point) 'display)
  15884. (not (get-text-property (1- (point)) 'display)))
  15885. (setq org-ts-what 'day))
  15886. (setq org-ts-what (or what org-ts-what)
  15887. inactive (= (char-after (match-beginning 0)) ?\[)
  15888. ts (match-string 0))
  15889. (replace-match "")
  15890. (when (string-match
  15891. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15892. ts)
  15893. (setq extra (match-string 1 ts))
  15894. (if suppress-tmp-delay
  15895. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15896. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15897. (setq with-hm t))
  15898. (setq time0 (org-parse-time-string ts))
  15899. (when (and updown
  15900. (eq org-ts-what 'minute)
  15901. (not current-prefix-arg))
  15902. ;; This looks like s-up and s-down. Change by one rounding step.
  15903. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15904. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15905. (setcar (cdr time0) (+ (nth 1 time0)
  15906. (if (> n 0) (- rem) (- dm rem))))))
  15907. (setq time
  15908. (encode-time (or (car time0) 0)
  15909. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15910. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15911. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15912. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15913. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15914. (nthcdr 6 time0)))
  15915. (when (and (member org-ts-what '(hour minute))
  15916. extra
  15917. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15918. (setq extra (org-modify-ts-extra
  15919. extra
  15920. (if (eq org-ts-what 'hour) 2 5)
  15921. n dm)))
  15922. (when (integerp org-ts-what)
  15923. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15924. (if (eq what 'calendar)
  15925. (let ((cal-date (org-get-date-from-calendar)))
  15926. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15927. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15928. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15929. (setcar time0 (or (car time0) 0))
  15930. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15931. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15932. (setq time (apply 'encode-time time0))))
  15933. ;; Insert the new time-stamp, and ensure point stays in the same
  15934. ;; category as before (i.e. not after the last position in that
  15935. ;; category).
  15936. (let ((pos (point)))
  15937. ;; Stay before inserted string. `save-excursion' is of no use.
  15938. (setq org-last-changed-timestamp
  15939. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15940. (goto-char pos))
  15941. (save-match-data
  15942. (looking-at org-ts-regexp3)
  15943. (goto-char (cond
  15944. ;; `day' category ends before `hour' if any, or at
  15945. ;; the end of the day name.
  15946. ((eq origin-cat 'day)
  15947. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15948. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15949. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15950. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15951. ;; `year' and `month' have both fixed size: point
  15952. ;; couldn't have moved into another part.
  15953. (t origin))))
  15954. ;; Update clock if on a CLOCK line.
  15955. (org-clock-update-time-maybe)
  15956. ;; Maybe adjust the closest clock in `org-clock-history'
  15957. (when org-clock-adjust-closest
  15958. (if (not (and (org-at-clock-log-p)
  15959. (< 1 (length (delq nil (mapcar 'marker-position
  15960. org-clock-history))))))
  15961. (message "No clock to adjust")
  15962. (cond ((save-excursion ; fix previous clock?
  15963. (re-search-backward org-ts-regexp0 nil t)
  15964. (org-looking-back (concat org-clock-string " \\[")))
  15965. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15966. ((save-excursion ; fix next clock?
  15967. (re-search-backward org-ts-regexp0 nil t)
  15968. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15969. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15970. (save-window-excursion
  15971. ;; Find closest clock to point, adjust the previous/next one in history
  15972. (let* ((p (save-excursion (org-back-to-heading t)))
  15973. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15974. (clfixnth
  15975. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15976. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15977. (if (not clfixpos)
  15978. (message "No clock to adjust")
  15979. (save-excursion
  15980. (org-goto-marker-or-bmk clfixpos)
  15981. (org-show-subtree)
  15982. (when (re-search-forward clrgx nil t)
  15983. (goto-char (match-beginning 1))
  15984. (let (org-clock-adjust-closest)
  15985. (org-timestamp-change n org-ts-what updown))
  15986. (message "Clock adjusted in %s for heading: %s"
  15987. (file-name-nondirectory (buffer-file-name))
  15988. (org-get-heading t t)))))))))
  15989. ;; Try to recenter the calendar window, if any.
  15990. (if (and org-calendar-follow-timestamp-change
  15991. (get-buffer-window "*Calendar*" t)
  15992. (memq org-ts-what '(day month year)))
  15993. (org-recenter-calendar (time-to-days time))))))
  15994. (defun org-modify-ts-extra (s pos n dm)
  15995. "Change the different parts of the lead-time and repeat fields in timestamp."
  15996. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15997. ng h m new rem)
  15998. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15999. (cond
  16000. ((or (org-pos-in-match-range pos 2)
  16001. (org-pos-in-match-range pos 3))
  16002. (setq m (string-to-number (match-string 3 s))
  16003. h (string-to-number (match-string 2 s)))
  16004. (if (org-pos-in-match-range pos 2)
  16005. (setq h (+ h n))
  16006. (setq n (* dm (org-no-warnings (signum n))))
  16007. (when (not (= 0 (setq rem (% m dm))))
  16008. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16009. (setq m (+ m n)))
  16010. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16011. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16012. (setq h (min 24 (max 0 h)))
  16013. (setq ng 1 new (format "-%02d:%02d" h m)))
  16014. ((org-pos-in-match-range pos 6)
  16015. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16016. ((org-pos-in-match-range pos 5)
  16017. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16018. ((org-pos-in-match-range pos 9)
  16019. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16020. ((org-pos-in-match-range pos 8)
  16021. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16022. (when ng
  16023. (setq s (concat
  16024. (substring s 0 (match-beginning ng))
  16025. new
  16026. (substring s (match-end ng))))))
  16027. s))
  16028. (defun org-recenter-calendar (date)
  16029. "If the calendar is visible, recenter it to DATE."
  16030. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16031. (when cwin
  16032. (let ((calendar-move-hook nil))
  16033. (with-selected-window cwin
  16034. (calendar-goto-date (if (listp date) date
  16035. (calendar-gregorian-from-absolute date))))))))
  16036. (defun org-goto-calendar (&optional arg)
  16037. "Go to the Emacs calendar at the current date.
  16038. If there is a time stamp in the current line, go to that date.
  16039. A prefix ARG can be used to force the current date."
  16040. (interactive "P")
  16041. (let ((tsr org-ts-regexp) diff
  16042. (calendar-move-hook nil)
  16043. (calendar-view-holidays-initially-flag nil)
  16044. (calendar-view-diary-initially-flag nil))
  16045. (if (or (org-at-timestamp-p)
  16046. (save-excursion
  16047. (beginning-of-line 1)
  16048. (looking-at (concat ".*" tsr))))
  16049. (let ((d1 (time-to-days (current-time)))
  16050. (d2 (time-to-days
  16051. (org-time-string-to-time (match-string 1)))))
  16052. (setq diff (- d2 d1))))
  16053. (calendar)
  16054. (calendar-goto-today)
  16055. (if (and diff (not arg)) (calendar-forward-day diff))))
  16056. (defun org-get-date-from-calendar ()
  16057. "Return a list (month day year) of date at point in calendar."
  16058. (with-current-buffer "*Calendar*"
  16059. (save-match-data
  16060. (calendar-cursor-to-date))))
  16061. (defun org-date-from-calendar ()
  16062. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16063. If there is already a time stamp at the cursor position, update it."
  16064. (interactive)
  16065. (if (org-at-timestamp-p t)
  16066. (org-timestamp-change 0 'calendar)
  16067. (let ((cal-date (org-get-date-from-calendar)))
  16068. (org-insert-time-stamp
  16069. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16070. (defcustom org-effort-durations
  16071. `(("h" . 60)
  16072. ("d" . ,(* 60 8))
  16073. ("w" . ,(* 60 8 5))
  16074. ("m" . ,(* 60 8 5 4))
  16075. ("y" . ,(* 60 8 5 40)))
  16076. "Conversion factor to minutes for an effort modifier.
  16077. Each entry has the form (MODIFIER . MINUTES).
  16078. In an effort string, a number followed by MODIFIER is multiplied
  16079. by the specified number of MINUTES to obtain an effort in
  16080. minutes.
  16081. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16082. effort string \"2hours\" is equivalent to 120 minutes."
  16083. :group 'org-agenda
  16084. :version "24.1"
  16085. :type '(alist :key-type (string :tag "Modifier")
  16086. :value-type (number :tag "Minutes")))
  16087. (defun org-minutes-to-clocksum-string (m)
  16088. "Format number of minutes as a clocksum string.
  16089. The format is determined by `org-time-clocksum-format',
  16090. `org-time-clocksum-use-fractional' and
  16091. `org-time-clocksum-fractional-format' and
  16092. `org-time-clocksum-use-effort-durations'."
  16093. (let ((clocksum "")
  16094. (m (round m)) ; Don't allow fractions of minutes
  16095. h d w mo y fmt n)
  16096. (setq h (if org-time-clocksum-use-effort-durations
  16097. (cdr (assoc "h" org-effort-durations)) 60)
  16098. d (if org-time-clocksum-use-effort-durations
  16099. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16100. w (if org-time-clocksum-use-effort-durations
  16101. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16102. mo (if org-time-clocksum-use-effort-durations
  16103. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16104. y (if org-time-clocksum-use-effort-durations
  16105. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16106. ;; fractional format
  16107. (if org-time-clocksum-use-fractional
  16108. (cond
  16109. ;; single format string
  16110. ((stringp org-time-clocksum-fractional-format)
  16111. (format org-time-clocksum-fractional-format (/ m (float h))))
  16112. ;; choice of fractional formats for different time units
  16113. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16114. (> (/ (truncate m) (* y d h)) 0))
  16115. (format fmt (/ m (* y d (float h)))))
  16116. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16117. (> (/ (truncate m) (* mo d h)) 0))
  16118. (format fmt (/ m (* mo d (float h)))))
  16119. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16120. (> (/ (truncate m) (* w d h)) 0))
  16121. (format fmt (/ m (* w d (float h)))))
  16122. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16123. (> (/ (truncate m) (* d h)) 0))
  16124. (format fmt (/ m (* d (float h)))))
  16125. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16126. (> (/ (truncate m) h) 0))
  16127. (format fmt (/ m (float h))))
  16128. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16129. (format fmt m))
  16130. ;; fall back to smallest time unit with a format
  16131. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16132. (format fmt (/ m (float h))))
  16133. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16134. (format fmt (/ m (* d (float h)))))
  16135. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16136. (format fmt (/ m (* w d (float h)))))
  16137. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16138. (format fmt (/ m (* mo d (float h)))))
  16139. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16140. (format fmt (/ m (* y d (float h))))))
  16141. ;; standard (non-fractional) format, with single format string
  16142. (if (stringp org-time-clocksum-format)
  16143. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16144. ;; separate formats components
  16145. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16146. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16147. (plist-get org-time-clocksum-format :require-years))
  16148. (setq clocksum (concat clocksum (format fmt n))
  16149. m (- m (* n y d h))))
  16150. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16151. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16152. (plist-get org-time-clocksum-format :require-months))
  16153. (setq clocksum (concat clocksum (format fmt n))
  16154. m (- m (* n mo d h))))
  16155. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16156. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16157. (plist-get org-time-clocksum-format :require-weeks))
  16158. (setq clocksum (concat clocksum (format fmt n))
  16159. m (- m (* n w d h))))
  16160. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16161. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16162. (plist-get org-time-clocksum-format :require-days))
  16163. (setq clocksum (concat clocksum (format fmt n))
  16164. m (- m (* n d h))))
  16165. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16166. (or (> (setq n (/ (truncate m) h)) 0)
  16167. (plist-get org-time-clocksum-format :require-hours))
  16168. (setq clocksum (concat clocksum (format fmt n))
  16169. m (- m (* n h))))
  16170. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16171. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16172. (setq clocksum (concat clocksum (format fmt m))))
  16173. ;; return formatted time duration
  16174. clocksum))))
  16175. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16176. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16177. "Org mode version 8.0")
  16178. (defun org-hours-to-clocksum-string (n)
  16179. (org-minutes-to-clocksum-string (* n 60)))
  16180. (defun org-hh:mm-string-to-minutes (s)
  16181. "Convert a string H:MM to a number of minutes.
  16182. If the string is just a number, interpret it as minutes.
  16183. In fact, the first hh:mm or number in the string will be taken,
  16184. there can be extra stuff in the string.
  16185. If no number is found, the return value is 0."
  16186. (cond
  16187. ((integerp s) s)
  16188. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16189. (+ (* (string-to-number (match-string 1 s)) 60)
  16190. (string-to-number (match-string 2 s))))
  16191. ((string-match "\\([0-9]+\\)" s)
  16192. (string-to-number (match-string 1 s)))
  16193. (t 0)))
  16194. (defcustom org-image-actual-width t
  16195. "Should we use the actual width of images when inlining them?
  16196. When set to `t', always use the image width.
  16197. When set to a number, use imagemagick (when available) to set
  16198. the image's width to this value.
  16199. When set to a number in a list, try to get the width from any
  16200. #+ATTR.* keyword if it matches a width specification like
  16201. #+ATTR_HTML: :width 300px
  16202. and fall back on that number if none is found.
  16203. When set to nil, try to get the width from an #+ATTR.* keyword
  16204. and fall back on the original width if none is found.
  16205. This requires Emacs >= 24.1, build with imagemagick support."
  16206. :group 'org-appearance
  16207. :version "24.4"
  16208. :package-version '(Org . "8.0")
  16209. :type '(choice
  16210. (const :tag "Use the image width" t)
  16211. (integer :tag "Use a number of pixels")
  16212. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16213. (const :tag "Use #+ATTR* or don't resize" nil)))
  16214. (defcustom org-agenda-inhibit-startup nil
  16215. "Inhibit startup when preparing agenda buffers.
  16216. When this variable is `t' (the default), the initialization of
  16217. the Org agenda buffers is inhibited: e.g. the visibility state
  16218. is not set, the tables are not re-aligned, etc."
  16219. :type 'boolean
  16220. :version "24.3"
  16221. :group 'org-agenda)
  16222. (defcustom org-agenda-ignore-drawer-properties nil
  16223. "Avoid updating text properties when building the agenda.
  16224. Properties are used to prepare buffers for effort estimates, appointments,
  16225. and subtree-local categories.
  16226. If you don't use these in the agenda, you can add them to this list and
  16227. agenda building will be a bit faster.
  16228. The value is a list, with zero or more of the symbols `effort', `appt',
  16229. or `category'."
  16230. :type '(set :greedy t
  16231. (const effort)
  16232. (const appt)
  16233. (const category))
  16234. :version "24.3"
  16235. :group 'org-agenda)
  16236. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16237. "Convert a duration string S to minutes.
  16238. A bare number is interpreted as minutes, modifiers can be set by
  16239. customizing `org-effort-durations' (which see).
  16240. Entries containing a colon are interpreted as H:MM by
  16241. `org-hh:mm-string-to-minutes'."
  16242. (let ((result 0)
  16243. (re (concat "\\([0-9.]+\\) *\\("
  16244. (regexp-opt (mapcar 'car org-effort-durations))
  16245. "\\)")))
  16246. (while (string-match re s)
  16247. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16248. (string-to-number (match-string 1 s))))
  16249. (setq s (replace-match "" nil t s)))
  16250. (setq result (floor result))
  16251. (incf result (org-hh:mm-string-to-minutes s))
  16252. (if output-to-string (number-to-string result) result)))
  16253. ;;;; Files
  16254. (defun org-save-all-org-buffers ()
  16255. "Save all Org-mode buffers without user confirmation."
  16256. (interactive)
  16257. (message "Saving all Org-mode buffers...")
  16258. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16259. (when (featurep 'org-id) (org-id-locations-save))
  16260. (message "Saving all Org-mode buffers... done"))
  16261. (defun org-revert-all-org-buffers ()
  16262. "Revert all Org-mode buffers.
  16263. Prompt for confirmation when there are unsaved changes.
  16264. Be sure you know what you are doing before letting this function
  16265. overwrite your changes.
  16266. This function is useful in a setup where one tracks org files
  16267. with a version control system, to revert on one machine after pulling
  16268. changes from another. I believe the procedure must be like this:
  16269. 1. M-x org-save-all-org-buffers
  16270. 2. Pull changes from the other machine, resolve conflicts
  16271. 3. M-x org-revert-all-org-buffers"
  16272. (interactive)
  16273. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16274. (user-error "Abort"))
  16275. (save-excursion
  16276. (save-window-excursion
  16277. (mapc
  16278. (lambda (b)
  16279. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16280. (with-current-buffer b buffer-file-name))
  16281. (org-pop-to-buffer-same-window b)
  16282. (revert-buffer t 'no-confirm)))
  16283. (buffer-list))
  16284. (when (and (featurep 'org-id) org-id-track-globally)
  16285. (org-id-locations-load)))))
  16286. ;;;; Agenda files
  16287. ;;;###autoload
  16288. (defun org-switchb (&optional arg)
  16289. "Switch between Org buffers.
  16290. With one prefix argument, restrict available buffers to files.
  16291. With two prefix arguments, restrict available buffers to agenda files.
  16292. Defaults to `iswitchb' for buffer name completion.
  16293. Set `org-completion-use-ido' to make it use ido instead."
  16294. (interactive "P")
  16295. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16296. ((equal arg '(16)) (org-buffer-list 'agenda))
  16297. (t (org-buffer-list))))
  16298. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16299. (org-completion-use-ido org-completion-use-ido))
  16300. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16301. (setq org-completion-use-iswitchb t))
  16302. (org-pop-to-buffer-same-window
  16303. (org-icompleting-read "Org buffer: "
  16304. (mapcar 'list (mapcar 'buffer-name blist))
  16305. nil t))))
  16306. ;;; Define some older names previously used for this functionality
  16307. ;;;###autoload
  16308. (defalias 'org-ido-switchb 'org-switchb)
  16309. ;;;###autoload
  16310. (defalias 'org-iswitchb 'org-switchb)
  16311. (defun org-buffer-list (&optional predicate exclude-tmp)
  16312. "Return a list of Org buffers.
  16313. PREDICATE can be `export', `files' or `agenda'.
  16314. export restrict the list to Export buffers.
  16315. files restrict the list to buffers visiting Org files.
  16316. agenda restrict the list to buffers visiting agenda files.
  16317. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16318. (let* ((bfn nil)
  16319. (agenda-files (and (eq predicate 'agenda)
  16320. (mapcar 'file-truename (org-agenda-files t))))
  16321. (filter
  16322. (cond
  16323. ((eq predicate 'files)
  16324. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16325. ((eq predicate 'export)
  16326. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16327. ((eq predicate 'agenda)
  16328. (lambda (b)
  16329. (with-current-buffer b
  16330. (and (derived-mode-p 'org-mode)
  16331. (setq bfn (buffer-file-name b))
  16332. (member (file-truename bfn) agenda-files)))))
  16333. (t (lambda (b) (with-current-buffer b
  16334. (or (derived-mode-p 'org-mode)
  16335. (string-match "\*Org .*Export"
  16336. (buffer-name b)))))))))
  16337. (delq nil
  16338. (mapcar
  16339. (lambda(b)
  16340. (if (and (funcall filter b)
  16341. (or (not exclude-tmp)
  16342. (not (string-match "tmp" (buffer-name b)))))
  16343. b
  16344. nil))
  16345. (buffer-list)))))
  16346. (defun org-agenda-files (&optional unrestricted archives)
  16347. "Get the list of agenda files.
  16348. Optional UNRESTRICTED means return the full list even if a restriction
  16349. is currently in place.
  16350. When ARCHIVES is t, include all archive files that are really being
  16351. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16352. `org-agenda-archives-mode' is t."
  16353. (let ((files
  16354. (cond
  16355. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16356. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16357. ((listp org-agenda-files) org-agenda-files)
  16358. (t (error "Invalid value of `org-agenda-files'")))))
  16359. (setq files (apply 'append
  16360. (mapcar (lambda (f)
  16361. (if (file-directory-p f)
  16362. (directory-files
  16363. f t org-agenda-file-regexp)
  16364. (list f)))
  16365. files)))
  16366. (when org-agenda-skip-unavailable-files
  16367. (setq files (delq nil
  16368. (mapcar (function
  16369. (lambda (file)
  16370. (and (file-readable-p file) file)))
  16371. files))))
  16372. (when (or (eq archives t)
  16373. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16374. (setq files (org-add-archive-files files)))
  16375. files))
  16376. (defun org-agenda-file-p (&optional file)
  16377. "Return non-nil, if FILE is an agenda file.
  16378. If FILE is omitted, use the file associated with the current
  16379. buffer."
  16380. (member (or file (buffer-file-name))
  16381. (org-agenda-files t)))
  16382. (defun org-edit-agenda-file-list ()
  16383. "Edit the list of agenda files.
  16384. Depending on setup, this either uses customize to edit the variable
  16385. `org-agenda-files', or it visits the file that is holding the list. In the
  16386. latter case, the buffer is set up in a way that saving it automatically kills
  16387. the buffer and restores the previous window configuration."
  16388. (interactive)
  16389. (if (stringp org-agenda-files)
  16390. (let ((cw (current-window-configuration)))
  16391. (find-file org-agenda-files)
  16392. (org-set-local 'org-window-configuration cw)
  16393. (org-add-hook 'after-save-hook
  16394. (lambda ()
  16395. (set-window-configuration
  16396. (prog1 org-window-configuration
  16397. (kill-buffer (current-buffer))))
  16398. (org-install-agenda-files-menu)
  16399. (message "New agenda file list installed"))
  16400. nil 'local)
  16401. (message "%s" (substitute-command-keys
  16402. "Edit list and finish with \\[save-buffer]")))
  16403. (customize-variable 'org-agenda-files)))
  16404. (defun org-store-new-agenda-file-list (list)
  16405. "Set new value for the agenda file list and save it correctly."
  16406. (if (stringp org-agenda-files)
  16407. (let ((fe (org-read-agenda-file-list t)) b u)
  16408. (while (setq b (find-buffer-visiting org-agenda-files))
  16409. (kill-buffer b))
  16410. (with-temp-file org-agenda-files
  16411. (insert
  16412. (mapconcat
  16413. (lambda (f) ;; Keep un-expanded entries.
  16414. (if (setq u (assoc f fe))
  16415. (cdr u)
  16416. f))
  16417. list "\n")
  16418. "\n")))
  16419. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16420. (org-insert-mode-line-in-empty-file nil))
  16421. (setq org-agenda-files list)
  16422. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16423. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16424. "Read the list of agenda files from a file.
  16425. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16426. filenames, used by `org-store-new-agenda-file-list' to write back
  16427. un-expanded file names."
  16428. (when (file-directory-p org-agenda-files)
  16429. (error "`org-agenda-files' cannot be a single directory"))
  16430. (when (stringp org-agenda-files)
  16431. (with-temp-buffer
  16432. (insert-file-contents org-agenda-files)
  16433. (mapcar
  16434. (lambda (f)
  16435. (let ((e (expand-file-name (substitute-in-file-name f)
  16436. org-directory)))
  16437. (if pair-with-expansion
  16438. (cons e f)
  16439. e)))
  16440. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16441. ;;;###autoload
  16442. (defun org-cycle-agenda-files ()
  16443. "Cycle through the files in `org-agenda-files'.
  16444. If the current buffer visits an agenda file, find the next one in the list.
  16445. If the current buffer does not, find the first agenda file."
  16446. (interactive)
  16447. (let* ((fs (org-agenda-files t))
  16448. (files (append fs (list (car fs))))
  16449. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16450. file)
  16451. (unless files (user-error "No agenda files"))
  16452. (catch 'exit
  16453. (while (setq file (pop files))
  16454. (if (equal (file-truename file) tcf)
  16455. (when (car files)
  16456. (find-file (car files))
  16457. (throw 'exit t))))
  16458. (find-file (car fs)))
  16459. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16460. (defun org-agenda-file-to-front (&optional to-end)
  16461. "Move/add the current file to the top of the agenda file list.
  16462. If the file is not present in the list, it is added to the front. If it is
  16463. present, it is moved there. With optional argument TO-END, add/move to the
  16464. end of the list."
  16465. (interactive "P")
  16466. (let ((org-agenda-skip-unavailable-files nil)
  16467. (file-alist (mapcar (lambda (x)
  16468. (cons (file-truename x) x))
  16469. (org-agenda-files t)))
  16470. (ctf (file-truename
  16471. (or buffer-file-name
  16472. (user-error "Please save the current buffer to a file"))))
  16473. x had)
  16474. (setq x (assoc ctf file-alist) had x)
  16475. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16476. (if to-end
  16477. (setq file-alist (append (delq x file-alist) (list x)))
  16478. (setq file-alist (cons x (delq x file-alist))))
  16479. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16480. (org-install-agenda-files-menu)
  16481. (message "File %s to %s of agenda file list"
  16482. (if had "moved" "added") (if to-end "end" "front"))))
  16483. (defun org-remove-file (&optional file)
  16484. "Remove current file from the list of files in variable `org-agenda-files'.
  16485. These are the files which are being checked for agenda entries.
  16486. Optional argument FILE means use this file instead of the current."
  16487. (interactive)
  16488. (let* ((org-agenda-skip-unavailable-files nil)
  16489. (file (or file buffer-file-name
  16490. (user-error "Current buffer does not visit a file")))
  16491. (true-file (file-truename file))
  16492. (afile (abbreviate-file-name file))
  16493. (files (delq nil (mapcar
  16494. (lambda (x)
  16495. (if (equal true-file
  16496. (file-truename x))
  16497. nil x))
  16498. (org-agenda-files t)))))
  16499. (if (not (= (length files) (length (org-agenda-files t))))
  16500. (progn
  16501. (org-store-new-agenda-file-list files)
  16502. (org-install-agenda-files-menu)
  16503. (message "Removed file: %s" afile))
  16504. (message "File was not in list: %s (not removed)" afile))))
  16505. (defun org-file-menu-entry (file)
  16506. (vector file (list 'find-file file) t))
  16507. (defun org-check-agenda-file (file)
  16508. "Make sure FILE exists. If not, ask user what to do."
  16509. (when (not (file-exists-p file))
  16510. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16511. (abbreviate-file-name file))
  16512. (let ((r (downcase (read-char-exclusive))))
  16513. (cond
  16514. ((equal r ?r)
  16515. (org-remove-file file)
  16516. (throw 'nextfile t))
  16517. (t (error "Abort"))))))
  16518. (defun org-get-agenda-file-buffer (file)
  16519. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16520. it to the list of buffers which might be released later."
  16521. (let ((buf (org-find-base-buffer-visiting file)))
  16522. (if buf
  16523. buf ; just return it
  16524. ;; Make a new buffer and remember it
  16525. (setq buf (find-file-noselect file))
  16526. (if buf (push buf org-agenda-new-buffers))
  16527. buf)))
  16528. (defun org-release-buffers (blist)
  16529. "Release all buffers in list, asking the user for confirmation when needed.
  16530. When a buffer is unmodified, it is just killed. When modified, it is saved
  16531. \(if the user agrees) and then killed."
  16532. (let (buf file)
  16533. (while (setq buf (pop blist))
  16534. (setq file (buffer-file-name buf))
  16535. (when (and (buffer-modified-p buf)
  16536. file
  16537. (y-or-n-p (format "Save file %s? " file)))
  16538. (with-current-buffer buf (save-buffer)))
  16539. (kill-buffer buf))))
  16540. (defun org-agenda-prepare-buffers (files)
  16541. "Create buffers for all agenda files, protect archived trees and comments."
  16542. (interactive)
  16543. (let ((pa '(:org-archived t))
  16544. (pc '(:org-comment t))
  16545. (pall '(:org-archived t :org-comment t))
  16546. (inhibit-read-only t)
  16547. (org-inhibit-startup org-agenda-inhibit-startup)
  16548. (rea (concat ":" org-archive-tag ":"))
  16549. file re pos)
  16550. (setq org-tag-alist-for-agenda nil
  16551. org-tag-groups-alist-for-agenda nil)
  16552. (save-window-excursion
  16553. (save-restriction
  16554. (while (setq file (pop files))
  16555. (catch 'nextfile
  16556. (if (bufferp file)
  16557. (set-buffer file)
  16558. (org-check-agenda-file file)
  16559. (set-buffer (org-get-agenda-file-buffer file)))
  16560. (widen)
  16561. (org-set-regexps-and-options-for-tags)
  16562. (setq pos (point))
  16563. (goto-char (point-min))
  16564. (let ((case-fold-search t))
  16565. (when (search-forward "#+setupfile" nil t)
  16566. ;; Don't set all regexps and options systematically as
  16567. ;; this is only run for setting agenda tags from setup
  16568. ;; file
  16569. (org-set-regexps-and-options)))
  16570. (or (memq 'category org-agenda-ignore-drawer-properties)
  16571. (org-refresh-category-properties))
  16572. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16573. (org-refresh-properties org-effort-property 'org-effort))
  16574. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16575. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16576. (setq org-todo-keywords-for-agenda
  16577. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16578. (setq org-done-keywords-for-agenda
  16579. (append org-done-keywords-for-agenda org-done-keywords))
  16580. (setq org-todo-keyword-alist-for-agenda
  16581. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16582. (setq org-drawers-for-agenda
  16583. (append org-drawers-for-agenda org-drawers))
  16584. (setq org-tag-alist-for-agenda
  16585. (org-uniquify
  16586. (append org-tag-alist-for-agenda
  16587. org-tag-alist
  16588. org-tag-persistent-alist)))
  16589. (if org-group-tags
  16590. (setq org-tag-groups-alist-for-agenda
  16591. (org-uniquify-alist
  16592. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16593. (org-with-silent-modifications
  16594. (save-excursion
  16595. (remove-text-properties (point-min) (point-max) pall)
  16596. (when org-agenda-skip-archived-trees
  16597. (goto-char (point-min))
  16598. (while (re-search-forward rea nil t)
  16599. (if (org-at-heading-p t)
  16600. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16601. (goto-char (point-min))
  16602. (setq re (format org-heading-keyword-regexp-format
  16603. org-comment-string))
  16604. (while (re-search-forward re nil t)
  16605. (add-text-properties
  16606. (match-beginning 0) (org-end-of-subtree t) pc))))
  16607. (goto-char pos)))))
  16608. (setq org-todo-keywords-for-agenda
  16609. (org-uniquify org-todo-keywords-for-agenda))
  16610. (setq org-todo-keyword-alist-for-agenda
  16611. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16612. ;;;; CDLaTeX minor mode
  16613. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16614. "Keymap for the minor `org-cdlatex-mode'.")
  16615. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16616. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16617. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16618. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16619. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16620. (defvar org-cdlatex-texmathp-advice-is-done nil
  16621. "Flag remembering if we have applied the advice to texmathp already.")
  16622. (define-minor-mode org-cdlatex-mode
  16623. "Toggle the minor `org-cdlatex-mode'.
  16624. This mode supports entering LaTeX environment and math in LaTeX fragments
  16625. in Org-mode.
  16626. \\{org-cdlatex-mode-map}"
  16627. nil " OCDL" nil
  16628. (when org-cdlatex-mode
  16629. (require 'cdlatex)
  16630. (run-hooks 'cdlatex-mode-hook)
  16631. (cdlatex-compute-tables))
  16632. (unless org-cdlatex-texmathp-advice-is-done
  16633. (setq org-cdlatex-texmathp-advice-is-done t)
  16634. (defadvice texmathp (around org-math-always-on activate)
  16635. "Always return t in org-mode buffers.
  16636. This is because we want to insert math symbols without dollars even outside
  16637. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16638. an embedded LaTeX fragment, let texmathp do its job.
  16639. \\[org-cdlatex-mode-map]"
  16640. (interactive)
  16641. (let (p)
  16642. (cond
  16643. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16644. ((eq this-command 'cdlatex-math-symbol)
  16645. (setq ad-return-value t
  16646. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16647. (t
  16648. (let ((p (org-inside-LaTeX-fragment-p)))
  16649. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16650. (setq ad-return-value t
  16651. texmathp-why '("Org-mode embedded math" . 0))
  16652. (if p ad-do-it)))))))))
  16653. (defun turn-on-org-cdlatex ()
  16654. "Unconditionally turn on `org-cdlatex-mode'."
  16655. (org-cdlatex-mode 1))
  16656. (defun org-try-cdlatex-tab ()
  16657. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16658. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16659. - inside a LaTeX fragment, or
  16660. - after the first word in a line, where an abbreviation expansion could
  16661. insert a LaTeX environment."
  16662. (when org-cdlatex-mode
  16663. (cond
  16664. ;; Before any word on the line: No expansion possible.
  16665. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16666. ;; Just after first word on the line: Expand it. Make sure it
  16667. ;; cannot happen on headlines, though.
  16668. ((save-excursion
  16669. (skip-chars-backward "a-zA-Z0-9*")
  16670. (skip-chars-backward " \t")
  16671. (and (bolp) (not (org-at-heading-p))))
  16672. (cdlatex-tab) t)
  16673. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16674. (defun org-cdlatex-underscore-caret (&optional arg)
  16675. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16676. Revert to the normal definition outside of these fragments."
  16677. (interactive "P")
  16678. (if (org-inside-LaTeX-fragment-p)
  16679. (call-interactively 'cdlatex-sub-superscript)
  16680. (let (org-cdlatex-mode)
  16681. (call-interactively (key-binding (vector last-input-event))))))
  16682. (defun org-cdlatex-math-modify (&optional arg)
  16683. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16684. Revert to the normal definition outside of these fragments."
  16685. (interactive "P")
  16686. (if (org-inside-LaTeX-fragment-p)
  16687. (call-interactively 'cdlatex-math-modify)
  16688. (let (org-cdlatex-mode)
  16689. (call-interactively (key-binding (vector last-input-event))))))
  16690. ;;;; LaTeX fragments
  16691. (defvar org-latex-regexps
  16692. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16693. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16694. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16695. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16696. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16697. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16698. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16699. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16700. "Regular expressions for matching embedded LaTeX.")
  16701. (defun org-inside-LaTeX-fragment-p ()
  16702. "Test if point is inside a LaTeX fragment.
  16703. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16704. sequence appearing also before point.
  16705. Even though the matchers for math are configurable, this function assumes
  16706. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16707. delimiters are skipped when they have been removed by customization.
  16708. The return value is nil, or a cons cell with the delimiter and the
  16709. position of this delimiter.
  16710. This function does a reasonably good job, but can locally be fooled by
  16711. for example currency specifications. For example it will assume being in
  16712. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16713. fragments that are properly closed, but during editing, we have to live
  16714. with the uncertainty caused by missing closing delimiters. This function
  16715. looks only before point, not after."
  16716. (catch 'exit
  16717. (let ((pos (point))
  16718. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16719. (lim (progn
  16720. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16721. (point)))
  16722. dd-on str (start 0) m re)
  16723. (goto-char pos)
  16724. (when dodollar
  16725. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16726. re (nth 1 (assoc "$" org-latex-regexps)))
  16727. (while (string-match re str start)
  16728. (cond
  16729. ((= (match-end 0) (length str))
  16730. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16731. ((= (match-end 0) (- (length str) 5))
  16732. (throw 'exit nil))
  16733. (t (setq start (match-end 0))))))
  16734. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16735. (goto-char pos)
  16736. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16737. (and (match-beginning 2) (throw 'exit nil))
  16738. ;; count $$
  16739. (while (re-search-backward "\\$\\$" lim t)
  16740. (setq dd-on (not dd-on)))
  16741. (goto-char pos)
  16742. (if dd-on (cons "$$" m))))))
  16743. (defun org-inside-latex-macro-p ()
  16744. "Is point inside a LaTeX macro or its arguments?"
  16745. (save-match-data
  16746. (org-in-regexp
  16747. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16748. (defvar org-latex-fragment-image-overlays nil
  16749. "List of overlays carrying the images of latex fragments.")
  16750. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16751. (defun org-remove-latex-fragment-image-overlays ()
  16752. "Remove all overlays with LaTeX fragment images in current buffer."
  16753. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16754. (setq org-latex-fragment-image-overlays nil))
  16755. (defun org-preview-latex-fragment (&optional subtree)
  16756. "Preview the LaTeX fragment at point, or all locally or globally.
  16757. If the cursor is in a LaTeX fragment, create the image and overlay
  16758. it over the source code. If there is no fragment at point, display
  16759. all fragments in the current text, from one headline to the next. With
  16760. prefix SUBTREE, display all fragments in the current subtree. With a
  16761. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16762. the cursor is before the first headline,
  16763. display all fragments in the buffer.
  16764. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16765. (interactive "P")
  16766. (unless buffer-file-name
  16767. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16768. (when (display-graphic-p)
  16769. (org-remove-latex-fragment-image-overlays)
  16770. (save-excursion
  16771. (save-restriction
  16772. (let (beg end at msg)
  16773. (cond
  16774. ((or (equal subtree '(16))
  16775. (not (save-excursion
  16776. (re-search-backward org-outline-regexp-bol nil t))))
  16777. (setq beg (point-min) end (point-max)
  16778. msg "Creating images for buffer...%s"))
  16779. ((equal subtree '(4))
  16780. (org-back-to-heading)
  16781. (setq beg (point) end (org-end-of-subtree t)
  16782. msg "Creating images for subtree...%s"))
  16783. (t
  16784. (if (setq at (org-inside-LaTeX-fragment-p))
  16785. (goto-char (max (point-min) (- (cdr at) 2)))
  16786. (org-back-to-heading))
  16787. (setq beg (point) end (progn (outline-next-heading) (point))
  16788. msg (if at "Creating image...%s"
  16789. "Creating images for entry...%s"))))
  16790. (message msg "")
  16791. (narrow-to-region beg end)
  16792. (goto-char beg)
  16793. (org-format-latex
  16794. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16795. (file-name-nondirectory
  16796. buffer-file-name)))
  16797. default-directory 'overlays msg at 'forbuffer
  16798. org-latex-create-formula-image-program)
  16799. (message msg "done. Use `C-c C-c' to remove images."))))))
  16800. (defun org-format-latex (prefix &optional dir overlays msg at
  16801. forbuffer processing-type)
  16802. "Replace LaTeX fragments with links to an image, and produce images.
  16803. Some of the options can be changed using the variable
  16804. `org-format-latex-options'."
  16805. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16806. (let* ((prefixnodir (file-name-nondirectory prefix))
  16807. (absprefix (expand-file-name prefix dir))
  16808. (todir (file-name-directory absprefix))
  16809. (opt org-format-latex-options)
  16810. (optnew org-format-latex-options)
  16811. (matchers (plist-get opt :matchers))
  16812. (re-list org-latex-regexps)
  16813. (cnt 0) txt hash link beg end re e checkdir
  16814. string
  16815. m n block-type block linkfile movefile ov)
  16816. ;; Check the different regular expressions
  16817. (while (setq e (pop re-list))
  16818. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16819. block (if block-type "\n\n" ""))
  16820. (when (member m matchers)
  16821. (goto-char (point-min))
  16822. (while (re-search-forward re nil t)
  16823. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16824. (or (not overlays)
  16825. (not (eq (get-char-property (match-beginning n)
  16826. 'org-overlay-type)
  16827. 'org-latex-overlay))))
  16828. (cond
  16829. ((eq processing-type 'verbatim))
  16830. ((eq processing-type 'mathjax)
  16831. ;; Prepare for MathJax processing.
  16832. (setq string (match-string n))
  16833. (when (member m '("$" "$1"))
  16834. (save-excursion
  16835. (delete-region (match-beginning n) (match-end n))
  16836. (goto-char (match-beginning n))
  16837. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16838. ((or (eq processing-type 'dvipng)
  16839. (eq processing-type 'imagemagick))
  16840. ;; Process to an image.
  16841. (setq txt (match-string n)
  16842. beg (match-beginning n) end (match-end n)
  16843. cnt (1+ cnt))
  16844. (let ((face (face-at-point))
  16845. (fg (plist-get opt :foreground))
  16846. (bg (plist-get opt :background))
  16847. ;; Ensure full list is printed.
  16848. print-length print-level)
  16849. (when forbuffer
  16850. ;; Get the colors from the face at point.
  16851. (goto-char beg)
  16852. (when (eq fg 'auto)
  16853. (setq fg (face-attribute face :foreground nil 'default)))
  16854. (when (eq bg 'auto)
  16855. (setq bg (face-attribute face :background nil 'default)))
  16856. (setq optnew (copy-sequence opt))
  16857. (plist-put optnew :foreground fg)
  16858. (plist-put optnew :background bg))
  16859. (setq hash (sha1 (prin1-to-string
  16860. (list org-format-latex-header
  16861. org-latex-default-packages-alist
  16862. org-latex-packages-alist
  16863. org-format-latex-options
  16864. forbuffer txt fg bg)))
  16865. linkfile (format "%s_%s.png" prefix hash)
  16866. movefile (format "%s_%s.png" absprefix hash)))
  16867. (setq link (concat block "[[file:" linkfile "]]" block))
  16868. (if msg (message msg cnt))
  16869. (goto-char beg)
  16870. (unless checkdir ; Ensure the directory exists.
  16871. (setq checkdir t)
  16872. (or (file-directory-p todir) (make-directory todir t)))
  16873. (unless (file-exists-p movefile)
  16874. (org-create-formula-image
  16875. txt movefile optnew forbuffer processing-type))
  16876. (if overlays
  16877. (progn
  16878. (mapc (lambda (o)
  16879. (if (eq (overlay-get o 'org-overlay-type)
  16880. 'org-latex-overlay)
  16881. (delete-overlay o)))
  16882. (overlays-in beg end))
  16883. (setq ov (make-overlay beg end))
  16884. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16885. (if (featurep 'xemacs)
  16886. (progn
  16887. (overlay-put ov 'invisible t)
  16888. (overlay-put
  16889. ov 'end-glyph
  16890. (make-glyph (vector 'png :file movefile))))
  16891. (overlay-put
  16892. ov 'display
  16893. (list 'image :type 'png :file movefile :ascent 'center)))
  16894. (push ov org-latex-fragment-image-overlays)
  16895. (goto-char end))
  16896. (delete-region beg end)
  16897. (insert (org-add-props link
  16898. (list 'org-latex-src
  16899. (replace-regexp-in-string
  16900. "\"" "" txt)
  16901. 'org-latex-src-embed-type
  16902. (if block-type 'paragraph 'character))))))
  16903. ((eq processing-type 'mathml)
  16904. ;; Process to MathML
  16905. (unless (save-match-data (org-format-latex-mathml-available-p))
  16906. (user-error "LaTeX to MathML converter not configured"))
  16907. (setq txt (match-string n)
  16908. beg (match-beginning n) end (match-end n)
  16909. cnt (1+ cnt))
  16910. (if msg (message msg cnt))
  16911. (goto-char beg)
  16912. (delete-region beg end)
  16913. (insert (org-format-latex-as-mathml
  16914. txt block-type prefix dir)))
  16915. (t
  16916. (error "Unknown conversion type %s for LaTeX fragments"
  16917. processing-type)))))))))
  16918. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16919. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16920. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16921. sucessful, return the portion between \"<math...> </math>\"
  16922. elements otherwise return nil. When MATHML-FILE is specified,
  16923. write the results in to that file. When invoked as an
  16924. interactive command, prompt for LATEX-FRAG, with initial value
  16925. set to the current active region and echo the results for user
  16926. inspection."
  16927. (interactive (list (let ((frag (when (org-region-active-p)
  16928. (buffer-substring-no-properties
  16929. (region-beginning) (region-end)))))
  16930. (read-string "LaTeX Fragment: " frag nil frag))))
  16931. (unless latex-frag (error "Invalid LaTeX fragment"))
  16932. (let* ((tmp-in-file (file-relative-name
  16933. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16934. (ignore (write-region latex-frag nil tmp-in-file))
  16935. (tmp-out-file (file-relative-name
  16936. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16937. (cmd (format-spec
  16938. org-latex-to-mathml-convert-command
  16939. `((?j . ,(shell-quote-argument
  16940. (expand-file-name org-latex-to-mathml-jar-file)))
  16941. (?I . ,(shell-quote-argument tmp-in-file))
  16942. (?o . ,(shell-quote-argument tmp-out-file)))))
  16943. mathml shell-command-output)
  16944. (when (org-called-interactively-p 'any)
  16945. (unless (org-format-latex-mathml-available-p)
  16946. (user-error "LaTeX to MathML converter not configured")))
  16947. (message "Running %s" cmd)
  16948. (setq shell-command-output (shell-command-to-string cmd))
  16949. (setq mathml
  16950. (when (file-readable-p tmp-out-file)
  16951. (with-current-buffer (find-file-noselect tmp-out-file t)
  16952. (goto-char (point-min))
  16953. (when (re-search-forward
  16954. (concat
  16955. (regexp-quote
  16956. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16957. "\\(.\\|\n\\)*"
  16958. (regexp-quote "</math>")) nil t)
  16959. (prog1 (match-string 0) (kill-buffer))))))
  16960. (cond
  16961. (mathml
  16962. (setq mathml
  16963. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16964. (when mathml-file
  16965. (write-region mathml nil mathml-file))
  16966. (when (org-called-interactively-p 'any)
  16967. (message mathml)))
  16968. ((message "LaTeX to MathML conversion failed")
  16969. (message shell-command-output)))
  16970. (delete-file tmp-in-file)
  16971. (when (file-exists-p tmp-out-file)
  16972. (delete-file tmp-out-file))
  16973. mathml))
  16974. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16975. prefix &optional dir)
  16976. "Use `org-create-math-formula' but check local cache first."
  16977. (let* ((absprefix (expand-file-name prefix dir))
  16978. (print-length nil) (print-level nil)
  16979. (formula-id (concat
  16980. "formula-"
  16981. (sha1
  16982. (prin1-to-string
  16983. (list latex-frag
  16984. org-latex-to-mathml-convert-command)))))
  16985. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16986. (formula-cache-dir (file-name-directory formula-cache)))
  16987. (unless (file-directory-p formula-cache-dir)
  16988. (make-directory formula-cache-dir t))
  16989. (unless (file-exists-p formula-cache)
  16990. (org-create-math-formula latex-frag formula-cache))
  16991. (if (file-exists-p formula-cache)
  16992. ;; Successful conversion. Return the link to MathML file.
  16993. (org-add-props
  16994. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16995. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16996. 'org-latex-src-embed-type (if latex-frag-type
  16997. 'paragraph 'character)))
  16998. ;; Failed conversion. Return the LaTeX fragment verbatim
  16999. latex-frag)))
  17000. (defun org-create-formula-image (string tofile options buffer &optional type)
  17001. "Create an image from LaTeX source using dvipng or convert.
  17002. This function calls either `org-create-formula-image-with-dvipng'
  17003. or `org-create-formula-image-with-imagemagick' depending on the
  17004. value of `org-latex-create-formula-image-program' or on the value
  17005. of the optional TYPE variable.
  17006. Note: ultimately these two function should be combined as they
  17007. share a good deal of logic."
  17008. (org-check-external-command
  17009. "latex" "needed to convert LaTeX fragments to images")
  17010. (funcall
  17011. (case (or type org-latex-create-formula-image-program)
  17012. ('dvipng
  17013. (org-check-external-command
  17014. "dvipng" "needed to convert LaTeX fragments to images")
  17015. #'org-create-formula-image-with-dvipng)
  17016. ('imagemagick
  17017. (org-check-external-command
  17018. "convert" "you need to install imagemagick")
  17019. #'org-create-formula-image-with-imagemagick)
  17020. (t (error
  17021. "Invalid value of `org-latex-create-formula-image-program'")))
  17022. string tofile options buffer))
  17023. (declare-function org-export-get-backend "ox" (name))
  17024. (declare-function org-export--get-global-options "ox" (&optional backend))
  17025. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17026. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17027. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17028. (defun org-create-formula--latex-header ()
  17029. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17030. (let ((info (org-combine-plists (org-export--get-global-options
  17031. (org-export-get-backend 'latex))
  17032. (org-export--get-inbuffer-options
  17033. (org-export-get-backend 'latex)))))
  17034. (org-latex-guess-babel-language
  17035. (org-latex-guess-inputenc
  17036. (org-splice-latex-header
  17037. org-format-latex-header
  17038. org-latex-default-packages-alist
  17039. org-latex-packages-alist t
  17040. (plist-get info :latex-header)))
  17041. info)))
  17042. ;; This function borrows from Ganesh Swami's latex2png.el
  17043. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17044. "This calls dvipng."
  17045. (require 'ox-latex)
  17046. (let* ((tmpdir (if (featurep 'xemacs)
  17047. (temp-directory)
  17048. temporary-file-directory))
  17049. (texfilebase (make-temp-name
  17050. (expand-file-name "orgtex" tmpdir)))
  17051. (texfile (concat texfilebase ".tex"))
  17052. (dvifile (concat texfilebase ".dvi"))
  17053. (pngfile (concat texfilebase ".png"))
  17054. (fnh (if (featurep 'xemacs)
  17055. (font-height (face-font 'default))
  17056. (face-attribute 'default :height nil)))
  17057. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17058. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17059. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17060. "Black"))
  17061. (bg (or (plist-get options (if buffer :background :html-background))
  17062. "Transparent")))
  17063. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17064. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17065. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17066. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17067. (let ((latex-header (org-create-formula--latex-header)))
  17068. (with-temp-file texfile
  17069. (insert latex-header)
  17070. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17071. (let ((dir default-directory))
  17072. (condition-case nil
  17073. (progn
  17074. (cd tmpdir)
  17075. (call-process "latex" nil nil nil texfile))
  17076. (error nil))
  17077. (cd dir))
  17078. (if (not (file-exists-p dvifile))
  17079. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17080. (condition-case nil
  17081. (if (featurep 'xemacs)
  17082. (call-process "dvipng" nil nil nil
  17083. "-fg" fg "-bg" bg
  17084. "-T" "tight"
  17085. "-o" pngfile
  17086. dvifile)
  17087. (call-process "dvipng" nil nil nil
  17088. "-fg" fg "-bg" bg
  17089. "-D" dpi
  17090. ;;"-x" scale "-y" scale
  17091. "-T" "tight"
  17092. "-o" pngfile
  17093. dvifile))
  17094. (error nil))
  17095. (if (not (file-exists-p pngfile))
  17096. (if org-format-latex-signal-error
  17097. (error "Failed to create png file from %s" texfile)
  17098. (message "Failed to create png file from %s" texfile)
  17099. nil)
  17100. ;; Use the requested file name and clean up
  17101. (copy-file pngfile tofile 'replace)
  17102. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17103. (if (file-exists-p (concat texfilebase e))
  17104. (delete-file (concat texfilebase e))))
  17105. pngfile))))
  17106. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17107. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17108. "This calls convert, which is included into imagemagick."
  17109. (require 'ox-latex)
  17110. (let* ((tmpdir (if (featurep 'xemacs)
  17111. (temp-directory)
  17112. temporary-file-directory))
  17113. (texfilebase (make-temp-name
  17114. (expand-file-name "orgtex" tmpdir)))
  17115. (texfile (concat texfilebase ".tex"))
  17116. (pdffile (concat texfilebase ".pdf"))
  17117. (pngfile (concat texfilebase ".png"))
  17118. (fnh (if (featurep 'xemacs)
  17119. (font-height (face-font 'default))
  17120. (face-attribute 'default :height nil)))
  17121. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17122. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17123. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17124. "black"))
  17125. (bg (or (plist-get options (if buffer :background :html-background))
  17126. "white")))
  17127. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17128. (setq fg (org-latex-color-format fg)))
  17129. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17130. (setq bg (org-latex-color-format
  17131. (if (string= bg "Transparent") "white" bg))))
  17132. (let ((latex-header (org-create-formula--latex-header)))
  17133. (with-temp-file texfile
  17134. (insert latex-header)
  17135. (insert "\n\\begin{document}\n"
  17136. "\\definecolor{fg}{rgb}{" fg "}\n"
  17137. "\\definecolor{bg}{rgb}{" bg "}\n"
  17138. "\n\\pagecolor{bg}\n"
  17139. "\n{\\color{fg}\n"
  17140. string
  17141. "\n}\n"
  17142. "\n\\end{document}\n")))
  17143. (org-latex-compile texfile t)
  17144. (if (not (file-exists-p pdffile))
  17145. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17146. (condition-case nil
  17147. (if (featurep 'xemacs)
  17148. (call-process "convert" nil nil nil
  17149. "-density" "96"
  17150. "-trim"
  17151. "-antialias"
  17152. pdffile
  17153. "-quality" "100"
  17154. ;; "-sharpen" "0x1.0"
  17155. pngfile)
  17156. (call-process "convert" nil nil nil
  17157. "-density" dpi
  17158. "-trim"
  17159. "-antialias"
  17160. pdffile
  17161. "-quality" "100"
  17162. ;; "-sharpen" "0x1.0"
  17163. pngfile))
  17164. (error nil))
  17165. (if (not (file-exists-p pngfile))
  17166. (if org-format-latex-signal-error
  17167. (error "Failed to create png file from %s" texfile)
  17168. (message "Failed to create png file from %s" texfile)
  17169. nil)
  17170. ;; Use the requested file name and clean up
  17171. (copy-file pngfile tofile 'replace)
  17172. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17173. (if (file-exists-p (concat texfilebase e))
  17174. (delete-file (concat texfilebase e))))
  17175. pngfile))))
  17176. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17177. "Fill a LaTeX header template TPL.
  17178. In the template, the following place holders will be recognized:
  17179. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17180. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17181. [PACKAGES] \\usepackage statements for PKG
  17182. [NO-PACKAGES] do not include PKG
  17183. [EXTRA] the string EXTRA
  17184. [NO-EXTRA] do not include EXTRA
  17185. For backward compatibility, if both the positive and the negative place
  17186. holder is missing, the positive one (without the \"NO-\") will be
  17187. assumed to be present at the end of the template.
  17188. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17189. EXTRA is a string.
  17190. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17191. (let (rpl (end ""))
  17192. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17193. (setq rpl (if (or (match-end 1) (not def-pkg))
  17194. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17195. tpl (replace-match rpl t t tpl))
  17196. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17197. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17198. (setq rpl (if (or (match-end 1) (not pkg))
  17199. "" (org-latex-packages-to-string pkg snippets-p t))
  17200. tpl (replace-match rpl t t tpl))
  17201. (if pkg (setq end
  17202. (concat end "\n"
  17203. (org-latex-packages-to-string pkg snippets-p)))))
  17204. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17205. (setq rpl (if (or (match-end 1) (not extra))
  17206. "" (concat extra "\n"))
  17207. tpl (replace-match rpl t t tpl))
  17208. (if (and extra (string-match "\\S-" extra))
  17209. (setq end (concat end "\n" extra))))
  17210. (if (string-match "\\S-" end)
  17211. (concat tpl "\n" end)
  17212. tpl)))
  17213. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17214. "Turn an alist of packages into a string with the \\usepackage macros."
  17215. (setq pkg (mapconcat (lambda(p)
  17216. (cond
  17217. ((stringp p) p)
  17218. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17219. (format "%% Package %s omitted" (cadr p)))
  17220. ((equal "" (car p))
  17221. (format "\\usepackage{%s}" (cadr p)))
  17222. (t
  17223. (format "\\usepackage[%s]{%s}"
  17224. (car p) (cadr p)))))
  17225. pkg
  17226. "\n"))
  17227. (if newline (concat pkg "\n") pkg))
  17228. (defun org-dvipng-color (attr)
  17229. "Return a RGB color specification for dvipng."
  17230. (apply 'format "rgb %s %s %s"
  17231. (mapcar 'org-normalize-color
  17232. (if (featurep 'xemacs)
  17233. (color-rgb-components
  17234. (face-property 'default
  17235. (cond ((eq attr :foreground) 'foreground)
  17236. ((eq attr :background) 'background))))
  17237. (color-values (face-attribute 'default attr nil))))))
  17238. (defun org-dvipng-color-format (color-name)
  17239. "Convert COLOR-NAME to a RGB color value for dvipng."
  17240. (apply 'format "rgb %s %s %s"
  17241. (mapcar 'org-normalize-color
  17242. (color-values color-name))))
  17243. (defun org-latex-color (attr)
  17244. "Return a RGB color for the LaTeX color package."
  17245. (apply 'format "%s,%s,%s"
  17246. (mapcar 'org-normalize-color
  17247. (if (featurep 'xemacs)
  17248. (color-rgb-components
  17249. (face-property 'default
  17250. (cond ((eq attr :foreground) 'foreground)
  17251. ((eq attr :background) 'background))))
  17252. (color-values (face-attribute 'default attr nil))))))
  17253. (defun org-latex-color-format (color-name)
  17254. "Convert COLOR-NAME to a RGB color value."
  17255. (apply 'format "%s,%s,%s"
  17256. (mapcar 'org-normalize-color
  17257. (color-values color-name))))
  17258. (defun org-normalize-color (value)
  17259. "Return string to be used as color value for an RGB component."
  17260. (format "%g" (/ value 65535.0)))
  17261. ;; Image display
  17262. (defvar org-inline-image-overlays nil)
  17263. (make-variable-buffer-local 'org-inline-image-overlays)
  17264. (defun org-toggle-inline-images (&optional include-linked)
  17265. "Toggle the display of inline images.
  17266. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17267. (interactive "P")
  17268. (if org-inline-image-overlays
  17269. (progn
  17270. (org-remove-inline-images)
  17271. (message "Inline image display turned off"))
  17272. (org-display-inline-images include-linked)
  17273. (if (and (org-called-interactively-p)
  17274. org-inline-image-overlays)
  17275. (message "%d images displayed inline"
  17276. (length org-inline-image-overlays))
  17277. (message "No images to display inline"))))
  17278. (defun org-redisplay-inline-images ()
  17279. "Refresh the display of inline images."
  17280. (interactive)
  17281. (if (not org-inline-image-overlays)
  17282. (org-toggle-inline-images)
  17283. (org-toggle-inline-images)
  17284. (org-toggle-inline-images)))
  17285. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17286. "Display inline images.
  17287. Normally only links without a description part are inlined, because this
  17288. is how it will work for export. When INCLUDE-LINKED is set, also links
  17289. with a description part will be inlined. This can be nice for a quick
  17290. look at those images, but it does not reflect what exported files will look
  17291. like.
  17292. When REFRESH is set, refresh existing images between BEG and END.
  17293. This will create new image displays only if necessary.
  17294. BEG and END default to the buffer boundaries."
  17295. (interactive "P")
  17296. (when (display-graphic-p)
  17297. (unless refresh
  17298. (org-remove-inline-images)
  17299. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17300. (save-excursion
  17301. (save-restriction
  17302. (widen)
  17303. (setq beg (or beg (point-min)) end (or end (point-max)))
  17304. (goto-char beg)
  17305. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17306. (substring (org-image-file-name-regexp) 0 -2)
  17307. "\\)\\]" (if include-linked "" "\\]")))
  17308. (case-fold-search t)
  17309. old file ov img type attrwidth width)
  17310. (while (re-search-forward re end t)
  17311. (setq old (get-char-property-and-overlay (match-beginning 1)
  17312. 'org-image-overlay)
  17313. file (expand-file-name
  17314. (concat (or (match-string 3) "") (match-string 4))))
  17315. (when (image-type-available-p 'imagemagick)
  17316. (setq attrwidth (if (or (listp org-image-actual-width)
  17317. (null org-image-actual-width))
  17318. (save-excursion
  17319. (save-match-data
  17320. (when (re-search-backward
  17321. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17322. (save-excursion
  17323. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17324. (string-to-number (match-string 1))))))
  17325. width (cond ((eq org-image-actual-width t) nil)
  17326. ((null org-image-actual-width) attrwidth)
  17327. ((numberp org-image-actual-width)
  17328. org-image-actual-width)
  17329. ((listp org-image-actual-width)
  17330. (or attrwidth (car org-image-actual-width))))
  17331. type (if width 'imagemagick)))
  17332. (when (file-exists-p file)
  17333. (if (and (car-safe old) refresh)
  17334. (image-refresh (overlay-get (cdr old) 'display))
  17335. (setq img (save-match-data (create-image file type nil :width width)))
  17336. (when img
  17337. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17338. (overlay-put ov 'display img)
  17339. (overlay-put ov 'face 'default)
  17340. (overlay-put ov 'org-image-overlay t)
  17341. (overlay-put ov 'modification-hooks
  17342. (list 'org-display-inline-remove-overlay))
  17343. (push ov org-inline-image-overlays))))))))))
  17344. (define-obsolete-function-alias
  17345. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17346. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17347. "Remove inline-display overlay if a corresponding region is modified."
  17348. (let ((inhibit-modification-hooks t))
  17349. (when (and ov after)
  17350. (delete ov org-inline-image-overlays)
  17351. (delete-overlay ov))))
  17352. (defun org-remove-inline-images ()
  17353. "Remove inline display of images."
  17354. (interactive)
  17355. (mapc 'delete-overlay org-inline-image-overlays)
  17356. (setq org-inline-image-overlays nil))
  17357. ;;;; Key bindings
  17358. ;; Outline functions from `outline-mode-prefix-map'
  17359. ;; that can be remapped in Org:
  17360. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17361. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17362. (define-key org-mode-map [remap outline-forward-same-level]
  17363. 'org-forward-heading-same-level)
  17364. (define-key org-mode-map [remap outline-backward-same-level]
  17365. 'org-backward-heading-same-level)
  17366. (define-key org-mode-map [remap show-branches]
  17367. 'org-kill-note-or-show-branches)
  17368. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17369. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17370. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17371. ;; Outline functions from `outline-mode-prefix-map' that can not
  17372. ;; be remapped in Org:
  17373. ;;
  17374. ;; - the column "key binding" shows whether the Outline function is still
  17375. ;; available in Org mode on the same key that it has been bound to in
  17376. ;; Outline mode:
  17377. ;; - "overridden": key used for a different functionality in Org mode
  17378. ;; - else: key still bound to the same Outline function in Org mode
  17379. ;;
  17380. ;; | Outline function | key binding | Org replacement |
  17381. ;; |------------------------------------+-------------+-----------------------|
  17382. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17383. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17384. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17385. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17386. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17387. ;; | `show-entry' | overridden | no replacement |
  17388. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17389. ;; | `show-branches' | `C-c C-k' | still same function |
  17390. ;; | `show-subtree' | overridden | visibility cycling |
  17391. ;; | `show-all' | overridden | no replacement |
  17392. ;; | `hide-subtree' | overridden | visibility cycling |
  17393. ;; | `hide-body' | overridden | no replacement |
  17394. ;; | `hide-entry' | overridden | visibility cycling |
  17395. ;; | `hide-leaves' | overridden | no replacement |
  17396. ;; | `hide-sublevels' | overridden | no replacement |
  17397. ;; | `hide-other' | overridden | no replacement |
  17398. ;; Make `C-c C-x' a prefix key
  17399. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17400. ;; TAB key with modifiers
  17401. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17402. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17403. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17404. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17405. ;; The following line is necessary under Suse GNU/Linux
  17406. (unless (featurep 'xemacs)
  17407. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17408. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17409. (define-key org-mode-map [backtab] 'org-shifttab)
  17410. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17411. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17412. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17413. ;; Cursor keys with modifiers
  17414. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17415. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17416. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17417. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17418. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17419. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17420. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17421. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17422. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17423. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17424. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17425. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17426. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17427. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17428. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17429. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17430. ;; Babel keys
  17431. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17432. (mapc (lambda (pair)
  17433. (define-key org-babel-map (car pair) (cdr pair)))
  17434. org-babel-key-bindings)
  17435. ;;; Extra keys for tty access.
  17436. ;; We only set them when really needed because otherwise the
  17437. ;; menus don't show the simple keys
  17438. (when (or org-use-extra-keys
  17439. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17440. (not window-system))
  17441. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17442. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17443. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17444. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17445. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17446. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17447. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17448. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17449. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17450. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17451. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17452. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17453. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17454. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17455. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17456. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17457. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17458. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17459. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17460. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17461. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17462. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17463. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17464. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17465. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17466. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17467. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17468. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17469. ;; All the other keys
  17470. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17471. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17472. (if (boundp 'narrow-map)
  17473. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17474. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17475. (if (boundp 'narrow-map)
  17476. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17477. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17478. (if (boundp 'narrow-map)
  17479. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17480. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17481. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17482. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17483. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17484. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17485. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17486. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17487. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17488. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17489. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17490. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17491. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17492. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17493. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17494. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17495. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17496. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17497. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17498. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17499. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17500. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17501. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17502. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17503. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17504. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17505. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17506. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17507. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17508. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17509. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17510. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17511. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17512. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17513. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17514. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17515. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17516. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17517. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17518. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17519. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17520. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17521. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17522. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17523. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17524. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17525. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17526. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17527. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17528. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17529. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17530. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17531. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17532. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17533. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17534. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17535. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17536. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17537. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17538. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17539. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17540. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17541. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17542. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17543. (org-defkey org-mode-map "\C-m" 'org-return)
  17544. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17545. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17546. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17547. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17548. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17549. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17550. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17551. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17552. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17553. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17554. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17555. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17556. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17557. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17558. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17559. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17560. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17561. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17562. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17563. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17564. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17565. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17566. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17567. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17568. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17569. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17570. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17571. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17572. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17573. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17574. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17575. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17576. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17577. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17578. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17579. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17580. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17581. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17582. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17583. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17584. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17585. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17586. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17587. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17588. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17589. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17590. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17591. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17592. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17593. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17594. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17595. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17596. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17597. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17598. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17599. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17600. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17601. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17602. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17603. (when (featurep 'xemacs)
  17604. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17605. (defconst org-speed-commands-default
  17606. '(
  17607. ("Outline Navigation")
  17608. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17609. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17610. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17611. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17612. ("F" . org-next-block)
  17613. ("B" . org-previous-block)
  17614. ("u" . (org-speed-move-safe 'outline-up-heading))
  17615. ("j" . org-goto)
  17616. ("g" . (org-refile t))
  17617. ("Outline Visibility")
  17618. ("c" . org-cycle)
  17619. ("C" . org-shifttab)
  17620. (" " . org-display-outline-path)
  17621. ("s" . org-narrow-to-subtree)
  17622. ("=" . org-columns)
  17623. ("Outline Structure Editing")
  17624. ("U" . org-shiftmetaup)
  17625. ("D" . org-shiftmetadown)
  17626. ("r" . org-metaright)
  17627. ("l" . org-metaleft)
  17628. ("R" . org-shiftmetaright)
  17629. ("L" . org-shiftmetaleft)
  17630. ("i" . (progn (forward-char 1) (call-interactively
  17631. 'org-insert-heading-respect-content)))
  17632. ("^" . org-sort)
  17633. ("w" . org-refile)
  17634. ("a" . org-archive-subtree-default-with-confirmation)
  17635. ("@" . org-mark-subtree)
  17636. ("#" . org-toggle-comment)
  17637. ("Clock Commands")
  17638. ("I" . org-clock-in)
  17639. ("O" . org-clock-out)
  17640. ("Meta Data Editing")
  17641. ("t" . org-todo)
  17642. ("," . (org-priority))
  17643. ("0" . (org-priority ?\ ))
  17644. ("1" . (org-priority ?A))
  17645. ("2" . (org-priority ?B))
  17646. ("3" . (org-priority ?C))
  17647. (":" . org-set-tags-command)
  17648. ("e" . org-set-effort)
  17649. ("E" . org-inc-effort)
  17650. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17651. (org-entry-put (point) "APPT_WARNTIME" m)))
  17652. ("Agenda Views etc")
  17653. ("v" . org-agenda)
  17654. ("/" . org-sparse-tree)
  17655. ("Misc")
  17656. ("o" . org-open-at-point)
  17657. ("?" . org-speed-command-help)
  17658. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17659. (">" . (org-agenda-remove-restriction-lock))
  17660. )
  17661. "The default speed commands.")
  17662. (defun org-print-speed-command (e)
  17663. (if (> (length (car e)) 1)
  17664. (progn
  17665. (princ "\n")
  17666. (princ (car e))
  17667. (princ "\n")
  17668. (princ (make-string (length (car e)) ?-))
  17669. (princ "\n"))
  17670. (princ (car e))
  17671. (princ " ")
  17672. (if (symbolp (cdr e))
  17673. (princ (symbol-name (cdr e)))
  17674. (prin1 (cdr e)))
  17675. (princ "\n")))
  17676. (defun org-speed-command-help ()
  17677. "Show the available speed commands."
  17678. (interactive)
  17679. (if (not org-use-speed-commands)
  17680. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17681. (with-output-to-temp-buffer "*Help*"
  17682. (princ "User-defined Speed commands\n===========================\n")
  17683. (mapc 'org-print-speed-command org-speed-commands-user)
  17684. (princ "\n")
  17685. (princ "Built-in Speed commands\n=======================\n")
  17686. (mapc 'org-print-speed-command org-speed-commands-default))
  17687. (with-current-buffer "*Help*"
  17688. (setq truncate-lines t))))
  17689. (defun org-speed-move-safe (cmd)
  17690. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17691. If not, return to the original position and throw an error."
  17692. (interactive)
  17693. (let ((pos (point)))
  17694. (call-interactively cmd)
  17695. (unless (and (bolp) (org-at-heading-p))
  17696. (goto-char pos)
  17697. (error "Boundary reached while executing %s" cmd))))
  17698. (defvar org-self-insert-command-undo-counter 0)
  17699. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17700. (defvar org-speed-command nil)
  17701. (define-obsolete-function-alias
  17702. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17703. (defun org-speed-command-activate (keys)
  17704. "Hook for activating single-letter speed commands.
  17705. `org-speed-commands-default' specifies a minimal command set.
  17706. Use `org-speed-commands-user' for further customization."
  17707. (when (or (and (bolp) (looking-at org-outline-regexp))
  17708. (and (functionp org-use-speed-commands)
  17709. (funcall org-use-speed-commands)))
  17710. (cdr (assoc keys (append org-speed-commands-user
  17711. org-speed-commands-default)))))
  17712. (define-obsolete-function-alias
  17713. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17714. (defun org-babel-speed-command-activate (keys)
  17715. "Hook for activating single-letter code block commands."
  17716. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17717. (cdr (assoc keys org-babel-key-bindings))))
  17718. (defcustom org-speed-command-hook
  17719. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17720. "Hook for activating speed commands at strategic locations.
  17721. Hook functions are called in sequence until a valid handler is
  17722. found.
  17723. Each hook takes a single argument, a user-pressed command key
  17724. which is also a `self-insert-command' from the global map.
  17725. Within the hook, examine the cursor position and the command key
  17726. and return nil or a valid handler as appropriate. Handler could
  17727. be one of an interactive command, a function, or a form.
  17728. Set `org-use-speed-commands' to non-nil value to enable this
  17729. hook. The default setting is `org-speed-command-activate'."
  17730. :group 'org-structure
  17731. :version "24.1"
  17732. :type 'hook)
  17733. (defun org-self-insert-command (N)
  17734. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17735. If the cursor is in a table looking at whitespace, the whitespace is
  17736. overwritten, and the table is not marked as requiring realignment."
  17737. (interactive "p")
  17738. (org-check-before-invisible-edit 'insert)
  17739. (cond
  17740. ((and org-use-speed-commands
  17741. (setq org-speed-command
  17742. (run-hook-with-args-until-success
  17743. 'org-speed-command-hook (this-command-keys))))
  17744. (cond
  17745. ((commandp org-speed-command)
  17746. (setq this-command org-speed-command)
  17747. (call-interactively org-speed-command))
  17748. ((functionp org-speed-command)
  17749. (funcall org-speed-command))
  17750. ((and org-speed-command (listp org-speed-command))
  17751. (eval org-speed-command))
  17752. (t (let (org-use-speed-commands)
  17753. (call-interactively 'org-self-insert-command)))))
  17754. ((and
  17755. (org-table-p)
  17756. (progn
  17757. ;; check if we blank the field, and if that triggers align
  17758. (and (featurep 'org-table) org-table-auto-blank-field
  17759. (member last-command
  17760. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17761. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17762. ;; got extra space, this field does not determine column width
  17763. (let (org-table-may-need-update) (org-table-blank-field))
  17764. ;; no extra space, this field may determine column width
  17765. (org-table-blank-field)))
  17766. t)
  17767. (eq N 1)
  17768. (looking-at "[^|\n]* |"))
  17769. (let (org-table-may-need-update)
  17770. (goto-char (1- (match-end 0)))
  17771. (backward-delete-char 1)
  17772. (goto-char (match-beginning 0))
  17773. (self-insert-command N)))
  17774. (t
  17775. (setq org-table-may-need-update t)
  17776. (self-insert-command N)
  17777. (org-fix-tags-on-the-fly)
  17778. (if org-self-insert-cluster-for-undo
  17779. (if (not (eq last-command 'org-self-insert-command))
  17780. (setq org-self-insert-command-undo-counter 1)
  17781. (if (>= org-self-insert-command-undo-counter 20)
  17782. (setq org-self-insert-command-undo-counter 1)
  17783. (and (> org-self-insert-command-undo-counter 0)
  17784. buffer-undo-list (listp buffer-undo-list)
  17785. (not (cadr buffer-undo-list)) ; remove nil entry
  17786. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17787. (setq org-self-insert-command-undo-counter
  17788. (1+ org-self-insert-command-undo-counter))))))))
  17789. (defun org-check-before-invisible-edit (kind)
  17790. "Check is editing if kind KIND would be dangerous with invisible text around.
  17791. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17792. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17793. (if (and org-catch-invisible-edits
  17794. (or (not (boundp 'visible-mode)) (not visible-mode))
  17795. (or (get-char-property (point) 'invisible)
  17796. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17797. ;; OK, we need to take a closer look
  17798. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17799. (invisible-before-point (if (bobp) nil (get-char-property
  17800. (1- (point)) 'invisible)))
  17801. (border-and-ok-direction
  17802. (or
  17803. ;; Check if we are acting predictably before invisible text
  17804. (and invisible-at-point (not invisible-before-point)
  17805. (memq kind '(insert delete-backward)))
  17806. ;; Check if we are acting predictably after invisible text
  17807. ;; This works not well, and I have turned it off. It seems
  17808. ;; better to always show and stop after invisible text.
  17809. ;; (and (not invisible-at-point) invisible-before-point
  17810. ;; (memq kind '(insert delete)))
  17811. )))
  17812. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17813. (memq invisible-before-point '(outline org-hide-block t)))
  17814. (if (eq org-catch-invisible-edits 'error)
  17815. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17816. (if (and org-custom-properties-overlays
  17817. (y-or-n-p "Display invisible properties in this buffer? "))
  17818. (org-toggle-custom-properties-visibility)
  17819. ;; Make the area visible
  17820. (save-excursion
  17821. (if invisible-before-point
  17822. (goto-char (previous-single-char-property-change
  17823. (point) 'invisible)))
  17824. (org-cycle))
  17825. (cond
  17826. ((eq org-catch-invisible-edits 'show)
  17827. ;; That's it, we do the edit after showing
  17828. (message
  17829. "Unfolding invisible region around point before editing")
  17830. (sit-for 1))
  17831. ((and (eq org-catch-invisible-edits 'smart)
  17832. border-and-ok-direction)
  17833. (message "Unfolding invisible region around point before editing"))
  17834. (t
  17835. ;; Don't do the edit, make the user repeat it in full visibility
  17836. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17837. (defun org-fix-tags-on-the-fly ()
  17838. (when (and (equal (char-after (point-at-bol)) ?*)
  17839. (org-at-heading-p))
  17840. (org-align-tags-here org-tags-column)))
  17841. (defun org-delete-backward-char (N)
  17842. "Like `delete-backward-char', insert whitespace at field end in tables.
  17843. When deleting backwards, in tables this function will insert whitespace in
  17844. front of the next \"|\" separator, to keep the table aligned. The table will
  17845. still be marked for re-alignment if the field did fill the entire column,
  17846. because, in this case the deletion might narrow the column."
  17847. (interactive "p")
  17848. (save-match-data
  17849. (org-check-before-invisible-edit 'delete-backward)
  17850. (if (and (org-table-p)
  17851. (eq N 1)
  17852. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17853. (looking-at ".*?|"))
  17854. (let ((pos (point))
  17855. (noalign (looking-at "[^|\n\r]* |"))
  17856. (c org-table-may-need-update))
  17857. (backward-delete-char N)
  17858. (if (not overwrite-mode)
  17859. (progn
  17860. (skip-chars-forward "^|")
  17861. (insert " ")
  17862. (goto-char (1- pos))))
  17863. ;; noalign: if there were two spaces at the end, this field
  17864. ;; does not determine the width of the column.
  17865. (if noalign (setq org-table-may-need-update c)))
  17866. (backward-delete-char N)
  17867. (org-fix-tags-on-the-fly))))
  17868. (defun org-delete-char (N)
  17869. "Like `delete-char', but insert whitespace at field end in tables.
  17870. When deleting characters, in tables this function will insert whitespace in
  17871. front of the next \"|\" separator, to keep the table aligned. The table will
  17872. still be marked for re-alignment if the field did fill the entire column,
  17873. because, in this case the deletion might narrow the column."
  17874. (interactive "p")
  17875. (save-match-data
  17876. (org-check-before-invisible-edit 'delete)
  17877. (if (and (org-table-p)
  17878. (not (bolp))
  17879. (not (= (char-after) ?|))
  17880. (eq N 1))
  17881. (if (looking-at ".*?|")
  17882. (let ((pos (point))
  17883. (noalign (looking-at "[^|\n\r]* |"))
  17884. (c org-table-may-need-update))
  17885. (replace-match
  17886. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17887. (goto-char pos)
  17888. ;; noalign: if there were two spaces at the end, this field
  17889. ;; does not determine the width of the column.
  17890. (if noalign (setq org-table-may-need-update c)))
  17891. (delete-char N))
  17892. (delete-char N)
  17893. (org-fix-tags-on-the-fly))))
  17894. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17895. (put 'org-self-insert-command 'delete-selection t)
  17896. (put 'orgtbl-self-insert-command 'delete-selection t)
  17897. (put 'org-delete-char 'delete-selection 'supersede)
  17898. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17899. (put 'org-yank 'delete-selection 'yank)
  17900. ;; Make `flyspell-mode' delay after some commands
  17901. (put 'org-self-insert-command 'flyspell-delayed t)
  17902. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17903. (put 'org-delete-char 'flyspell-delayed t)
  17904. (put 'org-delete-backward-char 'flyspell-delayed t)
  17905. ;; Make pabbrev-mode expand after org-mode commands
  17906. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17907. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17908. ;; How to do this: Measure non-white length of current string
  17909. ;; If equal to column width, we should realign.
  17910. (defun org-remap (map &rest commands)
  17911. "In MAP, remap the functions given in COMMANDS.
  17912. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17913. (let (new old)
  17914. (while commands
  17915. (setq old (pop commands) new (pop commands))
  17916. (if (fboundp 'command-remapping)
  17917. (org-defkey map (vector 'remap old) new)
  17918. (substitute-key-definition old new map global-map)))))
  17919. (defun org-transpose-words ()
  17920. "Transpose words for Org.
  17921. This uses the `org-mode-transpose-word-syntax-table' syntax
  17922. table, which interprets characters in `org-emphasis-alist' as
  17923. word constituants."
  17924. (interactive)
  17925. (with-syntax-table org-mode-transpose-word-syntax-table
  17926. (call-interactively 'transpose-words)))
  17927. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17928. (when (eq org-enable-table-editor 'optimized)
  17929. ;; If the user wants maximum table support, we need to hijack
  17930. ;; some standard editing functions
  17931. (org-remap org-mode-map
  17932. 'self-insert-command 'org-self-insert-command
  17933. 'delete-char 'org-delete-char
  17934. 'delete-backward-char 'org-delete-backward-char)
  17935. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17936. (defvar org-ctrl-c-ctrl-c-hook nil
  17937. "Hook for functions attaching themselves to `C-c C-c'.
  17938. This can be used to add additional functionality to the C-c C-c
  17939. key which executes context-dependent commands. This hook is run
  17940. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17941. run after the last test.
  17942. Each function will be called with no arguments. The function
  17943. must check if the context is appropriate for it to act. If yes,
  17944. it should do its thing and then return a non-nil value. If the
  17945. context is wrong, just do nothing and return nil.")
  17946. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17947. "Hook for functions attaching themselves to `C-c C-c'.
  17948. This can be used to add additional functionality to the C-c C-c
  17949. key which executes context-dependent commands. This hook is run
  17950. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17951. before the first test.
  17952. Each function will be called with no arguments. The function
  17953. must check if the context is appropriate for it to act. If yes,
  17954. it should do its thing and then return a non-nil value. If the
  17955. context is wrong, just do nothing and return nil.")
  17956. (defvar org-tab-first-hook nil
  17957. "Hook for functions to attach themselves to TAB.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.
  17959. This hook runs as the first action when TAB is pressed, even before
  17960. `org-cycle' messes around with the `outline-regexp' to cater for
  17961. inline tasks and plain list item folding.
  17962. If any function in this hook returns t, any other actions that
  17963. would have been caused by TAB (such as table field motion or visibility
  17964. cycling) will not occur.")
  17965. (defvar org-tab-after-check-for-table-hook nil
  17966. "Hook for functions to attach themselves to TAB.
  17967. See `org-ctrl-c-ctrl-c-hook' for more information.
  17968. This hook runs after it has been established that the cursor is not in a
  17969. table, but before checking if the cursor is in a headline or if global cycling
  17970. should be done.
  17971. If any function in this hook returns t, not other actions like visibility
  17972. cycling will be done.")
  17973. (defvar org-tab-after-check-for-cycling-hook nil
  17974. "Hook for functions to attach themselves to TAB.
  17975. See `org-ctrl-c-ctrl-c-hook' for more information.
  17976. This hook runs after it has been established that not table field motion and
  17977. not visibility should be done because of current context. This is probably
  17978. the place where a package like yasnippets can hook in.")
  17979. (defvar org-tab-before-tab-emulation-hook nil
  17980. "Hook for functions to attach themselves to TAB.
  17981. See `org-ctrl-c-ctrl-c-hook' for more information.
  17982. This hook runs after every other options for TAB have been exhausted, but
  17983. before indentation and \t insertion takes place.")
  17984. (defvar org-metaleft-hook nil
  17985. "Hook for functions attaching themselves to `M-left'.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17987. (defvar org-metaright-hook nil
  17988. "Hook for functions attaching themselves to `M-right'.
  17989. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17990. (defvar org-metaup-hook nil
  17991. "Hook for functions attaching themselves to `M-up'.
  17992. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17993. (defvar org-metadown-hook nil
  17994. "Hook for functions attaching themselves to `M-down'.
  17995. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17996. (defvar org-shiftmetaleft-hook nil
  17997. "Hook for functions attaching themselves to `M-S-left'.
  17998. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17999. (defvar org-shiftmetaright-hook nil
  18000. "Hook for functions attaching themselves to `M-S-right'.
  18001. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18002. (defvar org-shiftmetaup-hook nil
  18003. "Hook for functions attaching themselves to `M-S-up'.
  18004. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18005. (defvar org-shiftmetadown-hook nil
  18006. "Hook for functions attaching themselves to `M-S-down'.
  18007. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18008. (defvar org-metareturn-hook nil
  18009. "Hook for functions attaching themselves to `M-RET'.
  18010. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18011. (defvar org-shiftup-hook nil
  18012. "Hook for functions attaching themselves to `S-up'.
  18013. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18014. (defvar org-shiftup-final-hook nil
  18015. "Hook for functions attaching themselves to `S-up'.
  18016. This one runs after all other options except shift-select have been excluded.
  18017. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18018. (defvar org-shiftdown-hook nil
  18019. "Hook for functions attaching themselves to `S-down'.
  18020. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18021. (defvar org-shiftdown-final-hook nil
  18022. "Hook for functions attaching themselves to `S-down'.
  18023. This one runs after all other options except shift-select have been excluded.
  18024. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18025. (defvar org-shiftleft-hook nil
  18026. "Hook for functions attaching themselves to `S-left'.
  18027. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18028. (defvar org-shiftleft-final-hook nil
  18029. "Hook for functions attaching themselves to `S-left'.
  18030. This one runs after all other options except shift-select have been excluded.
  18031. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18032. (defvar org-shiftright-hook nil
  18033. "Hook for functions attaching themselves to `S-right'.
  18034. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18035. (defvar org-shiftright-final-hook nil
  18036. "Hook for functions attaching themselves to `S-right'.
  18037. This one runs after all other options except shift-select have been excluded.
  18038. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18039. (defun org-modifier-cursor-error ()
  18040. "Throw an error, a modified cursor command was applied in wrong context."
  18041. (user-error "This command is active in special context like tables, headlines or items"))
  18042. (defun org-shiftselect-error ()
  18043. "Throw an error because Shift-Cursor command was applied in wrong context."
  18044. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18045. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18046. (user-error "This command works only in special context like headlines or timestamps")))
  18047. (defun org-call-for-shift-select (cmd)
  18048. (let ((this-command-keys-shift-translated t))
  18049. (call-interactively cmd)))
  18050. (defun org-shifttab (&optional arg)
  18051. "Global visibility cycling or move to previous table field.
  18052. Call `org-table-previous-field' within a table.
  18053. When ARG is nil, cycle globally through visibility states.
  18054. When ARG is a numeric prefix, show contents of this level."
  18055. (interactive "P")
  18056. (cond
  18057. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18058. ((integerp arg)
  18059. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18060. (message "Content view to level: %d" arg)
  18061. (org-content (prefix-numeric-value arg2))
  18062. (org-cycle-show-empty-lines t)
  18063. (setq org-cycle-global-status 'overview)))
  18064. (t (call-interactively 'org-global-cycle))))
  18065. (defun org-shiftmetaleft ()
  18066. "Promote subtree or delete table column.
  18067. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18068. `org-table-delete-column', depending on context. See the
  18069. individual commands for more information."
  18070. (interactive)
  18071. (cond
  18072. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18073. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18074. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18075. ((if (not (org-region-active-p)) (org-at-item-p)
  18076. (save-excursion (goto-char (region-beginning))
  18077. (org-at-item-p)))
  18078. (call-interactively 'org-outdent-item-tree))
  18079. (t (org-modifier-cursor-error))))
  18080. (defun org-shiftmetaright ()
  18081. "Demote subtree or insert table column.
  18082. Calls `org-demote-subtree', `org-indent-item-tree', or
  18083. `org-table-insert-column', depending on context. See the
  18084. individual commands for more information."
  18085. (interactive)
  18086. (cond
  18087. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18088. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18089. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18090. ((if (not (org-region-active-p)) (org-at-item-p)
  18091. (save-excursion (goto-char (region-beginning))
  18092. (org-at-item-p)))
  18093. (call-interactively 'org-indent-item-tree))
  18094. (t (org-modifier-cursor-error))))
  18095. (defun org-shiftmetaup (&optional arg)
  18096. "Move subtree up or kill table row.
  18097. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18098. `org-move-item-up' or `org-timestamp-up', depending on context.
  18099. See the individual commands for more information."
  18100. (interactive "P")
  18101. (cond
  18102. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18103. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18104. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18105. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18106. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18107. (call-interactively 'org-timestamp-up)))
  18108. (t (call-interactively 'org-drag-line-backward))))
  18109. (defun org-shiftmetadown (&optional arg)
  18110. "Move subtree down or insert table row.
  18111. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18112. `org-move-item-down' or `org-timestamp-up', depending on context.
  18113. See the individual commands for more information."
  18114. (interactive "P")
  18115. (cond
  18116. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18117. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18118. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18119. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18120. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18121. (call-interactively 'org-timestamp-down)))
  18122. (t (call-interactively 'org-drag-line-forward))))
  18123. (defsubst org-hidden-tree-error ()
  18124. (user-error
  18125. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18126. (defun org-metaleft (&optional arg)
  18127. "Promote heading or move table column to left.
  18128. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18129. With no specific context, calls the Emacs default `backward-word'.
  18130. See the individual commands for more information."
  18131. (interactive "P")
  18132. (cond
  18133. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18134. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18135. ((org-with-limited-levels
  18136. (or (org-at-heading-p)
  18137. (and (org-region-active-p)
  18138. (save-excursion
  18139. (goto-char (region-beginning))
  18140. (org-at-heading-p)))))
  18141. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18142. (call-interactively 'org-do-promote))
  18143. ;; At an inline task.
  18144. ((org-at-heading-p)
  18145. (call-interactively 'org-inlinetask-promote))
  18146. ((or (org-at-item-p)
  18147. (and (org-region-active-p)
  18148. (save-excursion
  18149. (goto-char (region-beginning))
  18150. (org-at-item-p))))
  18151. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18152. (call-interactively 'org-outdent-item))
  18153. (t (call-interactively 'backward-word))))
  18154. (defun org-metaright (&optional arg)
  18155. "Demote a subtree, a list item or move table column to right.
  18156. In front of a drawer or a block keyword, indent it correctly.
  18157. With no specific context, calls the Emacs default `forward-word'.
  18158. See the individual commands for more information."
  18159. (interactive "P")
  18160. (cond
  18161. ((run-hook-with-args-until-success 'org-metaright-hook))
  18162. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18163. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18164. ((org-at-block-p) (call-interactively 'org-indent-block))
  18165. ((org-with-limited-levels
  18166. (or (org-at-heading-p)
  18167. (and (org-region-active-p)
  18168. (save-excursion
  18169. (goto-char (region-beginning))
  18170. (org-at-heading-p)))))
  18171. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18172. (call-interactively 'org-do-demote))
  18173. ;; At an inline task.
  18174. ((org-at-heading-p)
  18175. (call-interactively 'org-inlinetask-demote))
  18176. ((or (org-at-item-p)
  18177. (and (org-region-active-p)
  18178. (save-excursion
  18179. (goto-char (region-beginning))
  18180. (org-at-item-p))))
  18181. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18182. (call-interactively 'org-indent-item))
  18183. (t (call-interactively 'forward-word))))
  18184. (defun org-check-for-hidden (what)
  18185. "Check if there are hidden headlines/items in the current visual line.
  18186. WHAT can be either `headlines' or `items'. If the current line is
  18187. an outline or item heading and it has a folded subtree below it,
  18188. this function returns t, nil otherwise."
  18189. (let ((re (cond
  18190. ((eq what 'headlines) org-outline-regexp-bol)
  18191. ((eq what 'items) (org-item-beginning-re))
  18192. (t (error "This should not happen"))))
  18193. beg end)
  18194. (save-excursion
  18195. (catch 'exit
  18196. (unless (org-region-active-p)
  18197. (setq beg (point-at-bol))
  18198. (beginning-of-line 2)
  18199. (while (and (not (eobp)) ;; this is like `next-line'
  18200. (get-char-property (1- (point)) 'invisible))
  18201. (beginning-of-line 2))
  18202. (setq end (point))
  18203. (goto-char beg)
  18204. (goto-char (point-at-eol))
  18205. (setq end (max end (point)))
  18206. (while (re-search-forward re end t)
  18207. (if (get-char-property (match-beginning 0) 'invisible)
  18208. (throw 'exit t))))
  18209. nil))))
  18210. (defun org-metaup (&optional arg)
  18211. "Move subtree up or move table row up.
  18212. Calls `org-move-subtree-up' or `org-table-move-row' or
  18213. `org-move-item-up', depending on context. See the individual commands
  18214. for more information."
  18215. (interactive "P")
  18216. (cond
  18217. ((run-hook-with-args-until-success 'org-metaup-hook))
  18218. ((org-region-active-p)
  18219. (let* ((a (min (region-beginning) (region-end)))
  18220. (b (1- (max (region-beginning) (region-end))))
  18221. (c (save-excursion (goto-char a)
  18222. (move-beginning-of-line 0)))
  18223. (d (save-excursion (goto-char a)
  18224. (move-end-of-line 0) (point))))
  18225. (transpose-regions a b c d)
  18226. (goto-char c)))
  18227. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18228. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18229. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18230. (t (org-drag-element-backward))))
  18231. (defun org-metadown (&optional arg)
  18232. "Move subtree down or move table row down.
  18233. Calls `org-move-subtree-down' or `org-table-move-row' or
  18234. `org-move-item-down', depending on context. See the individual
  18235. commands for more information."
  18236. (interactive "P")
  18237. (cond
  18238. ((run-hook-with-args-until-success 'org-metadown-hook))
  18239. ((org-region-active-p)
  18240. (let* ((a (min (region-beginning) (region-end)))
  18241. (b (max (region-beginning) (region-end)))
  18242. (c (save-excursion (goto-char b)
  18243. (move-beginning-of-line 1)))
  18244. (d (save-excursion (goto-char b)
  18245. (move-end-of-line 1) (1+ (point)))))
  18246. (transpose-regions a b c d)
  18247. (goto-char d)))
  18248. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18249. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18250. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18251. (t (org-drag-element-forward))))
  18252. (defun org-shiftup (&optional arg)
  18253. "Increase item in timestamp or increase priority of current headline.
  18254. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18255. depending on context. See the individual commands for more information."
  18256. (interactive "P")
  18257. (cond
  18258. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18259. ((and org-support-shift-select (org-region-active-p))
  18260. (org-call-for-shift-select 'previous-line))
  18261. ((org-at-timestamp-p t)
  18262. (call-interactively (if org-edit-timestamp-down-means-later
  18263. 'org-timestamp-down 'org-timestamp-up)))
  18264. ((and (not (eq org-support-shift-select 'always))
  18265. org-enable-priority-commands
  18266. (org-at-heading-p))
  18267. (call-interactively 'org-priority-up))
  18268. ((and (not org-support-shift-select) (org-at-item-p))
  18269. (call-interactively 'org-previous-item))
  18270. ((org-clocktable-try-shift 'up arg))
  18271. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18272. (org-support-shift-select
  18273. (org-call-for-shift-select 'previous-line))
  18274. (t (org-shiftselect-error))))
  18275. (defun org-shiftdown (&optional arg)
  18276. "Decrease item in timestamp or decrease priority of current headline.
  18277. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18278. depending on context. See the individual commands for more information."
  18279. (interactive "P")
  18280. (cond
  18281. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18282. ((and org-support-shift-select (org-region-active-p))
  18283. (org-call-for-shift-select 'next-line))
  18284. ((org-at-timestamp-p t)
  18285. (call-interactively (if org-edit-timestamp-down-means-later
  18286. 'org-timestamp-up 'org-timestamp-down)))
  18287. ((and (not (eq org-support-shift-select 'always))
  18288. org-enable-priority-commands
  18289. (org-at-heading-p))
  18290. (call-interactively 'org-priority-down))
  18291. ((and (not org-support-shift-select) (org-at-item-p))
  18292. (call-interactively 'org-next-item))
  18293. ((org-clocktable-try-shift 'down arg))
  18294. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18295. (org-support-shift-select
  18296. (org-call-for-shift-select 'next-line))
  18297. (t (org-shiftselect-error))))
  18298. (defun org-shiftright (&optional arg)
  18299. "Cycle the thing at point or in the current line, depending on context.
  18300. Depending on context, this does one of the following:
  18301. - switch a timestamp at point one day into the future
  18302. - on a headline, switch to the next TODO keyword.
  18303. - on an item, switch entire list to the next bullet type
  18304. - on a property line, switch to the next allowed value
  18305. - on a clocktable definition line, move time block into the future"
  18306. (interactive "P")
  18307. (cond
  18308. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18309. ((and org-support-shift-select (org-region-active-p))
  18310. (org-call-for-shift-select 'forward-char))
  18311. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18312. ((and (not (eq org-support-shift-select 'always))
  18313. (org-at-heading-p))
  18314. (let ((org-inhibit-logging
  18315. (not org-treat-S-cursor-todo-selection-as-state-change))
  18316. (org-inhibit-blocking
  18317. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18318. (org-call-with-arg 'org-todo 'right)))
  18319. ((or (and org-support-shift-select
  18320. (not (eq org-support-shift-select 'always))
  18321. (org-at-item-bullet-p))
  18322. (and (not org-support-shift-select) (org-at-item-p)))
  18323. (org-call-with-arg 'org-cycle-list-bullet nil))
  18324. ((and (not (eq org-support-shift-select 'always))
  18325. (org-at-property-p))
  18326. (call-interactively 'org-property-next-allowed-value))
  18327. ((org-clocktable-try-shift 'right arg))
  18328. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18329. (org-support-shift-select
  18330. (org-call-for-shift-select 'forward-char))
  18331. (t (org-shiftselect-error))))
  18332. (defun org-shiftleft (&optional arg)
  18333. "Cycle the thing at point or in the current line, depending on context.
  18334. Depending on context, this does one of the following:
  18335. - switch a timestamp at point one day into the past
  18336. - on a headline, switch to the previous TODO keyword.
  18337. - on an item, switch entire list to the previous bullet type
  18338. - on a property line, switch to the previous allowed value
  18339. - on a clocktable definition line, move time block into the past"
  18340. (interactive "P")
  18341. (cond
  18342. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18343. ((and org-support-shift-select (org-region-active-p))
  18344. (org-call-for-shift-select 'backward-char))
  18345. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18346. ((and (not (eq org-support-shift-select 'always))
  18347. (org-at-heading-p))
  18348. (let ((org-inhibit-logging
  18349. (not org-treat-S-cursor-todo-selection-as-state-change))
  18350. (org-inhibit-blocking
  18351. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18352. (org-call-with-arg 'org-todo 'left)))
  18353. ((or (and org-support-shift-select
  18354. (not (eq org-support-shift-select 'always))
  18355. (org-at-item-bullet-p))
  18356. (and (not org-support-shift-select) (org-at-item-p)))
  18357. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18358. ((and (not (eq org-support-shift-select 'always))
  18359. (org-at-property-p))
  18360. (call-interactively 'org-property-previous-allowed-value))
  18361. ((org-clocktable-try-shift 'left arg))
  18362. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18363. (org-support-shift-select
  18364. (org-call-for-shift-select 'backward-char))
  18365. (t (org-shiftselect-error))))
  18366. (defun org-shiftcontrolright ()
  18367. "Switch to next TODO set."
  18368. (interactive)
  18369. (cond
  18370. ((and org-support-shift-select (org-region-active-p))
  18371. (org-call-for-shift-select 'forward-word))
  18372. ((and (not (eq org-support-shift-select 'always))
  18373. (org-at-heading-p))
  18374. (org-call-with-arg 'org-todo 'nextset))
  18375. (org-support-shift-select
  18376. (org-call-for-shift-select 'forward-word))
  18377. (t (org-shiftselect-error))))
  18378. (defun org-shiftcontrolleft ()
  18379. "Switch to previous TODO set."
  18380. (interactive)
  18381. (cond
  18382. ((and org-support-shift-select (org-region-active-p))
  18383. (org-call-for-shift-select 'backward-word))
  18384. ((and (not (eq org-support-shift-select 'always))
  18385. (org-at-heading-p))
  18386. (org-call-with-arg 'org-todo 'previousset))
  18387. (org-support-shift-select
  18388. (org-call-for-shift-select 'backward-word))
  18389. (t (org-shiftselect-error))))
  18390. (defun org-shiftcontrolup (&optional n)
  18391. "Change timestamps synchronously up in CLOCK log lines.
  18392. Optional argument N tells to change by that many units."
  18393. (interactive "P")
  18394. (cond ((and (not org-support-shift-select)
  18395. (org-at-clock-log-p)
  18396. (org-at-timestamp-p t))
  18397. (org-clock-timestamps-up n))
  18398. (t (org-shiftselect-error))))
  18399. (defun org-shiftcontroldown (&optional n)
  18400. "Change timestamps synchronously down in CLOCK log lines.
  18401. Optional argument N tells to change by that many units."
  18402. (interactive "P")
  18403. (cond ((and (not org-support-shift-select)
  18404. (org-at-clock-log-p)
  18405. (org-at-timestamp-p t))
  18406. (org-clock-timestamps-down n))
  18407. (t (org-shiftselect-error))))
  18408. (defun org-ctrl-c-ret ()
  18409. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18410. (interactive)
  18411. (cond
  18412. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18413. (t (call-interactively 'org-insert-heading))))
  18414. (defun org-find-visible ()
  18415. (let ((s (point)))
  18416. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18417. (get-char-property s 'invisible)))
  18418. s))
  18419. (defun org-find-invisible ()
  18420. (let ((s (point)))
  18421. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18422. (not (get-char-property s 'invisible))))
  18423. s))
  18424. (defun org-copy-visible (beg end)
  18425. "Copy the visible parts of the region."
  18426. (interactive "r")
  18427. (let (snippets s)
  18428. (save-excursion
  18429. (save-restriction
  18430. (narrow-to-region beg end)
  18431. (setq s (goto-char (point-min)))
  18432. (while (not (= (point) (point-max)))
  18433. (goto-char (org-find-invisible))
  18434. (push (buffer-substring s (point)) snippets)
  18435. (setq s (goto-char (org-find-visible))))))
  18436. (kill-new (apply 'concat (nreverse snippets)))))
  18437. (defun org-copy-special ()
  18438. "Copy region in table or copy current subtree.
  18439. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18440. See the individual commands for more information."
  18441. (interactive)
  18442. (call-interactively
  18443. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18444. (defun org-cut-special ()
  18445. "Cut region in table or cut current subtree.
  18446. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18447. See the individual commands for more information."
  18448. (interactive)
  18449. (call-interactively
  18450. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18451. (defun org-paste-special (arg)
  18452. "Paste rectangular region into table, or past subtree relative to level.
  18453. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18454. See the individual commands for more information."
  18455. (interactive "P")
  18456. (if (org-at-table-p)
  18457. (org-table-paste-rectangle)
  18458. (org-paste-subtree arg)))
  18459. (defsubst org-in-fixed-width-region-p ()
  18460. "Is point in a fixed-width region?"
  18461. (save-match-data
  18462. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18463. (defun org-edit-special (&optional arg)
  18464. "Call a special editor for the element at point.
  18465. When at a table, call the formula editor with `org-table-edit-formulas'.
  18466. When in a source code block, call `org-edit-src-code'.
  18467. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18468. When at an #+INCLUDE keyword, visit the included file.
  18469. On a link, call `ffap' to visit the link at point.
  18470. Otherwise, return a user error."
  18471. (interactive "P")
  18472. (let ((element (org-element-at-point)))
  18473. (assert (not buffer-read-only) nil
  18474. "Buffer is read-only: %s" (buffer-name))
  18475. (case (org-element-type element)
  18476. (src-block
  18477. (if (not arg) (org-edit-src-code)
  18478. (let* ((info (org-babel-get-src-block-info))
  18479. (lang (nth 0 info))
  18480. (params (nth 2 info))
  18481. (session (cdr (assq :session params))))
  18482. (if (not session) (org-edit-src-code)
  18483. ;; At a src-block with a session and function called with
  18484. ;; an ARG: switch to the buffer related to the inferior
  18485. ;; process.
  18486. (switch-to-buffer
  18487. (funcall (intern (concat "org-babel-prep-session:" lang))
  18488. session params))))))
  18489. (keyword
  18490. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18491. (find-file
  18492. (org-remove-double-quotes
  18493. (car (org-split-string (org-element-property :value element)))))
  18494. (user-error "No special environment to edit here")))
  18495. (table
  18496. (if (eq (org-element-property :type element) 'table.el)
  18497. (org-edit-src-code)
  18498. (call-interactively 'org-table-edit-formulas)))
  18499. ;; Only Org tables contain `table-row' type elements.
  18500. (table-row (call-interactively 'org-table-edit-formulas))
  18501. ((example-block export-block) (org-edit-src-code))
  18502. (fixed-width (org-edit-fixed-width-region))
  18503. (otherwise
  18504. ;; No notable element at point. Though, we may be at a link,
  18505. ;; which is an object. Thus, scan deeper.
  18506. (if (eq (org-element-type (org-element-context element)) 'link)
  18507. (call-interactively 'ffap)
  18508. (user-error "No special environment to edit here"))))))
  18509. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18510. (defun org-ctrl-c-ctrl-c (&optional arg)
  18511. "Set tags in headline, or update according to changed information at point.
  18512. This command does many different things, depending on context:
  18513. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18514. this is what we do.
  18515. - If the cursor is on a statistics cookie, update it.
  18516. - If the cursor is in a headline, prompt for tags and insert them
  18517. into the current line, aligned to `org-tags-column'. When called
  18518. with prefix arg, realign all tags in the current buffer.
  18519. - If the cursor is in one of the special #+KEYWORD lines, this
  18520. triggers scanning the buffer for these lines and updating the
  18521. information.
  18522. - If the cursor is inside a table, realign the table. This command
  18523. works even if the automatic table editor has been turned off.
  18524. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18525. the entire table.
  18526. - If the cursor is at a footnote reference or definition, jump to
  18527. the corresponding definition or references, respectively.
  18528. - If the cursor is a the beginning of a dynamic block, update it.
  18529. - If the current buffer is a capture buffer, close note and file it.
  18530. - If the cursor is on a <<<target>>>, update radio targets and
  18531. corresponding links in this buffer.
  18532. - If the cursor is on a numbered item in a plain list, renumber the
  18533. ordered list.
  18534. - If the cursor is on a checkbox, toggle it.
  18535. - If the cursor is on a code block, evaluate it. The variable
  18536. `org-confirm-babel-evaluate' can be used to control prompting
  18537. before code block evaluation, by default every code block
  18538. evaluation requires confirmation. Code block evaluation can be
  18539. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18540. (interactive "P")
  18541. (cond
  18542. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18543. org-occur-highlights
  18544. org-latex-fragment-image-overlays)
  18545. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18546. (org-remove-occur-highlights)
  18547. (org-remove-latex-fragment-image-overlays)
  18548. (message "Temporary highlights/overlays removed from current buffer"))
  18549. ((and (local-variable-p 'org-finish-function (current-buffer))
  18550. (fboundp org-finish-function))
  18551. (funcall org-finish-function))
  18552. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18553. (t
  18554. (let* ((context (org-element-context)) (type (org-element-type context)))
  18555. ;; Test if point is within a blank line.
  18556. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18557. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18558. (user-error "C-c C-c can do nothing useful at this location"))
  18559. ;; When at a link, act according to the parent instead.
  18560. (when (eq type 'link)
  18561. (setq context (org-element-property :parent context))
  18562. (setq type (org-element-type context)))
  18563. ;; For convenience: at the first line of a paragraph on the
  18564. ;; same line as an item, apply function on that item instead.
  18565. (when (eq type 'paragraph)
  18566. (let ((parent (org-element-property :parent context)))
  18567. (when (and (eq (org-element-type parent) 'item)
  18568. (= (point-at-bol) (org-element-property :begin parent)))
  18569. (setq context parent type 'item))))
  18570. ;; Act according to type of element or object at point.
  18571. (case type
  18572. (clock (org-clock-update-time-maybe))
  18573. (dynamic-block
  18574. (save-excursion
  18575. (goto-char (org-element-property :post-affiliated context))
  18576. (org-update-dblock)))
  18577. (footnote-definition
  18578. (goto-char (org-element-property :post-affiliated context))
  18579. (call-interactively 'org-footnote-action))
  18580. (footnote-reference (call-interactively 'org-footnote-action))
  18581. ((headline inlinetask)
  18582. (save-excursion (goto-char (org-element-property :begin context))
  18583. (call-interactively 'org-set-tags)))
  18584. (item
  18585. ;; At an item: a double C-u set checkbox to "[-]"
  18586. ;; unconditionally, whereas a single one will toggle its
  18587. ;; presence. Without an universal argument, if the item
  18588. ;; has a checkbox, toggle it. Otherwise repair the list.
  18589. (let* ((box (org-element-property :checkbox context))
  18590. (struct (org-element-property :structure context))
  18591. (old-struct (copy-tree struct))
  18592. (parents (org-list-parents-alist struct))
  18593. (prevs (org-list-prevs-alist struct))
  18594. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18595. (org-list-set-checkbox
  18596. (org-element-property :begin context) struct
  18597. (cond ((equal arg '(16)) "[-]")
  18598. ((and (not box) (equal arg '(4))) "[ ]")
  18599. ((or (not box) (equal arg '(4))) nil)
  18600. ((eq box 'on) "[ ]")
  18601. (t "[X]")))
  18602. ;; Mimic `org-list-write-struct' but with grabbing
  18603. ;; a return value from `org-list-struct-fix-box'.
  18604. (org-list-struct-fix-ind struct parents 2)
  18605. (org-list-struct-fix-item-end struct)
  18606. (org-list-struct-fix-bul struct prevs)
  18607. (org-list-struct-fix-ind struct parents)
  18608. (let ((block-item
  18609. (org-list-struct-fix-box struct parents prevs orderedp)))
  18610. (if (and box (equal struct old-struct))
  18611. (if (equal arg '(16))
  18612. (message "Checkboxes already reset")
  18613. (user-error "Cannot toggle this checkbox: %s"
  18614. (if (eq box 'on)
  18615. "all subitems checked"
  18616. "unchecked subitems")))
  18617. (org-list-struct-apply-struct struct old-struct)
  18618. (org-update-checkbox-count-maybe))
  18619. (when block-item
  18620. (message "Checkboxes were removed due to empty box at line %d"
  18621. (org-current-line block-item))))))
  18622. (keyword
  18623. (let ((org-inhibit-startup-visibility-stuff t)
  18624. (org-startup-align-all-tables nil))
  18625. (when (boundp 'org-table-coordinate-overlays)
  18626. (mapc 'delete-overlay org-table-coordinate-overlays)
  18627. (setq org-table-coordinate-overlays nil))
  18628. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18629. (message "Local setup has been refreshed"))
  18630. (plain-list
  18631. ;; At a plain list, with a double C-u argument, set
  18632. ;; checkboxes of each item to "[-]", whereas a single one
  18633. ;; will toggle their presence according to the state of the
  18634. ;; first item in the list. Without an argument, repair the
  18635. ;; list.
  18636. (let* ((begin (org-element-property :contents-begin context))
  18637. (beginm (move-marker (make-marker) begin))
  18638. (struct (org-element-property :structure context))
  18639. (old-struct (copy-tree struct))
  18640. (first-box (save-excursion
  18641. (goto-char begin)
  18642. (looking-at org-list-full-item-re)
  18643. (match-string-no-properties 3)))
  18644. (new-box (cond ((equal arg '(16)) "[-]")
  18645. ((equal arg '(4)) (unless first-box "[ ]"))
  18646. ((equal first-box "[X]") "[ ]")
  18647. (t "[X]"))))
  18648. (cond
  18649. (arg
  18650. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18651. (org-list-get-all-items
  18652. begin struct (org-list-prevs-alist struct))))
  18653. ((and first-box (eq (point) begin))
  18654. ;; For convenience, when point is at bol on the first
  18655. ;; item of the list and no argument is provided, simply
  18656. ;; toggle checkbox of that item, if any.
  18657. (org-list-set-checkbox begin struct new-box)))
  18658. (org-list-write-struct
  18659. struct (org-list-parents-alist struct) old-struct)
  18660. (org-update-checkbox-count-maybe)
  18661. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18662. ((property-drawer node-property)
  18663. (call-interactively 'org-property-action))
  18664. ((radio-target target)
  18665. (call-interactively 'org-update-radio-target-regexp))
  18666. (statistics-cookie
  18667. (call-interactively 'org-update-statistics-cookies))
  18668. ((table table-cell table-row)
  18669. ;; At a table, recalculate every field and align it. Also
  18670. ;; send the table if necessary. If the table has
  18671. ;; a `table.el' type, just give up. At a table row or
  18672. ;; cell, maybe recalculate line but always align table.
  18673. (if (eq (org-element-property :type context) 'table.el)
  18674. (message "Use C-c ' to edit table.el tables")
  18675. (let ((org-enable-table-editor t))
  18676. (if (or (eq type 'table)
  18677. ;; Check if point is at a TBLFM line.
  18678. (and (eq type 'table-row)
  18679. (= (point) (org-element-property :end context))))
  18680. (save-excursion
  18681. (if (org-at-TBLFM-p)
  18682. (progn (require 'org-table)
  18683. (org-table-calc-current-TBLFM))
  18684. (goto-char (org-element-property :contents-begin context))
  18685. (org-call-with-arg 'org-table-recalculate (or arg t))
  18686. (orgtbl-send-table 'maybe)))
  18687. (org-table-maybe-eval-formula)
  18688. (cond (arg (call-interactively 'org-table-recalculate))
  18689. ((org-table-maybe-recalculate-line))
  18690. (t (org-table-align)))))))
  18691. (timestamp (org-timestamp-change 0 'day))
  18692. (otherwise
  18693. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18694. (user-error
  18695. "C-c C-c can do nothing useful at this location")))))))))
  18696. (defun org-mode-restart ()
  18697. "Restart Org-mode, to scan again for special lines.
  18698. Also updates the keyword regular expressions."
  18699. (interactive)
  18700. (org-mode)
  18701. (message "Org-mode restarted"))
  18702. (defun org-kill-note-or-show-branches ()
  18703. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18704. (interactive)
  18705. (if (not org-finish-function)
  18706. (progn
  18707. (hide-subtree)
  18708. (call-interactively 'show-branches))
  18709. (let ((org-note-abort t))
  18710. (funcall org-finish-function))))
  18711. (defun org-open-line (n)
  18712. "Insert a new row in tables, call `open-line' elsewhere.
  18713. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18714. (interactive "*p")
  18715. (cond
  18716. ((not org-special-ctrl-o)
  18717. (open-line n))
  18718. ((org-at-table-p)
  18719. (org-table-insert-row))
  18720. (t
  18721. (open-line n))))
  18722. (defun org-return (&optional indent)
  18723. "Goto next table row or insert a newline.
  18724. Calls `org-table-next-row' or `newline', depending on context.
  18725. See the individual commands for more information."
  18726. (interactive)
  18727. (let (org-ts-what)
  18728. (cond
  18729. ((or (bobp) (org-in-src-block-p))
  18730. (if indent (newline-and-indent) (newline)))
  18731. ((org-at-table-p)
  18732. (org-table-justify-field-maybe)
  18733. (call-interactively 'org-table-next-row))
  18734. ;; when `newline-and-indent' is called within a list, make sure
  18735. ;; text moved stays inside the item.
  18736. ((and (org-in-item-p) indent)
  18737. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18738. (progn
  18739. (save-match-data (newline))
  18740. (org-indent-line-to (length (match-string 0))))
  18741. (let ((ind (org-get-indentation)))
  18742. (newline)
  18743. (if (org-looking-back org-list-end-re)
  18744. (org-indent-line)
  18745. (org-indent-line-to ind)))))
  18746. ((and org-return-follows-link
  18747. (org-at-timestamp-p t)
  18748. (not (eq org-ts-what 'after)))
  18749. (org-follow-timestamp-link))
  18750. ((and org-return-follows-link
  18751. (let ((tprop (get-text-property (point) 'face)))
  18752. (or (eq tprop 'org-link)
  18753. (and (listp tprop) (memq 'org-link tprop)))))
  18754. (call-interactively 'org-open-at-point))
  18755. ((and (org-at-heading-p)
  18756. (looking-at
  18757. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18758. (org-show-entry)
  18759. (end-of-line 1)
  18760. (newline))
  18761. (t (if indent (newline-and-indent) (newline))))))
  18762. (defun org-return-indent ()
  18763. "Goto next table row or insert a newline and indent.
  18764. Calls `org-table-next-row' or `newline-and-indent', depending on
  18765. context. See the individual commands for more information."
  18766. (interactive)
  18767. (org-return t))
  18768. (defun org-ctrl-c-star ()
  18769. "Compute table, or change heading status of lines.
  18770. Calls `org-table-recalculate' or `org-toggle-heading',
  18771. depending on context."
  18772. (interactive)
  18773. (cond
  18774. ((org-at-table-p)
  18775. (call-interactively 'org-table-recalculate))
  18776. (t
  18777. ;; Convert all lines in region to list items
  18778. (call-interactively 'org-toggle-heading))))
  18779. (defun org-ctrl-c-minus ()
  18780. "Insert separator line in table or modify bullet status of line.
  18781. Also turns a plain line or a region of lines into list items.
  18782. Calls `org-table-insert-hline', `org-toggle-item', or
  18783. `org-cycle-list-bullet', depending on context."
  18784. (interactive)
  18785. (cond
  18786. ((org-at-table-p)
  18787. (call-interactively 'org-table-insert-hline))
  18788. ((org-region-active-p)
  18789. (call-interactively 'org-toggle-item))
  18790. ((org-in-item-p)
  18791. (call-interactively 'org-cycle-list-bullet))
  18792. (t
  18793. (call-interactively 'org-toggle-item))))
  18794. (defun org-toggle-item (arg)
  18795. "Convert headings or normal lines to items, items to normal lines.
  18796. If there is no active region, only the current line is considered.
  18797. If the first non blank line in the region is a headline, convert
  18798. all headlines to items, shifting text accordingly.
  18799. If it is an item, convert all items to normal lines.
  18800. If it is normal text, change region into a list of items.
  18801. With a prefix argument ARG, change the region in a single item."
  18802. (interactive "P")
  18803. (let ((shift-text
  18804. (function
  18805. ;; Shift text in current section to IND, from point to END.
  18806. ;; The function leaves point to END line.
  18807. (lambda (ind end)
  18808. (let ((min-i 1000) (end (copy-marker end)))
  18809. ;; First determine the minimum indentation (MIN-I) of
  18810. ;; the text.
  18811. (save-excursion
  18812. (catch 'exit
  18813. (while (< (point) end)
  18814. (let ((i (org-get-indentation)))
  18815. (cond
  18816. ;; Skip blank lines and inline tasks.
  18817. ((looking-at "^[ \t]*$"))
  18818. ((looking-at org-outline-regexp-bol))
  18819. ;; We can't find less than 0 indentation.
  18820. ((zerop i) (throw 'exit (setq min-i 0)))
  18821. ((< i min-i) (setq min-i i))))
  18822. (forward-line))))
  18823. ;; Then indent each line so that a line indented to
  18824. ;; MIN-I becomes indented to IND. Ignore blank lines
  18825. ;; and inline tasks in the process.
  18826. (let ((delta (- ind min-i)))
  18827. (while (< (point) end)
  18828. (unless (or (looking-at "^[ \t]*$")
  18829. (looking-at org-outline-regexp-bol))
  18830. (org-indent-line-to (+ (org-get-indentation) delta)))
  18831. (forward-line)))))))
  18832. (skip-blanks
  18833. (function
  18834. ;; Return beginning of first non-blank line, starting from
  18835. ;; line at POS.
  18836. (lambda (pos)
  18837. (save-excursion
  18838. (goto-char pos)
  18839. (skip-chars-forward " \r\t\n")
  18840. (point-at-bol)))))
  18841. beg end)
  18842. ;; Determine boundaries of changes.
  18843. (if (org-region-active-p)
  18844. (setq beg (funcall skip-blanks (region-beginning))
  18845. end (copy-marker (region-end)))
  18846. (setq beg (funcall skip-blanks (point-at-bol))
  18847. end (copy-marker (point-at-eol))))
  18848. ;; Depending on the starting line, choose an action on the text
  18849. ;; between BEG and END.
  18850. (org-with-limited-levels
  18851. (save-excursion
  18852. (goto-char beg)
  18853. (cond
  18854. ;; Case 1. Start at an item: de-itemize. Note that it only
  18855. ;; happens when a region is active: `org-ctrl-c-minus'
  18856. ;; would call `org-cycle-list-bullet' otherwise.
  18857. ((org-at-item-p)
  18858. (while (< (point) end)
  18859. (when (org-at-item-p)
  18860. (skip-chars-forward " \t")
  18861. (delete-region (point) (match-end 0)))
  18862. (forward-line)))
  18863. ;; Case 2. Start at an heading: convert to items.
  18864. ((org-at-heading-p)
  18865. (let* ((bul (org-list-bullet-string "-"))
  18866. (bul-len (length bul))
  18867. ;; Indentation of the first heading. It should be
  18868. ;; relative to the indentation of its parent, if any.
  18869. (start-ind (save-excursion
  18870. (cond
  18871. ((not org-adapt-indentation) 0)
  18872. ((not (outline-previous-heading)) 0)
  18873. (t (length (match-string 0))))))
  18874. ;; Level of first heading. Further headings will be
  18875. ;; compared to it to determine hierarchy in the list.
  18876. (ref-level (org-reduced-level (org-outline-level))))
  18877. (while (< (point) end)
  18878. (let* ((level (org-reduced-level (org-outline-level)))
  18879. (delta (max 0 (- level ref-level))))
  18880. ;; If current headline is less indented than the first
  18881. ;; one, set it as reference, in order to preserve
  18882. ;; subtrees.
  18883. (when (< level ref-level) (setq ref-level level))
  18884. (replace-match bul t t)
  18885. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18886. ;; Ensure all text down to END (or SECTION-END) belongs
  18887. ;; to the newly created item.
  18888. (let ((section-end (save-excursion
  18889. (or (outline-next-heading) (point)))))
  18890. (forward-line)
  18891. (funcall shift-text
  18892. (+ start-ind (* (1+ delta) bul-len))
  18893. (min end section-end)))))))
  18894. ;; Case 3. Normal line with ARG: make the first line of region
  18895. ;; an item, and shift indentation of others lines to
  18896. ;; set them as item's body.
  18897. (arg (let* ((bul (org-list-bullet-string "-"))
  18898. (bul-len (length bul))
  18899. (ref-ind (org-get-indentation)))
  18900. (skip-chars-forward " \t")
  18901. (insert bul)
  18902. (forward-line)
  18903. (while (< (point) end)
  18904. ;; Ensure that lines less indented than first one
  18905. ;; still get included in item body.
  18906. (funcall shift-text
  18907. (+ ref-ind bul-len)
  18908. (min end (save-excursion (or (outline-next-heading)
  18909. (point)))))
  18910. (forward-line))))
  18911. ;; Case 4. Normal line without ARG: turn each non-item line
  18912. ;; into an item.
  18913. (t
  18914. (while (< (point) end)
  18915. (unless (or (org-at-heading-p) (org-at-item-p))
  18916. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18917. (replace-match
  18918. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18919. (forward-line))))))))
  18920. (defun org-toggle-heading (&optional nstars)
  18921. "Convert headings to normal text, or items or text to headings.
  18922. If there is no active region, only convert the current line.
  18923. With a \\[universal-argument] prefix, convert the whole list at
  18924. point into heading.
  18925. In a region:
  18926. - If the first non blank line is a headline, remove the stars
  18927. from all headlines in the region.
  18928. - If it is a normal line, turn each and every normal line (i.e.,
  18929. not an heading or an item) in the region into headings. If you
  18930. want to convert only the first line of this region, use one
  18931. universal prefix argument.
  18932. - If it is a plain list item, turn all plain list items into headings.
  18933. When converting a line into a heading, the number of stars is chosen
  18934. such that the lines become children of the current entry. However,
  18935. when a numeric prefix argument is given, its value determines the
  18936. number of stars to add."
  18937. (interactive "P")
  18938. (let ((skip-blanks
  18939. (function
  18940. ;; Return beginning of first non-blank line, starting from
  18941. ;; line at POS.
  18942. (lambda (pos)
  18943. (save-excursion
  18944. (goto-char pos)
  18945. (while (org-at-comment-p) (forward-line))
  18946. (skip-chars-forward " \r\t\n")
  18947. (point-at-bol)))))
  18948. beg end toggled)
  18949. ;; Determine boundaries of changes. If a universal prefix has
  18950. ;; been given, put the list in a region. If region ends at a bol,
  18951. ;; do not consider the last line to be in the region.
  18952. (when (and current-prefix-arg (org-at-item-p))
  18953. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18954. (org-mark-element))
  18955. (if (org-region-active-p)
  18956. (setq beg (funcall skip-blanks (region-beginning))
  18957. end (copy-marker (save-excursion
  18958. (goto-char (region-end))
  18959. (if (bolp) (point) (point-at-eol)))))
  18960. (setq beg (funcall skip-blanks (point-at-bol))
  18961. end (copy-marker (point-at-eol))))
  18962. ;; Ensure inline tasks don't count as headings.
  18963. (org-with-limited-levels
  18964. (save-excursion
  18965. (goto-char beg)
  18966. (cond
  18967. ;; Case 1. Started at an heading: de-star headings.
  18968. ((org-at-heading-p)
  18969. (while (< (point) end)
  18970. (when (org-at-heading-p t)
  18971. (looking-at org-outline-regexp) (replace-match "")
  18972. (setq toggled t))
  18973. (forward-line)))
  18974. ;; Case 2. Started at an item: change items into headlines.
  18975. ;; One star will be added by `org-list-to-subtree'.
  18976. ((org-at-item-p)
  18977. (let* ((stars (make-string
  18978. ;; subtract the star that will be added again by
  18979. ;; `org-list-to-subtree'
  18980. (if (numberp nstars) (1- nstars)
  18981. (or (org-current-level) 0))
  18982. ?*))
  18983. (add-stars
  18984. (cond (nstars "") ; stars from prefix only
  18985. ((equal stars "") "") ; before first heading
  18986. (org-odd-levels-only "*") ; inside heading, odd
  18987. (t "")))) ; inside heading, oddeven
  18988. (while (< (point) end)
  18989. (when (org-at-item-p)
  18990. ;; Pay attention to cases when region ends before list.
  18991. (let* ((struct (org-list-struct))
  18992. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18993. (save-restriction
  18994. (narrow-to-region (point) list-end)
  18995. (insert
  18996. (org-list-to-subtree
  18997. (org-list-parse-list t)
  18998. '(:istart (concat stars add-stars (funcall get-stars depth))
  18999. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19000. (setq toggled t))
  19001. (forward-line))))
  19002. ;; Case 3. Started at normal text: make every line an heading,
  19003. ;; skipping headlines and items.
  19004. (t (let* ((stars
  19005. (make-string
  19006. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19007. (add-stars
  19008. (cond (nstars "") ; stars from prefix only
  19009. ((equal stars "") "*") ; before first heading
  19010. (org-odd-levels-only "**") ; inside heading, odd
  19011. (t "*"))) ; inside heading, oddeven
  19012. (rpl (concat stars add-stars " "))
  19013. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19014. (while (< (point) (if (equal nstars '(4)) lend end))
  19015. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19016. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19017. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19018. (forward-line)))))))
  19019. (unless toggled (message "Cannot toggle heading from here"))))
  19020. (defun org-meta-return (&optional arg)
  19021. "Insert a new heading or wrap a region in a table.
  19022. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19023. on context. See the individual commands for more information."
  19024. (interactive "P")
  19025. (org-check-before-invisible-edit 'insert)
  19026. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19027. (let* ((element (org-element-at-point))
  19028. (type (org-element-type element)))
  19029. (when (eq type 'table-row)
  19030. (setq element (org-element-property :parent element))
  19031. (setq type 'table))
  19032. (if (and (eq type 'table)
  19033. (eq (org-element-property :type element) 'org)
  19034. (>= (point) (org-element-property :contents-begin element))
  19035. (< (point) (org-element-property :contents-end element)))
  19036. (call-interactively 'org-table-wrap-region)
  19037. (call-interactively 'org-insert-heading)))))
  19038. ;;; Menu entries
  19039. (defsubst org-in-subtree-not-table-p ()
  19040. "Are we in a subtree and not in a table?"
  19041. (and (not (org-before-first-heading-p))
  19042. (not (org-at-table-p))))
  19043. ;; Define the Org-mode menus
  19044. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19045. '("Tbl"
  19046. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19047. ["Next Field" org-cycle (org-at-table-p)]
  19048. ["Previous Field" org-shifttab (org-at-table-p)]
  19049. ["Next Row" org-return (org-at-table-p)]
  19050. "--"
  19051. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19052. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19053. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19054. "--"
  19055. ("Column"
  19056. ["Move Column Left" org-metaleft (org-at-table-p)]
  19057. ["Move Column Right" org-metaright (org-at-table-p)]
  19058. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19059. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19060. ("Row"
  19061. ["Move Row Up" org-metaup (org-at-table-p)]
  19062. ["Move Row Down" org-metadown (org-at-table-p)]
  19063. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19064. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19065. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19066. "--"
  19067. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19068. ("Rectangle"
  19069. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19070. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19071. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19072. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19073. "--"
  19074. ("Calculate"
  19075. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19076. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19077. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19078. "--"
  19079. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19080. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19081. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19082. "--"
  19083. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19084. "--"
  19085. ["Sum Column/Rectangle" org-table-sum
  19086. (or (org-at-table-p) (org-region-active-p))]
  19087. ["Which Column?" org-table-current-column (org-at-table-p)])
  19088. ["Debug Formulas"
  19089. org-table-toggle-formula-debugger
  19090. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19091. ["Show Col/Row Numbers"
  19092. org-table-toggle-coordinate-overlays
  19093. :style toggle
  19094. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19095. "--"
  19096. ["Create" org-table-create (and (not (org-at-table-p))
  19097. org-enable-table-editor)]
  19098. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19099. ["Import from File" org-table-import (not (org-at-table-p))]
  19100. ["Export to File" org-table-export (org-at-table-p)]
  19101. "--"
  19102. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19103. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19104. '("Org"
  19105. ("Show/Hide"
  19106. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19107. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19108. ["Sparse Tree..." org-sparse-tree t]
  19109. ["Reveal Context" org-reveal t]
  19110. ["Show All" show-all t]
  19111. "--"
  19112. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19113. "--"
  19114. ["New Heading" org-insert-heading t]
  19115. ("Navigate Headings"
  19116. ["Up" outline-up-heading t]
  19117. ["Next" outline-next-visible-heading t]
  19118. ["Previous" outline-previous-visible-heading t]
  19119. ["Next Same Level" outline-forward-same-level t]
  19120. ["Previous Same Level" outline-backward-same-level t]
  19121. "--"
  19122. ["Jump" org-goto t])
  19123. ("Edit Structure"
  19124. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19125. "--"
  19126. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19127. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19128. "--"
  19129. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19130. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19131. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19132. "--"
  19133. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19134. "--"
  19135. ["Copy visible text" org-copy-visible t]
  19136. "--"
  19137. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19138. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19139. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19140. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19141. "--"
  19142. ["Sort Region/Children" org-sort t]
  19143. "--"
  19144. ["Convert to odd levels" org-convert-to-odd-levels t]
  19145. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19146. ("Editing"
  19147. ["Emphasis..." org-emphasize t]
  19148. ["Edit Source Example" org-edit-special t]
  19149. "--"
  19150. ["Footnote new/jump" org-footnote-action t]
  19151. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19152. ("Archive"
  19153. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19154. "--"
  19155. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19156. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19157. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19158. )
  19159. "--"
  19160. ("Hyperlinks"
  19161. ["Store Link (Global)" org-store-link t]
  19162. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19163. ["Insert Link" org-insert-link t]
  19164. ["Follow Link" org-open-at-point t]
  19165. "--"
  19166. ["Next link" org-next-link t]
  19167. ["Previous link" org-previous-link t]
  19168. "--"
  19169. ["Descriptive Links"
  19170. org-toggle-link-display
  19171. :style radio
  19172. :selected org-descriptive-links
  19173. ]
  19174. ["Literal Links"
  19175. org-toggle-link-display
  19176. :style radio
  19177. :selected (not org-descriptive-links)])
  19178. "--"
  19179. ("TODO Lists"
  19180. ["TODO/DONE/-" org-todo t]
  19181. ("Select keyword"
  19182. ["Next keyword" org-shiftright (org-at-heading-p)]
  19183. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19184. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19185. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19186. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19187. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19188. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19189. "--"
  19190. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19191. :selected org-enforce-todo-dependencies :style toggle :active t]
  19192. "Settings for tree at point"
  19193. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19194. :selected (org-entry-get nil "ORDERED")
  19195. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19196. ["Do Children parallel" org-toggle-ordered-property :style radio
  19197. :selected (not (org-entry-get nil "ORDERED"))
  19198. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19199. "--"
  19200. ["Set Priority" org-priority t]
  19201. ["Priority Up" org-shiftup t]
  19202. ["Priority Down" org-shiftdown t]
  19203. "--"
  19204. ["Get news from all feeds" org-feed-update-all t]
  19205. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19206. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19207. ("TAGS and Properties"
  19208. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19209. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19210. "--"
  19211. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19212. ["Column view of properties" org-columns t]
  19213. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19214. ("Dates and Scheduling"
  19215. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19216. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19217. ("Change Date"
  19218. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19219. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19220. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19221. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19222. ["Compute Time Range" org-evaluate-time-range t]
  19223. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19224. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19225. "--"
  19226. ["Custom time format" org-toggle-time-stamp-overlays
  19227. :style radio :selected org-display-custom-times]
  19228. "--"
  19229. ["Goto Calendar" org-goto-calendar t]
  19230. ["Date from Calendar" org-date-from-calendar t]
  19231. "--"
  19232. ["Start/Restart Timer" org-timer-start t]
  19233. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19234. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19235. ["Insert Timer String" org-timer t]
  19236. ["Insert Timer Item" org-timer-item t])
  19237. ("Logging work"
  19238. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19239. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19240. ["Clock out" org-clock-out t]
  19241. ["Clock cancel" org-clock-cancel t]
  19242. "--"
  19243. ["Mark as default task" org-clock-mark-default-task t]
  19244. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19245. ["Goto running clock" org-clock-goto t]
  19246. "--"
  19247. ["Display times" org-clock-display t]
  19248. ["Create clock table" org-clock-report t]
  19249. "--"
  19250. ["Record DONE time"
  19251. (progn (setq org-log-done (not org-log-done))
  19252. (message "Switching to %s will %s record a timestamp"
  19253. (car org-done-keywords)
  19254. (if org-log-done "automatically" "not")))
  19255. :style toggle :selected org-log-done])
  19256. "--"
  19257. ["Agenda Command..." org-agenda t]
  19258. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19259. ("File List for Agenda")
  19260. ("Special views current file"
  19261. ["TODO Tree" org-show-todo-tree t]
  19262. ["Check Deadlines" org-check-deadlines t]
  19263. ["Timeline" org-timeline t]
  19264. ["Tags/Property tree" org-match-sparse-tree t])
  19265. "--"
  19266. ["Export/Publish..." org-export-dispatch t]
  19267. ("LaTeX"
  19268. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19269. :selected org-cdlatex-mode]
  19270. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19271. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19272. ["Modify math symbol" org-cdlatex-math-modify
  19273. (org-inside-LaTeX-fragment-p)]
  19274. ["Insert citation" org-reftex-citation t]
  19275. "--"
  19276. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19277. "--"
  19278. ("MobileOrg"
  19279. ["Push Files and Views" org-mobile-push t]
  19280. ["Get Captured and Flagged" org-mobile-pull t]
  19281. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19282. "--"
  19283. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19284. "--"
  19285. ("Documentation"
  19286. ["Show Version" org-version t]
  19287. ["Info Documentation" org-info t])
  19288. ("Customize"
  19289. ["Browse Org Group" org-customize t]
  19290. "--"
  19291. ["Expand This Menu" org-create-customize-menu
  19292. (fboundp 'customize-menu-create)])
  19293. ["Send bug report" org-submit-bug-report t]
  19294. "--"
  19295. ("Refresh/Reload"
  19296. ["Refresh setup current buffer" org-mode-restart t]
  19297. ["Reload Org (after update)" org-reload t]
  19298. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19299. ))
  19300. (defun org-info (&optional node)
  19301. "Read documentation for Org-mode in the info system.
  19302. With optional NODE, go directly to that node."
  19303. (interactive)
  19304. (info (format "(org)%s" (or node ""))))
  19305. ;;;###autoload
  19306. (defun org-submit-bug-report ()
  19307. "Submit a bug report on Org-mode via mail.
  19308. Don't hesitate to report any problems or inaccurate documentation.
  19309. If you don't have setup sending mail from (X)Emacs, please copy the
  19310. output buffer into your mail program, as it gives us important
  19311. information about your Org-mode version and configuration."
  19312. (interactive)
  19313. (require 'reporter)
  19314. (org-load-modules-maybe)
  19315. (org-require-autoloaded-modules)
  19316. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19317. (reporter-submit-bug-report
  19318. "emacs-orgmode@gnu.org"
  19319. (org-version nil 'full)
  19320. (let (list)
  19321. (save-window-excursion
  19322. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19323. (delete-other-windows)
  19324. (erase-buffer)
  19325. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19326. We would like to add your full Org-mode and Outline configuration to the
  19327. bug report. This greatly simplifies the work of the maintainer and
  19328. other experts on the mailing list.
  19329. HOWEVER, some variables you have customized may contain private
  19330. information. The names of customers, colleagues, or friends, might
  19331. appear in the form of file names, tags, todo states, or search strings.
  19332. If you answer yes to the prompt, you might want to check and remove
  19333. such private information before sending the email.")
  19334. (add-text-properties (point-min) (point-max) '(face org-warning))
  19335. (when (yes-or-no-p "Include your Org-mode configuration ")
  19336. (mapatoms
  19337. (lambda (v)
  19338. (and (boundp v)
  19339. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19340. (or (and (symbol-value v)
  19341. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19342. (and
  19343. (get v 'custom-type) (get v 'standard-value)
  19344. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19345. (push v list)))))
  19346. (kill-buffer (get-buffer "*Warn about privacy*"))
  19347. list))
  19348. nil nil
  19349. "Remember to cover the basics, that is, what you expected to happen and
  19350. what in fact did happen. You don't know how to make a good report? See
  19351. http://orgmode.org/manual/Feedback.html#Feedback
  19352. Your bug report will be posted to the Org-mode mailing list.
  19353. ------------------------------------------------------------------------")
  19354. (save-excursion
  19355. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19356. (replace-match "\\1Bug: \\3 [\\2]")))))
  19357. (defun org-install-agenda-files-menu ()
  19358. (let ((bl (buffer-list)))
  19359. (save-excursion
  19360. (while bl
  19361. (set-buffer (pop bl))
  19362. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19363. (when (derived-mode-p 'org-mode)
  19364. (easy-menu-change
  19365. '("Org") "File List for Agenda"
  19366. (append
  19367. (list
  19368. ["Edit File List" (org-edit-agenda-file-list) t]
  19369. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19370. ["Remove Current File from List" org-remove-file t]
  19371. ["Cycle through agenda files" org-cycle-agenda-files t]
  19372. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19373. "--")
  19374. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19375. ;;;; Documentation
  19376. (defun org-require-autoloaded-modules ()
  19377. (interactive)
  19378. (mapc 'require
  19379. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19380. org-table org-timer)))
  19381. ;;;###autoload
  19382. (defun org-reload (&optional uncompiled)
  19383. "Reload all org lisp files.
  19384. With prefix arg UNCOMPILED, load the uncompiled versions."
  19385. (interactive "P")
  19386. (require 'loadhist)
  19387. (let* ((org-dir (org-find-library-dir "org"))
  19388. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19389. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19390. (remove-re (mapconcat 'identity
  19391. (mapcar (lambda (f) (concat "^" f "$"))
  19392. (list (if (featurep 'xemacs)
  19393. "org-colview"
  19394. "org-colview-xemacs")
  19395. "org" "org-loaddefs" "org-version"))
  19396. "\\|"))
  19397. (feats (delete-dups
  19398. (mapcar 'file-name-sans-extension
  19399. (mapcar 'file-name-nondirectory
  19400. (delq nil
  19401. (mapcar 'feature-file
  19402. features))))))
  19403. (lfeat (append
  19404. (sort
  19405. (setq feats
  19406. (delq nil (mapcar
  19407. (lambda (f)
  19408. (if (and (string-match feature-re f)
  19409. (not (string-match remove-re f)))
  19410. f nil))
  19411. feats)))
  19412. 'string-lessp)
  19413. (list "org-version" "org")))
  19414. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19415. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19416. load-uncore load-misses)
  19417. (setq load-misses
  19418. (delq 't
  19419. (mapcar (lambda (f)
  19420. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19421. (and (string= org-dir contrib-dir)
  19422. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19423. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19424. (add-to-list 'load-uncore f 'append)
  19425. 't)
  19426. f))
  19427. lfeat)))
  19428. (if load-uncore
  19429. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19430. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19431. (if load-misses
  19432. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19433. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19434. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19435. ;;;###autoload
  19436. (defun org-customize ()
  19437. "Call the customize function with org as argument."
  19438. (interactive)
  19439. (org-load-modules-maybe)
  19440. (org-require-autoloaded-modules)
  19441. (customize-browse 'org))
  19442. (defun org-create-customize-menu ()
  19443. "Create a full customization menu for Org-mode, insert it into the menu."
  19444. (interactive)
  19445. (org-load-modules-maybe)
  19446. (org-require-autoloaded-modules)
  19447. (if (fboundp 'customize-menu-create)
  19448. (progn
  19449. (easy-menu-change
  19450. '("Org") "Customize"
  19451. `(["Browse Org group" org-customize t]
  19452. "--"
  19453. ,(customize-menu-create 'org)
  19454. ["Set" Custom-set t]
  19455. ["Save" Custom-save t]
  19456. ["Reset to Current" Custom-reset-current t]
  19457. ["Reset to Saved" Custom-reset-saved t]
  19458. ["Reset to Standard Settings" Custom-reset-standard t]))
  19459. (message "\"Org\"-menu now contains full customization menu"))
  19460. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19461. ;;;; Miscellaneous stuff
  19462. ;;; Generally useful functions
  19463. (defun org-get-at-bol (property)
  19464. "Get text property PROPERTY at beginning of line."
  19465. (get-text-property (point-at-bol) property))
  19466. (defun org-find-text-property-in-string (prop s)
  19467. "Return the first non-nil value of property PROP in string S."
  19468. (or (get-text-property 0 prop s)
  19469. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19470. prop s)))
  19471. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19472. "Display the given MESSAGE as a warning."
  19473. (if (fboundp 'display-warning)
  19474. (display-warning 'org message
  19475. (if (featurep 'xemacs) 'warning :warning))
  19476. (let ((buf (get-buffer-create "*Org warnings*")))
  19477. (with-current-buffer buf
  19478. (goto-char (point-max))
  19479. (insert "Warning (Org): " message)
  19480. (unless (bolp)
  19481. (newline)))
  19482. (display-buffer buf)
  19483. (sit-for 0))))
  19484. (defun org-eval (form)
  19485. "Eval FORM and return result."
  19486. (condition-case error
  19487. (eval form)
  19488. (error (format "%%![Error: %s]" error))))
  19489. (defun org-in-clocktable-p ()
  19490. "Check if the cursor is in a clocktable."
  19491. (let ((pos (point)) start)
  19492. (save-excursion
  19493. (end-of-line 1)
  19494. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19495. (setq start (match-beginning 0))
  19496. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19497. (>= (match-end 0) pos)
  19498. start))))
  19499. (defun org-in-commented-line ()
  19500. "Is point in a line starting with `#'?"
  19501. (equal (char-after (point-at-bol)) ?#))
  19502. (defun org-in-indented-comment-line ()
  19503. "Is point in a line starting with `#' after some white space?"
  19504. (save-excursion
  19505. (save-match-data
  19506. (goto-char (point-at-bol))
  19507. (looking-at "[ \t]*#"))))
  19508. (defun org-in-verbatim-emphasis ()
  19509. (save-match-data
  19510. (and (org-in-regexp org-emph-re 2)
  19511. (>= (point) (match-beginning 3))
  19512. (<= (point) (match-end 4))
  19513. (member (match-string 3) '("=" "~")))))
  19514. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19515. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19516. (if (and marker (marker-buffer marker)
  19517. (buffer-live-p (marker-buffer marker)))
  19518. (progn
  19519. (org-pop-to-buffer-same-window (marker-buffer marker))
  19520. (if (or (> marker (point-max)) (< marker (point-min)))
  19521. (widen))
  19522. (goto-char marker)
  19523. (org-show-context 'org-goto))
  19524. (if bookmark
  19525. (bookmark-jump bookmark)
  19526. (error "Cannot find location"))))
  19527. (defun org-quote-csv-field (s)
  19528. "Quote field for inclusion in CSV material."
  19529. (if (string-match "[\",]" s)
  19530. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19531. s))
  19532. (defun org-force-self-insert (N)
  19533. "Needed to enforce self-insert under remapping."
  19534. (interactive "p")
  19535. (self-insert-command N))
  19536. (defun org-string-width (s)
  19537. "Compute width of string, ignoring invisible characters.
  19538. This ignores character with invisibility property `org-link', and also
  19539. characters with property `org-cwidth', because these will become invisible
  19540. upon the next fontification round."
  19541. (let (b l)
  19542. (when (or (eq t buffer-invisibility-spec)
  19543. (assq 'org-link buffer-invisibility-spec))
  19544. (while (setq b (text-property-any 0 (length s)
  19545. 'invisible 'org-link s))
  19546. (setq s (concat (substring s 0 b)
  19547. (substring s (or (next-single-property-change
  19548. b 'invisible s) (length s)))))))
  19549. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19550. (setq s (concat (substring s 0 b)
  19551. (substring s (or (next-single-property-change
  19552. b 'org-cwidth s) (length s))))))
  19553. (setq l (string-width s) b -1)
  19554. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19555. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19556. l))
  19557. (defun org-shorten-string (s maxlength)
  19558. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19559. If the string is shorter or has length MAXLENGTH, just return the
  19560. original string. If it is longer, the functions finds a space in the
  19561. string, breaks this string off at that locations and adds three dots
  19562. as ellipsis. Including the ellipsis, the string will not be longer
  19563. than MAXLENGTH. If finding a good breaking point in the string does
  19564. not work, the string is just chopped off in the middle of a word
  19565. if necessary."
  19566. (if (<= (length s) maxlength)
  19567. s
  19568. (let* ((n (max (- maxlength 4) 1))
  19569. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19570. (if (string-match re s)
  19571. (concat (match-string 1 s) "...")
  19572. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19573. (defun org-get-indentation (&optional line)
  19574. "Get the indentation of the current line, interpreting tabs.
  19575. When LINE is given, assume it represents a line and compute its indentation."
  19576. (if line
  19577. (if (string-match "^ *" (org-remove-tabs line))
  19578. (match-end 0))
  19579. (save-excursion
  19580. (beginning-of-line 1)
  19581. (skip-chars-forward " \t")
  19582. (current-column))))
  19583. (defun org-get-string-indentation (s)
  19584. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19585. (let ((n -1) (i 0) (w tab-width) c)
  19586. (catch 'exit
  19587. (while (< (setq n (1+ n)) (length s))
  19588. (setq c (aref s n))
  19589. (cond ((= c ?\ ) (setq i (1+ i)))
  19590. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19591. (t (throw 'exit t)))))
  19592. i))
  19593. (defun org-remove-tabs (s &optional width)
  19594. "Replace tabulators in S with spaces.
  19595. Assumes that s is a single line, starting in column 0."
  19596. (setq width (or width tab-width))
  19597. (while (string-match "\t" s)
  19598. (setq s (replace-match
  19599. (make-string
  19600. (- (* width (/ (+ (match-beginning 0) width) width))
  19601. (match-beginning 0)) ?\ )
  19602. t t s)))
  19603. s)
  19604. (defun org-fix-indentation (line ind)
  19605. "Fix indentation in LINE.
  19606. IND is a cons cell with target and minimum indentation.
  19607. If the current indentation in LINE is smaller than the minimum,
  19608. leave it alone. If it is larger than ind, set it to the target."
  19609. (let* ((l (org-remove-tabs line))
  19610. (i (org-get-indentation l))
  19611. (i1 (car ind)) (i2 (cdr ind)))
  19612. (if (>= i i2) (setq l (substring line i2)))
  19613. (if (> i1 0)
  19614. (concat (make-string i1 ?\ ) l)
  19615. l)))
  19616. (defun org-remove-indentation (code &optional n)
  19617. "Remove the maximum common indentation from the lines in CODE.
  19618. N may optionally be the number of spaces to remove."
  19619. (with-temp-buffer
  19620. (insert code)
  19621. (org-do-remove-indentation n)
  19622. (buffer-string)))
  19623. (defun org-do-remove-indentation (&optional n)
  19624. "Remove the maximum common indentation from the buffer."
  19625. (untabify (point-min) (point-max))
  19626. (let ((min 10000) re)
  19627. (if n
  19628. (setq min n)
  19629. (goto-char (point-min))
  19630. (while (re-search-forward "^ *[^ \n]" nil t)
  19631. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19632. (unless (or (= min 0) (= min 10000))
  19633. (setq re (format "^ \\{%d\\}" min))
  19634. (goto-char (point-min))
  19635. (while (re-search-forward re nil t)
  19636. (replace-match "")
  19637. (end-of-line 1))
  19638. min)))
  19639. (defun org-fill-template (template alist)
  19640. "Find each %key of ALIST in TEMPLATE and replace it."
  19641. (let ((case-fold-search nil)
  19642. entry key value)
  19643. (setq alist (sort (copy-sequence alist)
  19644. (lambda (a b) (< (length (car a)) (length (car b))))))
  19645. (while (setq entry (pop alist))
  19646. (setq template
  19647. (replace-regexp-in-string
  19648. (concat "%" (regexp-quote (car entry)))
  19649. (or (cdr entry) "") template t t)))
  19650. template))
  19651. (defun org-base-buffer (buffer)
  19652. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19653. (if (not buffer)
  19654. buffer
  19655. (or (buffer-base-buffer buffer)
  19656. buffer)))
  19657. (defun org-trim (s)
  19658. "Remove whitespace at beginning and end of string."
  19659. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19660. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19661. s)
  19662. (defun org-wrap (string &optional width lines)
  19663. "Wrap string to either a number of lines, or a width in characters.
  19664. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19665. that costs. If there is a word longer than WIDTH, the text is actually
  19666. wrapped to the length of that word.
  19667. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19668. many lines, whatever width that takes.
  19669. The return value is a list of lines, without newlines at the end."
  19670. (let* ((words (org-split-string string "[ \t\n]+"))
  19671. (maxword (apply 'max (mapcar 'org-string-width words)))
  19672. w ll)
  19673. (cond (width
  19674. (org-do-wrap words (max maxword width)))
  19675. (lines
  19676. (setq w maxword)
  19677. (setq ll (org-do-wrap words maxword))
  19678. (if (<= (length ll) lines)
  19679. ll
  19680. (setq ll words)
  19681. (while (> (length ll) lines)
  19682. (setq w (1+ w))
  19683. (setq ll (org-do-wrap words w)))
  19684. ll))
  19685. (t (error "Cannot wrap this")))))
  19686. (defun org-do-wrap (words width)
  19687. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19688. (let (lines line)
  19689. (while words
  19690. (setq line (pop words))
  19691. (while (and words (< (+ (length line) (length (car words))) width))
  19692. (setq line (concat line " " (pop words))))
  19693. (setq lines (push line lines)))
  19694. (nreverse lines)))
  19695. (defun org-split-string (string &optional separators)
  19696. "Splits STRING into substrings at SEPARATORS.
  19697. No empty strings are returned if there are matches at the beginning
  19698. and end of string."
  19699. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19700. (start 0)
  19701. notfirst
  19702. (list nil))
  19703. (while (and (string-match rexp string
  19704. (if (and notfirst
  19705. (= start (match-beginning 0))
  19706. (< start (length string)))
  19707. (1+ start) start))
  19708. (< (match-beginning 0) (length string)))
  19709. (setq notfirst t)
  19710. (or (eq (match-beginning 0) 0)
  19711. (and (eq (match-beginning 0) (match-end 0))
  19712. (eq (match-beginning 0) start))
  19713. (setq list
  19714. (cons (substring string start (match-beginning 0))
  19715. list)))
  19716. (setq start (match-end 0)))
  19717. (or (eq start (length string))
  19718. (setq list
  19719. (cons (substring string start)
  19720. list)))
  19721. (nreverse list)))
  19722. (defun org-quote-vert (s)
  19723. "Replace \"|\" with \"\\vert\"."
  19724. (while (string-match "|" s)
  19725. (setq s (replace-match "\\vert" t t s)))
  19726. s)
  19727. (defun org-uuidgen-p (s)
  19728. "Is S an ID created by UUIDGEN?"
  19729. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19730. (defun org-in-src-block-p (&optional inside)
  19731. "Whether point is in a code source block.
  19732. When INSIDE is non-nil, don't consider we are within a src block
  19733. when point is at #+BEGIN_SRC or #+END_SRC."
  19734. (let ((case-fold-search t) ov)
  19735. (or (and (setq ov (overlays-at (point)))
  19736. (memq 'org-block-background
  19737. (overlay-properties (car ov))))
  19738. (and (not inside)
  19739. (save-match-data
  19740. (save-excursion
  19741. (beginning-of-line)
  19742. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19743. (defun org-context ()
  19744. "Return a list of contexts of the current cursor position.
  19745. If several contexts apply, all are returned.
  19746. Each context entry is a list with a symbol naming the context, and
  19747. two positions indicating start and end of the context. Possible
  19748. contexts are:
  19749. :headline anywhere in a headline
  19750. :headline-stars on the leading stars in a headline
  19751. :todo-keyword on a TODO keyword (including DONE) in a headline
  19752. :tags on the TAGS in a headline
  19753. :priority on the priority cookie in a headline
  19754. :item on the first line of a plain list item
  19755. :item-bullet on the bullet/number of a plain list item
  19756. :checkbox on the checkbox in a plain list item
  19757. :table in an org-mode table
  19758. :table-special on a special filed in a table
  19759. :table-table in a table.el table
  19760. :clocktable in a clocktable
  19761. :src-block in a source block
  19762. :link on a hyperlink
  19763. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19764. :target on a <<target>>
  19765. :radio-target on a <<<radio-target>>>
  19766. :latex-fragment on a LaTeX fragment
  19767. :latex-preview on a LaTeX fragment with overlaid preview image
  19768. This function expects the position to be visible because it uses font-lock
  19769. faces as a help to recognize the following contexts: :table-special, :link,
  19770. and :keyword."
  19771. (let* ((f (get-text-property (point) 'face))
  19772. (faces (if (listp f) f (list f)))
  19773. (case-fold-search t)
  19774. (p (point)) clist o)
  19775. ;; First the large context
  19776. (cond
  19777. ((org-at-heading-p t)
  19778. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19779. (when (progn
  19780. (beginning-of-line 1)
  19781. (looking-at org-todo-line-tags-regexp))
  19782. (push (org-point-in-group p 1 :headline-stars) clist)
  19783. (push (org-point-in-group p 2 :todo-keyword) clist)
  19784. (push (org-point-in-group p 4 :tags) clist))
  19785. (goto-char p)
  19786. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19787. (if (looking-at "\\[#[A-Z0-9]\\]")
  19788. (push (org-point-in-group p 0 :priority) clist)))
  19789. ((org-at-item-p)
  19790. (push (org-point-in-group p 2 :item-bullet) clist)
  19791. (push (list :item (point-at-bol)
  19792. (save-excursion (org-end-of-item) (point)))
  19793. clist)
  19794. (and (org-at-item-checkbox-p)
  19795. (push (org-point-in-group p 0 :checkbox) clist)))
  19796. ((org-at-table-p)
  19797. (push (list :table (org-table-begin) (org-table-end)) clist)
  19798. (if (memq 'org-formula faces)
  19799. (push (list :table-special
  19800. (previous-single-property-change p 'face)
  19801. (next-single-property-change p 'face)) clist)))
  19802. ((org-at-table-p 'any)
  19803. (push (list :table-table) clist)))
  19804. (goto-char p)
  19805. (let ((case-fold-search t))
  19806. ;; New the "medium" contexts: clocktables, source blocks
  19807. (cond ((org-in-clocktable-p)
  19808. (push (list :clocktable
  19809. (and (or (looking-at "#\\+BEGIN: clocktable")
  19810. (search-backward "#+BEGIN: clocktable" nil t))
  19811. (match-beginning 0))
  19812. (and (re-search-forward "#\\+END:?" nil t)
  19813. (match-end 0))) clist))
  19814. ((org-in-src-block-p)
  19815. (push (list :src-block
  19816. (and (or (looking-at "#\\+BEGIN_SRC")
  19817. (search-backward "#+BEGIN_SRC" nil t))
  19818. (match-beginning 0))
  19819. (and (search-forward "#+END_SRC" nil t)
  19820. (match-beginning 0))) clist))))
  19821. (goto-char p)
  19822. ;; Now the small context
  19823. (cond
  19824. ((org-at-timestamp-p)
  19825. (push (org-point-in-group p 0 :timestamp) clist))
  19826. ((memq 'org-link faces)
  19827. (push (list :link
  19828. (previous-single-property-change p 'face)
  19829. (next-single-property-change p 'face)) clist))
  19830. ((memq 'org-special-keyword faces)
  19831. (push (list :keyword
  19832. (previous-single-property-change p 'face)
  19833. (next-single-property-change p 'face)) clist))
  19834. ((org-at-target-p)
  19835. (push (org-point-in-group p 0 :target) clist)
  19836. (goto-char (1- (match-beginning 0)))
  19837. (if (looking-at org-radio-target-regexp)
  19838. (push (org-point-in-group p 0 :radio-target) clist))
  19839. (goto-char p))
  19840. ((setq o (car (delq nil
  19841. (mapcar
  19842. (lambda (x)
  19843. (if (memq x org-latex-fragment-image-overlays) x))
  19844. (overlays-at (point))))))
  19845. (push (list :latex-fragment
  19846. (overlay-start o) (overlay-end o)) clist)
  19847. (push (list :latex-preview
  19848. (overlay-start o) (overlay-end o)) clist))
  19849. ((org-inside-LaTeX-fragment-p)
  19850. ;; FIXME: positions wrong.
  19851. (push (list :latex-fragment (point) (point)) clist)))
  19852. (setq clist (nreverse (delq nil clist)))
  19853. clist))
  19854. ;; FIXME: Compare with at-regexp-p Do we need both?
  19855. (defun org-in-regexp (re &optional nlines visually)
  19856. "Check if point is inside a match of regexp.
  19857. Normally only the current line is checked, but you can include NLINES extra
  19858. lines both before and after point into the search.
  19859. If VISUALLY is set, require that the cursor is not after the match but
  19860. really on, so that the block visually is on the match."
  19861. (catch 'exit
  19862. (let ((pos (point))
  19863. (eol (point-at-eol (+ 1 (or nlines 0))))
  19864. (inc (if visually 1 0)))
  19865. (save-excursion
  19866. (beginning-of-line (- 1 (or nlines 0)))
  19867. (while (re-search-forward re eol t)
  19868. (if (and (<= (match-beginning 0) pos)
  19869. (>= (+ inc (match-end 0)) pos))
  19870. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19871. (defun org-at-regexp-p (regexp)
  19872. "Is point inside a match of REGEXP in the current line?"
  19873. (catch 'exit
  19874. (save-excursion
  19875. (let ((pos (point)) (end (point-at-eol)))
  19876. (beginning-of-line 1)
  19877. (while (re-search-forward regexp end t)
  19878. (if (and (<= (match-beginning 0) pos)
  19879. (>= (match-end 0) pos))
  19880. (throw 'exit t)))
  19881. nil))))
  19882. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19883. "Non-nil when point is between matches of START-RE and END-RE.
  19884. Also return a non-nil value when point is on one of the matches.
  19885. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19886. buffer positions. Default values are the positions of headlines
  19887. surrounding the point.
  19888. The functions returns a cons cell whose car (resp. cdr) is the
  19889. position before START-RE (resp. after END-RE)."
  19890. (save-match-data
  19891. (let ((pos (point))
  19892. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19893. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19894. beg end)
  19895. (save-excursion
  19896. ;; Point is on a block when on START-RE or if START-RE can be
  19897. ;; found before it...
  19898. (and (or (org-at-regexp-p start-re)
  19899. (re-search-backward start-re limit-up t))
  19900. (setq beg (match-beginning 0))
  19901. ;; ... and END-RE after it...
  19902. (goto-char (match-end 0))
  19903. (re-search-forward end-re limit-down t)
  19904. (> (setq end (match-end 0)) pos)
  19905. ;; ... without another START-RE in-between.
  19906. (goto-char (match-beginning 0))
  19907. (not (re-search-backward start-re (1+ beg) t))
  19908. ;; Return value.
  19909. (cons beg end))))))
  19910. (defun org-in-block-p (names)
  19911. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19912. NAMES is a list of strings containing names of blocks.
  19913. Return first block name matched, or nil. Beware that in case of
  19914. nested blocks, the returned name may not belong to the closest
  19915. block from point."
  19916. (save-match-data
  19917. (catch 'exit
  19918. (let ((case-fold-search t)
  19919. (lim-up (save-excursion (outline-previous-heading)))
  19920. (lim-down (save-excursion (outline-next-heading))))
  19921. (mapc (lambda (name)
  19922. (let ((n (regexp-quote name)))
  19923. (when (org-between-regexps-p
  19924. (concat "^[ \t]*#\\+begin_" n)
  19925. (concat "^[ \t]*#\\+end_" n)
  19926. lim-up lim-down)
  19927. (throw 'exit n))))
  19928. names))
  19929. nil)))
  19930. (defun org-in-drawer-p ()
  19931. "Is point within a drawer?"
  19932. (save-match-data
  19933. (let ((case-fold-search t)
  19934. (lim-up (save-excursion (outline-previous-heading)))
  19935. (lim-down (save-excursion (outline-next-heading))))
  19936. (org-between-regexps-p
  19937. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19938. "^[ \t]*:end:.*$"
  19939. lim-up lim-down))))
  19940. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19941. "Call `multi-occur' with buffers for all agenda files."
  19942. (interactive "sOrg-files matching: \np")
  19943. (let* ((files (org-agenda-files))
  19944. (tnames (mapcar 'file-truename files))
  19945. (extra org-agenda-text-search-extra-files)
  19946. f)
  19947. (when (eq (car extra) 'agenda-archives)
  19948. (setq extra (cdr extra))
  19949. (setq files (org-add-archive-files files)))
  19950. (while (setq f (pop extra))
  19951. (unless (member (file-truename f) tnames)
  19952. (add-to-list 'files f 'append)
  19953. (add-to-list 'tnames (file-truename f) 'append)))
  19954. (multi-occur
  19955. (mapcar (lambda (x)
  19956. (with-current-buffer
  19957. (or (get-file-buffer x) (find-file-noselect x))
  19958. (widen)
  19959. (current-buffer)))
  19960. files)
  19961. regexp)))
  19962. (if (boundp 'occur-mode-find-occurrence-hook)
  19963. ;; Emacs 23
  19964. (add-hook 'occur-mode-find-occurrence-hook
  19965. (lambda ()
  19966. (when (derived-mode-p 'org-mode)
  19967. (org-reveal))))
  19968. ;; Emacs 22
  19969. (defadvice occur-mode-goto-occurrence
  19970. (after org-occur-reveal activate)
  19971. (and (derived-mode-p 'org-mode) (org-reveal)))
  19972. (defadvice occur-mode-goto-occurrence-other-window
  19973. (after org-occur-reveal activate)
  19974. (and (derived-mode-p 'org-mode) (org-reveal)))
  19975. (defadvice occur-mode-display-occurrence
  19976. (after org-occur-reveal activate)
  19977. (when (derived-mode-p 'org-mode)
  19978. (let ((pos (occur-mode-find-occurrence)))
  19979. (with-current-buffer (marker-buffer pos)
  19980. (save-excursion
  19981. (goto-char pos)
  19982. (org-reveal)))))))
  19983. (defun org-occur-link-in-agenda-files ()
  19984. "Create a link and search for it in the agendas.
  19985. The link is not stored in `org-stored-links', it is just created
  19986. for the search purpose."
  19987. (interactive)
  19988. (let ((link (condition-case nil
  19989. (org-store-link nil)
  19990. (error "Unable to create a link to here"))))
  19991. (org-occur-in-agenda-files (regexp-quote link))))
  19992. (defun org-reverse-string (string)
  19993. "Return the reverse of STRING."
  19994. (apply 'string (reverse (string-to-list string))))
  19995. (defsubst org-uniquify (list)
  19996. "Non-destructively remove duplicate elements from LIST."
  19997. (let ((res (copy-sequence list))) (delete-dups res)))
  19998. (defun org-uniquify-alist (alist)
  19999. "Merge elements of ALIST with the same key.
  20000. For example, in this alist:
  20001. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20002. => '((a 1 3) (b 2))
  20003. merge (a 1) and (a 3) into (a 1 3).
  20004. The function returns the new ALIST."
  20005. (let (rtn)
  20006. (mapc
  20007. (lambda (e)
  20008. (let (n)
  20009. (if (not (assoc (car e) rtn))
  20010. (push e rtn)
  20011. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20012. (setq rtn (assq-delete-all (car e) rtn))
  20013. (push n rtn))))
  20014. alist)
  20015. rtn))
  20016. (defun org-delete-all (elts list)
  20017. "Remove all elements in ELTS from LIST."
  20018. (while elts
  20019. (setq list (delete (pop elts) list)))
  20020. list)
  20021. (defun org-count (cl-item cl-seq)
  20022. "Count the number of occurrences of ITEM in SEQ.
  20023. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20024. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20025. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20026. (while (< cl-start cl-end)
  20027. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20028. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20029. (setq cl-start (1+ cl-start)))
  20030. cl-count))
  20031. (defun org-remove-if (predicate seq)
  20032. "Remove everything from SEQ that fulfills PREDICATE."
  20033. (let (res e)
  20034. (while seq
  20035. (setq e (pop seq))
  20036. (if (not (funcall predicate e)) (push e res)))
  20037. (nreverse res)))
  20038. (defun org-remove-if-not (predicate seq)
  20039. "Remove everything from SEQ that does not fulfill PREDICATE."
  20040. (let (res e)
  20041. (while seq
  20042. (setq e (pop seq))
  20043. (if (funcall predicate e) (push e res)))
  20044. (nreverse res)))
  20045. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20046. "Reduce two-argument FUNCTION across SEQ.
  20047. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20048. \":initial-value\" removed."
  20049. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20050. (cadr (memq :initial-value cl-keys)))
  20051. (cl-seq (pop cl-seq))
  20052. (t (funcall cl-func)))))
  20053. (while cl-seq
  20054. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20055. cl-accum))
  20056. (defun org-every (pred seq)
  20057. "Return true if PREDICATE is true of every element of SEQ.
  20058. Adapted from `every' in cl.el."
  20059. (catch 'org-every
  20060. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20061. t))
  20062. (defun org-some (pred seq)
  20063. "Return true if PREDICATE is true of any element of SEQ.
  20064. Adapted from `some' in cl.el."
  20065. (catch 'org-some
  20066. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20067. nil))
  20068. (defun org-back-over-empty-lines ()
  20069. "Move backwards over whitespace, to the beginning of the first empty line.
  20070. Returns the number of empty lines passed."
  20071. (let ((pos (point)))
  20072. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20073. (skip-chars-backward " \t\n\r")
  20074. (unless (eobp)
  20075. (forward-line -1)))
  20076. (beginning-of-line 2)
  20077. (goto-char (min (point) pos))
  20078. (count-lines (point) pos)))
  20079. (defun org-skip-whitespace ()
  20080. (skip-chars-forward " \t\n\r"))
  20081. (defun org-point-in-group (point group &optional context)
  20082. "Check if POINT is in match-group GROUP.
  20083. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20084. match. If the match group does not exist or point is not inside it,
  20085. return nil."
  20086. (and (match-beginning group)
  20087. (>= point (match-beginning group))
  20088. (<= point (match-end group))
  20089. (if context
  20090. (list context (match-beginning group) (match-end group))
  20091. t)))
  20092. (defun org-switch-to-buffer-other-window (&rest args)
  20093. "Switch to buffer in a second window on the current frame.
  20094. In particular, do not allow pop-up frames.
  20095. Returns the newly created buffer."
  20096. (org-no-popups
  20097. (apply 'switch-to-buffer-other-window args)))
  20098. (defun org-combine-plists (&rest plists)
  20099. "Create a single property list from all plists in PLISTS.
  20100. The process starts by copying the first list, and then setting properties
  20101. from the other lists. Settings in the last list are the most significant
  20102. ones and overrule settings in the other lists."
  20103. (let ((rtn (copy-sequence (pop plists)))
  20104. p v ls)
  20105. (while plists
  20106. (setq ls (pop plists))
  20107. (while ls
  20108. (setq p (pop ls) v (pop ls))
  20109. (setq rtn (plist-put rtn p v))))
  20110. rtn))
  20111. (defun org-replace-escapes (string table)
  20112. "Replace %-escapes in STRING with values in TABLE.
  20113. TABLE is an association list with keys like \"%a\" and string values.
  20114. The sequences in STRING may contain normal field width and padding information,
  20115. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20116. so values can contain further %-escapes if they are define later in TABLE."
  20117. (let ((tbl (copy-alist table))
  20118. (case-fold-search nil)
  20119. (pchg 0)
  20120. e re rpl)
  20121. (while (setq e (pop tbl))
  20122. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20123. (when (and (cdr e) (string-match re (cdr e)))
  20124. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20125. (safe "SREF"))
  20126. (add-text-properties 0 3 (list 'sref sref) safe)
  20127. (setcdr e (replace-match safe t t (cdr e)))))
  20128. (while (string-match re string)
  20129. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20130. (cdr e)))
  20131. (setq string (replace-match rpl t t string))))
  20132. (while (setq pchg (next-property-change pchg string))
  20133. (let ((sref (get-text-property pchg 'sref string)))
  20134. (when (and sref (string-match "SREF" string pchg))
  20135. (setq string (replace-match sref t t string)))))
  20136. string))
  20137. (defun org-sublist (list start end)
  20138. "Return a section of LIST, from START to END.
  20139. Counting starts at 1."
  20140. (let (rtn (c start))
  20141. (setq list (nthcdr (1- start) list))
  20142. (while (and list (<= c end))
  20143. (push (pop list) rtn)
  20144. (setq c (1+ c)))
  20145. (nreverse rtn)))
  20146. (defun org-find-base-buffer-visiting (file)
  20147. "Like `find-buffer-visiting' but always return the base buffer and
  20148. not an indirect buffer."
  20149. (let ((buf (or (get-file-buffer file)
  20150. (find-buffer-visiting file))))
  20151. (if buf
  20152. (or (buffer-base-buffer buf) buf)
  20153. nil)))
  20154. (defun org-image-file-name-regexp (&optional extensions)
  20155. "Return regexp matching the file names of images.
  20156. If EXTENSIONS is given, only match these."
  20157. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20158. (image-file-name-regexp)
  20159. (let ((image-file-name-extensions
  20160. (or extensions
  20161. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20162. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20163. (concat "\\."
  20164. (regexp-opt (nconc (mapcar 'upcase
  20165. image-file-name-extensions)
  20166. image-file-name-extensions)
  20167. t)
  20168. "\\'"))))
  20169. (defun org-file-image-p (file &optional extensions)
  20170. "Return non-nil if FILE is an image."
  20171. (save-match-data
  20172. (string-match (org-image-file-name-regexp extensions) file)))
  20173. (defun org-get-cursor-date (&optional with-time)
  20174. "Return the date at cursor in as a time.
  20175. This works in the calendar and in the agenda, anywhere else it just
  20176. returns the current time.
  20177. If WITH-TIME is non-nil, returns the time of the event at point (in
  20178. the agenda) or the current time of the day."
  20179. (let (date day defd tp tm hod mod)
  20180. (when with-time
  20181. (setq tp (get-text-property (point) 'time))
  20182. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20183. (setq hod (string-to-number (match-string 1 tp))
  20184. mod (string-to-number (match-string 2 tp))))
  20185. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20186. mod (nth 1 (decode-time (current-time))))))
  20187. (cond
  20188. ((eq major-mode 'calendar-mode)
  20189. (setq date (calendar-cursor-to-date)
  20190. defd (encode-time 0 (or mod 0) (or hod 0)
  20191. (nth 1 date) (nth 0 date) (nth 2 date))))
  20192. ((eq major-mode 'org-agenda-mode)
  20193. (setq day (get-text-property (point) 'day))
  20194. (if day
  20195. (setq date (calendar-gregorian-from-absolute day)
  20196. defd (encode-time 0 (or mod 0) (or hod 0)
  20197. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20198. (or defd (current-time))))
  20199. (defun org-mark-subtree (&optional up)
  20200. "Mark the current subtree.
  20201. This puts point at the start of the current subtree, and mark at
  20202. the end. If a numeric prefix UP is given, move up into the
  20203. hierarchy of headlines by UP levels before marking the subtree."
  20204. (interactive "P")
  20205. (org-with-limited-levels
  20206. (cond ((org-at-heading-p) (beginning-of-line))
  20207. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20208. (t (outline-previous-visible-heading 1))))
  20209. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20210. (if (org-called-interactively-p 'any)
  20211. (call-interactively 'org-mark-element)
  20212. (org-mark-element)))
  20213. ;;; Indentation
  20214. (defun org-indent-line ()
  20215. "Indent line depending on context."
  20216. (interactive)
  20217. (let* ((pos (point))
  20218. (itemp (org-at-item-p))
  20219. (case-fold-search t)
  20220. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20221. (inline-task-p (and (featurep 'org-inlinetask)
  20222. (org-inlinetask-in-task-p)))
  20223. (inline-re (and inline-task-p
  20224. (org-inlinetask-outline-regexp)))
  20225. column)
  20226. (if (and orgstruct-is-++ (eq pos (point)))
  20227. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20228. (indent-according-to-mode))
  20229. (beginning-of-line 1)
  20230. (cond
  20231. ;; Headings
  20232. ((looking-at org-outline-regexp) (setq column 0))
  20233. ;; Footnote definition
  20234. ((looking-at org-footnote-definition-re) (setq column 0))
  20235. ;; Literal examples
  20236. ((looking-at "[ \t]*:\\( \\|$\\)")
  20237. (setq column (org-get-indentation))) ; do nothing
  20238. ;; Lists
  20239. ((ignore-errors (goto-char (org-in-item-p)))
  20240. (setq column (if itemp
  20241. (org-get-indentation)
  20242. (org-list-item-body-column (point))))
  20243. (goto-char pos))
  20244. ;; Drawers
  20245. ((and (looking-at "[ \t]*:END:")
  20246. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20247. (save-excursion
  20248. (goto-char (1- (match-beginning 1)))
  20249. (setq column (current-column))))
  20250. ;; Special blocks
  20251. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20252. (save-excursion
  20253. (re-search-backward
  20254. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20255. (setq column (org-get-indentation (match-string 0))))
  20256. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20257. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20258. (save-excursion
  20259. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20260. (setq column
  20261. (cond ((equal (downcase (match-string 1)) "src")
  20262. ;; src blocks: let `org-edit-src-exit' handle them
  20263. (org-get-indentation))
  20264. ((equal (downcase (match-string 1)) "example")
  20265. (max (org-get-indentation)
  20266. (org-get-indentation (match-string 0))))
  20267. (t
  20268. (org-get-indentation (match-string 0))))))
  20269. ;; This line has nothing special, look at the previous relevant
  20270. ;; line to compute indentation
  20271. (t
  20272. (beginning-of-line 0)
  20273. (while (and (not (bobp))
  20274. (not (looking-at org-table-line-regexp))
  20275. (not (looking-at org-drawer-regexp))
  20276. ;; When point started in an inline task, do not move
  20277. ;; above task starting line.
  20278. (not (and inline-task-p (looking-at inline-re)))
  20279. ;; Skip drawers, blocks, empty lines, verbatim,
  20280. ;; comments, tables, footnotes definitions, lists,
  20281. ;; inline tasks.
  20282. (or (and (looking-at "[ \t]*:END:")
  20283. (re-search-backward org-drawer-regexp nil t))
  20284. (and (looking-at "[ \t]*#\\+end_")
  20285. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20286. (looking-at "[ \t]*[\n:#|]")
  20287. (looking-at org-footnote-definition-re)
  20288. (and (not inline-task-p)
  20289. (featurep 'org-inlinetask)
  20290. (org-inlinetask-in-task-p)
  20291. (or (org-inlinetask-goto-beginning) t))))
  20292. (beginning-of-line 0))
  20293. (cond
  20294. ;; There was a list item above.
  20295. ((ignore-errors (goto-char (org-in-item-p)))
  20296. (goto-char (org-list-get-top-point (org-list-struct)))
  20297. (setq column (org-get-indentation)))
  20298. ;; There was an heading above.
  20299. ((looking-at "\\*+[ \t]+")
  20300. (if (not org-adapt-indentation)
  20301. (setq column 0)
  20302. (goto-char (match-end 0))
  20303. (setq column (current-column))))
  20304. ;; A drawer had started and is unfinished
  20305. ((looking-at org-drawer-regexp)
  20306. (goto-char (1- (match-beginning 1)))
  20307. (setq column (current-column)))
  20308. ;; Else, nothing noticeable found: get indentation and go on.
  20309. (t (setq column (org-get-indentation))))))
  20310. ;; Now apply indentation and move cursor accordingly
  20311. (goto-char pos)
  20312. (if (<= (current-column) (current-indentation))
  20313. (org-indent-line-to column)
  20314. (save-excursion (org-indent-line-to column)))
  20315. ;; Special polishing for properties, see `org-property-format'
  20316. (setq column (current-column))
  20317. (beginning-of-line 1)
  20318. (if (looking-at org-property-re)
  20319. (replace-match (concat (match-string 4)
  20320. (format org-property-format
  20321. (match-string 1) (match-string 3)))
  20322. t t))
  20323. (org-move-to-column column))))
  20324. (defun org-indent-drawer ()
  20325. "Indent the drawer at point."
  20326. (interactive)
  20327. (let ((p (point))
  20328. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20329. (match-end 0)))
  20330. (folded
  20331. (save-excursion
  20332. (end-of-line)
  20333. (when (overlays-at (point))
  20334. (member 'invisible (overlay-properties
  20335. (car (overlays-at (point)))))))))
  20336. (when folded (org-cycle))
  20337. (indent-for-tab-command)
  20338. (while (and (move-beginning-of-line 2) (< (point) e))
  20339. (indent-for-tab-command))
  20340. (goto-char p)
  20341. (when folded (org-cycle)))
  20342. (message "Drawer at point indented"))
  20343. (defun org-indent-block ()
  20344. "Indent the block at point."
  20345. (interactive)
  20346. (let ((p (point))
  20347. (case-fold-search t)
  20348. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20349. (match-end 0)))
  20350. (folded
  20351. (save-excursion
  20352. (end-of-line)
  20353. (when (overlays-at (point))
  20354. (member 'invisible (overlay-properties
  20355. (car (overlays-at (point)))))))))
  20356. (when folded (org-cycle))
  20357. (indent-for-tab-command)
  20358. (while (and (move-beginning-of-line 2) (< (point) e))
  20359. (indent-for-tab-command))
  20360. (goto-char p)
  20361. (when folded (org-cycle)))
  20362. (message "Block at point indented"))
  20363. (defun org-indent-region (start end)
  20364. "Indent region."
  20365. (interactive "r")
  20366. (save-excursion
  20367. (let ((line-end (org-current-line end)))
  20368. (goto-char start)
  20369. (while (< (org-current-line) line-end)
  20370. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20371. (t (call-interactively 'org-indent-line)))
  20372. (move-beginning-of-line 2)))))
  20373. ;;; Filling
  20374. ;; We use our own fill-paragraph and auto-fill functions.
  20375. ;; `org-fill-paragraph' relies on adaptive filling and context
  20376. ;; checking. Appropriate `fill-prefix' is computed with
  20377. ;; `org-adaptive-fill-function'.
  20378. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20379. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20380. ;; `org-adaptive-fill-function' value. Internally,
  20381. ;; `org-comment-line-break-function' breaks the line.
  20382. ;; `org-setup-filling' installs filling and auto-filling related
  20383. ;; variables during `org-mode' initialization.
  20384. (defvar org-element-paragraph-separate) ; org-element.el
  20385. (defun org-setup-filling ()
  20386. (require 'org-element)
  20387. ;; Prevent auto-fill from inserting unwanted new items.
  20388. (when (boundp 'fill-nobreak-predicate)
  20389. (org-set-local
  20390. 'fill-nobreak-predicate
  20391. (org-uniquify
  20392. (append fill-nobreak-predicate
  20393. '(org-fill-line-break-nobreak-p
  20394. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20395. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20396. (org-set-local 'paragraph-start paragraph-ending)
  20397. (org-set-local 'paragraph-separate paragraph-ending))
  20398. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20399. (org-set-local 'auto-fill-inhibit-regexp nil)
  20400. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20401. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20402. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20403. (defun org-fill-line-break-nobreak-p ()
  20404. "Non-nil when a new line at point would create an Org line break."
  20405. (save-excursion
  20406. (skip-chars-backward "[ \t]")
  20407. (skip-chars-backward "\\\\")
  20408. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20409. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20410. "Non-nil when a new line at point would split a timestamp."
  20411. (and (org-at-timestamp-p t)
  20412. (not (looking-at org-ts-regexp-both))))
  20413. (declare-function message-in-body-p "message" ())
  20414. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20415. (defun org-adaptive-fill-function ()
  20416. "Compute a fill prefix for the current line.
  20417. Return fill prefix, as a string, or nil if current line isn't
  20418. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20419. matches in paragraphs or comments, use it."
  20420. (catch 'exit
  20421. (when (derived-mode-p 'message-mode)
  20422. (save-excursion
  20423. (beginning-of-line)
  20424. (cond ((or (not (message-in-body-p))
  20425. (looking-at orgtbl-line-start-regexp))
  20426. (throw 'exit nil))
  20427. ((looking-at message-cite-prefix-regexp)
  20428. (throw 'exit (match-string-no-properties 0)))
  20429. ((looking-at org-outline-regexp)
  20430. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20431. (org-with-wide-buffer
  20432. (let* ((p (line-beginning-position))
  20433. (element (save-excursion
  20434. (beginning-of-line)
  20435. (or (ignore-errors (org-element-at-point))
  20436. (user-error "An element cannot be parsed line %d"
  20437. (line-number-at-pos (point))))))
  20438. (type (org-element-type element))
  20439. (post-affiliated (org-element-property :post-affiliated element)))
  20440. (unless (and post-affiliated (< p post-affiliated))
  20441. (case type
  20442. (comment
  20443. (save-excursion
  20444. (beginning-of-line)
  20445. (looking-at "[ \t]*")
  20446. (concat (match-string 0) "# ")))
  20447. (footnote-definition "")
  20448. ((item plain-list)
  20449. (make-string (org-list-item-body-column
  20450. (or post-affiliated
  20451. (org-element-property :begin element)))
  20452. ? ))
  20453. (paragraph
  20454. ;; Fill prefix is usually the same as the current line,
  20455. ;; unless the paragraph is at the beginning of an item.
  20456. (let ((parent (org-element-property :parent element)))
  20457. (save-excursion
  20458. (beginning-of-line)
  20459. (cond ((eq (org-element-type parent) 'item)
  20460. (make-string (org-list-item-body-column
  20461. (org-element-property :begin parent))
  20462. ? ))
  20463. ((and adaptive-fill-regexp
  20464. ;; Locally disable
  20465. ;; `adaptive-fill-function' to let
  20466. ;; `fill-context-prefix' handle
  20467. ;; `adaptive-fill-regexp' variable.
  20468. (let (adaptive-fill-function)
  20469. (fill-context-prefix
  20470. post-affiliated
  20471. (org-element-property :end element)))))
  20472. ((looking-at "[ \t]+") (match-string 0))
  20473. (t "")))))
  20474. (comment-block
  20475. ;; Only fill contents if P is within block boundaries.
  20476. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20477. (forward-line)
  20478. (point)))
  20479. (cend (save-excursion
  20480. (goto-char (org-element-property :end element))
  20481. (skip-chars-backward " \r\t\n")
  20482. (line-beginning-position))))
  20483. (when (and (>= p cbeg) (< p cend))
  20484. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20485. (match-string 0)
  20486. ""))))))))))
  20487. (declare-function message-goto-body "message" ())
  20488. (defvar message-cite-prefix-regexp) ; From message.el
  20489. (defun org-fill-paragraph (&optional justify)
  20490. "Fill element at point, when applicable.
  20491. This function only applies to comment blocks, comments, example
  20492. blocks and paragraphs. Also, as a special case, re-align table
  20493. when point is at one.
  20494. If JUSTIFY is non-nil (interactively, with prefix argument),
  20495. justify as well. If `sentence-end-double-space' is non-nil, then
  20496. period followed by one space does not end a sentence, so don't
  20497. break a line there. The variable `fill-column' controls the
  20498. width for filling.
  20499. For convenience, when point is at a plain list, an item or
  20500. a footnote definition, try to fill the first paragraph within."
  20501. (interactive)
  20502. (if (and (derived-mode-p 'message-mode)
  20503. (or (not (message-in-body-p))
  20504. (save-excursion (move-beginning-of-line 1)
  20505. (looking-at message-cite-prefix-regexp))))
  20506. ;; First ensure filling is correct in message-mode.
  20507. (let ((fill-paragraph-function
  20508. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20509. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20510. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20511. (paragraph-separate
  20512. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20513. (fill-paragraph nil))
  20514. (with-syntax-table org-mode-transpose-word-syntax-table
  20515. ;; Move to end of line in order to get the first paragraph
  20516. ;; within a plain list or a footnote definition.
  20517. (let ((element (save-excursion
  20518. (end-of-line)
  20519. (or (ignore-errors (org-element-at-point))
  20520. (user-error "An element cannot be parsed line %d"
  20521. (line-number-at-pos (point)))))))
  20522. ;; First check if point is in a blank line at the beginning of
  20523. ;; the buffer. In that case, ignore filling.
  20524. (case (org-element-type element)
  20525. ;; Use major mode filling function is src blocks.
  20526. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20527. ;; Align Org tables, leave table.el tables as-is.
  20528. (table-row (org-table-align) t)
  20529. (table
  20530. (when (eq (org-element-property :type element) 'org)
  20531. (save-excursion
  20532. (goto-char (org-element-property :post-affiliated element))
  20533. (org-table-align)))
  20534. t)
  20535. (paragraph
  20536. ;; Paragraphs may contain `line-break' type objects.
  20537. (let ((beg (max (point-min)
  20538. (org-element-property :contents-begin element)))
  20539. (end (min (point-max)
  20540. (org-element-property :contents-end element))))
  20541. ;; Do nothing if point is at an affiliated keyword.
  20542. (if (< (line-end-position) beg) t
  20543. (when (derived-mode-p 'message-mode)
  20544. ;; In `message-mode', do not fill following citation
  20545. ;; in current paragraph nor text before message body.
  20546. (let ((body-start (save-excursion (message-goto-body))))
  20547. (when body-start (setq beg (max body-start beg))))
  20548. (when (save-excursion
  20549. (re-search-forward
  20550. (concat "^" message-cite-prefix-regexp) end t))
  20551. (setq end (match-beginning 0))))
  20552. ;; Fill paragraph, taking line breaks into account.
  20553. ;; For that, slice the paragraph using line breaks as
  20554. ;; separators, and fill the parts in reverse order to
  20555. ;; avoid messing with markers.
  20556. (save-excursion
  20557. (goto-char end)
  20558. (mapc
  20559. (lambda (pos)
  20560. (fill-region-as-paragraph pos (point) justify)
  20561. (goto-char pos))
  20562. ;; Find the list of ending positions for line breaks
  20563. ;; in the current paragraph. Add paragraph
  20564. ;; beginning to include first slice.
  20565. (nreverse
  20566. (cons beg
  20567. (org-element-map
  20568. (org-element--parse-objects
  20569. beg end nil (org-element-restriction 'paragraph))
  20570. 'line-break
  20571. (lambda (lb) (org-element-property :end lb)))))))
  20572. t)))
  20573. ;; Contents of `comment-block' type elements should be
  20574. ;; filled as plain text, but only if point is within block
  20575. ;; markers.
  20576. (comment-block
  20577. (let* ((case-fold-search t)
  20578. (beg (save-excursion
  20579. (goto-char (org-element-property :begin element))
  20580. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20581. (forward-line)
  20582. (point)))
  20583. (end (save-excursion
  20584. (goto-char (org-element-property :end element))
  20585. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20586. (line-beginning-position))))
  20587. (if (or (< (point) beg) (> (point) end)) t
  20588. (fill-region-as-paragraph
  20589. (save-excursion (end-of-line)
  20590. (re-search-backward "^[ \t]*$" beg 'move)
  20591. (line-beginning-position))
  20592. (save-excursion (beginning-of-line)
  20593. (re-search-forward "^[ \t]*$" end 'move)
  20594. (line-beginning-position))
  20595. justify))))
  20596. ;; Fill comments.
  20597. (comment
  20598. (let ((begin (org-element-property :post-affiliated element))
  20599. (end (org-element-property :end element)))
  20600. (when (and (>= (point) begin) (<= (point) end))
  20601. (let ((begin (save-excursion
  20602. (end-of-line)
  20603. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20604. (progn (forward-line) (point))
  20605. begin)))
  20606. (end (save-excursion
  20607. (end-of-line)
  20608. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20609. (1- (line-beginning-position))
  20610. (skip-chars-backward " \r\t\n")
  20611. (line-end-position)))))
  20612. ;; Do not fill comments when at a blank line.
  20613. (when (> end begin)
  20614. (let ((fill-prefix
  20615. (save-excursion
  20616. (beginning-of-line)
  20617. (looking-at "[ \t]*#")
  20618. (let ((comment-prefix (match-string 0)))
  20619. (goto-char (match-end 0))
  20620. (if (looking-at adaptive-fill-regexp)
  20621. (concat comment-prefix (match-string 0))
  20622. (concat comment-prefix " "))))))
  20623. (save-excursion
  20624. (fill-region-as-paragraph begin end justify))))))
  20625. t))
  20626. ;; Ignore every other element.
  20627. (otherwise t))))))
  20628. (defun org-auto-fill-function ()
  20629. "Auto-fill function."
  20630. ;; Check if auto-filling is meaningful.
  20631. (let ((fc (current-fill-column)))
  20632. (when (and fc (> (current-column) fc))
  20633. (let* ((fill-prefix (org-adaptive-fill-function))
  20634. ;; Enforce empty fill prefix, if required. Otherwise, it
  20635. ;; will be computed again.
  20636. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20637. (when fill-prefix (do-auto-fill))))))
  20638. (defun org-comment-line-break-function (&optional soft)
  20639. "Break line at point and indent, continuing comment if within one.
  20640. The inserted newline is marked hard if variable
  20641. `use-hard-newlines' is true, unless optional argument SOFT is
  20642. non-nil."
  20643. (if soft (insert-and-inherit ?\n) (newline 1))
  20644. (save-excursion (forward-char -1) (delete-horizontal-space))
  20645. (delete-horizontal-space)
  20646. (indent-to-left-margin)
  20647. (insert-before-markers-and-inherit fill-prefix))
  20648. ;;; Comments
  20649. ;; Org comments syntax is quite complex. It requires the entire line
  20650. ;; to be just a comment. Also, even with the right syntax at the
  20651. ;; beginning of line, some some elements (i.e. verse-block or
  20652. ;; example-block) don't accept comments. Usual Emacs comment commands
  20653. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20654. ;; Org still relies on `comment-dwim', but cannot trust
  20655. ;; `comment-only-p'. So, `comment-region-function' and
  20656. ;; `uncomment-region-function' both point
  20657. ;; to`org-comment-or-uncomment-region'. Eventually,
  20658. ;; `org-insert-comment' takes care of insertion of comments at the
  20659. ;; beginning of line.
  20660. ;; `org-setup-comments-handling' install comments related variables
  20661. ;; during `org-mode' initialization.
  20662. (defun org-setup-comments-handling ()
  20663. (interactive)
  20664. (org-set-local 'comment-use-syntax nil)
  20665. (org-set-local 'comment-start "# ")
  20666. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20667. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20668. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20669. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20670. (defun org-insert-comment ()
  20671. "Insert an empty comment above current line.
  20672. If the line is empty, insert comment at its beginning."
  20673. (beginning-of-line)
  20674. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20675. (org-indent-line)
  20676. (insert "# "))
  20677. (defvar comment-empty-lines) ; From newcomment.el.
  20678. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20679. "Comment or uncomment each non-blank line in the region.
  20680. Uncomment each non-blank line between BEG and END if it only
  20681. contains commented lines. Otherwise, comment them."
  20682. (save-restriction
  20683. ;; Restrict region
  20684. (narrow-to-region (save-excursion (goto-char beg)
  20685. (skip-chars-forward " \r\t\n" end)
  20686. (line-beginning-position))
  20687. (save-excursion (goto-char end)
  20688. (skip-chars-backward " \r\t\n" beg)
  20689. (line-end-position)))
  20690. (let ((uncommentp
  20691. ;; UNCOMMENTP is non-nil when every non blank line between
  20692. ;; BEG and END is a comment.
  20693. (save-excursion
  20694. (goto-char (point-min))
  20695. (while (and (not (eobp))
  20696. (let ((element (org-element-at-point)))
  20697. (and (eq (org-element-type element) 'comment)
  20698. (goto-char (min (point-max)
  20699. (org-element-property
  20700. :end element)))))))
  20701. (eobp))))
  20702. (if uncommentp
  20703. ;; Only blank lines and comments in region: uncomment it.
  20704. (save-excursion
  20705. (goto-char (point-min))
  20706. (while (not (eobp))
  20707. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20708. (replace-match "" nil nil nil 1))
  20709. (forward-line)))
  20710. ;; Comment each line in region.
  20711. (let ((min-indent (point-max)))
  20712. ;; First find the minimum indentation across all lines.
  20713. (save-excursion
  20714. (goto-char (point-min))
  20715. (while (and (not (eobp)) (not (zerop min-indent)))
  20716. (unless (looking-at "[ \t]*$")
  20717. (setq min-indent (min min-indent (current-indentation))))
  20718. (forward-line)))
  20719. ;; Then loop over all lines.
  20720. (save-excursion
  20721. (goto-char (point-min))
  20722. (while (not (eobp))
  20723. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20724. ;; Don't get fooled by invisible text (e.g. link path)
  20725. ;; when moving to column MIN-INDENT.
  20726. (let ((buffer-invisibility-spec nil))
  20727. (org-move-to-column min-indent t))
  20728. (insert comment-start))
  20729. (forward-line))))))))
  20730. ;;; Planning
  20731. ;; This section contains tools to operate on timestamp objects, as
  20732. ;; returned by, e.g. `org-element-context'.
  20733. (defun org-timestamp-has-time-p (timestamp)
  20734. "Non-nil when TIMESTAMP has a time specified."
  20735. (org-element-property :hour-start timestamp))
  20736. (defun org-timestamp-format (timestamp format &optional end utc)
  20737. "Format a TIMESTAMP element into a string.
  20738. FORMAT is a format specifier to be passed to
  20739. `format-time-string'.
  20740. When optional argument END is non-nil, use end of date-range or
  20741. time-range, if possible.
  20742. When optional argument UTC is non-nil, time will be expressed as
  20743. Universal Time."
  20744. (format-time-string
  20745. format
  20746. (apply 'encode-time
  20747. (cons 0
  20748. (mapcar
  20749. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20750. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20751. '(:minute-start :hour-start :day-start :month-start
  20752. :year-start)))))
  20753. utc))
  20754. (defun org-timestamp-split-range (timestamp &optional end)
  20755. "Extract a timestamp object from a date or time range.
  20756. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20757. the end of the range. Otherwise, extract its start.
  20758. Return a new timestamp object sharing the same parent as
  20759. TIMESTAMP."
  20760. (let ((type (org-element-property :type timestamp)))
  20761. (if (memq type '(active inactive diary)) timestamp
  20762. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20763. ;; Set new type.
  20764. (org-element-put-property
  20765. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20766. ;; Copy start properties over end properties if END is
  20767. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20768. (let ((p-alist '((:minute-start . :minute-end)
  20769. (:hour-start . :hour-end)
  20770. (:day-start . :day-end)
  20771. (:month-start . :month-end)
  20772. (:year-start . :year-end))))
  20773. (dolist (p-cell p-alist)
  20774. (org-element-put-property
  20775. split-ts
  20776. (funcall (if end 'car 'cdr) p-cell)
  20777. (org-element-property
  20778. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20779. ;; Eventually refresh `:raw-value'.
  20780. (org-element-put-property split-ts :raw-value nil)
  20781. (org-element-put-property
  20782. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20783. (defun org-timestamp-translate (timestamp &optional boundary)
  20784. "Apply `org-translate-time' on a TIMESTAMP object.
  20785. When optional argument BOUNDARY is non-nil, it is either the
  20786. symbol `start' or `end'. In this case, only translate the
  20787. starting or ending part of TIMESTAMP if it is a date or time
  20788. range. Otherwise, translate both parts."
  20789. (if (and (not boundary)
  20790. (memq (org-element-property :type timestamp)
  20791. '(active-range inactive-range)))
  20792. (concat
  20793. (org-translate-time
  20794. (org-element-property :raw-value
  20795. (org-timestamp-split-range timestamp)))
  20796. "--"
  20797. (org-translate-time
  20798. (org-element-property :raw-value
  20799. (org-timestamp-split-range timestamp t))))
  20800. (org-translate-time
  20801. (org-element-property
  20802. :raw-value
  20803. (if (not boundary) timestamp
  20804. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20805. ;;; Other stuff.
  20806. (defun org-toggle-fixed-width-section (arg)
  20807. "Toggle the fixed-width export.
  20808. If there is no active region, the QUOTE keyword at the current headline is
  20809. inserted or removed. When present, it causes the text between this headline
  20810. and the next to be exported as fixed-width text, and unmodified.
  20811. If there is an active region, this command adds or removes a colon as the
  20812. first character of this line. If the first character of a line is a colon,
  20813. this line is also exported in fixed-width font."
  20814. (interactive "P")
  20815. (let* ((cc 0)
  20816. (regionp (org-region-active-p))
  20817. (beg (if regionp (region-beginning) (point)))
  20818. (end (if regionp (region-end)))
  20819. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20820. (case-fold-search nil)
  20821. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20822. off)
  20823. (if regionp
  20824. (save-excursion
  20825. (goto-char beg)
  20826. (setq cc (current-column))
  20827. (beginning-of-line 1)
  20828. (setq off (looking-at re))
  20829. (while (> nlines 0)
  20830. (setq nlines (1- nlines))
  20831. (beginning-of-line 1)
  20832. (cond
  20833. (arg
  20834. (org-move-to-column cc t)
  20835. (insert ": \n")
  20836. (forward-line -1))
  20837. ((and off (looking-at re))
  20838. (replace-match "" t t nil 1))
  20839. ((not off) (org-move-to-column cc t) (insert ": ")))
  20840. (forward-line 1)))
  20841. (save-excursion
  20842. (org-back-to-heading)
  20843. (cond
  20844. ((looking-at (format org-heading-keyword-regexp-format
  20845. org-quote-string))
  20846. (goto-char (match-end 1))
  20847. (looking-at (concat " +" org-quote-string))
  20848. (replace-match "" t t)
  20849. (when (eolp) (insert " ")))
  20850. ((looking-at org-outline-regexp)
  20851. (goto-char (match-end 0))
  20852. (insert org-quote-string " ")))))))
  20853. (defun org-reftex-citation ()
  20854. "Use reftex-citation to insert a citation into the buffer.
  20855. This looks for a line like
  20856. #+BIBLIOGRAPHY: foo plain option:-d
  20857. and derives from it that foo.bib is the bibliography file relevant
  20858. for this document. It then installs the necessary environment for RefTeX
  20859. to work in this buffer and calls `reftex-citation' to insert a citation
  20860. into the buffer.
  20861. Export of such citations to both LaTeX and HTML is handled by the contributed
  20862. package ox-bibtex by Taru Karttunen."
  20863. (interactive)
  20864. (let ((reftex-docstruct-symbol 'rds)
  20865. (reftex-cite-format "\\cite{%l}")
  20866. rds bib)
  20867. (save-excursion
  20868. (save-restriction
  20869. (widen)
  20870. (let ((case-fold-search t)
  20871. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20872. (if (not (save-excursion
  20873. (or (re-search-forward re nil t)
  20874. (re-search-backward re nil t))))
  20875. (error "No bibliography defined in file")
  20876. (setq bib (concat (match-string 1) ".bib")
  20877. rds (list (list 'bib bib)))))))
  20878. (call-interactively 'reftex-citation)))
  20879. ;;;; Functions extending outline functionality
  20880. (defun org-beginning-of-line (&optional arg)
  20881. "Go to the beginning of the current line. If that is invisible, continue
  20882. to a visible line beginning. This makes the function of C-a more intuitive.
  20883. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20884. first attempt, and only move to after the tags when the cursor is already
  20885. beyond the end of the headline."
  20886. (interactive "P")
  20887. (let ((pos (point))
  20888. (special (if (consp org-special-ctrl-a/e)
  20889. (car org-special-ctrl-a/e)
  20890. org-special-ctrl-a/e))
  20891. deactivate-mark refpos)
  20892. (if (org-bound-and-true-p visual-line-mode)
  20893. (beginning-of-visual-line 1)
  20894. (beginning-of-line 1))
  20895. (if (and arg (fboundp 'move-beginning-of-line))
  20896. (call-interactively 'move-beginning-of-line)
  20897. (if (bobp)
  20898. nil
  20899. (backward-char 1)
  20900. (if (org-truely-invisible-p)
  20901. (while (and (not (bobp)) (org-truely-invisible-p))
  20902. (backward-char 1)
  20903. (beginning-of-line 1))
  20904. (forward-char 1))))
  20905. (when special
  20906. (cond
  20907. ((and (looking-at org-complex-heading-regexp)
  20908. (= (char-after (match-end 1)) ?\ ))
  20909. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20910. (point-at-eol)))
  20911. (goto-char
  20912. (if (eq special t)
  20913. (cond ((> pos refpos) refpos)
  20914. ((= pos (point)) refpos)
  20915. (t (point)))
  20916. (cond ((> pos (point)) (point))
  20917. ((not (eq last-command this-command)) (point))
  20918. (t refpos)))))
  20919. ((org-at-item-p)
  20920. ;; Being at an item and not looking at an the item means point
  20921. ;; was previously moved to beginning of a visual line, which
  20922. ;; doesn't contain the item. Therefore, do nothing special,
  20923. ;; just stay here.
  20924. (when (looking-at org-list-full-item-re)
  20925. ;; Set special position at first white space character after
  20926. ;; bullet, and check-box, if any.
  20927. (let ((after-bullet
  20928. (let ((box (match-end 3)))
  20929. (if (not box) (match-end 1)
  20930. (let ((after (char-after box)))
  20931. (if (and after (= after ? )) (1+ box) box))))))
  20932. ;; Special case: Move point to special position when
  20933. ;; currently after it or at beginning of line.
  20934. (if (eq special t)
  20935. (when (or (> pos after-bullet) (= (point) pos))
  20936. (goto-char after-bullet))
  20937. ;; Reversed case: Move point to special position when
  20938. ;; point was already at beginning of line and command is
  20939. ;; repeated.
  20940. (when (and (= (point) pos) (eq last-command this-command))
  20941. (goto-char after-bullet))))))))
  20942. (org-no-warnings
  20943. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20944. (setq disable-point-adjustment
  20945. (or (not (invisible-p (point)))
  20946. (not (invisible-p (max (point-min) (1- (point))))))))
  20947. (defun org-end-of-line (&optional arg)
  20948. "Go to the end of the line.
  20949. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20950. tags on the first attempt, and only move to after the tags when
  20951. the cursor is already beyond the end of the headline."
  20952. (interactive "P")
  20953. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20954. org-special-ctrl-a/e))
  20955. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20956. 'end-of-visual-line)
  20957. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20958. (t 'end-of-line)))
  20959. deactivate-mark)
  20960. (if (or (not special) arg) (call-interactively move-fun)
  20961. (let* ((element (save-excursion (beginning-of-line)
  20962. (org-element-at-point)))
  20963. (type (org-element-type element)))
  20964. (cond
  20965. ((memq type '(headline inlinetask))
  20966. (let ((pos (point)))
  20967. (beginning-of-line 1)
  20968. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20969. (if (eq special t)
  20970. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20971. (goto-char (match-beginning 1))
  20972. (goto-char (match-end 0)))
  20973. (if (or (< pos (match-end 0))
  20974. (not (eq this-command last-command)))
  20975. (goto-char (match-end 0))
  20976. (goto-char (match-beginning 1))))
  20977. (call-interactively move-fun))))
  20978. ((org-element-property :hiddenp element)
  20979. ;; If element is hidden, `move-end-of-line' would put point
  20980. ;; after it. Use `end-of-line' to stay on current line.
  20981. (call-interactively 'end-of-line))
  20982. (t (call-interactively move-fun)))))
  20983. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20984. (setq disable-point-adjustment
  20985. (or (not (invisible-p (point)))
  20986. (not (invisible-p (max (point-min) (1- (point))))))))
  20987. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20988. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20989. (defun org-backward-sentence (&optional arg)
  20990. "Go to beginning of sentence, or beginning of table field.
  20991. This will call `backward-sentence' or `org-table-beginning-of-field',
  20992. depending on context."
  20993. (interactive "P")
  20994. (cond
  20995. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20996. (t (call-interactively 'backward-sentence))))
  20997. (defun org-forward-sentence (&optional arg)
  20998. "Go to end of sentence, or end of table field.
  20999. This will call `forward-sentence' or `org-table-end-of-field',
  21000. depending on context."
  21001. (interactive "P")
  21002. (cond
  21003. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21004. (t (call-interactively 'forward-sentence))))
  21005. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21006. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21007. (defun org-kill-line (&optional arg)
  21008. "Kill line, to tags or end of line."
  21009. (interactive "P")
  21010. (cond
  21011. ((or (not org-special-ctrl-k)
  21012. (bolp)
  21013. (not (org-at-heading-p)))
  21014. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21015. org-ctrl-k-protect-subtree)
  21016. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21017. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21018. (user-error "C-k aborted as it would kill a hidden subtree")))
  21019. (call-interactively
  21020. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21021. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21022. (kill-region (point) (match-beginning 1))
  21023. (org-set-tags nil t))
  21024. (t (kill-region (point) (point-at-eol)))))
  21025. (define-key org-mode-map "\C-k" 'org-kill-line)
  21026. (defun org-yank (&optional arg)
  21027. "Yank. If the kill is a subtree, treat it specially.
  21028. This command will look at the current kill and check if is a single
  21029. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21030. cursor is at the beginning of a line or after the stars of a currently
  21031. empty headline, then the yank is handled specially. How exactly depends
  21032. on the value of the following variables, both set by default.
  21033. org-yank-folded-subtrees
  21034. When set, the subtree(s) will be folded after insertion, but only
  21035. if doing so would now swallow text after the yanked text.
  21036. org-yank-adjusted-subtrees
  21037. When set, the subtree will be promoted or demoted in order to
  21038. fit into the local outline tree structure, which means that the level
  21039. will be adjusted so that it becomes the smaller one of the two
  21040. *visible* surrounding headings.
  21041. Any prefix to this command will cause `yank' to be called directly with
  21042. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21043. will just
  21044. plainly yank the text as it is.
  21045. \[1] The test checks if the first non-white line is a heading
  21046. and if there are no other headings with fewer stars."
  21047. (interactive "P")
  21048. (org-yank-generic 'yank arg))
  21049. (defun org-yank-generic (command arg)
  21050. "Perform some yank-like command.
  21051. This function implements the behavior described in the `org-yank'
  21052. documentation. However, it has been generalized to work for any
  21053. interactive command with similar behavior."
  21054. ;; pretend to be command COMMAND
  21055. (setq this-command command)
  21056. (if arg
  21057. (call-interactively command)
  21058. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21059. (and (org-kill-is-subtree-p)
  21060. (or (bolp)
  21061. (and (looking-at "[ \t]*$")
  21062. (string-match
  21063. "\\`\\*+\\'"
  21064. (buffer-substring (point-at-bol) (point)))))))
  21065. swallowp)
  21066. (cond
  21067. ((and subtreep org-yank-folded-subtrees)
  21068. (let ((beg (point))
  21069. end)
  21070. (if (and subtreep org-yank-adjusted-subtrees)
  21071. (org-paste-subtree nil nil 'for-yank)
  21072. (call-interactively command))
  21073. (setq end (point))
  21074. (goto-char beg)
  21075. (when (and (bolp) subtreep
  21076. (not (setq swallowp
  21077. (org-yank-folding-would-swallow-text beg end))))
  21078. (org-with-limited-levels
  21079. (or (looking-at org-outline-regexp)
  21080. (re-search-forward org-outline-regexp-bol end t))
  21081. (while (and (< (point) end) (looking-at org-outline-regexp))
  21082. (hide-subtree)
  21083. (org-cycle-show-empty-lines 'folded)
  21084. (condition-case nil
  21085. (outline-forward-same-level 1)
  21086. (error (goto-char end))))))
  21087. (when swallowp
  21088. (message
  21089. "Inserted text not folded because that would swallow text"))
  21090. (goto-char end)
  21091. (skip-chars-forward " \t\n\r")
  21092. (beginning-of-line 1)
  21093. (push-mark beg 'nomsg)))
  21094. ((and subtreep org-yank-adjusted-subtrees)
  21095. (let ((beg (point-at-bol)))
  21096. (org-paste-subtree nil nil 'for-yank)
  21097. (push-mark beg 'nomsg)))
  21098. (t
  21099. (call-interactively command))))))
  21100. (defun org-yank-folding-would-swallow-text (beg end)
  21101. "Would hide-subtree at BEG swallow any text after END?"
  21102. (let (level)
  21103. (org-with-limited-levels
  21104. (save-excursion
  21105. (goto-char beg)
  21106. (when (or (looking-at org-outline-regexp)
  21107. (re-search-forward org-outline-regexp-bol end t))
  21108. (setq level (org-outline-level)))
  21109. (goto-char end)
  21110. (skip-chars-forward " \t\r\n\v\f")
  21111. (if (or (eobp)
  21112. (and (bolp) (looking-at org-outline-regexp)
  21113. (<= (org-outline-level) level)))
  21114. nil ; Nothing would be swallowed
  21115. t))))) ; something would swallow
  21116. (define-key org-mode-map "\C-y" 'org-yank)
  21117. (defun org-truely-invisible-p ()
  21118. "Check if point is at a character currently not visible.
  21119. This version does not only check the character property, but also
  21120. `visible-mode'."
  21121. ;; Early versions of noutline don't have `outline-invisible-p'.
  21122. (if (org-bound-and-true-p visible-mode)
  21123. nil
  21124. (outline-invisible-p)))
  21125. (defun org-invisible-p2 ()
  21126. "Check if point is at a character currently not visible."
  21127. (save-excursion
  21128. (if (and (eolp) (not (bobp))) (backward-char 1))
  21129. ;; Early versions of noutline don't have `outline-invisible-p'.
  21130. (outline-invisible-p)))
  21131. (defun org-back-to-heading (&optional invisible-ok)
  21132. "Call `outline-back-to-heading', but provide a better error message."
  21133. (condition-case nil
  21134. (outline-back-to-heading invisible-ok)
  21135. (error (error "Before first headline at position %d in buffer %s"
  21136. (point) (current-buffer)))))
  21137. (defun org-before-first-heading-p ()
  21138. "Before first heading?"
  21139. (save-excursion
  21140. (end-of-line)
  21141. (null (re-search-backward org-outline-regexp-bol nil t))))
  21142. (defun org-at-heading-p (&optional ignored)
  21143. (outline-on-heading-p t))
  21144. ;; Compatibility alias with Org versions < 7.8.03
  21145. (defalias 'org-on-heading-p 'org-at-heading-p)
  21146. (defun org-at-comment-p nil
  21147. "Is cursor in a line starting with a # character?"
  21148. (save-excursion
  21149. (beginning-of-line)
  21150. (looking-at "^#")))
  21151. (defun org-at-drawer-p nil
  21152. "Is cursor at a drawer keyword?"
  21153. (save-excursion
  21154. (move-beginning-of-line 1)
  21155. (looking-at org-drawer-regexp)))
  21156. (defun org-at-block-p nil
  21157. "Is cursor at a block keyword?"
  21158. (save-excursion
  21159. (move-beginning-of-line 1)
  21160. (looking-at org-block-regexp)))
  21161. (defun org-point-at-end-of-empty-headline ()
  21162. "If point is at the end of an empty headline, return t, else nil.
  21163. If the heading only contains a TODO keyword, it is still still considered
  21164. empty."
  21165. (and (looking-at "[ \t]*$")
  21166. (when org-todo-line-regexp
  21167. (save-excursion
  21168. (beginning-of-line 1)
  21169. (let ((case-fold-search nil))
  21170. (looking-at org-todo-line-regexp)
  21171. (string= (match-string 3) ""))))))
  21172. (defun org-at-heading-or-item-p ()
  21173. (or (org-at-heading-p) (org-at-item-p)))
  21174. (defun org-at-target-p ()
  21175. (or (org-in-regexp org-radio-target-regexp)
  21176. (org-in-regexp org-target-regexp)))
  21177. ;; Compatibility alias with Org versions < 7.8.03
  21178. (defalias 'org-on-target-p 'org-at-target-p)
  21179. (defun org-up-heading-all (arg)
  21180. "Move to the heading line of which the present line is a subheading.
  21181. This function considers both visible and invisible heading lines.
  21182. With argument, move up ARG levels."
  21183. (if (fboundp 'outline-up-heading-all)
  21184. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21185. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21186. (defun org-up-heading-safe ()
  21187. "Move to the heading line of which the present line is a subheading.
  21188. This version will not throw an error. It will return the level of the
  21189. headline found, or nil if no higher level is found.
  21190. Also, this function will be a lot faster than `outline-up-heading',
  21191. because it relies on stars being the outline starters. This can really
  21192. make a significant difference in outlines with very many siblings."
  21193. (let (start-level re)
  21194. (org-back-to-heading t)
  21195. (setq start-level (funcall outline-level))
  21196. (if (equal start-level 1)
  21197. nil
  21198. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21199. (if (re-search-backward re nil t)
  21200. (funcall outline-level)))))
  21201. (defun org-first-sibling-p ()
  21202. "Is this heading the first child of its parents?"
  21203. (interactive)
  21204. (let ((re org-outline-regexp-bol)
  21205. level l)
  21206. (unless (org-at-heading-p t)
  21207. (user-error "Not at a heading"))
  21208. (setq level (funcall outline-level))
  21209. (save-excursion
  21210. (if (not (re-search-backward re nil t))
  21211. t
  21212. (setq l (funcall outline-level))
  21213. (< l level)))))
  21214. (defun org-goto-sibling (&optional previous)
  21215. "Goto the next sibling, even if it is invisible.
  21216. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21217. when a sibling was found. When none is found, return nil and don't
  21218. move point."
  21219. (let ((fun (if previous 're-search-backward 're-search-forward))
  21220. (pos (point))
  21221. (re org-outline-regexp-bol)
  21222. level l)
  21223. (when (condition-case nil (org-back-to-heading t) (error nil))
  21224. (setq level (funcall outline-level))
  21225. (catch 'exit
  21226. (or previous (forward-char 1))
  21227. (while (funcall fun re nil t)
  21228. (setq l (funcall outline-level))
  21229. (when (< l level) (goto-char pos) (throw 'exit nil))
  21230. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21231. (goto-char pos)
  21232. nil))))
  21233. (defun org-show-siblings ()
  21234. "Show all siblings of the current headline."
  21235. (save-excursion
  21236. (while (org-goto-sibling) (org-flag-heading nil)))
  21237. (save-excursion
  21238. (while (org-goto-sibling 'previous)
  21239. (org-flag-heading nil))))
  21240. (defun org-goto-first-child ()
  21241. "Goto the first child, even if it is invisible.
  21242. Return t when a child was found. Otherwise don't move point and
  21243. return nil."
  21244. (let (level (pos (point)) (re org-outline-regexp-bol))
  21245. (when (condition-case nil (org-back-to-heading t) (error nil))
  21246. (setq level (outline-level))
  21247. (forward-char 1)
  21248. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21249. (progn (goto-char (match-beginning 0)) t)
  21250. (goto-char pos) nil))))
  21251. (defun org-show-hidden-entry ()
  21252. "Show an entry where even the heading is hidden."
  21253. (save-excursion
  21254. (org-show-entry)))
  21255. (defun org-flag-heading (flag &optional entry)
  21256. "Flag the current heading. FLAG non-nil means make invisible.
  21257. When ENTRY is non-nil, show the entire entry."
  21258. (save-excursion
  21259. (org-back-to-heading t)
  21260. ;; Check if we should show the entire entry
  21261. (if entry
  21262. (progn
  21263. (org-show-entry)
  21264. (save-excursion
  21265. (and (outline-next-heading)
  21266. (org-flag-heading nil))))
  21267. (outline-flag-region (max (point-min) (1- (point)))
  21268. (save-excursion (outline-end-of-heading) (point))
  21269. flag))))
  21270. (defun org-get-next-sibling ()
  21271. "Move to next heading of the same level, and return point.
  21272. If there is no such heading, return nil.
  21273. This is like outline-next-sibling, but invisible headings are ok."
  21274. (let ((level (funcall outline-level)))
  21275. (outline-next-heading)
  21276. (while (and (not (eobp)) (> (funcall outline-level) level))
  21277. (outline-next-heading))
  21278. (if (or (eobp) (< (funcall outline-level) level))
  21279. nil
  21280. (point))))
  21281. (defun org-get-last-sibling ()
  21282. "Move to previous heading of the same level, and return point.
  21283. If there is no such heading, return nil."
  21284. (let ((opoint (point))
  21285. (level (funcall outline-level)))
  21286. (outline-previous-heading)
  21287. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21288. (while (and (> (funcall outline-level) level)
  21289. (not (bobp)))
  21290. (outline-previous-heading))
  21291. (if (< (funcall outline-level) level)
  21292. nil
  21293. (point)))))
  21294. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21295. "Goto to the end of a subtree."
  21296. ;; This contains an exact copy of the original function, but it uses
  21297. ;; `org-back-to-heading', to make it work also in invisible
  21298. ;; trees. And is uses an invisible-ok argument.
  21299. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21300. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21301. ;; with many children and grandchildren etc, this can be much faster
  21302. ;; than the outline version.
  21303. (org-back-to-heading invisible-ok)
  21304. (let ((first t)
  21305. (level (funcall outline-level)))
  21306. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21307. ;; A true heading (not a plain list item), in Org-mode
  21308. ;; This means we can easily find the end by looking
  21309. ;; only for the right number of stars. Using a regexp to do
  21310. ;; this is so much faster than using a Lisp loop.
  21311. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21312. (forward-char 1)
  21313. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21314. ;; something else, do it the slow way
  21315. (while (and (not (eobp))
  21316. (or first (> (funcall outline-level) level)))
  21317. (setq first nil)
  21318. (outline-next-heading)))
  21319. (unless to-heading
  21320. (if (memq (preceding-char) '(?\n ?\^M))
  21321. (progn
  21322. ;; Go to end of line before heading
  21323. (forward-char -1)
  21324. (if (memq (preceding-char) '(?\n ?\^M))
  21325. ;; leave blank line before heading
  21326. (forward-char -1))))))
  21327. (point))
  21328. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21329. "Use Org version in org-mode, for dramatic speed-up."
  21330. (if (derived-mode-p 'org-mode)
  21331. (progn
  21332. (org-end-of-subtree nil t)
  21333. (unless (eobp) (backward-char 1)))
  21334. ad-do-it))
  21335. (defun org-end-of-meta-data-and-drawers ()
  21336. "Jump to the first text after meta data and drawers in the current entry.
  21337. This will move over empty lines, lines with planning time stamps,
  21338. clocking lines, and drawers."
  21339. (org-back-to-heading t)
  21340. (let ((end (save-excursion (outline-next-heading) (point)))
  21341. (re (concat "\\(" org-drawer-regexp "\\)"
  21342. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21343. (forward-line 1)
  21344. (while (re-search-forward re end t)
  21345. (if (not (match-end 1))
  21346. ;; empty or planning line
  21347. (forward-line 1)
  21348. ;; a drawer, find the end
  21349. (re-search-forward "^[ \t]*:END:" end 'move)
  21350. (forward-line 1)))
  21351. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21352. (point)))
  21353. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21354. "Move forward to the ARG'th subheading at same level as this one.
  21355. Stop at the first and last subheadings of a superior heading.
  21356. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21357. non-nil it will also look at invisible ones."
  21358. (interactive "p")
  21359. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21360. (if (and arg (< arg 0))
  21361. (goto-char (point-min))
  21362. (outline-next-heading))
  21363. (org-at-heading-p)
  21364. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21365. (f (if (and arg (< arg 0))
  21366. 're-search-backward
  21367. 're-search-forward))
  21368. (count (if arg (abs arg) 1))
  21369. (result (point)))
  21370. (while (and (prog1 (> count 0)
  21371. (forward-char (if (and arg (< arg 0)) -1 1)))
  21372. (funcall f org-outline-regexp-bol nil 'move))
  21373. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21374. (cond ((< l level) (setq count 0))
  21375. ((and (= l level)
  21376. (or invisible-ok
  21377. (progn
  21378. (goto-char (line-beginning-position))
  21379. (not (outline-invisible-p)))))
  21380. (setq count (1- count))
  21381. (when (eq l level)
  21382. (setq result (point)))))))
  21383. (goto-char result))
  21384. (beginning-of-line 1)))
  21385. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21386. "Move backward to the ARG'th subheading at same level as this one.
  21387. Stop at the first and last subheadings of a superior heading."
  21388. (interactive "p")
  21389. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21390. (defun org-next-block (arg &optional backward block-regexp)
  21391. "Jump to the next block.
  21392. With a prefix argument ARG, jump forward ARG many source blocks.
  21393. When BACKWARD is non-nil, jump to the previous block.
  21394. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21395. (interactive "p")
  21396. (let ((re (or block-regexp org-block-regexp))
  21397. (re-search-fn (or (and backward 're-search-backward)
  21398. 're-search-forward)))
  21399. (if (looking-at re) (forward-char 1))
  21400. (condition-case nil
  21401. (funcall re-search-fn re nil nil arg)
  21402. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21403. (goto-char (match-beginning 0)) (org-show-context)))
  21404. (defun org-previous-block (arg &optional block-regexp)
  21405. "Jump to the previous block.
  21406. With a prefix argument ARG, jump backward ARG many source blocks.
  21407. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21408. (interactive "p")
  21409. (org-next-block arg t block-regexp))
  21410. (defun org-forward-paragraph ()
  21411. "Move forward to beginning of next paragraph or equivalent.
  21412. The function moves point to the beginning of the next visible
  21413. structural element, which can be a paragraph, a table, a list
  21414. item, etc. It also provides some special moves for convenience:
  21415. - On an affiliated keyword, jump to the beginning of the
  21416. relative element.
  21417. - On an item or a footnote definition, move to the second
  21418. element inside, if any.
  21419. - On a table or a property drawer, jump after it.
  21420. - On a verse or source block, stop after blank lines."
  21421. (interactive)
  21422. (when (eobp) (user-error "Cannot move further down"))
  21423. (let* ((deactivate-mark nil)
  21424. (element (org-element-at-point))
  21425. (type (org-element-type element))
  21426. (post-affiliated (org-element-property :post-affiliated element))
  21427. (contents-begin (org-element-property :contents-begin element))
  21428. (contents-end (org-element-property :contents-end element))
  21429. (end (let ((end (org-element-property :end element)) (parent element))
  21430. (while (and (setq parent (org-element-property :parent parent))
  21431. (= (org-element-property :contents-end parent) end))
  21432. (setq end (org-element-property :end parent)))
  21433. end)))
  21434. (cond ((not element)
  21435. (skip-chars-forward " \r\t\n")
  21436. (or (eobp) (beginning-of-line)))
  21437. ;; On affiliated keywords, move to element's beginning.
  21438. ((and post-affiliated (< (point) post-affiliated))
  21439. (goto-char post-affiliated))
  21440. ;; At a table row, move to the end of the table. Similarly,
  21441. ;; at a node property, move to the end of the property
  21442. ;; drawer.
  21443. ((memq type '(node-property table-row))
  21444. (goto-char (org-element-property
  21445. :end (org-element-property :parent element))))
  21446. ((memq type '(property-drawer table)) (goto-char end))
  21447. ;; Consider blank lines as separators in verse and source
  21448. ;; blocks to ease editing.
  21449. ((memq type '(src-block verse-block))
  21450. (when (eq type 'src-block)
  21451. (setq contents-end
  21452. (save-excursion (goto-char end)
  21453. (skip-chars-backward " \r\t\n")
  21454. (line-beginning-position))))
  21455. (beginning-of-line)
  21456. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21457. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21458. (goto-char end)
  21459. (skip-chars-forward " \r\t\n")
  21460. (if (= (point) contents-end) (goto-char end)
  21461. (beginning-of-line))))
  21462. ;; With no contents, just skip element.
  21463. ((not contents-begin) (goto-char end))
  21464. ;; If contents are invisible, skip the element altogether.
  21465. ((outline-invisible-p (line-end-position))
  21466. (case type
  21467. (headline
  21468. (org-with-limited-levels (outline-next-visible-heading 1)))
  21469. ;; At a plain list, make sure we move to the next item
  21470. ;; instead of skipping the whole list.
  21471. (plain-list (forward-char)
  21472. (org-forward-paragraph))
  21473. (otherwise (goto-char end))))
  21474. ((>= (point) contents-end) (goto-char end))
  21475. ((>= (point) contents-begin)
  21476. ;; This can only happen on paragraphs and plain lists.
  21477. (case type
  21478. (paragraph (goto-char end))
  21479. ;; At a plain list, try to move to second element in
  21480. ;; first item, if possible.
  21481. (plain-list (end-of-line)
  21482. (org-forward-paragraph))))
  21483. ;; When contents start on the middle of a line (e.g. in
  21484. ;; items and footnote definitions), try to reach first
  21485. ;; element starting after current line.
  21486. ((> (line-end-position) contents-begin)
  21487. (end-of-line)
  21488. (org-forward-paragraph))
  21489. (t (goto-char contents-begin)))))
  21490. (defun org-backward-paragraph ()
  21491. "Move backward to start of previous paragraph or equivalent.
  21492. The function moves point to the beginning of the current
  21493. structural element, which can be a paragraph, a table, a list
  21494. item, etc., or to the beginning of the previous visible one if
  21495. point is already there. It also provides some special moves for
  21496. convenience:
  21497. - On an affiliated keyword, jump to the first one.
  21498. - On a table or a property drawer, move to its beginning.
  21499. - On a verse or source block, stop before blank lines."
  21500. (interactive)
  21501. (when (bobp) (user-error "Cannot move further up"))
  21502. (let* ((deactivate-mark nil)
  21503. (element (org-element-at-point))
  21504. (type (org-element-type element))
  21505. (contents-begin (org-element-property :contents-begin element))
  21506. (contents-end (org-element-property :contents-end element))
  21507. (post-affiliated (org-element-property :post-affiliated element))
  21508. (begin (org-element-property :begin element)))
  21509. (cond
  21510. ((not element) (goto-char (point-min)))
  21511. ((= (point) begin)
  21512. (backward-char)
  21513. (org-backward-paragraph))
  21514. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21515. ((memq type '(node-property table-row))
  21516. (goto-char (org-element-property
  21517. :post-affiliated (org-element-property :parent element))))
  21518. ((memq type '(property-drawer table)) (goto-char begin))
  21519. ((memq type '(src-block verse-block))
  21520. (when (eq type 'src-block)
  21521. (setq contents-begin
  21522. (save-excursion (goto-char begin) (forward-line) (point))))
  21523. (if (= (point) contents-begin) (goto-char post-affiliated)
  21524. ;; Inside a verse block, see blank lines as paragraph
  21525. ;; separators.
  21526. (let ((origin (point)))
  21527. (skip-chars-backward " \r\t\n" contents-begin)
  21528. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21529. (skip-chars-forward " \r\t\n" origin)
  21530. (if (= (point) origin) (goto-char contents-begin)
  21531. (beginning-of-line))))))
  21532. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21533. ((eq type 'paragraph)
  21534. (goto-char contents-begin)
  21535. ;; When at first paragraph in an item or a footnote definition,
  21536. ;; move directly to beginning of line.
  21537. (let ((parent-contents
  21538. (org-element-property
  21539. :contents-begin (org-element-property :parent element))))
  21540. (when (and parent-contents (= parent-contents contents-begin))
  21541. (beginning-of-line))))
  21542. ;; At the end of a greater element, move to the beginning of the
  21543. ;; last element within.
  21544. ((>= (point) contents-end)
  21545. (goto-char (1- contents-end))
  21546. (org-backward-paragraph))
  21547. (t (goto-char (or post-affiliated begin))))
  21548. ;; Ensure we never leave point invisible.
  21549. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21550. (defun org-forward-element ()
  21551. "Move forward by one element.
  21552. Move to the next element at the same level, when possible."
  21553. (interactive)
  21554. (cond ((eobp) (user-error "Cannot move further down"))
  21555. ((org-with-limited-levels (org-at-heading-p))
  21556. (let ((origin (point)))
  21557. (goto-char (org-end-of-subtree nil t))
  21558. (unless (org-with-limited-levels (org-at-heading-p))
  21559. (goto-char origin)
  21560. (user-error "Cannot move further down"))))
  21561. (t
  21562. (let* ((elem (org-element-at-point))
  21563. (end (org-element-property :end elem))
  21564. (parent (org-element-property :parent elem)))
  21565. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21566. (goto-char (org-element-property :end parent)))
  21567. ((integer-or-marker-p end) (goto-char end))
  21568. (t (message "No element at point")))))))
  21569. (defun org-backward-element ()
  21570. "Move backward by one element.
  21571. Move to the previous element at the same level, when possible."
  21572. (interactive)
  21573. (cond ((bobp) (user-error "Cannot move further up"))
  21574. ((org-with-limited-levels (org-at-heading-p))
  21575. ;; At a headline, move to the previous one, if any, or stay
  21576. ;; here.
  21577. (let ((origin (point)))
  21578. (org-with-limited-levels (org-backward-heading-same-level 1))
  21579. ;; When current headline has no sibling above, move to its
  21580. ;; parent.
  21581. (when (= (point) origin)
  21582. (or (org-with-limited-levels (org-up-heading-safe))
  21583. (progn (goto-char origin)
  21584. (user-error "Cannot move further up"))))))
  21585. (t
  21586. (let* ((trail (org-element-at-point 'keep-trail))
  21587. (elem (car trail))
  21588. (prev-elem (nth 1 trail))
  21589. (beg (org-element-property :begin elem)))
  21590. (cond
  21591. ;; Move to beginning of current element if point isn't
  21592. ;; there already.
  21593. ((null beg) (message "No element at point"))
  21594. ((/= (point) beg) (goto-char beg))
  21595. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21596. ((org-before-first-heading-p) (goto-char (point-min)))
  21597. (t (org-back-to-heading)))))))
  21598. (defun org-up-element ()
  21599. "Move to upper element."
  21600. (interactive)
  21601. (if (org-with-limited-levels (org-at-heading-p))
  21602. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21603. (let* ((elem (org-element-at-point))
  21604. (parent (org-element-property :parent elem)))
  21605. (if parent (goto-char (org-element-property :begin parent))
  21606. (if (org-with-limited-levels (org-before-first-heading-p))
  21607. (user-error "No surrounding element")
  21608. (org-with-limited-levels (org-back-to-heading)))))))
  21609. (defvar org-element-greater-elements)
  21610. (defun org-down-element ()
  21611. "Move to inner element."
  21612. (interactive)
  21613. (let ((element (org-element-at-point)))
  21614. (cond
  21615. ((memq (org-element-type element) '(plain-list table))
  21616. (goto-char (org-element-property :contents-begin element))
  21617. (forward-char))
  21618. ((memq (org-element-type element) org-element-greater-elements)
  21619. ;; If contents are hidden, first disclose them.
  21620. (when (org-element-property :hiddenp element) (org-cycle))
  21621. (goto-char (or (org-element-property :contents-begin element)
  21622. (user-error "No content for this element"))))
  21623. (t (user-error "No inner element")))))
  21624. (defun org-drag-element-backward ()
  21625. "Move backward element at point."
  21626. (interactive)
  21627. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21628. (let* ((trail (org-element-at-point 'keep-trail))
  21629. (elem (car trail))
  21630. (prev-elem (nth 1 trail)))
  21631. ;; Error out if no previous element or previous element is
  21632. ;; a parent of the current one.
  21633. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21634. (user-error "Cannot drag element backward")
  21635. (let ((pos (point)))
  21636. (org-element-swap-A-B prev-elem elem)
  21637. (goto-char (+ (org-element-property :begin prev-elem)
  21638. (- pos (org-element-property :begin elem)))))))))
  21639. (defun org-drag-element-forward ()
  21640. "Move forward element at point."
  21641. (interactive)
  21642. (let* ((pos (point))
  21643. (elem (org-element-at-point)))
  21644. (when (= (point-max) (org-element-property :end elem))
  21645. (user-error "Cannot drag element forward"))
  21646. (goto-char (org-element-property :end elem))
  21647. (let ((next-elem (org-element-at-point)))
  21648. (when (or (org-element-nested-p elem next-elem)
  21649. (and (eq (org-element-type next-elem) 'headline)
  21650. (not (eq (org-element-type elem) 'headline))))
  21651. (goto-char pos)
  21652. (user-error "Cannot drag element forward"))
  21653. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21654. ;; body's length (without final blanks) and by the length of
  21655. ;; blanks between ELEM and NEXT-ELEM.
  21656. (let ((size-next (- (save-excursion
  21657. (goto-char (org-element-property :end next-elem))
  21658. (skip-chars-backward " \r\t\n")
  21659. (forward-line)
  21660. ;; Small correction if buffer doesn't end
  21661. ;; with a newline character.
  21662. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21663. (org-element-property :begin next-elem)))
  21664. (size-blank (- (org-element-property :end elem)
  21665. (save-excursion
  21666. (goto-char (org-element-property :end elem))
  21667. (skip-chars-backward " \r\t\n")
  21668. (forward-line)
  21669. (point)))))
  21670. (org-element-swap-A-B elem next-elem)
  21671. (goto-char (+ pos size-next size-blank))))))
  21672. (defun org-drag-line-forward (arg)
  21673. "Drag the line at point ARG lines forward."
  21674. (interactive "p")
  21675. (dotimes (n (abs arg))
  21676. (let ((c (current-column)))
  21677. (if (< 0 arg)
  21678. (progn
  21679. (beginning-of-line 2)
  21680. (transpose-lines 1)
  21681. (beginning-of-line 0))
  21682. (transpose-lines 1)
  21683. (beginning-of-line -1))
  21684. (org-move-to-column c))))
  21685. (defun org-drag-line-backward (arg)
  21686. "Drag the line at point ARG lines backward."
  21687. (interactive "p")
  21688. (org-drag-line-forward (- arg)))
  21689. (defun org-mark-element ()
  21690. "Put point at beginning of this element, mark at end.
  21691. Interactively, if this command is repeated or (in Transient Mark
  21692. mode) if the mark is active, it marks the next element after the
  21693. ones already marked."
  21694. (interactive)
  21695. (let (deactivate-mark)
  21696. (if (and (org-called-interactively-p 'any)
  21697. (or (and (eq last-command this-command) (mark t))
  21698. (and transient-mark-mode mark-active)))
  21699. (set-mark
  21700. (save-excursion
  21701. (goto-char (mark))
  21702. (goto-char (org-element-property :end (org-element-at-point)))))
  21703. (let ((element (org-element-at-point)))
  21704. (end-of-line)
  21705. (push-mark (org-element-property :end element) t t)
  21706. (goto-char (org-element-property :begin element))))))
  21707. (defun org-narrow-to-element ()
  21708. "Narrow buffer to current element."
  21709. (interactive)
  21710. (let ((elem (org-element-at-point)))
  21711. (cond
  21712. ((eq (car elem) 'headline)
  21713. (narrow-to-region
  21714. (org-element-property :begin elem)
  21715. (org-element-property :end elem)))
  21716. ((memq (car elem) org-element-greater-elements)
  21717. (narrow-to-region
  21718. (org-element-property :contents-begin elem)
  21719. (org-element-property :contents-end elem)))
  21720. (t
  21721. (narrow-to-region
  21722. (org-element-property :begin elem)
  21723. (org-element-property :end elem))))))
  21724. (defun org-transpose-element ()
  21725. "Transpose current and previous elements, keeping blank lines between.
  21726. Point is moved after both elements."
  21727. (interactive)
  21728. (org-skip-whitespace)
  21729. (let ((end (org-element-property :end (org-element-at-point))))
  21730. (org-drag-element-backward)
  21731. (goto-char end)))
  21732. (defun org-unindent-buffer ()
  21733. "Un-indent the visible part of the buffer.
  21734. Relative indentation (between items, inside blocks, etc.) isn't
  21735. modified."
  21736. (interactive)
  21737. (unless (eq major-mode 'org-mode)
  21738. (user-error "Cannot un-indent a buffer not in Org mode"))
  21739. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21740. unindent-tree ; For byte-compiler.
  21741. (unindent-tree
  21742. (function
  21743. (lambda (contents)
  21744. (mapc
  21745. (lambda (element)
  21746. (if (memq (org-element-type element) '(headline section))
  21747. (funcall unindent-tree (org-element-contents element))
  21748. (save-excursion
  21749. (save-restriction
  21750. (narrow-to-region
  21751. (org-element-property :begin element)
  21752. (org-element-property :end element))
  21753. (org-do-remove-indentation)))))
  21754. (reverse contents))))))
  21755. (funcall unindent-tree (org-element-contents parse-tree))))
  21756. (defun org-show-subtree ()
  21757. "Show everything after this heading at deeper levels."
  21758. (interactive)
  21759. (outline-flag-region
  21760. (point)
  21761. (save-excursion
  21762. (org-end-of-subtree t t))
  21763. nil))
  21764. (defun org-show-entry ()
  21765. "Show the body directly following this heading.
  21766. Show the heading too, if it is currently invisible."
  21767. (interactive)
  21768. (save-excursion
  21769. (condition-case nil
  21770. (progn
  21771. (org-back-to-heading t)
  21772. (outline-flag-region
  21773. (max (point-min) (1- (point)))
  21774. (save-excursion
  21775. (if (re-search-forward
  21776. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21777. (match-beginning 1)
  21778. (point-max)))
  21779. nil)
  21780. (org-cycle-hide-drawers 'children))
  21781. (error nil))))
  21782. (defun org-make-options-regexp (kwds &optional extra)
  21783. "Make a regular expression for keyword lines."
  21784. (concat
  21785. "^#\\+\\("
  21786. (mapconcat 'regexp-quote kwds "\\|")
  21787. (if extra (concat "\\|" extra))
  21788. "\\):[ \t]*\\(.*\\)"))
  21789. ;; Make isearch reveal the necessary context
  21790. (defun org-isearch-end ()
  21791. "Reveal context after isearch exits."
  21792. (when isearch-success ; only if search was successful
  21793. (if (featurep 'xemacs)
  21794. ;; Under XEmacs, the hook is run in the correct place,
  21795. ;; we directly show the context.
  21796. (org-show-context 'isearch)
  21797. ;; In Emacs the hook runs *before* restoring the overlays.
  21798. ;; So we have to use a one-time post-command-hook to do this.
  21799. ;; (Emacs 22 has a special variable, see function `org-mode')
  21800. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21801. isearch-mode-end-hook-quit)
  21802. ;; Only when the isearch was not quitted.
  21803. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21804. 'append 'local)))
  21805. (org-fix-ellipsis-at-bol)))
  21806. (defun org-isearch-post-command ()
  21807. "Remove self from hook, and show context."
  21808. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21809. (org-show-context 'isearch))
  21810. ;;;; Integration with and fixes for other packages
  21811. ;;; Imenu support
  21812. (defvar org-imenu-markers nil
  21813. "All markers currently used by Imenu.")
  21814. (make-variable-buffer-local 'org-imenu-markers)
  21815. (defun org-imenu-new-marker (&optional pos)
  21816. "Return a new marker for use by Imenu, and remember the marker."
  21817. (let ((m (make-marker)))
  21818. (move-marker m (or pos (point)))
  21819. (push m org-imenu-markers)
  21820. m))
  21821. (defun org-imenu-get-tree ()
  21822. "Produce the index for Imenu."
  21823. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21824. (setq org-imenu-markers nil)
  21825. (let* ((n org-imenu-depth)
  21826. (re (concat "^" (org-get-limited-outline-regexp)))
  21827. (subs (make-vector (1+ n) nil))
  21828. (last-level 0)
  21829. m level head0 head)
  21830. (save-excursion
  21831. (save-restriction
  21832. (widen)
  21833. (goto-char (point-max))
  21834. (while (re-search-backward re nil t)
  21835. (setq level (org-reduced-level (funcall outline-level)))
  21836. (when (and (<= level n)
  21837. (looking-at org-complex-heading-regexp)
  21838. (setq head0 (org-match-string-no-properties 4)))
  21839. (setq head (org-link-display-format head0)
  21840. m (org-imenu-new-marker))
  21841. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21842. (if (>= level last-level)
  21843. (push (cons head m) (aref subs level))
  21844. (push (cons head (aref subs (1+ level))) (aref subs level))
  21845. (loop for i from (1+ level) to n do (aset subs i nil)))
  21846. (setq last-level level)))))
  21847. (aref subs 1)))
  21848. (eval-after-load "imenu"
  21849. '(progn
  21850. (add-hook 'imenu-after-jump-hook
  21851. (lambda ()
  21852. (if (derived-mode-p 'org-mode)
  21853. (org-show-context 'org-goto))))))
  21854. (defun org-link-display-format (link)
  21855. "Replace a link with its the description.
  21856. If there is no description, use the link target."
  21857. (save-match-data
  21858. (if (string-match org-bracket-link-analytic-regexp link)
  21859. (replace-match (if (match-end 5)
  21860. (match-string 5 link)
  21861. (concat (match-string 1 link)
  21862. (match-string 3 link)))
  21863. nil t link)
  21864. link)))
  21865. (defun org-toggle-link-display ()
  21866. "Toggle the literal or descriptive display of links."
  21867. (interactive)
  21868. (if org-descriptive-links
  21869. (progn (org-remove-from-invisibility-spec '(org-link))
  21870. (org-restart-font-lock)
  21871. (setq org-descriptive-links nil))
  21872. (progn (add-to-invisibility-spec '(org-link))
  21873. (org-restart-font-lock)
  21874. (setq org-descriptive-links t))))
  21875. ;; Speedbar support
  21876. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21877. "Overlay marking the agenda restriction line in speedbar.")
  21878. (overlay-put org-speedbar-restriction-lock-overlay
  21879. 'face 'org-agenda-restriction-lock)
  21880. (overlay-put org-speedbar-restriction-lock-overlay
  21881. 'help-echo "Agendas are currently limited to this item.")
  21882. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21883. (defun org-speedbar-set-agenda-restriction ()
  21884. "Restrict future agenda commands to the location at point in speedbar.
  21885. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21886. (interactive)
  21887. (require 'org-agenda)
  21888. (let (p m tp np dir txt)
  21889. (cond
  21890. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21891. 'org-imenu t))
  21892. (setq m (get-text-property p 'org-imenu-marker))
  21893. (with-current-buffer (marker-buffer m)
  21894. (goto-char m)
  21895. (org-agenda-set-restriction-lock 'subtree)))
  21896. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21897. 'speedbar-function 'speedbar-find-file))
  21898. (setq tp (previous-single-property-change
  21899. (1+ p) 'speedbar-function)
  21900. np (next-single-property-change
  21901. tp 'speedbar-function)
  21902. dir (speedbar-line-directory)
  21903. txt (buffer-substring-no-properties (or tp (point-min))
  21904. (or np (point-max))))
  21905. (with-current-buffer (find-file-noselect
  21906. (let ((default-directory dir))
  21907. (expand-file-name txt)))
  21908. (unless (derived-mode-p 'org-mode)
  21909. (user-error "Cannot restrict to non-Org-mode file"))
  21910. (org-agenda-set-restriction-lock 'file)))
  21911. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21912. (move-overlay org-speedbar-restriction-lock-overlay
  21913. (point-at-bol) (point-at-eol))
  21914. (setq current-prefix-arg nil)
  21915. (org-agenda-maybe-redo)))
  21916. (defvar speedbar-file-key-map)
  21917. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21918. (eval-after-load "speedbar"
  21919. '(progn
  21920. (speedbar-add-supported-extension ".org")
  21921. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21922. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21923. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21924. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21925. (add-hook 'speedbar-visiting-tag-hook
  21926. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21927. ;;; Fixes and Hacks for problems with other packages
  21928. ;; Make flyspell not check words in links, to not mess up our keymap
  21929. (defvar org-element-affiliated-keywords) ; From org-element.el
  21930. (defvar org-element-block-name-alist) ; From org-element.el
  21931. (defun org-mode-flyspell-verify ()
  21932. "Don't let flyspell put overlays at active buttons, or on
  21933. {todo,all-time,additional-option-like}-keywords."
  21934. (require 'org-element) ; For `org-element-affiliated-keywords'
  21935. (let ((pos (max (1- (point)) (point-min)))
  21936. (word (thing-at-point 'word)))
  21937. (and (not (get-text-property pos 'keymap))
  21938. (not (get-text-property pos 'org-no-flyspell))
  21939. (not (member word org-todo-keywords-1))
  21940. (not (member word org-all-time-keywords))
  21941. (not (member word org-options-keywords))
  21942. (not (member word (mapcar 'car org-startup-options)))
  21943. (not (member-ignore-case word org-element-affiliated-keywords))
  21944. (not (member-ignore-case word (org-get-export-keywords)))
  21945. (not (member-ignore-case
  21946. word (mapcar 'car org-element-block-name-alist)))
  21947. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21948. (not (org-in-src-block-p)))))
  21949. (defun org-remove-flyspell-overlays-in (beg end)
  21950. "Remove flyspell overlays in region."
  21951. (and (org-bound-and-true-p flyspell-mode)
  21952. (fboundp 'flyspell-delete-region-overlays)
  21953. (flyspell-delete-region-overlays beg end))
  21954. (add-text-properties beg end '(org-no-flyspell t)))
  21955. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21956. (eval-after-load "bookmark"
  21957. '(if (boundp 'bookmark-after-jump-hook)
  21958. ;; We can use the hook
  21959. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21960. ;; Hook not available, use advice
  21961. (defadvice bookmark-jump (after org-make-visible activate)
  21962. "Make the position visible."
  21963. (org-bookmark-jump-unhide))))
  21964. ;; Make sure saveplace shows the location if it was hidden
  21965. (eval-after-load "saveplace"
  21966. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21967. "Make the position visible."
  21968. (org-bookmark-jump-unhide)))
  21969. ;; Make sure ecb shows the location if it was hidden
  21970. (eval-after-load "ecb"
  21971. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21972. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21973. (if (derived-mode-p 'org-mode)
  21974. (org-show-context))))
  21975. (defun org-bookmark-jump-unhide ()
  21976. "Unhide the current position, to show the bookmark location."
  21977. (and (derived-mode-p 'org-mode)
  21978. (or (outline-invisible-p)
  21979. (save-excursion (goto-char (max (point-min) (1- (point))))
  21980. (outline-invisible-p)))
  21981. (org-show-context 'bookmark-jump)))
  21982. ;; Make session.el ignore our circular variable
  21983. (defvar session-globals-exclude)
  21984. (eval-after-load "session"
  21985. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21986. ;;;; Finish up
  21987. (provide 'org)
  21988. (run-hooks 'org-load-hook)
  21989. ;;; org.el ends here