org.el 907 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-cache-reset "org-element" (&optional all))
  128. (declare-function org-element-contents "org-element" (element))
  129. (declare-function org-element-context "org-element" (&optional element))
  130. (declare-function org-element-interpret-data "org-element"
  131. (data &optional parent))
  132. (declare-function org-element-map "org-element"
  133. (data types fun &optional info first-match no-recursion))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element"
  136. (&optional granularity visible-only))
  137. (declare-function org-element-property "org-element" (property element))
  138. (declare-function org-element-put-property "org-element"
  139. (element property value))
  140. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  141. (declare-function org-element--parse-objects "org-element"
  142. (beg end acc restriction))
  143. (declare-function org-element-parse-buffer "org-element"
  144. (&optional granularity visible-only))
  145. (declare-function org-element-restriction "org-element" (element))
  146. (declare-function org-element-type "org-element" (element))
  147. ;; load languages based on value of `org-babel-load-languages'
  148. (defvar org-babel-load-languages)
  149. ;;;###autoload
  150. (defun org-babel-do-load-languages (sym value)
  151. "Load the languages defined in `org-babel-load-languages'."
  152. (set-default sym value)
  153. (mapc (lambda (pair)
  154. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  155. (if active
  156. (progn
  157. (require (intern (concat "ob-" lang))))
  158. (progn
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-execute:" lang)))
  161. (funcall 'fmakunbound
  162. (intern (concat "org-babel-expand-body:" lang)))))))
  163. org-babel-load-languages))
  164. ;;;###autoload
  165. (defun org-babel-load-file (file &optional compile)
  166. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  167. This function exports the source code using `org-babel-tangle'
  168. and then loads the resulting file using `load-file'. With prefix
  169. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  170. file to byte-code before it is loaded."
  171. (interactive "fFile to load: \nP")
  172. (require 'ob-core)
  173. (let* ((age (lambda (file)
  174. (float-time
  175. (time-subtract (current-time)
  176. (nth 5 (or (file-attributes (file-truename file))
  177. (file-attributes file)))))))
  178. (base-name (file-name-sans-extension file))
  179. (exported-file (concat base-name ".el")))
  180. ;; tangle if the org-mode file is newer than the elisp file
  181. (unless (and (file-exists-p exported-file)
  182. (> (funcall age file) (funcall age exported-file)))
  183. (setq exported-file
  184. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  185. (message "%s %s"
  186. (if compile
  187. (progn (byte-compile-file exported-file 'load)
  188. "Compiled and loaded")
  189. (progn (load-file exported-file) "Loaded"))
  190. exported-file)))
  191. (defcustom org-babel-load-languages '((emacs-lisp . t))
  192. "Languages which can be evaluated in Org-mode buffers.
  193. This list can be used to load support for any of the languages
  194. below, note that each language will depend on a different set of
  195. system executables and/or Emacs modes. When a language is
  196. \"loaded\", then code blocks in that language can be evaluated
  197. with `org-babel-execute-src-block' bound by default to C-c
  198. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  199. be set to remove code block evaluation from the C-c C-c
  200. keybinding. By default only Emacs Lisp (which has no
  201. requirements) is loaded."
  202. :group 'org-babel
  203. :set 'org-babel-do-load-languages
  204. :version "24.1"
  205. :type '(alist :tag "Babel Languages"
  206. :key-type
  207. (choice
  208. (const :tag "Awk" awk)
  209. (const :tag "C" C)
  210. (const :tag "R" R)
  211. (const :tag "Asymptote" asymptote)
  212. (const :tag "Calc" calc)
  213. (const :tag "Clojure" clojure)
  214. (const :tag "CSS" css)
  215. (const :tag "Ditaa" ditaa)
  216. (const :tag "Dot" dot)
  217. (const :tag "Emacs Lisp" emacs-lisp)
  218. (const :tag "Fortran" fortran)
  219. (const :tag "Gnuplot" gnuplot)
  220. (const :tag "Haskell" haskell)
  221. (const :tag "IO" io)
  222. (const :tag "Java" java)
  223. (const :tag "Javascript" js)
  224. (const :tag "LaTeX" latex)
  225. (const :tag "Ledger" ledger)
  226. (const :tag "Lilypond" lilypond)
  227. (const :tag "Lisp" lisp)
  228. (const :tag "Makefile" makefile)
  229. (const :tag "Maxima" maxima)
  230. (const :tag "Matlab" matlab)
  231. (const :tag "Mscgen" mscgen)
  232. (const :tag "Ocaml" ocaml)
  233. (const :tag "Octave" octave)
  234. (const :tag "Org" org)
  235. (const :tag "Perl" perl)
  236. (const :tag "Pico Lisp" picolisp)
  237. (const :tag "PlantUML" plantuml)
  238. (const :tag "Python" python)
  239. (const :tag "Ruby" ruby)
  240. (const :tag "Sass" sass)
  241. (const :tag "Scala" scala)
  242. (const :tag "Scheme" scheme)
  243. (const :tag "Screen" screen)
  244. (const :tag "Shell Script" sh)
  245. (const :tag "Shen" shen)
  246. (const :tag "Sql" sql)
  247. (const :tag "Sqlite" sqlite)
  248. (const :tag "ebnf2ps" ebnf2ps))
  249. :value-type (boolean :tag "Activate" :value t)))
  250. ;;;; Customization variables
  251. (defcustom org-clone-delete-id nil
  252. "Remove ID property of clones of a subtree.
  253. When non-nil, clones of a subtree don't inherit the ID property.
  254. Otherwise they inherit the ID property with a new unique
  255. identifier."
  256. :type 'boolean
  257. :version "24.1"
  258. :group 'org-id)
  259. ;;; Version
  260. (org-check-version)
  261. ;;;###autoload
  262. (defun org-version (&optional here full message)
  263. "Show the org-mode version in the echo area.
  264. With prefix argument HERE, insert it at point.
  265. When FULL is non-nil, use a verbose version string.
  266. When MESSAGE is non-nil, display a message with the version."
  267. (interactive "P")
  268. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  269. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  270. (load-suffixes (list ".el"))
  271. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  272. (org-trash (or
  273. (and (fboundp 'org-release) (fboundp 'org-git-version))
  274. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  275. (load-suffixes save-load-suffixes)
  276. (org-version (org-release))
  277. (git-version (org-git-version))
  278. (version (format "Org-mode version %s (%s @ %s)"
  279. org-version
  280. git-version
  281. (if org-install-dir
  282. (if (string= org-dir org-install-dir)
  283. org-install-dir
  284. (concat "mixed installation! " org-install-dir " and " org-dir))
  285. "org-loaddefs.el can not be found!")))
  286. (_version (if full version org-version)))
  287. (if (org-called-interactively-p 'interactive)
  288. (if here
  289. (insert version)
  290. (message version))
  291. (if message (message _version))
  292. _version)))
  293. (defconst org-version (org-version))
  294. ;;; Compatibility constants
  295. ;;; The custom variables
  296. (defgroup org nil
  297. "Outline-based notes management and organizer."
  298. :tag "Org"
  299. :group 'outlines
  300. :group 'calendar)
  301. (defcustom org-mode-hook nil
  302. "Mode hook for Org-mode, run after the mode was turned on."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-load-hook nil
  306. "Hook that is run after org.el has been loaded."
  307. :group 'org
  308. :type 'hook)
  309. (defcustom org-log-buffer-setup-hook nil
  310. "Hook that is run after an Org log buffer is created."
  311. :group 'org
  312. :version "24.1"
  313. :type 'hook)
  314. (defvar org-modules) ; defined below
  315. (defvar org-modules-loaded nil
  316. "Have the modules been loaded already?")
  317. (defun org-load-modules-maybe (&optional force)
  318. "Load all extensions listed in `org-modules'."
  319. (when (or force (not org-modules-loaded))
  320. (mapc (lambda (ext)
  321. (condition-case nil (require ext)
  322. (error (message "Problems while trying to load feature `%s'" ext))))
  323. org-modules)
  324. (setq org-modules-loaded t)))
  325. (defun org-set-modules (var value)
  326. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  327. (set var value)
  328. (when (featurep 'org)
  329. (org-load-modules-maybe 'force)
  330. (org-element-cache-reset 'all)))
  331. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  332. "Modules that should always be loaded together with org.el.
  333. If a description starts with <C>, the file is not part of Emacs
  334. and loading it will require that you have downloaded and properly
  335. installed the Org mode distribution.
  336. You can also use this system to load external packages (i.e. neither Org
  337. core modules, nor modules from the CONTRIB directory). Just add symbols
  338. to the end of the list. If the package is called org-xyz.el, then you need
  339. to add the symbol `xyz', and the package must have a call to:
  340. \(provide 'org-xyz)
  341. For export specific modules, see also `org-export-backends'."
  342. :group 'org
  343. :set 'org-set-modules
  344. :version "24.4"
  345. :package-version '(Org . "8.0")
  346. :type
  347. '(set :greedy t
  348. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  349. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  350. (const :tag " crypt: Encryption of subtrees" org-crypt)
  351. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  352. (const :tag " docview: Links to doc-view buffers" org-docview)
  353. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  354. (const :tag " habit: Track your consistency with habits" org-habit)
  355. (const :tag " id: Global IDs for identifying entries" org-id)
  356. (const :tag " info: Links to Info nodes" org-info)
  357. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  358. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  359. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  360. (const :tag " mouse: Additional mouse support" org-mouse)
  361. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  362. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  363. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  364. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  365. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  366. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  367. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  368. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  369. (const :tag "C collector: Collect properties into tables" org-collector)
  370. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  371. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  372. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  373. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  374. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  375. (const :tag "C eval: Include command output as text" org-eval)
  376. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  377. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  378. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  379. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  380. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  381. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  382. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  383. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  384. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  385. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  386. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  387. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  388. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  389. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  390. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  391. (const :tag "C registry: A registry for Org-mode links" org-registry)
  392. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  393. (const :tag "C secretary: Team management with org-mode" org-secretary)
  394. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  395. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  396. (const :tag "C track: Keep up with Org-mode development" org-track)
  397. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  398. (const :tag "C vm: Links to VM folders/messages" org-vm)
  399. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  400. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  401. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  402. (defvar org-export--registered-backends) ; From ox.el.
  403. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  404. (declare-function org-export-backend-name "ox" (backend))
  405. (defcustom org-export-backends '(ascii html icalendar latex)
  406. "List of export back-ends that should be always available.
  407. If a description starts with <C>, the file is not part of Emacs
  408. and loading it will require that you have downloaded and properly
  409. installed the Org mode distribution.
  410. Unlike to `org-modules', libraries in this list will not be
  411. loaded along with Org, but only once the export framework is
  412. needed.
  413. This variable needs to be set before org.el is loaded. If you
  414. need to make a change while Emacs is running, use the customize
  415. interface or run the following code, where VAL stands for the new
  416. value of the variable, after updating it:
  417. \(progn
  418. \(setq org-export--registered-backends
  419. \(org-remove-if-not
  420. \(lambda (backend)
  421. \(let ((name (org-export-backend-name backend)))
  422. \(or (memq name val)
  423. \(catch 'parentp
  424. \(dolist (b val)
  425. \(and (org-export-derived-backend-p b name)
  426. \(throw 'parentp t)))))))
  427. org-export--registered-backends))
  428. \(let ((new-list (mapcar 'org-export-backend-name
  429. org-export--registered-backends)))
  430. \(dolist (backend val)
  431. \(cond
  432. \((not (load (format \"ox-%s\" backend) t t))
  433. \(message \"Problems while trying to load export back-end `%s'\"
  434. backend))
  435. \((not (memq backend new-list)) (push backend new-list))))
  436. \(set-default 'org-export-backends new-list)))
  437. Adding a back-end to this list will also pull the back-end it
  438. depends on, if any."
  439. :group 'org
  440. :group 'org-export
  441. :version "24.4"
  442. :package-version '(Org . "8.0")
  443. :initialize 'custom-initialize-set
  444. :set (lambda (var val)
  445. (if (not (featurep 'ox)) (set-default var val)
  446. ;; Any back-end not required anymore (not present in VAL and not
  447. ;; a parent of any back-end in the new value) is removed from the
  448. ;; list of registered back-ends.
  449. (setq org-export--registered-backends
  450. (org-remove-if-not
  451. (lambda (backend)
  452. (let ((name (org-export-backend-name backend)))
  453. (or (memq name val)
  454. (catch 'parentp
  455. (dolist (b val)
  456. (and (org-export-derived-backend-p b name)
  457. (throw 'parentp t)))))))
  458. org-export--registered-backends))
  459. ;; Now build NEW-LIST of both new back-ends and required
  460. ;; parents.
  461. (let ((new-list (mapcar 'org-export-backend-name
  462. org-export--registered-backends)))
  463. (dolist (backend val)
  464. (cond
  465. ((not (load (format "ox-%s" backend) t t))
  466. (message "Problems while trying to load export back-end `%s'"
  467. backend))
  468. ((not (memq backend new-list)) (push backend new-list))))
  469. ;; Set VAR to that list with fixed dependencies.
  470. (set-default var new-list))))
  471. :type '(set :greedy t
  472. (const :tag " ascii Export buffer to ASCII format" ascii)
  473. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  474. (const :tag " html Export buffer to HTML format" html)
  475. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  476. (const :tag " latex Export buffer to LaTeX format" latex)
  477. (const :tag " man Export buffer to MAN format" man)
  478. (const :tag " md Export buffer to Markdown format" md)
  479. (const :tag " odt Export buffer to ODT format" odt)
  480. (const :tag " org Export buffer to Org format" org)
  481. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  482. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  483. (const :tag "C deck Export buffer to deck.js presentations" deck)
  484. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  485. (const :tag "C groff Export buffer to Groff format" groff)
  486. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  487. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  488. (const :tag "C s5 Export buffer to s5 presentations" s5)
  489. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  490. (eval-after-load 'ox
  491. '(mapc
  492. (lambda (backend)
  493. (condition-case nil (require (intern (format "ox-%s" backend)))
  494. (error (message "Problems while trying to load export back-end `%s'"
  495. backend))))
  496. org-export-backends))
  497. (defcustom org-support-shift-select nil
  498. "Non-nil means make shift-cursor commands select text when possible.
  499. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  500. start selecting a region, or enlarge regions started in this way.
  501. In Org-mode, in special contexts, these same keys are used for
  502. other purposes, important enough to compete with shift selection.
  503. Org tries to balance these needs by supporting `shift-select-mode'
  504. outside these special contexts, under control of this variable.
  505. The default of this variable is nil, to avoid confusing behavior. Shifted
  506. cursor keys will then execute Org commands in the following contexts:
  507. - on a headline, changing TODO state (left/right) and priority (up/down)
  508. - on a time stamp, changing the time
  509. - in a plain list item, changing the bullet type
  510. - in a property definition line, switching between allowed values
  511. - in the BEGIN line of a clock table (changing the time block).
  512. Outside these contexts, the commands will throw an error.
  513. When this variable is t and the cursor is not in a special
  514. context, Org-mode will support shift-selection for making and
  515. enlarging regions. To make this more effective, the bullet
  516. cycling will no longer happen anywhere in an item line, but only
  517. if the cursor is exactly on the bullet.
  518. If you set this variable to the symbol `always', then the keys
  519. will not be special in headlines, property lines, and item lines,
  520. to make shift selection work there as well. If this is what you
  521. want, you can use the following alternative commands: `C-c C-t'
  522. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  523. can be used to switch TODO sets, `C-c -' to cycle item bullet
  524. types, and properties can be edited by hand or in column view.
  525. However, when the cursor is on a timestamp, shift-cursor commands
  526. will still edit the time stamp - this is just too good to give up.
  527. XEmacs user should have this variable set to nil, because
  528. `shift-select-mode' is in Emacs 23 or later only."
  529. :group 'org
  530. :type '(choice
  531. (const :tag "Never" nil)
  532. (const :tag "When outside special context" t)
  533. (const :tag "Everywhere except timestamps" always)))
  534. (defcustom org-loop-over-headlines-in-active-region nil
  535. "Shall some commands act upon headlines in the active region?
  536. When set to `t', some commands will be performed in all headlines
  537. within the active region.
  538. When set to `start-level', some commands will be performed in all
  539. headlines within the active region, provided that these headlines
  540. are of the same level than the first one.
  541. When set to a string, those commands will be performed on the
  542. matching headlines within the active region. Such string must be
  543. a tags/property/todo match as it is used in the agenda tags view.
  544. The list of commands is: `org-schedule', `org-deadline',
  545. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  546. `org-archive-to-archive-sibling'. The archiving commands skip
  547. already archived entries."
  548. :type '(choice (const :tag "Don't loop" nil)
  549. (const :tag "All headlines in active region" t)
  550. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  551. (string :tag "Tags/Property/Todo matcher"))
  552. :version "24.1"
  553. :group 'org-todo
  554. :group 'org-archive)
  555. (defgroup org-startup nil
  556. "Options concerning startup of Org-mode."
  557. :tag "Org Startup"
  558. :group 'org)
  559. (defcustom org-startup-folded t
  560. "Non-nil means entering Org-mode will switch to OVERVIEW.
  561. This can also be configured on a per-file basis by adding one of
  562. the following lines anywhere in the buffer:
  563. #+STARTUP: fold (or `overview', this is equivalent)
  564. #+STARTUP: nofold (or `showall', this is equivalent)
  565. #+STARTUP: content
  566. #+STARTUP: showeverything
  567. By default, this option is ignored when Org opens agenda files
  568. for the first time. If you want the agenda to honor the startup
  569. option, set `org-agenda-inhibit-startup' to nil."
  570. :group 'org-startup
  571. :type '(choice
  572. (const :tag "nofold: show all" nil)
  573. (const :tag "fold: overview" t)
  574. (const :tag "content: all headlines" content)
  575. (const :tag "show everything, even drawers" showeverything)))
  576. (defcustom org-startup-truncated t
  577. "Non-nil means entering Org-mode will set `truncate-lines'.
  578. This is useful since some lines containing links can be very long and
  579. uninteresting. Also tables look terrible when wrapped."
  580. :group 'org-startup
  581. :type 'boolean)
  582. (defcustom org-startup-indented nil
  583. "Non-nil means turn on `org-indent-mode' on startup.
  584. This can also be configured on a per-file basis by adding one of
  585. the following lines anywhere in the buffer:
  586. #+STARTUP: indent
  587. #+STARTUP: noindent"
  588. :group 'org-structure
  589. :type '(choice
  590. (const :tag "Not" nil)
  591. (const :tag "Globally (slow on startup in large files)" t)))
  592. (defcustom org-use-sub-superscripts t
  593. "Non-nil means interpret \"_\" and \"^\" for display.
  594. When this option is turned on, you can use TeX-like syntax for sub- and
  595. superscripts. Several characters after \"_\" or \"^\" will be
  596. considered as a single item - so grouping with {} is normally not
  597. needed. For example, the following things will be parsed as single
  598. sub- or superscripts.
  599. 10^24 or 10^tau several digits will be considered 1 item.
  600. 10^-12 or 10^-tau a leading sign with digits or a word
  601. x^2-y^3 will be read as x^2 - y^3, because items are
  602. terminated by almost any nonword/nondigit char.
  603. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  604. Still, ambiguity is possible - so when in doubt use {} to enclose
  605. the sub/superscript. If you set this variable to the symbol
  606. `{}', the braces are *required* in order to trigger
  607. interpretations as sub/superscript. This can be helpful in
  608. documents that need \"_\" frequently in plain text."
  609. :group 'org-startup
  610. :version "24.1"
  611. :type '(choice
  612. (const :tag "Always interpret" t)
  613. (const :tag "Only with braces" {})
  614. (const :tag "Never interpret" nil)))
  615. (defcustom org-startup-with-beamer-mode nil
  616. "Non-nil means turn on `org-beamer-mode' on startup.
  617. This can also be configured on a per-file basis by adding one of
  618. the following lines anywhere in the buffer:
  619. #+STARTUP: beamer"
  620. :group 'org-startup
  621. :version "24.1"
  622. :type 'boolean)
  623. (defcustom org-startup-align-all-tables nil
  624. "Non-nil means align all tables when visiting a file.
  625. This is useful when the column width in tables is forced with <N> cookies
  626. in table fields. Such tables will look correct only after the first re-align.
  627. This can also be configured on a per-file basis by adding one of
  628. the following lines anywhere in the buffer:
  629. #+STARTUP: align
  630. #+STARTUP: noalign"
  631. :group 'org-startup
  632. :type 'boolean)
  633. (defcustom org-startup-with-inline-images nil
  634. "Non-nil means show inline images when loading a new Org file.
  635. This can also be configured on a per-file basis by adding one of
  636. the following lines anywhere in the buffer:
  637. #+STARTUP: inlineimages
  638. #+STARTUP: noinlineimages"
  639. :group 'org-startup
  640. :version "24.1"
  641. :type 'boolean)
  642. (defcustom org-startup-with-latex-preview nil
  643. "Non-nil means preview LaTeX fragments when loading a new Org file.
  644. This can also be configured on a per-file basis by adding one of
  645. the followinglines anywhere in the buffer:
  646. #+STARTUP: latexpreview
  647. #+STARTUP: nolatexpreview"
  648. :group 'org-startup
  649. :version "24.4"
  650. :package-version '(Org . "8.0")
  651. :type 'boolean)
  652. (defcustom org-insert-mode-line-in-empty-file nil
  653. "Non-nil means insert the first line setting Org-mode in empty files.
  654. When the function `org-mode' is called interactively in an empty file, this
  655. normally means that the file name does not automatically trigger Org-mode.
  656. To ensure that the file will always be in Org-mode in the future, a
  657. line enforcing Org-mode will be inserted into the buffer, if this option
  658. has been set."
  659. :group 'org-startup
  660. :type 'boolean)
  661. (defcustom org-replace-disputed-keys nil
  662. "Non-nil means use alternative key bindings for some keys.
  663. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  664. These keys are also used by other packages like shift-selection-mode'
  665. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  666. If you want to use Org-mode together with one of these other modes,
  667. or more generally if you would like to move some Org-mode commands to
  668. other keys, set this variable and configure the keys with the variable
  669. `org-disputed-keys'.
  670. This option is only relevant at load-time of Org-mode, and must be set
  671. *before* org.el is loaded. Changing it requires a restart of Emacs to
  672. become effective."
  673. :group 'org-startup
  674. :type 'boolean)
  675. (defcustom org-use-extra-keys nil
  676. "Non-nil means use extra key sequence definitions for certain commands.
  677. This happens automatically if you run XEmacs or if `window-system'
  678. is nil. This variable lets you do the same manually. You must
  679. set it before loading org.
  680. Example: on Carbon Emacs 22 running graphically, with an external
  681. keyboard on a Powerbook, the default way of setting M-left might
  682. not work for either Alt or ESC. Setting this variable will make
  683. it work for ESC."
  684. :group 'org-startup
  685. :type 'boolean)
  686. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  687. (defcustom org-disputed-keys
  688. '(([(shift up)] . [(meta p)])
  689. ([(shift down)] . [(meta n)])
  690. ([(shift left)] . [(meta -)])
  691. ([(shift right)] . [(meta +)])
  692. ([(control shift right)] . [(meta shift +)])
  693. ([(control shift left)] . [(meta shift -)]))
  694. "Keys for which Org-mode and other modes compete.
  695. This is an alist, cars are the default keys, second element specifies
  696. the alternative to use when `org-replace-disputed-keys' is t.
  697. Keys can be specified in any syntax supported by `define-key'.
  698. The value of this option takes effect only at Org-mode's startup,
  699. therefore you'll have to restart Emacs to apply it after changing."
  700. :group 'org-startup
  701. :type 'alist)
  702. (defun org-key (key)
  703. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  704. Or return the original if not disputed.
  705. Also apply the translations defined in `org-xemacs-key-equivalents'."
  706. (when org-replace-disputed-keys
  707. (let* ((nkey (key-description key))
  708. (x (org-find-if (lambda (x)
  709. (equal (key-description (car x)) nkey))
  710. org-disputed-keys)))
  711. (setq key (if x (cdr x) key))))
  712. (when (featurep 'xemacs)
  713. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  714. key)
  715. (defun org-find-if (predicate seq)
  716. (catch 'exit
  717. (while seq
  718. (if (funcall predicate (car seq))
  719. (throw 'exit (car seq))
  720. (pop seq)))))
  721. (defun org-defkey (keymap key def)
  722. "Define a key, possibly translated, as returned by `org-key'."
  723. (define-key keymap (org-key key) def))
  724. (defcustom org-ellipsis nil
  725. "The ellipsis to use in the Org-mode outline.
  726. When nil, just use the standard three dots. When a string, use that instead,
  727. When a face, use the standard 3 dots, but with the specified face.
  728. The change affects only Org-mode (which will then use its own display table).
  729. Changing this requires executing `M-x org-mode' in a buffer to become
  730. effective."
  731. :group 'org-startup
  732. :type '(choice (const :tag "Default" nil)
  733. (face :tag "Face" :value org-warning)
  734. (string :tag "String" :value "...#")))
  735. (defvar org-display-table nil
  736. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  737. (defgroup org-keywords nil
  738. "Keywords in Org-mode."
  739. :tag "Org Keywords"
  740. :group 'org)
  741. (defcustom org-deadline-string "DEADLINE:"
  742. "String to mark deadline entries.
  743. A deadline is this string, followed by a time stamp. Should be a word,
  744. terminated by a colon. You can insert a schedule keyword and
  745. a timestamp with \\[org-deadline].
  746. Changes become only effective after restarting Emacs."
  747. :group 'org-keywords
  748. :type 'string)
  749. (defcustom org-scheduled-string "SCHEDULED:"
  750. "String to mark scheduled TODO entries.
  751. A schedule is this string, followed by a time stamp. Should be a word,
  752. terminated by a colon. You can insert a schedule keyword and
  753. a timestamp with \\[org-schedule].
  754. Changes become only effective after restarting Emacs."
  755. :group 'org-keywords
  756. :type 'string)
  757. (defcustom org-closed-string "CLOSED:"
  758. "String used as the prefix for timestamps logging closing a TODO entry."
  759. :group 'org-keywords
  760. :type 'string)
  761. (defcustom org-clock-string "CLOCK:"
  762. "String used as prefix for timestamps clocking work hours on an item."
  763. :group 'org-keywords
  764. :type 'string)
  765. (defcustom org-closed-keep-when-no-todo nil
  766. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  767. :group 'org-todo
  768. :group 'org-keywords
  769. :version "24.4"
  770. :package-version '(Org . "8.0")
  771. :type 'boolean)
  772. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  773. org-scheduled-string "\\|"
  774. org-deadline-string "\\|"
  775. org-closed-string "\\|"
  776. org-clock-string "\\)")
  777. "Matches a line with planning or clock info.")
  778. (defcustom org-comment-string "COMMENT"
  779. "Entries starting with this keyword will never be exported.
  780. An entry can be toggled between COMMENT and normal with
  781. \\[org-toggle-comment].
  782. Changes become only effective after restarting Emacs."
  783. :group 'org-keywords
  784. :type 'string)
  785. (defcustom org-quote-string "QUOTE"
  786. "Entries starting with this keyword will be exported in fixed-width font.
  787. Quoting applies only to the text in the entry following the headline, and does
  788. not extend beyond the next headline, even if that is lower level.
  789. An entry can be toggled between QUOTE and normal with
  790. \\[org-toggle-fixed-width-section]."
  791. :group 'org-keywords
  792. :type 'string)
  793. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  794. "Matches first line of a hidden block.
  795. Group 1 contains drawer's name.")
  796. (defconst org-repeat-re
  797. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  798. "Regular expression for specifying repeated events.
  799. After a match, group 1 contains the repeat expression.")
  800. (defgroup org-structure nil
  801. "Options concerning the general structure of Org-mode files."
  802. :tag "Org Structure"
  803. :group 'org)
  804. (defgroup org-reveal-location nil
  805. "Options about how to make context of a location visible."
  806. :tag "Org Reveal Location"
  807. :group 'org-structure)
  808. (defconst org-context-choice
  809. '(choice
  810. (const :tag "Always" t)
  811. (const :tag "Never" nil)
  812. (repeat :greedy t :tag "Individual contexts"
  813. (cons
  814. (choice :tag "Context"
  815. (const agenda)
  816. (const org-goto)
  817. (const occur-tree)
  818. (const tags-tree)
  819. (const link-search)
  820. (const mark-goto)
  821. (const bookmark-jump)
  822. (const isearch)
  823. (const default))
  824. (boolean))))
  825. "Contexts for the reveal options.")
  826. (defcustom org-show-hierarchy-above '((default . t))
  827. "Non-nil means show full hierarchy when revealing a location.
  828. Org-mode often shows locations in an org-mode file which might have
  829. been invisible before. When this is set, the hierarchy of headings
  830. above the exposed location is shown.
  831. Turning this off for example for sparse trees makes them very compact.
  832. Instead of t, this can also be an alist specifying this option for different
  833. contexts. Valid contexts are
  834. agenda when exposing an entry from the agenda
  835. org-goto when using the command `org-goto' on key C-c C-j
  836. occur-tree when using the command `org-occur' on key C-c /
  837. tags-tree when constructing a sparse tree based on tags matches
  838. link-search when exposing search matches associated with a link
  839. mark-goto when exposing the jump goal of a mark
  840. bookmark-jump when exposing a bookmark location
  841. isearch when exiting from an incremental search
  842. default default for all contexts not set explicitly"
  843. :group 'org-reveal-location
  844. :type org-context-choice)
  845. (defcustom org-show-following-heading '((default . nil))
  846. "Non-nil means show following heading when revealing a location.
  847. Org-mode often shows locations in an org-mode file which might have
  848. been invisible before. When this is set, the heading following the
  849. match is shown.
  850. Turning this off for example for sparse trees makes them very compact,
  851. but makes it harder to edit the location of the match. In such a case,
  852. use the command \\[org-reveal] to show more context.
  853. Instead of t, this can also be an alist specifying this option for different
  854. contexts. See `org-show-hierarchy-above' for valid contexts."
  855. :group 'org-reveal-location
  856. :type org-context-choice)
  857. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  858. "Non-nil means show all sibling heading when revealing a location.
  859. Org-mode often shows locations in an org-mode file which might have
  860. been invisible before. When this is set, the sibling of the current entry
  861. heading are all made visible. If `org-show-hierarchy-above' is t,
  862. the same happens on each level of the hierarchy above the current entry.
  863. By default this is on for the isearch context, off for all other contexts.
  864. Turning this off for example for sparse trees makes them very compact,
  865. but makes it harder to edit the location of the match. In such a case,
  866. use the command \\[org-reveal] to show more context.
  867. Instead of t, this can also be an alist specifying this option for different
  868. contexts. See `org-show-hierarchy-above' for valid contexts."
  869. :group 'org-reveal-location
  870. :type org-context-choice
  871. :version "24.4"
  872. :package-version '(Org . "8.0"))
  873. (defcustom org-show-entry-below '((default . nil))
  874. "Non-nil means show the entry below a headline when revealing a location.
  875. Org-mode often shows locations in an org-mode file which might have
  876. been invisible before. When this is set, the text below the headline that is
  877. exposed is also shown.
  878. By default this is off for all contexts.
  879. Instead of t, this can also be an alist specifying this option for different
  880. contexts. See `org-show-hierarchy-above' for valid contexts."
  881. :group 'org-reveal-location
  882. :type org-context-choice)
  883. (defcustom org-indirect-buffer-display 'other-window
  884. "How should indirect tree buffers be displayed?
  885. This applies to indirect buffers created with the commands
  886. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  887. Valid values are:
  888. current-window Display in the current window
  889. other-window Just display in another window.
  890. dedicated-frame Create one new frame, and re-use it each time.
  891. new-frame Make a new frame each time. Note that in this case
  892. previously-made indirect buffers are kept, and you need to
  893. kill these buffers yourself."
  894. :group 'org-structure
  895. :group 'org-agenda-windows
  896. :type '(choice
  897. (const :tag "In current window" current-window)
  898. (const :tag "In current frame, other window" other-window)
  899. (const :tag "Each time a new frame" new-frame)
  900. (const :tag "One dedicated frame" dedicated-frame)))
  901. (defcustom org-use-speed-commands nil
  902. "Non-nil means activate single letter commands at beginning of a headline.
  903. This may also be a function to test for appropriate locations where speed
  904. commands should be active."
  905. :group 'org-structure
  906. :type '(choice
  907. (const :tag "Never" nil)
  908. (const :tag "At beginning of headline stars" t)
  909. (function)))
  910. (defcustom org-speed-commands-user nil
  911. "Alist of additional speed commands.
  912. This list will be checked before `org-speed-commands-default'
  913. when the variable `org-use-speed-commands' is non-nil
  914. and when the cursor is at the beginning of a headline.
  915. The car if each entry is a string with a single letter, which must
  916. be assigned to `self-insert-command' in the global map.
  917. The cdr is either a command to be called interactively, a function
  918. to be called, or a form to be evaluated.
  919. An entry that is just a list with a single string will be interpreted
  920. as a descriptive headline that will be added when listing the speed
  921. commands in the Help buffer using the `?' speed command."
  922. :group 'org-structure
  923. :type '(repeat :value ("k" . ignore)
  924. (choice :value ("k" . ignore)
  925. (list :tag "Descriptive Headline" (string :tag "Headline"))
  926. (cons :tag "Letter and Command"
  927. (string :tag "Command letter")
  928. (choice
  929. (function)
  930. (sexp))))))
  931. (defcustom org-bookmark-names-plist
  932. '(:last-capture "org-capture-last-stored"
  933. :last-refile "org-refile-last-stored"
  934. :last-capture-marker "org-capture-last-stored-marker")
  935. "Names for bookmarks automatically set by some Org commands.
  936. This can provide strings as names for a number of bookmakrs Org sets
  937. automatically. The following keys are currently implemented:
  938. :last-capture
  939. :last-capture-marker
  940. :last-refile
  941. When a key does not show up in the property list, the corresponding bookmark
  942. is not set."
  943. :group 'org-structure
  944. :type 'plist)
  945. (defgroup org-cycle nil
  946. "Options concerning visibility cycling in Org-mode."
  947. :tag "Org Cycle"
  948. :group 'org-structure)
  949. (defcustom org-cycle-skip-children-state-if-no-children t
  950. "Non-nil means skip CHILDREN state in entries that don't have any."
  951. :group 'org-cycle
  952. :type 'boolean)
  953. (defcustom org-cycle-max-level nil
  954. "Maximum level which should still be subject to visibility cycling.
  955. Levels higher than this will, for cycling, be treated as text, not a headline.
  956. When `org-odd-levels-only' is set, a value of N in this variable actually
  957. means 2N-1 stars as the limiting headline.
  958. When nil, cycle all levels.
  959. Note that the limiting level of cycling is also influenced by
  960. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  961. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  962. than its value."
  963. :group 'org-cycle
  964. :type '(choice
  965. (const :tag "No limit" nil)
  966. (integer :tag "Maximum level")))
  967. (defcustom org-hide-block-startup nil
  968. "Non-nil means entering Org-mode will fold all blocks.
  969. This can also be set in on a per-file basis with
  970. #+STARTUP: hideblocks
  971. #+STARTUP: showblocks"
  972. :group 'org-startup
  973. :group 'org-cycle
  974. :type 'boolean)
  975. (defcustom org-cycle-global-at-bob nil
  976. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  977. This makes it possible to do global cycling without having to use S-TAB or
  978. \\[universal-argument] TAB. For this special case to work, the first line
  979. of the buffer must not be a headline -- it may be empty or some other text.
  980. When used in this way, `org-cycle-hook' is disabled temporarily to make
  981. sure the cursor stays at the beginning of the buffer. When this option is
  982. nil, don't do anything special at the beginning of the buffer."
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-level-after-item/entry-creation t
  986. "Non-nil means cycle entry level or item indentation in new empty entries.
  987. When the cursor is at the end of an empty headline, i.e., with only stars
  988. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  989. and then all possible ancestor states, before returning to the original state.
  990. This makes data entry extremely fast: M-RET to create a new headline,
  991. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  992. When the cursor is at the end of an empty plain list item, one TAB will
  993. make it a subitem, two or more tabs will back up to make this an item
  994. higher up in the item hierarchy."
  995. :group 'org-cycle
  996. :type 'boolean)
  997. (defcustom org-cycle-emulate-tab t
  998. "Where should `org-cycle' emulate TAB.
  999. nil Never
  1000. white Only in completely white lines
  1001. whitestart Only at the beginning of lines, before the first non-white char
  1002. t Everywhere except in headlines
  1003. exc-hl-bol Everywhere except at the start of a headline
  1004. If TAB is used in a place where it does not emulate TAB, the current subtree
  1005. visibility is cycled."
  1006. :group 'org-cycle
  1007. :type '(choice (const :tag "Never" nil)
  1008. (const :tag "Only in completely white lines" white)
  1009. (const :tag "Before first char in a line" whitestart)
  1010. (const :tag "Everywhere except in headlines" t)
  1011. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1012. (defcustom org-cycle-separator-lines 2
  1013. "Number of empty lines needed to keep an empty line between collapsed trees.
  1014. If you leave an empty line between the end of a subtree and the following
  1015. headline, this empty line is hidden when the subtree is folded.
  1016. Org-mode will leave (exactly) one empty line visible if the number of
  1017. empty lines is equal or larger to the number given in this variable.
  1018. So the default 2 means at least 2 empty lines after the end of a subtree
  1019. are needed to produce free space between a collapsed subtree and the
  1020. following headline.
  1021. If the number is negative, and the number of empty lines is at least -N,
  1022. all empty lines are shown.
  1023. Special case: when 0, never leave empty lines in collapsed view."
  1024. :group 'org-cycle
  1025. :type 'integer)
  1026. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1027. (defcustom org-pre-cycle-hook nil
  1028. "Hook that is run before visibility cycling is happening.
  1029. The function(s) in this hook must accept a single argument which indicates
  1030. the new state that will be set right after running this hook. The
  1031. argument is a symbol. Before a global state change, it can have the values
  1032. `overview', `content', or `all'. Before a local state change, it can have
  1033. the values `folded', `children', or `subtree'."
  1034. :group 'org-cycle
  1035. :type 'hook)
  1036. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1037. org-cycle-hide-drawers
  1038. org-cycle-hide-inline-tasks
  1039. org-cycle-show-empty-lines
  1040. org-optimize-window-after-visibility-change)
  1041. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1042. The function(s) in this hook must accept a single argument which indicates
  1043. the new state that was set by the most recent `org-cycle' command. The
  1044. argument is a symbol. After a global state change, it can have the values
  1045. `overview', `contents', or `all'. After a local state change, it can have
  1046. the values `folded', `children', or `subtree'."
  1047. :group 'org-cycle
  1048. :type 'hook)
  1049. (defgroup org-edit-structure nil
  1050. "Options concerning structure editing in Org-mode."
  1051. :tag "Org Edit Structure"
  1052. :group 'org-structure)
  1053. (defcustom org-odd-levels-only nil
  1054. "Non-nil means skip even levels and only use odd levels for the outline.
  1055. This has the effect that two stars are being added/taken away in
  1056. promotion/demotion commands. It also influences how levels are
  1057. handled by the exporters.
  1058. Changing it requires restart of `font-lock-mode' to become effective
  1059. for fontification also in regions already fontified.
  1060. You may also set this on a per-file basis by adding one of the following
  1061. lines to the buffer:
  1062. #+STARTUP: odd
  1063. #+STARTUP: oddeven"
  1064. :group 'org-edit-structure
  1065. :group 'org-appearance
  1066. :type 'boolean)
  1067. (defcustom org-adapt-indentation t
  1068. "Non-nil means adapt indentation to outline node level.
  1069. When this variable is set, Org assumes that you write outlines by
  1070. indenting text in each node to align with the headline (after the stars).
  1071. The following issues are influenced by this variable:
  1072. - When this is set and the *entire* text in an entry is indented, the
  1073. indentation is increased by one space in a demotion command, and
  1074. decreased by one in a promotion command. If any line in the entry
  1075. body starts with text at column 0, indentation is not changed at all.
  1076. - Property drawers and planning information is inserted indented when
  1077. this variable s set. When nil, they will not be indented.
  1078. - TAB indents a line relative to context. The lines below a headline
  1079. will be indented when this variable is set.
  1080. Note that this is all about true indentation, by adding and removing
  1081. space characters. See also `org-indent.el' which does level-dependent
  1082. indentation in a virtual way, i.e. at display time in Emacs."
  1083. :group 'org-edit-structure
  1084. :type 'boolean)
  1085. (defcustom org-special-ctrl-a/e nil
  1086. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1087. When t, `C-a' will bring back the cursor to the beginning of the
  1088. headline text, i.e. after the stars and after a possible TODO
  1089. keyword. In an item, this will be the position after bullet and
  1090. check-box, if any. When the cursor is already at that position,
  1091. another `C-a' will bring it to the beginning of the line.
  1092. `C-e' will jump to the end of the headline, ignoring the presence
  1093. of tags in the headline. A second `C-e' will then jump to the
  1094. true end of the line, after any tags. This also means that, when
  1095. this variable is non-nil, `C-e' also will never jump beyond the
  1096. end of the heading of a folded section, i.e. not after the
  1097. ellipses.
  1098. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1099. normally, going to the true line boundary first. Only a directly
  1100. following, identical keypress will bring the cursor to the
  1101. special positions.
  1102. This may also be a cons cell where the behavior for `C-a' and
  1103. `C-e' is set separately."
  1104. :group 'org-edit-structure
  1105. :type '(choice
  1106. (const :tag "off" nil)
  1107. (const :tag "on: after stars/bullet and before tags first" t)
  1108. (const :tag "reversed: true line boundary first" reversed)
  1109. (cons :tag "Set C-a and C-e separately"
  1110. (choice :tag "Special C-a"
  1111. (const :tag "off" nil)
  1112. (const :tag "on: after stars/bullet first" t)
  1113. (const :tag "reversed: before stars/bullet first" reversed))
  1114. (choice :tag "Special C-e"
  1115. (const :tag "off" nil)
  1116. (const :tag "on: before tags first" t)
  1117. (const :tag "reversed: after tags first" reversed)))))
  1118. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1119. (defcustom org-special-ctrl-k nil
  1120. "Non-nil means `C-k' will behave specially in headlines.
  1121. When nil, `C-k' will call the default `kill-line' command.
  1122. When t, the following will happen while the cursor is in the headline:
  1123. - When the cursor is at the beginning of a headline, kill the entire
  1124. line and possible the folded subtree below the line.
  1125. - When in the middle of the headline text, kill the headline up to the tags.
  1126. - When after the headline text, kill the tags."
  1127. :group 'org-edit-structure
  1128. :type 'boolean)
  1129. (defcustom org-ctrl-k-protect-subtree nil
  1130. "Non-nil means, do not delete a hidden subtree with C-k.
  1131. When set to the symbol `error', simply throw an error when C-k is
  1132. used to kill (part-of) a headline that has hidden text behind it.
  1133. Any other non-nil value will result in a query to the user, if it is
  1134. OK to kill that hidden subtree. When nil, kill without remorse."
  1135. :group 'org-edit-structure
  1136. :version "24.1"
  1137. :type '(choice
  1138. (const :tag "Do not protect hidden subtrees" nil)
  1139. (const :tag "Protect hidden subtrees with a security query" t)
  1140. (const :tag "Never kill a hidden subtree with C-k" error)))
  1141. (defcustom org-special-ctrl-o t
  1142. "Non-nil means, make `C-o' insert a row in tables."
  1143. :group 'org-edit-structure
  1144. :type 'boolean)
  1145. (defcustom org-catch-invisible-edits nil
  1146. "Check if in invisible region before inserting or deleting a character.
  1147. Valid values are:
  1148. nil Do not check, so just do invisible edits.
  1149. error Throw an error and do nothing.
  1150. show Make point visible, and do the requested edit.
  1151. show-and-error Make point visible, then throw an error and abort the edit.
  1152. smart Make point visible, and do insertion/deletion if it is
  1153. adjacent to visible text and the change feels predictable.
  1154. Never delete a previously invisible character or add in the
  1155. middle or right after an invisible region. Basically, this
  1156. allows insertion and backward-delete right before ellipses.
  1157. FIXME: maybe in this case we should not even show?"
  1158. :group 'org-edit-structure
  1159. :version "24.1"
  1160. :type '(choice
  1161. (const :tag "Do not check" nil)
  1162. (const :tag "Throw error when trying to edit" error)
  1163. (const :tag "Unhide, but do not do the edit" show-and-error)
  1164. (const :tag "Show invisible part and do the edit" show)
  1165. (const :tag "Be smart and do the right thing" smart)))
  1166. (defcustom org-yank-folded-subtrees t
  1167. "Non-nil means when yanking subtrees, fold them.
  1168. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1169. it starts with a heading and all other headings in it are either children
  1170. or siblings, then fold all the subtrees. However, do this only if no
  1171. text after the yank would be swallowed into a folded tree by this action."
  1172. :group 'org-edit-structure
  1173. :type 'boolean)
  1174. (defcustom org-yank-adjusted-subtrees nil
  1175. "Non-nil means when yanking subtrees, adjust the level.
  1176. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1177. this function for details."
  1178. :group 'org-edit-structure
  1179. :type 'boolean)
  1180. (defcustom org-M-RET-may-split-line '((default . t))
  1181. "Non-nil means M-RET will split the line at the cursor position.
  1182. When nil, it will go to the end of the line before making a
  1183. new line.
  1184. You may also set this option in a different way for different
  1185. contexts. Valid contexts are:
  1186. headline when creating a new headline
  1187. item when creating a new item
  1188. table in a table field
  1189. default the value to be used for all contexts not explicitly
  1190. customized"
  1191. :group 'org-structure
  1192. :group 'org-table
  1193. :type '(choice
  1194. (const :tag "Always" t)
  1195. (const :tag "Never" nil)
  1196. (repeat :greedy t :tag "Individual contexts"
  1197. (cons
  1198. (choice :tag "Context"
  1199. (const headline)
  1200. (const item)
  1201. (const table)
  1202. (const default))
  1203. (boolean)))))
  1204. (defcustom org-insert-heading-respect-content nil
  1205. "Non-nil means insert new headings after the current subtree.
  1206. When nil, the new heading is created directly after the current line.
  1207. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1208. this variable on for the duration of the command."
  1209. :group 'org-structure
  1210. :type 'boolean)
  1211. (defcustom org-blank-before-new-entry '((heading . auto)
  1212. (plain-list-item . auto))
  1213. "Should `org-insert-heading' leave a blank line before new heading/item?
  1214. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1215. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1216. which case Org will look at the surrounding headings/items and try to
  1217. make an intelligent decision whether to insert a blank line or not.
  1218. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1219. the setting here is ignored and no empty line is inserted to avoid breaking
  1220. the list structure."
  1221. :group 'org-edit-structure
  1222. :type '(list
  1223. (cons (const heading)
  1224. (choice (const :tag "Never" nil)
  1225. (const :tag "Always" t)
  1226. (const :tag "Auto" auto)))
  1227. (cons (const plain-list-item)
  1228. (choice (const :tag "Never" nil)
  1229. (const :tag "Always" t)
  1230. (const :tag "Auto" auto)))))
  1231. (defcustom org-insert-heading-hook nil
  1232. "Hook being run after inserting a new heading."
  1233. :group 'org-edit-structure
  1234. :type 'hook)
  1235. (defcustom org-enable-fixed-width-editor t
  1236. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1237. This currently only means they are never auto-wrapped.
  1238. When nil, such lines will be treated like ordinary lines.
  1239. See also the QUOTE keyword."
  1240. :group 'org-edit-structure
  1241. :type 'boolean)
  1242. (defcustom org-goto-auto-isearch t
  1243. "Non-nil means typing characters in `org-goto' starts incremental search.
  1244. When nil, you can use these keybindings to navigate the buffer:
  1245. q Quit the org-goto interface
  1246. n Go to the next visible heading
  1247. p Go to the previous visible heading
  1248. f Go one heading forward on same level
  1249. b Go one heading backward on same level
  1250. u Go one heading up"
  1251. :group 'org-edit-structure
  1252. :type 'boolean)
  1253. (defgroup org-sparse-trees nil
  1254. "Options concerning sparse trees in Org-mode."
  1255. :tag "Org Sparse Trees"
  1256. :group 'org-structure)
  1257. (defcustom org-highlight-sparse-tree-matches t
  1258. "Non-nil means highlight all matches that define a sparse tree.
  1259. The highlights will automatically disappear the next time the buffer is
  1260. changed by an edit command."
  1261. :group 'org-sparse-trees
  1262. :type 'boolean)
  1263. (defcustom org-remove-highlights-with-change t
  1264. "Non-nil means any change to the buffer will remove temporary highlights.
  1265. Such highlights are created by `org-occur' and `org-clock-display'.
  1266. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1267. The highlights created by `org-preview-latex-fragment' always need
  1268. `C-c C-c' to be removed."
  1269. :group 'org-sparse-trees
  1270. :group 'org-time
  1271. :type 'boolean)
  1272. (defcustom org-occur-hook '(org-first-headline-recenter)
  1273. "Hook that is run after `org-occur' has constructed a sparse tree.
  1274. This can be used to recenter the window to show as much of the structure
  1275. as possible."
  1276. :group 'org-sparse-trees
  1277. :type 'hook)
  1278. (defgroup org-imenu-and-speedbar nil
  1279. "Options concerning imenu and speedbar in Org-mode."
  1280. :tag "Org Imenu and Speedbar"
  1281. :group 'org-structure)
  1282. (defcustom org-imenu-depth 2
  1283. "The maximum level for Imenu access to Org-mode headlines.
  1284. This also applied for speedbar access."
  1285. :group 'org-imenu-and-speedbar
  1286. :type 'integer)
  1287. (defgroup org-table nil
  1288. "Options concerning tables in Org-mode."
  1289. :tag "Org Table"
  1290. :group 'org)
  1291. (defcustom org-enable-table-editor 'optimized
  1292. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1293. When nil, such lines will be treated like ordinary lines.
  1294. When equal to the symbol `optimized', the table editor will be optimized to
  1295. do the following:
  1296. - Automatic overwrite mode in front of whitespace in table fields.
  1297. This makes the structure of the table stay in tact as long as the edited
  1298. field does not exceed the column width.
  1299. - Minimize the number of realigns. Normally, the table is aligned each time
  1300. TAB or RET are pressed to move to another field. With optimization this
  1301. happens only if changes to a field might have changed the column width.
  1302. Optimization requires replacing the functions `self-insert-command',
  1303. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1304. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1305. very good at guessing when a re-align will be necessary, but you can always
  1306. force one with \\[org-ctrl-c-ctrl-c].
  1307. If you would like to use the optimized version in Org-mode, but the
  1308. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1309. This variable can be used to turn on and off the table editor during a session,
  1310. but in order to toggle optimization, a restart is required.
  1311. See also the variable `org-table-auto-blank-field'."
  1312. :group 'org-table
  1313. :type '(choice
  1314. (const :tag "off" nil)
  1315. (const :tag "on" t)
  1316. (const :tag "on, optimized" optimized)))
  1317. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1318. (version<= emacs-version "24.1"))
  1319. "Non-nil means cluster self-insert commands for undo when possible.
  1320. If this is set, then, like in the Emacs command loop, 20 consecutive
  1321. characters will be undone together.
  1322. This is configurable, because there is some impact on typing performance."
  1323. :group 'org-table
  1324. :type 'boolean)
  1325. (defcustom org-table-tab-recognizes-table.el t
  1326. "Non-nil means TAB will automatically notice a table.el table.
  1327. When it sees such a table, it moves point into it and - if necessary -
  1328. calls `table-recognize-table'."
  1329. :group 'org-table-editing
  1330. :type 'boolean)
  1331. (defgroup org-link nil
  1332. "Options concerning links in Org-mode."
  1333. :tag "Org Link"
  1334. :group 'org)
  1335. (defvar org-link-abbrev-alist-local nil
  1336. "Buffer-local version of `org-link-abbrev-alist', which see.
  1337. The value of this is taken from the #+LINK lines.")
  1338. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1339. (defcustom org-link-abbrev-alist nil
  1340. "Alist of link abbreviations.
  1341. The car of each element is a string, to be replaced at the start of a link.
  1342. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1343. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1344. [[linkkey:tag][description]]
  1345. The 'linkkey' must be a word word, starting with a letter, followed
  1346. by letters, numbers, '-' or '_'.
  1347. If REPLACE is a string, the tag will simply be appended to create the link.
  1348. If the string contains \"%s\", the tag will be inserted there. If the string
  1349. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1350. at that point (see the function `url-hexify-string'). If the string contains
  1351. the specifier \"%(my-function)\", then the custom function `my-function' will
  1352. be invoked: this function takes the tag as its only argument and must return
  1353. a string.
  1354. REPLACE may also be a function that will be called with the tag as the
  1355. only argument to create the link, which should be returned as a string.
  1356. See the manual for examples."
  1357. :group 'org-link
  1358. :type '(repeat
  1359. (cons
  1360. (string :tag "Protocol")
  1361. (choice
  1362. (string :tag "Format")
  1363. (function)))))
  1364. (defcustom org-descriptive-links t
  1365. "Non-nil means Org will display descriptive links.
  1366. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1367. \"Org Website\", hiding the link itself and just displaying its
  1368. description. When set to `nil', Org will display the full links
  1369. literally.
  1370. You can interactively set the value of this variable by calling
  1371. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1372. :group 'org-link
  1373. :type 'boolean)
  1374. (defcustom org-link-file-path-type 'adaptive
  1375. "How the path name in file links should be stored.
  1376. Valid values are:
  1377. relative Relative to the current directory, i.e. the directory of the file
  1378. into which the link is being inserted.
  1379. absolute Absolute path, if possible with ~ for home directory.
  1380. noabbrev Absolute path, no abbreviation of home directory.
  1381. adaptive Use relative path for files in the current directory and sub-
  1382. directories of it. For other files, use an absolute path."
  1383. :group 'org-link
  1384. :type '(choice
  1385. (const relative)
  1386. (const absolute)
  1387. (const noabbrev)
  1388. (const adaptive)))
  1389. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1390. "Types of links that should be activated in Org-mode files.
  1391. This is a list of symbols, each leading to the activation of a certain link
  1392. type. In principle, it does not hurt to turn on most link types - there may
  1393. be a small gain when turning off unused link types. The types are:
  1394. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1395. angle Links in angular brackets that may contain whitespace like
  1396. <bbdb:Carsten Dominik>.
  1397. plain Plain links in normal text, no whitespace, like http://google.com.
  1398. radio Text that is matched by a radio target, see manual for details.
  1399. tag Tag settings in a headline (link to tag search).
  1400. date Time stamps (link to calendar).
  1401. footnote Footnote labels.
  1402. Changing this variable requires a restart of Emacs to become effective."
  1403. :group 'org-link
  1404. :type '(set :greedy t
  1405. (const :tag "Double bracket links" bracket)
  1406. (const :tag "Angular bracket links" angle)
  1407. (const :tag "Plain text links" plain)
  1408. (const :tag "Radio target matches" radio)
  1409. (const :tag "Tags" tag)
  1410. (const :tag "Timestamps" date)
  1411. (const :tag "Footnotes" footnote)))
  1412. (defcustom org-make-link-description-function nil
  1413. "Function to use for generating link descriptions from links.
  1414. When nil, the link location will be used. This function must take
  1415. two parameters: the first one is the link, the second one is the
  1416. description generated by `org-insert-link'. The function should
  1417. return the description to use."
  1418. :group 'org-link
  1419. :type '(choice (const nil) (function)))
  1420. (defgroup org-link-store nil
  1421. "Options concerning storing links in Org-mode."
  1422. :tag "Org Store Link"
  1423. :group 'org-link)
  1424. (defcustom org-url-hexify-p t
  1425. "When non-nil, hexify URL when creating a link."
  1426. :type 'boolean
  1427. :version "24.3"
  1428. :group 'org-link-store)
  1429. (defcustom org-email-link-description-format "Email %c: %.30s"
  1430. "Format of the description part of a link to an email or usenet message.
  1431. The following %-escapes will be replaced by corresponding information:
  1432. %F full \"From\" field
  1433. %f name, taken from \"From\" field, address if no name
  1434. %T full \"To\" field
  1435. %t first name in \"To\" field, address if no name
  1436. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1437. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1438. %s subject
  1439. %d date
  1440. %m message-id.
  1441. You may use normal field width specification between the % and the letter.
  1442. This is for example useful to limit the length of the subject.
  1443. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1444. :group 'org-link-store
  1445. :type 'string)
  1446. (defcustom org-from-is-user-regexp
  1447. (let (r1 r2)
  1448. (when (and user-mail-address (not (string= user-mail-address "")))
  1449. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1450. (when (and user-full-name (not (string= user-full-name "")))
  1451. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1452. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1453. "Regexp matched against the \"From:\" header of an email or usenet message.
  1454. It should match if the message is from the user him/herself."
  1455. :group 'org-link-store
  1456. :type 'regexp)
  1457. (defcustom org-context-in-file-links t
  1458. "Non-nil means file links from `org-store-link' contain context.
  1459. A search string will be added to the file name with :: as separator and
  1460. used to find the context when the link is activated by the command
  1461. `org-open-at-point'. When this option is t, the entire active region
  1462. will be placed in the search string of the file link. If set to a
  1463. positive integer, only the first n lines of context will be stored.
  1464. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1465. negates this setting for the duration of the command."
  1466. :group 'org-link-store
  1467. :type '(choice boolean integer))
  1468. (defcustom org-keep-stored-link-after-insertion nil
  1469. "Non-nil means keep link in list for entire session.
  1470. The command `org-store-link' adds a link pointing to the current
  1471. location to an internal list. These links accumulate during a session.
  1472. The command `org-insert-link' can be used to insert links into any
  1473. Org-mode file (offering completion for all stored links). When this
  1474. option is nil, every link which has been inserted once using \\[org-insert-link]
  1475. will be removed from the list, to make completing the unused links
  1476. more efficient."
  1477. :group 'org-link-store
  1478. :type 'boolean)
  1479. (defgroup org-link-follow nil
  1480. "Options concerning following links in Org-mode."
  1481. :tag "Org Follow Link"
  1482. :group 'org-link)
  1483. (defcustom org-link-translation-function nil
  1484. "Function to translate links with different syntax to Org syntax.
  1485. This can be used to translate links created for example by the Planner
  1486. or emacs-wiki packages to Org syntax.
  1487. The function must accept two parameters, a TYPE containing the link
  1488. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1489. which is everything after the link protocol. It should return a cons
  1490. with possibly modified values of type and path.
  1491. Org contains a function for this, so if you set this variable to
  1492. `org-translate-link-from-planner', you should be able follow many
  1493. links created by planner."
  1494. :group 'org-link-follow
  1495. :type '(choice (const nil) (function)))
  1496. (defcustom org-follow-link-hook nil
  1497. "Hook that is run after a link has been followed."
  1498. :group 'org-link-follow
  1499. :type 'hook)
  1500. (defcustom org-tab-follows-link nil
  1501. "Non-nil means on links TAB will follow the link.
  1502. Needs to be set before org.el is loaded.
  1503. This really should not be used, it does not make sense, and the
  1504. implementation is bad."
  1505. :group 'org-link-follow
  1506. :type 'boolean)
  1507. (defcustom org-return-follows-link nil
  1508. "Non-nil means on links RET will follow the link.
  1509. In tables, the special behavior of RET has precedence."
  1510. :group 'org-link-follow
  1511. :type 'boolean)
  1512. (defcustom org-mouse-1-follows-link
  1513. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1514. "Non-nil means mouse-1 on a link will follow the link.
  1515. A longer mouse click will still set point. Does not work on XEmacs.
  1516. Needs to be set before org.el is loaded."
  1517. :group 'org-link-follow
  1518. :type 'boolean)
  1519. (defcustom org-mark-ring-length 4
  1520. "Number of different positions to be recorded in the ring.
  1521. Changing this requires a restart of Emacs to work correctly."
  1522. :group 'org-link-follow
  1523. :type 'integer)
  1524. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1525. "Non-nil means internal links in Org files must exactly match a headline.
  1526. When nil, the link search tries to match a phrase with all words
  1527. in the search text."
  1528. :group 'org-link-follow
  1529. :version "24.1"
  1530. :type '(choice
  1531. (const :tag "Use fuzzy text search" nil)
  1532. (const :tag "Match only exact headline" t)
  1533. (const :tag "Match exact headline or query to create it"
  1534. query-to-create)))
  1535. (defcustom org-link-frame-setup
  1536. '((vm . vm-visit-folder-other-frame)
  1537. (vm-imap . vm-visit-imap-folder-other-frame)
  1538. (gnus . org-gnus-no-new-news)
  1539. (file . find-file-other-window)
  1540. (wl . wl-other-frame))
  1541. "Setup the frame configuration for following links.
  1542. When following a link with Emacs, it may often be useful to display
  1543. this link in another window or frame. This variable can be used to
  1544. set this up for the different types of links.
  1545. For VM, use any of
  1546. `vm-visit-folder'
  1547. `vm-visit-folder-other-window'
  1548. `vm-visit-folder-other-frame'
  1549. For Gnus, use any of
  1550. `gnus'
  1551. `gnus-other-frame'
  1552. `org-gnus-no-new-news'
  1553. For FILE, use any of
  1554. `find-file'
  1555. `find-file-other-window'
  1556. `find-file-other-frame'
  1557. For Wanderlust use any of
  1558. `wl'
  1559. `wl-other-frame'
  1560. For the calendar, use the variable `calendar-setup'.
  1561. For BBDB, it is currently only possible to display the matches in
  1562. another window."
  1563. :group 'org-link-follow
  1564. :type '(list
  1565. (cons (const vm)
  1566. (choice
  1567. (const vm-visit-folder)
  1568. (const vm-visit-folder-other-window)
  1569. (const vm-visit-folder-other-frame)))
  1570. (cons (const vm-imap)
  1571. (choice
  1572. (const vm-visit-imap-folder)
  1573. (const vm-visit-imap-folder-other-window)
  1574. (const vm-visit-imap-folder-other-frame)))
  1575. (cons (const gnus)
  1576. (choice
  1577. (const gnus)
  1578. (const gnus-other-frame)
  1579. (const org-gnus-no-new-news)))
  1580. (cons (const file)
  1581. (choice
  1582. (const find-file)
  1583. (const find-file-other-window)
  1584. (const find-file-other-frame)))
  1585. (cons (const wl)
  1586. (choice
  1587. (const wl)
  1588. (const wl-other-frame)))))
  1589. (defcustom org-display-internal-link-with-indirect-buffer nil
  1590. "Non-nil means use indirect buffer to display infile links.
  1591. Activating internal links (from one location in a file to another location
  1592. in the same file) normally just jumps to the location. When the link is
  1593. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1594. is displayed in
  1595. another window. When this option is set, the other window actually displays
  1596. an indirect buffer clone of the current buffer, to avoid any visibility
  1597. changes to the current buffer."
  1598. :group 'org-link-follow
  1599. :type 'boolean)
  1600. (defcustom org-open-non-existing-files nil
  1601. "Non-nil means `org-open-file' will open non-existing files.
  1602. When nil, an error will be generated.
  1603. This variable applies only to external applications because they
  1604. might choke on non-existing files. If the link is to a file that
  1605. will be opened in Emacs, the variable is ignored."
  1606. :group 'org-link-follow
  1607. :type 'boolean)
  1608. (defcustom org-open-directory-means-index-dot-org nil
  1609. "Non-nil means a link to a directory really means to index.org.
  1610. When nil, following a directory link will run dired or open a finder/explorer
  1611. window on that directory."
  1612. :group 'org-link-follow
  1613. :type 'boolean)
  1614. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1615. "Function and arguments to call for following mailto links.
  1616. This is a list with the first element being a Lisp function, and the
  1617. remaining elements being arguments to the function. In string arguments,
  1618. %a will be replaced by the address, and %s will be replaced by the subject
  1619. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1620. :group 'org-link-follow
  1621. :type '(choice
  1622. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1623. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1624. (const :tag "message-mail" (message-mail "%a" "%s"))
  1625. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1626. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1627. "Non-nil means ask for confirmation before executing shell links.
  1628. Shell links can be dangerous: just think about a link
  1629. [[shell:rm -rf ~/*][Google Search]]
  1630. This link would show up in your Org-mode document as \"Google Search\",
  1631. but really it would remove your entire home directory.
  1632. Therefore we advise against setting this variable to nil.
  1633. Just change it to `y-or-n-p' if you want to confirm with a
  1634. single keystroke rather than having to type \"yes\"."
  1635. :group 'org-link-follow
  1636. :type '(choice
  1637. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1638. (const :tag "with y-or-n (faster)" y-or-n-p)
  1639. (const :tag "no confirmation (dangerous)" nil)))
  1640. (put 'org-confirm-shell-link-function
  1641. 'safe-local-variable
  1642. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1643. (defcustom org-confirm-shell-link-not-regexp ""
  1644. "A regexp to skip confirmation for shell links."
  1645. :group 'org-link-follow
  1646. :version "24.1"
  1647. :type 'regexp)
  1648. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1649. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1650. Elisp links can be dangerous: just think about a link
  1651. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1652. This link would show up in your Org-mode document as \"Google Search\",
  1653. but really it would remove your entire home directory.
  1654. Therefore we advise against setting this variable to nil.
  1655. Just change it to `y-or-n-p' if you want to confirm with a
  1656. single keystroke rather than having to type \"yes\"."
  1657. :group 'org-link-follow
  1658. :type '(choice
  1659. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1660. (const :tag "with y-or-n (faster)" y-or-n-p)
  1661. (const :tag "no confirmation (dangerous)" nil)))
  1662. (put 'org-confirm-shell-link-function
  1663. 'safe-local-variable
  1664. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1665. (defcustom org-confirm-elisp-link-not-regexp ""
  1666. "A regexp to skip confirmation for Elisp links."
  1667. :group 'org-link-follow
  1668. :version "24.1"
  1669. :type 'regexp)
  1670. (defconst org-file-apps-defaults-gnu
  1671. '((remote . emacs)
  1672. (system . mailcap)
  1673. (t . mailcap))
  1674. "Default file applications on a UNIX or GNU/Linux system.
  1675. See `org-file-apps'.")
  1676. (defconst org-file-apps-defaults-macosx
  1677. '((remote . emacs)
  1678. (t . "open %s")
  1679. (system . "open %s")
  1680. ("ps.gz" . "gv %s")
  1681. ("eps.gz" . "gv %s")
  1682. ("dvi" . "xdvi %s")
  1683. ("fig" . "xfig %s"))
  1684. "Default file applications on a MacOS X system.
  1685. The system \"open\" is known as a default, but we use X11 applications
  1686. for some files for which the OS does not have a good default.
  1687. See `org-file-apps'.")
  1688. (defconst org-file-apps-defaults-windowsnt
  1689. (list
  1690. '(remote . emacs)
  1691. (cons t
  1692. (list (if (featurep 'xemacs)
  1693. 'mswindows-shell-execute
  1694. 'w32-shell-execute)
  1695. "open" 'file))
  1696. (cons 'system
  1697. (list (if (featurep 'xemacs)
  1698. 'mswindows-shell-execute
  1699. 'w32-shell-execute)
  1700. "open" 'file)))
  1701. "Default file applications on a Windows NT system.
  1702. The system \"open\" is used for most files.
  1703. See `org-file-apps'.")
  1704. (defcustom org-file-apps
  1705. '((auto-mode . emacs)
  1706. ("\\.mm\\'" . default)
  1707. ("\\.x?html?\\'" . default)
  1708. ("\\.pdf\\'" . default))
  1709. "External applications for opening `file:path' items in a document.
  1710. Org-mode uses system defaults for different file types, but
  1711. you can use this variable to set the application for a given file
  1712. extension. The entries in this list are cons cells where the car identifies
  1713. files and the cdr the corresponding command. Possible values for the
  1714. file identifier are
  1715. \"string\" A string as a file identifier can be interpreted in different
  1716. ways, depending on its contents:
  1717. - Alphanumeric characters only:
  1718. Match links with this file extension.
  1719. Example: (\"pdf\" . \"evince %s\")
  1720. to open PDFs with evince.
  1721. - Regular expression: Match links where the
  1722. filename matches the regexp. If you want to
  1723. use groups here, use shy groups.
  1724. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1725. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1726. to open *.html and *.xhtml with firefox.
  1727. - Regular expression which contains (non-shy) groups:
  1728. Match links where the whole link, including \"::\", and
  1729. anything after that, matches the regexp.
  1730. In a custom command string, %1, %2, etc. are replaced with
  1731. the parts of the link that were matched by the groups.
  1732. For backwards compatibility, if a command string is given
  1733. that does not use any of the group matches, this case is
  1734. handled identically to the second one (i.e. match against
  1735. file name only).
  1736. In a custom lisp form, you can access the group matches with
  1737. (match-string n link).
  1738. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1739. to open [[file:document.pdf::5]] with evince at page 5.
  1740. `directory' Matches a directory
  1741. `remote' Matches a remote file, accessible through tramp or efs.
  1742. Remote files most likely should be visited through Emacs
  1743. because external applications cannot handle such paths.
  1744. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1745. so all files Emacs knows how to handle. Using this with
  1746. command `emacs' will open most files in Emacs. Beware that this
  1747. will also open html files inside Emacs, unless you add
  1748. (\"html\" . default) to the list as well.
  1749. t Default for files not matched by any of the other options.
  1750. `system' The system command to open files, like `open' on Windows
  1751. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1752. that will be selected if you call `C-c C-o' with a double
  1753. \\[universal-argument] \\[universal-argument] prefix.
  1754. Possible values for the command are:
  1755. `emacs' The file will be visited by the current Emacs process.
  1756. `default' Use the default application for this file type, which is the
  1757. association for t in the list, most likely in the system-specific
  1758. part.
  1759. This can be used to overrule an unwanted setting in the
  1760. system-specific variable.
  1761. `system' Use the system command for opening files, like \"open\".
  1762. This command is specified by the entry whose car is `system'.
  1763. Most likely, the system-specific version of this variable
  1764. does define this command, but you can overrule/replace it
  1765. here.
  1766. string A command to be executed by a shell; %s will be replaced
  1767. by the path to the file.
  1768. sexp A Lisp form which will be evaluated. The file path will
  1769. be available in the Lisp variable `file'.
  1770. For more examples, see the system specific constants
  1771. `org-file-apps-defaults-macosx'
  1772. `org-file-apps-defaults-windowsnt'
  1773. `org-file-apps-defaults-gnu'."
  1774. :group 'org-link-follow
  1775. :type '(repeat
  1776. (cons (choice :value ""
  1777. (string :tag "Extension")
  1778. (const :tag "System command to open files" system)
  1779. (const :tag "Default for unrecognized files" t)
  1780. (const :tag "Remote file" remote)
  1781. (const :tag "Links to a directory" directory)
  1782. (const :tag "Any files that have Emacs modes"
  1783. auto-mode))
  1784. (choice :value ""
  1785. (const :tag "Visit with Emacs" emacs)
  1786. (const :tag "Use default" default)
  1787. (const :tag "Use the system command" system)
  1788. (string :tag "Command")
  1789. (sexp :tag "Lisp form")))))
  1790. (defcustom org-doi-server-url "http://dx.doi.org/"
  1791. "The URL of the DOI server."
  1792. :type 'string
  1793. :version "24.3"
  1794. :group 'org-link-follow)
  1795. (defgroup org-refile nil
  1796. "Options concerning refiling entries in Org-mode."
  1797. :tag "Org Refile"
  1798. :group 'org)
  1799. (defcustom org-directory "~/org"
  1800. "Directory with org files.
  1801. This is just a default location to look for Org files. There is no need
  1802. at all to put your files into this directory. It is only used in the
  1803. following situations:
  1804. 1. When a capture template specifies a target file that is not an
  1805. absolute path. The path will then be interpreted relative to
  1806. `org-directory'
  1807. 2. When a capture note is filed away in an interactive way (when exiting the
  1808. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1809. with `org-directory' as the default path."
  1810. :group 'org-refile
  1811. :group 'org-remember
  1812. :group 'org-capture
  1813. :type 'directory)
  1814. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1815. "Default target for storing notes.
  1816. Used as a fall back file for org-remember.el and org-capture.el, for
  1817. templates that do not specify a target file."
  1818. :group 'org-refile
  1819. :group 'org-remember
  1820. :group 'org-capture
  1821. :type '(choice
  1822. (const :tag "Default from remember-data-file" nil)
  1823. file))
  1824. (defcustom org-goto-interface 'outline
  1825. "The default interface to be used for `org-goto'.
  1826. Allowed values are:
  1827. outline The interface shows an outline of the relevant file
  1828. and the correct heading is found by moving through
  1829. the outline or by searching with incremental search.
  1830. outline-path-completion Headlines in the current buffer are offered via
  1831. completion. This is the interface also used by
  1832. the refile command."
  1833. :group 'org-refile
  1834. :type '(choice
  1835. (const :tag "Outline" outline)
  1836. (const :tag "Outline-path-completion" outline-path-completion)))
  1837. (defcustom org-goto-max-level 5
  1838. "Maximum target level when running `org-goto' with refile interface."
  1839. :group 'org-refile
  1840. :type 'integer)
  1841. (defcustom org-reverse-note-order nil
  1842. "Non-nil means store new notes at the beginning of a file or entry.
  1843. When nil, new notes will be filed to the end of a file or entry.
  1844. This can also be a list with cons cells of regular expressions that
  1845. are matched against file names, and values."
  1846. :group 'org-remember
  1847. :group 'org-capture
  1848. :group 'org-refile
  1849. :type '(choice
  1850. (const :tag "Reverse always" t)
  1851. (const :tag "Reverse never" nil)
  1852. (repeat :tag "By file name regexp"
  1853. (cons regexp boolean))))
  1854. (defcustom org-log-refile nil
  1855. "Information to record when a task is refiled.
  1856. Possible values are:
  1857. nil Don't add anything
  1858. time Add a time stamp to the task
  1859. note Prompt for a note and add it with template `org-log-note-headings'
  1860. This option can also be set with on a per-file-basis with
  1861. #+STARTUP: nologrefile
  1862. #+STARTUP: logrefile
  1863. #+STARTUP: lognoterefile
  1864. You can have local logging settings for a subtree by setting the LOGGING
  1865. property to one or more of these keywords.
  1866. When bulk-refiling from the agenda, the value `note' is forbidden and
  1867. will temporarily be changed to `time'."
  1868. :group 'org-refile
  1869. :group 'org-progress
  1870. :version "24.1"
  1871. :type '(choice
  1872. (const :tag "No logging" nil)
  1873. (const :tag "Record timestamp" time)
  1874. (const :tag "Record timestamp with note." note)))
  1875. (defcustom org-refile-targets nil
  1876. "Targets for refiling entries with \\[org-refile].
  1877. This is a list of cons cells. Each cell contains:
  1878. - a specification of the files to be considered, either a list of files,
  1879. or a symbol whose function or variable value will be used to retrieve
  1880. a file name or a list of file names. If you use `org-agenda-files' for
  1881. that, all agenda files will be scanned for targets. Nil means consider
  1882. headings in the current buffer.
  1883. - A specification of how to find candidate refile targets. This may be
  1884. any of:
  1885. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1886. This tag has to be present in all target headlines, inheritance will
  1887. not be considered.
  1888. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1889. todo keyword.
  1890. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1891. headlines that are refiling targets.
  1892. - a cons cell (:level . N). Any headline of level N is considered a target.
  1893. Note that, when `org-odd-levels-only' is set, level corresponds to
  1894. order in hierarchy, not to the number of stars.
  1895. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1896. Note that, when `org-odd-levels-only' is set, level corresponds to
  1897. order in hierarchy, not to the number of stars.
  1898. Each element of this list generates a set of possible targets.
  1899. The union of these sets is presented (with completion) to
  1900. the user by `org-refile'.
  1901. You can set the variable `org-refile-target-verify-function' to a function
  1902. to verify each headline found by the simple criteria above.
  1903. When this variable is nil, all top-level headlines in the current buffer
  1904. are used, equivalent to the value `((nil . (:level . 1))'."
  1905. :group 'org-refile
  1906. :type '(repeat
  1907. (cons
  1908. (choice :value org-agenda-files
  1909. (const :tag "All agenda files" org-agenda-files)
  1910. (const :tag "Current buffer" nil)
  1911. (function) (variable) (file))
  1912. (choice :tag "Identify target headline by"
  1913. (cons :tag "Specific tag" (const :value :tag) (string))
  1914. (cons :tag "TODO keyword" (const :value :todo) (string))
  1915. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1916. (cons :tag "Level number" (const :value :level) (integer))
  1917. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1918. (defcustom org-refile-target-verify-function nil
  1919. "Function to verify if the headline at point should be a refile target.
  1920. The function will be called without arguments, with point at the
  1921. beginning of the headline. It should return t and leave point
  1922. where it is if the headline is a valid target for refiling.
  1923. If the target should not be selected, the function must return nil.
  1924. In addition to this, it may move point to a place from where the search
  1925. should be continued. For example, the function may decide that the entire
  1926. subtree of the current entry should be excluded and move point to the end
  1927. of the subtree."
  1928. :group 'org-refile
  1929. :type '(choice
  1930. (const nil)
  1931. (function)))
  1932. (defcustom org-refile-use-cache nil
  1933. "Non-nil means cache refile targets to speed up the process.
  1934. The cache for a particular file will be updated automatically when
  1935. the buffer has been killed, or when any of the marker used for flagging
  1936. refile targets no longer points at a live buffer.
  1937. If you have added new entries to a buffer that might themselves be targets,
  1938. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1939. find that easier, `C-u C-u C-u C-c C-w'."
  1940. :group 'org-refile
  1941. :version "24.1"
  1942. :type 'boolean)
  1943. (defcustom org-refile-use-outline-path nil
  1944. "Non-nil means provide refile targets as paths.
  1945. So a level 3 headline will be available as level1/level2/level3.
  1946. When the value is `file', also include the file name (without directory)
  1947. into the path. In this case, you can also stop the completion after
  1948. the file name, to get entries inserted as top level in the file.
  1949. When `full-file-path', include the full file path."
  1950. :group 'org-refile
  1951. :type '(choice
  1952. (const :tag "Not" nil)
  1953. (const :tag "Yes" t)
  1954. (const :tag "Start with file name" file)
  1955. (const :tag "Start with full file path" full-file-path)))
  1956. (defcustom org-outline-path-complete-in-steps t
  1957. "Non-nil means complete the outline path in hierarchical steps.
  1958. When Org-mode uses the refile interface to select an outline path
  1959. \(see variable `org-refile-use-outline-path'), the completion of
  1960. the path can be done is a single go, or if can be done in steps down
  1961. the headline hierarchy. Going in steps is probably the best if you
  1962. do not use a special completion package like `ido' or `icicles'.
  1963. However, when using these packages, going in one step can be very
  1964. fast, while still showing the whole path to the entry."
  1965. :group 'org-refile
  1966. :type 'boolean)
  1967. (defcustom org-refile-allow-creating-parent-nodes nil
  1968. "Non-nil means allow to create new nodes as refile targets.
  1969. New nodes are then created by adding \"/new node name\" to the completion
  1970. of an existing node. When the value of this variable is `confirm',
  1971. new node creation must be confirmed by the user (recommended)
  1972. When nil, the completion must match an existing entry.
  1973. Note that, if the new heading is not seen by the criteria
  1974. listed in `org-refile-targets', multiple instances of the same
  1975. heading would be created by trying again to file under the new
  1976. heading."
  1977. :group 'org-refile
  1978. :type '(choice
  1979. (const :tag "Never" nil)
  1980. (const :tag "Always" t)
  1981. (const :tag "Prompt for confirmation" confirm)))
  1982. (defcustom org-refile-active-region-within-subtree nil
  1983. "Non-nil means also refile active region within a subtree.
  1984. By default `org-refile' doesn't allow refiling regions if they
  1985. don't contain a set of subtrees, but it might be convenient to
  1986. do so sometimes: in that case, the first line of the region is
  1987. converted to a headline before refiling."
  1988. :group 'org-refile
  1989. :version "24.1"
  1990. :type 'boolean)
  1991. (defgroup org-todo nil
  1992. "Options concerning TODO items in Org-mode."
  1993. :tag "Org TODO"
  1994. :group 'org)
  1995. (defgroup org-progress nil
  1996. "Options concerning Progress logging in Org-mode."
  1997. :tag "Org Progress"
  1998. :group 'org-time)
  1999. (defvar org-todo-interpretation-widgets
  2000. '((:tag "Sequence (cycling hits every state)" sequence)
  2001. (:tag "Type (cycling directly to DONE)" type))
  2002. "The available interpretation symbols for customizing `org-todo-keywords'.
  2003. Interested libraries should add to this list.")
  2004. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2005. "List of TODO entry keyword sequences and their interpretation.
  2006. \\<org-mode-map>This is a list of sequences.
  2007. Each sequence starts with a symbol, either `sequence' or `type',
  2008. indicating if the keywords should be interpreted as a sequence of
  2009. action steps, or as different types of TODO items. The first
  2010. keywords are states requiring action - these states will select a headline
  2011. for inclusion into the global TODO list Org-mode produces. If one of
  2012. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2013. signify that no further action is necessary. If \"|\" is not found,
  2014. the last keyword is treated as the only DONE state of the sequence.
  2015. The command \\[org-todo] cycles an entry through these states, and one
  2016. additional state where no keyword is present. For details about this
  2017. cycling, see the manual.
  2018. TODO keywords and interpretation can also be set on a per-file basis with
  2019. the special #+SEQ_TODO and #+TYP_TODO lines.
  2020. Each keyword can optionally specify a character for fast state selection
  2021. \(in combination with the variable `org-use-fast-todo-selection')
  2022. and specifiers for state change logging, using the same syntax that
  2023. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2024. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2025. indicates to record a time stamp each time this state is selected.
  2026. Each keyword may also specify if a timestamp or a note should be
  2027. recorded when entering or leaving the state, by adding additional
  2028. characters in the parenthesis after the keyword. This looks like this:
  2029. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2030. record only the time of the state change. With X and Y being either
  2031. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2032. Y when leaving the state if and only if the *target* state does not
  2033. define X. You may omit any of the fast-selection key or X or /Y,
  2034. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2035. For backward compatibility, this variable may also be just a list
  2036. of keywords. In this case the interpretation (sequence or type) will be
  2037. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2038. :group 'org-todo
  2039. :group 'org-keywords
  2040. :type '(choice
  2041. (repeat :tag "Old syntax, just keywords"
  2042. (string :tag "Keyword"))
  2043. (repeat :tag "New syntax"
  2044. (cons
  2045. (choice
  2046. :tag "Interpretation"
  2047. ;;Quick and dirty way to see
  2048. ;;`org-todo-interpretations'. This takes the
  2049. ;;place of item arguments
  2050. :convert-widget
  2051. (lambda (widget)
  2052. (widget-put widget
  2053. :args (mapcar
  2054. #'(lambda (x)
  2055. (widget-convert
  2056. (cons 'const x)))
  2057. org-todo-interpretation-widgets))
  2058. widget))
  2059. (repeat
  2060. (string :tag "Keyword"))))))
  2061. (defvar org-todo-keywords-1 nil
  2062. "All TODO and DONE keywords active in a buffer.")
  2063. (make-variable-buffer-local 'org-todo-keywords-1)
  2064. (defvar org-todo-keywords-for-agenda nil)
  2065. (defvar org-done-keywords-for-agenda nil)
  2066. (defvar org-todo-keyword-alist-for-agenda nil)
  2067. (defvar org-tag-alist-for-agenda nil
  2068. "Alist of all tags from all agenda files.")
  2069. (defvar org-tag-groups-alist-for-agenda nil
  2070. "Alist of all groups tags from all current agenda files.")
  2071. (defvar org-tag-groups-alist nil)
  2072. (make-variable-buffer-local 'org-tag-groups-alist)
  2073. (defvar org-agenda-contributing-files nil)
  2074. (defvar org-not-done-keywords nil)
  2075. (make-variable-buffer-local 'org-not-done-keywords)
  2076. (defvar org-done-keywords nil)
  2077. (make-variable-buffer-local 'org-done-keywords)
  2078. (defvar org-todo-heads nil)
  2079. (make-variable-buffer-local 'org-todo-heads)
  2080. (defvar org-todo-sets nil)
  2081. (make-variable-buffer-local 'org-todo-sets)
  2082. (defvar org-todo-log-states nil)
  2083. (make-variable-buffer-local 'org-todo-log-states)
  2084. (defvar org-todo-kwd-alist nil)
  2085. (make-variable-buffer-local 'org-todo-kwd-alist)
  2086. (defvar org-todo-key-alist nil)
  2087. (make-variable-buffer-local 'org-todo-key-alist)
  2088. (defvar org-todo-key-trigger nil)
  2089. (make-variable-buffer-local 'org-todo-key-trigger)
  2090. (defcustom org-todo-interpretation 'sequence
  2091. "Controls how TODO keywords are interpreted.
  2092. This variable is in principle obsolete and is only used for
  2093. backward compatibility, if the interpretation of todo keywords is
  2094. not given already in `org-todo-keywords'. See that variable for
  2095. more information."
  2096. :group 'org-todo
  2097. :group 'org-keywords
  2098. :type '(choice (const sequence)
  2099. (const type)))
  2100. (defcustom org-use-fast-todo-selection t
  2101. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2102. This variable describes if and under what circumstances the cycling
  2103. mechanism for TODO keywords will be replaced by a single-key, direct
  2104. selection scheme.
  2105. When nil, fast selection is never used.
  2106. When the symbol `prefix', it will be used when `org-todo' is called
  2107. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2108. `C-u t' in an agenda buffer.
  2109. When t, fast selection is used by default. In this case, the prefix
  2110. argument forces cycling instead.
  2111. In all cases, the special interface is only used if access keys have
  2112. actually been assigned by the user, i.e. if keywords in the configuration
  2113. are followed by a letter in parenthesis, like TODO(t)."
  2114. :group 'org-todo
  2115. :type '(choice
  2116. (const :tag "Never" nil)
  2117. (const :tag "By default" t)
  2118. (const :tag "Only with C-u C-c C-t" prefix)))
  2119. (defcustom org-provide-todo-statistics t
  2120. "Non-nil means update todo statistics after insert and toggle.
  2121. ALL-HEADLINES means update todo statistics by including headlines
  2122. with no TODO keyword as well, counting them as not done.
  2123. A list of TODO keywords means the same, but skip keywords that are
  2124. not in this list.
  2125. When this is set, todo statistics is updated in the parent of the
  2126. current entry each time a todo state is changed."
  2127. :group 'org-todo
  2128. :type '(choice
  2129. (const :tag "Yes, only for TODO entries" t)
  2130. (const :tag "Yes, including all entries" 'all-headlines)
  2131. (repeat :tag "Yes, for TODOs in this list"
  2132. (string :tag "TODO keyword"))
  2133. (other :tag "No TODO statistics" nil)))
  2134. (defcustom org-hierarchical-todo-statistics t
  2135. "Non-nil means TODO statistics covers just direct children.
  2136. When nil, all entries in the subtree are considered.
  2137. This has only an effect if `org-provide-todo-statistics' is set.
  2138. To set this to nil for only a single subtree, use a COOKIE_DATA
  2139. property and include the word \"recursive\" into the value."
  2140. :group 'org-todo
  2141. :type 'boolean)
  2142. (defcustom org-after-todo-state-change-hook nil
  2143. "Hook which is run after the state of a TODO item was changed.
  2144. The new state (a string with a TODO keyword, or nil) is available in the
  2145. Lisp variable `org-state'."
  2146. :group 'org-todo
  2147. :type 'hook)
  2148. (defvar org-blocker-hook nil
  2149. "Hook for functions that are allowed to block a state change.
  2150. Functions in this hook should not modify the buffer.
  2151. Each function gets as its single argument a property list,
  2152. see `org-trigger-hook' for more information about this list.
  2153. If any of the functions in this hook returns nil, the state change
  2154. is blocked.")
  2155. (defvar org-trigger-hook nil
  2156. "Hook for functions that are triggered by a state change.
  2157. Each function gets as its single argument a property list with at
  2158. least the following elements:
  2159. (:type type-of-change :position pos-at-entry-start
  2160. :from old-state :to new-state)
  2161. Depending on the type, more properties may be present.
  2162. This mechanism is currently implemented for:
  2163. TODO state changes
  2164. ------------------
  2165. :type todo-state-change
  2166. :from previous state (keyword as a string), or nil, or a symbol
  2167. 'todo' or 'done', to indicate the general type of state.
  2168. :to new state, like in :from")
  2169. (defcustom org-enforce-todo-dependencies nil
  2170. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2171. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2172. be blocked if any prior sibling is not yet done.
  2173. Finally, if the parent is blocked because of ordered siblings of its own,
  2174. the child will also be blocked."
  2175. :set (lambda (var val)
  2176. (set var val)
  2177. (if val
  2178. (add-hook 'org-blocker-hook
  2179. 'org-block-todo-from-children-or-siblings-or-parent)
  2180. (remove-hook 'org-blocker-hook
  2181. 'org-block-todo-from-children-or-siblings-or-parent)))
  2182. :group 'org-todo
  2183. :type 'boolean)
  2184. (defcustom org-enforce-todo-checkbox-dependencies nil
  2185. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2186. When this is nil, checkboxes have no influence on switching TODO states.
  2187. When non-nil, you first need to check off all check boxes before the TODO
  2188. entry can be switched to DONE.
  2189. This variable needs to be set before org.el is loaded, and you need to
  2190. restart Emacs after a change to make the change effective. The only way
  2191. to change is while Emacs is running is through the customize interface."
  2192. :set (lambda (var val)
  2193. (set var val)
  2194. (if val
  2195. (add-hook 'org-blocker-hook
  2196. 'org-block-todo-from-checkboxes)
  2197. (remove-hook 'org-blocker-hook
  2198. 'org-block-todo-from-checkboxes)))
  2199. :group 'org-todo
  2200. :type 'boolean)
  2201. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2202. "Non-nil means inserting a TODO heading is treated as state change.
  2203. So when the command \\[org-insert-todo-heading] is used, state change
  2204. logging will apply if appropriate. When nil, the new TODO item will
  2205. be inserted directly, and no logging will take place."
  2206. :group 'org-todo
  2207. :type 'boolean)
  2208. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2209. "Non-nil means switching TODO states with S-cursor counts as state change.
  2210. This is the default behavior. However, setting this to nil allows a
  2211. convenient way to select a TODO state and bypass any logging associated
  2212. with that."
  2213. :group 'org-todo
  2214. :type 'boolean)
  2215. (defcustom org-todo-state-tags-triggers nil
  2216. "Tag changes that should be triggered by TODO state changes.
  2217. This is a list. Each entry is
  2218. (state-change (tag . flag) .......)
  2219. State-change can be a string with a state, and empty string to indicate the
  2220. state that has no TODO keyword, or it can be one of the symbols `todo'
  2221. or `done', meaning any not-done or done state, respectively."
  2222. :group 'org-todo
  2223. :group 'org-tags
  2224. :type '(repeat
  2225. (cons (choice :tag "When changing to"
  2226. (const :tag "Not-done state" todo)
  2227. (const :tag "Done state" done)
  2228. (string :tag "State"))
  2229. (repeat
  2230. (cons :tag "Tag action"
  2231. (string :tag "Tag")
  2232. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2233. (defcustom org-log-done nil
  2234. "Information to record when a task moves to the DONE state.
  2235. Possible values are:
  2236. nil Don't add anything, just change the keyword
  2237. time Add a time stamp to the task
  2238. note Prompt for a note and add it with template `org-log-note-headings'
  2239. This option can also be set with on a per-file-basis with
  2240. #+STARTUP: nologdone
  2241. #+STARTUP: logdone
  2242. #+STARTUP: lognotedone
  2243. You can have local logging settings for a subtree by setting the LOGGING
  2244. property to one or more of these keywords."
  2245. :group 'org-todo
  2246. :group 'org-progress
  2247. :type '(choice
  2248. (const :tag "No logging" nil)
  2249. (const :tag "Record CLOSED timestamp" time)
  2250. (const :tag "Record CLOSED timestamp with note." note)))
  2251. ;; Normalize old uses of org-log-done.
  2252. (cond
  2253. ((eq org-log-done t) (setq org-log-done 'time))
  2254. ((and (listp org-log-done) (memq 'done org-log-done))
  2255. (setq org-log-done 'note)))
  2256. (defcustom org-log-reschedule nil
  2257. "Information to record when the scheduling date of a tasks is modified.
  2258. Possible values are:
  2259. nil Don't add anything, just change the date
  2260. time Add a time stamp to the task
  2261. note Prompt for a note and add it with template `org-log-note-headings'
  2262. This option can also be set with on a per-file-basis with
  2263. #+STARTUP: nologreschedule
  2264. #+STARTUP: logreschedule
  2265. #+STARTUP: lognotereschedule"
  2266. :group 'org-todo
  2267. :group 'org-progress
  2268. :type '(choice
  2269. (const :tag "No logging" nil)
  2270. (const :tag "Record timestamp" time)
  2271. (const :tag "Record timestamp with note." note)))
  2272. (defcustom org-log-redeadline nil
  2273. "Information to record when the deadline date of a tasks is modified.
  2274. Possible values are:
  2275. nil Don't add anything, just change the date
  2276. time Add a time stamp to the task
  2277. note Prompt for a note and add it with template `org-log-note-headings'
  2278. This option can also be set with on a per-file-basis with
  2279. #+STARTUP: nologredeadline
  2280. #+STARTUP: logredeadline
  2281. #+STARTUP: lognoteredeadline
  2282. You can have local logging settings for a subtree by setting the LOGGING
  2283. property to one or more of these keywords."
  2284. :group 'org-todo
  2285. :group 'org-progress
  2286. :type '(choice
  2287. (const :tag "No logging" nil)
  2288. (const :tag "Record timestamp" time)
  2289. (const :tag "Record timestamp with note." note)))
  2290. (defcustom org-log-note-clock-out nil
  2291. "Non-nil means record a note when clocking out of an item.
  2292. This can also be configured on a per-file basis by adding one of
  2293. the following lines anywhere in the buffer:
  2294. #+STARTUP: lognoteclock-out
  2295. #+STARTUP: nolognoteclock-out"
  2296. :group 'org-todo
  2297. :group 'org-progress
  2298. :type 'boolean)
  2299. (defcustom org-log-done-with-time t
  2300. "Non-nil means the CLOSED time stamp will contain date and time.
  2301. When nil, only the date will be recorded."
  2302. :group 'org-progress
  2303. :type 'boolean)
  2304. (defcustom org-log-note-headings
  2305. '((done . "CLOSING NOTE %t")
  2306. (state . "State %-12s from %-12S %t")
  2307. (note . "Note taken on %t")
  2308. (reschedule . "Rescheduled from %S on %t")
  2309. (delschedule . "Not scheduled, was %S on %t")
  2310. (redeadline . "New deadline from %S on %t")
  2311. (deldeadline . "Removed deadline, was %S on %t")
  2312. (refile . "Refiled on %t")
  2313. (clock-out . ""))
  2314. "Headings for notes added to entries.
  2315. The value is an alist, with the car being a symbol indicating the note
  2316. context, and the cdr is the heading to be used. The heading may also be the
  2317. empty string.
  2318. %t in the heading will be replaced by a time stamp.
  2319. %T will be an active time stamp instead the default inactive one
  2320. %d will be replaced by a short-format time stamp.
  2321. %D will be replaced by an active short-format time stamp.
  2322. %s will be replaced by the new TODO state, in double quotes.
  2323. %S will be replaced by the old TODO state, in double quotes.
  2324. %u will be replaced by the user name.
  2325. %U will be replaced by the full user name.
  2326. In fact, it is not a good idea to change the `state' entry, because
  2327. agenda log mode depends on the format of these entries."
  2328. :group 'org-todo
  2329. :group 'org-progress
  2330. :type '(list :greedy t
  2331. (cons (const :tag "Heading when closing an item" done) string)
  2332. (cons (const :tag
  2333. "Heading when changing todo state (todo sequence only)"
  2334. state) string)
  2335. (cons (const :tag "Heading when just taking a note" note) string)
  2336. (cons (const :tag "Heading when clocking out" clock-out) string)
  2337. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2338. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2339. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2340. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2341. (cons (const :tag "Heading when refiling" refile) string)))
  2342. (unless (assq 'note org-log-note-headings)
  2343. (push '(note . "%t") org-log-note-headings))
  2344. (defcustom org-log-into-drawer nil
  2345. "Non-nil means insert state change notes and time stamps into a drawer.
  2346. When nil, state changes notes will be inserted after the headline and
  2347. any scheduling and clock lines, but not inside a drawer.
  2348. The value of this variable should be the name of the drawer to use.
  2349. LOGBOOK is proposed as the default drawer for this purpose, you can
  2350. also set this to a string to define the drawer of your choice.
  2351. A value of t is also allowed, representing \"LOGBOOK\".
  2352. A value of t or nil can also be set with on a per-file-basis with
  2353. #+STARTUP: logdrawer
  2354. #+STARTUP: nologdrawer
  2355. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2356. will be ignored.
  2357. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2358. a subtree."
  2359. :group 'org-todo
  2360. :group 'org-progress
  2361. :type '(choice
  2362. (const :tag "Not into a drawer" nil)
  2363. (const :tag "LOGBOOK" t)
  2364. (string :tag "Other")))
  2365. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2366. (defun org-log-into-drawer ()
  2367. "Return the value of `org-log-into-drawer', but let properties overrule.
  2368. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2369. used instead of the default value."
  2370. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2371. (cond
  2372. ((not p) org-log-into-drawer)
  2373. ((equal p "nil") nil)
  2374. ((equal p "t") "LOGBOOK")
  2375. (t p))))
  2376. (defcustom org-log-state-notes-insert-after-drawers nil
  2377. "Non-nil means insert state change notes after any drawers in entry.
  2378. Only the drawers that *immediately* follow the headline and the
  2379. deadline/scheduled line are skipped.
  2380. When nil, insert notes right after the heading and perhaps the line
  2381. with deadline/scheduling if present.
  2382. This variable will have no effect if `org-log-into-drawer' is
  2383. set."
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-log-states-order-reversed t
  2388. "Non-nil means the latest state note will be directly after heading.
  2389. When nil, the state change notes will be ordered according to time.
  2390. This option can also be set with on a per-file-basis with
  2391. #+STARTUP: logstatesreversed
  2392. #+STARTUP: nologstatesreversed"
  2393. :group 'org-todo
  2394. :group 'org-progress
  2395. :type 'boolean)
  2396. (defcustom org-todo-repeat-to-state nil
  2397. "The TODO state to which a repeater should return the repeating task.
  2398. By default this is the first task in a TODO sequence, or the previous state
  2399. in a TODO_TYP set. But you can specify another task here.
  2400. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2401. :group 'org-todo
  2402. :version "24.1"
  2403. :type '(choice (const :tag "Head of sequence" nil)
  2404. (string :tag "Specific state")))
  2405. (defcustom org-log-repeat 'time
  2406. "Non-nil means record moving through the DONE state when triggering repeat.
  2407. An auto-repeating task is immediately switched back to TODO when
  2408. marked DONE. If you are not logging state changes (by adding \"@\"
  2409. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2410. record a closing note, there will be no record of the task moving
  2411. through DONE. This variable forces taking a note anyway.
  2412. nil Don't force a record
  2413. time Record a time stamp
  2414. note Prompt for a note and add it with template `org-log-note-headings'
  2415. This option can also be set with on a per-file-basis with
  2416. #+STARTUP: nologrepeat
  2417. #+STARTUP: logrepeat
  2418. #+STARTUP: lognoterepeat
  2419. You can have local logging settings for a subtree by setting the LOGGING
  2420. property to one or more of these keywords."
  2421. :group 'org-todo
  2422. :group 'org-progress
  2423. :type '(choice
  2424. (const :tag "Don't force a record" nil)
  2425. (const :tag "Force recording the DONE state" time)
  2426. (const :tag "Force recording a note with the DONE state" note)))
  2427. (defgroup org-priorities nil
  2428. "Priorities in Org-mode."
  2429. :tag "Org Priorities"
  2430. :group 'org-todo)
  2431. (defcustom org-enable-priority-commands t
  2432. "Non-nil means priority commands are active.
  2433. When nil, these commands will be disabled, so that you never accidentally
  2434. set a priority."
  2435. :group 'org-priorities
  2436. :type 'boolean)
  2437. (defcustom org-highest-priority ?A
  2438. "The highest priority of TODO items. A character like ?A, ?B etc.
  2439. Must have a smaller ASCII number than `org-lowest-priority'."
  2440. :group 'org-priorities
  2441. :type 'character)
  2442. (defcustom org-lowest-priority ?C
  2443. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2444. Must have a larger ASCII number than `org-highest-priority'."
  2445. :group 'org-priorities
  2446. :type 'character)
  2447. (defcustom org-default-priority ?B
  2448. "The default priority of TODO items.
  2449. This is the priority an item gets if no explicit priority is given.
  2450. When starting to cycle on an empty priority the first step in the cycle
  2451. depends on `org-priority-start-cycle-with-default'. The resulting first
  2452. step priority must not exceed the range from `org-highest-priority' to
  2453. `org-lowest-priority' which means that `org-default-priority' has to be
  2454. in this range exclusive or inclusive the range boundaries. Else the
  2455. first step refuses to set the default and the second will fall back
  2456. to (depending on the command used) the highest or lowest priority."
  2457. :group 'org-priorities
  2458. :type 'character)
  2459. (defcustom org-priority-start-cycle-with-default t
  2460. "Non-nil means start with default priority when starting to cycle.
  2461. When this is nil, the first step in the cycle will be (depending on the
  2462. command used) one higher or lower than the default priority.
  2463. See also `org-default-priority'."
  2464. :group 'org-priorities
  2465. :type 'boolean)
  2466. (defcustom org-get-priority-function nil
  2467. "Function to extract the priority from a string.
  2468. The string is normally the headline. If this is nil Org computes the
  2469. priority from the priority cookie like [#A] in the headline. It returns
  2470. an integer, increasing by 1000 for each priority level.
  2471. The user can set a different function here, which should take a string
  2472. as an argument and return the numeric priority."
  2473. :group 'org-priorities
  2474. :version "24.1"
  2475. :type '(choice
  2476. (const nil)
  2477. (function)))
  2478. (defgroup org-time nil
  2479. "Options concerning time stamps and deadlines in Org-mode."
  2480. :tag "Org Time"
  2481. :group 'org)
  2482. (defcustom org-insert-labeled-timestamps-at-point nil
  2483. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2484. When nil, these labeled time stamps are forces into the second line of an
  2485. entry, just after the headline. When scheduling from the global TODO list,
  2486. the time stamp will always be forced into the second line."
  2487. :group 'org-time
  2488. :type 'boolean)
  2489. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2490. "Formats for `format-time-string' which are used for time stamps.
  2491. It is not recommended to change this constant.")
  2492. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2493. "Number of minutes to round time stamps to.
  2494. These are two values, the first applies when first creating a time stamp.
  2495. The second applies when changing it with the commands `S-up' and `S-down'.
  2496. When changing the time stamp, this means that it will change in steps
  2497. of N minutes, as given by the second value.
  2498. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2499. numbers should be factors of 60, so for example 5, 10, 15.
  2500. When this is larger than 1, you can still force an exact time stamp by using
  2501. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2502. and by using a prefix arg to `S-up/down' to specify the exact number
  2503. of minutes to shift."
  2504. :group 'org-time
  2505. :get #'(lambda (var) ; Make sure both elements are there
  2506. (if (integerp (default-value var))
  2507. (list (default-value var) 5)
  2508. (default-value var)))
  2509. :type '(list
  2510. (integer :tag "when inserting times")
  2511. (integer :tag "when modifying times")))
  2512. ;; Normalize old customizations of this variable.
  2513. (when (integerp org-time-stamp-rounding-minutes)
  2514. (setq org-time-stamp-rounding-minutes
  2515. (list org-time-stamp-rounding-minutes
  2516. org-time-stamp-rounding-minutes)))
  2517. (defcustom org-display-custom-times nil
  2518. "Non-nil means overlay custom formats over all time stamps.
  2519. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2520. To turn this on on a per-file basis, insert anywhere in the file:
  2521. #+STARTUP: customtime"
  2522. :group 'org-time
  2523. :set 'set-default
  2524. :type 'sexp)
  2525. (make-variable-buffer-local 'org-display-custom-times)
  2526. (defcustom org-time-stamp-custom-formats
  2527. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2528. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2529. These are overlaid over the default ISO format if the variable
  2530. `org-display-custom-times' is set. Time like %H:%M should be at the
  2531. end of the second format. The custom formats are also honored by export
  2532. commands, if custom time display is turned on at the time of export."
  2533. :group 'org-time
  2534. :type 'sexp)
  2535. (defun org-time-stamp-format (&optional long inactive)
  2536. "Get the right format for a time string."
  2537. (let ((f (if long (cdr org-time-stamp-formats)
  2538. (car org-time-stamp-formats))))
  2539. (if inactive
  2540. (concat "[" (substring f 1 -1) "]")
  2541. f)))
  2542. (defcustom org-time-clocksum-format
  2543. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2544. "The format string used when creating CLOCKSUM lines.
  2545. This is also used when Org mode generates a time duration.
  2546. The value can be a single format string containing two
  2547. %-sequences, which will be filled with the number of hours and
  2548. minutes in that order.
  2549. Alternatively, the value can be a plist associating any of the
  2550. keys :years, :months, :weeks, :days, :hours or :minutes with
  2551. format strings. The time duration is formatted using only the
  2552. time components that are needed and concatenating the results.
  2553. If a time unit in absent, it falls back to the next smallest
  2554. unit.
  2555. The keys :require-years, :require-months, :require-days,
  2556. :require-weeks, :require-hours, :require-minutes are also
  2557. meaningful. A non-nil value for these keys indicates that the
  2558. corresponding time component should always be included, even if
  2559. its value is 0.
  2560. For example,
  2561. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2562. :require-minutes t)
  2563. means durations longer than a day will be expressed in days,
  2564. hours and minutes, and durations less than a day will always be
  2565. expressed in hours and minutes (even for durations less than an
  2566. hour).
  2567. The value
  2568. \(:days \"%dd\" :minutes \"%dm\")
  2569. means durations longer than a day will be expressed in days and
  2570. minutes, and durations less than a day will be expressed entirely
  2571. in minutes (even for durations longer than an hour)."
  2572. :group 'org-time
  2573. :group 'org-clock
  2574. :version "24.4"
  2575. :package-version '(Org . "8.0")
  2576. :type '(choice (string :tag "Format string")
  2577. (set :tag "Plist"
  2578. (group :inline t (const :tag "Years" :years)
  2579. (string :tag "Format string"))
  2580. (group :inline t
  2581. (const :tag "Always show years" :require-years)
  2582. (const t))
  2583. (group :inline t (const :tag "Months" :months)
  2584. (string :tag "Format string"))
  2585. (group :inline t
  2586. (const :tag "Always show months" :require-months)
  2587. (const t))
  2588. (group :inline t (const :tag "Weeks" :weeks)
  2589. (string :tag "Format string"))
  2590. (group :inline t
  2591. (const :tag "Always show weeks" :require-weeks)
  2592. (const t))
  2593. (group :inline t (const :tag "Days" :days)
  2594. (string :tag "Format string"))
  2595. (group :inline t
  2596. (const :tag "Always show days" :require-days)
  2597. (const t))
  2598. (group :inline t (const :tag "Hours" :hours)
  2599. (string :tag "Format string"))
  2600. (group :inline t
  2601. (const :tag "Always show hours" :require-hours)
  2602. (const t))
  2603. (group :inline t (const :tag "Minutes" :minutes)
  2604. (string :tag "Format string"))
  2605. (group :inline t
  2606. (const :tag "Always show minutes" :require-minutes)
  2607. (const t)))))
  2608. (defcustom org-time-clocksum-use-fractional nil
  2609. "When non-nil, \\[org-clock-display] uses fractional times.
  2610. See `org-time-clocksum-format' for more on time clock formats."
  2611. :group 'org-time
  2612. :group 'org-clock
  2613. :version "24.3"
  2614. :type 'boolean)
  2615. (defcustom org-time-clocksum-use-effort-durations nil
  2616. "When non-nil, \\[org-clock-display] uses effort durations.
  2617. E.g. by default, one day is considered to be a 8 hours effort,
  2618. so a task that has been clocked for 16 hours will be displayed
  2619. as during 2 days in the clock display or in the clocktable.
  2620. See `org-effort-durations' on how to set effort durations
  2621. and `org-time-clocksum-format' for more on time clock formats."
  2622. :group 'org-time
  2623. :group 'org-clock
  2624. :version "24.4"
  2625. :package-version '(Org . "8.0")
  2626. :type 'boolean)
  2627. (defcustom org-time-clocksum-fractional-format "%.2f"
  2628. "The format string used when creating CLOCKSUM lines,
  2629. or when Org mode generates a time duration, if
  2630. `org-time-clocksum-use-fractional' is enabled.
  2631. The value can be a single format string containing one
  2632. %-sequence, which will be filled with the number of hours as
  2633. a float.
  2634. Alternatively, the value can be a plist associating any of the
  2635. keys :years, :months, :weeks, :days, :hours or :minutes with
  2636. a format string. The time duration is formatted using the
  2637. largest time unit which gives a non-zero integer part. If all
  2638. specified formats have zero integer part, the smallest time unit
  2639. is used."
  2640. :group 'org-time
  2641. :type '(choice (string :tag "Format string")
  2642. (set (group :inline t (const :tag "Years" :years)
  2643. (string :tag "Format string"))
  2644. (group :inline t (const :tag "Months" :months)
  2645. (string :tag "Format string"))
  2646. (group :inline t (const :tag "Weeks" :weeks)
  2647. (string :tag "Format string"))
  2648. (group :inline t (const :tag "Days" :days)
  2649. (string :tag "Format string"))
  2650. (group :inline t (const :tag "Hours" :hours)
  2651. (string :tag "Format string"))
  2652. (group :inline t (const :tag "Minutes" :minutes)
  2653. (string :tag "Format string")))))
  2654. (defcustom org-deadline-warning-days 14
  2655. "Number of days before expiration during which a deadline becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. When 0 or negative, it means use this number (the absolute value of it)
  2658. even if a deadline has a different individual lead time specified.
  2659. Custom commands can set this variable in the options section."
  2660. :group 'org-time
  2661. :group 'org-agenda-daily/weekly
  2662. :type 'integer)
  2663. (defcustom org-scheduled-delay-days 0
  2664. "Number of days before a scheduled item becomes active.
  2665. This variable governs the display in sparse trees and in the agenda.
  2666. The default value (i.e. 0) means: don't delay scheduled item.
  2667. When negative, it means use this number (the absolute value of it)
  2668. even if a scheduled item has a different individual delay time
  2669. specified.
  2670. Custom commands can set this variable in the options section."
  2671. :group 'org-time
  2672. :group 'org-agenda-daily/weekly
  2673. :version "24.4"
  2674. :package-version '(Org . "8.0")
  2675. :type 'integer)
  2676. (defcustom org-read-date-prefer-future t
  2677. "Non-nil means assume future for incomplete date input from user.
  2678. This affects the following situations:
  2679. 1. The user gives a month but not a year.
  2680. For example, if it is April and you enter \"feb 2\", this will be read
  2681. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2682. 2. The user gives a day, but no month.
  2683. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2684. read this as the third of *next* month. However, if you enter \"17\",
  2685. it will be considered as *this* month.
  2686. If you set this variable to the symbol `time', then also the following
  2687. will work:
  2688. 3. If the user gives a time.
  2689. If the time is before now, it will be interpreted as tomorrow.
  2690. Currently none of this works for ISO week specifications.
  2691. When this option is nil, the current day, month and year will always be
  2692. used as defaults.
  2693. See also `org-agenda-jump-prefer-future'."
  2694. :group 'org-time
  2695. :type '(choice
  2696. (const :tag "Never" nil)
  2697. (const :tag "Check month and day" t)
  2698. (const :tag "Check month, day, and time" time)))
  2699. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2700. "Should the agenda jump command prefer the future for incomplete dates?
  2701. The default is to do the same as configured in `org-read-date-prefer-future'.
  2702. But you can also set a deviating value here.
  2703. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2704. :group 'org-agenda
  2705. :group 'org-time
  2706. :version "24.1"
  2707. :type '(choice
  2708. (const :tag "Use org-read-date-prefer-future"
  2709. org-read-date-prefer-future)
  2710. (const :tag "Never" nil)
  2711. (const :tag "Always" t)))
  2712. (defcustom org-read-date-force-compatible-dates t
  2713. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2714. Depending on the system Emacs is running on, certain dates cannot
  2715. be represented with the type used internally to represent time.
  2716. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2717. correctly. Some systems allow for earlier dates, some for later,
  2718. some for both. One way to find out it to insert any date into an
  2719. Org buffer, putting the cursor on the year and hitting S-up and
  2720. S-down to test the range.
  2721. When this variable is set to t, the date/time prompt will not let
  2722. you specify dates outside the 1970-2037 range, so it is certain that
  2723. these dates will work in whatever version of Emacs you are
  2724. running, and also that you can move a file from one Emacs implementation
  2725. to another. WHenever Org is forcing the year for you, it will display
  2726. a message and beep.
  2727. When this variable is nil, Org will check if the date is
  2728. representable in the specific Emacs implementation you are using.
  2729. If not, it will force a year, usually the current year, and beep
  2730. to remind you. Currently this setting is not recommended because
  2731. the likelihood that you will open your Org files in an Emacs that
  2732. has limited date range is not negligible.
  2733. A workaround for this problem is to use diary sexp dates for time
  2734. stamps outside of this range."
  2735. :group 'org-time
  2736. :version "24.1"
  2737. :type 'boolean)
  2738. (defcustom org-read-date-display-live t
  2739. "Non-nil means display current interpretation of date prompt live.
  2740. This display will be in an overlay, in the minibuffer."
  2741. :group 'org-time
  2742. :type 'boolean)
  2743. (defcustom org-read-date-popup-calendar t
  2744. "Non-nil means pop up a calendar when prompting for a date.
  2745. In the calendar, the date can be selected with mouse-1. However, the
  2746. minibuffer will also be active, and you can simply enter the date as well.
  2747. When nil, only the minibuffer will be available."
  2748. :group 'org-time
  2749. :type 'boolean)
  2750. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2751. 'org-read-date-popup-calendar)
  2752. (make-obsolete-variable
  2753. 'org-read-date-minibuffer-setup-hook
  2754. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2755. (defcustom org-read-date-minibuffer-setup-hook nil
  2756. "Hook to be used to set up keys for the date/time interface.
  2757. Add key definitions to `minibuffer-local-map', which will be a
  2758. temporary copy.
  2759. WARNING: This option is obsolete, you should use
  2760. `org-read-date-minibuffer-local-map' to set up keys."
  2761. :group 'org-time
  2762. :type 'hook)
  2763. (defcustom org-extend-today-until 0
  2764. "The hour when your day really ends. Must be an integer.
  2765. This has influence for the following applications:
  2766. - When switching the agenda to \"today\". It it is still earlier than
  2767. the time given here, the day recognized as TODAY is actually yesterday.
  2768. - When a date is read from the user and it is still before the time given
  2769. here, the current date and time will be assumed to be yesterday, 23:59.
  2770. Also, timestamps inserted in capture templates follow this rule.
  2771. IMPORTANT: This is a feature whose implementation is and likely will
  2772. remain incomplete. Really, it is only here because past midnight seems to
  2773. be the favorite working time of John Wiegley :-)"
  2774. :group 'org-time
  2775. :type 'integer)
  2776. (defcustom org-use-effective-time nil
  2777. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2778. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2779. \"effective time\" of any timestamps between midnight and 8am will be
  2780. 23:59 of the previous day."
  2781. :group 'org-time
  2782. :version "24.1"
  2783. :type 'boolean)
  2784. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2785. "When non-nil, use the last clock out time for `org-todo'.
  2786. Note that this option has precedence over the combined use of
  2787. `org-use-effective-time' and `org-extend-today-until'."
  2788. :group 'org-time
  2789. :version "24.4"
  2790. :package-version '(Org . "8.0")
  2791. :type 'boolean)
  2792. (defcustom org-edit-timestamp-down-means-later nil
  2793. "Non-nil means S-down will increase the time in a time stamp.
  2794. When nil, S-up will increase."
  2795. :group 'org-time
  2796. :type 'boolean)
  2797. (defcustom org-calendar-follow-timestamp-change t
  2798. "Non-nil means make the calendar window follow timestamp changes.
  2799. When a timestamp is modified and the calendar window is visible, it will be
  2800. moved to the new date."
  2801. :group 'org-time
  2802. :type 'boolean)
  2803. (defgroup org-tags nil
  2804. "Options concerning tags in Org-mode."
  2805. :tag "Org Tags"
  2806. :group 'org)
  2807. (defcustom org-tag-alist nil
  2808. "List of tags allowed in Org-mode files.
  2809. When this list is nil, Org-mode will base TAG input on what is already in the
  2810. buffer.
  2811. The value of this variable is an alist, the car of each entry must be a
  2812. keyword as a string, the cdr may be a character that is used to select
  2813. that tag through the fast-tag-selection interface.
  2814. See the manual for details."
  2815. :group 'org-tags
  2816. :type '(repeat
  2817. (choice
  2818. (cons (string :tag "Tag name")
  2819. (character :tag "Access char"))
  2820. (list :tag "Start radio group"
  2821. (const :startgroup)
  2822. (option (string :tag "Group description")))
  2823. (list :tag "Group tags delimiter"
  2824. (const :grouptags))
  2825. (list :tag "End radio group"
  2826. (const :endgroup)
  2827. (option (string :tag "Group description")))
  2828. (const :tag "New line" (:newline)))))
  2829. (defcustom org-tag-persistent-alist nil
  2830. "List of tags that will always appear in all Org-mode files.
  2831. This is in addition to any in buffer settings or customizations
  2832. of `org-tag-alist'.
  2833. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2834. The value of this variable is an alist, the car of each entry must be a
  2835. keyword as a string, the cdr may be a character that is used to select
  2836. that tag through the fast-tag-selection interface.
  2837. See the manual for details.
  2838. To disable these tags on a per-file basis, insert anywhere in the file:
  2839. #+STARTUP: noptag"
  2840. :group 'org-tags
  2841. :type '(repeat
  2842. (choice
  2843. (cons (string :tag "Tag name")
  2844. (character :tag "Access char"))
  2845. (const :tag "Start radio group" (:startgroup))
  2846. (const :tag "Group tags delimiter" (:grouptags))
  2847. (const :tag "End radio group" (:endgroup))
  2848. (const :tag "New line" (:newline)))))
  2849. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2850. "If non-nil, always offer completion for all tags of all agenda files.
  2851. Instead of customizing this variable directly, you might want to
  2852. set it locally for capture buffers, because there no list of
  2853. tags in that file can be created dynamically (there are none).
  2854. (add-hook 'org-capture-mode-hook
  2855. (lambda ()
  2856. (set (make-local-variable
  2857. 'org-complete-tags-always-offer-all-agenda-tags)
  2858. t)))"
  2859. :group 'org-tags
  2860. :version "24.1"
  2861. :type 'boolean)
  2862. (defvar org-file-tags nil
  2863. "List of tags that can be inherited by all entries in the file.
  2864. The tags will be inherited if the variable `org-use-tag-inheritance'
  2865. says they should be.
  2866. This variable is populated from #+FILETAGS lines.")
  2867. (defcustom org-use-fast-tag-selection 'auto
  2868. "Non-nil means use fast tag selection scheme.
  2869. This is a special interface to select and deselect tags with single keys.
  2870. When nil, fast selection is never used.
  2871. When the symbol `auto', fast selection is used if and only if selection
  2872. characters for tags have been configured, either through the variable
  2873. `org-tag-alist' or through a #+TAGS line in the buffer.
  2874. When t, fast selection is always used and selection keys are assigned
  2875. automatically if necessary."
  2876. :group 'org-tags
  2877. :type '(choice
  2878. (const :tag "Always" t)
  2879. (const :tag "Never" nil)
  2880. (const :tag "When selection characters are configured" auto)))
  2881. (defcustom org-fast-tag-selection-single-key nil
  2882. "Non-nil means fast tag selection exits after first change.
  2883. When nil, you have to press RET to exit it.
  2884. During fast tag selection, you can toggle this flag with `C-c'.
  2885. This variable can also have the value `expert'. In this case, the window
  2886. displaying the tags menu is not even shown, until you press C-c again."
  2887. :group 'org-tags
  2888. :type '(choice
  2889. (const :tag "No" nil)
  2890. (const :tag "Yes" t)
  2891. (const :tag "Expert" expert)))
  2892. (defvar org-fast-tag-selection-include-todo nil
  2893. "Non-nil means fast tags selection interface will also offer TODO states.
  2894. This is an undocumented feature, you should not rely on it.")
  2895. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2896. "The column to which tags should be indented in a headline.
  2897. If this number is positive, it specifies the column. If it is negative,
  2898. it means that the tags should be flushright to that column. For example,
  2899. -80 works well for a normal 80 character screen.
  2900. When 0, place tags directly after headline text, with only one space in
  2901. between."
  2902. :group 'org-tags
  2903. :type 'integer)
  2904. (defcustom org-auto-align-tags t
  2905. "Non-nil keeps tags aligned when modifying headlines.
  2906. Some operations (i.e. demoting) change the length of a headline and
  2907. therefore shift the tags around. With this option turned on, after
  2908. each such operation the tags are again aligned to `org-tags-column'."
  2909. :group 'org-tags
  2910. :type 'boolean)
  2911. (defcustom org-use-tag-inheritance t
  2912. "Non-nil means tags in levels apply also for sublevels.
  2913. When nil, only the tags directly given in a specific line apply there.
  2914. This may also be a list of tags that should be inherited, or a regexp that
  2915. matches tags that should be inherited. Additional control is possible
  2916. with the variable `org-tags-exclude-from-inheritance' which gives an
  2917. explicit list of tags to be excluded from inheritance, even if the value of
  2918. `org-use-tag-inheritance' would select it for inheritance.
  2919. If this option is t, a match early-on in a tree can lead to a large
  2920. number of matches in the subtree when constructing the agenda or creating
  2921. a sparse tree. If you only want to see the first match in a tree during
  2922. a search, check out the variable `org-tags-match-list-sublevels'."
  2923. :group 'org-tags
  2924. :type '(choice
  2925. (const :tag "Not" nil)
  2926. (const :tag "Always" t)
  2927. (repeat :tag "Specific tags" (string :tag "Tag"))
  2928. (regexp :tag "Tags matched by regexp")))
  2929. (defcustom org-tags-exclude-from-inheritance nil
  2930. "List of tags that should never be inherited.
  2931. This is a way to exclude a few tags from inheritance. For way to do
  2932. the opposite, to actively allow inheritance for selected tags,
  2933. see the variable `org-use-tag-inheritance'."
  2934. :group 'org-tags
  2935. :type '(repeat (string :tag "Tag")))
  2936. (defun org-tag-inherit-p (tag)
  2937. "Check if TAG is one that should be inherited."
  2938. (cond
  2939. ((member tag org-tags-exclude-from-inheritance) nil)
  2940. ((eq org-use-tag-inheritance t) t)
  2941. ((not org-use-tag-inheritance) nil)
  2942. ((stringp org-use-tag-inheritance)
  2943. (string-match org-use-tag-inheritance tag))
  2944. ((listp org-use-tag-inheritance)
  2945. (member tag org-use-tag-inheritance))
  2946. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2947. (defcustom org-tags-match-list-sublevels t
  2948. "Non-nil means list also sublevels of headlines matching a search.
  2949. This variable applies to tags/property searches, and also to stuck
  2950. projects because this search is based on a tags match as well.
  2951. When set to the symbol `indented', sublevels are indented with
  2952. leading dots.
  2953. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2954. the sublevels of a headline matching a tag search often also match
  2955. the same search. Listing all of them can create very long lists.
  2956. Setting this variable to nil causes subtrees of a match to be skipped.
  2957. This variable is semi-obsolete and probably should always be true. It
  2958. is better to limit inheritance to certain tags using the variables
  2959. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2960. :group 'org-tags
  2961. :type '(choice
  2962. (const :tag "No, don't list them" nil)
  2963. (const :tag "Yes, do list them" t)
  2964. (const :tag "List them, indented with leading dots" indented)))
  2965. (defcustom org-tags-sort-function nil
  2966. "When set, tags are sorted using this function as a comparator."
  2967. :group 'org-tags
  2968. :type '(choice
  2969. (const :tag "No sorting" nil)
  2970. (const :tag "Alphabetical" string<)
  2971. (const :tag "Reverse alphabetical" string>)
  2972. (function :tag "Custom function" nil)))
  2973. (defvar org-tags-history nil
  2974. "History of minibuffer reads for tags.")
  2975. (defvar org-last-tags-completion-table nil
  2976. "The last used completion table for tags.")
  2977. (defvar org-after-tags-change-hook nil
  2978. "Hook that is run after the tags in a line have changed.")
  2979. (defgroup org-properties nil
  2980. "Options concerning properties in Org-mode."
  2981. :tag "Org Properties"
  2982. :group 'org)
  2983. (defcustom org-property-format "%-10s %s"
  2984. "How property key/value pairs should be formatted by `indent-line'.
  2985. When `indent-line' hits a property definition, it will format the line
  2986. according to this format, mainly to make sure that the values are
  2987. lined-up with respect to each other."
  2988. :group 'org-properties
  2989. :type 'string)
  2990. (defcustom org-properties-postprocess-alist nil
  2991. "Alist of properties and functions to adjust inserted values.
  2992. Elements of this alist must be of the form
  2993. ([string] [function])
  2994. where [string] must be a property name and [function] must be a
  2995. lambda expression: this lambda expression must take one argument,
  2996. the value to adjust, and return the new value as a string.
  2997. For example, this element will allow the property \"Remaining\"
  2998. to be updated wrt the relation between the \"Effort\" property
  2999. and the clock summary:
  3000. ((\"Remaining\" (lambda(value)
  3001. (let ((clocksum (org-clock-sum-current-item))
  3002. (effort (org-duration-string-to-minutes
  3003. (org-entry-get (point) \"Effort\"))))
  3004. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3005. :group 'org-properties
  3006. :version "24.1"
  3007. :type '(alist :key-type (string :tag "Property")
  3008. :value-type (function :tag "Function")))
  3009. (defcustom org-use-property-inheritance nil
  3010. "Non-nil means properties apply also for sublevels.
  3011. This setting is chiefly used during property searches. Turning it on can
  3012. cause significant overhead when doing a search, which is why it is not
  3013. on by default.
  3014. When nil, only the properties directly given in the current entry count.
  3015. When t, every property is inherited. The value may also be a list of
  3016. properties that should have inheritance, or a regular expression matching
  3017. properties that should be inherited.
  3018. However, note that some special properties use inheritance under special
  3019. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3020. and the properties ending in \"_ALL\" when they are used as descriptor
  3021. for valid values of a property.
  3022. Note for programmers:
  3023. When querying an entry with `org-entry-get', you can control if inheritance
  3024. should be used. By default, `org-entry-get' looks only at the local
  3025. properties. You can request inheritance by setting the inherit argument
  3026. to t (to force inheritance) or to `selective' (to respect the setting
  3027. in this variable)."
  3028. :group 'org-properties
  3029. :type '(choice
  3030. (const :tag "Not" nil)
  3031. (const :tag "Always" t)
  3032. (repeat :tag "Specific properties" (string :tag "Property"))
  3033. (regexp :tag "Properties matched by regexp")))
  3034. (defun org-property-inherit-p (property)
  3035. "Check if PROPERTY is one that should be inherited."
  3036. (cond
  3037. ((eq org-use-property-inheritance t) t)
  3038. ((not org-use-property-inheritance) nil)
  3039. ((stringp org-use-property-inheritance)
  3040. (string-match org-use-property-inheritance property))
  3041. ((listp org-use-property-inheritance)
  3042. (member property org-use-property-inheritance))
  3043. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3044. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3045. "The default column format, if no other format has been defined.
  3046. This variable can be set on the per-file basis by inserting a line
  3047. #+COLUMNS: %25ITEM ....."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-ellipses ".."
  3051. "The ellipses to be used when a field in column view is truncated.
  3052. When this is the empty string, as many characters as possible are shown,
  3053. but then there will be no visual indication that the field has been truncated.
  3054. When this is a string of length N, the last N characters of a truncated
  3055. field are replaced by this string. If the column is narrower than the
  3056. ellipses string, only part of the ellipses string will be shown."
  3057. :group 'org-properties
  3058. :type 'string)
  3059. (defcustom org-columns-modify-value-for-display-function nil
  3060. "Function that modifies values for display in column view.
  3061. For example, it can be used to cut out a certain part from a time stamp.
  3062. The function must take 2 arguments:
  3063. column-title The title of the column (*not* the property name)
  3064. value The value that should be modified.
  3065. The function should return the value that should be displayed,
  3066. or nil if the normal value should be used."
  3067. :group 'org-properties
  3068. :type '(choice (const nil) (function)))
  3069. (defcustom org-effort-property "Effort"
  3070. "The property that is being used to keep track of effort estimates.
  3071. Effort estimates given in this property need to have the format H:MM."
  3072. :group 'org-properties
  3073. :group 'org-progress
  3074. :type '(string :tag "Property"))
  3075. (defconst org-global-properties-fixed
  3076. '(("VISIBILITY_ALL" . "folded children content all")
  3077. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3078. "List of property/value pairs that can be inherited by any entry.
  3079. These are fixed values, for the preset properties. The user variable
  3080. that can be used to add to this list is `org-global-properties'.
  3081. The entries in this list are cons cells where the car is a property
  3082. name and cdr is a string with the value. If the value represents
  3083. multiple items like an \"_ALL\" property, separate the items by
  3084. spaces.")
  3085. (defcustom org-global-properties nil
  3086. "List of property/value pairs that can be inherited by any entry.
  3087. This list will be combined with the constant `org-global-properties-fixed'.
  3088. The entries in this list are cons cells where the car is a property
  3089. name and cdr is a string with the value.
  3090. You can set buffer-local values for the same purpose in the variable
  3091. `org-file-properties' this by adding lines like
  3092. #+PROPERTY: NAME VALUE"
  3093. :group 'org-properties
  3094. :type '(repeat
  3095. (cons (string :tag "Property")
  3096. (string :tag "Value"))))
  3097. (defvar org-file-properties nil
  3098. "List of property/value pairs that can be inherited by any entry.
  3099. Valid for the current buffer.
  3100. This variable is populated from #+PROPERTY lines.")
  3101. (make-variable-buffer-local 'org-file-properties)
  3102. (defgroup org-agenda nil
  3103. "Options concerning agenda views in Org-mode."
  3104. :tag "Org Agenda"
  3105. :group 'org)
  3106. (defvar org-category nil
  3107. "Variable used by org files to set a category for agenda display.
  3108. Such files should use a file variable to set it, for example
  3109. # -*- mode: org; org-category: \"ELisp\"
  3110. or contain a special line
  3111. #+CATEGORY: ELisp
  3112. If the file does not specify a category, then file's base name
  3113. is used instead.")
  3114. (make-variable-buffer-local 'org-category)
  3115. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3116. (defcustom org-agenda-files nil
  3117. "The files to be used for agenda display.
  3118. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3119. \\[org-remove-file]. You can also use customize to edit the list.
  3120. If an entry is a directory, all files in that directory that are matched by
  3121. `org-agenda-file-regexp' will be part of the file list.
  3122. If the value of the variable is not a list but a single file name, then
  3123. the list of agenda files is actually stored and maintained in that file, one
  3124. agenda file per line. In this file paths can be given relative to
  3125. `org-directory'. Tilde expansion and environment variable substitution
  3126. are also made."
  3127. :group 'org-agenda
  3128. :type '(choice
  3129. (repeat :tag "List of files and directories" file)
  3130. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3131. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3132. "Regular expression to match files for `org-agenda-files'.
  3133. If any element in the list in that variable contains a directory instead
  3134. of a normal file, all files in that directory that are matched by this
  3135. regular expression will be included."
  3136. :group 'org-agenda
  3137. :type 'regexp)
  3138. (defcustom org-agenda-text-search-extra-files nil
  3139. "List of extra files to be searched by text search commands.
  3140. These files will be searched in addition to the agenda files by the
  3141. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3142. Note that these files will only be searched for text search commands,
  3143. not for the other agenda views like todo lists, tag searches or the weekly
  3144. agenda. This variable is intended to list notes and possibly archive files
  3145. that should also be searched by these two commands.
  3146. In fact, if the first element in the list is the symbol `agenda-archives',
  3147. then all archive files of all agenda files will be added to the search
  3148. scope."
  3149. :group 'org-agenda
  3150. :type '(set :greedy t
  3151. (const :tag "Agenda Archives" agenda-archives)
  3152. (repeat :inline t (file))))
  3153. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3154. 'org-agenda-text-search-extra-files)
  3155. (defcustom org-agenda-skip-unavailable-files nil
  3156. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3157. A nil value means to remove them, after a query, from the list."
  3158. :group 'org-agenda
  3159. :type 'boolean)
  3160. (defcustom org-calendar-to-agenda-key [?c]
  3161. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3162. The command `org-calendar-goto-agenda' will be bound to this key. The
  3163. default is the character `c' because then `c' can be used to switch back and
  3164. forth between agenda and calendar."
  3165. :group 'org-agenda
  3166. :type 'sexp)
  3167. (defcustom org-calendar-insert-diary-entry-key [?i]
  3168. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3169. This option is irrelevant until `org-agenda-diary-file' has been configured
  3170. to point to an Org-mode file. When that is the case, the command
  3171. `org-agenda-diary-entry' will be bound to the key given here, by default
  3172. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3173. if you want to continue doing this, you need to change this to a different
  3174. key."
  3175. :group 'org-agenda
  3176. :type 'sexp)
  3177. (defcustom org-agenda-diary-file 'diary-file
  3178. "File to which to add new entries with the `i' key in agenda and calendar.
  3179. When this is the symbol `diary-file', the functionality in the Emacs
  3180. calendar will be used to add entries to the `diary-file'. But when this
  3181. points to a file, `org-agenda-diary-entry' will be used instead."
  3182. :group 'org-agenda
  3183. :type '(choice
  3184. (const :tag "The standard Emacs diary file" diary-file)
  3185. (file :tag "Special Org file diary entries")))
  3186. (eval-after-load "calendar"
  3187. '(progn
  3188. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3189. 'org-calendar-goto-agenda)
  3190. (add-hook 'calendar-mode-hook
  3191. (lambda ()
  3192. (unless (eq org-agenda-diary-file 'diary-file)
  3193. (define-key calendar-mode-map
  3194. org-calendar-insert-diary-entry-key
  3195. 'org-agenda-diary-entry))))))
  3196. (defgroup org-latex nil
  3197. "Options for embedding LaTeX code into Org-mode."
  3198. :tag "Org LaTeX"
  3199. :group 'org)
  3200. (defcustom org-format-latex-options
  3201. '(:foreground default :background default :scale 1.0
  3202. :html-foreground "Black" :html-background "Transparent"
  3203. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3204. "Options for creating images from LaTeX fragments.
  3205. This is a property list with the following properties:
  3206. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3207. `default' means use the foreground of the default face.
  3208. `auto' means use the foreground from the text face.
  3209. :background the background color, or \"Transparent\".
  3210. `default' means use the background of the default face.
  3211. `auto' means use the background from the text face.
  3212. :scale a scaling factor for the size of the images, to get more pixels
  3213. :html-foreground, :html-background, :html-scale
  3214. the same numbers for HTML export.
  3215. :matchers a list indicating which matchers should be used to
  3216. find LaTeX fragments. Valid members of this list are:
  3217. \"begin\" find environments
  3218. \"$1\" find single characters surrounded by $.$
  3219. \"$\" find math expressions surrounded by $...$
  3220. \"$$\" find math expressions surrounded by $$....$$
  3221. \"\\(\" find math expressions surrounded by \\(...\\)
  3222. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3223. :group 'org-latex
  3224. :type 'plist)
  3225. (defcustom org-format-latex-signal-error t
  3226. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3227. When nil, just push out a message."
  3228. :group 'org-latex
  3229. :version "24.1"
  3230. :type 'boolean)
  3231. (defcustom org-latex-to-mathml-jar-file nil
  3232. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3233. Use this to specify additional executable file say a jar file.
  3234. When using MathToWeb as the converter, specify the full-path to
  3235. your mathtoweb.jar file."
  3236. :group 'org-latex
  3237. :version "24.1"
  3238. :type '(choice
  3239. (const :tag "None" nil)
  3240. (file :tag "JAR file" :must-match t)))
  3241. (defcustom org-latex-to-mathml-convert-command nil
  3242. "Command to convert LaTeX fragments to MathML.
  3243. Replace format-specifiers in the command as noted below and use
  3244. `shell-command' to convert LaTeX to MathML.
  3245. %j: Executable file in fully expanded form as specified by
  3246. `org-latex-to-mathml-jar-file'.
  3247. %I: Input LaTeX file in fully expanded form
  3248. %o: Output MathML file
  3249. This command is used by `org-create-math-formula'.
  3250. When using MathToWeb as the converter, set this to
  3251. \"java -jar %j -unicode -force -df %o %I\"."
  3252. :group 'org-latex
  3253. :version "24.1"
  3254. :type '(choice
  3255. (const :tag "None" nil)
  3256. (string :tag "\nShell command")))
  3257. (defcustom org-latex-create-formula-image-program 'dvipng
  3258. "Program to convert LaTeX fragments with.
  3259. dvipng Process the LaTeX fragments to dvi file, then convert
  3260. dvi files to png files using dvipng.
  3261. This will also include processing of non-math environments.
  3262. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3263. to convert pdf files to png files"
  3264. :group 'org-latex
  3265. :version "24.1"
  3266. :type '(choice
  3267. (const :tag "dvipng" dvipng)
  3268. (const :tag "imagemagick" imagemagick)))
  3269. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3270. "Path to store latex preview images.
  3271. A relative path here creates many directories relative to the
  3272. processed org files paths. An absolute path puts all preview
  3273. images at the same place."
  3274. :group 'org-latex
  3275. :version "24.3"
  3276. :type 'string)
  3277. (defun org-format-latex-mathml-available-p ()
  3278. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3279. (save-match-data
  3280. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3281. org-latex-to-mathml-convert-command)
  3282. (let ((executable (car (split-string
  3283. org-latex-to-mathml-convert-command))))
  3284. (when (executable-find executable)
  3285. (if (string-match
  3286. "%j" org-latex-to-mathml-convert-command)
  3287. (file-readable-p org-latex-to-mathml-jar-file)
  3288. t))))))
  3289. (defcustom org-format-latex-header "\\documentclass{article}
  3290. \\usepackage[usenames]{color}
  3291. \[PACKAGES]
  3292. \[DEFAULT-PACKAGES]
  3293. \\pagestyle{empty} % do not remove
  3294. % The settings below are copied from fullpage.sty
  3295. \\setlength{\\textwidth}{\\paperwidth}
  3296. \\addtolength{\\textwidth}{-3cm}
  3297. \\setlength{\\oddsidemargin}{1.5cm}
  3298. \\addtolength{\\oddsidemargin}{-2.54cm}
  3299. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3300. \\setlength{\\textheight}{\\paperheight}
  3301. \\addtolength{\\textheight}{-\\headheight}
  3302. \\addtolength{\\textheight}{-\\headsep}
  3303. \\addtolength{\\textheight}{-\\footskip}
  3304. \\addtolength{\\textheight}{-3cm}
  3305. \\setlength{\\topmargin}{1.5cm}
  3306. \\addtolength{\\topmargin}{-2.54cm}"
  3307. "The document header used for processing LaTeX fragments.
  3308. It is imperative that this header make sure that no page number
  3309. appears on the page. The package defined in the variables
  3310. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3311. will either replace the placeholder \"[PACKAGES]\" in this
  3312. header, or they will be appended."
  3313. :group 'org-latex
  3314. :type 'string)
  3315. (defun org-set-packages-alist (var val)
  3316. "Set the packages alist and make sure it has 3 elements per entry."
  3317. (set var (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. val)))
  3322. (defun org-get-packages-alist (var)
  3323. "Get the packages alist and make sure it has 3 elements per entry."
  3324. (mapcar (lambda (x)
  3325. (if (and (consp x) (= (length x) 2))
  3326. (list (car x) (nth 1 x) t)
  3327. x))
  3328. (default-value var)))
  3329. (defcustom org-latex-default-packages-alist
  3330. '(("AUTO" "inputenc" t)
  3331. ("T1" "fontenc" t)
  3332. ("" "fixltx2e" nil)
  3333. ("" "graphicx" t)
  3334. ("" "longtable" nil)
  3335. ("" "float" nil)
  3336. ("" "wrapfig" nil)
  3337. ("" "rotating" nil)
  3338. ("normalem" "ulem" t)
  3339. ("" "amsmath" t)
  3340. ("" "textcomp" t)
  3341. ("" "marvosym" t)
  3342. ("" "wasysym" t)
  3343. ("" "amssymb" t)
  3344. ("" "hyperref" nil)
  3345. "\\tolerance=1000")
  3346. "Alist of default packages to be inserted in the header.
  3347. Change this only if one of the packages here causes an
  3348. incompatibility with another package you are using.
  3349. The packages in this list are needed by one part or another of
  3350. Org mode to function properly:
  3351. - inputenc, fontenc: for basic font and character selection
  3352. - fixltx2e: Important patches of LaTeX itself
  3353. - graphicx: for including images
  3354. - longtable: For multipage tables
  3355. - float, wrapfig: for figure placement
  3356. - rotating: for sideways figures and tables
  3357. - ulem: for underline and strike-through
  3358. - amsmath: for subscript and superscript and math environments
  3359. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3360. for interpreting the entities in `org-entities'. You can skip
  3361. some of these packages if you don't use any of their symbols.
  3362. - hyperref: for cross references
  3363. Therefore you should not modify this variable unless you know
  3364. what you are doing. The one reason to change it anyway is that
  3365. you might be loading some other package that conflicts with one
  3366. of the default packages. Each cell is of the format
  3367. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3368. the package also needs to be included when compiling LaTeX
  3369. snippets into images for inclusion into non-LaTeX output."
  3370. :group 'org-latex
  3371. :group 'org-export-latex
  3372. :set 'org-set-packages-alist
  3373. :get 'org-get-packages-alist
  3374. :version "24.1"
  3375. :type '(repeat
  3376. (choice
  3377. (list :tag "options/package pair"
  3378. (string :tag "options")
  3379. (string :tag "package")
  3380. (boolean :tag "Snippet"))
  3381. (string :tag "A line of LaTeX"))))
  3382. (defcustom org-latex-packages-alist nil
  3383. "Alist of packages to be inserted in every LaTeX header.
  3384. These will be inserted after `org-latex-default-packages-alist'.
  3385. Each cell is of the format:
  3386. \(\"options\" \"package\" snippet-flag)
  3387. SNIPPET-FLAG, when t, indicates that this package is also needed
  3388. when turning LaTeX snippets into images for inclusion into
  3389. non-LaTeX output.
  3390. Make sure that you only list packages here which:
  3391. - you want in every file
  3392. - do not conflict with the setup in `org-format-latex-header'.
  3393. - do not conflict with the default packages in
  3394. `org-latex-default-packages-alist'."
  3395. :group 'org-latex
  3396. :group 'org-export-latex
  3397. :set 'org-set-packages-alist
  3398. :get 'org-get-packages-alist
  3399. :type '(repeat
  3400. (choice
  3401. (list :tag "options/package pair"
  3402. (string :tag "options")
  3403. (string :tag "package")
  3404. (boolean :tag "Snippet"))
  3405. (string :tag "A line of LaTeX"))))
  3406. (defgroup org-appearance nil
  3407. "Settings for Org-mode appearance."
  3408. :tag "Org Appearance"
  3409. :group 'org)
  3410. (defcustom org-level-color-stars-only nil
  3411. "Non-nil means fontify only the stars in each headline.
  3412. When nil, the entire headline is fontified.
  3413. Changing it requires restart of `font-lock-mode' to become effective
  3414. also in regions already fontified."
  3415. :group 'org-appearance
  3416. :type 'boolean)
  3417. (defcustom org-hide-leading-stars nil
  3418. "Non-nil means hide the first N-1 stars in a headline.
  3419. This works by using the face `org-hide' for these stars. This
  3420. face is white for a light background, and black for a dark
  3421. background. You may have to customize the face `org-hide' to
  3422. make this work.
  3423. Changing it requires restart of `font-lock-mode' to become effective
  3424. also in regions already fontified.
  3425. You may also set this on a per-file basis by adding one of the following
  3426. lines to the buffer:
  3427. #+STARTUP: hidestars
  3428. #+STARTUP: showstars"
  3429. :group 'org-appearance
  3430. :type 'boolean)
  3431. (defcustom org-hidden-keywords nil
  3432. "List of symbols corresponding to keywords to be hidden the org buffer.
  3433. For example, a value '(title) for this list will make the document's title
  3434. appear in the buffer without the initial #+TITLE: keyword."
  3435. :group 'org-appearance
  3436. :version "24.1"
  3437. :type '(set (const :tag "#+AUTHOR" author)
  3438. (const :tag "#+DATE" date)
  3439. (const :tag "#+EMAIL" email)
  3440. (const :tag "#+TITLE" title)))
  3441. (defcustom org-custom-properties nil
  3442. "List of properties (as strings) with a special meaning.
  3443. The default use of these custom properties is to let the user
  3444. hide them with `org-toggle-custom-properties-visibility'."
  3445. :group 'org-properties
  3446. :group 'org-appearance
  3447. :version "24.3"
  3448. :type '(repeat (string :tag "Property Name")))
  3449. (defcustom org-fontify-done-headline nil
  3450. "Non-nil means change the face of a headline if it is marked DONE.
  3451. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3452. When this is non-nil, the headline after the keyword is set to the
  3453. `org-headline-done' as an additional indication."
  3454. :group 'org-appearance
  3455. :type 'boolean)
  3456. (defcustom org-fontify-emphasized-text t
  3457. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3458. Changing this variable requires a restart of Emacs to take effect."
  3459. :group 'org-appearance
  3460. :type 'boolean)
  3461. (defcustom org-fontify-whole-heading-line nil
  3462. "Non-nil means fontify the whole line for headings.
  3463. This is useful when setting a background color for the
  3464. org-level-* faces."
  3465. :group 'org-appearance
  3466. :type 'boolean)
  3467. (defcustom org-highlight-latex-and-related nil
  3468. "Non-nil means highlight LaTeX related syntax in the buffer.
  3469. When non nil, the value should be a list containing any of the
  3470. following symbols:
  3471. `latex' Highlight LaTeX snippets and environments.
  3472. `script' Highlight subscript and superscript.
  3473. `entities' Highlight entities."
  3474. :group 'org-appearance
  3475. :version "24.4"
  3476. :package-version '(Org . "8.0")
  3477. :type '(choice
  3478. (const :tag "No highlighting" nil)
  3479. (set :greedy t :tag "Highlight"
  3480. (const :tag "LaTeX snippets and environments" latex)
  3481. (const :tag "Subscript and superscript" script)
  3482. (const :tag "Entities" entities))))
  3483. (defcustom org-hide-emphasis-markers nil
  3484. "Non-nil mean font-lock should hide the emphasis marker characters."
  3485. :group 'org-appearance
  3486. :type 'boolean)
  3487. (defcustom org-pretty-entities nil
  3488. "Non-nil means show entities as UTF8 characters.
  3489. When nil, the \\name form remains in the buffer."
  3490. :group 'org-appearance
  3491. :version "24.1"
  3492. :type 'boolean)
  3493. (defcustom org-pretty-entities-include-sub-superscripts t
  3494. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3495. :group 'org-appearance
  3496. :version "24.1"
  3497. :type 'boolean)
  3498. (defvar org-emph-re nil
  3499. "Regular expression for matching emphasis.
  3500. After a match, the match groups contain these elements:
  3501. 0 The match of the full regular expression, including the characters
  3502. before and after the proper match
  3503. 1 The character before the proper match, or empty at beginning of line
  3504. 2 The proper match, including the leading and trailing markers
  3505. 3 The leading marker like * or /, indicating the type of highlighting
  3506. 4 The text between the emphasis markers, not including the markers
  3507. 5 The character after the match, empty at the end of a line")
  3508. (defvar org-verbatim-re nil
  3509. "Regular expression for matching verbatim text.")
  3510. (defvar org-emphasis-regexp-components) ; defined just below
  3511. (defvar org-emphasis-alist) ; defined just below
  3512. (defun org-set-emph-re (var val)
  3513. "Set variable and compute the emphasis regular expression."
  3514. (set var val)
  3515. (when (and (boundp 'org-emphasis-alist)
  3516. (boundp 'org-emphasis-regexp-components)
  3517. org-emphasis-alist org-emphasis-regexp-components)
  3518. (let* ((e org-emphasis-regexp-components)
  3519. (pre (car e))
  3520. (post (nth 1 e))
  3521. (border (nth 2 e))
  3522. (body (nth 3 e))
  3523. (nl (nth 4 e))
  3524. (body1 (concat body "*?"))
  3525. (markers (mapconcat 'car org-emphasis-alist ""))
  3526. (vmarkers (mapconcat
  3527. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3528. org-emphasis-alist "")))
  3529. ;; make sure special characters appear at the right position in the class
  3530. (if (string-match "\\^" markers)
  3531. (setq markers (concat (replace-match "" t t markers) "^")))
  3532. (if (string-match "-" markers)
  3533. (setq markers (concat (replace-match "" t t markers) "-")))
  3534. (if (string-match "\\^" vmarkers)
  3535. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3536. (if (string-match "-" vmarkers)
  3537. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3538. (if (> nl 0)
  3539. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3540. (int-to-string nl) "\\}")))
  3541. ;; Make the regexp
  3542. (setq org-emph-re
  3543. (concat "\\([" pre "]\\|^\\)"
  3544. "\\("
  3545. "\\([" markers "]\\)"
  3546. "\\("
  3547. "[^" border "]\\|"
  3548. "[^" border "]"
  3549. body1
  3550. "[^" border "]"
  3551. "\\)"
  3552. "\\3\\)"
  3553. "\\([" post "]\\|$\\)"))
  3554. (setq org-verbatim-re
  3555. (concat "\\([" pre "]\\|^\\)"
  3556. "\\("
  3557. "\\([" vmarkers "]\\)"
  3558. "\\("
  3559. "[^" border "]\\|"
  3560. "[^" border "]"
  3561. body1
  3562. "[^" border "]"
  3563. "\\)"
  3564. "\\3\\)"
  3565. "\\([" post "]\\|$\\)")))))
  3566. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3567. ;; set this option proved cumbersome. See this message/thread:
  3568. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3569. (defvar org-emphasis-regexp-components
  3570. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3571. "Components used to build the regular expression for emphasis.
  3572. This is a list with five entries. Terminology: In an emphasis string
  3573. like \" *strong word* \", we call the initial space PREMATCH, the final
  3574. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3575. and \"trong wor\" is the body. The different components in this variable
  3576. specify what is allowed/forbidden in each part:
  3577. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3578. post Chars allowed as postmatch. End of line will be allowed too.
  3579. border The chars *forbidden* as border characters.
  3580. body-regexp A regexp like \".\" to match a body character. Don't use
  3581. non-shy groups here, and don't allow newline here.
  3582. newline The maximum number of newlines allowed in an emphasis exp.
  3583. You need to reload Org or to restart Emacs after customizing this.")
  3584. (defcustom org-emphasis-alist
  3585. `(("*" bold)
  3586. ("/" italic)
  3587. ("_" underline)
  3588. ("=" org-code verbatim)
  3589. ("~" org-verbatim verbatim)
  3590. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3591. "Alist of characters and faces to emphasize text.
  3592. Text starting and ending with a special character will be emphasized,
  3593. for example *bold*, _underlined_ and /italic/. This variable sets the
  3594. marker characters and the face to be used by font-lock for highlighting
  3595. in Org-mode Emacs buffers.
  3596. You need to reload Org or to restart Emacs after customizing this."
  3597. :group 'org-appearance
  3598. :set 'org-set-emph-re
  3599. :version "24.4"
  3600. :package-version '(Org . "8.0")
  3601. :type '(repeat
  3602. (list
  3603. (string :tag "Marker character")
  3604. (choice
  3605. (face :tag "Font-lock-face")
  3606. (plist :tag "Face property list"))
  3607. (option (const verbatim)))))
  3608. (defvar org-protecting-blocks
  3609. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3610. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3611. This is needed for font-lock setup.")
  3612. ;;; Miscellaneous options
  3613. (defgroup org-completion nil
  3614. "Completion in Org-mode."
  3615. :tag "Org Completion"
  3616. :group 'org)
  3617. (defcustom org-completion-use-ido nil
  3618. "Non-nil means use ido completion wherever possible.
  3619. Note that `ido-mode' must be active for this variable to be relevant.
  3620. If you decide to turn this variable on, you might well want to turn off
  3621. `org-outline-path-complete-in-steps'.
  3622. See also `org-completion-use-iswitchb'."
  3623. :group 'org-completion
  3624. :type 'boolean)
  3625. (defcustom org-completion-use-iswitchb nil
  3626. "Non-nil means use iswitchb completion wherever possible.
  3627. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3628. If you decide to turn this variable on, you might well want to turn off
  3629. `org-outline-path-complete-in-steps'.
  3630. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3631. :group 'org-completion
  3632. :type 'boolean)
  3633. (defcustom org-completion-fallback-command 'hippie-expand
  3634. "The expansion command called by \\[pcomplete] in normal context.
  3635. Normal means, no org-mode-specific context."
  3636. :group 'org-completion
  3637. :type 'function)
  3638. ;;; Functions and variables from their packages
  3639. ;; Declared here to avoid compiler warnings
  3640. ;; XEmacs only
  3641. (defvar outline-mode-menu-heading)
  3642. (defvar outline-mode-menu-show)
  3643. (defvar outline-mode-menu-hide)
  3644. (defvar zmacs-regions) ; XEmacs regions
  3645. ;; Emacs only
  3646. (defvar mark-active)
  3647. ;; Various packages
  3648. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3649. (declare-function calendar-forward-day "cal-move" (arg))
  3650. (declare-function calendar-goto-date "cal-move" (date))
  3651. (declare-function calendar-goto-today "cal-move" ())
  3652. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3653. (defvar calc-embedded-close-formula)
  3654. (defvar calc-embedded-open-formula)
  3655. (declare-function cdlatex-tab "ext:cdlatex" ())
  3656. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3657. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3658. (defvar font-lock-unfontify-region-function)
  3659. (declare-function iswitchb-read-buffer "iswitchb"
  3660. (prompt &optional default require-match start matches-set))
  3661. (defvar iswitchb-temp-buflist)
  3662. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3663. (defvar org-agenda-tags-todo-honor-ignore-options)
  3664. (declare-function org-agenda-skip "org-agenda" ())
  3665. (declare-function
  3666. org-agenda-format-item "org-agenda"
  3667. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3668. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3669. (declare-function org-agenda-change-all-lines "org-agenda"
  3670. (newhead hdmarker &optional fixface just-this))
  3671. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3672. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3673. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3674. (beg end))
  3675. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3676. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3677. "org-agenda" (&optional end))
  3678. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3679. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3680. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3681. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3682. (declare-function org-indent-mode "org-indent" (&optional arg))
  3683. (declare-function parse-time-string "parse-time" (string))
  3684. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3685. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3686. (defvar remember-data-file)
  3687. (defvar texmathp-why)
  3688. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3689. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3690. (defvar org-latex-regexps)
  3691. ;;; Autoload and prepare some org modules
  3692. ;; Some table stuff that needs to be defined here, because it is used
  3693. ;; by the functions setting up org-mode or checking for table context.
  3694. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3695. "Detect an org-type or table-type table.")
  3696. (defconst org-table-line-regexp "^[ \t]*|"
  3697. "Detect an org-type table line.")
  3698. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3699. "Detect an org-type table line.")
  3700. (defconst org-table-hline-regexp "^[ \t]*|-"
  3701. "Detect an org-type table hline.")
  3702. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3703. "Detect a table-type table hline.")
  3704. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3705. "Detect the first line outside a table when searching from within it.
  3706. This works for both table types.")
  3707. ;; Autoload the functions in org-table.el that are needed by functions here.
  3708. (eval-and-compile
  3709. (org-autoload "org-table"
  3710. '(org-table-begin org-table-blank-field org-table-end)))
  3711. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3712. "Detect a #+TBLFM line.")
  3713. ;;;###autoload
  3714. (defun turn-on-orgtbl ()
  3715. "Unconditionally turn on `orgtbl-mode'."
  3716. (require 'org-table)
  3717. (orgtbl-mode 1))
  3718. (defun org-at-table-p (&optional table-type)
  3719. "Return t if the cursor is inside an org-type table.
  3720. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3721. (if org-enable-table-editor
  3722. (save-excursion
  3723. (beginning-of-line 1)
  3724. (looking-at (if table-type org-table-any-line-regexp
  3725. org-table-line-regexp)))
  3726. nil))
  3727. (defsubst org-table-p () (org-at-table-p))
  3728. (defun org-at-table.el-p ()
  3729. "Return t if and only if we are at a table.el table."
  3730. (and (org-at-table-p 'any)
  3731. (save-excursion
  3732. (goto-char (org-table-begin 'any))
  3733. (looking-at org-table1-hline-regexp))))
  3734. (defun org-table-recognize-table.el ()
  3735. "If there is a table.el table nearby, recognize it and move into it."
  3736. (if org-table-tab-recognizes-table.el
  3737. (if (org-at-table.el-p)
  3738. (progn
  3739. (beginning-of-line 1)
  3740. (if (looking-at org-table-dataline-regexp)
  3741. nil
  3742. (if (looking-at org-table1-hline-regexp)
  3743. (progn
  3744. (beginning-of-line 2)
  3745. (if (looking-at org-table-any-border-regexp)
  3746. (beginning-of-line -1)))))
  3747. (if (re-search-forward "|" (org-table-end t) t)
  3748. (progn
  3749. (require 'table)
  3750. (if (table--at-cell-p (point))
  3751. t
  3752. (message "recognizing table.el table...")
  3753. (table-recognize-table)
  3754. (message "recognizing table.el table...done")))
  3755. (error "This should not happen"))
  3756. t)
  3757. nil)
  3758. nil))
  3759. (defun org-at-table-hline-p ()
  3760. "Return t if the cursor is inside a hline in a table."
  3761. (if org-enable-table-editor
  3762. (save-excursion
  3763. (beginning-of-line 1)
  3764. (looking-at org-table-hline-regexp))
  3765. nil))
  3766. (defun org-table-map-tables (function &optional quietly)
  3767. "Apply FUNCTION to the start of all tables in the buffer."
  3768. (save-excursion
  3769. (save-restriction
  3770. (widen)
  3771. (goto-char (point-min))
  3772. (while (re-search-forward org-table-any-line-regexp nil t)
  3773. (unless quietly
  3774. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3775. (beginning-of-line 1)
  3776. (when (and (looking-at org-table-line-regexp)
  3777. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3778. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3779. (save-excursion (funcall function))
  3780. (or (looking-at org-table-line-regexp)
  3781. (forward-char 1)))
  3782. (re-search-forward org-table-any-border-regexp nil 1))))
  3783. (unless quietly (message "Mapping tables: done")))
  3784. ;; Declare and autoload functions from org-agenda.el
  3785. (eval-and-compile
  3786. (org-autoload "org-agenda"
  3787. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3788. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3789. (declare-function org-clock-update-mode-line "org-clock" ())
  3790. (declare-function org-resolve-clocks "org-clock"
  3791. (&optional also-non-dangling-p prompt last-valid))
  3792. (defun org-at-TBLFM-p (&optional pos)
  3793. "Return t when point (or POS) is in #+TBLFM line."
  3794. (save-excursion
  3795. (let ((pos pos)))
  3796. (goto-char (or pos (point)))
  3797. (beginning-of-line 1)
  3798. (looking-at org-TBLFM-regexp)))
  3799. (defvar org-clock-start-time)
  3800. (defvar org-clock-marker (make-marker)
  3801. "Marker recording the last clock-in.")
  3802. (defvar org-clock-hd-marker (make-marker)
  3803. "Marker recording the last clock-in, but the headline position.")
  3804. (defvar org-clock-heading ""
  3805. "The heading of the current clock entry.")
  3806. (defun org-clock-is-active ()
  3807. "Return the buffer where the clock is currently running.
  3808. Return nil if no clock is running."
  3809. (marker-buffer org-clock-marker))
  3810. (eval-and-compile
  3811. (org-autoload "org-clock" '(org-clock-remove-overlays
  3812. org-clock-update-time-maybe
  3813. org-clocktable-shift)))
  3814. (defun org-check-running-clock ()
  3815. "Check if the current buffer contains the running clock.
  3816. If yes, offer to stop it and to save the buffer with the changes."
  3817. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3818. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3819. (buffer-name))))
  3820. (org-clock-out)
  3821. (when (y-or-n-p "Save changed buffer?")
  3822. (save-buffer))))
  3823. (defun org-clocktable-try-shift (dir n)
  3824. "Check if this line starts a clock table, if yes, shift the time block."
  3825. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3826. (org-clocktable-shift dir n)))
  3827. ;;;###autoload
  3828. (defun org-clock-persistence-insinuate ()
  3829. "Set up hooks for clock persistence."
  3830. (require 'org-clock)
  3831. (add-hook 'org-mode-hook 'org-clock-load)
  3832. (add-hook 'kill-emacs-hook 'org-clock-save))
  3833. ;; Define the variable already here, to make sure we have it.
  3834. (defvar org-indent-mode nil
  3835. "Non-nil if Org-Indent mode is enabled.
  3836. Use the command `org-indent-mode' to change this variable.")
  3837. ;; Autoload archiving code
  3838. ;; The stuff that is needed for cycling and tags has to be defined here.
  3839. (defgroup org-archive nil
  3840. "Options concerning archiving in Org-mode."
  3841. :tag "Org Archive"
  3842. :group 'org-structure)
  3843. (defcustom org-archive-location "%s_archive::"
  3844. "The location where subtrees should be archived.
  3845. The value of this variable is a string, consisting of two parts,
  3846. separated by a double-colon. The first part is a filename and
  3847. the second part is a headline.
  3848. When the filename is omitted, archiving happens in the same file.
  3849. %s in the filename will be replaced by the current file
  3850. name (without the directory part). Archiving to a different file
  3851. is useful to keep archived entries from contributing to the
  3852. Org-mode Agenda.
  3853. The archived entries will be filed as subtrees of the specified
  3854. headline. When the headline is omitted, the subtrees are simply
  3855. filed away at the end of the file, as top-level entries. Also in
  3856. the heading you can use %s to represent the file name, this can be
  3857. useful when using the same archive for a number of different files.
  3858. Here are a few examples:
  3859. \"%s_archive::\"
  3860. If the current file is Projects.org, archive in file
  3861. Projects.org_archive, as top-level trees. This is the default.
  3862. \"::* Archived Tasks\"
  3863. Archive in the current file, under the top-level headline
  3864. \"* Archived Tasks\".
  3865. \"~/org/archive.org::\"
  3866. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3867. \"~/org/archive.org::* From %s\"
  3868. Archive in file ~/org/archive.org (absolute path), under headlines
  3869. \"From FILENAME\" where file name is the current file name.
  3870. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3871. The \"datetree/\" string is special, signifying to archive
  3872. items to the datetree. Items are placed in either the CLOSED
  3873. date of the item, or the current date if there is no CLOSED date.
  3874. The heading will be a subentry to the current date. There doesn't
  3875. need to be a heading, but there always needs to be a slash after
  3876. datetree. For example, to store archived items directly in the
  3877. datetree, use \"~/org/datetree.org::datetree/\".
  3878. \"basement::** Finished Tasks\"
  3879. Archive in file ./basement (relative path), as level 3 trees
  3880. below the level 2 heading \"** Finished Tasks\".
  3881. You may set this option on a per-file basis by adding to the buffer a
  3882. line like
  3883. #+ARCHIVE: basement::** Finished Tasks
  3884. You may also define it locally for a subtree by setting an ARCHIVE property
  3885. in the entry. If such a property is found in an entry, or anywhere up
  3886. the hierarchy, it will be used."
  3887. :group 'org-archive
  3888. :type 'string)
  3889. (defcustom org-archive-tag "ARCHIVE"
  3890. "The tag that marks a subtree as archived.
  3891. An archived subtree does not open during visibility cycling, and does
  3892. not contribute to the agenda listings.
  3893. After changing this, font-lock must be restarted in the relevant buffers to
  3894. get the proper fontification."
  3895. :group 'org-archive
  3896. :group 'org-keywords
  3897. :type 'string)
  3898. (defcustom org-agenda-skip-archived-trees t
  3899. "Non-nil means the agenda will skip any items located in archived trees.
  3900. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3901. variable is no longer recommended, you should leave it at the value t.
  3902. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3903. :group 'org-archive
  3904. :group 'org-agenda-skip
  3905. :type 'boolean)
  3906. (defcustom org-columns-skip-archived-trees t
  3907. "Non-nil means ignore archived trees when creating column view."
  3908. :group 'org-archive
  3909. :group 'org-properties
  3910. :type 'boolean)
  3911. (defcustom org-cycle-open-archived-trees nil
  3912. "Non-nil means `org-cycle' will open archived trees.
  3913. An archived tree is a tree marked with the tag ARCHIVE.
  3914. When nil, archived trees will stay folded. You can still open them with
  3915. normal outline commands like `show-all', but not with the cycling commands."
  3916. :group 'org-archive
  3917. :group 'org-cycle
  3918. :type 'boolean)
  3919. (defcustom org-sparse-tree-open-archived-trees nil
  3920. "Non-nil means sparse tree construction shows matches in archived trees.
  3921. When nil, matches in these trees are highlighted, but the trees are kept in
  3922. collapsed state."
  3923. :group 'org-archive
  3924. :group 'org-sparse-trees
  3925. :type 'boolean)
  3926. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3927. "The default date type when building a sparse tree.
  3928. When this is nil, a date is a scheduled or a deadline timestamp.
  3929. Otherwise, these types are allowed:
  3930. all: all timestamps
  3931. active: only active timestamps (<...>)
  3932. inactive: only inactive timestamps (<...)
  3933. scheduled: only scheduled timestamps
  3934. deadline: only deadline timestamps"
  3935. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3936. (const :tag "All timestamps" all)
  3937. (const :tag "Only active timestamps" active)
  3938. (const :tag "Only inactive timestamps" inactive)
  3939. (const :tag "Only scheduled timestamps" scheduled)
  3940. (const :tag "Only deadline timestamps" deadline)
  3941. (const :tag "Only closed timestamps" closed))
  3942. :version "24.3"
  3943. :group 'org-sparse-trees)
  3944. (defun org-cycle-hide-archived-subtrees (state)
  3945. "Re-hide all archived subtrees after a visibility state change."
  3946. (when (and (not org-cycle-open-archived-trees)
  3947. (not (memq state '(overview folded))))
  3948. (save-excursion
  3949. (let* ((globalp (memq state '(contents all)))
  3950. (beg (if globalp (point-min) (point)))
  3951. (end (if globalp (point-max) (org-end-of-subtree t))))
  3952. (org-hide-archived-subtrees beg end)
  3953. (goto-char beg)
  3954. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3955. (message "%s" (substitute-command-keys
  3956. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3957. (defun org-force-cycle-archived ()
  3958. "Cycle subtree even if it is archived."
  3959. (interactive)
  3960. (setq this-command 'org-cycle)
  3961. (let ((org-cycle-open-archived-trees t))
  3962. (call-interactively 'org-cycle)))
  3963. (defun org-hide-archived-subtrees (beg end)
  3964. "Re-hide all archived subtrees after a visibility state change."
  3965. (save-excursion
  3966. (let* ((re (concat ":" org-archive-tag ":")))
  3967. (goto-char beg)
  3968. (while (re-search-forward re end t)
  3969. (when (org-at-heading-p)
  3970. (org-flag-subtree t)
  3971. (org-end-of-subtree t))))))
  3972. (declare-function outline-end-of-heading "outline" ())
  3973. (declare-function outline-flag-region "outline" (from to flag))
  3974. (defun org-flag-subtree (flag)
  3975. (save-excursion
  3976. (org-back-to-heading t)
  3977. (outline-end-of-heading)
  3978. (outline-flag-region (point)
  3979. (progn (org-end-of-subtree t) (point))
  3980. flag)))
  3981. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3982. (eval-and-compile
  3983. (org-autoload "org-archive"
  3984. '(org-add-archive-files)))
  3985. ;; Autoload Column View Code
  3986. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3987. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3988. (declare-function org-columns-compute "org-colview" (property))
  3989. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3990. '(org-columns-number-to-string
  3991. org-columns-get-format-and-top-level
  3992. org-columns-compute
  3993. org-columns-remove-overlays))
  3994. ;; Autoload ID code
  3995. (declare-function org-id-store-link "org-id")
  3996. (declare-function org-id-locations-load "org-id")
  3997. (declare-function org-id-locations-save "org-id")
  3998. (defvar org-id-track-globally)
  3999. (org-autoload "org-id"
  4000. '(org-id-new
  4001. org-id-copy
  4002. org-id-get-with-outline-path-completion
  4003. org-id-get-with-outline-drilling))
  4004. ;;; Variables for pre-computed regular expressions, all buffer local
  4005. (defvar org-todo-regexp nil
  4006. "Matches any of the TODO state keywords.")
  4007. (make-variable-buffer-local 'org-todo-regexp)
  4008. (defvar org-not-done-regexp nil
  4009. "Matches any of the TODO state keywords except the last one.")
  4010. (make-variable-buffer-local 'org-not-done-regexp)
  4011. (defvar org-not-done-heading-regexp nil
  4012. "Matches a TODO headline that is not done.")
  4013. (make-variable-buffer-local 'org-not-done-regexp)
  4014. (defvar org-todo-line-regexp nil
  4015. "Matches a headline and puts TODO state into group 2 if present.")
  4016. (make-variable-buffer-local 'org-todo-line-regexp)
  4017. (defvar org-complex-heading-regexp nil
  4018. "Matches a headline and puts everything into groups:
  4019. group 1: the stars
  4020. group 2: The todo keyword, maybe
  4021. group 3: Priority cookie
  4022. group 4: True headline
  4023. group 5: Tags")
  4024. (make-variable-buffer-local 'org-complex-heading-regexp)
  4025. (defvar org-complex-heading-regexp-format nil
  4026. "Printf format to make regexp to match an exact headline.
  4027. This regexp will match the headline of any node which has the
  4028. exact headline text that is put into the format, but may have any
  4029. TODO state, priority and tags.")
  4030. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4031. (defvar org-todo-line-tags-regexp nil
  4032. "Matches a headline and puts TODO state into group 2 if present.
  4033. Also put tags into group 4 if tags are present.")
  4034. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4035. (defvar org-ds-keyword-length 12
  4036. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4037. (make-variable-buffer-local 'org-ds-keyword-length)
  4038. (defvar org-deadline-regexp nil
  4039. "Matches the DEADLINE keyword.")
  4040. (make-variable-buffer-local 'org-deadline-regexp)
  4041. (defvar org-deadline-time-regexp nil
  4042. "Matches the DEADLINE keyword together with a time stamp.")
  4043. (make-variable-buffer-local 'org-deadline-time-regexp)
  4044. (defvar org-deadline-time-hour-regexp nil
  4045. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4046. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4047. (defvar org-deadline-line-regexp nil
  4048. "Matches the DEADLINE keyword and the rest of the line.")
  4049. (make-variable-buffer-local 'org-deadline-line-regexp)
  4050. (defvar org-scheduled-regexp nil
  4051. "Matches the SCHEDULED keyword.")
  4052. (make-variable-buffer-local 'org-scheduled-regexp)
  4053. (defvar org-scheduled-time-regexp nil
  4054. "Matches the SCHEDULED keyword together with a time stamp.")
  4055. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4056. (defvar org-scheduled-time-hour-regexp nil
  4057. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4058. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4059. (defvar org-closed-time-regexp nil
  4060. "Matches the CLOSED keyword together with a time stamp.")
  4061. (make-variable-buffer-local 'org-closed-time-regexp)
  4062. (defvar org-keyword-time-regexp nil
  4063. "Matches any of the 4 keywords, together with the time stamp.")
  4064. (make-variable-buffer-local 'org-keyword-time-regexp)
  4065. (defvar org-keyword-time-not-clock-regexp nil
  4066. "Matches any of the 3 keywords, together with the time stamp.")
  4067. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4068. (defvar org-maybe-keyword-time-regexp nil
  4069. "Matches a timestamp, possibly preceded by a keyword.")
  4070. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4071. (defvar org-all-time-keywords nil
  4072. "List of time keywords.")
  4073. (make-variable-buffer-local 'org-all-time-keywords)
  4074. (defconst org-plain-time-of-day-regexp
  4075. (concat
  4076. "\\(\\<[012]?[0-9]"
  4077. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4078. "\\(--?"
  4079. "\\(\\<[012]?[0-9]"
  4080. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4081. "\\)?")
  4082. "Regular expression to match a plain time or time range.
  4083. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4084. groups carry important information:
  4085. 0 the full match
  4086. 1 the first time, range or not
  4087. 8 the second time, if it is a range.")
  4088. (defconst org-plain-time-extension-regexp
  4089. (concat
  4090. "\\(\\<[012]?[0-9]"
  4091. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4092. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4093. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4094. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4095. groups carry important information:
  4096. 0 the full match
  4097. 7 hours of duration
  4098. 9 minutes of duration")
  4099. (defconst org-stamp-time-of-day-regexp
  4100. (concat
  4101. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4102. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4103. "\\(--?"
  4104. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4105. "Regular expression to match a timestamp time or time range.
  4106. After a match, the following groups carry important information:
  4107. 0 the full match
  4108. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4109. 2 the first time, range or not
  4110. 4 the second time, if it is a range.")
  4111. (defconst org-startup-options
  4112. '(("fold" org-startup-folded t)
  4113. ("overview" org-startup-folded t)
  4114. ("nofold" org-startup-folded nil)
  4115. ("showall" org-startup-folded nil)
  4116. ("showeverything" org-startup-folded showeverything)
  4117. ("content" org-startup-folded content)
  4118. ("indent" org-startup-indented t)
  4119. ("noindent" org-startup-indented nil)
  4120. ("hidestars" org-hide-leading-stars t)
  4121. ("showstars" org-hide-leading-stars nil)
  4122. ("odd" org-odd-levels-only t)
  4123. ("oddeven" org-odd-levels-only nil)
  4124. ("align" org-startup-align-all-tables t)
  4125. ("noalign" org-startup-align-all-tables nil)
  4126. ("inlineimages" org-startup-with-inline-images t)
  4127. ("noinlineimages" org-startup-with-inline-images nil)
  4128. ("latexpreview" org-startup-with-latex-preview t)
  4129. ("nolatexpreview" org-startup-with-latex-preview nil)
  4130. ("customtime" org-display-custom-times t)
  4131. ("logdone" org-log-done time)
  4132. ("lognotedone" org-log-done note)
  4133. ("nologdone" org-log-done nil)
  4134. ("lognoteclock-out" org-log-note-clock-out t)
  4135. ("nolognoteclock-out" org-log-note-clock-out nil)
  4136. ("logrepeat" org-log-repeat state)
  4137. ("lognoterepeat" org-log-repeat note)
  4138. ("logdrawer" org-log-into-drawer t)
  4139. ("nologdrawer" org-log-into-drawer nil)
  4140. ("logstatesreversed" org-log-states-order-reversed t)
  4141. ("nologstatesreversed" org-log-states-order-reversed nil)
  4142. ("nologrepeat" org-log-repeat nil)
  4143. ("logreschedule" org-log-reschedule time)
  4144. ("lognotereschedule" org-log-reschedule note)
  4145. ("nologreschedule" org-log-reschedule nil)
  4146. ("logredeadline" org-log-redeadline time)
  4147. ("lognoteredeadline" org-log-redeadline note)
  4148. ("nologredeadline" org-log-redeadline nil)
  4149. ("logrefile" org-log-refile time)
  4150. ("lognoterefile" org-log-refile note)
  4151. ("nologrefile" org-log-refile nil)
  4152. ("fninline" org-footnote-define-inline t)
  4153. ("nofninline" org-footnote-define-inline nil)
  4154. ("fnlocal" org-footnote-section nil)
  4155. ("fnauto" org-footnote-auto-label t)
  4156. ("fnprompt" org-footnote-auto-label nil)
  4157. ("fnconfirm" org-footnote-auto-label confirm)
  4158. ("fnplain" org-footnote-auto-label plain)
  4159. ("fnadjust" org-footnote-auto-adjust t)
  4160. ("nofnadjust" org-footnote-auto-adjust nil)
  4161. ("constcgs" constants-unit-system cgs)
  4162. ("constSI" constants-unit-system SI)
  4163. ("noptag" org-tag-persistent-alist nil)
  4164. ("hideblocks" org-hide-block-startup t)
  4165. ("nohideblocks" org-hide-block-startup nil)
  4166. ("beamer" org-startup-with-beamer-mode t)
  4167. ("entitiespretty" org-pretty-entities t)
  4168. ("entitiesplain" org-pretty-entities nil))
  4169. "Variable associated with STARTUP options for org-mode.
  4170. Each element is a list of three items: the startup options (as written
  4171. in the #+STARTUP line), the corresponding variable, and the value to set
  4172. this variable to if the option is found. An optional forth element PUSH
  4173. means to push this value onto the list in the variable.")
  4174. (defun org-update-property-plist (key val props)
  4175. "Update PROPS with KEY and VAL."
  4176. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4177. (key (if appending (substring key 0 (- (length key) 1)) key))
  4178. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4179. (previous (cdr (assoc key props))))
  4180. (if appending
  4181. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4182. (cons (cons key val) remainder))))
  4183. (defconst org-block-regexp
  4184. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4185. "Regular expression for hiding blocks.")
  4186. (defconst org-heading-keyword-regexp-format
  4187. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4188. "Printf format for a regexp matching a headline with some keyword.
  4189. This regexp will match the headline of any node which has the
  4190. exact keyword that is put into the format. The keyword isn't in
  4191. any group by default, but the stars and the body are.")
  4192. (defconst org-heading-keyword-maybe-regexp-format
  4193. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4194. "Printf format for a regexp matching a headline, possibly with some keyword.
  4195. This regexp can match any headline with the specified keyword, or
  4196. without a keyword. The keyword isn't in any group by default,
  4197. but the stars and the body are.")
  4198. (defcustom org-group-tags t
  4199. "When non-nil (the default), use group tags.
  4200. This can be turned on/off through `org-toggle-tags-groups'."
  4201. :group 'org-tags
  4202. :group 'org-startup
  4203. :type 'boolean)
  4204. (defun org-toggle-tags-groups ()
  4205. "Toggle support for group tags.
  4206. Support for group tags is controlled by the option
  4207. `org-group-tags', which is non-nil by default."
  4208. (interactive)
  4209. (setq org-group-tags (not org-group-tags))
  4210. (cond ((and (derived-mode-p 'org-agenda-mode)
  4211. org-group-tags)
  4212. (org-agenda-redo))
  4213. ((derived-mode-p 'org-mode)
  4214. (let ((org-inhibit-startup t)) (org-mode))))
  4215. (message "Groups tags support has been turned %s"
  4216. (if org-group-tags "on" "off")))
  4217. (defun org-set-regexps-and-options-for-tags ()
  4218. "Precompute variables used for tags."
  4219. (when (derived-mode-p 'org-mode)
  4220. (org-set-local 'org-file-tags nil)
  4221. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4222. (splitre "[ \t]+")
  4223. (start 0)
  4224. tags ftags key value)
  4225. (save-excursion
  4226. (save-restriction
  4227. (widen)
  4228. (goto-char (point-min))
  4229. (while (re-search-forward re nil t)
  4230. (setq key (upcase (org-match-string-no-properties 1))
  4231. value (org-match-string-no-properties 2))
  4232. (if (stringp value) (setq value (org-trim value)))
  4233. (cond
  4234. ((equal key "TAGS")
  4235. (setq tags (append tags (if tags '("\\n") nil)
  4236. (org-split-string value splitre))))
  4237. ((equal key "FILETAGS")
  4238. (when (string-match "\\S-" value)
  4239. (setq ftags
  4240. (append
  4241. ftags
  4242. (apply 'append
  4243. (mapcar (lambda (x) (org-split-string x ":"))
  4244. (org-split-string value)))))))))))
  4245. ;; Process the file tags.
  4246. (and ftags (org-set-local 'org-file-tags
  4247. (mapcar 'org-add-prop-inherited ftags)))
  4248. (org-set-local 'org-tag-groups-alist nil)
  4249. ;; Process the tags.
  4250. (when (and (not tags) org-tag-alist)
  4251. (setq tags
  4252. (mapcar
  4253. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4254. ((eq (car tg) :endgroup) "}")
  4255. ((eq (car tg) :grouptags) ":")
  4256. ((eq (car tg) :newline) "\n")
  4257. (t (concat (car tg)
  4258. (if (characterp (cdr tg))
  4259. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4260. org-tag-alist)))
  4261. (let (e tgs g)
  4262. (while (setq e (pop tags))
  4263. (cond
  4264. ((equal e "{")
  4265. (progn (push '(:startgroup) tgs)
  4266. (when (equal (nth 1 tags) ":")
  4267. (push (list (replace-regexp-in-string
  4268. "(.+)$" "" (nth 0 tags)))
  4269. org-tag-groups-alist)
  4270. (setq g 0))))
  4271. ((equal e ":") (push '(:grouptags) tgs))
  4272. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4273. ((equal e "\\n") (push '(:newline) tgs))
  4274. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4275. (push (cons (match-string 1 e)
  4276. (string-to-char (match-string 2 e))) tgs)
  4277. (if (and g (> g 0))
  4278. (setcar org-tag-groups-alist
  4279. (append (car org-tag-groups-alist)
  4280. (list (match-string 1 e)))))
  4281. (if g (setq g (1+ g))))
  4282. (t (push (list e) tgs)
  4283. (if (and g (> g 0))
  4284. (setcar org-tag-groups-alist
  4285. (append (car org-tag-groups-alist) (list e))))
  4286. (if g (setq g (1+ g))))))
  4287. (org-set-local 'org-tag-alist nil)
  4288. (while (setq e (pop tgs))
  4289. (or (and (stringp (car e))
  4290. (assoc (car e) org-tag-alist))
  4291. (push e org-tag-alist)))
  4292. ;; Return a list with tag variables
  4293. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4294. (defvar org-ota nil)
  4295. (defun org-set-regexps-and-options ()
  4296. "Precompute regular expressions used in the current buffer."
  4297. (when (derived-mode-p 'org-mode)
  4298. (org-set-local 'org-todo-kwd-alist nil)
  4299. (org-set-local 'org-todo-key-alist nil)
  4300. (org-set-local 'org-todo-key-trigger nil)
  4301. (org-set-local 'org-todo-keywords-1 nil)
  4302. (org-set-local 'org-done-keywords nil)
  4303. (org-set-local 'org-todo-heads nil)
  4304. (org-set-local 'org-todo-sets nil)
  4305. (org-set-local 'org-todo-log-states nil)
  4306. (org-set-local 'org-file-properties nil)
  4307. (let ((re (org-make-options-regexp
  4308. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4309. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4310. "SETUPFILE" "OPTIONS")
  4311. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4312. (splitre "[ \t]+")
  4313. (scripts org-use-sub-superscripts)
  4314. kwds kws0 kwsa key log value cat arch const links hw dws
  4315. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4316. (start 0))
  4317. (save-excursion
  4318. (save-restriction
  4319. (widen)
  4320. (goto-char (point-min))
  4321. (while
  4322. (or (and
  4323. ext-setup-or-nil
  4324. (not org-ota)
  4325. (let (ret)
  4326. (with-temp-buffer
  4327. (insert ext-setup-or-nil)
  4328. (let ((major-mode 'org-mode) org-ota)
  4329. (setq ret (save-match-data
  4330. (org-set-regexps-and-options-for-tags)))))
  4331. ;; Append setupfile tags to existing tags
  4332. (setq org-ota t)
  4333. (setq org-file-tags
  4334. (delq nil (append org-file-tags (nth 0 ret)))
  4335. org-tag-alist
  4336. (delq nil (append org-tag-alist (nth 1 ret)))
  4337. org-tag-groups-alist
  4338. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4339. (and ext-setup-or-nil
  4340. (string-match re ext-setup-or-nil start)
  4341. (setq start (match-end 0)))
  4342. (and (setq ext-setup-or-nil nil start 0)
  4343. (re-search-forward re nil t)))
  4344. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4345. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4346. (if (stringp value) (setq value (org-trim value)))
  4347. (cond
  4348. ((equal key "CATEGORY")
  4349. (setq cat value))
  4350. ((member key '("SEQ_TODO" "TODO"))
  4351. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4352. ((equal key "TYP_TODO")
  4353. (push (cons 'type (org-split-string value splitre)) kwds))
  4354. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4355. ;; general TODO-like setup
  4356. (push (cons (intern (downcase (match-string 1 key)))
  4357. (org-split-string value splitre)) kwds))
  4358. ((equal key "COLUMNS")
  4359. (org-set-local 'org-columns-default-format value))
  4360. ((equal key "LINK")
  4361. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4362. (push (cons (match-string 1 value)
  4363. (org-trim (match-string 2 value)))
  4364. links)))
  4365. ((equal key "PRIORITIES")
  4366. (setq prio (org-split-string value " +")))
  4367. ((equal key "PROPERTY")
  4368. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4369. (setq props (org-update-property-plist (match-string 1 value)
  4370. (match-string 2 value)
  4371. props))))
  4372. ((equal key "CONSTANTS")
  4373. (org-table-set-constants))
  4374. ((equal key "STARTUP")
  4375. (let ((opts (org-split-string value splitre))
  4376. l var val)
  4377. (while (setq l (pop opts))
  4378. (when (setq l (assoc l org-startup-options))
  4379. (setq var (nth 1 l) val (nth 2 l))
  4380. (if (not (nth 3 l))
  4381. (set (make-local-variable var) val)
  4382. (if (not (listp (symbol-value var)))
  4383. (set (make-local-variable var) nil))
  4384. (set (make-local-variable var) (symbol-value var))
  4385. (add-to-list var val))))))
  4386. ((equal key "ARCHIVE")
  4387. (setq arch value)
  4388. (remove-text-properties 0 (length arch)
  4389. '(face t fontified t) arch))
  4390. ((equal key "OPTIONS")
  4391. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4392. (setq scripts (read (match-string 2 value)))))
  4393. ((and (equal key "SETUPFILE")
  4394. ;; Prevent checking in Gnus messages
  4395. (not buffer-read-only))
  4396. (setq setup-contents (org-file-contents
  4397. (expand-file-name
  4398. (org-remove-double-quotes value))
  4399. 'noerror))
  4400. (if (not ext-setup-or-nil)
  4401. (setq ext-setup-or-nil setup-contents start 0)
  4402. (setq ext-setup-or-nil
  4403. (concat (substring ext-setup-or-nil 0 start)
  4404. "\n" setup-contents "\n"
  4405. (substring ext-setup-or-nil start)))))))
  4406. ;; search for property blocks
  4407. (goto-char (point-min))
  4408. (while (re-search-forward org-block-regexp nil t)
  4409. (when (equal "PROPERTY" (upcase (match-string 1)))
  4410. (setq value (replace-regexp-in-string
  4411. "[\n\r]" " " (match-string 4)))
  4412. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4413. (setq props (org-update-property-plist (match-string 1 value)
  4414. (match-string 2 value)
  4415. props)))))))
  4416. (org-set-local 'org-use-sub-superscripts scripts)
  4417. (when cat
  4418. (org-set-local 'org-category (intern cat))
  4419. (push (cons "CATEGORY" cat) props))
  4420. (when prio
  4421. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4422. (setq prio (mapcar 'string-to-char prio))
  4423. (org-set-local 'org-highest-priority (nth 0 prio))
  4424. (org-set-local 'org-lowest-priority (nth 1 prio))
  4425. (org-set-local 'org-default-priority (nth 2 prio)))
  4426. (and props (org-set-local 'org-file-properties (nreverse props)))
  4427. (and arch (org-set-local 'org-archive-location arch))
  4428. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4429. ;; Process the TODO keywords
  4430. (unless kwds
  4431. ;; Use the global values as if they had been given locally.
  4432. (setq kwds (default-value 'org-todo-keywords))
  4433. (if (stringp (car kwds))
  4434. (setq kwds (list (cons org-todo-interpretation
  4435. (default-value 'org-todo-keywords)))))
  4436. (setq kwds (reverse kwds)))
  4437. (setq kwds (nreverse kwds))
  4438. (let (inter kws kw)
  4439. (while (setq kws (pop kwds))
  4440. (let ((kws (or
  4441. (run-hook-with-args-until-success
  4442. 'org-todo-setup-filter-hook kws)
  4443. kws)))
  4444. (setq inter (pop kws) sep (member "|" kws)
  4445. kws0 (delete "|" (copy-sequence kws))
  4446. kwsa nil
  4447. kws1 (mapcar
  4448. (lambda (x)
  4449. ;; 1 2
  4450. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4451. (progn
  4452. (setq kw (match-string 1 x)
  4453. key (and (match-end 2) (match-string 2 x))
  4454. log (org-extract-log-state-settings x))
  4455. (push (cons kw (and key (string-to-char key))) kwsa)
  4456. (and log (push log org-todo-log-states))
  4457. kw)
  4458. (error "Invalid TODO keyword %s" x)))
  4459. kws0)
  4460. kwsa (if kwsa (append '((:startgroup))
  4461. (nreverse kwsa)
  4462. '((:endgroup))))
  4463. hw (car kws1)
  4464. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4465. tail (list inter hw (car dws) (org-last dws))))
  4466. (add-to-list 'org-todo-heads hw 'append)
  4467. (push kws1 org-todo-sets)
  4468. (setq org-done-keywords (append org-done-keywords dws nil))
  4469. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4470. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4471. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4472. (setq org-todo-sets (nreverse org-todo-sets)
  4473. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4474. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4475. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4476. ;; Compute the regular expressions and other local variables.
  4477. ;; Using `org-outline-regexp-bol' would complicate them much,
  4478. ;; because of the fixed white space at the end of that string.
  4479. (if (not org-done-keywords)
  4480. (setq org-done-keywords (and org-todo-keywords-1
  4481. (list (org-last org-todo-keywords-1)))))
  4482. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4483. (length org-scheduled-string)
  4484. (length org-clock-string)
  4485. (length org-closed-string)))
  4486. org-not-done-keywords
  4487. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4488. org-todo-regexp
  4489. (concat "\\("
  4490. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4491. "\\)")
  4492. org-not-done-regexp
  4493. (concat "\\("
  4494. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4495. "\\)")
  4496. org-not-done-heading-regexp
  4497. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4498. org-todo-line-regexp
  4499. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4500. org-complex-heading-regexp
  4501. (concat "^\\(\\*+\\)"
  4502. "\\(?: +" org-todo-regexp "\\)?"
  4503. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4504. "\\(?: +\\(.*?\\)\\)??"
  4505. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4506. "[ \t]*$")
  4507. org-complex-heading-regexp-format
  4508. (concat "^\\(\\*+\\)"
  4509. "\\(?: +" org-todo-regexp "\\)?"
  4510. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4511. "\\(?: +"
  4512. ;; Stats cookies can be stuck to body.
  4513. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4514. "\\(%s\\)"
  4515. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4516. "\\)"
  4517. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4518. "[ \t]*$")
  4519. org-todo-line-tags-regexp
  4520. (concat "^\\(\\*+\\)"
  4521. "\\(?: +" org-todo-regexp "\\)?"
  4522. "\\(?: +\\(.*?\\)\\)??"
  4523. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4524. "[ \t]*$")
  4525. org-deadline-regexp (concat "\\<" org-deadline-string)
  4526. org-deadline-time-regexp
  4527. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4528. org-deadline-time-hour-regexp
  4529. (concat "\\<" org-deadline-string
  4530. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4531. org-deadline-line-regexp
  4532. (concat "\\<\\(" org-deadline-string "\\).*")
  4533. org-scheduled-regexp
  4534. (concat "\\<" org-scheduled-string)
  4535. org-scheduled-time-regexp
  4536. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4537. org-scheduled-time-hour-regexp
  4538. (concat "\\<" org-scheduled-string
  4539. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4540. org-closed-time-regexp
  4541. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4542. org-keyword-time-regexp
  4543. (concat "\\<\\(" org-scheduled-string
  4544. "\\|" org-deadline-string
  4545. "\\|" org-closed-string
  4546. "\\|" org-clock-string "\\)"
  4547. " *[[<]\\([^]>]+\\)[]>]")
  4548. org-keyword-time-not-clock-regexp
  4549. (concat "\\<\\(" org-scheduled-string
  4550. "\\|" org-deadline-string
  4551. "\\|" org-closed-string
  4552. "\\)"
  4553. " *[[<]\\([^]>]+\\)[]>]")
  4554. org-maybe-keyword-time-regexp
  4555. (concat "\\(\\<\\(" org-scheduled-string
  4556. "\\|" org-deadline-string
  4557. "\\|" org-closed-string
  4558. "\\|" org-clock-string "\\)\\)?"
  4559. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4560. org-all-time-keywords
  4561. (mapcar (lambda (w) (substring w 0 -1))
  4562. (list org-scheduled-string org-deadline-string
  4563. org-clock-string org-closed-string)))
  4564. (setq org-ota nil)
  4565. (org-compute-latex-and-related-regexp))))
  4566. (defun org-file-contents (file &optional noerror)
  4567. "Return the contents of FILE, as a string."
  4568. (if (or (not file)
  4569. (not (file-readable-p file)))
  4570. (if noerror
  4571. (message "Cannot read file \"%s\"" file)
  4572. (error "Cannot read file \"%s\"" file))
  4573. (with-temp-buffer
  4574. (insert-file-contents file)
  4575. (buffer-string))))
  4576. (defun org-extract-log-state-settings (x)
  4577. "Extract the log state setting from a TODO keyword string.
  4578. This will extract info from a string like \"WAIT(w@/!)\"."
  4579. (let (kw key log1 log2)
  4580. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4581. (setq kw (match-string 1 x)
  4582. key (and (match-end 2) (match-string 2 x))
  4583. log1 (and (match-end 3) (match-string 3 x))
  4584. log2 (and (match-end 4) (match-string 4 x)))
  4585. (and (or log1 log2)
  4586. (list kw
  4587. (and log1 (if (equal log1 "!") 'time 'note))
  4588. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4589. (defun org-remove-keyword-keys (list)
  4590. "Remove a pair of parenthesis at the end of each string in LIST."
  4591. (mapcar (lambda (x)
  4592. (if (string-match "(.*)$" x)
  4593. (substring x 0 (match-beginning 0))
  4594. x))
  4595. list))
  4596. (defun org-assign-fast-keys (alist)
  4597. "Assign fast keys to a keyword-key alist.
  4598. Respect keys that are already there."
  4599. (let (new e (alt ?0))
  4600. (while (setq e (pop alist))
  4601. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4602. (cdr e)) ;; Key already assigned.
  4603. (push e new)
  4604. (let ((clist (string-to-list (downcase (car e))))
  4605. (used (append new alist)))
  4606. (when (= (car clist) ?@)
  4607. (pop clist))
  4608. (while (and clist (rassoc (car clist) used))
  4609. (pop clist))
  4610. (unless clist
  4611. (while (rassoc alt used)
  4612. (incf alt)))
  4613. (push (cons (car e) (or (car clist) alt)) new))))
  4614. (nreverse new)))
  4615. ;;; Some variables used in various places
  4616. (defvar org-window-configuration nil
  4617. "Used in various places to store a window configuration.")
  4618. (defvar org-selected-window nil
  4619. "Used in various places to store a window configuration.")
  4620. (defvar org-finish-function nil
  4621. "Function to be called when `C-c C-c' is used.
  4622. This is for getting out of special buffers like capture.")
  4623. ;; FIXME: Occasionally check by commenting these, to make sure
  4624. ;; no other functions uses these, forgetting to let-bind them.
  4625. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4626. (defvar org-last-state)
  4627. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4628. ;; Defined somewhere in this file, but used before definition.
  4629. (defvar org-entities) ;; defined in org-entities.el
  4630. (defvar org-struct-menu)
  4631. (defvar org-org-menu)
  4632. (defvar org-tbl-menu)
  4633. ;;;; Define the Org-mode
  4634. ;; We use a before-change function to check if a table might need
  4635. ;; an update.
  4636. (defvar org-table-may-need-update t
  4637. "Indicates that a table might need an update.
  4638. This variable is set by `org-before-change-function'.
  4639. `org-table-align' sets it back to nil.")
  4640. (defun org-before-change-function (beg end)
  4641. "Every change indicates that a table might need an update."
  4642. (setq org-table-may-need-update t))
  4643. (defvar org-mode-map)
  4644. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4645. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4646. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4647. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4648. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4649. (defvar org-table-buffer-is-an nil)
  4650. (defvar bidi-paragraph-direction)
  4651. (defvar buffer-face-mode-face)
  4652. (require 'outline)
  4653. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4654. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
  4655. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4656. ;; Other stuff we need.
  4657. (require 'time-date)
  4658. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4659. (require 'easymenu)
  4660. (require 'overlay)
  4661. ;; (require 'org-macs) moved higher up in the file before it is first used
  4662. (require 'org-entities)
  4663. ;; (require 'org-compat) moved higher up in the file before it is first used
  4664. (require 'org-faces)
  4665. (require 'org-list)
  4666. (require 'org-pcomplete)
  4667. (require 'org-src)
  4668. (require 'org-footnote)
  4669. (require 'org-macro)
  4670. ;; babel
  4671. (require 'ob)
  4672. ;;;###autoload
  4673. (define-derived-mode org-mode outline-mode "Org"
  4674. "Outline-based notes management and organizer, alias
  4675. \"Carsten's outline-mode for keeping track of everything.\"
  4676. Org-mode develops organizational tasks around a NOTES file which
  4677. contains information about projects as plain text. Org-mode is
  4678. implemented on top of outline-mode, which is ideal to keep the content
  4679. of large files well structured. It supports ToDo items, deadlines and
  4680. time stamps, which magically appear in the diary listing of the Emacs
  4681. calendar. Tables are easily created with a built-in table editor.
  4682. Plain text URL-like links connect to websites, emails (VM), Usenet
  4683. messages (Gnus), BBDB entries, and any files related to the project.
  4684. For printing and sharing of notes, an Org-mode file (or a part of it)
  4685. can be exported as a structured ASCII or HTML file.
  4686. The following commands are available:
  4687. \\{org-mode-map}"
  4688. ;; Get rid of Outline menus, they are not needed
  4689. ;; Need to do this here because define-derived-mode sets up
  4690. ;; the keymap so late. Still, it is a waste to call this each time
  4691. ;; we switch another buffer into org-mode.
  4692. (if (featurep 'xemacs)
  4693. (when (boundp 'outline-mode-menu-heading)
  4694. ;; Assume this is Greg's port, it uses easymenu
  4695. (easy-menu-remove outline-mode-menu-heading)
  4696. (easy-menu-remove outline-mode-menu-show)
  4697. (easy-menu-remove outline-mode-menu-hide))
  4698. (define-key org-mode-map [menu-bar headings] 'undefined)
  4699. (define-key org-mode-map [menu-bar hide] 'undefined)
  4700. (define-key org-mode-map [menu-bar show] 'undefined))
  4701. (org-load-modules-maybe)
  4702. (easy-menu-add org-org-menu)
  4703. (easy-menu-add org-tbl-menu)
  4704. (org-install-agenda-files-menu)
  4705. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4706. (add-to-invisibility-spec '(org-cwidth))
  4707. (add-to-invisibility-spec '(org-hide-block . t))
  4708. (when (featurep 'xemacs)
  4709. (org-set-local 'line-move-ignore-invisible t))
  4710. (org-set-local 'outline-regexp org-outline-regexp)
  4711. (org-set-local 'outline-level 'org-outline-level)
  4712. (setq bidi-paragraph-direction 'left-to-right)
  4713. (when (and org-ellipsis
  4714. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4715. (fboundp 'make-glyph-code))
  4716. (unless org-display-table
  4717. (setq org-display-table (make-display-table)))
  4718. (set-display-table-slot
  4719. org-display-table 4
  4720. (vconcat (mapcar
  4721. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4722. org-ellipsis)))
  4723. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4724. (setq buffer-display-table org-display-table))
  4725. (org-set-regexps-and-options-for-tags)
  4726. (org-set-regexps-and-options)
  4727. (org-set-font-lock-defaults)
  4728. (when (and org-tag-faces (not org-tags-special-faces-re))
  4729. ;; tag faces set outside customize.... force initialization.
  4730. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4731. ;; Calc embedded
  4732. (org-set-local 'calc-embedded-open-mode "# ")
  4733. ;; Modify a few syntax entries
  4734. (modify-syntax-entry ?@ "w")
  4735. (modify-syntax-entry ?\" "\"")
  4736. (if org-startup-truncated (setq truncate-lines t))
  4737. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4738. (org-set-local 'font-lock-unfontify-region-function
  4739. 'org-unfontify-region)
  4740. ;; Activate before-change-function
  4741. (org-set-local 'org-table-may-need-update t)
  4742. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4743. 'local)
  4744. ;; Check for running clock before killing a buffer
  4745. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4746. ;; Initialize macros templates.
  4747. (org-macro-initialize-templates)
  4748. ;; Initialize radio targets.
  4749. (org-update-radio-target-regexp)
  4750. ;; Indentation.
  4751. (org-set-local 'indent-line-function 'org-indent-line)
  4752. (org-set-local 'indent-region-function 'org-indent-region)
  4753. ;; Filling and auto-filling.
  4754. (org-setup-filling)
  4755. ;; Comments.
  4756. (org-setup-comments-handling)
  4757. ;; Initialize cache.
  4758. (org-element-cache-reset)
  4759. ;; Beginning/end of defun
  4760. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4761. (org-set-local 'end-of-defun-function 'org-forward-element)
  4762. ;; Next error for sparse trees
  4763. (org-set-local 'next-error-function 'org-occur-next-match)
  4764. ;; Make sure dependence stuff works reliably, even for users who set it
  4765. ;; too late :-(
  4766. (if org-enforce-todo-dependencies
  4767. (add-hook 'org-blocker-hook
  4768. 'org-block-todo-from-children-or-siblings-or-parent)
  4769. (remove-hook 'org-blocker-hook
  4770. 'org-block-todo-from-children-or-siblings-or-parent))
  4771. (if org-enforce-todo-checkbox-dependencies
  4772. (add-hook 'org-blocker-hook
  4773. 'org-block-todo-from-checkboxes)
  4774. (remove-hook 'org-blocker-hook
  4775. 'org-block-todo-from-checkboxes))
  4776. ;; Align options lines
  4777. (org-set-local
  4778. 'align-mode-rules-list
  4779. '((org-in-buffer-settings
  4780. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4781. (modes . '(org-mode)))))
  4782. ;; Imenu
  4783. (org-set-local 'imenu-create-index-function
  4784. 'org-imenu-get-tree)
  4785. ;; Make isearch reveal context
  4786. (if (or (featurep 'xemacs)
  4787. (not (boundp 'outline-isearch-open-invisible-function)))
  4788. ;; Emacs 21 and XEmacs make use of the hook
  4789. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4790. ;; Emacs 22 deals with this through a special variable
  4791. (org-set-local 'outline-isearch-open-invisible-function
  4792. (lambda (&rest ignore) (org-show-context 'isearch)))
  4793. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4794. ;; Setup the pcomplete hooks
  4795. (set (make-local-variable 'pcomplete-command-completion-function)
  4796. 'org-pcomplete-initial)
  4797. (set (make-local-variable 'pcomplete-command-name-function)
  4798. 'org-command-at-point)
  4799. (set (make-local-variable 'pcomplete-default-completion-function)
  4800. 'ignore)
  4801. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4802. 'org-parse-arguments)
  4803. (set (make-local-variable 'pcomplete-termination-string) "")
  4804. (when (>= emacs-major-version 23)
  4805. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4806. ;; If empty file that did not turn on org-mode automatically, make it to.
  4807. (if (and org-insert-mode-line-in-empty-file
  4808. (org-called-interactively-p 'any)
  4809. (= (point-min) (point-max)))
  4810. (insert "# -*- mode: org -*-\n\n"))
  4811. (unless org-inhibit-startup
  4812. (org-unmodified
  4813. (and org-startup-with-beamer-mode (org-beamer-mode))
  4814. (when org-startup-align-all-tables
  4815. (org-table-map-tables 'org-table-align 'quietly))
  4816. (when org-startup-with-inline-images
  4817. (org-display-inline-images))
  4818. (when org-startup-with-latex-preview
  4819. (org-preview-latex-fragment))
  4820. (unless org-inhibit-startup-visibility-stuff
  4821. (org-set-startup-visibility))))
  4822. ;; Try to set org-hide correctly
  4823. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4824. ;; Update `customize-package-emacs-version-alist'
  4825. (add-to-list 'customize-package-emacs-version-alist
  4826. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4827. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4828. ("8.0" . "24.4")))
  4829. (defvar org-mode-transpose-word-syntax-table
  4830. (let ((st (make-syntax-table)))
  4831. (mapc (lambda(c) (modify-syntax-entry
  4832. (string-to-char (car c)) "w p" st))
  4833. org-emphasis-alist)
  4834. st))
  4835. (when (fboundp 'abbrev-table-put)
  4836. (abbrev-table-put org-mode-abbrev-table
  4837. :parents (list text-mode-abbrev-table)))
  4838. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4839. (defsubst org-fix-ellipsis-at-bol ()
  4840. (save-excursion (goto-char (window-start)) (recenter 0)))
  4841. (defun org-find-invisible-foreground ()
  4842. (let ((candidates (remove
  4843. "unspecified-bg"
  4844. (nconc
  4845. (list (face-background 'default)
  4846. (face-background 'org-default))
  4847. (mapcar
  4848. (lambda (alist)
  4849. (when (boundp alist)
  4850. (cdr (assoc 'background-color (symbol-value alist)))))
  4851. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4852. (list (face-foreground 'org-hide))))))
  4853. (car (remove nil candidates))))
  4854. (defun org-current-time (&optional rounding-minutes past)
  4855. "Current time, possibly rounded to ROUNDING-MINUTES.
  4856. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4857. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4858. the rounding returns a past time."
  4859. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4860. (car org-time-stamp-rounding-minutes)))
  4861. (time (decode-time)) res)
  4862. (if (< r 1)
  4863. (current-time)
  4864. (setq res
  4865. (apply 'encode-time
  4866. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4867. (nthcdr 2 time))))
  4868. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4869. (seconds-to-time (- (org-float-time res) (* r 60)))
  4870. res))))
  4871. (defun org-today ()
  4872. "Return today date, considering `org-extend-today-until'."
  4873. (time-to-days
  4874. (time-subtract (current-time)
  4875. (list 0 (* 3600 org-extend-today-until) 0))))
  4876. ;;;; Font-Lock stuff, including the activators
  4877. (defvar org-mouse-map (make-sparse-keymap))
  4878. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4879. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4880. (when org-mouse-1-follows-link
  4881. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4882. (when org-tab-follows-link
  4883. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4884. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4885. (require 'font-lock)
  4886. (defconst org-non-link-chars "]\t\n\r<>")
  4887. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4888. "shell" "elisp" "doi" "message"))
  4889. (defvar org-link-types-re nil
  4890. "Matches a link that has a url-like prefix like \"http:\"")
  4891. (defvar org-link-re-with-space nil
  4892. "Matches a link with spaces, optional angular brackets around it.")
  4893. (defvar org-link-re-with-space2 nil
  4894. "Matches a link with spaces, optional angular brackets around it.")
  4895. (defvar org-link-re-with-space3 nil
  4896. "Matches a link with spaces, only for internal part in bracket links.")
  4897. (defvar org-angle-link-re nil
  4898. "Matches link with angular brackets, spaces are allowed.")
  4899. (defvar org-plain-link-re nil
  4900. "Matches plain link, without spaces.")
  4901. (defvar org-bracket-link-regexp nil
  4902. "Matches a link in double brackets.")
  4903. (defvar org-bracket-link-analytic-regexp nil
  4904. "Regular expression used to analyze links.
  4905. Here is what the match groups contain after a match:
  4906. 1: http:
  4907. 2: http
  4908. 3: path
  4909. 4: [desc]
  4910. 5: desc")
  4911. (defvar org-bracket-link-analytic-regexp++ nil
  4912. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4913. (defvar org-any-link-re nil
  4914. "Regular expression matching any link.")
  4915. (defconst org-match-sexp-depth 3
  4916. "Number of stacked braces for sub/superscript matching.")
  4917. (defun org-create-multibrace-regexp (left right n)
  4918. "Create a regular expression which will match a balanced sexp.
  4919. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4920. as single character strings.
  4921. The regexp returned will match the entire expression including the
  4922. delimiters. It will also define a single group which contains the
  4923. match except for the outermost delimiters. The maximum depth of
  4924. stacked delimiters is N. Escaping delimiters is not possible."
  4925. (let* ((nothing (concat "[^" left right "]*?"))
  4926. (or "\\|")
  4927. (re nothing)
  4928. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4929. (while (> n 1)
  4930. (setq n (1- n)
  4931. re (concat re or next)
  4932. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4933. (concat left "\\(" re "\\)" right)))
  4934. (defvar org-match-substring-regexp
  4935. (concat
  4936. "\\(\\S-\\)\\([_^]\\)\\("
  4937. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4938. "\\|"
  4939. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4940. "\\|"
  4941. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4942. "The regular expression matching a sub- or superscript.")
  4943. (defvar org-match-substring-with-braces-regexp
  4944. (concat
  4945. "\\(\\S-\\)\\([_^]\\)\\("
  4946. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4947. "\\)")
  4948. "The regular expression matching a sub- or superscript, forcing braces.")
  4949. (defun org-make-link-regexps ()
  4950. "Update the link regular expressions.
  4951. This should be called after the variable `org-link-types' has changed."
  4952. (setq org-link-types-re
  4953. (concat
  4954. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4955. org-link-re-with-space
  4956. (concat
  4957. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. "\\([^" org-non-link-chars " ]"
  4959. "[^" org-non-link-chars "]*"
  4960. "[^" org-non-link-chars " ]\\)>?")
  4961. org-link-re-with-space2
  4962. (concat
  4963. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4964. "\\([^" org-non-link-chars " ]"
  4965. "[^\t\n\r]*"
  4966. "[^" org-non-link-chars " ]\\)>?")
  4967. org-link-re-with-space3
  4968. (concat
  4969. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4970. "\\([^" org-non-link-chars " ]"
  4971. "[^\t\n\r]*\\)")
  4972. org-angle-link-re
  4973. (concat
  4974. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4975. "\\([^" org-non-link-chars " ]"
  4976. "[^" org-non-link-chars "]*"
  4977. "\\)>")
  4978. org-plain-link-re
  4979. (concat
  4980. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4981. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4982. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4983. org-bracket-link-regexp
  4984. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4985. org-bracket-link-analytic-regexp
  4986. (concat
  4987. "\\[\\["
  4988. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4989. "\\([^]]+\\)"
  4990. "\\]"
  4991. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4992. "\\]")
  4993. org-bracket-link-analytic-regexp++
  4994. (concat
  4995. "\\[\\["
  4996. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4997. "\\([^]]+\\)"
  4998. "\\]"
  4999. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5000. "\\]")
  5001. org-any-link-re
  5002. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5003. org-angle-link-re "\\)\\|\\("
  5004. org-plain-link-re "\\)")))
  5005. (org-make-link-regexps)
  5006. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5007. "Regular expression for fast time stamp matching.")
  5008. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5009. "Regular expression for fast time stamp matching.")
  5010. (defconst org-ts-regexp0
  5011. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5012. "Regular expression matching time strings for analysis.
  5013. This one does not require the space after the date, so it can be used
  5014. on a string that terminates immediately after the date.")
  5015. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5016. "Regular expression matching time strings for analysis.")
  5017. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5018. "Regular expression matching time stamps, with groups.")
  5019. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5020. "Regular expression matching time stamps (also [..]), with groups.")
  5021. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5022. "Regular expression matching a time stamp range.")
  5023. (defconst org-tr-regexp-both
  5024. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5025. "Regular expression matching a time stamp range.")
  5026. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5027. org-ts-regexp "\\)?")
  5028. "Regular expression matching a time stamp or time stamp range.")
  5029. (defconst org-tsr-regexp-both
  5030. (concat org-ts-regexp-both "\\(--?-?"
  5031. org-ts-regexp-both "\\)?")
  5032. "Regular expression matching a time stamp or time stamp range.
  5033. The time stamps may be either active or inactive.")
  5034. (defvar org-emph-face nil)
  5035. (defun org-do-emphasis-faces (limit)
  5036. "Run through the buffer and add overlays to emphasized strings."
  5037. (let (rtn a)
  5038. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5039. (if (not (= (char-after (match-beginning 3))
  5040. (char-after (match-beginning 4))))
  5041. (progn
  5042. (setq rtn t)
  5043. (setq a (assoc (match-string 3) org-emphasis-alist))
  5044. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5045. 'face
  5046. (nth 1 a))
  5047. (and (nth 2 a)
  5048. (org-remove-flyspell-overlays-in
  5049. (match-beginning 0) (match-end 0)))
  5050. (add-text-properties (match-beginning 2) (match-end 2)
  5051. '(font-lock-multiline t org-emphasis t))
  5052. (when org-hide-emphasis-markers
  5053. (add-text-properties (match-end 4) (match-beginning 5)
  5054. '(invisible org-link))
  5055. (add-text-properties (match-beginning 3) (match-end 3)
  5056. '(invisible org-link)))))
  5057. (backward-char 1))
  5058. rtn))
  5059. (defun org-emphasize (&optional char)
  5060. "Insert or change an emphasis, i.e. a font like bold or italic.
  5061. If there is an active region, change that region to a new emphasis.
  5062. If there is no region, just insert the marker characters and position
  5063. the cursor between them.
  5064. CHAR should be the marker character. If it is a space, it means to
  5065. remove the emphasis of the selected region.
  5066. If CHAR is not given (for example in an interactive call) it will be
  5067. prompted for."
  5068. (interactive)
  5069. (let ((erc org-emphasis-regexp-components)
  5070. (prompt "")
  5071. (string "") beg end move c s)
  5072. (if (org-region-active-p)
  5073. (setq beg (region-beginning) end (region-end)
  5074. string (buffer-substring beg end))
  5075. (setq move t))
  5076. (unless char
  5077. (message "Emphasis marker or tag: [%s]"
  5078. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5079. (setq char (read-char-exclusive)))
  5080. (if (equal char ?\ )
  5081. (setq s "" move nil)
  5082. (unless (assoc (char-to-string char) org-emphasis-alist)
  5083. (user-error "No such emphasis marker: \"%c\"" char))
  5084. (setq s (char-to-string char)))
  5085. (while (and (> (length string) 1)
  5086. (equal (substring string 0 1) (substring string -1))
  5087. (assoc (substring string 0 1) org-emphasis-alist))
  5088. (setq string (substring string 1 -1)))
  5089. (setq string (concat s string s))
  5090. (if beg (delete-region beg end))
  5091. (unless (or (bolp)
  5092. (string-match (concat "[" (nth 0 erc) "\n]")
  5093. (char-to-string (char-before (point)))))
  5094. (insert " "))
  5095. (unless (or (eobp)
  5096. (string-match (concat "[" (nth 1 erc) "\n]")
  5097. (char-to-string (char-after (point)))))
  5098. (insert " ") (backward-char 1))
  5099. (insert string)
  5100. (and move (backward-char 1))))
  5101. (defconst org-nonsticky-props
  5102. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5103. (defsubst org-rear-nonsticky-at (pos)
  5104. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5105. (defun org-activate-plain-links (limit)
  5106. "Run through the buffer and add overlays to links."
  5107. (let (f hl)
  5108. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5109. (not (org-in-src-block-p)))
  5110. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5111. (setq f (get-text-property (match-beginning 0) 'face))
  5112. (setq hl (org-match-string-no-properties 0))
  5113. (if (or (eq f 'org-tag)
  5114. (and (listp f) (memq 'org-tag f)))
  5115. nil
  5116. (add-text-properties (match-beginning 0) (match-end 0)
  5117. (list 'mouse-face 'highlight
  5118. 'face 'org-link
  5119. 'htmlize-link `(:uri ,hl)
  5120. 'keymap org-mouse-map))
  5121. (org-rear-nonsticky-at (match-end 0)))
  5122. t)))
  5123. (defun org-activate-code (limit)
  5124. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5125. (progn
  5126. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5127. (remove-text-properties (match-beginning 0) (match-end 0)
  5128. '(display t invisible t intangible t))
  5129. t)))
  5130. (defcustom org-src-fontify-natively nil
  5131. "When non-nil, fontify code in code blocks."
  5132. :type 'boolean
  5133. :version "24.1"
  5134. :group 'org-appearance
  5135. :group 'org-babel)
  5136. (defcustom org-allow-promoting-top-level-subtree nil
  5137. "When non-nil, allow promoting a top level subtree.
  5138. The leading star of the top level headline will be replaced
  5139. by a #."
  5140. :type 'boolean
  5141. :version "24.1"
  5142. :group 'org-appearance)
  5143. (defun org-fontify-meta-lines-and-blocks (limit)
  5144. (condition-case nil
  5145. (org-fontify-meta-lines-and-blocks-1 limit)
  5146. (error (message "org-mode fontification error"))))
  5147. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5148. "Fontify #+ lines and blocks."
  5149. (let ((case-fold-search t))
  5150. (if (re-search-forward
  5151. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5152. limit t)
  5153. (let ((beg (match-beginning 0))
  5154. (block-start (match-end 0))
  5155. (block-end nil)
  5156. (lang (match-string 7))
  5157. (beg1 (line-beginning-position 2))
  5158. (dc1 (downcase (match-string 2)))
  5159. (dc3 (downcase (match-string 3)))
  5160. end end1 quoting block-type ovl)
  5161. (cond
  5162. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5163. ;; a single line of backend-specific content
  5164. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5165. (remove-text-properties (match-beginning 0) (match-end 0)
  5166. '(display t invisible t intangible t))
  5167. (add-text-properties (match-beginning 1) (match-end 3)
  5168. '(font-lock-fontified t face org-meta-line))
  5169. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5170. '(font-lock-fontified t face org-block))
  5171. ; for backend-specific code
  5172. t)
  5173. ((and (match-end 4) (equal dc3 "+begin"))
  5174. ;; Truly a block
  5175. (setq block-type (downcase (match-string 5))
  5176. quoting (member block-type org-protecting-blocks))
  5177. (when (re-search-forward
  5178. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5179. nil t) ;; on purpose, we look further than LIMIT
  5180. (setq end (min (point-max) (match-end 0))
  5181. end1 (min (point-max) (1- (match-beginning 0))))
  5182. (setq block-end (match-beginning 0))
  5183. (when quoting
  5184. (remove-text-properties beg end
  5185. '(display t invisible t intangible t)))
  5186. (add-text-properties
  5187. beg end
  5188. '(font-lock-fontified t font-lock-multiline t))
  5189. (add-text-properties beg beg1 '(face org-meta-line))
  5190. (add-text-properties end1 (min (point-max) (1+ end))
  5191. '(face org-meta-line)) ; for end_src
  5192. (cond
  5193. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5194. (org-src-font-lock-fontify-block lang block-start block-end)
  5195. ;; remove old background overlays
  5196. (mapc (lambda (ov)
  5197. (if (eq (overlay-get ov 'face) 'org-block-background)
  5198. (delete-overlay ov)))
  5199. (overlays-at (/ (+ beg1 block-end) 2)))
  5200. ;; add a background overlay
  5201. (setq ovl (make-overlay beg1 block-end))
  5202. (overlay-put ovl 'face 'org-block-background)
  5203. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5204. (quoting
  5205. (add-text-properties beg1 (min (point-max) (1+ end1))
  5206. '(face org-block))) ; end of source block
  5207. ((not org-fontify-quote-and-verse-blocks))
  5208. ((string= block-type "quote")
  5209. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5210. ((string= block-type "verse")
  5211. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5212. (add-text-properties beg beg1 '(face org-block-begin-line))
  5213. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5214. '(face org-block-end-line))
  5215. t))
  5216. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5217. (add-text-properties
  5218. beg (match-end 3)
  5219. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5220. '(font-lock-fontified t invisible t)
  5221. '(font-lock-fontified t face org-document-info-keyword)))
  5222. (add-text-properties
  5223. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5224. (if (string-equal dc1 "+title:")
  5225. '(font-lock-fontified t face org-document-title)
  5226. '(font-lock-fontified t face org-document-info))))
  5227. ((or (equal dc1 "+results")
  5228. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5229. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5230. "+call:" "+header:" "+headers:" "+name:"))
  5231. (and (match-end 4) (equal dc3 "+attr")))
  5232. (add-text-properties
  5233. beg (match-end 0)
  5234. '(font-lock-fontified t face org-meta-line))
  5235. t)
  5236. ((member dc3 '(" " ""))
  5237. (add-text-properties
  5238. beg (match-end 0)
  5239. '(font-lock-fontified t face font-lock-comment-face)))
  5240. ((not (member (char-after beg) '(?\ ?\t)))
  5241. ;; just any other in-buffer setting, but not indented
  5242. (add-text-properties
  5243. beg (match-end 0)
  5244. '(font-lock-fontified t face org-meta-line))
  5245. t)
  5246. (t nil))))))
  5247. (defun org-activate-angle-links (limit)
  5248. "Run through the buffer and add overlays to links."
  5249. (if (and (re-search-forward org-angle-link-re limit t)
  5250. (not (org-in-src-block-p)))
  5251. (progn
  5252. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5253. (add-text-properties (match-beginning 0) (match-end 0)
  5254. (list 'mouse-face 'highlight
  5255. 'keymap org-mouse-map))
  5256. (org-rear-nonsticky-at (match-end 0))
  5257. t)))
  5258. (defun org-activate-footnote-links (limit)
  5259. "Run through the buffer and add overlays to footnotes."
  5260. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5261. (when fn
  5262. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5263. (org-remove-flyspell-overlays-in beg end)
  5264. (add-text-properties beg end
  5265. (list 'mouse-face 'highlight
  5266. 'keymap org-mouse-map
  5267. 'help-echo
  5268. (if (= (point-at-bol) beg)
  5269. "Footnote definition"
  5270. "Footnote reference")
  5271. 'font-lock-fontified t
  5272. 'font-lock-multiline t
  5273. 'face 'org-footnote))))))
  5274. (defun org-activate-bracket-links (limit)
  5275. "Run through the buffer and add overlays to bracketed links."
  5276. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5277. (not (org-in-src-block-p)))
  5278. (let* ((hl (org-match-string-no-properties 1))
  5279. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5280. (ip (org-maybe-intangible
  5281. (list 'invisible 'org-link
  5282. 'keymap org-mouse-map 'mouse-face 'highlight
  5283. 'font-lock-multiline t 'help-echo help
  5284. 'htmlize-link `(:uri ,hl))))
  5285. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5286. 'font-lock-multiline t 'help-echo help
  5287. 'htmlize-link `(:uri ,hl))))
  5288. ;; We need to remove the invisible property here. Table narrowing
  5289. ;; may have made some of this invisible.
  5290. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5291. (remove-text-properties (match-beginning 0) (match-end 0)
  5292. '(invisible nil))
  5293. (if (match-end 3)
  5294. (progn
  5295. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5296. (org-rear-nonsticky-at (match-beginning 3))
  5297. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5298. (org-rear-nonsticky-at (match-end 3))
  5299. (add-text-properties (match-end 3) (match-end 0) ip)
  5300. (org-rear-nonsticky-at (match-end 0)))
  5301. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5302. (org-rear-nonsticky-at (match-beginning 1))
  5303. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5304. (org-rear-nonsticky-at (match-end 1))
  5305. (add-text-properties (match-end 1) (match-end 0) ip)
  5306. (org-rear-nonsticky-at (match-end 0)))
  5307. t)))
  5308. (defun org-activate-dates (limit)
  5309. "Run through the buffer and add overlays to dates."
  5310. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5311. (not (equal (char-before (match-beginning 0)) 91)))
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map))
  5317. (org-rear-nonsticky-at (match-end 0))
  5318. (when org-display-custom-times
  5319. (if (match-end 3)
  5320. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5321. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5322. t)))
  5323. (defvar org-target-link-regexp nil
  5324. "Regular expression matching radio targets in plain text.")
  5325. (make-variable-buffer-local 'org-target-link-regexp)
  5326. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5327. "Regular expression matching a link target.")
  5328. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5329. "Regular expression matching a radio target.")
  5330. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5331. "Regular expression matching any target.")
  5332. (defun org-activate-target-links (limit)
  5333. "Run through the buffer and add overlays to target matches."
  5334. (when org-target-link-regexp
  5335. (let ((case-fold-search t))
  5336. (if (re-search-forward org-target-link-regexp limit t)
  5337. (progn
  5338. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5339. (add-text-properties (match-beginning 0) (match-end 0)
  5340. (list 'mouse-face 'highlight
  5341. 'keymap org-mouse-map
  5342. 'help-echo "Radio target link"
  5343. 'org-linked-text t))
  5344. (org-rear-nonsticky-at (match-end 0))
  5345. t)))))
  5346. (defun org-update-radio-target-regexp ()
  5347. "Find all radio targets in this file and update the regular expression."
  5348. (interactive)
  5349. (when (memq 'radio org-activate-links)
  5350. (setq org-target-link-regexp
  5351. (org-make-target-link-regexp (org-all-targets 'radio)))
  5352. (org-restart-font-lock)))
  5353. (defun org-hide-wide-columns (limit)
  5354. (let (s e)
  5355. (setq s (text-property-any (point) (or limit (point-max))
  5356. 'org-cwidth t))
  5357. (when s
  5358. (setq e (next-single-property-change s 'org-cwidth))
  5359. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5360. (goto-char e)
  5361. t)))
  5362. (defvar org-latex-and-related-regexp nil
  5363. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5364. (defvar org-match-substring-regexp)
  5365. (defvar org-match-substring-with-braces-regexp)
  5366. (defun org-compute-latex-and-related-regexp ()
  5367. "Compute regular expression for LaTeX, entities and sub/superscript.
  5368. Result depends on variable `org-highlight-latex-and-related'."
  5369. (org-set-local
  5370. 'org-latex-and-related-regexp
  5371. (let* ((re-sub
  5372. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5373. ((eq org-use-sub-superscripts '{})
  5374. (list org-match-substring-with-braces-regexp))
  5375. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5376. (re-latex
  5377. (when (memq 'latex org-highlight-latex-and-related)
  5378. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5379. (delq nil
  5380. (mapcar (lambda (x)
  5381. (and (member (car x) matchers) (nth 1 x)))
  5382. org-latex-regexps)))))
  5383. (re-entities
  5384. (when (memq 'entities org-highlight-latex-and-related)
  5385. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5386. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5387. (defun org-do-latex-and-related (limit)
  5388. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5389. LIMIT bounds the search for syntax to highlight. Stop at first
  5390. highlighted object, if any. Return t if some highlighting was
  5391. done, nil otherwise."
  5392. (when (org-string-nw-p org-latex-and-related-regexp)
  5393. (catch 'found
  5394. (while (re-search-forward org-latex-and-related-regexp limit t)
  5395. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5396. 'face))
  5397. '(org-code org-verbatim underline))
  5398. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5399. '(?_ ?^))
  5400. 1
  5401. 0)))
  5402. (font-lock-prepend-text-property
  5403. (+ offset (match-beginning 0)) (match-end 0)
  5404. 'face 'org-latex-and-related)
  5405. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5406. '(font-lock-multiline t)))
  5407. (throw 'found t)))
  5408. nil)))
  5409. (defun org-restart-font-lock ()
  5410. "Restart `font-lock-mode', to force refontification."
  5411. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5412. (font-lock-mode -1)
  5413. (font-lock-mode 1)))
  5414. (defun org-all-targets (&optional radio)
  5415. "Return a list of all targets in this file.
  5416. When optional argument RADIO is non-nil, only find radio
  5417. targets."
  5418. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5419. (save-excursion
  5420. (goto-char (point-min))
  5421. (while (re-search-forward re nil t)
  5422. ;; Make sure point is really within the object.
  5423. (backward-char)
  5424. (let ((obj (org-element-context)))
  5425. (when (memq (org-element-type obj) '(radio-target target))
  5426. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5427. rtn)))
  5428. (defun org-make-target-link-regexp (targets)
  5429. "Make regular expression matching all strings in TARGETS.
  5430. The regular expression finds the targets also if there is a line break
  5431. between words."
  5432. (and targets
  5433. (concat
  5434. "\\<\\("
  5435. (mapconcat
  5436. (lambda (x)
  5437. (setq x (regexp-quote x))
  5438. (while (string-match " +" x)
  5439. (setq x (replace-match "\\s-+" t t x)))
  5440. x)
  5441. targets
  5442. "\\|")
  5443. "\\)\\>")))
  5444. (defun org-activate-tags (limit)
  5445. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5446. (progn
  5447. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5448. (add-text-properties (match-beginning 1) (match-end 1)
  5449. (list 'mouse-face 'highlight
  5450. 'keymap org-mouse-map))
  5451. (org-rear-nonsticky-at (match-end 1))
  5452. t)))
  5453. (defun org-outline-level ()
  5454. "Compute the outline level of the heading at point.
  5455. If this is called at a normal headline, the level is the number of stars.
  5456. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5457. (save-excursion
  5458. (if (not (condition-case nil
  5459. (org-back-to-heading t)
  5460. (error nil)))
  5461. 0
  5462. (looking-at org-outline-regexp)
  5463. (1- (- (match-end 0) (match-beginning 0))))))
  5464. (defvar org-font-lock-keywords nil)
  5465. (defsubst org-re-property (property &optional literal)
  5466. "Return a regexp matching a PROPERTY line.
  5467. Match group 3 will be set to the value if it exists."
  5468. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5469. (if literal property (regexp-quote property))
  5470. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5471. (defconst org-property-re
  5472. (org-re-property ".*?" 'literal)
  5473. "Regular expression matching a property line.
  5474. There are four matching groups:
  5475. 1: :PROPKEY: including the leading and trailing colon,
  5476. 2: PROPKEY without the leading and trailing colon,
  5477. 3: PROPVAL without leading or trailing spaces,
  5478. 4: the indentation of the current line,
  5479. 5: trailing whitespace.")
  5480. (defvar org-font-lock-hook nil
  5481. "Functions to be called for special font lock stuff.")
  5482. (defvar org-font-lock-set-keywords-hook nil
  5483. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5484. This is called after `org-font-lock-extra-keywords' is defined, but before
  5485. it is installed to be used by font lock. This can be useful if something
  5486. needs to be inserted at a specific position in the font-lock sequence.")
  5487. (defun org-font-lock-hook (limit)
  5488. "Run `org-font-lock-hook' within LIMIT."
  5489. (run-hook-with-args 'org-font-lock-hook limit))
  5490. (defun org-set-font-lock-defaults ()
  5491. "Set font lock defaults for the current buffer."
  5492. (let* ((em org-fontify-emphasized-text)
  5493. (lk org-activate-links)
  5494. (org-font-lock-extra-keywords
  5495. (list
  5496. ;; Call the hook
  5497. '(org-font-lock-hook)
  5498. ;; Headlines
  5499. `(,(if org-fontify-whole-heading-line
  5500. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5501. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5502. (1 (org-get-level-face 1))
  5503. (2 (org-get-level-face 2))
  5504. (3 (org-get-level-face 3)))
  5505. ;; Table lines
  5506. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5507. (1 'org-table t))
  5508. ;; Table internals
  5509. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5510. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5511. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5512. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5513. ;; Drawers
  5514. (list org-drawer-regexp '(0 'org-special-keyword t))
  5515. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5516. ;; Properties
  5517. (list org-property-re
  5518. '(1 'org-special-keyword t)
  5519. '(3 'org-property-value t))
  5520. ;; Links
  5521. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5522. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5523. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5524. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5525. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5526. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5527. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5528. ;; Targets.
  5529. (list org-any-target-regexp '(0 'org-target t))
  5530. ;; Diary sexps.
  5531. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5532. ;; Macro
  5533. '("{{{.+}}}" (0 'org-macro t))
  5534. '(org-hide-wide-columns (0 nil append))
  5535. ;; TODO keyword
  5536. (list (format org-heading-keyword-regexp-format
  5537. org-todo-regexp)
  5538. '(2 (org-get-todo-face 2) t))
  5539. ;; DONE
  5540. (if org-fontify-done-headline
  5541. (list (format org-heading-keyword-regexp-format
  5542. (concat
  5543. "\\(?:"
  5544. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5545. "\\)"))
  5546. '(2 'org-headline-done t))
  5547. nil)
  5548. ;; Priorities
  5549. '(org-font-lock-add-priority-faces)
  5550. ;; Tags
  5551. '(org-font-lock-add-tag-faces)
  5552. ;; Tags groups
  5553. (if (and org-group-tags org-tag-groups-alist)
  5554. (list (concat org-outline-regexp-bol ".+\\(:"
  5555. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5556. ":\\).*$")
  5557. '(1 'org-tag-group prepend)))
  5558. ;; Special keywords
  5559. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5560. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5561. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5562. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5563. ;; Emphasis
  5564. (if em
  5565. (if (featurep 'xemacs)
  5566. '(org-do-emphasis-faces (0 nil append))
  5567. '(org-do-emphasis-faces)))
  5568. ;; Checkboxes
  5569. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5570. 1 'org-checkbox prepend)
  5571. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5572. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5573. (0 (org-get-checkbox-statistics-face) t)))
  5574. ;; Description list items
  5575. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5576. 1 'org-list-dt prepend)
  5577. ;; ARCHIVEd headings
  5578. (list (concat
  5579. org-outline-regexp-bol
  5580. "\\(.*:" org-archive-tag ":.*\\)")
  5581. '(1 'org-archived prepend))
  5582. ;; Specials
  5583. '(org-do-latex-and-related)
  5584. '(org-fontify-entities)
  5585. '(org-raise-scripts)
  5586. ;; Code
  5587. '(org-activate-code (1 'org-code t))
  5588. ;; COMMENT
  5589. (list (format org-heading-keyword-regexp-format
  5590. (concat "\\("
  5591. org-comment-string "\\|" org-quote-string
  5592. "\\)"))
  5593. '(2 'org-special-keyword t))
  5594. ;; Blocks and meta lines
  5595. '(org-fontify-meta-lines-and-blocks))))
  5596. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5597. (run-hooks 'org-font-lock-set-keywords-hook)
  5598. ;; Now set the full font-lock-keywords
  5599. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5600. (org-set-local 'font-lock-defaults
  5601. '(org-font-lock-keywords t nil nil backward-paragraph))
  5602. (kill-local-variable 'font-lock-keywords) nil))
  5603. (defun org-toggle-pretty-entities ()
  5604. "Toggle the composition display of entities as UTF8 characters."
  5605. (interactive)
  5606. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5607. (org-restart-font-lock)
  5608. (if org-pretty-entities
  5609. (message "Entities are now displayed as UTF8 characters")
  5610. (save-restriction
  5611. (widen)
  5612. (org-decompose-region (point-min) (point-max))
  5613. (message "Entities are now displayed as plain text"))))
  5614. (defvar org-custom-properties-overlays nil
  5615. "List of overlays used for custom properties.")
  5616. (make-variable-buffer-local 'org-custom-properties-overlays)
  5617. (defun org-toggle-custom-properties-visibility ()
  5618. "Display or hide properties in `org-custom-properties'."
  5619. (interactive)
  5620. (if org-custom-properties-overlays
  5621. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5622. (setq org-custom-properties-overlays nil))
  5623. (unless (not org-custom-properties)
  5624. (save-excursion
  5625. (save-restriction
  5626. (widen)
  5627. (goto-char (point-min))
  5628. (while (re-search-forward org-property-re nil t)
  5629. (mapc (lambda(p)
  5630. (when (equal p (substring (match-string 1) 1 -1))
  5631. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5632. (overlay-put o 'invisible t)
  5633. (overlay-put o 'org-custom-property t)
  5634. (push o org-custom-properties-overlays))))
  5635. org-custom-properties)))))))
  5636. (defun org-fontify-entities (limit)
  5637. "Find an entity to fontify."
  5638. (let (ee)
  5639. (when org-pretty-entities
  5640. (catch 'match
  5641. (while (re-search-forward
  5642. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5643. limit t)
  5644. (if (and (not (org-in-indented-comment-line))
  5645. (setq ee (org-entity-get (match-string 1)))
  5646. (= (length (nth 6 ee)) 1))
  5647. (let*
  5648. ((end (if (equal (match-string 2) "{}")
  5649. (match-end 2)
  5650. (match-end 1))))
  5651. (add-text-properties
  5652. (match-beginning 0) end
  5653. (list 'font-lock-fontified t))
  5654. (compose-region (match-beginning 0) end
  5655. (nth 6 ee) nil)
  5656. (backward-char 1)
  5657. (throw 'match t))))
  5658. nil))))
  5659. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5660. "Fontify string S like in Org-mode."
  5661. (with-temp-buffer
  5662. (insert s)
  5663. (let ((org-odd-levels-only odd-levels))
  5664. (org-mode)
  5665. (font-lock-fontify-buffer)
  5666. (buffer-string))))
  5667. (defvar org-m nil)
  5668. (defvar org-l nil)
  5669. (defvar org-f nil)
  5670. (defun org-get-level-face (n)
  5671. "Get the right face for match N in font-lock matching of headlines."
  5672. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5673. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5674. (if org-cycle-level-faces
  5675. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5676. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5677. (cond
  5678. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5679. ((eq n 2) org-f)
  5680. (t (if org-level-color-stars-only nil org-f))))
  5681. (defun org-get-todo-face (kwd)
  5682. "Get the right face for a TODO keyword KWD.
  5683. If KWD is a number, get the corresponding match group."
  5684. (if (numberp kwd) (setq kwd (match-string kwd)))
  5685. (or (org-face-from-face-or-color
  5686. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5687. (and (member kwd org-done-keywords) 'org-done)
  5688. 'org-todo))
  5689. (defun org-face-from-face-or-color (context inherit face-or-color)
  5690. "Create a face list that inherits INHERIT, but sets the foreground color.
  5691. When FACE-OR-COLOR is not a string, just return it."
  5692. (if (stringp face-or-color)
  5693. (list :inherit inherit
  5694. (cdr (assoc context org-faces-easy-properties))
  5695. face-or-color)
  5696. face-or-color))
  5697. (defun org-font-lock-add-tag-faces (limit)
  5698. "Add the special tag faces."
  5699. (when (and org-tag-faces org-tags-special-faces-re)
  5700. (while (re-search-forward org-tags-special-faces-re limit t)
  5701. (add-text-properties (match-beginning 1) (match-end 1)
  5702. (list 'face (org-get-tag-face 1)
  5703. 'font-lock-fontified t))
  5704. (backward-char 1))))
  5705. (defun org-font-lock-add-priority-faces (limit)
  5706. "Add the special priority faces."
  5707. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5708. (when (save-match-data (org-at-heading-p))
  5709. (add-text-properties
  5710. (match-beginning 0) (match-end 0)
  5711. (list 'face (or (org-face-from-face-or-color
  5712. 'priority 'org-priority
  5713. (cdr (assoc (char-after (match-beginning 1))
  5714. org-priority-faces)))
  5715. 'org-priority)
  5716. 'font-lock-fontified t)))))
  5717. (defun org-get-tag-face (kwd)
  5718. "Get the right face for a TODO keyword KWD.
  5719. If KWD is a number, get the corresponding match group."
  5720. (if (numberp kwd) (setq kwd (match-string kwd)))
  5721. (or (org-face-from-face-or-color
  5722. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5723. 'org-tag))
  5724. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5725. "Remove fontification and activation overlays from links."
  5726. (font-lock-default-unfontify-region beg end)
  5727. (let* ((buffer-undo-list t)
  5728. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5729. (inhibit-modification-hooks t)
  5730. deactivate-mark buffer-file-name buffer-file-truename)
  5731. (org-decompose-region beg end)
  5732. (remove-text-properties beg end
  5733. '(mouse-face t keymap t org-linked-text t
  5734. invisible t intangible t
  5735. org-no-flyspell t org-emphasis t))
  5736. (org-remove-font-lock-display-properties beg end)))
  5737. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5738. ((raise 0.3) (height 0.7))
  5739. ((raise -0.5))
  5740. ((raise 0.5)))
  5741. "Display properties for showing superscripts and subscripts.")
  5742. (defun org-remove-font-lock-display-properties (beg end)
  5743. "Remove specific display properties that have been added by font lock.
  5744. The will remove the raise properties that are used to show superscripts
  5745. and subscripts."
  5746. (let (next prop)
  5747. (while (< beg end)
  5748. (setq next (next-single-property-change beg 'display nil end)
  5749. prop (get-text-property beg 'display))
  5750. (if (member prop org-script-display)
  5751. (put-text-property beg next 'display nil))
  5752. (setq beg next))))
  5753. (defun org-raise-scripts (limit)
  5754. "Add raise properties to sub/superscripts."
  5755. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5756. (if (re-search-forward
  5757. (if (eq org-use-sub-superscripts t)
  5758. org-match-substring-regexp
  5759. org-match-substring-with-braces-regexp)
  5760. limit t)
  5761. (let* ((pos (point)) table-p comment-p
  5762. (mpos (match-beginning 3))
  5763. (emph-p (get-text-property mpos 'org-emphasis))
  5764. (link-p (get-text-property mpos 'mouse-face))
  5765. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5766. (goto-char (point-at-bol))
  5767. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5768. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5769. (goto-char pos)
  5770. ;; Handle a_b^c
  5771. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5772. (if (or comment-p emph-p link-p keyw-p)
  5773. t
  5774. (put-text-property (match-beginning 3) (match-end 0)
  5775. 'display
  5776. (if (equal (char-after (match-beginning 2)) ?^)
  5777. (nth (if table-p 3 1) org-script-display)
  5778. (nth (if table-p 2 0) org-script-display)))
  5779. (add-text-properties (match-beginning 2) (match-end 2)
  5780. (list 'invisible t
  5781. 'org-dwidth t 'org-dwidth-n 1))
  5782. (if (and (eq (char-after (match-beginning 3)) ?{)
  5783. (eq (char-before (match-end 3)) ?}))
  5784. (progn
  5785. (add-text-properties
  5786. (match-beginning 3) (1+ (match-beginning 3))
  5787. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5788. (add-text-properties
  5789. (1- (match-end 3)) (match-end 3)
  5790. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5791. t)))))
  5792. ;;;; Visibility cycling, including org-goto and indirect buffer
  5793. ;;; Cycling
  5794. (defvar org-cycle-global-status nil)
  5795. (make-variable-buffer-local 'org-cycle-global-status)
  5796. (put 'org-cycle-global-status 'org-state t)
  5797. (defvar org-cycle-subtree-status nil)
  5798. (make-variable-buffer-local 'org-cycle-subtree-status)
  5799. (put 'org-cycle-subtree-status 'org-state t)
  5800. (defvar org-inlinetask-min-level)
  5801. (defun org-unlogged-message (&rest args)
  5802. "Display a message, but avoid logging it in the *Messages* buffer."
  5803. (let ((message-log-max nil))
  5804. (apply 'message args)))
  5805. ;;;###autoload
  5806. (defun org-cycle (&optional arg)
  5807. "TAB-action and visibility cycling for Org-mode.
  5808. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5809. is outline visibility cycling, but it also invokes other actions
  5810. in special contexts.
  5811. - When this function is called with a prefix argument, rotate the entire
  5812. buffer through 3 states (global cycling)
  5813. 1. OVERVIEW: Show only top-level headlines.
  5814. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5815. 3. SHOW ALL: Show everything.
  5816. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5817. determined by the variable `org-startup-folded', and by any VISIBILITY
  5818. properties in the buffer.
  5819. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5820. including any drawers.
  5821. - When inside a table, re-align the table and move to the next field.
  5822. - When point is at the beginning of a headline, rotate the subtree started
  5823. by this line through 3 different states (local cycling)
  5824. 1. FOLDED: Only the main headline is shown.
  5825. 2. CHILDREN: The main headline and the direct children are shown.
  5826. From this state, you can move to one of the children
  5827. and zoom in further.
  5828. 3. SUBTREE: Show the entire subtree, including body text.
  5829. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5830. - When point is at the beginning of an empty headline and the variable
  5831. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5832. of the headline by demoting and promoting it to likely levels. This
  5833. speeds up creation document structure by pressing TAB once or several
  5834. times right after creating a new headline.
  5835. - When there is a numeric prefix, go up to a heading with level ARG, do
  5836. a `show-subtree' and return to the previous cursor position. If ARG
  5837. is negative, go up that many levels.
  5838. - When point is not at the beginning of a headline, execute the global
  5839. binding for TAB, which is re-indenting the line. See the option
  5840. `org-cycle-emulate-tab' for details.
  5841. - Special case: if point is at the beginning of the buffer and there is
  5842. no headline in line 1, this function will act as if called with prefix arg
  5843. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5844. But only if also the variable `org-cycle-global-at-bob' is t."
  5845. (interactive "P")
  5846. (org-load-modules-maybe)
  5847. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5848. (and org-cycle-level-after-item/entry-creation
  5849. (or (org-cycle-level)
  5850. (org-cycle-item-indentation))))
  5851. (let* ((limit-level
  5852. (or org-cycle-max-level
  5853. (and (boundp 'org-inlinetask-min-level)
  5854. org-inlinetask-min-level
  5855. (1- org-inlinetask-min-level))))
  5856. (nstars (and limit-level
  5857. (if org-odd-levels-only
  5858. (and limit-level (1- (* limit-level 2)))
  5859. limit-level)))
  5860. (org-outline-regexp
  5861. (if (not (derived-mode-p 'org-mode))
  5862. outline-regexp
  5863. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5864. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5865. (not (looking-at org-outline-regexp))))
  5866. (org-cycle-hook
  5867. (if bob-special
  5868. (delq 'org-optimize-window-after-visibility-change
  5869. (copy-sequence org-cycle-hook))
  5870. org-cycle-hook))
  5871. (pos (point)))
  5872. (if (or bob-special (equal arg '(4)))
  5873. ;; special case: use global cycling
  5874. (setq arg t))
  5875. (cond
  5876. ((equal arg '(16))
  5877. (setq last-command 'dummy)
  5878. (org-set-startup-visibility)
  5879. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5880. ((equal arg '(64))
  5881. (show-all)
  5882. (org-unlogged-message "Entire buffer visible, including drawers"))
  5883. ;; Table: enter it or move to the next field.
  5884. ((org-at-table-p 'any)
  5885. (if (org-at-table.el-p)
  5886. (message "Use C-c ' to edit table.el tables")
  5887. (if arg (org-table-edit-field t)
  5888. (org-table-justify-field-maybe)
  5889. (call-interactively 'org-table-next-field))))
  5890. ((run-hook-with-args-until-success
  5891. 'org-tab-after-check-for-table-hook))
  5892. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5893. ((eq arg t) (org-cycle-internal-global))
  5894. ;; Drawers: delegate to `org-flag-drawer'.
  5895. ((save-excursion
  5896. (beginning-of-line 1)
  5897. (looking-at org-drawer-regexp))
  5898. (org-flag-drawer ; toggle block visibility
  5899. (not (get-char-property (match-end 0) 'invisible))))
  5900. ;; Show-subtree, ARG levels up from here.
  5901. ((integerp arg)
  5902. (save-excursion
  5903. (org-back-to-heading)
  5904. (outline-up-heading (if (< arg 0) (- arg)
  5905. (- (funcall outline-level) arg)))
  5906. (org-show-subtree)))
  5907. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5908. ((and (featurep 'org-inlinetask)
  5909. (org-inlinetask-at-task-p)
  5910. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5911. (org-inlinetask-toggle-visibility))
  5912. ((org-try-cdlatex-tab))
  5913. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5914. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5915. (save-excursion (beginning-of-line 1)
  5916. (looking-at org-outline-regexp)))
  5917. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5918. (org-cycle-internal-local))
  5919. ;; From there: TAB emulation and template completion.
  5920. (buffer-read-only (org-back-to-heading))
  5921. ((run-hook-with-args-until-success
  5922. 'org-tab-after-check-for-cycling-hook))
  5923. ((org-try-structure-completion))
  5924. ((run-hook-with-args-until-success
  5925. 'org-tab-before-tab-emulation-hook))
  5926. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5927. (or (not (bolp))
  5928. (not (looking-at org-outline-regexp))))
  5929. (call-interactively (global-key-binding "\t")))
  5930. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5931. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5932. (or (and (eq org-cycle-emulate-tab 'white)
  5933. (= (match-end 0) (point-at-eol)))
  5934. (and (eq org-cycle-emulate-tab 'whitestart)
  5935. (>= (match-end 0) pos))))
  5936. t
  5937. (eq org-cycle-emulate-tab t))
  5938. (call-interactively (global-key-binding "\t")))
  5939. (t (save-excursion
  5940. (org-back-to-heading)
  5941. (org-cycle)))))))
  5942. (defun org-cycle-internal-global ()
  5943. "Do the global cycling action."
  5944. ;; Hack to avoid display of messages for .org attachments in Gnus
  5945. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5946. (cond
  5947. ((and (eq last-command this-command)
  5948. (eq org-cycle-global-status 'overview))
  5949. ;; We just created the overview - now do table of contents
  5950. ;; This can be slow in very large buffers, so indicate action
  5951. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5952. (unless ga (org-unlogged-message "CONTENTS..."))
  5953. (org-content)
  5954. (unless ga (org-unlogged-message "CONTENTS...done"))
  5955. (setq org-cycle-global-status 'contents)
  5956. (run-hook-with-args 'org-cycle-hook 'contents))
  5957. ((and (eq last-command this-command)
  5958. (eq org-cycle-global-status 'contents))
  5959. ;; We just showed the table of contents - now show everything
  5960. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5961. (show-all)
  5962. (unless ga (org-unlogged-message "SHOW ALL"))
  5963. (setq org-cycle-global-status 'all)
  5964. (run-hook-with-args 'org-cycle-hook 'all))
  5965. (t
  5966. ;; Default action: go to overview
  5967. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5968. (org-overview)
  5969. (unless ga (org-unlogged-message "OVERVIEW"))
  5970. (setq org-cycle-global-status 'overview)
  5971. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5972. (defun org-cycle-internal-local ()
  5973. "Do the local cycling action."
  5974. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5975. ;; First, determine end of headline (EOH), end of subtree or item
  5976. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5977. (save-excursion
  5978. (if (org-at-item-p)
  5979. (progn
  5980. (beginning-of-line)
  5981. (setq struct (org-list-struct))
  5982. (setq eoh (point-at-eol))
  5983. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5984. (setq has-children (org-list-has-child-p (point) struct)))
  5985. (org-back-to-heading)
  5986. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5987. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5988. (setq has-children
  5989. (or (save-excursion
  5990. (let ((level (funcall outline-level)))
  5991. (outline-next-heading)
  5992. (and (org-at-heading-p t)
  5993. (> (funcall outline-level) level))))
  5994. (save-excursion
  5995. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5996. ;; Determine end invisible part of buffer (EOL)
  5997. (beginning-of-line 2)
  5998. ;; XEmacs doesn't have `next-single-char-property-change'
  5999. (if (featurep 'xemacs)
  6000. (while (and (not (eobp)) ;; this is like `next-line'
  6001. (get-char-property (1- (point)) 'invisible))
  6002. (beginning-of-line 2))
  6003. (while (and (not (eobp)) ;; this is like `next-line'
  6004. (get-char-property (1- (point)) 'invisible))
  6005. (goto-char (next-single-char-property-change (point) 'invisible))
  6006. (and (eolp) (beginning-of-line 2))))
  6007. (setq eol (point)))
  6008. ;; Find out what to do next and set `this-command'
  6009. (cond
  6010. ((= eos eoh)
  6011. ;; Nothing is hidden behind this heading
  6012. (unless (org-before-first-heading-p)
  6013. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6014. (org-unlogged-message "EMPTY ENTRY")
  6015. (setq org-cycle-subtree-status nil)
  6016. (save-excursion
  6017. (goto-char eos)
  6018. (outline-next-heading)
  6019. (if (outline-invisible-p) (org-flag-heading nil))))
  6020. ((and (or (>= eol eos)
  6021. (not (string-match "\\S-" (buffer-substring eol eos))))
  6022. (or has-children
  6023. (not (setq children-skipped
  6024. org-cycle-skip-children-state-if-no-children))))
  6025. ;; Entire subtree is hidden in one line: children view
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6028. (if (org-at-item-p)
  6029. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6030. (org-show-entry)
  6031. (org-with-limited-levels (show-children))
  6032. ;; FIXME: This slows down the func way too much.
  6033. ;; How keep drawers hidden in subtree anyway?
  6034. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6035. ;; (org-cycle-hide-drawers 'subtree))
  6036. ;; Fold every list in subtree to top-level items.
  6037. (when (eq org-cycle-include-plain-lists 'integrate)
  6038. (save-excursion
  6039. (org-back-to-heading)
  6040. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6041. (beginning-of-line 1)
  6042. (let* ((struct (org-list-struct))
  6043. (prevs (org-list-prevs-alist struct))
  6044. (end (org-list-get-bottom-point struct)))
  6045. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6046. (org-list-get-all-items (point) struct prevs))
  6047. (goto-char (if (< end eos) end eos)))))))
  6048. (org-unlogged-message "CHILDREN")
  6049. (save-excursion
  6050. (goto-char eos)
  6051. (outline-next-heading)
  6052. (if (outline-invisible-p) (org-flag-heading nil)))
  6053. (setq org-cycle-subtree-status 'children)
  6054. (unless (org-before-first-heading-p)
  6055. (run-hook-with-args 'org-cycle-hook 'children)))
  6056. ((or children-skipped
  6057. (and (eq last-command this-command)
  6058. (eq org-cycle-subtree-status 'children)))
  6059. ;; We just showed the children, or no children are there,
  6060. ;; now show everything.
  6061. (unless (org-before-first-heading-p)
  6062. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6063. (outline-flag-region eoh eos nil)
  6064. (org-unlogged-message
  6065. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6066. (setq org-cycle-subtree-status 'subtree)
  6067. (unless (org-before-first-heading-p)
  6068. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6069. (t
  6070. ;; Default action: hide the subtree.
  6071. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6072. (outline-flag-region eoh eos t)
  6073. (org-unlogged-message "FOLDED")
  6074. (setq org-cycle-subtree-status 'folded)
  6075. (unless (org-before-first-heading-p)
  6076. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6077. ;;;###autoload
  6078. (defun org-global-cycle (&optional arg)
  6079. "Cycle the global visibility. For details see `org-cycle'.
  6080. With \\[universal-argument] prefix arg, switch to startup visibility.
  6081. With a numeric prefix, show all headlines up to that level."
  6082. (interactive "P")
  6083. (let ((org-cycle-include-plain-lists
  6084. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6085. (cond
  6086. ((integerp arg)
  6087. (show-all)
  6088. (hide-sublevels arg)
  6089. (setq org-cycle-global-status 'contents))
  6090. ((equal arg '(4))
  6091. (org-set-startup-visibility)
  6092. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6093. (t
  6094. (org-cycle '(4))))))
  6095. (defun org-set-startup-visibility ()
  6096. "Set the visibility required by startup options and properties."
  6097. (cond
  6098. ((eq org-startup-folded t)
  6099. (org-cycle '(4)))
  6100. ((eq org-startup-folded 'content)
  6101. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6102. (org-cycle '(4)) (org-cycle '(4)))))
  6103. (unless (eq org-startup-folded 'showeverything)
  6104. (if org-hide-block-startup (org-hide-block-all))
  6105. (org-set-visibility-according-to-property 'no-cleanup)
  6106. (org-cycle-hide-archived-subtrees 'all)
  6107. (org-cycle-hide-drawers 'all)
  6108. (org-cycle-show-empty-lines t)))
  6109. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6110. "Switch subtree visibilities according to :VISIBILITY: property."
  6111. (interactive)
  6112. (let (org-show-entry-below state)
  6113. (save-excursion
  6114. (goto-char (point-min))
  6115. (while (re-search-forward
  6116. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6117. nil t)
  6118. (setq state (match-string 1))
  6119. (save-excursion
  6120. (org-back-to-heading t)
  6121. (hide-subtree)
  6122. (org-reveal)
  6123. (cond
  6124. ((equal state '("fold" "folded"))
  6125. (hide-subtree))
  6126. ((equal state "children")
  6127. (org-show-hidden-entry)
  6128. (show-children))
  6129. ((equal state "content")
  6130. (save-excursion
  6131. (save-restriction
  6132. (org-narrow-to-subtree)
  6133. (org-content))))
  6134. ((member state '("all" "showall"))
  6135. (show-subtree)))))
  6136. (unless no-cleanup
  6137. (org-cycle-hide-archived-subtrees 'all)
  6138. (org-cycle-hide-drawers 'all)
  6139. (org-cycle-show-empty-lines 'all)))))
  6140. ;; This function uses outline-regexp instead of the more fundamental
  6141. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6142. ;; buffers, where outline-regexp is needed.
  6143. (defun org-overview ()
  6144. "Switch to overview mode, showing only top-level headlines.
  6145. Really, this shows all headlines with level equal or greater than the level
  6146. of the first headline in the buffer. This is important, because if the
  6147. first headline is not level one, then (hide-sublevels 1) gives confusing
  6148. results."
  6149. (interactive)
  6150. (let ((pos (point))
  6151. (level (save-excursion
  6152. (goto-char (point-min))
  6153. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6154. (progn
  6155. (goto-char (match-beginning 0))
  6156. (funcall outline-level))))))
  6157. (and level (hide-sublevels level))
  6158. (recenter '(4))
  6159. (goto-char pos)))
  6160. (defun org-content (&optional arg)
  6161. "Show all headlines in the buffer, like a table of contents.
  6162. With numerical argument N, show content up to level N."
  6163. (interactive "P")
  6164. (save-excursion
  6165. ;; Visit all headings and show their offspring
  6166. (and (integerp arg) (org-overview))
  6167. (goto-char (point-max))
  6168. (catch 'exit
  6169. (while (and (progn (condition-case nil
  6170. (outline-previous-visible-heading 1)
  6171. (error (goto-char (point-min))))
  6172. t)
  6173. (looking-at org-outline-regexp))
  6174. (if (integerp arg)
  6175. (show-children (1- arg))
  6176. (show-branches))
  6177. (if (bobp) (throw 'exit nil))))))
  6178. (defun org-optimize-window-after-visibility-change (state)
  6179. "Adjust the window after a change in outline visibility.
  6180. This function is the default value of the hook `org-cycle-hook'."
  6181. (when (get-buffer-window (current-buffer))
  6182. (cond
  6183. ((eq state 'content) nil)
  6184. ((eq state 'all) nil)
  6185. ((eq state 'folded) nil)
  6186. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6187. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6188. (defun org-remove-empty-overlays-at (pos)
  6189. "Remove outline overlays that do not contain non-white stuff."
  6190. (mapc
  6191. (lambda (o)
  6192. (and (eq 'outline (overlay-get o 'invisible))
  6193. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6194. (overlay-end o))))
  6195. (delete-overlay o)))
  6196. (overlays-at pos)))
  6197. (defun org-clean-visibility-after-subtree-move ()
  6198. "Fix visibility issues after moving a subtree."
  6199. ;; First, find a reasonable region to look at:
  6200. ;; Start two siblings above, end three below
  6201. (let* ((beg (save-excursion
  6202. (and (org-get-last-sibling)
  6203. (org-get-last-sibling))
  6204. (point)))
  6205. (end (save-excursion
  6206. (and (org-get-next-sibling)
  6207. (org-get-next-sibling)
  6208. (org-get-next-sibling))
  6209. (if (org-at-heading-p)
  6210. (point-at-eol)
  6211. (point))))
  6212. (level (looking-at "\\*+"))
  6213. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6214. (save-excursion
  6215. (save-restriction
  6216. (narrow-to-region beg end)
  6217. (when re
  6218. ;; Properly fold already folded siblings
  6219. (goto-char (point-min))
  6220. (while (re-search-forward re nil t)
  6221. (if (and (not (outline-invisible-p))
  6222. (save-excursion
  6223. (goto-char (point-at-eol)) (outline-invisible-p)))
  6224. (hide-entry))))
  6225. (org-cycle-show-empty-lines 'overview)
  6226. (org-cycle-hide-drawers 'overview)))))
  6227. (defun org-cycle-show-empty-lines (state)
  6228. "Show empty lines above all visible headlines.
  6229. The region to be covered depends on STATE when called through
  6230. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6231. entire buffer covered. Note that an empty line is only shown if there
  6232. are at least `org-cycle-separator-lines' empty lines before the headline."
  6233. (when (not (= org-cycle-separator-lines 0))
  6234. (save-excursion
  6235. (let* ((n (abs org-cycle-separator-lines))
  6236. (re (cond
  6237. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6238. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6239. (t (let ((ns (number-to-string (- n 2))))
  6240. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6241. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6242. beg end b e)
  6243. (cond
  6244. ((memq state '(overview contents t))
  6245. (setq beg (point-min) end (point-max)))
  6246. ((memq state '(children folded))
  6247. (setq beg (point) end (progn (org-end-of-subtree t t)
  6248. (beginning-of-line 2)
  6249. (point)))))
  6250. (when beg
  6251. (goto-char beg)
  6252. (while (re-search-forward re end t)
  6253. (unless (get-char-property (match-end 1) 'invisible)
  6254. (setq e (match-end 1))
  6255. (if (< org-cycle-separator-lines 0)
  6256. (setq b (save-excursion
  6257. (goto-char (match-beginning 0))
  6258. (org-back-over-empty-lines)
  6259. (if (save-excursion
  6260. (goto-char (max (point-min) (1- (point))))
  6261. (org-at-heading-p))
  6262. (1- (point))
  6263. (point))))
  6264. (setq b (match-beginning 1)))
  6265. (outline-flag-region b e nil)))))))
  6266. ;; Never hide empty lines at the end of the file.
  6267. (save-excursion
  6268. (goto-char (point-max))
  6269. (outline-previous-heading)
  6270. (outline-end-of-heading)
  6271. (if (and (looking-at "[ \t\n]+")
  6272. (= (match-end 0) (point-max)))
  6273. (outline-flag-region (point) (match-end 0) nil))))
  6274. (defun org-show-empty-lines-in-parent ()
  6275. "Move to the parent and re-show empty lines before visible headlines."
  6276. (save-excursion
  6277. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6278. (org-cycle-show-empty-lines context))))
  6279. (defun org-files-list ()
  6280. "Return `org-agenda-files' list, plus all open org-mode files.
  6281. This is useful for operations that need to scan all of a user's
  6282. open and agenda-wise Org files."
  6283. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6284. (dolist (buf (buffer-list))
  6285. (with-current-buffer buf
  6286. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6287. (let ((file (expand-file-name (buffer-file-name))))
  6288. (unless (member file files)
  6289. (push file files))))))
  6290. files))
  6291. (defsubst org-entry-beginning-position ()
  6292. "Return the beginning position of the current entry."
  6293. (save-excursion (outline-back-to-heading t) (point)))
  6294. (defsubst org-entry-end-position ()
  6295. "Return the end position of the current entry."
  6296. (save-excursion (outline-next-heading) (point)))
  6297. (defun org-cycle-hide-drawers (state &optional exceptions)
  6298. "Re-hide all drawers after a visibility state change.
  6299. When non-nil, optional argument EXCEPTIONS is a list of strings
  6300. specifying which drawers should not be hidden."
  6301. (when (and (derived-mode-p 'org-mode)
  6302. (not (memq state '(overview folded contents))))
  6303. (save-excursion
  6304. (let* ((globalp (memq state '(contents all)))
  6305. (beg (if globalp (point-min) (point)))
  6306. (end (if globalp (point-max)
  6307. (if (eq state 'children)
  6308. (save-excursion (outline-next-heading) (point))
  6309. (org-end-of-subtree t)))))
  6310. (goto-char beg)
  6311. (while (re-search-forward org-drawer-regexp end t)
  6312. (unless (member-ignore-case (match-string 1) exceptions)
  6313. (org-flag-drawer t)))))))
  6314. (defun org-cycle-hide-inline-tasks (state)
  6315. "Re-hide inline task when switching to 'contents visibility state."
  6316. (when (and (eq state 'contents)
  6317. (boundp 'org-inlinetask-min-level)
  6318. org-inlinetask-min-level)
  6319. (hide-sublevels (1- org-inlinetask-min-level))))
  6320. (defun org-flag-drawer (flag &optional element)
  6321. "When FLAG is non-nil, hide the drawer we are at.
  6322. Otherwise make it visible. When optional argument ELEMENT is
  6323. a parsed drawer, as returned by `org-element-at-point', hide or
  6324. show that drawer instead."
  6325. (let ((drawer (or element (org-element-at-point))))
  6326. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6327. (save-excursion
  6328. (goto-char (org-element-property :post-affiliated drawer))
  6329. (outline-flag-region
  6330. (line-end-position)
  6331. (progn (goto-char (org-element-property :end drawer))
  6332. (skip-chars-backward " \r\t\n")
  6333. (line-end-position))
  6334. flag)))))
  6335. (defun org-subtree-end-visible-p ()
  6336. "Is the end of the current subtree visible?"
  6337. (pos-visible-in-window-p
  6338. (save-excursion (org-end-of-subtree t) (point))))
  6339. (defun org-first-headline-recenter (&optional N)
  6340. "Move cursor to the first headline and recenter the headline.
  6341. Optional argument N means put the headline into the Nth line of the window."
  6342. (goto-char (point-min))
  6343. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6344. (beginning-of-line)
  6345. (recenter (prefix-numeric-value N))))
  6346. ;;; Saving and restoring visibility
  6347. (defun org-outline-overlay-data (&optional use-markers)
  6348. "Return a list of the locations of all outline overlays.
  6349. These are overlays with the `invisible' property value `outline'.
  6350. The return value is a list of cons cells, with start and stop
  6351. positions for each overlay.
  6352. If USE-MARKERS is set, return the positions as markers."
  6353. (let (beg end)
  6354. (save-excursion
  6355. (save-restriction
  6356. (widen)
  6357. (delq nil
  6358. (mapcar (lambda (o)
  6359. (when (eq (overlay-get o 'invisible) 'outline)
  6360. (setq beg (overlay-start o)
  6361. end (overlay-end o))
  6362. (and beg end (> end beg)
  6363. (if use-markers
  6364. (cons (move-marker (make-marker) beg)
  6365. (move-marker (make-marker) end))
  6366. (cons beg end)))))
  6367. (overlays-in (point-min) (point-max))))))))
  6368. (defun org-set-outline-overlay-data (data)
  6369. "Create visibility overlays for all positions in DATA.
  6370. DATA should have been made by `org-outline-overlay-data'."
  6371. (let (o)
  6372. (save-excursion
  6373. (save-restriction
  6374. (widen)
  6375. (show-all)
  6376. (mapc (lambda (c)
  6377. (outline-flag-region (car c) (cdr c) t))
  6378. data)))))
  6379. ;;; Folding of blocks
  6380. (defvar org-hide-block-overlays nil
  6381. "Overlays hiding blocks.")
  6382. (make-variable-buffer-local 'org-hide-block-overlays)
  6383. (defun org-block-map (function &optional start end)
  6384. "Call FUNCTION at the head of all source blocks in the current buffer.
  6385. Optional arguments START and END can be used to limit the range."
  6386. (let ((start (or start (point-min)))
  6387. (end (or end (point-max))))
  6388. (save-excursion
  6389. (goto-char start)
  6390. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6391. (save-excursion
  6392. (save-match-data
  6393. (goto-char (match-beginning 0))
  6394. (funcall function)))))))
  6395. (defun org-hide-block-toggle-all ()
  6396. "Toggle the visibility of all blocks in the current buffer."
  6397. (org-block-map #'org-hide-block-toggle))
  6398. (defun org-hide-block-all ()
  6399. "Fold all blocks in the current buffer."
  6400. (interactive)
  6401. (org-show-block-all)
  6402. (org-block-map #'org-hide-block-toggle-maybe))
  6403. (defun org-show-block-all ()
  6404. "Unfold all blocks in the current buffer."
  6405. (interactive)
  6406. (mapc 'delete-overlay org-hide-block-overlays)
  6407. (setq org-hide-block-overlays nil))
  6408. (defun org-hide-block-toggle-maybe ()
  6409. "Toggle visibility of block at point."
  6410. (interactive)
  6411. (let ((case-fold-search t))
  6412. (if (save-excursion
  6413. (beginning-of-line 1)
  6414. (looking-at org-block-regexp))
  6415. (progn (org-hide-block-toggle)
  6416. t) ;; to signal that we took action
  6417. nil))) ;; to signal that we did not
  6418. (defun org-hide-block-toggle (&optional force)
  6419. "Toggle the visibility of the current block."
  6420. (interactive)
  6421. (save-excursion
  6422. (beginning-of-line)
  6423. (if (re-search-forward org-block-regexp nil t)
  6424. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6425. (end (match-end 0)) ;; end of entire body
  6426. ov)
  6427. (if (memq t (mapcar (lambda (overlay)
  6428. (eq (overlay-get overlay 'invisible)
  6429. 'org-hide-block))
  6430. (overlays-at start)))
  6431. (if (or (not force) (eq force 'off))
  6432. (mapc (lambda (ov)
  6433. (when (member ov org-hide-block-overlays)
  6434. (setq org-hide-block-overlays
  6435. (delq ov org-hide-block-overlays)))
  6436. (when (eq (overlay-get ov 'invisible)
  6437. 'org-hide-block)
  6438. (delete-overlay ov)))
  6439. (overlays-at start)))
  6440. (setq ov (make-overlay start end))
  6441. (overlay-put ov 'invisible 'org-hide-block)
  6442. ;; make the block accessible to isearch
  6443. (overlay-put
  6444. ov 'isearch-open-invisible
  6445. (lambda (ov)
  6446. (when (member ov org-hide-block-overlays)
  6447. (setq org-hide-block-overlays
  6448. (delq ov org-hide-block-overlays)))
  6449. (when (eq (overlay-get ov 'invisible)
  6450. 'org-hide-block)
  6451. (delete-overlay ov))))
  6452. (push ov org-hide-block-overlays)))
  6453. (user-error "Not looking at a source block"))))
  6454. ;; org-tab-after-check-for-cycling-hook
  6455. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6456. ;; Remove overlays when changing major mode
  6457. (add-hook 'org-mode-hook
  6458. (lambda () (org-add-hook 'change-major-mode-hook
  6459. 'org-show-block-all 'append 'local)))
  6460. ;;; Org-goto
  6461. (defvar org-goto-window-configuration nil)
  6462. (defvar org-goto-marker nil)
  6463. (defvar org-goto-map)
  6464. (defun org-goto-map ()
  6465. "Set the keymap `org-goto'."
  6466. (setq org-goto-map
  6467. (let ((map (make-sparse-keymap)))
  6468. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6469. mouse-drag-region universal-argument org-occur))
  6470. cmd)
  6471. (while (setq cmd (pop cmds))
  6472. (substitute-key-definition cmd cmd map global-map)))
  6473. (suppress-keymap map)
  6474. (org-defkey map "\C-m" 'org-goto-ret)
  6475. (org-defkey map [(return)] 'org-goto-ret)
  6476. (org-defkey map [(left)] 'org-goto-left)
  6477. (org-defkey map [(right)] 'org-goto-right)
  6478. (org-defkey map [(control ?g)] 'org-goto-quit)
  6479. (org-defkey map "\C-i" 'org-cycle)
  6480. (org-defkey map [(tab)] 'org-cycle)
  6481. (org-defkey map [(down)] 'outline-next-visible-heading)
  6482. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6483. (if org-goto-auto-isearch
  6484. (if (fboundp 'define-key-after)
  6485. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6486. nil)
  6487. (org-defkey map "q" 'org-goto-quit)
  6488. (org-defkey map "n" 'outline-next-visible-heading)
  6489. (org-defkey map "p" 'outline-previous-visible-heading)
  6490. (org-defkey map "f" 'outline-forward-same-level)
  6491. (org-defkey map "b" 'outline-backward-same-level)
  6492. (org-defkey map "u" 'outline-up-heading))
  6493. (org-defkey map "/" 'org-occur)
  6494. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6495. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6496. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6497. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6498. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6499. map)))
  6500. (defconst org-goto-help
  6501. "Browse buffer copy, to find location or copy text.%s
  6502. RET=jump to location C-g=quit and return to previous location
  6503. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6504. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6505. (defun org-goto (&optional alternative-interface)
  6506. "Look up a different location in the current file, keeping current visibility.
  6507. When you want look-up or go to a different location in a
  6508. document, the fastest way is often to fold the entire buffer and
  6509. then dive into the tree. This method has the disadvantage, that
  6510. the previous location will be folded, which may not be what you
  6511. want.
  6512. This command works around this by showing a copy of the current
  6513. buffer in an indirect buffer, in overview mode. You can dive
  6514. into the tree in that copy, use org-occur and incremental search
  6515. to find a location. When pressing RET or `Q', the command
  6516. returns to the original buffer in which the visibility is still
  6517. unchanged. After RET it will also jump to the location selected
  6518. in the indirect buffer and expose the headline hierarchy above.
  6519. With a prefix argument, use the alternative interface: e.g. if
  6520. `org-goto-interface' is 'outline use 'outline-path-completion."
  6521. (interactive "P")
  6522. (org-goto-map)
  6523. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6524. (org-refile-use-outline-path t)
  6525. (org-refile-target-verify-function nil)
  6526. (interface
  6527. (if (not alternative-interface)
  6528. org-goto-interface
  6529. (if (eq org-goto-interface 'outline)
  6530. 'outline-path-completion
  6531. 'outline)))
  6532. (org-goto-start-pos (point))
  6533. (selected-point
  6534. (if (eq interface 'outline)
  6535. (car (org-get-location (current-buffer) org-goto-help))
  6536. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6537. (org-refile-check-position pa)
  6538. (nth 3 pa)))))
  6539. (if selected-point
  6540. (progn
  6541. (org-mark-ring-push org-goto-start-pos)
  6542. (goto-char selected-point)
  6543. (if (or (outline-invisible-p) (org-invisible-p2))
  6544. (org-show-context 'org-goto)))
  6545. (message "Quit"))))
  6546. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6547. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6548. (defvar org-goto-local-auto-isearch-map) ; defined below
  6549. (defun org-get-location (buf help)
  6550. "Let the user select a location in the Org-mode buffer BUF.
  6551. This function uses a recursive edit. It returns the selected position
  6552. or nil."
  6553. (org-no-popups
  6554. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6555. (isearch-hide-immediately nil)
  6556. (isearch-search-fun-function
  6557. (lambda () 'org-goto-local-search-headings))
  6558. (org-goto-selected-point org-goto-exit-command))
  6559. (save-excursion
  6560. (save-window-excursion
  6561. (delete-other-windows)
  6562. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6563. (org-pop-to-buffer-same-window
  6564. (condition-case nil
  6565. (make-indirect-buffer (current-buffer) "*org-goto*")
  6566. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6567. (with-output-to-temp-buffer "*Org Help*"
  6568. (princ (format help (if org-goto-auto-isearch
  6569. " Just type for auto-isearch."
  6570. " n/p/f/b/u to navigate, q to quit."))))
  6571. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6572. (setq buffer-read-only nil)
  6573. (let ((org-startup-truncated t)
  6574. (org-startup-folded nil)
  6575. (org-startup-align-all-tables nil))
  6576. (org-mode)
  6577. (org-overview))
  6578. (setq buffer-read-only t)
  6579. (if (and (boundp 'org-goto-start-pos)
  6580. (integer-or-marker-p org-goto-start-pos))
  6581. (let ((org-show-hierarchy-above t)
  6582. (org-show-siblings t)
  6583. (org-show-following-heading t))
  6584. (goto-char org-goto-start-pos)
  6585. (and (outline-invisible-p) (org-show-context)))
  6586. (goto-char (point-min)))
  6587. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6588. (message "Select location and press RET")
  6589. (use-local-map org-goto-map)
  6590. (recursive-edit)))
  6591. (kill-buffer "*org-goto*")
  6592. (cons org-goto-selected-point org-goto-exit-command))))
  6593. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6594. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6595. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6596. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6597. (defun org-goto-local-search-headings (string bound noerror)
  6598. "Search and make sure that any matches are in headlines."
  6599. (catch 'return
  6600. (while (if isearch-forward
  6601. (search-forward string bound noerror)
  6602. (search-backward string bound noerror))
  6603. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6604. (and (member :headline context)
  6605. (not (member :tags context))))
  6606. (throw 'return (point))))))
  6607. (defun org-goto-local-auto-isearch ()
  6608. "Start isearch."
  6609. (interactive)
  6610. (goto-char (point-min))
  6611. (let ((keys (this-command-keys)))
  6612. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6613. (isearch-mode t)
  6614. (isearch-process-search-char (string-to-char keys)))))
  6615. (defun org-goto-ret (&optional arg)
  6616. "Finish `org-goto' by going to the new location."
  6617. (interactive "P")
  6618. (setq org-goto-selected-point (point)
  6619. org-goto-exit-command 'return)
  6620. (throw 'exit nil))
  6621. (defun org-goto-left ()
  6622. "Finish `org-goto' by going to the new location."
  6623. (interactive)
  6624. (if (org-at-heading-p)
  6625. (progn
  6626. (beginning-of-line 1)
  6627. (setq org-goto-selected-point (point)
  6628. org-goto-exit-command 'left)
  6629. (throw 'exit nil))
  6630. (user-error "Not on a heading")))
  6631. (defun org-goto-right ()
  6632. "Finish `org-goto' by going to the new location."
  6633. (interactive)
  6634. (if (org-at-heading-p)
  6635. (progn
  6636. (setq org-goto-selected-point (point)
  6637. org-goto-exit-command 'right)
  6638. (throw 'exit nil))
  6639. (user-error "Not on a heading")))
  6640. (defun org-goto-quit ()
  6641. "Finish `org-goto' without cursor motion."
  6642. (interactive)
  6643. (setq org-goto-selected-point nil)
  6644. (setq org-goto-exit-command 'quit)
  6645. (throw 'exit nil))
  6646. ;;; Indirect buffer display of subtrees
  6647. (defvar org-indirect-dedicated-frame nil
  6648. "This is the frame being used for indirect tree display.")
  6649. (defvar org-last-indirect-buffer nil)
  6650. (defun org-tree-to-indirect-buffer (&optional arg)
  6651. "Create indirect buffer and narrow it to current subtree.
  6652. With a numerical prefix ARG, go up to this level and then take that tree.
  6653. If ARG is negative, go up that many levels.
  6654. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6655. indirect buffer previously made with this command, to avoid proliferation of
  6656. indirect buffers. However, when you call the command with a \
  6657. \\[universal-argument] prefix, or
  6658. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6659. is kept so that you can work with several indirect buffers at the same time.
  6660. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6661. \\[universal-argument] prefix also
  6662. requests that a new frame be made for the new buffer, so that the dedicated
  6663. frame is not changed."
  6664. (interactive "P")
  6665. (let ((cbuf (current-buffer))
  6666. (cwin (selected-window))
  6667. (pos (point))
  6668. beg end level heading ibuf)
  6669. (save-excursion
  6670. (org-back-to-heading t)
  6671. (when (numberp arg)
  6672. (setq level (org-outline-level))
  6673. (if (< arg 0) (setq arg (+ level arg)))
  6674. (while (> (setq level (org-outline-level)) arg)
  6675. (org-up-heading-safe)))
  6676. (setq beg (point)
  6677. heading (org-get-heading))
  6678. (org-end-of-subtree t t)
  6679. (if (org-at-heading-p) (backward-char 1))
  6680. (setq end (point)))
  6681. (if (and (buffer-live-p org-last-indirect-buffer)
  6682. (not (eq org-indirect-buffer-display 'new-frame))
  6683. (not arg))
  6684. (kill-buffer org-last-indirect-buffer))
  6685. (setq ibuf (org-get-indirect-buffer cbuf)
  6686. org-last-indirect-buffer ibuf)
  6687. (cond
  6688. ((or (eq org-indirect-buffer-display 'new-frame)
  6689. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6690. (select-frame (make-frame))
  6691. (delete-other-windows)
  6692. (org-pop-to-buffer-same-window ibuf)
  6693. (org-set-frame-title heading))
  6694. ((eq org-indirect-buffer-display 'dedicated-frame)
  6695. (raise-frame
  6696. (select-frame (or (and org-indirect-dedicated-frame
  6697. (frame-live-p org-indirect-dedicated-frame)
  6698. org-indirect-dedicated-frame)
  6699. (setq org-indirect-dedicated-frame (make-frame)))))
  6700. (delete-other-windows)
  6701. (org-pop-to-buffer-same-window ibuf)
  6702. (org-set-frame-title (concat "Indirect: " heading)))
  6703. ((eq org-indirect-buffer-display 'current-window)
  6704. (org-pop-to-buffer-same-window ibuf))
  6705. ((eq org-indirect-buffer-display 'other-window)
  6706. (pop-to-buffer ibuf))
  6707. (t (error "Invalid value")))
  6708. (if (featurep 'xemacs)
  6709. (save-excursion (org-mode) (turn-on-font-lock)))
  6710. (narrow-to-region beg end)
  6711. (show-all)
  6712. (goto-char pos)
  6713. (run-hook-with-args 'org-cycle-hook 'all)
  6714. (and (window-live-p cwin) (select-window cwin))))
  6715. (defun org-get-indirect-buffer (&optional buffer)
  6716. (setq buffer (or buffer (current-buffer)))
  6717. (let ((n 1) (base (buffer-name buffer)) bname)
  6718. (while (buffer-live-p
  6719. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6720. (setq n (1+ n)))
  6721. (condition-case nil
  6722. (make-indirect-buffer buffer bname 'clone)
  6723. (error (make-indirect-buffer buffer bname)))))
  6724. (defun org-set-frame-title (title)
  6725. "Set the title of the current frame to the string TITLE."
  6726. ;; FIXME: how to name a single frame in XEmacs???
  6727. (unless (featurep 'xemacs)
  6728. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6729. ;;;; Structure editing
  6730. ;;; Inserting headlines
  6731. (defun org-previous-line-empty-p (&optional next)
  6732. "Is the previous line a blank line?
  6733. When NEXT is non-nil, check the next line instead."
  6734. (save-excursion
  6735. (and (not (bobp))
  6736. (or (beginning-of-line (if next 2 0)) t)
  6737. (save-match-data
  6738. (looking-at "[ \t]*$")))))
  6739. (defun org-insert-heading (&optional arg invisible-ok)
  6740. "Insert a new heading or item with same depth at point.
  6741. If point is in a plain list and ARG is nil, create a new list item.
  6742. With one universal prefix argument, insert a heading even in lists.
  6743. With two universal prefix arguments, insert the heading at the end
  6744. of the parent subtree.
  6745. If point is at the beginning of a headline, insert a sibling before
  6746. the current headline. If point is not at the beginning, split the line
  6747. and create a new headline with the text in the current line after point
  6748. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6749. If point is at the beginning of a normal line, turn this line into
  6750. a heading.
  6751. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6752. This is important for non-interactive uses of the command."
  6753. (interactive "P")
  6754. (if (org-called-interactively-p 'any) (org-reveal))
  6755. (let ((itemp (org-in-item-p))
  6756. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6757. (respect-content (or org-insert-heading-respect-content
  6758. (equal arg '(16))))
  6759. (initial-content "")
  6760. (adjust-empty-lines t))
  6761. (cond
  6762. ((or (= (buffer-size) 0)
  6763. (and (not (save-excursion
  6764. (and (ignore-errors (org-back-to-heading invisible-ok))
  6765. (org-at-heading-p))))
  6766. (or arg (not itemp))))
  6767. ;; At beginning of buffer or so high up that only a heading
  6768. ;; makes sense.
  6769. (insert
  6770. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6771. (if (org-in-src-block-p) ",* " "* "))
  6772. (run-hooks 'org-insert-heading-hook))
  6773. ((and itemp (not (equal arg '(4))))
  6774. ;; Insert an item
  6775. (org-insert-item))
  6776. (t
  6777. ;; Insert a heading
  6778. (save-restriction
  6779. (widen)
  6780. (let* ((level nil)
  6781. (on-heading (org-at-heading-p))
  6782. (empty-line-p (if on-heading
  6783. (org-previous-line-empty-p)
  6784. ;; We will decide later
  6785. nil))
  6786. ;; Get a level string to fall back on
  6787. (fix-level
  6788. (save-excursion
  6789. (org-back-to-heading t)
  6790. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6791. (looking-at org-outline-regexp)
  6792. (make-string (1- (length (match-string 0))) ?*)))
  6793. (stars
  6794. (save-excursion
  6795. (condition-case nil
  6796. (progn
  6797. (org-back-to-heading invisible-ok)
  6798. (when (and (not on-heading)
  6799. (featurep 'org-inlinetask)
  6800. (integerp org-inlinetask-min-level)
  6801. (>= (length (match-string 0))
  6802. org-inlinetask-min-level))
  6803. ;; Find a heading level before the inline task
  6804. (while (and (setq level (org-up-heading-safe))
  6805. (>= level org-inlinetask-min-level)))
  6806. (if (org-at-heading-p)
  6807. (org-back-to-heading invisible-ok)
  6808. (error "This should not happen")))
  6809. (unless (and (save-excursion
  6810. (save-match-data
  6811. (org-backward-heading-same-level
  6812. 1 invisible-ok))
  6813. (= (point) (match-beginning 0)))
  6814. (not (org-previous-line-empty-p t)))
  6815. (setq empty-line-p (or empty-line-p
  6816. (org-previous-line-empty-p))))
  6817. (match-string 0))
  6818. (error (or fix-level "* ")))))
  6819. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6820. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6821. pos hide-previous previous-pos)
  6822. ;; If we insert after content, move there and clean up whitespace
  6823. (when respect-content
  6824. (org-end-of-subtree nil t)
  6825. (skip-chars-backward " \r\n")
  6826. (and (looking-at "[ \t]+") (replace-match ""))
  6827. (unless (eobp) (forward-char 1))
  6828. (when (looking-at "^\\*")
  6829. (unless (bobp) (backward-char 1))
  6830. (insert "\n")))
  6831. ;; If we are splitting, grab the text that should be moved to the new headline
  6832. (when may-split
  6833. (if (org-on-heading-p)
  6834. ;; This is a heading, we split intelligently (keeping tags)
  6835. (let ((pos (point)))
  6836. (goto-char (point-at-bol))
  6837. (unless (looking-at org-complex-heading-regexp)
  6838. (error "This should not happen"))
  6839. (when (and (match-beginning 4)
  6840. (> pos (match-beginning 4))
  6841. (< pos (match-end 4)))
  6842. (setq initial-content (buffer-substring pos (match-end 4)))
  6843. (goto-char pos)
  6844. (delete-region (point) (match-end 4))
  6845. (if (looking-at "[ \t]*$")
  6846. (replace-match "")
  6847. (insert (make-string (length initial-content) ?\ )))
  6848. (setq initial-content (org-trim initial-content)))
  6849. (goto-char pos))
  6850. ;; a normal line
  6851. (unless (bolp)
  6852. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6853. (delete-region (point) (point-at-eol))
  6854. (setq initial-content (org-trim initial-content)))))
  6855. ;; If we are at the beginning of the line, insert before it. Else after
  6856. (cond
  6857. ((and (bolp) (looking-at "[ \t]*$")))
  6858. ((and (bolp) (not (looking-at "[ \t]*$")))
  6859. (open-line 1))
  6860. (t
  6861. (goto-char (point-at-eol))
  6862. (insert "\n")))
  6863. ;; Insert the new heading
  6864. (insert stars)
  6865. (just-one-space)
  6866. (insert initial-content)
  6867. (when adjust-empty-lines
  6868. (if (or (not blank)
  6869. (and blank (not (org-previous-line-empty-p))))
  6870. (org-N-empty-lines-before-current (if blank 1 0))))
  6871. (run-hooks 'org-insert-heading-hook)))))))
  6872. (defun org-N-empty-lines-before-current (N)
  6873. "Make the number of empty lines before current exactly N.
  6874. So this will delete or add empty lines."
  6875. (save-excursion
  6876. (goto-char (point-at-bol))
  6877. (if (looking-back "\\s-+" nil 'greedy)
  6878. (replace-match ""))
  6879. (or (bobp) (insert "\n"))
  6880. (while (> N 0)
  6881. (insert "\n")
  6882. (setq N (1- N)))))
  6883. (defun org-get-heading (&optional no-tags no-todo)
  6884. "Return the heading of the current entry, without the stars.
  6885. When NO-TAGS is non-nil, don't include tags.
  6886. When NO-TODO is non-nil, don't include TODO keywords."
  6887. (save-excursion
  6888. (org-back-to-heading t)
  6889. (cond
  6890. ((and no-tags no-todo)
  6891. (looking-at org-complex-heading-regexp)
  6892. (match-string 4))
  6893. (no-tags
  6894. (looking-at (concat org-outline-regexp
  6895. "\\(.*?\\)"
  6896. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6897. (match-string 1))
  6898. (no-todo
  6899. (looking-at org-todo-line-regexp)
  6900. (match-string 3))
  6901. (t (looking-at org-heading-regexp)
  6902. (match-string 2)))))
  6903. (defvar orgstruct-mode) ; defined below
  6904. (defun org-heading-components ()
  6905. "Return the components of the current heading.
  6906. This is a list with the following elements:
  6907. - the level as an integer
  6908. - the reduced level, different if `org-odd-levels-only' is set.
  6909. - the TODO keyword, or nil
  6910. - the priority character, like ?A, or nil if no priority is given
  6911. - the headline text itself, or the tags string if no headline text
  6912. - the tags string, or nil."
  6913. (save-excursion
  6914. (org-back-to-heading t)
  6915. (if (let (case-fold-search)
  6916. (looking-at
  6917. (if orgstruct-mode
  6918. org-heading-regexp
  6919. org-complex-heading-regexp)))
  6920. (if orgstruct-mode
  6921. (list (length (match-string 1))
  6922. (org-reduced-level (length (match-string 1)))
  6923. nil
  6924. nil
  6925. (match-string 2)
  6926. nil)
  6927. (list (length (match-string 1))
  6928. (org-reduced-level (length (match-string 1)))
  6929. (org-match-string-no-properties 2)
  6930. (and (match-end 3) (aref (match-string 3) 2))
  6931. (org-match-string-no-properties 4)
  6932. (org-match-string-no-properties 5))))))
  6933. (defun org-get-entry ()
  6934. "Get the entry text, after heading, entire subtree."
  6935. (save-excursion
  6936. (org-back-to-heading t)
  6937. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6938. (defun org-insert-heading-after-current ()
  6939. "Insert a new heading with same level as current, after current subtree."
  6940. (interactive)
  6941. (org-back-to-heading)
  6942. (org-insert-heading)
  6943. (org-move-subtree-down)
  6944. (end-of-line 1))
  6945. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6946. "Insert heading with `org-insert-heading-respect-content' set to t."
  6947. (interactive "P")
  6948. (let ((org-insert-heading-respect-content t))
  6949. (org-insert-heading '(4) invisible-ok)))
  6950. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6951. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6952. (interactive "P")
  6953. (let ((org-insert-heading-respect-content t))
  6954. (org-insert-todo-heading force-state '(4))))
  6955. (defun org-insert-todo-heading (arg &optional force-heading)
  6956. "Insert a new heading with the same level and TODO state as current heading.
  6957. If the heading has no TODO state, or if the state is DONE, use the first
  6958. state (TODO by default). Also one prefix arg, force first state. With two
  6959. prefix args, force inserting at the end of the parent subtree."
  6960. (interactive "P")
  6961. (when (or force-heading (not (org-insert-item 'checkbox)))
  6962. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6963. force-heading))
  6964. (save-excursion
  6965. (org-back-to-heading)
  6966. (outline-previous-heading)
  6967. (looking-at org-todo-line-regexp))
  6968. (let*
  6969. ((new-mark-x
  6970. (if (or arg
  6971. (not (match-beginning 2))
  6972. (member (match-string 2) org-done-keywords))
  6973. (car org-todo-keywords-1)
  6974. (match-string 2)))
  6975. (new-mark
  6976. (or
  6977. (run-hook-with-args-until-success
  6978. 'org-todo-get-default-hook new-mark-x nil)
  6979. new-mark-x)))
  6980. (beginning-of-line 1)
  6981. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6982. (if org-treat-insert-todo-heading-as-state-change
  6983. (org-todo new-mark)
  6984. (insert new-mark " "))))
  6985. (when org-provide-todo-statistics
  6986. (org-update-parent-todo-statistics))))
  6987. (defun org-insert-subheading (arg)
  6988. "Insert a new subheading and demote it.
  6989. Works for outline headings and for plain lists alike."
  6990. (interactive "P")
  6991. (org-insert-heading arg)
  6992. (cond
  6993. ((org-at-heading-p) (org-do-demote))
  6994. ((org-at-item-p) (org-indent-item))))
  6995. (defun org-insert-todo-subheading (arg)
  6996. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6997. Works for outline headings and for plain lists alike."
  6998. (interactive "P")
  6999. (org-insert-todo-heading arg)
  7000. (cond
  7001. ((org-at-heading-p) (org-do-demote))
  7002. ((org-at-item-p) (org-indent-item))))
  7003. ;;; Promotion and Demotion
  7004. (defvar org-after-demote-entry-hook nil
  7005. "Hook run after an entry has been demoted.
  7006. The cursor will be at the beginning of the entry.
  7007. When a subtree is being demoted, the hook will be called for each node.")
  7008. (defvar org-after-promote-entry-hook nil
  7009. "Hook run after an entry has been promoted.
  7010. The cursor will be at the beginning of the entry.
  7011. When a subtree is being promoted, the hook will be called for each node.")
  7012. (defun org-promote-subtree ()
  7013. "Promote the entire subtree.
  7014. See also `org-promote'."
  7015. (interactive)
  7016. (save-excursion
  7017. (org-with-limited-levels (org-map-tree 'org-promote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-demote-subtree ()
  7020. "Demote the entire subtree. See `org-demote'.
  7021. See also `org-promote'."
  7022. (interactive)
  7023. (save-excursion
  7024. (org-with-limited-levels (org-map-tree 'org-demote)))
  7025. (org-fix-position-after-promote))
  7026. (defun org-do-promote ()
  7027. "Promote the current heading higher up the tree.
  7028. If the region is active in `transient-mark-mode', promote all headings
  7029. in the region."
  7030. (interactive)
  7031. (save-excursion
  7032. (if (org-region-active-p)
  7033. (org-map-region 'org-promote (region-beginning) (region-end))
  7034. (org-promote)))
  7035. (org-fix-position-after-promote))
  7036. (defun org-do-demote ()
  7037. "Demote the current heading lower down the tree.
  7038. If the region is active in `transient-mark-mode', demote all headings
  7039. in the region."
  7040. (interactive)
  7041. (save-excursion
  7042. (if (org-region-active-p)
  7043. (org-map-region 'org-demote (region-beginning) (region-end))
  7044. (org-demote)))
  7045. (org-fix-position-after-promote))
  7046. (defun org-fix-position-after-promote ()
  7047. "Make sure that after pro/demotion cursor position is right."
  7048. (let ((pos (point)))
  7049. (when (save-excursion
  7050. (beginning-of-line 1)
  7051. (looking-at org-todo-line-regexp)
  7052. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7053. (cond ((eobp) (insert " "))
  7054. ((eolp) (insert " "))
  7055. ((equal (char-after) ?\ ) (forward-char 1))))))
  7056. (defun org-current-level ()
  7057. "Return the level of the current entry, or nil if before the first headline.
  7058. The level is the number of stars at the beginning of the headline."
  7059. (save-excursion
  7060. (org-with-limited-levels
  7061. (if (ignore-errors (org-back-to-heading t))
  7062. (funcall outline-level)))))
  7063. (defun org-get-previous-line-level ()
  7064. "Return the outline depth of the last headline before the current line.
  7065. Returns 0 for the first headline in the buffer, and nil if before the
  7066. first headline."
  7067. (let ((current-level (org-current-level))
  7068. (prev-level (when (> (line-number-at-pos) 1)
  7069. (save-excursion
  7070. (beginning-of-line 0)
  7071. (org-current-level)))))
  7072. (cond ((null current-level) nil) ; Before first headline
  7073. ((null prev-level) 0) ; At first headline
  7074. (prev-level))))
  7075. (defun org-reduced-level (l)
  7076. "Compute the effective level of a heading.
  7077. This takes into account the setting of `org-odd-levels-only'."
  7078. (cond
  7079. ((zerop l) 0)
  7080. (org-odd-levels-only (1+ (floor (/ l 2))))
  7081. (t l)))
  7082. (defun org-level-increment ()
  7083. "Return the number of stars that will be added or removed at a
  7084. time to headlines when structure editing, based on the value of
  7085. `org-odd-levels-only'."
  7086. (if org-odd-levels-only 2 1))
  7087. (defun org-get-valid-level (level &optional change)
  7088. "Rectify a level change under the influence of `org-odd-levels-only'
  7089. LEVEL is a current level, CHANGE is by how much the level should be
  7090. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7091. even level numbers will become the next higher odd number."
  7092. (if org-odd-levels-only
  7093. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7094. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7095. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7096. (max 1 (+ level (or change 0)))))
  7097. (if (boundp 'define-obsolete-function-alias)
  7098. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7099. (define-obsolete-function-alias 'org-get-legal-level
  7100. 'org-get-valid-level)
  7101. (define-obsolete-function-alias 'org-get-legal-level
  7102. 'org-get-valid-level "23.1")))
  7103. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7104. ;; ̀org-with-limited-levels'
  7105. (defun org-promote ()
  7106. "Promote the current heading higher up the tree.
  7107. If the region is active in `transient-mark-mode', promote all headings
  7108. in the region."
  7109. (org-back-to-heading t)
  7110. (let* ((level (save-match-data (funcall outline-level)))
  7111. (after-change-functions (remove 'flyspell-after-change-function
  7112. after-change-functions))
  7113. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7114. (diff (abs (- level (length up-head) -1))))
  7115. (cond ((and (= level 1) org-called-with-limited-levels
  7116. org-allow-promoting-top-level-subtree)
  7117. (replace-match "# " nil t))
  7118. ((= level 1)
  7119. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7120. (t (replace-match up-head nil t)))
  7121. ;; Fixup tag positioning
  7122. (unless (= level 1)
  7123. (and org-auto-align-tags (org-set-tags nil t))
  7124. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7125. (run-hooks 'org-after-promote-entry-hook)))
  7126. (defun org-demote ()
  7127. "Demote the current heading lower down the tree.
  7128. If the region is active in `transient-mark-mode', demote all headings
  7129. in the region."
  7130. (org-back-to-heading t)
  7131. (let* ((level (save-match-data (funcall outline-level)))
  7132. (after-change-functions (remove 'flyspell-after-change-function
  7133. after-change-functions))
  7134. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7135. (diff (abs (- level (length down-head) -1))))
  7136. (replace-match down-head nil t)
  7137. ;; Fixup tag positioning
  7138. (and org-auto-align-tags (org-set-tags nil t))
  7139. (if org-adapt-indentation (org-fixup-indentation diff))
  7140. (run-hooks 'org-after-demote-entry-hook)))
  7141. (defun org-cycle-level ()
  7142. "Cycle the level of an empty headline through possible states.
  7143. This goes first to child, then to parent, level, then up the hierarchy.
  7144. After top level, it switches back to sibling level."
  7145. (interactive)
  7146. (let ((org-adapt-indentation nil))
  7147. (when (org-point-at-end-of-empty-headline)
  7148. (setq this-command 'org-cycle-level) ; Only needed for caching
  7149. (let ((cur-level (org-current-level))
  7150. (prev-level (org-get-previous-line-level)))
  7151. (cond
  7152. ;; If first headline in file, promote to top-level.
  7153. ((= prev-level 0)
  7154. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7155. do (org-do-promote)))
  7156. ;; If same level as prev, demote one.
  7157. ((= prev-level cur-level)
  7158. (org-do-demote))
  7159. ;; If parent is top-level, promote to top level if not already.
  7160. ((= prev-level 1)
  7161. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7162. do (org-do-promote)))
  7163. ;; If top-level, return to prev-level.
  7164. ((= cur-level 1)
  7165. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7166. do (org-do-demote)))
  7167. ;; If less than prev-level, promote one.
  7168. ((< cur-level prev-level)
  7169. (org-do-promote))
  7170. ;; If deeper than prev-level, promote until higher than
  7171. ;; prev-level.
  7172. ((> cur-level prev-level)
  7173. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7174. do (org-do-promote))))
  7175. t))))
  7176. (defun org-map-tree (fun)
  7177. "Call FUN for every heading underneath the current one."
  7178. (org-back-to-heading)
  7179. (let ((level (funcall outline-level)))
  7180. (save-excursion
  7181. (funcall fun)
  7182. (while (and (progn
  7183. (outline-next-heading)
  7184. (> (funcall outline-level) level))
  7185. (not (eobp)))
  7186. (funcall fun)))))
  7187. (defun org-map-region (fun beg end)
  7188. "Call FUN for every heading between BEG and END."
  7189. (let ((org-ignore-region t))
  7190. (save-excursion
  7191. (setq end (copy-marker end))
  7192. (goto-char beg)
  7193. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7194. (< (point) end))
  7195. (funcall fun))
  7196. (while (and (progn
  7197. (outline-next-heading)
  7198. (< (point) end))
  7199. (not (eobp)))
  7200. (funcall fun)))))
  7201. (defvar org-property-end-re) ; silence byte-compiler
  7202. (defun org-fixup-indentation (diff)
  7203. "Change the indentation in the current entry by DIFF.
  7204. However, if any line in the current entry has no indentation, or if it
  7205. would end up with no indentation after the change, nothing at all is done."
  7206. (save-excursion
  7207. (let ((end (save-excursion (outline-next-heading)
  7208. (point-marker)))
  7209. (prohibit (if (> diff 0)
  7210. "^\\S-"
  7211. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7212. col)
  7213. (unless (save-excursion (end-of-line 1)
  7214. (re-search-forward prohibit end t))
  7215. (while (and (< (point) end)
  7216. (re-search-forward "^[ \t]+" end t))
  7217. (goto-char (match-end 0))
  7218. (setq col (current-column))
  7219. (if (< diff 0) (replace-match ""))
  7220. (org-indent-to-column (+ diff col))))
  7221. (move-marker end nil))))
  7222. (defun org-convert-to-odd-levels ()
  7223. "Convert an org-mode file with all levels allowed to one with odd levels.
  7224. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7225. level 5 etc."
  7226. (interactive)
  7227. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7228. (let ((outline-level 'org-outline-level)
  7229. (org-odd-levels-only nil) n)
  7230. (save-excursion
  7231. (goto-char (point-min))
  7232. (while (re-search-forward "^\\*\\*+ " nil t)
  7233. (setq n (- (length (match-string 0)) 2))
  7234. (while (>= (setq n (1- n)) 0)
  7235. (org-demote))
  7236. (end-of-line 1))))))
  7237. (defun org-convert-to-oddeven-levels ()
  7238. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7239. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7240. file contains a section with an even level, conversion would
  7241. destroy the structure of the file. An error is signaled in this
  7242. case."
  7243. (interactive)
  7244. (goto-char (point-min))
  7245. ;; First check if there are no even levels
  7246. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7247. (org-show-context t)
  7248. (error "Not all levels are odd in this file. Conversion not possible"))
  7249. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7250. (let ((outline-regexp org-outline-regexp)
  7251. (outline-level 'org-outline-level)
  7252. (org-odd-levels-only nil) n)
  7253. (save-excursion
  7254. (goto-char (point-min))
  7255. (while (re-search-forward "^\\*\\*+ " nil t)
  7256. (setq n (/ (1- (length (match-string 0))) 2))
  7257. (while (>= (setq n (1- n)) 0)
  7258. (org-promote))
  7259. (end-of-line 1))))))
  7260. (defun org-tr-level (n)
  7261. "Make N odd if required."
  7262. (if org-odd-levels-only (1+ (/ n 2)) n))
  7263. ;;; Vertical tree motion, cutting and pasting of subtrees
  7264. (defun org-move-subtree-up (&optional arg)
  7265. "Move the current subtree up past ARG headlines of the same level."
  7266. (interactive "p")
  7267. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7268. (defun org-move-subtree-down (&optional arg)
  7269. "Move the current subtree down past ARG headlines of the same level."
  7270. (interactive "p")
  7271. (setq arg (prefix-numeric-value arg))
  7272. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7273. 'org-get-last-sibling))
  7274. (ins-point (make-marker))
  7275. (cnt (abs arg))
  7276. (col (current-column))
  7277. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7278. ;; Select the tree
  7279. (org-back-to-heading)
  7280. (setq beg0 (point))
  7281. (save-excursion
  7282. (setq ne-beg (org-back-over-empty-lines))
  7283. (setq beg (point)))
  7284. (save-match-data
  7285. (save-excursion (outline-end-of-heading)
  7286. (setq folded (outline-invisible-p)))
  7287. (outline-end-of-subtree))
  7288. (outline-next-heading)
  7289. (setq ne-end (org-back-over-empty-lines))
  7290. (setq end (point))
  7291. (goto-char beg0)
  7292. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7293. ;; include less whitespace
  7294. (save-excursion
  7295. (goto-char beg)
  7296. (forward-line (- ne-beg ne-end))
  7297. (setq beg (point))))
  7298. ;; Find insertion point, with error handling
  7299. (while (> cnt 0)
  7300. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7301. (progn (goto-char beg0)
  7302. (user-error "Cannot move past superior level or buffer limit")))
  7303. (setq cnt (1- cnt)))
  7304. (if (> arg 0)
  7305. ;; Moving forward - still need to move over subtree
  7306. (progn (org-end-of-subtree t t)
  7307. (save-excursion
  7308. (org-back-over-empty-lines)
  7309. (or (bolp) (newline)))))
  7310. (setq ne-ins (org-back-over-empty-lines))
  7311. (move-marker ins-point (point))
  7312. (setq txt (buffer-substring beg end))
  7313. (org-save-markers-in-region beg end)
  7314. (delete-region beg end)
  7315. (org-remove-empty-overlays-at beg)
  7316. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7317. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7318. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7319. (let ((bbb (point)))
  7320. (insert-before-markers txt)
  7321. (org-reinstall-markers-in-region bbb)
  7322. (move-marker ins-point bbb))
  7323. (or (bolp) (insert "\n"))
  7324. (setq ins-end (point))
  7325. (goto-char ins-point)
  7326. (org-skip-whitespace)
  7327. (when (and (< arg 0)
  7328. (org-first-sibling-p)
  7329. (> ne-ins ne-beg))
  7330. ;; Move whitespace back to beginning
  7331. (save-excursion
  7332. (goto-char ins-end)
  7333. (let ((kill-whole-line t))
  7334. (kill-line (- ne-ins ne-beg)) (point)))
  7335. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7336. (move-marker ins-point nil)
  7337. (if folded
  7338. (hide-subtree)
  7339. (org-show-entry)
  7340. (show-children)
  7341. (org-cycle-hide-drawers 'children))
  7342. (org-clean-visibility-after-subtree-move)
  7343. ;; move back to the initial column we were at
  7344. (move-to-column col)))
  7345. (defvar org-subtree-clip ""
  7346. "Clipboard for cut and paste of subtrees.
  7347. This is actually only a copy of the kill, because we use the normal kill
  7348. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7349. (defvar org-subtree-clip-folded nil
  7350. "Was the last copied subtree folded?
  7351. This is used to fold the tree back after pasting.")
  7352. (defun org-cut-subtree (&optional n)
  7353. "Cut the current subtree into the clipboard.
  7354. With prefix arg N, cut this many sequential subtrees.
  7355. This is a short-hand for marking the subtree and then cutting it."
  7356. (interactive "p")
  7357. (org-copy-subtree n 'cut))
  7358. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7359. "Copy the current subtree it in the clipboard.
  7360. With prefix arg N, copy this many sequential subtrees.
  7361. This is a short-hand for marking the subtree and then copying it.
  7362. If CUT is non-nil, actually cut the subtree.
  7363. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7364. of some markers in the region, even if CUT is non-nil. This is
  7365. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7366. (interactive "p")
  7367. (let (beg end folded (beg0 (point)))
  7368. (if (org-called-interactively-p 'any)
  7369. (org-back-to-heading nil) ; take what looks like a subtree
  7370. (org-back-to-heading t)) ; take what is really there
  7371. (setq beg (point))
  7372. (skip-chars-forward " \t\r\n")
  7373. (save-match-data
  7374. (if nosubtrees
  7375. (outline-next-heading)
  7376. (save-excursion (outline-end-of-heading)
  7377. (setq folded (outline-invisible-p)))
  7378. (condition-case nil
  7379. (org-forward-heading-same-level (1- n) t)
  7380. (error nil))
  7381. (org-end-of-subtree t t)))
  7382. (setq end (point))
  7383. (goto-char beg0)
  7384. (when (> end beg)
  7385. (setq org-subtree-clip-folded folded)
  7386. (when (or cut force-store-markers)
  7387. (org-save-markers-in-region beg end))
  7388. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7389. (setq org-subtree-clip (current-kill 0))
  7390. (message "%s: Subtree(s) with %d characters"
  7391. (if cut "Cut" "Copied")
  7392. (length org-subtree-clip)))))
  7393. (defun org-paste-subtree (&optional level tree for-yank)
  7394. "Paste the clipboard as a subtree, with modification of headline level.
  7395. The entire subtree is promoted or demoted in order to match a new headline
  7396. level.
  7397. If the cursor is at the beginning of a headline, the same level as
  7398. that headline is used to paste the tree.
  7399. If not, the new level is derived from the *visible* headings
  7400. before and after the insertion point, and taken to be the inferior headline
  7401. level of the two. So if the previous visible heading is level 3 and the
  7402. next is level 4 (or vice versa), level 4 will be used for insertion.
  7403. This makes sure that the subtree remains an independent subtree and does
  7404. not swallow low level entries.
  7405. You can also force a different level, either by using a numeric prefix
  7406. argument, or by inserting the heading marker by hand. For example, if the
  7407. cursor is after \"*****\", then the tree will be shifted to level 5.
  7408. If optional TREE is given, use this text instead of the kill ring.
  7409. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7410. move back over whitespace before inserting, and move point to the end of
  7411. the inserted text when done."
  7412. (interactive "P")
  7413. (setq tree (or tree (and kill-ring (current-kill 0))))
  7414. (unless (org-kill-is-subtree-p tree)
  7415. (user-error "%s"
  7416. (substitute-command-keys
  7417. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7418. (org-with-limited-levels
  7419. (let* ((visp (not (outline-invisible-p)))
  7420. (txt tree)
  7421. (^re_ "\\(\\*+\\)[ \t]*")
  7422. (old-level (if (string-match org-outline-regexp-bol txt)
  7423. (- (match-end 0) (match-beginning 0) 1)
  7424. -1))
  7425. (force-level (cond (level (prefix-numeric-value level))
  7426. ((and (looking-at "[ \t]*$")
  7427. (string-match
  7428. "^\\*+$" (buffer-substring
  7429. (point-at-bol) (point))))
  7430. (- (match-end 1) (match-beginning 1)))
  7431. ((and (bolp)
  7432. (looking-at org-outline-regexp))
  7433. (- (match-end 0) (point) 1))))
  7434. (previous-level (save-excursion
  7435. (condition-case nil
  7436. (progn
  7437. (outline-previous-visible-heading 1)
  7438. (if (looking-at ^re_)
  7439. (- (match-end 0) (match-beginning 0) 1)
  7440. 1))
  7441. (error 1))))
  7442. (next-level (save-excursion
  7443. (condition-case nil
  7444. (progn
  7445. (or (looking-at org-outline-regexp)
  7446. (outline-next-visible-heading 1))
  7447. (if (looking-at ^re_)
  7448. (- (match-end 0) (match-beginning 0) 1)
  7449. 1))
  7450. (error 1))))
  7451. (new-level (or force-level (max previous-level next-level)))
  7452. (shift (if (or (= old-level -1)
  7453. (= new-level -1)
  7454. (= old-level new-level))
  7455. 0
  7456. (- new-level old-level)))
  7457. (delta (if (> shift 0) -1 1))
  7458. (func (if (> shift 0) 'org-demote 'org-promote))
  7459. (org-odd-levels-only nil)
  7460. beg end newend)
  7461. ;; Remove the forced level indicator
  7462. (if force-level
  7463. (delete-region (point-at-bol) (point)))
  7464. ;; Paste
  7465. (beginning-of-line (if (bolp) 1 2))
  7466. (setq beg (point))
  7467. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7468. (insert-before-markers txt)
  7469. (unless (string-match "\n\\'" txt) (insert "\n"))
  7470. (setq newend (point))
  7471. (org-reinstall-markers-in-region beg)
  7472. (setq end (point))
  7473. (goto-char beg)
  7474. (skip-chars-forward " \t\n\r")
  7475. (setq beg (point))
  7476. (if (and (outline-invisible-p) visp)
  7477. (save-excursion (outline-show-heading)))
  7478. ;; Shift if necessary
  7479. (unless (= shift 0)
  7480. (save-restriction
  7481. (narrow-to-region beg end)
  7482. (while (not (= shift 0))
  7483. (org-map-region func (point-min) (point-max))
  7484. (setq shift (+ delta shift)))
  7485. (goto-char (point-min))
  7486. (setq newend (point-max))))
  7487. (when (or (org-called-interactively-p 'interactive) for-yank)
  7488. (message "Clipboard pasted as level %d subtree" new-level))
  7489. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7490. kill-ring
  7491. (eq org-subtree-clip (current-kill 0))
  7492. org-subtree-clip-folded)
  7493. ;; The tree was folded before it was killed/copied
  7494. (hide-subtree))
  7495. (and for-yank (goto-char newend)))))
  7496. (defun org-kill-is-subtree-p (&optional txt)
  7497. "Check if the current kill is an outline subtree, or a set of trees.
  7498. Returns nil if kill does not start with a headline, or if the first
  7499. headline level is not the largest headline level in the tree.
  7500. So this will actually accept several entries of equal levels as well,
  7501. which is OK for `org-paste-subtree'.
  7502. If optional TXT is given, check this string instead of the current kill."
  7503. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7504. (re (org-get-limited-outline-regexp))
  7505. (^re (concat "^" re))
  7506. (start-level (and kill
  7507. (string-match
  7508. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7509. kill)
  7510. (- (match-end 2) (match-beginning 2) 1)))
  7511. (start (1+ (or (match-beginning 2) -1))))
  7512. (if (not start-level)
  7513. (progn
  7514. nil) ;; does not even start with a heading
  7515. (catch 'exit
  7516. (while (setq start (string-match ^re kill (1+ start)))
  7517. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7518. (throw 'exit nil)))
  7519. t))))
  7520. (defvar org-markers-to-move nil
  7521. "Markers that should be moved with a cut-and-paste operation.
  7522. Those markers are stored together with their positions relative to
  7523. the start of the region.")
  7524. (defun org-save-markers-in-region (beg end)
  7525. "Check markers in region.
  7526. If these markers are between BEG and END, record their position relative
  7527. to BEG, so that after moving the block of text, we can put the markers back
  7528. into place.
  7529. This function gets called just before an entry or tree gets cut from the
  7530. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7531. called immediately, to move the markers with the entries."
  7532. (setq org-markers-to-move nil)
  7533. (when (featurep 'org-clock)
  7534. (org-clock-save-markers-for-cut-and-paste beg end))
  7535. (when (featurep 'org-agenda)
  7536. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7537. (defun org-check-and-save-marker (marker beg end)
  7538. "Check if MARKER is between BEG and END.
  7539. If yes, remember the marker and the distance to BEG."
  7540. (when (and (marker-buffer marker)
  7541. (equal (marker-buffer marker) (current-buffer)))
  7542. (if (and (>= marker beg) (< marker end))
  7543. (push (cons marker (- marker beg)) org-markers-to-move))))
  7544. (defun org-reinstall-markers-in-region (beg)
  7545. "Move all remembered markers to their position relative to BEG."
  7546. (mapc (lambda (x)
  7547. (move-marker (car x) (+ beg (cdr x))))
  7548. org-markers-to-move)
  7549. (setq org-markers-to-move nil))
  7550. (defun org-narrow-to-subtree ()
  7551. "Narrow buffer to the current subtree."
  7552. (interactive)
  7553. (save-excursion
  7554. (save-match-data
  7555. (org-with-limited-levels
  7556. (narrow-to-region
  7557. (progn (org-back-to-heading t) (point))
  7558. (progn (org-end-of-subtree t t)
  7559. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7560. (point)))))))
  7561. (defun org-narrow-to-block ()
  7562. "Narrow buffer to the current block."
  7563. (interactive)
  7564. (let* ((case-fold-search t)
  7565. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7566. "^[ \t]*#\\+end_.*")))
  7567. (if blockp
  7568. (narrow-to-region (car blockp) (cdr blockp))
  7569. (user-error "Not in a block"))))
  7570. (eval-when-compile
  7571. (defvar org-property-drawer-re))
  7572. (defvar org-property-start-re) ;; defined below
  7573. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7574. "Clone the task (subtree) at point N times.
  7575. The clones will be inserted as siblings.
  7576. In interactive use, the user will be prompted for the number of
  7577. clones to be produced. If the entry has a timestamp, the user
  7578. will also be prompted for a time shift, which may be a repeater
  7579. as used in time stamps, for example `+3d'. To disable this,
  7580. you can call the function with a universal prefix argument.
  7581. When a valid repeater is given and the entry contains any time
  7582. stamps, the clones will become a sequence in time, with time
  7583. stamps in the subtree shifted for each clone produced. If SHIFT
  7584. is nil or the empty string, time stamps will be left alone. The
  7585. ID property of the original subtree is removed.
  7586. If the original subtree did contain time stamps with a repeater,
  7587. the following will happen:
  7588. - the repeater will be removed in each clone
  7589. - an additional clone will be produced, with the current, unshifted
  7590. date(s) in the entry.
  7591. - the original entry will be placed *after* all the clones, with
  7592. repeater intact.
  7593. - the start days in the repeater in the original entry will be shifted
  7594. to past the last clone.
  7595. In this way you can spell out a number of instances of a repeating task,
  7596. and still retain the repeater to cover future instances of the task."
  7597. (interactive "nNumber of clones to produce: ")
  7598. (let ((shift
  7599. (or shift
  7600. (if (and (not (equal current-prefix-arg '(4)))
  7601. (save-excursion
  7602. (re-search-forward org-ts-regexp-both
  7603. (save-excursion
  7604. (org-end-of-subtree t)
  7605. (point)) t)))
  7606. (read-from-minibuffer
  7607. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7608. ""))) ;; No time shift
  7609. (n-no-remove -1)
  7610. (drawer-re org-drawer-regexp)
  7611. beg end template task idprop
  7612. shift-n shift-what doshift nmin nmax)
  7613. (if (not (and (integerp n) (> n 0)))
  7614. (error "Invalid number of replications %s" n))
  7615. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7616. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7617. shift)))
  7618. (error "Invalid shift specification %s" shift))
  7619. (when doshift
  7620. (setq shift-n (string-to-number (match-string 1 shift))
  7621. shift-what (cdr (assoc (match-string 2 shift)
  7622. '(("d" . day) ("w" . week)
  7623. ("m" . month) ("y" . year))))))
  7624. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7625. (setq nmin 1 nmax n)
  7626. (org-back-to-heading t)
  7627. (setq beg (point))
  7628. (setq idprop (org-entry-get nil "ID"))
  7629. (org-end-of-subtree t t)
  7630. (or (bolp) (insert "\n"))
  7631. (setq end (point))
  7632. (setq template (buffer-substring beg end))
  7633. (when (and doshift
  7634. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7635. (delete-region beg end)
  7636. (setq end beg)
  7637. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7638. (goto-char end)
  7639. (loop for n from nmin to nmax do
  7640. ;; prepare clone
  7641. (with-temp-buffer
  7642. (insert template)
  7643. (org-mode)
  7644. (goto-char (point-min))
  7645. (org-show-subtree)
  7646. (and idprop (if org-clone-delete-id
  7647. (org-entry-delete nil "ID")
  7648. (org-id-get-create t)))
  7649. (unless (= n 0)
  7650. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7651. (kill-whole-line))
  7652. (goto-char (point-min))
  7653. (while (re-search-forward drawer-re nil t)
  7654. (org-remove-empty-drawer-at (point))))
  7655. (goto-char (point-min))
  7656. (when doshift
  7657. (while (re-search-forward org-ts-regexp-both nil t)
  7658. (org-timestamp-change (* n shift-n) shift-what))
  7659. (unless (= n n-no-remove)
  7660. (goto-char (point-min))
  7661. (while (re-search-forward org-ts-regexp nil t)
  7662. (save-excursion
  7663. (goto-char (match-beginning 0))
  7664. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7665. (delete-region (match-beginning 1) (match-end 1)))))))
  7666. (setq task (buffer-string)))
  7667. (insert task))
  7668. (goto-char beg)))
  7669. ;;; Outline Sorting
  7670. (defun org-sort (with-case)
  7671. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7672. Optional argument WITH-CASE means sort case-sensitively."
  7673. (interactive "P")
  7674. (cond
  7675. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7676. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7677. (t
  7678. (org-call-with-arg 'org-sort-entries with-case))))
  7679. (defun org-sort-remove-invisible (s)
  7680. "Remove invisible links from string S."
  7681. (remove-text-properties 0 (length s) org-rm-props s)
  7682. (while (string-match org-bracket-link-regexp s)
  7683. (setq s (replace-match (if (match-end 2)
  7684. (match-string 3 s)
  7685. (match-string 1 s)) t t s)))
  7686. (let ((st (format " %s " s)))
  7687. (while (string-match org-emph-re st)
  7688. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7689. (setq s (substring st 1 -1)))
  7690. s)
  7691. (defvar org-priority-regexp) ; defined later in the file
  7692. (defvar org-after-sorting-entries-or-items-hook nil
  7693. "Hook that is run after a bunch of entries or items have been sorted.
  7694. When children are sorted, the cursor is in the parent line when this
  7695. hook gets called. When a region or a plain list is sorted, the cursor
  7696. will be in the first entry of the sorted region/list.")
  7697. (defun org-sort-entries
  7698. (&optional with-case sorting-type getkey-func compare-func property)
  7699. "Sort entries on a certain level of an outline tree.
  7700. If there is an active region, the entries in the region are sorted.
  7701. Else, if the cursor is before the first entry, sort the top-level items.
  7702. Else, the children of the entry at point are sorted.
  7703. Sorting can be alphabetically, numerically, by date/time as given by
  7704. a time stamp, by a property, by priority order, or by a custom function.
  7705. The command prompts for the sorting type unless it has been given to the
  7706. function through the SORTING-TYPE argument, which needs to be a character,
  7707. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7708. precise meaning of each character:
  7709. n Numerically, by converting the beginning of the entry/item to a number.
  7710. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7711. o By order of TODO keywords.
  7712. t By date/time, either the first active time stamp in the entry, or, if
  7713. none exist, by the first inactive one.
  7714. s By the scheduled date/time.
  7715. d By deadline date/time.
  7716. c By creation time, which is assumed to be the first inactive time stamp
  7717. at the beginning of a line.
  7718. p By priority according to the cookie.
  7719. r By the value of a property.
  7720. Capital letters will reverse the sort order.
  7721. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7722. called with point at the beginning of the record. It must return either
  7723. a string or a number that should serve as the sorting key for that record.
  7724. Comparing entries ignores case by default. However, with an optional argument
  7725. WITH-CASE, the sorting considers case as well.
  7726. Sorting is done against the visible part of the headlines, it ignores hidden
  7727. links.
  7728. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7729. (interactive "P")
  7730. (let ((case-func (if with-case 'identity 'downcase))
  7731. (cmstr
  7732. ;; The clock marker is lost when using `sort-subr', let's
  7733. ;; store the clocking string.
  7734. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7735. (save-excursion
  7736. (goto-char org-clock-marker)
  7737. (looking-back "^.*") (match-string-no-properties 0))))
  7738. start beg end stars re re2
  7739. txt what tmp)
  7740. ;; Find beginning and end of region to sort
  7741. (cond
  7742. ((org-region-active-p)
  7743. ;; we will sort the region
  7744. (setq end (region-end)
  7745. what "region")
  7746. (goto-char (region-beginning))
  7747. (if (not (org-at-heading-p)) (outline-next-heading))
  7748. (setq start (point)))
  7749. ((or (org-at-heading-p)
  7750. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7751. ;; we will sort the children of the current headline
  7752. (org-back-to-heading)
  7753. (setq start (point)
  7754. end (progn (org-end-of-subtree t t)
  7755. (or (bolp) (insert "\n"))
  7756. (org-back-over-empty-lines)
  7757. (point))
  7758. what "children")
  7759. (goto-char start)
  7760. (show-subtree)
  7761. (outline-next-heading))
  7762. (t
  7763. ;; we will sort the top-level entries in this file
  7764. (goto-char (point-min))
  7765. (or (org-at-heading-p) (outline-next-heading))
  7766. (setq start (point))
  7767. (goto-char (point-max))
  7768. (beginning-of-line 1)
  7769. (when (looking-at ".*?\\S-")
  7770. ;; File ends in a non-white line
  7771. (end-of-line 1)
  7772. (insert "\n"))
  7773. (setq end (point-max))
  7774. (setq what "top-level")
  7775. (goto-char start)
  7776. (show-all)))
  7777. (setq beg (point))
  7778. (if (>= beg end) (user-error "Nothing to sort"))
  7779. (looking-at "\\(\\*+\\)")
  7780. (setq stars (match-string 1)
  7781. re (concat "^" (regexp-quote stars) " +")
  7782. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7783. txt (buffer-substring beg end))
  7784. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7785. (if (and (not (equal stars "*")) (string-match re2 txt))
  7786. (user-error "Region to sort contains a level above the first entry"))
  7787. (unless sorting-type
  7788. (message
  7789. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7790. [t]ime [s]cheduled [d]eadline [c]reated
  7791. A/N/P/R/O/F/T/S/D/C means reversed:"
  7792. what)
  7793. (setq sorting-type (read-char-exclusive))
  7794. (unless getkey-func
  7795. (and (= (downcase sorting-type) ?f)
  7796. (setq getkey-func
  7797. (org-icompleting-read "Sort using function: "
  7798. obarray 'fboundp t nil nil))
  7799. (setq getkey-func (intern getkey-func))))
  7800. (and (= (downcase sorting-type) ?r)
  7801. (not property)
  7802. (setq property
  7803. (org-icompleting-read "Property: "
  7804. (mapcar 'list (org-buffer-property-keys t))
  7805. nil t))))
  7806. (message "Sorting entries...")
  7807. (save-restriction
  7808. (narrow-to-region start end)
  7809. (let ((dcst (downcase sorting-type))
  7810. (case-fold-search nil)
  7811. (now (current-time)))
  7812. (sort-subr
  7813. (/= dcst sorting-type)
  7814. ;; This function moves to the beginning character of the "record" to
  7815. ;; be sorted.
  7816. (lambda nil
  7817. (if (re-search-forward re nil t)
  7818. (goto-char (match-beginning 0))
  7819. (goto-char (point-max))))
  7820. ;; This function moves to the last character of the "record" being
  7821. ;; sorted.
  7822. (lambda nil
  7823. (save-match-data
  7824. (condition-case nil
  7825. (outline-forward-same-level 1)
  7826. (error
  7827. (goto-char (point-max))))))
  7828. ;; This function returns the value that gets sorted against.
  7829. (lambda nil
  7830. (cond
  7831. ((= dcst ?n)
  7832. (if (looking-at org-complex-heading-regexp)
  7833. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7834. nil))
  7835. ((= dcst ?a)
  7836. (if (looking-at org-complex-heading-regexp)
  7837. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7838. nil))
  7839. ((= dcst ?t)
  7840. (let ((end (save-excursion (outline-next-heading) (point))))
  7841. (if (or (re-search-forward org-ts-regexp end t)
  7842. (re-search-forward org-ts-regexp-both end t))
  7843. (org-time-string-to-seconds (match-string 0))
  7844. (org-float-time now))))
  7845. ((= dcst ?c)
  7846. (let ((end (save-excursion (outline-next-heading) (point))))
  7847. (if (re-search-forward
  7848. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7849. end t)
  7850. (org-time-string-to-seconds (match-string 0))
  7851. (org-float-time now))))
  7852. ((= dcst ?s)
  7853. (let ((end (save-excursion (outline-next-heading) (point))))
  7854. (if (re-search-forward org-scheduled-time-regexp end t)
  7855. (org-time-string-to-seconds (match-string 1))
  7856. (org-float-time now))))
  7857. ((= dcst ?d)
  7858. (let ((end (save-excursion (outline-next-heading) (point))))
  7859. (if (re-search-forward org-deadline-time-regexp end t)
  7860. (org-time-string-to-seconds (match-string 1))
  7861. (org-float-time now))))
  7862. ((= dcst ?p)
  7863. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7864. (string-to-char (match-string 2))
  7865. org-default-priority))
  7866. ((= dcst ?r)
  7867. (or (org-entry-get nil property) ""))
  7868. ((= dcst ?o)
  7869. (if (looking-at org-complex-heading-regexp)
  7870. (- 9999 (length (member (match-string 2)
  7871. org-todo-keywords-1)))))
  7872. ((= dcst ?f)
  7873. (if getkey-func
  7874. (progn
  7875. (setq tmp (funcall getkey-func))
  7876. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7877. tmp)
  7878. (error "Invalid key function `%s'" getkey-func)))
  7879. (t (error "Invalid sorting type `%c'" sorting-type))))
  7880. nil
  7881. (cond
  7882. ((= dcst ?a) 'string<)
  7883. ((= dcst ?f) compare-func)
  7884. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7885. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7886. ;; Reset the clock marker if needed
  7887. (when cmstr
  7888. (save-excursion
  7889. (goto-char start)
  7890. (search-forward cmstr nil t)
  7891. (move-marker org-clock-marker (point))))
  7892. (message "Sorting entries...done")))
  7893. (defun org-do-sort (table what &optional with-case sorting-type)
  7894. "Sort TABLE of WHAT according to SORTING-TYPE.
  7895. The user will be prompted for the SORTING-TYPE if the call to this
  7896. function does not specify it. WHAT is only for the prompt, to indicate
  7897. what is being sorted. The sorting key will be extracted from
  7898. the car of the elements of the table.
  7899. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7900. (unless sorting-type
  7901. (message
  7902. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7903. what)
  7904. (setq sorting-type (read-char-exclusive)))
  7905. (let ((dcst (downcase sorting-type))
  7906. extractfun comparefun)
  7907. ;; Define the appropriate functions
  7908. (cond
  7909. ((= dcst ?n)
  7910. (setq extractfun 'string-to-number
  7911. comparefun (if (= dcst sorting-type) '< '>)))
  7912. ((= dcst ?a)
  7913. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7914. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7915. comparefun (if (= dcst sorting-type)
  7916. 'string<
  7917. (lambda (a b) (and (not (string< a b))
  7918. (not (string= a b)))))))
  7919. ((= dcst ?t)
  7920. (setq extractfun
  7921. (lambda (x)
  7922. (if (or (string-match org-ts-regexp x)
  7923. (string-match org-ts-regexp-both x))
  7924. (org-float-time
  7925. (org-time-string-to-time (match-string 0 x)))
  7926. 0))
  7927. comparefun (if (= dcst sorting-type) '< '>)))
  7928. (t (error "Invalid sorting type `%c'" sorting-type)))
  7929. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7930. table)
  7931. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7932. ;;; The orgstruct minor mode
  7933. ;; Define a minor mode which can be used in other modes in order to
  7934. ;; integrate the org-mode structure editing commands.
  7935. ;; This is really a hack, because the org-mode structure commands use
  7936. ;; keys which normally belong to the major mode. Here is how it
  7937. ;; works: The minor mode defines all the keys necessary to operate the
  7938. ;; structure commands, but wraps the commands into a function which
  7939. ;; tests if the cursor is currently at a headline or a plain list
  7940. ;; item. If that is the case, the structure command is used,
  7941. ;; temporarily setting many Org-mode variables like regular
  7942. ;; expressions for filling etc. However, when any of those keys is
  7943. ;; used at a different location, function uses `key-binding' to look
  7944. ;; up if the key has an associated command in another currently active
  7945. ;; keymap (minor modes, major mode, global), and executes that
  7946. ;; command. There might be problems if any of the keys is otherwise
  7947. ;; used as a prefix key.
  7948. (defcustom orgstruct-heading-prefix-regexp nil
  7949. "Regexp that matches the custom prefix of Org headlines in
  7950. orgstruct(++)-mode."
  7951. :group 'org
  7952. :version "24.4"
  7953. :package-version '(Org . "8.0")
  7954. :type 'string)
  7955. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7956. (defcustom orgstruct-setup-hook nil
  7957. "Hook run after orgstruct-mode-map is filled."
  7958. :group 'org
  7959. :version "24.4"
  7960. :package-version '(Org . "8.0")
  7961. :type 'hook)
  7962. (defvar orgstruct-initialized nil)
  7963. (defvar org-local-vars nil
  7964. "List of local variables, for use by `orgstruct-mode'.")
  7965. ;;;###autoload
  7966. (define-minor-mode orgstruct-mode
  7967. "Toggle the minor mode `orgstruct-mode'.
  7968. This mode is for using Org-mode structure commands in other
  7969. modes. The following keys behave as if Org-mode were active, if
  7970. the cursor is on a headline, or on a plain list item (both as
  7971. defined by Org-mode)."
  7972. nil " OrgStruct" (make-sparse-keymap)
  7973. (funcall (if orgstruct-mode
  7974. 'add-to-invisibility-spec
  7975. 'remove-from-invisibility-spec)
  7976. '(outline . t))
  7977. (when orgstruct-mode
  7978. (org-load-modules-maybe)
  7979. (unless orgstruct-initialized
  7980. (orgstruct-setup)
  7981. (setq orgstruct-initialized t))))
  7982. ;;;###autoload
  7983. (defun turn-on-orgstruct ()
  7984. "Unconditionally turn on `orgstruct-mode'."
  7985. (orgstruct-mode 1))
  7986. (defvar org-fb-vars nil)
  7987. (make-variable-buffer-local 'org-fb-vars)
  7988. (defun orgstruct++-mode (&optional arg)
  7989. "Toggle `orgstruct-mode', the enhanced version of it.
  7990. In addition to setting orgstruct-mode, this also exports all
  7991. indentation and autofilling variables from org-mode into the
  7992. buffer. It will also recognize item context in multiline items."
  7993. (interactive "P")
  7994. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7995. (if (< arg 1)
  7996. (progn (orgstruct-mode -1)
  7997. (mapc (lambda(v)
  7998. (org-set-local (car v)
  7999. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8000. org-fb-vars))
  8001. (orgstruct-mode 1)
  8002. (setq org-fb-vars nil)
  8003. (unless org-local-vars
  8004. (setq org-local-vars (org-get-local-variables)))
  8005. (let (var val)
  8006. (mapc
  8007. (lambda (x)
  8008. (when (string-match
  8009. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8010. (symbol-name (car x)))
  8011. (setq var (car x) val (nth 1 x))
  8012. (push (list var `(quote ,(eval var))) org-fb-vars)
  8013. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8014. org-local-vars)
  8015. (org-set-local 'orgstruct-is-++ t))))
  8016. (defvar orgstruct-is-++ nil
  8017. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8018. (make-variable-buffer-local 'orgstruct-is-++)
  8019. ;;;###autoload
  8020. (defun turn-on-orgstruct++ ()
  8021. "Unconditionally turn on `orgstruct++-mode'."
  8022. (orgstruct++-mode 1))
  8023. (defun orgstruct-error ()
  8024. "Error when there is no default binding for a structure key."
  8025. (interactive)
  8026. (funcall (if (fboundp 'user-error)
  8027. 'user-error
  8028. 'error)
  8029. "This key has no function outside structure elements"))
  8030. (defun orgstruct-setup ()
  8031. "Setup orgstruct keymap."
  8032. (dolist (cell '((org-demote . t)
  8033. (org-metaleft . t)
  8034. (org-metaright . t)
  8035. (org-promote . t)
  8036. (org-shiftmetaleft . t)
  8037. (org-shiftmetaright . t)
  8038. org-backward-element
  8039. org-backward-heading-same-level
  8040. org-ctrl-c-ret
  8041. org-ctrl-c-minus
  8042. org-ctrl-c-star
  8043. org-cycle
  8044. org-forward-heading-same-level
  8045. org-insert-heading
  8046. org-insert-heading-respect-content
  8047. org-kill-note-or-show-branches
  8048. org-mark-subtree
  8049. org-meta-return
  8050. org-metadown
  8051. org-metaup
  8052. org-narrow-to-subtree
  8053. org-promote-subtree
  8054. org-reveal
  8055. org-shiftdown
  8056. org-shiftleft
  8057. org-shiftmetadown
  8058. org-shiftmetaup
  8059. org-shiftright
  8060. org-shifttab
  8061. org-shifttab
  8062. org-shiftup
  8063. org-show-subtree
  8064. org-sort
  8065. org-up-element
  8066. outline-demote
  8067. outline-next-visible-heading
  8068. outline-previous-visible-heading
  8069. outline-promote
  8070. outline-up-heading
  8071. show-children))
  8072. (let ((f (or (car-safe cell) cell))
  8073. (disable-when-heading-prefix (cdr-safe cell)))
  8074. (when (fboundp f)
  8075. (let ((new-bindings))
  8076. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8077. (where-is-internal f outline-mode-map)))
  8078. (push binding new-bindings)
  8079. ;; TODO use local-function-key-map
  8080. (dolist (rep '(("<tab>" . "TAB")
  8081. ("<return>" . "RET")
  8082. ("<escape>" . "ESC")
  8083. ("<delete>" . "DEL")))
  8084. (setq binding (read-kbd-macro
  8085. (let ((case-fold-search))
  8086. (replace-regexp-in-string
  8087. (regexp-quote (cdr rep))
  8088. (car rep)
  8089. (key-description binding)))))
  8090. (pushnew binding new-bindings :test 'equal)))
  8091. (dolist (binding new-bindings)
  8092. (let ((key (lookup-key orgstruct-mode-map binding)))
  8093. (when (or (not key) (numberp key))
  8094. (condition-case nil
  8095. (org-defkey orgstruct-mode-map
  8096. binding
  8097. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8098. (error nil)))))))))
  8099. (run-hooks 'orgstruct-setup-hook))
  8100. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8101. "Create a function for binding in the structure minor mode.
  8102. FUN is the command to call inside a table. KEY is the key that
  8103. should be checked in for a command to execute outside of tables.
  8104. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8105. if `orgstruct-heading-prefix-regexp' is non-nil."
  8106. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8107. (let ((nname name)
  8108. (i 0))
  8109. (while (fboundp (intern nname))
  8110. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8111. (setq name (intern nname)))
  8112. (eval
  8113. (let ((bindings '((org-heading-regexp
  8114. (concat "^"
  8115. orgstruct-heading-prefix-regexp
  8116. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8117. (org-outline-regexp
  8118. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8119. (org-outline-regexp-bol
  8120. (concat "^" org-outline-regexp))
  8121. (outline-regexp org-outline-regexp)
  8122. (outline-heading-end-regexp "\n")
  8123. (outline-level 'org-outline-level)
  8124. (outline-heading-alist))))
  8125. `(defun ,name (arg)
  8126. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8127. "Outside of structure, run the binding of `"
  8128. (key-description key) "'."
  8129. (when disable-when-heading-prefix
  8130. (concat
  8131. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8132. "back to the default binding due to limitations of Org's implementation of\n"
  8133. "`" (symbol-name fun) "'.")))
  8134. (interactive "p")
  8135. (let* ((disable
  8136. ,(when disable-when-heading-prefix
  8137. '(and orgstruct-heading-prefix-regexp
  8138. (not (string= orgstruct-heading-prefix-regexp "")))))
  8139. (fallback
  8140. (or disable
  8141. (not
  8142. (let* ,bindings
  8143. (org-context-p 'headline 'item
  8144. ,(when (memq fun
  8145. '(org-insert-heading
  8146. org-insert-heading-respect-content
  8147. org-meta-return))
  8148. '(when orgstruct-is-++
  8149. 'item-body))))))))
  8150. (if fallback
  8151. (let* ((orgstruct-mode)
  8152. (binding
  8153. (loop with key = ,key
  8154. for rep in
  8155. '(nil
  8156. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8157. ("<\\([^>]*\\)return>" . "\\1RET")
  8158. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8159. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8160. do
  8161. (when rep
  8162. (setq key (read-kbd-macro
  8163. (let ((case-fold-search))
  8164. (replace-regexp-in-string
  8165. (car rep)
  8166. (cdr rep)
  8167. (key-description key))))))
  8168. thereis (key-binding key))))
  8169. (if (keymapp binding)
  8170. (set-temporary-overlay-map binding)
  8171. (let ((func (or binding
  8172. (unless disable
  8173. 'orgstruct-error))))
  8174. (when func
  8175. (call-interactively func)))))
  8176. (org-run-like-in-org-mode
  8177. (lambda ()
  8178. (interactive)
  8179. (let* ,bindings
  8180. (call-interactively ',fun)))))))))
  8181. name))
  8182. (defun org-contextualize-keys (alist contexts)
  8183. "Return valid elements in ALIST depending on CONTEXTS.
  8184. `org-agenda-custom-commands' or `org-capture-templates' are the
  8185. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8186. or `org-capture-templates-contexts' are the associated contexts
  8187. definitions."
  8188. (let ((contexts
  8189. ;; normalize contexts
  8190. (mapcar
  8191. (lambda(c) (cond ((listp (cadr c))
  8192. (list (car c) (car c) (cadr c)))
  8193. ((string= "" (cadr c))
  8194. (list (car c) (car c) (caddr c)))
  8195. (t c))) contexts))
  8196. (a alist) c r s)
  8197. ;; loop over all commands or templates
  8198. (while (setq c (pop a))
  8199. (let (vrules repl)
  8200. (cond
  8201. ((not (assoc (car c) contexts))
  8202. (push c r))
  8203. ((and (assoc (car c) contexts)
  8204. (setq vrules (org-contextualize-validate-key
  8205. (car c) contexts)))
  8206. (mapc (lambda (vr)
  8207. (when (not (equal (car vr) (cadr vr)))
  8208. (setq repl vr))) vrules)
  8209. (if (not repl) (push c r)
  8210. (push (cadr repl) s)
  8211. (push
  8212. (cons (car c)
  8213. (cdr (or (assoc (cadr repl) alist)
  8214. (error "Undefined key `%s' as contextual replacement for `%s'"
  8215. (cadr repl) (car c)))))
  8216. r))))))
  8217. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8218. (delq
  8219. nil
  8220. (delete-dups
  8221. (mapcar (lambda (x)
  8222. (let ((tpl (car x)))
  8223. (when (not (delq
  8224. nil
  8225. (mapcar (lambda(y)
  8226. (equal y tpl)) s))) x)))
  8227. (reverse r))))))
  8228. (defun org-contextualize-validate-key (key contexts)
  8229. "Check CONTEXTS for agenda or capture KEY."
  8230. (let (r rr res)
  8231. (while (setq r (pop contexts))
  8232. (mapc
  8233. (lambda (rr)
  8234. (when
  8235. (and (equal key (car r))
  8236. (if (functionp rr) (funcall rr)
  8237. (or (and (eq (car rr) 'in-file)
  8238. (buffer-file-name)
  8239. (string-match (cdr rr) (buffer-file-name)))
  8240. (and (eq (car rr) 'in-mode)
  8241. (string-match (cdr rr) (symbol-name major-mode)))
  8242. (and (eq (car rr) 'in-buffer)
  8243. (string-match (cdr rr) (buffer-name)))
  8244. (when (and (eq (car rr) 'not-in-file)
  8245. (buffer-file-name))
  8246. (not (string-match (cdr rr) (buffer-file-name))))
  8247. (when (eq (car rr) 'not-in-mode)
  8248. (not (string-match (cdr rr) (symbol-name major-mode))))
  8249. (when (eq (car rr) 'not-in-buffer)
  8250. (not (string-match (cdr rr) (buffer-name)))))))
  8251. (push r res)))
  8252. (car (last r))))
  8253. (delete-dups (delq nil res))))
  8254. (defun org-context-p (&rest contexts)
  8255. "Check if local context is any of CONTEXTS.
  8256. Possible values in the list of contexts are `table', `headline', and `item'."
  8257. (let ((pos (point)))
  8258. (goto-char (point-at-bol))
  8259. (prog1 (or (and (memq 'table contexts)
  8260. (looking-at "[ \t]*|"))
  8261. (and (memq 'headline contexts)
  8262. (looking-at org-outline-regexp))
  8263. (and (memq 'item contexts)
  8264. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8265. (and (memq 'item-body contexts)
  8266. (org-in-item-p)))
  8267. (goto-char pos))))
  8268. (defun org-get-local-variables ()
  8269. "Return a list of all local variables in an Org mode buffer."
  8270. (let (varlist)
  8271. (with-current-buffer (get-buffer-create "*Org tmp*")
  8272. (erase-buffer)
  8273. (org-mode)
  8274. (setq varlist (buffer-local-variables)))
  8275. (kill-buffer "*Org tmp*")
  8276. (delq nil
  8277. (mapcar
  8278. (lambda (x)
  8279. (setq x
  8280. (if (symbolp x)
  8281. (list x)
  8282. (list (car x) (cdr x))))
  8283. (if (and (not (get (car x) 'org-state))
  8284. (string-match
  8285. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8286. (symbol-name (car x))))
  8287. x nil))
  8288. varlist))))
  8289. (defun org-clone-local-variables (from-buffer &optional regexp)
  8290. "Clone local variables from FROM-BUFFER.
  8291. Optional argument REGEXP selects variables to clone."
  8292. (mapc
  8293. (lambda (pair)
  8294. (and (symbolp (car pair))
  8295. (or (null regexp)
  8296. (string-match regexp (symbol-name (car pair))))
  8297. (set (make-local-variable (car pair))
  8298. (cdr pair))))
  8299. (buffer-local-variables from-buffer)))
  8300. ;;;###autoload
  8301. (defun org-run-like-in-org-mode (cmd)
  8302. "Run a command, pretending that the current buffer is in Org-mode.
  8303. This will temporarily bind local variables that are typically bound in
  8304. Org-mode to the values they have in Org-mode, and then interactively
  8305. call CMD."
  8306. (org-load-modules-maybe)
  8307. (unless org-local-vars
  8308. (setq org-local-vars (org-get-local-variables)))
  8309. (let (binds)
  8310. (dolist (var org-local-vars)
  8311. (when (or (not (boundp (car var)))
  8312. (eq (symbol-value (car var))
  8313. (default-value (car var))))
  8314. (push (list (car var) `(quote ,(cadr var))) binds)))
  8315. (eval `(let ,binds
  8316. (call-interactively (quote ,cmd))))))
  8317. ;;;; Archiving
  8318. (defun org-get-category (&optional pos force-refresh)
  8319. "Get the category applying to position POS."
  8320. (save-match-data
  8321. (if force-refresh (org-refresh-category-properties))
  8322. (let ((pos (or pos (point))))
  8323. (or (get-text-property pos 'org-category)
  8324. (progn (org-refresh-category-properties)
  8325. (get-text-property pos 'org-category))))))
  8326. (defun org-refresh-category-properties ()
  8327. "Refresh category text properties in the buffer."
  8328. (let ((case-fold-search t)
  8329. (inhibit-read-only t)
  8330. (def-cat (cond
  8331. ((null org-category)
  8332. (if buffer-file-name
  8333. (file-name-sans-extension
  8334. (file-name-nondirectory buffer-file-name))
  8335. "???"))
  8336. ((symbolp org-category) (symbol-name org-category))
  8337. (t org-category)))
  8338. beg end cat pos optionp)
  8339. (org-with-silent-modifications
  8340. (save-excursion
  8341. (save-restriction
  8342. (widen)
  8343. (goto-char (point-min))
  8344. (put-text-property (point) (point-max) 'org-category def-cat)
  8345. (while (re-search-forward
  8346. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8347. (setq pos (match-end 0)
  8348. optionp (equal (char-after (match-beginning 0)) ?#)
  8349. cat (org-trim (match-string 2)))
  8350. (if optionp
  8351. (setq beg (point-at-bol) end (point-max))
  8352. (org-back-to-heading t)
  8353. (setq beg (point) end (org-end-of-subtree t t)))
  8354. (put-text-property beg end 'org-category cat)
  8355. (put-text-property beg end 'org-category-position beg)
  8356. (goto-char pos)))))))
  8357. (defun org-refresh-properties (dprop tprop)
  8358. "Refresh buffer text properties.
  8359. DPROP is the drawer property and TPROP is the corresponding text
  8360. property to set."
  8361. (let ((case-fold-search t)
  8362. (inhibit-read-only t) p)
  8363. (org-with-silent-modifications
  8364. (save-excursion
  8365. (save-restriction
  8366. (widen)
  8367. (goto-char (point-min))
  8368. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8369. (setq p (org-match-string-no-properties 1))
  8370. (save-excursion
  8371. (org-back-to-heading t)
  8372. (put-text-property
  8373. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8374. ;;;; Link Stuff
  8375. ;;; Link abbreviations
  8376. (defun org-link-expand-abbrev (link)
  8377. "Apply replacements as defined in `org-link-abbrev-alist'."
  8378. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8379. (let* ((key (match-string 1 link))
  8380. (as (or (assoc key org-link-abbrev-alist-local)
  8381. (assoc key org-link-abbrev-alist)))
  8382. (tag (and (match-end 2) (match-string 3 link)))
  8383. rpl)
  8384. (if (not as)
  8385. link
  8386. (setq rpl (cdr as))
  8387. (cond
  8388. ((symbolp rpl) (funcall rpl tag))
  8389. ((string-match "%(\\([^)]+\\))" rpl)
  8390. (replace-match
  8391. (save-match-data
  8392. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8393. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8394. ((string-match "%h" rpl)
  8395. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8396. (t (concat rpl tag)))))
  8397. link))
  8398. ;;; Storing and inserting links
  8399. (defvar org-insert-link-history nil
  8400. "Minibuffer history for links inserted with `org-insert-link'.")
  8401. (defvar org-stored-links nil
  8402. "Contains the links stored with `org-store-link'.")
  8403. (defvar org-store-link-plist nil
  8404. "Plist with info about the most recently link created with `org-store-link'.")
  8405. (defvar org-link-protocols nil
  8406. "Link protocols added to Org-mode using `org-add-link-type'.")
  8407. (defvar org-store-link-functions nil
  8408. "List of functions that are called to create and store a link.
  8409. Each function will be called in turn until one returns a non-nil
  8410. value. Each function should check if it is responsible for creating
  8411. this link (for example by looking at the major mode).
  8412. If not, it must exit and return nil.
  8413. If yes, it should return a non-nil value after a calling
  8414. `org-store-link-props' with a list of properties and values.
  8415. Special properties are:
  8416. :type The link prefix, like \"http\". This must be given.
  8417. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8418. This is obligatory as well.
  8419. :description Optional default description for the second pair
  8420. of brackets in an Org-mode link. The user can still change
  8421. this when inserting this link into an Org-mode buffer.
  8422. In addition to these, any additional properties can be specified
  8423. and then used in capture templates.")
  8424. (defun org-add-link-type (type &optional follow export)
  8425. "Add TYPE to the list of `org-link-types'.
  8426. Re-compute all regular expressions depending on `org-link-types'
  8427. FOLLOW and EXPORT are two functions.
  8428. FOLLOW should take the link path as the single argument and do whatever
  8429. is necessary to follow the link, for example find a file or display
  8430. a mail message.
  8431. EXPORT should format the link path for export to one of the export formats.
  8432. It should be a function accepting three arguments:
  8433. path the path of the link, the text after the prefix (like \"http:\")
  8434. desc the description of the link, if any, or a description added by
  8435. org-export-normalize-links if there is none
  8436. format the export format, a symbol like `html' or `latex' or `ascii'..
  8437. The function may use the FORMAT information to return different values
  8438. depending on the format. The return value will be put literally into
  8439. the exported file. If the return value is nil, this means Org should
  8440. do what it normally does with links which do not have EXPORT defined.
  8441. Org-mode has a built-in default for exporting links. If you are happy with
  8442. this default, there is no need to define an export function for the link
  8443. type. For a simple example of an export function, see `org-bbdb.el'."
  8444. (add-to-list 'org-link-types type t)
  8445. (org-make-link-regexps)
  8446. (if (assoc type org-link-protocols)
  8447. (setcdr (assoc type org-link-protocols) (list follow export))
  8448. (push (list type follow export) org-link-protocols)))
  8449. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8450. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8451. ;;;###autoload
  8452. (defun org-store-link (arg)
  8453. "\\<org-mode-map>Store an org-link to the current location.
  8454. This link is added to `org-stored-links' and can later be inserted
  8455. into an org-buffer with \\[org-insert-link].
  8456. For some link types, a prefix arg is interpreted.
  8457. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8458. For file links, arg negates `org-context-in-file-links'.
  8459. A double prefix arg force skipping storing functions that are not
  8460. part of Org's core.
  8461. A triple prefix arg force storing a link for each line in the
  8462. active region."
  8463. (interactive "P")
  8464. (org-load-modules-maybe)
  8465. (if (and (equal arg '(64)) (org-region-active-p))
  8466. (save-excursion
  8467. (let ((end (region-end)))
  8468. (goto-char (region-beginning))
  8469. (set-mark (point))
  8470. (while (< (point-at-eol) end)
  8471. (move-end-of-line 1) (activate-mark)
  8472. (let (current-prefix-arg)
  8473. (call-interactively 'org-store-link))
  8474. (move-beginning-of-line 2)
  8475. (set-mark (point)))))
  8476. (org-with-limited-levels
  8477. (setq org-store-link-plist nil)
  8478. (let (link cpltxt desc description search
  8479. txt custom-id agenda-link sfuns sfunsn)
  8480. (cond
  8481. ;; Store a link using an external link type
  8482. ((and (not (equal arg '(16)))
  8483. (setq sfuns
  8484. (delq
  8485. nil (mapcar (lambda (f)
  8486. (let (fs) (if (funcall f) (push f fs))))
  8487. org-store-link-functions))
  8488. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8489. (or (and (cdr sfuns)
  8490. (funcall (intern
  8491. (completing-read
  8492. "Which function for creating the link? "
  8493. sfunsn t (car sfunsn)))))
  8494. (funcall (caar sfuns)))
  8495. (setq link (plist-get org-store-link-plist :link)
  8496. desc (or (plist-get org-store-link-plist
  8497. :description) link))))
  8498. ;; Store a link from a source code buffer
  8499. ((org-src-edit-buffer-p)
  8500. (let (label gc)
  8501. (while (or (not label)
  8502. (save-excursion
  8503. (save-restriction
  8504. (widen)
  8505. (goto-char (point-min))
  8506. (re-search-forward
  8507. (regexp-quote (format org-coderef-label-format label))
  8508. nil t))))
  8509. (when label (message "Label exists already") (sit-for 2))
  8510. (setq label (read-string "Code line label: " label)))
  8511. (end-of-line 1)
  8512. (setq link (format org-coderef-label-format label))
  8513. (setq gc (- 79 (length link)))
  8514. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8515. (insert link)
  8516. (setq link (concat "(" label ")") desc nil)))
  8517. ;; We are in the agenda, link to referenced location
  8518. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8519. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8520. (get-text-property (point) 'org-marker))))
  8521. (when m
  8522. (org-with-point-at m
  8523. (setq agenda-link
  8524. (if (org-called-interactively-p 'any)
  8525. (call-interactively 'org-store-link)
  8526. (org-store-link nil)))))))
  8527. ((eq major-mode 'calendar-mode)
  8528. (let ((cd (calendar-cursor-to-date)))
  8529. (setq link
  8530. (format-time-string
  8531. (car org-time-stamp-formats)
  8532. (apply 'encode-time
  8533. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8534. nil nil nil))))
  8535. (org-store-link-props :type "calendar" :date cd)))
  8536. ((eq major-mode 'help-mode)
  8537. (setq link (concat "help:" (save-excursion
  8538. (goto-char (point-min))
  8539. (looking-at "^[^ ]+")
  8540. (match-string 0))))
  8541. (org-store-link-props :type "help"))
  8542. ((eq major-mode 'w3-mode)
  8543. (setq cpltxt (if (and (buffer-name)
  8544. (not (string-match "Untitled" (buffer-name))))
  8545. (buffer-name)
  8546. (url-view-url t))
  8547. link (url-view-url t))
  8548. (org-store-link-props :type "w3" :url (url-view-url t)))
  8549. ((eq major-mode 'image-mode)
  8550. (setq cpltxt (concat "file:"
  8551. (abbreviate-file-name buffer-file-name))
  8552. link cpltxt)
  8553. (org-store-link-props :type "image" :file buffer-file-name))
  8554. ;; In dired, store a link to the file of the current line
  8555. ((eq major-mode 'dired-mode)
  8556. (let ((file (dired-get-filename nil t)))
  8557. (setq file (if file
  8558. (abbreviate-file-name
  8559. (expand-file-name (dired-get-filename nil t)))
  8560. ;; otherwise, no file so use current directory.
  8561. default-directory))
  8562. (setq cpltxt (concat "file:" file)
  8563. link cpltxt)))
  8564. ((setq search (run-hook-with-args-until-success
  8565. 'org-create-file-search-functions))
  8566. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8567. "::" search))
  8568. (setq cpltxt (or description link)))
  8569. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8570. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8571. (cond
  8572. ;; Store a link using the target at point
  8573. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8574. (setq cpltxt
  8575. (concat "file:"
  8576. (abbreviate-file-name
  8577. (buffer-file-name (buffer-base-buffer)))
  8578. "::" (match-string 1))
  8579. link cpltxt))
  8580. ((and (featurep 'org-id)
  8581. (or (eq org-id-link-to-org-use-id t)
  8582. (and (org-called-interactively-p 'any)
  8583. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8584. (and (eq org-id-link-to-org-use-id
  8585. 'create-if-interactive-and-no-custom-id)
  8586. (not custom-id))))
  8587. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8588. ;; Store a link using the ID at point
  8589. (setq link (condition-case nil
  8590. (prog1 (org-id-store-link)
  8591. (setq desc (plist-get org-store-link-plist
  8592. :description)))
  8593. (error
  8594. ;; Probably before first headline, link only to file
  8595. (concat "file:"
  8596. (abbreviate-file-name
  8597. (buffer-file-name (buffer-base-buffer))))))))
  8598. (t
  8599. ;; Just link to current headline
  8600. (setq cpltxt (concat "file:"
  8601. (abbreviate-file-name
  8602. (buffer-file-name (buffer-base-buffer)))))
  8603. ;; Add a context search string
  8604. (when (org-xor org-context-in-file-links arg)
  8605. (let* ((ee (org-element-at-point))
  8606. (et (org-element-type ee))
  8607. (ev (plist-get (cadr ee) :value))
  8608. (ek (plist-get (cadr ee) :key))
  8609. (eok (and (stringp ek) (string-match "name" ek))))
  8610. (setq txt (cond
  8611. ((org-at-heading-p) nil)
  8612. ((and (eq et 'keyword) eok) ev)
  8613. ((org-region-active-p)
  8614. (buffer-substring (region-beginning) (region-end)))))
  8615. (when (or (null txt) (string-match "\\S-" txt))
  8616. (setq cpltxt
  8617. (concat cpltxt "::"
  8618. (condition-case nil
  8619. (org-make-org-heading-search-string txt)
  8620. (error "")))
  8621. desc (or (and (eq et 'keyword) eok ev)
  8622. (nth 4 (ignore-errors (org-heading-components)))
  8623. "NONE")))))
  8624. (if (string-match "::\\'" cpltxt)
  8625. (setq cpltxt (substring cpltxt 0 -2)))
  8626. (setq link cpltxt))))
  8627. ((buffer-file-name (buffer-base-buffer))
  8628. ;; Just link to this file here.
  8629. (setq cpltxt (concat "file:"
  8630. (abbreviate-file-name
  8631. (buffer-file-name (buffer-base-buffer)))))
  8632. ;; Add a context string.
  8633. (when (org-xor org-context-in-file-links arg)
  8634. (setq txt (if (org-region-active-p)
  8635. (buffer-substring (region-beginning) (region-end))
  8636. (buffer-substring (point-at-bol) (point-at-eol))))
  8637. ;; Only use search option if there is some text.
  8638. (when (string-match "\\S-" txt)
  8639. (setq cpltxt
  8640. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8641. desc "NONE")))
  8642. (setq link cpltxt))
  8643. ((org-called-interactively-p 'interactive)
  8644. (user-error "No method for storing a link from this buffer"))
  8645. (t (setq link nil)))
  8646. ;; We're done setting link and desc, clean up
  8647. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8648. (setq link (or link cpltxt)
  8649. desc (or desc cpltxt))
  8650. (cond ((equal desc "NONE") (setq desc nil))
  8651. ((string-match org-bracket-link-analytic-regexp desc)
  8652. (let ((d0 (match-string 3 desc))
  8653. (p0 (match-string 5 desc)))
  8654. (setq desc
  8655. (replace-regexp-in-string
  8656. org-bracket-link-regexp
  8657. (concat (or p0 d0)
  8658. (if (equal (length (match-string 0 desc))
  8659. (length desc)) "*" "")) desc)))))
  8660. ;; Return the link
  8661. (if (not (and (or (org-called-interactively-p 'any)
  8662. executing-kbd-macro) link))
  8663. (or agenda-link (and link (org-make-link-string link desc)))
  8664. (push (list link desc) org-stored-links)
  8665. (message "Stored: %s" (or desc link))
  8666. (when custom-id
  8667. (setq link (concat "file:" (abbreviate-file-name
  8668. (buffer-file-name)) "::#" custom-id))
  8669. (push (list link desc) org-stored-links)))))))
  8670. (defun org-store-link-props (&rest plist)
  8671. "Store link properties, extract names and addresses."
  8672. (let (x adr)
  8673. (when (setq x (plist-get plist :from))
  8674. (setq adr (mail-extract-address-components x))
  8675. (setq plist (plist-put plist :fromname (car adr)))
  8676. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8677. (when (setq x (plist-get plist :to))
  8678. (setq adr (mail-extract-address-components x))
  8679. (setq plist (plist-put plist :toname (car adr)))
  8680. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8681. (let ((from (plist-get plist :from))
  8682. (to (plist-get plist :to)))
  8683. (when (and from to org-from-is-user-regexp)
  8684. (setq plist
  8685. (plist-put plist :fromto
  8686. (if (string-match org-from-is-user-regexp from)
  8687. (concat "to %t")
  8688. (concat "from %f"))))))
  8689. (setq org-store-link-plist plist))
  8690. (defun org-add-link-props (&rest plist)
  8691. "Add these properties to the link property list."
  8692. (let (key value)
  8693. (while plist
  8694. (setq key (pop plist) value (pop plist))
  8695. (setq org-store-link-plist
  8696. (plist-put org-store-link-plist key value)))))
  8697. (defun org-email-link-description (&optional fmt)
  8698. "Return the description part of an email link.
  8699. This takes information from `org-store-link-plist' and formats it
  8700. according to FMT (default from `org-email-link-description-format')."
  8701. (setq fmt (or fmt org-email-link-description-format))
  8702. (let* ((p org-store-link-plist)
  8703. (to (plist-get p :toaddress))
  8704. (from (plist-get p :fromaddress))
  8705. (table
  8706. (list
  8707. (cons "%c" (plist-get p :fromto))
  8708. (cons "%F" (plist-get p :from))
  8709. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8710. (cons "%T" (plist-get p :to))
  8711. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8712. (cons "%s" (plist-get p :subject))
  8713. (cons "%d" (plist-get p :date))
  8714. (cons "%m" (plist-get p :message-id)))))
  8715. (when (string-match "%c" fmt)
  8716. ;; Check if the user wrote this message
  8717. (if (and org-from-is-user-regexp from to
  8718. (save-match-data (string-match org-from-is-user-regexp from)))
  8719. (setq fmt (replace-match "to %t" t t fmt))
  8720. (setq fmt (replace-match "from %f" t t fmt))))
  8721. (org-replace-escapes fmt table)))
  8722. (defun org-make-org-heading-search-string (&optional string)
  8723. "Make search string for the current headline or STRING."
  8724. (let ((s (or string
  8725. (and (derived-mode-p 'org-mode)
  8726. (save-excursion
  8727. (org-back-to-heading t)
  8728. (org-element-property :raw-value (org-element-at-point))))))
  8729. (lines org-context-in-file-links))
  8730. (or string (setq s (concat "*" s))) ; Add * for headlines
  8731. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8732. (when (and string (integerp lines) (> lines 0))
  8733. (let ((slines (org-split-string s "\n")))
  8734. (when (< lines (length slines))
  8735. (setq s (mapconcat
  8736. 'identity
  8737. (reverse (nthcdr (- (length slines) lines)
  8738. (reverse slines))) "\n")))))
  8739. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8740. (defun org-make-link-string (link &optional description)
  8741. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8742. (unless (string-match "\\S-" link)
  8743. (error "Empty link"))
  8744. (when (and description
  8745. (stringp description)
  8746. (not (string-match "\\S-" description)))
  8747. (setq description nil))
  8748. (when (stringp description)
  8749. ;; Remove brackets from the description, they are fatal.
  8750. (while (string-match "\\[" description)
  8751. (setq description (replace-match "{" t t description)))
  8752. (while (string-match "\\]" description)
  8753. (setq description (replace-match "}" t t description))))
  8754. (when (equal link description)
  8755. ;; No description needed, it is identical
  8756. (setq description nil))
  8757. (when (and (not description)
  8758. (not (string-match (org-image-file-name-regexp) link))
  8759. (not (equal link (org-link-escape link))))
  8760. (setq description (org-extract-attributes link)))
  8761. (setq link
  8762. (cond ((string-match (org-image-file-name-regexp) link) link)
  8763. ((string-match org-link-types-re link)
  8764. (concat (match-string 1 link)
  8765. (org-link-escape (substring link (match-end 1)))))
  8766. (t (org-link-escape link))))
  8767. (concat "[[" link "]"
  8768. (if description (concat "[" description "]") "")
  8769. "]"))
  8770. (defconst org-link-escape-chars
  8771. ;;%20 %2B %3B %3D %5B %5D
  8772. '(?\ ?\+ ?\; ?\= ?\[ ?\])
  8773. "List of characters that should be escaped in link.
  8774. This is the list that is used for internal purposes.")
  8775. (defconst org-link-escape-chars-browser
  8776. ;;%20 %22
  8777. '(?\ ?\")
  8778. "List of escapes for characters that are problematic in links.
  8779. This is the list that is used before handing over to the browser.")
  8780. (defun org-link-escape (text &optional table merge)
  8781. "Return percent escaped representation of TEXT.
  8782. TEXT is a string with the text to escape.
  8783. Optional argument TABLE is a list with characters that should be
  8784. escaped. When nil, `org-link-escape-chars' is used.
  8785. If optional argument MERGE is set, merge TABLE into
  8786. `org-link-escape-chars'."
  8787. (cond
  8788. ((and table merge)
  8789. (mapc (lambda (defchr)
  8790. (unless (member defchr table)
  8791. (setq table (cons defchr table)))) org-link-escape-chars))
  8792. ((null table)
  8793. (setq table org-link-escape-chars)))
  8794. (mapconcat
  8795. (lambda (char)
  8796. (if (or (member char table)
  8797. (and (or (< char 32) (= char ?\%) (> char 126))
  8798. org-url-hexify-p))
  8799. (mapconcat (lambda (sequence-element)
  8800. (format "%%%.2X" sequence-element))
  8801. (or (encode-coding-char char 'utf-8)
  8802. (error "Unable to percent escape character: %s"
  8803. (char-to-string char))) "")
  8804. (char-to-string char))) text ""))
  8805. (defun org-link-escape-browser (text)
  8806. (if (org-string-match-p
  8807. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8808. text)
  8809. (org-link-escape text org-link-escape-chars-browser)
  8810. text))
  8811. (defun org-link-unescape (str)
  8812. "Unhex hexified Unicode strings as returned from the JavaScript function
  8813. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8814. (unless (and (null str) (string= "" str))
  8815. (let ((pos 0) (case-fold-search t) unhexed)
  8816. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8817. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8818. (setq str (replace-match unhexed t t str))
  8819. (setq pos (+ pos (length unhexed))))))
  8820. str)
  8821. (defun org-link-unescape-compound (hex)
  8822. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8823. Note: this function also decodes single byte encodings like
  8824. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8825. (save-match-data
  8826. (let* ((bytes (cdr (split-string hex "%")))
  8827. (ret "")
  8828. (eat 0)
  8829. (sum 0))
  8830. (while bytes
  8831. (let* ((val (string-to-number (pop bytes) 16))
  8832. (shift-xor
  8833. (if (= 0 eat)
  8834. (cond
  8835. ((>= val 252) (cons 6 252))
  8836. ((>= val 248) (cons 5 248))
  8837. ((>= val 240) (cons 4 240))
  8838. ((>= val 224) (cons 3 224))
  8839. ((>= val 192) (cons 2 192))
  8840. (t (cons 0 0)))
  8841. (cons 6 128))))
  8842. (if (>= val 192) (setq eat (car shift-xor)))
  8843. (setq val (logxor val (cdr shift-xor)))
  8844. (setq sum (+ (lsh sum (car shift-xor)) val))
  8845. (if (> eat 0) (setq eat (- eat 1)))
  8846. (cond
  8847. ((= 0 eat) ;multi byte
  8848. (setq ret (concat ret (org-char-to-string sum)))
  8849. (setq sum 0))
  8850. ((not bytes) ; single byte(s)
  8851. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8852. )) ;; end (while bytes
  8853. ret )))
  8854. (defun org-link-unescape-single-byte-sequence (hex)
  8855. "Unhexify hex-encoded single byte character sequences."
  8856. (mapconcat (lambda (byte)
  8857. (char-to-string (string-to-number byte 16)))
  8858. (cdr (split-string hex "%")) ""))
  8859. (defun org-xor (a b)
  8860. "Exclusive or."
  8861. (if a (not b) b))
  8862. (defun org-fixup-message-id-for-http (s)
  8863. "Replace special characters in a message id, so it can be used in an http query."
  8864. (when (string-match "%" s)
  8865. (setq s (mapconcat (lambda (c)
  8866. (if (eq c ?%)
  8867. "%25"
  8868. (char-to-string c)))
  8869. s "")))
  8870. (while (string-match "<" s)
  8871. (setq s (replace-match "%3C" t t s)))
  8872. (while (string-match ">" s)
  8873. (setq s (replace-match "%3E" t t s)))
  8874. (while (string-match "@" s)
  8875. (setq s (replace-match "%40" t t s)))
  8876. s)
  8877. (defun org-link-prettify (link)
  8878. "Return a human-readable representation of LINK.
  8879. The car of LINK must be a raw link the cdr of LINK must be either
  8880. a link description or nil."
  8881. (let ((desc (or (cadr link) "<no description>")))
  8882. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8883. "<" (car link) ">")))
  8884. ;;;###autoload
  8885. (defun org-insert-link-global ()
  8886. "Insert a link like Org-mode does.
  8887. This command can be called in any mode to insert a link in Org-mode syntax."
  8888. (interactive)
  8889. (org-load-modules-maybe)
  8890. (org-run-like-in-org-mode 'org-insert-link))
  8891. (defun org-insert-all-links (&optional keep)
  8892. "Insert all links in `org-stored-links'."
  8893. (interactive "P")
  8894. (let ((links (copy-sequence org-stored-links)) l)
  8895. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8896. (insert "- ")
  8897. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8898. (insert "\n"))))
  8899. (defun org-link-fontify-links-to-this-file ()
  8900. "Fontify links to the current file in `org-stored-links'."
  8901. (let ((f (buffer-file-name)) a b)
  8902. (setq a (mapcar (lambda(l)
  8903. (let ((ll (car l)))
  8904. (when (and (string-match "^file:\\(.+\\)::" ll)
  8905. (equal f (expand-file-name (match-string 1 ll))))
  8906. ll)))
  8907. org-stored-links))
  8908. (when (featurep 'org-id)
  8909. (setq b (mapcar (lambda(l)
  8910. (let ((ll (car l)))
  8911. (when (and (string-match "^id:\\(.+\\)$" ll)
  8912. (equal f (expand-file-name
  8913. (or (org-id-find-id-file
  8914. (match-string 1 ll)) ""))))
  8915. ll)))
  8916. org-stored-links)))
  8917. (mapcar (lambda(l)
  8918. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8919. (delq nil (append a b)))))
  8920. (defvar org-link-links-in-this-file nil)
  8921. (defun org-insert-link (&optional complete-file link-location default-description)
  8922. "Insert a link. At the prompt, enter the link.
  8923. Completion can be used to insert any of the link protocol prefixes like
  8924. http or ftp in use.
  8925. The history can be used to select a link previously stored with
  8926. `org-store-link'. When the empty string is entered (i.e. if you just
  8927. press RET at the prompt), the link defaults to the most recently
  8928. stored link. As SPC triggers completion in the minibuffer, you need to
  8929. use M-SPC or C-q SPC to force the insertion of a space character.
  8930. You will also be prompted for a description, and if one is given, it will
  8931. be displayed in the buffer instead of the link.
  8932. If there is already a link at point, this command will allow you to edit link
  8933. and description parts.
  8934. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8935. be selected using completion. The path to the file will be relative to the
  8936. current directory if the file is in the current directory or a subdirectory.
  8937. Otherwise, the link will be the absolute path as completed in the minibuffer
  8938. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8939. option `org-link-file-path-type'.
  8940. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8941. the current directory or below.
  8942. With three \\[universal-argument] prefixes, negate the meaning of
  8943. `org-keep-stored-link-after-insertion'.
  8944. If `org-make-link-description-function' is non-nil, this function will be
  8945. called with the link target, and the result will be the default
  8946. link description.
  8947. If the LINK-LOCATION parameter is non-nil, this value will be
  8948. used as the link location instead of reading one interactively.
  8949. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8950. be used as the default description."
  8951. (interactive "P")
  8952. (let* ((wcf (current-window-configuration))
  8953. (origbuf (current-buffer))
  8954. (region (if (org-region-active-p)
  8955. (buffer-substring (region-beginning) (region-end))))
  8956. (remove (and region (list (region-beginning) (region-end))))
  8957. (desc region)
  8958. tmphist ; byte-compile incorrectly complains about this
  8959. (link link-location)
  8960. (abbrevs org-link-abbrev-alist-local)
  8961. entry file all-prefixes auto-desc)
  8962. (cond
  8963. (link-location) ; specified by arg, just use it.
  8964. ((org-in-regexp org-bracket-link-regexp 1)
  8965. ;; We do have a link at point, and we are going to edit it.
  8966. (setq remove (list (match-beginning 0) (match-end 0)))
  8967. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8968. (setq link (read-string "Link: "
  8969. (org-link-unescape
  8970. (org-match-string-no-properties 1)))))
  8971. ((or (org-in-regexp org-angle-link-re)
  8972. (org-in-regexp org-plain-link-re))
  8973. ;; Convert to bracket link
  8974. (setq remove (list (match-beginning 0) (match-end 0))
  8975. link (read-string "Link: "
  8976. (org-remove-angle-brackets (match-string 0)))))
  8977. ((member complete-file '((4) (16)))
  8978. ;; Completing read for file names.
  8979. (setq link (org-file-complete-link complete-file)))
  8980. (t
  8981. ;; Read link, with completion for stored links.
  8982. (org-link-fontify-links-to-this-file)
  8983. (org-switch-to-buffer-other-window "*Org Links*")
  8984. (with-current-buffer "*Org Links*"
  8985. (erase-buffer)
  8986. (insert "Insert a link.
  8987. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8988. (when org-stored-links
  8989. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8990. (insert (mapconcat 'org-link-prettify
  8991. (reverse org-stored-links) "\n")))
  8992. (goto-char (point-min)))
  8993. (let ((cw (selected-window)))
  8994. (select-window (get-buffer-window "*Org Links*" 'visible))
  8995. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8996. (unless (pos-visible-in-window-p (point-max))
  8997. (org-fit-window-to-buffer))
  8998. (and (window-live-p cw) (select-window cw)))
  8999. ;; Fake a link history, containing the stored links.
  9000. (setq tmphist (append (mapcar 'car org-stored-links)
  9001. org-insert-link-history))
  9002. (setq all-prefixes (append (mapcar 'car abbrevs)
  9003. (mapcar 'car org-link-abbrev-alist)
  9004. org-link-types))
  9005. (unwind-protect
  9006. (progn
  9007. (setq link
  9008. (org-completing-read
  9009. "Link: "
  9010. (append
  9011. (mapcar (lambda (x) (concat x ":"))
  9012. all-prefixes)
  9013. (mapcar 'car org-stored-links))
  9014. nil nil nil
  9015. 'tmphist
  9016. (caar org-stored-links)))
  9017. (if (not (string-match "\\S-" link))
  9018. (user-error "No link selected"))
  9019. (mapc (lambda(l)
  9020. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9021. org-stored-links)
  9022. (if (or (member link all-prefixes)
  9023. (and (equal ":" (substring link -1))
  9024. (member (substring link 0 -1) all-prefixes)
  9025. (setq link (substring link 0 -1))))
  9026. (setq link (with-current-buffer origbuf
  9027. (org-link-try-special-completion link)))))
  9028. (set-window-configuration wcf)
  9029. (kill-buffer "*Org Links*"))
  9030. (setq entry (assoc link org-stored-links))
  9031. (or entry (push link org-insert-link-history))
  9032. (setq desc (or desc (nth 1 entry)))))
  9033. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9034. (not org-keep-stored-link-after-insertion))
  9035. (setq org-stored-links (delq (assoc link org-stored-links)
  9036. org-stored-links)))
  9037. (if (string-match org-plain-link-re link)
  9038. ;; URL-like link, normalize the use of angular brackets.
  9039. (setq link (org-remove-angle-brackets link)))
  9040. ;; Check if we are linking to the current file with a search
  9041. ;; option If yes, simplify the link by using only the search
  9042. ;; option.
  9043. (when (and buffer-file-name
  9044. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9045. (let* ((path (match-string 1 link))
  9046. (case-fold-search nil)
  9047. (search (match-string 2 link)))
  9048. (save-match-data
  9049. (if (equal (file-truename buffer-file-name) (file-truename path))
  9050. ;; We are linking to this same file, with a search option
  9051. (setq link search)))))
  9052. ;; Check if we can/should use a relative path. If yes, simplify the link
  9053. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9054. (let* ((type (match-string 1 link))
  9055. (path (match-string 2 link))
  9056. (origpath path)
  9057. (case-fold-search nil))
  9058. (cond
  9059. ((or (eq org-link-file-path-type 'absolute)
  9060. (equal complete-file '(16)))
  9061. (setq path (abbreviate-file-name (expand-file-name path))))
  9062. ((eq org-link-file-path-type 'noabbrev)
  9063. (setq path (expand-file-name path)))
  9064. ((eq org-link-file-path-type 'relative)
  9065. (setq path (file-relative-name path)))
  9066. (t
  9067. (save-match-data
  9068. (if (string-match (concat "^" (regexp-quote
  9069. (expand-file-name
  9070. (file-name-as-directory
  9071. default-directory))))
  9072. (expand-file-name path))
  9073. ;; We are linking a file with relative path name.
  9074. (setq path (substring (expand-file-name path)
  9075. (match-end 0)))
  9076. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9077. (setq link (concat type path))
  9078. (if (equal desc origpath)
  9079. (setq desc path))))
  9080. (if org-make-link-description-function
  9081. (setq desc
  9082. (or (condition-case nil
  9083. (funcall org-make-link-description-function link desc)
  9084. (error (progn (message "Can't get link description from `%s'"
  9085. (symbol-name org-make-link-description-function))
  9086. (sit-for 2) nil)))
  9087. (read-string "Description: " default-description)))
  9088. (if default-description (setq desc default-description)
  9089. (setq desc (or (and auto-desc desc)
  9090. (read-string "Description: " desc)))))
  9091. (unless (string-match "\\S-" desc) (setq desc nil))
  9092. (if remove (apply 'delete-region remove))
  9093. (insert (org-make-link-string link desc))))
  9094. (defun org-link-try-special-completion (type)
  9095. "If there is completion support for link type TYPE, offer it."
  9096. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9097. (if (functionp fun)
  9098. (funcall fun)
  9099. (read-string "Link (no completion support): " (concat type ":")))))
  9100. (defun org-file-complete-link (&optional arg)
  9101. "Create a file link using completion."
  9102. (let (file link)
  9103. (setq file (org-iread-file-name "File: "))
  9104. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9105. (pwd1 (file-name-as-directory (abbreviate-file-name
  9106. (expand-file-name ".")))))
  9107. (cond
  9108. ((equal arg '(16))
  9109. (setq link (concat
  9110. "file:"
  9111. (abbreviate-file-name (expand-file-name file)))))
  9112. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9113. (setq link (concat "file:" (match-string 1 file))))
  9114. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9115. (expand-file-name file))
  9116. (setq link (concat
  9117. "file:" (match-string 1 (expand-file-name file)))))
  9118. (t (setq link (concat "file:" file)))))
  9119. link))
  9120. (defun org-iread-file-name (&rest args)
  9121. "Read-file-name using `ido-mode' speedup if available.
  9122. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9123. See `read-file-name' for a description of parameters."
  9124. (org-without-partial-completion
  9125. (if (and org-completion-use-ido
  9126. (fboundp 'ido-read-file-name)
  9127. (boundp 'ido-mode) ido-mode
  9128. (listp (second args)))
  9129. (let ((ido-enter-matching-directory nil))
  9130. (apply 'ido-read-file-name args))
  9131. (apply 'read-file-name args))))
  9132. (defun org-completing-read (&rest args)
  9133. "Completing-read with SPACE being a normal character."
  9134. (let ((enable-recursive-minibuffers t)
  9135. (minibuffer-local-completion-map
  9136. (copy-keymap minibuffer-local-completion-map)))
  9137. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9138. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9139. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9140. (apply 'org-icompleting-read args)))
  9141. (defun org-completing-read-no-i (&rest args)
  9142. (let (org-completion-use-ido org-completion-use-iswitchb)
  9143. (apply 'org-completing-read args)))
  9144. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9145. "Use iswitch as a completing-read replacement to choose from choices.
  9146. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9147. from."
  9148. (let* ((iswitchb-use-virtual-buffers nil)
  9149. (iswitchb-make-buflist-hook
  9150. (lambda ()
  9151. (setq iswitchb-temp-buflist choices))))
  9152. (iswitchb-read-buffer prompt)))
  9153. (defun org-icompleting-read (&rest args)
  9154. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9155. (org-without-partial-completion
  9156. (if (and org-completion-use-ido
  9157. (fboundp 'ido-completing-read)
  9158. (boundp 'ido-mode) ido-mode
  9159. (listp (second args)))
  9160. (let ((ido-enter-matching-directory nil))
  9161. (apply 'ido-completing-read (concat (car args))
  9162. (if (consp (car (nth 1 args)))
  9163. (mapcar 'car (nth 1 args))
  9164. (nth 1 args))
  9165. (cddr args)))
  9166. (if (and org-completion-use-iswitchb
  9167. (boundp 'iswitchb-mode) iswitchb-mode
  9168. (listp (second args)))
  9169. (apply 'org-iswitchb-completing-read (concat (car args))
  9170. (if (consp (car (nth 1 args)))
  9171. (mapcar 'car (nth 1 args))
  9172. (nth 1 args))
  9173. (cddr args))
  9174. (apply 'completing-read args)))))
  9175. (defun org-extract-attributes (s)
  9176. "Extract the attributes cookie from a string and set as text property."
  9177. (let (a attr (start 0) key value)
  9178. (save-match-data
  9179. (when (string-match "{{\\([^}]+\\)}}$" s)
  9180. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9181. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9182. (setq key (match-string 1 a) value (match-string 2 a)
  9183. start (match-end 0)
  9184. attr (plist-put attr (intern key) value))))
  9185. (org-add-props s nil 'org-attr attr))
  9186. s))
  9187. ;;; Opening/following a link
  9188. (defvar org-link-search-failed nil)
  9189. (defvar org-open-link-functions nil
  9190. "Hook for functions finding a plain text link.
  9191. These functions must take a single argument, the link content.
  9192. They will be called for links that look like [[link text][description]]
  9193. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9194. like a filename (e.g. \"./blue.png\").
  9195. These functions will be called *before* Org attempts to resolve the
  9196. link by doing text searches in the current buffer - so if you want a
  9197. link \"[[target]]\" to still find \"<<target>>\", your function should
  9198. handle this as a special case.
  9199. When the function does handle the link, it must return a non-nil value.
  9200. If it decides that it is not responsible for this link, it must return
  9201. nil to indicate that that Org-mode can continue with other options
  9202. like exact and fuzzy text search.")
  9203. (defun org-next-link (&optional search-backward)
  9204. "Move forward to the next link.
  9205. If the link is in hidden text, expose it."
  9206. (interactive "P")
  9207. (when (and org-link-search-failed (eq this-command last-command))
  9208. (goto-char (point-min))
  9209. (message "Link search wrapped back to beginning of buffer"))
  9210. (setq org-link-search-failed nil)
  9211. (let* ((pos (point))
  9212. (ct (org-context))
  9213. (a (assoc :link ct))
  9214. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9215. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9216. ((looking-at org-any-link-re)
  9217. ;; Don't stay stuck at link without an org-link face
  9218. (forward-char (if search-backward -1 1))))
  9219. (if (funcall srch-fun org-any-link-re nil t)
  9220. (progn
  9221. (goto-char (match-beginning 0))
  9222. (if (outline-invisible-p) (org-show-context)))
  9223. (goto-char pos)
  9224. (setq org-link-search-failed t)
  9225. (message "No further link found"))))
  9226. (defun org-previous-link ()
  9227. "Move backward to the previous link.
  9228. If the link is in hidden text, expose it."
  9229. (interactive)
  9230. (funcall 'org-next-link t))
  9231. (defun org-translate-link (s)
  9232. "Translate a link string if a translation function has been defined."
  9233. (if (and org-link-translation-function
  9234. (fboundp org-link-translation-function)
  9235. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9236. (progn
  9237. (setq s (funcall org-link-translation-function
  9238. (match-string 1 s) (match-string 2 s)))
  9239. (concat (car s) ":" (cdr s)))
  9240. s))
  9241. (defun org-translate-link-from-planner (type path)
  9242. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9243. This is still an experimental function, your mileage may vary."
  9244. (cond
  9245. ((member type '("http" "https" "news" "ftp"))
  9246. ;; standard Internet links are the same.
  9247. nil)
  9248. ((and (equal type "irc") (string-match "^//" path))
  9249. ;; Planner has two / at the beginning of an irc link, we have 1.
  9250. ;; We should have zero, actually....
  9251. (setq path (substring path 1)))
  9252. ((and (equal type "lisp") (string-match "^/" path))
  9253. ;; Planner has a slash, we do not.
  9254. (setq type "elisp" path (substring path 1)))
  9255. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9256. ;; A typical message link. Planner has the id after the final slash,
  9257. ;; we separate it with a hash mark
  9258. (setq path (concat (match-string 1 path) "#"
  9259. (org-remove-angle-brackets (match-string 2 path))))))
  9260. (cons type path))
  9261. (defun org-find-file-at-mouse (ev)
  9262. "Open file link or URL at mouse."
  9263. (interactive "e")
  9264. (mouse-set-point ev)
  9265. (org-open-at-point 'in-emacs))
  9266. (defun org-open-at-mouse (ev)
  9267. "Open file link or URL at mouse.
  9268. See the docstring of `org-open-file' for details."
  9269. (interactive "e")
  9270. (mouse-set-point ev)
  9271. (if (eq major-mode 'org-agenda-mode)
  9272. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9273. (org-open-at-point))
  9274. (defvar org-window-config-before-follow-link nil
  9275. "The window configuration before following a link.
  9276. This is saved in case the need arises to restore it.")
  9277. (defvar org-open-link-marker (make-marker)
  9278. "Marker pointing to the location where `org-open-at-point; was called.")
  9279. ;;;###autoload
  9280. (defun org-open-at-point-global ()
  9281. "Follow a link like Org-mode does.
  9282. This command can be called in any mode to follow a link that has
  9283. Org-mode syntax."
  9284. (interactive)
  9285. (org-run-like-in-org-mode 'org-open-at-point))
  9286. ;;;###autoload
  9287. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9288. "Open a link in the string S, as if it was in Org-mode."
  9289. (interactive "sLink: \nP")
  9290. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9291. (with-temp-buffer
  9292. (let ((org-inhibit-startup (not reference-buffer)))
  9293. (org-mode)
  9294. (insert s)
  9295. (goto-char (point-min))
  9296. (when reference-buffer
  9297. (setq org-link-abbrev-alist-local
  9298. (with-current-buffer reference-buffer
  9299. org-link-abbrev-alist-local)))
  9300. (org-open-at-point arg reference-buffer)))))
  9301. (defvar org-open-at-point-functions nil
  9302. "Hook that is run when following a link at point.
  9303. Functions in this hook must return t if they identify and follow
  9304. a link at point. If they don't find anything interesting at point,
  9305. they must return nil.")
  9306. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9307. (defun org-open-at-point (&optional arg reference-buffer)
  9308. "Open link at or after point.
  9309. If there is no link at point, this function will search forward up to
  9310. the end of the current line.
  9311. Normally, files will be opened by an appropriate application. If the
  9312. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9313. With a double prefix argument, try to open outside of Emacs, in the
  9314. application the system uses for this file type."
  9315. (interactive "P")
  9316. ;; if in a code block, then open the block's results
  9317. (unless (call-interactively #'org-babel-open-src-block-result)
  9318. (org-load-modules-maybe)
  9319. (move-marker org-open-link-marker (point))
  9320. (setq org-window-config-before-follow-link (current-window-configuration))
  9321. (org-remove-occur-highlights nil nil t)
  9322. (cond
  9323. ((and (org-at-heading-p)
  9324. (not (org-at-timestamp-p t))
  9325. (not (org-in-regexp
  9326. (concat org-plain-link-re "\\|"
  9327. org-bracket-link-regexp "\\|"
  9328. org-angle-link-re "\\|"
  9329. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9330. (not (get-text-property (point) 'org-linked-text)))
  9331. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9332. (lk0 (car lkall))
  9333. (lk (if (stringp lk0) (list lk0) lk0))
  9334. (lkend (cdr lkall)))
  9335. (mapcar (lambda(l)
  9336. (search-forward l nil lkend)
  9337. (goto-char (match-beginning 0))
  9338. (org-open-at-point))
  9339. lk))
  9340. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9341. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9342. ((and (org-at-timestamp-p t)
  9343. (not (org-in-regexp org-bracket-link-regexp)))
  9344. (org-follow-timestamp-link))
  9345. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9346. (not (org-in-regexp org-any-link-re)))
  9347. (org-footnote-action))
  9348. (t
  9349. (let (type path link line search (pos (point)))
  9350. (catch 'match
  9351. (save-excursion
  9352. (skip-chars-forward "^]\n\r")
  9353. (when (org-in-regexp org-bracket-link-regexp 1)
  9354. (setq link (org-extract-attributes
  9355. (org-link-unescape (org-match-string-no-properties 1))))
  9356. (while (string-match " *\n *" link)
  9357. (setq link (replace-match " " t t link)))
  9358. (setq link (org-link-expand-abbrev link))
  9359. (cond
  9360. ((or (file-name-absolute-p link)
  9361. (string-match "^\\.\\.?/" link))
  9362. (setq type "file" path link))
  9363. ((string-match org-link-re-with-space3 link)
  9364. (setq type (match-string 1 link) path (match-string 2 link)))
  9365. ((string-match "^help:+\\(.+\\)" link)
  9366. (setq type "help" path (match-string 1 link)))
  9367. (t (setq type "thisfile" path link)))
  9368. (throw 'match t)))
  9369. (when (get-text-property (point) 'org-linked-text)
  9370. (setq type "thisfile"
  9371. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9372. (1+ (point)) (point))
  9373. path (buffer-substring
  9374. (or (previous-single-property-change pos 'org-linked-text)
  9375. (point-min))
  9376. (or (next-single-property-change pos 'org-linked-text)
  9377. (point-max)))
  9378. ;; Ensure we will search for a <<<radio>>> link, not
  9379. ;; a simple reference like <<ref>>
  9380. path (concat "<" path))
  9381. (throw 'match t))
  9382. (save-excursion
  9383. (when (or (org-in-regexp org-angle-link-re)
  9384. (let ((match (org-in-regexp org-plain-link-re)))
  9385. ;; Check a plain link is not within a bracket link
  9386. (and match
  9387. (save-excursion
  9388. (progn
  9389. (goto-char (car match))
  9390. (not (org-in-regexp org-bracket-link-regexp))))))
  9391. (let ((line_ending (save-excursion (end-of-line) (point))))
  9392. ;; We are in a line before a plain or bracket link
  9393. (or (re-search-forward org-plain-link-re line_ending t)
  9394. (re-search-forward org-bracket-link-regexp line_ending t))))
  9395. (setq type (match-string 1)
  9396. path (org-link-unescape (match-string 2)))
  9397. (throw 'match t)))
  9398. (save-excursion
  9399. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9400. (setq type "tags"
  9401. path (match-string 1))
  9402. (while (string-match ":" path)
  9403. (setq path (replace-match "+" t t path)))
  9404. (throw 'match t)))
  9405. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9406. (setq type "tree-match"
  9407. path (match-string 1))
  9408. (throw 'match t)))
  9409. (unless path
  9410. (user-error "No link found"))
  9411. ;; switch back to reference buffer
  9412. ;; needed when if called in a temporary buffer through
  9413. ;; org-open-link-from-string
  9414. (with-current-buffer (or reference-buffer (current-buffer))
  9415. ;; Remove any trailing spaces in path
  9416. (if (string-match " +\\'" path)
  9417. (setq path (replace-match "" t t path)))
  9418. (if (and org-link-translation-function
  9419. (fboundp org-link-translation-function))
  9420. ;; Check if we need to translate the link
  9421. (let ((tmp (funcall org-link-translation-function type path)))
  9422. (setq type (car tmp) path (cdr tmp))))
  9423. (cond
  9424. ((assoc type org-link-protocols)
  9425. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9426. ((equal type "help")
  9427. (let ((f-or-v (intern path)))
  9428. (cond ((fboundp f-or-v)
  9429. (describe-function f-or-v))
  9430. ((boundp f-or-v)
  9431. (describe-variable f-or-v))
  9432. (t (error "Not a known function or variable")))))
  9433. ((equal type "mailto")
  9434. (let ((cmd (car org-link-mailto-program))
  9435. (args (cdr org-link-mailto-program)) args1
  9436. (address path) (subject "") a)
  9437. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9438. (setq address (match-string 1 path)
  9439. subject (org-link-escape (match-string 2 path))))
  9440. (while args
  9441. (cond
  9442. ((not (stringp (car args))) (push (pop args) args1))
  9443. (t (setq a (pop args))
  9444. (if (string-match "%a" a)
  9445. (setq a (replace-match address t t a)))
  9446. (if (string-match "%s" a)
  9447. (setq a (replace-match subject t t a)))
  9448. (push a args1))))
  9449. (apply cmd (nreverse args1))))
  9450. ((member type '("http" "https" "ftp" "news"))
  9451. (browse-url (concat type ":" (org-link-escape-browser path))))
  9452. ((string= type "doi")
  9453. (browse-url (concat org-doi-server-url
  9454. (org-link-escape-browser path))))
  9455. ((member type '("message"))
  9456. (browse-url (concat type ":" path)))
  9457. ((string= type "tags")
  9458. (org-tags-view arg path))
  9459. ((string= type "tree-match")
  9460. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9461. ((string= type "file")
  9462. (if (string-match "::\\([0-9]+\\)\\'" path)
  9463. (setq line (string-to-number (match-string 1 path))
  9464. path (substring path 0 (match-beginning 0)))
  9465. (if (string-match "::\\(.+\\)\\'" path)
  9466. (setq search (match-string 1 path)
  9467. path (substring path 0 (match-beginning 0)))))
  9468. (if (string-match "[*?{]" (file-name-nondirectory path))
  9469. (dired path)
  9470. (org-open-file path arg line search)))
  9471. ((string= type "shell")
  9472. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9473. (cmd path))
  9474. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9475. (string-match org-confirm-shell-link-not-regexp cmd))
  9476. (not org-confirm-shell-link-function)
  9477. (funcall org-confirm-shell-link-function
  9478. (format "Execute \"%s\" in shell? "
  9479. (org-add-props cmd nil
  9480. 'face 'org-warning))))
  9481. (progn
  9482. (message "Executing %s" cmd)
  9483. (shell-command cmd buf)
  9484. (if (featurep 'midnight)
  9485. (setq clean-buffer-list-kill-buffer-names
  9486. (cons buf clean-buffer-list-kill-buffer-names))))
  9487. (error "Abort"))))
  9488. ((string= type "elisp")
  9489. (let ((cmd path))
  9490. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9491. (string-match org-confirm-elisp-link-not-regexp cmd))
  9492. (not org-confirm-elisp-link-function)
  9493. (funcall org-confirm-elisp-link-function
  9494. (format "Execute \"%s\" as elisp? "
  9495. (org-add-props cmd nil
  9496. 'face 'org-warning))))
  9497. (message "%s => %s" cmd
  9498. (if (equal (string-to-char cmd) ?\()
  9499. (eval (read cmd))
  9500. (call-interactively (read cmd))))
  9501. (error "Abort"))))
  9502. ((and (string= type "thisfile")
  9503. (or (run-hook-with-args-until-success
  9504. 'org-open-link-functions path)
  9505. (and link
  9506. (string-match "^id:" link)
  9507. (or (featurep 'org-id) (require 'org-id))
  9508. (progn
  9509. (funcall (nth 1 (assoc "id" org-link-protocols))
  9510. (substring path 3))
  9511. t)))))
  9512. ((string= type "thisfile")
  9513. (if arg
  9514. (switch-to-buffer-other-window
  9515. (org-get-buffer-for-internal-link (current-buffer)))
  9516. (org-mark-ring-push))
  9517. (let ((cmd `(org-link-search
  9518. ,path
  9519. ,(cond ((equal arg '(4)) ''occur)
  9520. ((equal arg '(16)) ''org-occur))
  9521. ,pos)))
  9522. (condition-case nil (let ((org-link-search-inhibit-query t))
  9523. (eval cmd))
  9524. (error (progn (widen) (eval cmd))))))
  9525. (t (browse-url-at-point)))))))
  9526. (move-marker org-open-link-marker nil)
  9527. (run-hook-with-args 'org-follow-link-hook)))
  9528. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9529. "Offer links in the current entry and return the selected link.
  9530. If there is only one link, return it.
  9531. If NTH is an integer, return the NTH link found.
  9532. If ZERO is a string, check also this string for a link, and if
  9533. there is one, return it."
  9534. (with-current-buffer buffer
  9535. (save-excursion
  9536. (save-restriction
  9537. (widen)
  9538. (goto-char marker)
  9539. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9540. "\\(" org-angle-link-re "\\)\\|"
  9541. "\\(" org-plain-link-re "\\)"))
  9542. (cnt ?0)
  9543. (in-emacs (if (integerp nth) nil nth))
  9544. have-zero end links link c)
  9545. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9546. (push (match-string 0 zero) links)
  9547. (setq cnt (1- cnt) have-zero t))
  9548. (save-excursion
  9549. (org-back-to-heading t)
  9550. (setq end (save-excursion (outline-next-heading) (point)))
  9551. (while (re-search-forward re end t)
  9552. (push (match-string 0) links))
  9553. (setq links (org-uniquify (reverse links))))
  9554. (cond
  9555. ((null links)
  9556. (message "No links"))
  9557. ((equal (length links) 1)
  9558. (setq link (car links)))
  9559. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9560. (setq link (nth (if have-zero nth (1- nth)) links)))
  9561. (t ; we have to select a link
  9562. (save-excursion
  9563. (save-window-excursion
  9564. (delete-other-windows)
  9565. (with-output-to-temp-buffer "*Select Link*"
  9566. (mapc (lambda (l)
  9567. (if (not (string-match org-bracket-link-regexp l))
  9568. (princ (format "[%c] %s\n" (incf cnt)
  9569. (org-remove-angle-brackets l)))
  9570. (if (match-end 3)
  9571. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9572. (match-string 3 l) (match-string 1 l)))
  9573. (princ (format "[%c] %s\n" (incf cnt)
  9574. (match-string 1 l))))))
  9575. links))
  9576. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9577. (message "Select link to open, RET to open all:")
  9578. (setq c (read-char-exclusive))
  9579. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9580. (when (equal c ?q) (error "Abort"))
  9581. (if (equal c ?\C-m)
  9582. (setq link links)
  9583. (setq nth (- c ?0))
  9584. (if have-zero (setq nth (1+ nth)))
  9585. (unless (and (integerp nth) (>= (length links) nth))
  9586. (user-error "Invalid link selection"))
  9587. (setq link (nth (1- nth) links)))))
  9588. (cons link end))))))
  9589. ;; Add special file links that specify the way of opening
  9590. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9591. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9592. (defun org-open-file-with-system (path)
  9593. "Open file at PATH using the system way of opening it."
  9594. (org-open-file path 'system))
  9595. (defun org-open-file-with-emacs (path)
  9596. "Open file at PATH in Emacs."
  9597. (org-open-file path 'emacs))
  9598. ;;; File search
  9599. (defvar org-create-file-search-functions nil
  9600. "List of functions to construct the right search string for a file link.
  9601. These functions are called in turn with point at the location to
  9602. which the link should point.
  9603. A function in the hook should first test if it would like to
  9604. handle this file type, for example by checking the `major-mode'
  9605. or the file extension. If it decides not to handle this file, it
  9606. should just return nil to give other functions a chance. If it
  9607. does handle the file, it must return the search string to be used
  9608. when following the link. The search string will be part of the
  9609. file link, given after a double colon, and `org-open-at-point'
  9610. will automatically search for it. If special measures must be
  9611. taken to make the search successful, another function should be
  9612. added to the companion hook `org-execute-file-search-functions',
  9613. which see.
  9614. A function in this hook may also use `setq' to set the variable
  9615. `description' to provide a suggestion for the descriptive text to
  9616. be used for this link when it gets inserted into an Org-mode
  9617. buffer with \\[org-insert-link].")
  9618. (defvar org-execute-file-search-functions nil
  9619. "List of functions to execute a file search triggered by a link.
  9620. Functions added to this hook must accept a single argument, the
  9621. search string that was part of the file link, the part after the
  9622. double colon. The function must first check if it would like to
  9623. handle this search, for example by checking the `major-mode' or
  9624. the file extension. If it decides not to handle this search, it
  9625. should just return nil to give other functions a chance. If it
  9626. does handle the search, it must return a non-nil value to keep
  9627. other functions from trying.
  9628. Each function can access the current prefix argument through the
  9629. variable `current-prefix-arg'. Note that a single prefix is used
  9630. to force opening a link in Emacs, so it may be good to only use a
  9631. numeric or double prefix to guide the search function.
  9632. In case this is needed, a function in this hook can also restore
  9633. the window configuration before `org-open-at-point' was called using:
  9634. (set-window-configuration org-window-config-before-follow-link)")
  9635. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9636. (defun org-link-search (s &optional type avoid-pos stealth)
  9637. "Search for a link search option.
  9638. If S is surrounded by forward slashes, it is interpreted as a
  9639. regular expression. In org-mode files, this will create an `org-occur'
  9640. sparse tree. In ordinary files, `occur' will be used to list matches.
  9641. If the current buffer is in `dired-mode', grep will be used to search
  9642. in all files. If AVOID-POS is given, ignore matches near that position.
  9643. When optional argument STEALTH is non-nil, do not modify
  9644. visibility around point, thus ignoring
  9645. `org-show-hierarchy-above', `org-show-following-heading' and
  9646. `org-show-siblings' variables."
  9647. (let ((case-fold-search t)
  9648. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9649. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9650. (append '(("") (" ") ("\t") ("\n"))
  9651. org-emphasis-alist)
  9652. "\\|") "\\)"))
  9653. (pos (point))
  9654. (pre nil) (post nil)
  9655. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9656. (cond
  9657. ;; First check if there are any special search functions
  9658. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9659. ;; Now try the builtin stuff
  9660. ((and (equal (string-to-char s0) ?#)
  9661. (> (length s0) 1)
  9662. (save-excursion
  9663. (goto-char (point-min))
  9664. (and
  9665. (re-search-forward
  9666. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9667. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9668. (setq type 'dedicated
  9669. pos (match-beginning 0))))
  9670. ;; There is an exact target for this
  9671. (goto-char pos)
  9672. (org-back-to-heading t)))
  9673. ((save-excursion
  9674. (goto-char (point-min))
  9675. (and
  9676. (re-search-forward
  9677. (concat "<<" (regexp-quote s0) ">>") nil t)
  9678. (setq type 'dedicated
  9679. pos (match-beginning 0))))
  9680. ;; There is an exact target for this
  9681. (goto-char pos))
  9682. ((save-excursion
  9683. (goto-char (point-min))
  9684. (and
  9685. (re-search-forward
  9686. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9687. (setq type 'dedicated pos (match-beginning 0))))
  9688. ;; Found an element with a matching #+name affiliated keyword.
  9689. (goto-char pos))
  9690. ((and (string-match "^(\\(.*\\))$" s0)
  9691. (save-excursion
  9692. (goto-char (point-min))
  9693. (and
  9694. (re-search-forward
  9695. (concat "[^[]" (regexp-quote
  9696. (format org-coderef-label-format
  9697. (match-string 1 s0))))
  9698. nil t)
  9699. (setq type 'dedicated
  9700. pos (1+ (match-beginning 0))))))
  9701. ;; There is a coderef target for this
  9702. (goto-char pos))
  9703. ((string-match "^/\\(.*\\)/$" s)
  9704. ;; A regular expression
  9705. (cond
  9706. ((derived-mode-p 'org-mode)
  9707. (org-occur (match-string 1 s)))
  9708. (t (org-do-occur (match-string 1 s)))))
  9709. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9710. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9711. (goto-char (point-min))
  9712. (cond
  9713. ((let (case-fold-search)
  9714. (re-search-forward (format org-complex-heading-regexp-format
  9715. (regexp-quote s))
  9716. nil t))
  9717. ;; OK, found a match
  9718. (setq type 'dedicated)
  9719. (goto-char (match-beginning 0)))
  9720. ((and (not org-link-search-inhibit-query)
  9721. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9722. (y-or-n-p "No match - create this as a new heading? "))
  9723. (goto-char (point-max))
  9724. (or (bolp) (newline))
  9725. (insert "* " s "\n")
  9726. (beginning-of-line 0))
  9727. (t
  9728. (goto-char pos)
  9729. (error "No match"))))
  9730. (t
  9731. ;; A normal search string
  9732. (when (equal (string-to-char s) ?*)
  9733. ;; Anchor on headlines, post may include tags.
  9734. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9735. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9736. s (substring s 1)))
  9737. (remove-text-properties
  9738. 0 (length s)
  9739. '(face nil mouse-face nil keymap nil fontified nil) s)
  9740. ;; Make a series of regular expressions to find a match
  9741. (setq words (org-split-string s "[ \n\r\t]+")
  9742. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9743. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9744. "\\)" markers)
  9745. re2a_ (concat "\\(" (mapconcat 'downcase words
  9746. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9747. re2a (concat "[ \t\r\n]" re2a_)
  9748. re4_ (concat "\\(" (mapconcat 'downcase words
  9749. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9750. re4 (concat "[^a-zA-Z_]" re4_)
  9751. re1 (concat pre re2 post)
  9752. re3 (concat pre (if pre re4_ re4) post)
  9753. re5 (concat pre ".*" re4)
  9754. re2 (concat pre re2)
  9755. re2a (concat pre (if pre re2a_ re2a))
  9756. re4 (concat pre (if pre re4_ re4))
  9757. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9758. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9759. re5 "\\)"))
  9760. (cond
  9761. ((eq type 'org-occur) (org-occur reall))
  9762. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9763. (t (goto-char (point-min))
  9764. (setq type 'fuzzy)
  9765. (if (or (and (org-search-not-self 1 re0 nil t)
  9766. (setq type 'dedicated))
  9767. (org-search-not-self 1 re1 nil t)
  9768. (org-search-not-self 1 re2 nil t)
  9769. (org-search-not-self 1 re2a nil t)
  9770. (org-search-not-self 1 re3 nil t)
  9771. (org-search-not-self 1 re4 nil t)
  9772. (org-search-not-self 1 re5 nil t))
  9773. (goto-char (match-beginning 1))
  9774. (goto-char pos)
  9775. (error "No match"))))))
  9776. (and (derived-mode-p 'org-mode)
  9777. (not stealth)
  9778. (org-show-context 'link-search))
  9779. type))
  9780. (defun org-search-not-self (group &rest args)
  9781. "Execute `re-search-forward', but only accept matches that do not
  9782. enclose the position of `org-open-link-marker'."
  9783. (let ((m org-open-link-marker))
  9784. (catch 'exit
  9785. (while (apply 're-search-forward args)
  9786. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9787. (goto-char (match-end group))
  9788. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9789. (> (match-beginning 0) (marker-position m))
  9790. (< (match-end 0) (marker-position m)))
  9791. (save-match-data
  9792. (or (not (org-in-regexp
  9793. org-bracket-link-analytic-regexp 1))
  9794. (not (match-end 4)) ; no description
  9795. (and (<= (match-beginning 4) (point))
  9796. (>= (match-end 4) (point))))))
  9797. (throw 'exit (point))))))))
  9798. (defun org-get-buffer-for-internal-link (buffer)
  9799. "Return a buffer to be used for displaying the link target of internal links."
  9800. (cond
  9801. ((not org-display-internal-link-with-indirect-buffer)
  9802. buffer)
  9803. ((string-match "(Clone)$" (buffer-name buffer))
  9804. (message "Buffer is already a clone, not making another one")
  9805. ;; we also do not modify visibility in this case
  9806. buffer)
  9807. (t ; make a new indirect buffer for displaying the link
  9808. (let* ((bn (buffer-name buffer))
  9809. (ibn (concat bn "(Clone)"))
  9810. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9811. (with-current-buffer ib (org-overview))
  9812. ib))))
  9813. (defun org-do-occur (regexp &optional cleanup)
  9814. "Call the Emacs command `occur'.
  9815. If CLEANUP is non-nil, remove the printout of the regular expression
  9816. in the *Occur* buffer. This is useful if the regex is long and not useful
  9817. to read."
  9818. (occur regexp)
  9819. (when cleanup
  9820. (let ((cwin (selected-window)) win beg end)
  9821. (when (setq win (get-buffer-window "*Occur*"))
  9822. (select-window win))
  9823. (goto-char (point-min))
  9824. (when (re-search-forward "match[a-z]+" nil t)
  9825. (setq beg (match-end 0))
  9826. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9827. (setq end (1- (match-beginning 0)))))
  9828. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9829. (goto-char (point-min))
  9830. (select-window cwin))))
  9831. ;;; The mark ring for links jumps
  9832. (defvar org-mark-ring nil
  9833. "Mark ring for positions before jumps in Org-mode.")
  9834. (defvar org-mark-ring-last-goto nil
  9835. "Last position in the mark ring used to go back.")
  9836. ;; Fill and close the ring
  9837. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9838. (loop for i from 1 to org-mark-ring-length do
  9839. (push (make-marker) org-mark-ring))
  9840. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9841. org-mark-ring)
  9842. (defun org-mark-ring-push (&optional pos buffer)
  9843. "Put the current position or POS into the mark ring and rotate it."
  9844. (interactive)
  9845. (setq pos (or pos (point)))
  9846. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9847. (move-marker (car org-mark-ring)
  9848. (or pos (point))
  9849. (or buffer (current-buffer)))
  9850. (message "%s"
  9851. (substitute-command-keys
  9852. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9853. (defun org-mark-ring-goto (&optional n)
  9854. "Jump to the previous position in the mark ring.
  9855. With prefix arg N, jump back that many stored positions. When
  9856. called several times in succession, walk through the entire ring.
  9857. Org-mode commands jumping to a different position in the current file,
  9858. or to another Org-mode file, automatically push the old position
  9859. onto the ring."
  9860. (interactive "p")
  9861. (let (p m)
  9862. (if (eq last-command this-command)
  9863. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9864. (setq p org-mark-ring))
  9865. (setq org-mark-ring-last-goto p)
  9866. (setq m (car p))
  9867. (org-pop-to-buffer-same-window (marker-buffer m))
  9868. (goto-char m)
  9869. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9870. (defun org-remove-angle-brackets (s)
  9871. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9872. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9873. s)
  9874. (defun org-add-angle-brackets (s)
  9875. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9876. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9877. s)
  9878. (defun org-remove-double-quotes (s)
  9879. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9880. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9881. s)
  9882. ;;; Following specific links
  9883. (defun org-follow-timestamp-link ()
  9884. "Open an agenda view for the time-stamp date/range at point."
  9885. (cond
  9886. ((org-at-date-range-p t)
  9887. (let ((org-agenda-start-on-weekday)
  9888. (t1 (match-string 1))
  9889. (t2 (match-string 2)) tt1 tt2)
  9890. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9891. tt2 (time-to-days (org-time-string-to-time t2)))
  9892. (let ((org-agenda-buffer-tmp-name
  9893. (format "*Org Agenda(a:%s)"
  9894. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9895. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9896. ((org-at-timestamp-p t)
  9897. (let ((org-agenda-buffer-tmp-name
  9898. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9899. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9900. (substring (match-string 1) 0 10)))
  9901. 1)))
  9902. (t (error "This should not happen"))))
  9903. ;;; Following file links
  9904. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9905. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9906. (declare-function mailcap-mime-info
  9907. "mailcap" (string &optional request no-decode))
  9908. (defvar org-wait nil)
  9909. (defun org-open-file (path &optional in-emacs line search)
  9910. "Open the file at PATH.
  9911. First, this expands any special file name abbreviations. Then the
  9912. configuration variable `org-file-apps' is checked if it contains an
  9913. entry for this file type, and if yes, the corresponding command is launched.
  9914. If no application is found, Emacs simply visits the file.
  9915. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9916. With a double \\[universal-argument] \\[universal-argument] \
  9917. prefix arg, Org tries to avoid opening in Emacs
  9918. and to use an external application to visit the file.
  9919. Optional LINE specifies a line to go to, optional SEARCH a string
  9920. to search for. If LINE or SEARCH is given, the file will be
  9921. opened in Emacs, unless an entry from org-file-apps that makes
  9922. use of groups in a regexp matches.
  9923. If you want to change the way frames are used when following a
  9924. link, please customize `org-link-frame-setup'.
  9925. If the file does not exist, an error is thrown."
  9926. (let* ((file (if (equal path "")
  9927. buffer-file-name
  9928. (substitute-in-file-name (expand-file-name path))))
  9929. (file-apps (append org-file-apps (org-default-apps)))
  9930. (apps (org-remove-if
  9931. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9932. (apps-dlink (org-remove-if-not
  9933. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9934. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9935. (dirp (if remp nil (file-directory-p file)))
  9936. (file (if (and dirp org-open-directory-means-index-dot-org)
  9937. (concat (file-name-as-directory file) "index.org")
  9938. file))
  9939. (a-m-a-p (assq 'auto-mode apps))
  9940. (dfile (downcase file))
  9941. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9942. (link (cond ((and (eq line nil)
  9943. (eq search nil))
  9944. file)
  9945. (line
  9946. (concat file "::" (number-to-string line)))
  9947. (search
  9948. (concat file "::" search))))
  9949. (dlink (downcase link))
  9950. (old-buffer (current-buffer))
  9951. (old-pos (point))
  9952. (old-mode major-mode)
  9953. ext cmd link-match-data)
  9954. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9955. (setq ext (match-string 1 dfile))
  9956. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9957. (setq ext (match-string 1 dfile))))
  9958. (cond
  9959. ((member in-emacs '((16) system))
  9960. (setq cmd (cdr (assoc 'system apps))))
  9961. (in-emacs (setq cmd 'emacs))
  9962. (t
  9963. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9964. (and dirp (cdr (assoc 'directory apps)))
  9965. ; first, try matching against apps-dlink
  9966. ; if we get a match here, store the match data for later
  9967. (let ((match (assoc-default dlink apps-dlink
  9968. 'string-match)))
  9969. (if match
  9970. (progn (setq link-match-data (match-data))
  9971. match)
  9972. (progn (setq in-emacs (or in-emacs line search))
  9973. nil))) ; if we have no match in apps-dlink,
  9974. ; always open the file in emacs if line or search
  9975. ; is given (for backwards compatibility)
  9976. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9977. 'string-match)
  9978. (cdr (assoc ext apps))
  9979. (cdr (assoc t apps))))))
  9980. (when (eq cmd 'system)
  9981. (setq cmd (cdr (assoc 'system apps))))
  9982. (when (eq cmd 'default)
  9983. (setq cmd (cdr (assoc t apps))))
  9984. (when (eq cmd 'mailcap)
  9985. (require 'mailcap)
  9986. (mailcap-parse-mailcaps)
  9987. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9988. (command (mailcap-mime-info mime-type)))
  9989. (if (stringp command)
  9990. (setq cmd command)
  9991. (setq cmd 'emacs))))
  9992. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9993. (not (file-exists-p file))
  9994. (not org-open-non-existing-files))
  9995. (user-error "No such file: %s" file))
  9996. (cond
  9997. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9998. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9999. (while (string-match "['\"]%s['\"]" cmd)
  10000. (setq cmd (replace-match "%s" t t cmd)))
  10001. (while (string-match "%s" cmd)
  10002. (setq cmd (replace-match
  10003. (save-match-data
  10004. (shell-quote-argument
  10005. (convert-standard-filename file)))
  10006. t t cmd)))
  10007. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10008. (save-match-data
  10009. (let ((match-index 1)
  10010. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10011. (set-match-data link-match-data)
  10012. (while (<= match-index number-of-groups)
  10013. (let ((regex (concat "%" (number-to-string match-index)))
  10014. (replace-with (match-string match-index dlink)))
  10015. (while (string-match regex cmd)
  10016. (setq cmd (replace-match replace-with t t cmd))))
  10017. (setq match-index (+ match-index 1)))))
  10018. (save-window-excursion
  10019. (message "Running %s...done" cmd)
  10020. (start-process-shell-command cmd nil cmd)
  10021. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10022. ((or (stringp cmd)
  10023. (eq cmd 'emacs))
  10024. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10025. (widen)
  10026. (if line (org-goto-line line)
  10027. (if search (org-link-search search))))
  10028. ((consp cmd)
  10029. (let ((file (convert-standard-filename file)))
  10030. (save-match-data
  10031. (set-match-data link-match-data)
  10032. (eval cmd))))
  10033. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10034. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10035. (or (not (equal old-buffer (current-buffer)))
  10036. (not (equal old-pos (point))))
  10037. (org-mark-ring-push old-pos old-buffer))))
  10038. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10039. "This function returns non-nil if `entry' uses a regular
  10040. expression which should be matched against the whole link by
  10041. org-open-file.
  10042. It assumes that is the case when the entry uses a regular
  10043. expression which has at least one grouping construct and the
  10044. action is either a lisp form or a command string containing
  10045. '%1', i.e. using at least one subexpression match as a
  10046. parameter."
  10047. (let ((selector (car entry))
  10048. (action (cdr entry)))
  10049. (if (stringp selector)
  10050. (and (> (regexp-opt-depth selector) 0)
  10051. (or (and (stringp action)
  10052. (string-match "%[0-9]" action))
  10053. (consp action)))
  10054. nil)))
  10055. (defun org-default-apps ()
  10056. "Return the default applications for this operating system."
  10057. (cond
  10058. ((eq system-type 'darwin)
  10059. org-file-apps-defaults-macosx)
  10060. ((eq system-type 'windows-nt)
  10061. org-file-apps-defaults-windowsnt)
  10062. (t org-file-apps-defaults-gnu)))
  10063. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10064. "Convert extensions to regular expressions in the cars of LIST.
  10065. Also, weed out any non-string entries, because the return value is used
  10066. only for regexp matching.
  10067. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10068. point to the symbol `emacs', indicating that the file should
  10069. be opened in Emacs."
  10070. (append
  10071. (delq nil
  10072. (mapcar (lambda (x)
  10073. (if (not (stringp (car x)))
  10074. nil
  10075. (if (string-match "\\W" (car x))
  10076. x
  10077. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10078. list))
  10079. (if add-auto-mode
  10080. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10081. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10082. (defun org-file-remote-p (file)
  10083. "Test whether FILE specifies a location on a remote system.
  10084. Return non-nil if the location is indeed remote.
  10085. For example, the filename \"/user@host:/foo\" specifies a location
  10086. on the system \"/user@host:\"."
  10087. (cond ((fboundp 'file-remote-p)
  10088. (file-remote-p file))
  10089. ((fboundp 'tramp-handle-file-remote-p)
  10090. (tramp-handle-file-remote-p file))
  10091. ((and (boundp 'ange-ftp-name-format)
  10092. (string-match (car ange-ftp-name-format) file))
  10093. t)))
  10094. ;;;; Refiling
  10095. (defun org-get-org-file ()
  10096. "Read a filename, with default directory `org-directory'."
  10097. (let ((default (or org-default-notes-file remember-data-file)))
  10098. (read-file-name (format "File name [%s]: " default)
  10099. (file-name-as-directory org-directory)
  10100. default)))
  10101. (defun org-notes-order-reversed-p ()
  10102. "Check if the current file should receive notes in reversed order."
  10103. (cond
  10104. ((not org-reverse-note-order) nil)
  10105. ((eq t org-reverse-note-order) t)
  10106. ((not (listp org-reverse-note-order)) nil)
  10107. (t (catch 'exit
  10108. (let ((all org-reverse-note-order)
  10109. entry)
  10110. (while (setq entry (pop all))
  10111. (if (string-match (car entry) buffer-file-name)
  10112. (throw 'exit (cdr entry))))
  10113. nil)))))
  10114. (defvar org-refile-target-table nil
  10115. "The list of refile targets, created by `org-refile'.")
  10116. (defvar org-agenda-new-buffers nil
  10117. "Buffers created to visit agenda files.")
  10118. (defvar org-refile-cache nil
  10119. "Cache for refile targets.")
  10120. (defvar org-refile-markers nil
  10121. "All the markers used for caching refile locations.")
  10122. (defun org-refile-marker (pos)
  10123. "Get a new refile marker, but only if caching is in use."
  10124. (if (not org-refile-use-cache)
  10125. pos
  10126. (let ((m (make-marker)))
  10127. (move-marker m pos)
  10128. (push m org-refile-markers)
  10129. m)))
  10130. (defun org-refile-cache-clear ()
  10131. "Clear the refile cache and disable all the markers."
  10132. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10133. (setq org-refile-markers nil)
  10134. (setq org-refile-cache nil)
  10135. (message "Refile cache has been cleared"))
  10136. (defun org-refile-cache-check-set (set)
  10137. "Check if all the markers in the cache still have live buffers."
  10138. (let (marker)
  10139. (catch 'exit
  10140. (while (and set (setq marker (nth 3 (pop set))))
  10141. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10142. (when (and marker (null (marker-buffer marker)))
  10143. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10144. (sit-for 3)
  10145. (throw 'exit nil)))
  10146. t)))
  10147. (defun org-refile-cache-put (set &rest identifiers)
  10148. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10149. (let* ((key (sha1 (prin1-to-string identifiers)))
  10150. (entry (assoc key org-refile-cache)))
  10151. (if entry
  10152. (setcdr entry set)
  10153. (push (cons key set) org-refile-cache))))
  10154. (defun org-refile-cache-get (&rest identifiers)
  10155. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10156. (cond
  10157. ((not org-refile-cache) nil)
  10158. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10159. (t
  10160. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10161. org-refile-cache))))
  10162. (and set (org-refile-cache-check-set set) set)))))
  10163. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10164. "Produce a table with refile targets."
  10165. (let ((case-fold-search nil)
  10166. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10167. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10168. targets tgs txt re files f desc descre fast-path-p level pos0)
  10169. (message "Getting targets...")
  10170. (with-current-buffer (or default-buffer (current-buffer))
  10171. (while (setq entry (pop entries))
  10172. (setq files (car entry) desc (cdr entry))
  10173. (setq fast-path-p nil)
  10174. (cond
  10175. ((null files) (setq files (list (current-buffer))))
  10176. ((eq files 'org-agenda-files)
  10177. (setq files (org-agenda-files 'unrestricted)))
  10178. ((and (symbolp files) (fboundp files))
  10179. (setq files (funcall files)))
  10180. ((and (symbolp files) (boundp files))
  10181. (setq files (symbol-value files))))
  10182. (if (stringp files) (setq files (list files)))
  10183. (cond
  10184. ((eq (car desc) :tag)
  10185. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10186. ((eq (car desc) :todo)
  10187. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10188. ((eq (car desc) :regexp)
  10189. (setq descre (cdr desc)))
  10190. ((eq (car desc) :level)
  10191. (setq descre (concat "^\\*\\{" (number-to-string
  10192. (if org-odd-levels-only
  10193. (1- (* 2 (cdr desc)))
  10194. (cdr desc)))
  10195. "\\}[ \t]")))
  10196. ((eq (car desc) :maxlevel)
  10197. (setq fast-path-p t)
  10198. (setq descre (concat "^\\*\\{1," (number-to-string
  10199. (if org-odd-levels-only
  10200. (1- (* 2 (cdr desc)))
  10201. (cdr desc)))
  10202. "\\}[ \t]")))
  10203. (t (error "Bad refiling target description %s" desc)))
  10204. (while (setq f (pop files))
  10205. (with-current-buffer
  10206. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10207. (or
  10208. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10209. (progn
  10210. (if (bufferp f) (setq f (buffer-file-name
  10211. (buffer-base-buffer f))))
  10212. (setq f (and f (expand-file-name f)))
  10213. (if (eq org-refile-use-outline-path 'file)
  10214. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10215. (save-excursion
  10216. (save-restriction
  10217. (widen)
  10218. (goto-char (point-min))
  10219. (while (re-search-forward descre nil t)
  10220. (goto-char (setq pos0 (point-at-bol)))
  10221. (catch 'next
  10222. (when org-refile-target-verify-function
  10223. (save-match-data
  10224. (or (funcall org-refile-target-verify-function)
  10225. (throw 'next t))))
  10226. (when (and (looking-at org-complex-heading-regexp)
  10227. (not (member (match-string 4) excluded-entries))
  10228. (match-string 4))
  10229. (setq level (org-reduced-level
  10230. (- (match-end 1) (match-beginning 1)))
  10231. txt (org-link-display-format (match-string 4))
  10232. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10233. re (format org-complex-heading-regexp-format
  10234. (regexp-quote (match-string 4))))
  10235. (when org-refile-use-outline-path
  10236. (setq txt (mapconcat
  10237. 'org-protect-slash
  10238. (append
  10239. (if (eq org-refile-use-outline-path
  10240. 'file)
  10241. (list (file-name-nondirectory
  10242. (buffer-file-name
  10243. (buffer-base-buffer))))
  10244. (if (eq org-refile-use-outline-path
  10245. 'full-file-path)
  10246. (list (buffer-file-name
  10247. (buffer-base-buffer)))))
  10248. (org-get-outline-path fast-path-p
  10249. level txt)
  10250. (list txt))
  10251. "/")))
  10252. (push (list txt f re (org-refile-marker (point)))
  10253. tgs)))
  10254. (when (= (point) pos0)
  10255. ;; verification function has not moved point
  10256. (goto-char (point-at-eol))))))))
  10257. (when org-refile-use-cache
  10258. (org-refile-cache-put tgs (buffer-file-name) descre))
  10259. (setq targets (append tgs targets))))))
  10260. (message "Getting targets...done")
  10261. (nreverse targets)))
  10262. (defun org-protect-slash (s)
  10263. (while (string-match "/" s)
  10264. (setq s (replace-match "\\" t t s)))
  10265. s)
  10266. (defvar org-olpa (make-vector 20 nil))
  10267. (defun org-get-outline-path (&optional fastp level heading)
  10268. "Return the outline path to the current entry, as a list.
  10269. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10270. routine which makes outline path derivations for an entire file,
  10271. avoiding backtracing. Refile target collection makes use of that."
  10272. (if fastp
  10273. (progn
  10274. (if (> level 19)
  10275. (error "Outline path failure, more than 19 levels"))
  10276. (loop for i from level upto 19 do
  10277. (aset org-olpa i nil))
  10278. (prog1
  10279. (delq nil (append org-olpa nil))
  10280. (aset org-olpa level heading)))
  10281. (let (rtn case-fold-search)
  10282. (save-excursion
  10283. (save-restriction
  10284. (widen)
  10285. (while (org-up-heading-safe)
  10286. (when (looking-at org-complex-heading-regexp)
  10287. (push (org-trim
  10288. (replace-regexp-in-string
  10289. ;; Remove statistical/checkboxes cookies
  10290. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10291. (org-match-string-no-properties 4)))
  10292. rtn)))
  10293. rtn)))))
  10294. (defun org-format-outline-path (path &optional width prefix separator)
  10295. "Format the outline path PATH for display.
  10296. WIDTH is the maximum number of characters that is available.
  10297. PREFIX is a prefix to be included in the returned string,
  10298. such as the file name.
  10299. SEPARATOR is inserted between the different parts of the path,
  10300. the default is \"/\"."
  10301. (setq width (or width 79))
  10302. (if prefix (setq width (- width (length prefix))))
  10303. (if (not path)
  10304. (or prefix "")
  10305. (let* ((nsteps (length path))
  10306. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10307. (maxwidth (if (<= total-width width)
  10308. 10000 ;; everything fits
  10309. ;; we need to shorten the level headings
  10310. (/ (- width nsteps) nsteps)))
  10311. (org-odd-levels-only nil)
  10312. (n 0)
  10313. (total (1+ (length prefix))))
  10314. (setq maxwidth (max maxwidth 10))
  10315. (concat prefix
  10316. (if prefix (or separator "/"))
  10317. (mapconcat
  10318. (lambda (h)
  10319. (setq n (1+ n))
  10320. (if (and (= n nsteps) (< maxwidth 10000))
  10321. (setq maxwidth (- total-width total)))
  10322. (if (< (length h) maxwidth)
  10323. (progn (setq total (+ total (length h) 1)) h)
  10324. (setq h (substring h 0 (- maxwidth 2))
  10325. total (+ total maxwidth 1))
  10326. (if (string-match "[ \t]+\\'" h)
  10327. (setq h (substring h 0 (match-beginning 0))))
  10328. (setq h (concat h "..")))
  10329. (org-add-props h nil 'face
  10330. (nth (% (1- n) org-n-level-faces)
  10331. org-level-faces))
  10332. h)
  10333. path (or separator "/"))))))
  10334. (defun org-display-outline-path (&optional file current separator just-return-string)
  10335. "Display the current outline path in the echo area.
  10336. If FILE is non-nil, prepend the output with the file name.
  10337. If CURRENT is non-nil, append the current heading to the output.
  10338. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10339. the different parts of the path and defaults to \"/\".
  10340. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10341. (interactive "P")
  10342. (let* (case-fold-search
  10343. (bfn (buffer-file-name (buffer-base-buffer)))
  10344. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10345. res)
  10346. (if current (setq path (append path
  10347. (save-excursion
  10348. (org-back-to-heading t)
  10349. (if (looking-at org-complex-heading-regexp)
  10350. (list (match-string 4)))))))
  10351. (setq res
  10352. (org-format-outline-path
  10353. path
  10354. (1- (frame-width))
  10355. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10356. separator))
  10357. (if just-return-string
  10358. (org-no-properties res)
  10359. (org-unlogged-message "%s" res))))
  10360. (defvar org-refile-history nil
  10361. "History for refiling operations.")
  10362. (defvar org-after-refile-insert-hook nil
  10363. "Hook run after `org-refile' has inserted its stuff at the new location.
  10364. Note that this is still *before* the stuff will be removed from
  10365. the *old* location.")
  10366. (defvar org-capture-last-stored-marker)
  10367. (defvar org-refile-keep nil
  10368. "Non-nil means `org-refile' will copy instead of refile.")
  10369. (defun org-copy ()
  10370. "Like `org-refile', but copy."
  10371. (interactive)
  10372. (let ((org-refile-keep t))
  10373. (funcall 'org-refile nil nil nil "Copy")))
  10374. (defun org-refile (&optional goto default-buffer rfloc msg)
  10375. "Move the entry or entries at point to another heading.
  10376. The list of target headings is compiled using the information in
  10377. `org-refile-targets', which see.
  10378. At the target location, the entry is filed as a subitem of the target
  10379. heading. Depending on `org-reverse-note-order', the new subitem will
  10380. either be the first or the last subitem.
  10381. If there is an active region, all entries in that region will be moved.
  10382. However, the region must fulfill the requirement that the first heading
  10383. is the first one sets the top-level of the moved text - at most siblings
  10384. below it are allowed.
  10385. With prefix arg GOTO, the command will only visit the target location
  10386. and not actually move anything.
  10387. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10388. go to the location where the last refiling operation has put the subtree.
  10389. With a numeric prefix argument of `2', refile to the running clock.
  10390. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10391. being set to `t' and copy to the target location, don't move it.
  10392. Beware that keeping refiled entries may result in duplicated ID
  10393. properties.
  10394. RFLOC can be a refile location obtained in a different way.
  10395. MSG is a string to replace \"Refile\" in the default prompt with
  10396. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10397. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10398. If you are using target caching (see `org-refile-use-cache'),
  10399. you have to clear the target cache in order to find new targets.
  10400. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10401. prefix argument (`C-u C-u C-u C-c C-w')."
  10402. (interactive "P")
  10403. (if (member goto '(0 (64)))
  10404. (org-refile-cache-clear)
  10405. (let* ((actionmsg (or msg "Refile"))
  10406. (cbuf (current-buffer))
  10407. (regionp (org-region-active-p))
  10408. (region-start (and regionp (region-beginning)))
  10409. (region-end (and regionp (region-end)))
  10410. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10411. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10412. pos it nbuf file re level reversed)
  10413. (setq last-command nil)
  10414. (when regionp
  10415. (goto-char region-start)
  10416. (or (bolp) (goto-char (point-at-bol)))
  10417. (setq region-start (point))
  10418. (unless (or (org-kill-is-subtree-p
  10419. (buffer-substring region-start region-end))
  10420. (prog1 org-refile-active-region-within-subtree
  10421. (let ((s (point-at-eol)))
  10422. (org-toggle-heading)
  10423. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10424. (user-error "The region is not a (sequence of) subtree(s)")))
  10425. (if (equal goto '(16))
  10426. (org-refile-goto-last-stored)
  10427. (when (or
  10428. (and (equal goto 2)
  10429. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10430. (prog1
  10431. (setq it (list (or org-clock-heading "running clock")
  10432. (buffer-file-name
  10433. (marker-buffer org-clock-hd-marker))
  10434. ""
  10435. (marker-position org-clock-hd-marker)))
  10436. (setq goto nil)))
  10437. (setq it (or rfloc
  10438. (let (heading-text)
  10439. (save-excursion
  10440. (unless goto
  10441. (org-back-to-heading t)
  10442. (setq heading-text
  10443. (nth 4 (org-heading-components))))
  10444. (org-refile-get-location
  10445. (cond (goto "Goto")
  10446. (regionp (concat actionmsg " region to"))
  10447. (t (concat actionmsg " subtree \""
  10448. heading-text "\" to")))
  10449. default-buffer
  10450. (and (not (equal '(4) goto))
  10451. org-refile-allow-creating-parent-nodes)
  10452. goto))))))
  10453. (setq file (nth 1 it)
  10454. re (nth 2 it)
  10455. pos (nth 3 it))
  10456. (if (and (not goto)
  10457. pos
  10458. (equal (buffer-file-name) file)
  10459. (if regionp
  10460. (and (>= pos region-start)
  10461. (<= pos region-end))
  10462. (and (>= pos (point))
  10463. (< pos (save-excursion
  10464. (org-end-of-subtree t t))))))
  10465. (error "Cannot refile to position inside the tree or region"))
  10466. (setq nbuf (or (find-buffer-visiting file)
  10467. (find-file-noselect file)))
  10468. (if (and goto (not (equal goto 3)))
  10469. (progn
  10470. (org-pop-to-buffer-same-window nbuf)
  10471. (goto-char pos)
  10472. (org-show-context 'org-goto))
  10473. (if regionp
  10474. (progn
  10475. (org-kill-new (buffer-substring region-start region-end))
  10476. (org-save-markers-in-region region-start region-end))
  10477. (org-copy-subtree 1 nil t))
  10478. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10479. (find-file-noselect file)))
  10480. (setq reversed (org-notes-order-reversed-p))
  10481. (save-excursion
  10482. (save-restriction
  10483. (widen)
  10484. (if pos
  10485. (progn
  10486. (goto-char pos)
  10487. (looking-at org-outline-regexp)
  10488. (setq level (org-get-valid-level (funcall outline-level) 1))
  10489. (goto-char
  10490. (if reversed
  10491. (or (outline-next-heading) (point-max))
  10492. (or (save-excursion (org-get-next-sibling))
  10493. (org-end-of-subtree t t)
  10494. (point-max)))))
  10495. (setq level 1)
  10496. (if (not reversed)
  10497. (goto-char (point-max))
  10498. (goto-char (point-min))
  10499. (or (outline-next-heading) (goto-char (point-max)))))
  10500. (if (not (bolp)) (newline))
  10501. (org-paste-subtree level)
  10502. (when org-log-refile
  10503. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10504. (unless (eq org-log-refile 'note)
  10505. (save-excursion (org-add-log-note))))
  10506. (and org-auto-align-tags
  10507. (let ((org-loop-over-headlines-in-active-region nil))
  10508. (org-set-tags nil t)))
  10509. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10510. :last-refile)))
  10511. (when bookmark-name
  10512. (with-demoted-errors
  10513. (bookmark-set bookmark-name))))
  10514. ;; If we are refiling for capture, make sure that the
  10515. ;; last-capture pointers point here
  10516. (when (org-bound-and-true-p org-refile-for-capture)
  10517. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10518. :last-capture-marker)))
  10519. (when bookmark-name
  10520. (with-demoted-errors
  10521. (bookmark-set bookmark-name))))
  10522. (move-marker org-capture-last-stored-marker (point)))
  10523. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10524. (run-hooks 'org-after-refile-insert-hook))))
  10525. (unless org-refile-keep
  10526. (if regionp
  10527. (delete-region (point) (+ (point) (- region-end region-start)))
  10528. (delete-region
  10529. (and (org-back-to-heading t) (point))
  10530. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10531. (when (featurep 'org-inlinetask)
  10532. (org-inlinetask-remove-END-maybe))
  10533. (setq org-markers-to-move nil)
  10534. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10535. (defun org-refile-goto-last-stored ()
  10536. "Go to the location where the last refile was stored."
  10537. (interactive)
  10538. (bookmark-jump "org-refile-last-stored")
  10539. (message "This is the location of the last refile"))
  10540. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10541. no-exclude)
  10542. "Prompt the user for a refile location, using PROMPT.
  10543. PROMPT should not be suffixed with a colon and a space, because
  10544. this function appends the default value from
  10545. `org-refile-history' automatically, if that is not empty.
  10546. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10547. this is used for the GOTO interface."
  10548. (let ((org-refile-targets org-refile-targets)
  10549. (org-refile-use-outline-path org-refile-use-outline-path)
  10550. excluded-entries)
  10551. (when (and (derived-mode-p 'org-mode)
  10552. (not org-refile-use-cache)
  10553. (not no-exclude))
  10554. (org-map-tree
  10555. (lambda()
  10556. (setq excluded-entries
  10557. (append excluded-entries (list (org-get-heading t t)))))))
  10558. (setq org-refile-target-table
  10559. (org-refile-get-targets default-buffer excluded-entries)))
  10560. (unless org-refile-target-table
  10561. (user-error "No refile targets"))
  10562. (let* ((cbuf (current-buffer))
  10563. (partial-completion-mode nil)
  10564. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10565. (cfunc (if (and org-refile-use-outline-path
  10566. org-outline-path-complete-in-steps)
  10567. 'org-olpath-completing-read
  10568. 'org-icompleting-read))
  10569. (extra (if org-refile-use-outline-path "/" ""))
  10570. (cbnex (concat (buffer-name) extra))
  10571. (filename (and cfn (expand-file-name cfn)))
  10572. (tbl (mapcar
  10573. (lambda (x)
  10574. (if (and (not (member org-refile-use-outline-path
  10575. '(file full-file-path)))
  10576. (not (equal filename (nth 1 x))))
  10577. (cons (concat (car x) extra " ("
  10578. (file-name-nondirectory (nth 1 x)) ")")
  10579. (cdr x))
  10580. (cons (concat (car x) extra) (cdr x))))
  10581. org-refile-target-table))
  10582. (completion-ignore-case t)
  10583. cdef
  10584. (prompt (concat prompt
  10585. (or (and (car org-refile-history)
  10586. (concat " (default " (car org-refile-history) ")"))
  10587. (and (assoc cbnex tbl) (setq cdef cbnex)
  10588. (concat " (default " cbnex ")"))) ": "))
  10589. pa answ parent-target child parent old-hist)
  10590. (setq old-hist org-refile-history)
  10591. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10592. nil 'org-refile-history (or cdef (car org-refile-history))))
  10593. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10594. (org-refile-check-position pa)
  10595. (if pa
  10596. (progn
  10597. (when (or (not org-refile-history)
  10598. (not (eq old-hist org-refile-history))
  10599. (not (equal (car pa) (car org-refile-history))))
  10600. (setq org-refile-history
  10601. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10602. org-refile-history
  10603. (cdr org-refile-history))))
  10604. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10605. (pop org-refile-history)))
  10606. pa)
  10607. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10608. (progn
  10609. (setq parent (match-string 1 answ)
  10610. child (match-string 2 answ))
  10611. (setq parent-target (or (assoc parent tbl)
  10612. (assoc (concat parent "/") tbl)))
  10613. (when (and parent-target
  10614. (or (eq new-nodes t)
  10615. (and (eq new-nodes 'confirm)
  10616. (y-or-n-p (format "Create new node \"%s\"? "
  10617. child)))))
  10618. (org-refile-new-child parent-target child)))
  10619. (user-error "Invalid target location")))))
  10620. (declare-function org-string-nw-p "org-macs" (s))
  10621. (defun org-refile-check-position (refile-pointer)
  10622. "Check if the refile pointer matches the headline to which it points."
  10623. (let* ((file (nth 1 refile-pointer))
  10624. (re (nth 2 refile-pointer))
  10625. (pos (nth 3 refile-pointer))
  10626. buffer)
  10627. (if (and (not (markerp pos)) (not file))
  10628. (user-error "Please save the buffer to a file before refiling")
  10629. (when (org-string-nw-p re)
  10630. (setq buffer (if (markerp pos)
  10631. (marker-buffer pos)
  10632. (or (find-buffer-visiting file)
  10633. (find-file-noselect file))))
  10634. (with-current-buffer buffer
  10635. (save-excursion
  10636. (save-restriction
  10637. (widen)
  10638. (goto-char pos)
  10639. (beginning-of-line 1)
  10640. (unless (org-looking-at-p re)
  10641. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10642. (defun org-refile-new-child (parent-target child)
  10643. "Use refile target PARENT-TARGET to add new CHILD below it."
  10644. (unless parent-target
  10645. (error "Cannot find parent for new node"))
  10646. (let ((file (nth 1 parent-target))
  10647. (pos (nth 3 parent-target))
  10648. level)
  10649. (with-current-buffer (or (find-buffer-visiting file)
  10650. (find-file-noselect file))
  10651. (save-excursion
  10652. (save-restriction
  10653. (widen)
  10654. (if pos
  10655. (goto-char pos)
  10656. (goto-char (point-max))
  10657. (if (not (bolp)) (newline)))
  10658. (when (looking-at org-outline-regexp)
  10659. (setq level (funcall outline-level))
  10660. (org-end-of-subtree t t))
  10661. (org-back-over-empty-lines)
  10662. (insert "\n" (make-string
  10663. (if pos (org-get-valid-level level 1) 1) ?*)
  10664. " " child "\n")
  10665. (beginning-of-line 0)
  10666. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10667. (defun org-olpath-completing-read (prompt collection &rest args)
  10668. "Read an outline path like a file name."
  10669. (let ((thetable collection)
  10670. (org-completion-use-ido nil) ; does not work with ido.
  10671. (org-completion-use-iswitchb nil)) ; or iswitchb
  10672. (apply
  10673. 'org-icompleting-read prompt
  10674. (lambda (string predicate &optional flag)
  10675. (let (rtn r f (l (length string)))
  10676. (cond
  10677. ((eq flag nil)
  10678. ;; try completion
  10679. (try-completion string thetable))
  10680. ((eq flag t)
  10681. ;; all-completions
  10682. (setq rtn (all-completions string thetable predicate))
  10683. (mapcar
  10684. (lambda (x)
  10685. (setq r (substring x l))
  10686. (if (string-match " ([^)]*)$" x)
  10687. (setq f (match-string 0 x))
  10688. (setq f ""))
  10689. (if (string-match "/" r)
  10690. (concat string (substring r 0 (match-end 0)) f)
  10691. x))
  10692. rtn))
  10693. ((eq flag 'lambda)
  10694. ;; exact match?
  10695. (assoc string thetable)))))
  10696. args)))
  10697. ;;;; Dynamic blocks
  10698. (defun org-find-dblock (name)
  10699. "Find the first dynamic block with name NAME in the buffer.
  10700. If not found, stay at current position and return nil."
  10701. (let ((case-fold-search t) pos)
  10702. (save-excursion
  10703. (goto-char (point-min))
  10704. (setq pos (and (re-search-forward
  10705. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10706. (match-beginning 0))))
  10707. (if pos (goto-char pos))
  10708. pos))
  10709. (defconst org-dblock-start-re
  10710. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10711. "Matches the start line of a dynamic block, with parameters.")
  10712. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10713. "Matches the end of a dynamic block.")
  10714. (defun org-create-dblock (plist)
  10715. "Create a dynamic block section, with parameters taken from PLIST.
  10716. PLIST must contain a :name entry which is used as name of the block."
  10717. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10718. (end-of-line 1)
  10719. (newline))
  10720. (let ((col (current-column))
  10721. (name (plist-get plist :name)))
  10722. (insert "#+BEGIN: " name)
  10723. (while plist
  10724. (if (eq (car plist) :name)
  10725. (setq plist (cddr plist))
  10726. (insert " " (prin1-to-string (pop plist)))))
  10727. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10728. (beginning-of-line -2)))
  10729. (defun org-prepare-dblock ()
  10730. "Prepare dynamic block for refresh.
  10731. This empties the block, puts the cursor at the insert position and returns
  10732. the property list including an extra property :name with the block name."
  10733. (unless (looking-at org-dblock-start-re)
  10734. (user-error "Not at a dynamic block"))
  10735. (let* ((begdel (1+ (match-end 0)))
  10736. (name (org-no-properties (match-string 1)))
  10737. (params (append (list :name name)
  10738. (read (concat "(" (match-string 3) ")")))))
  10739. (save-excursion
  10740. (beginning-of-line 1)
  10741. (skip-chars-forward " \t")
  10742. (setq params (plist-put params :indentation-column (current-column))))
  10743. (unless (re-search-forward org-dblock-end-re nil t)
  10744. (error "Dynamic block not terminated"))
  10745. (setq params
  10746. (append params
  10747. (list :content (buffer-substring
  10748. begdel (match-beginning 0)))))
  10749. (delete-region begdel (match-beginning 0))
  10750. (goto-char begdel)
  10751. (open-line 1)
  10752. params))
  10753. (defun org-map-dblocks (&optional command)
  10754. "Apply COMMAND to all dynamic blocks in the current buffer.
  10755. If COMMAND is not given, use `org-update-dblock'."
  10756. (let ((cmd (or command 'org-update-dblock)))
  10757. (save-excursion
  10758. (goto-char (point-min))
  10759. (while (re-search-forward org-dblock-start-re nil t)
  10760. (goto-char (match-beginning 0))
  10761. (save-excursion
  10762. (condition-case nil
  10763. (funcall cmd)
  10764. (error (message "Error during update of dynamic block"))))
  10765. (unless (re-search-forward org-dblock-end-re nil t)
  10766. (error "Dynamic block not terminated"))))))
  10767. (defun org-dblock-update (&optional arg)
  10768. "User command for updating dynamic blocks.
  10769. Update the dynamic block at point. With prefix ARG, update all dynamic
  10770. blocks in the buffer."
  10771. (interactive "P")
  10772. (if arg
  10773. (org-update-all-dblocks)
  10774. (or (looking-at org-dblock-start-re)
  10775. (org-beginning-of-dblock))
  10776. (org-update-dblock)))
  10777. (defun org-update-dblock ()
  10778. "Update the dynamic block at point.
  10779. This means to empty the block, parse for parameters and then call
  10780. the correct writing function."
  10781. (interactive)
  10782. (save-window-excursion
  10783. (let* ((pos (point))
  10784. (line (org-current-line))
  10785. (params (org-prepare-dblock))
  10786. (name (plist-get params :name))
  10787. (indent (plist-get params :indentation-column))
  10788. (cmd (intern (concat "org-dblock-write:" name))))
  10789. (message "Updating dynamic block `%s' at line %d..." name line)
  10790. (funcall cmd params)
  10791. (message "Updating dynamic block `%s' at line %d...done" name line)
  10792. (goto-char pos)
  10793. (when (and indent (> indent 0))
  10794. (setq indent (make-string indent ?\ ))
  10795. (save-excursion
  10796. (org-beginning-of-dblock)
  10797. (forward-line 1)
  10798. (while (not (looking-at org-dblock-end-re))
  10799. (insert indent)
  10800. (beginning-of-line 2))
  10801. (when (looking-at org-dblock-end-re)
  10802. (and (looking-at "[ \t]+")
  10803. (replace-match ""))
  10804. (insert indent)))))))
  10805. (defun org-beginning-of-dblock ()
  10806. "Find the beginning of the dynamic block at point.
  10807. Error if there is no such block at point."
  10808. (let ((pos (point))
  10809. beg)
  10810. (end-of-line 1)
  10811. (if (and (re-search-backward org-dblock-start-re nil t)
  10812. (setq beg (match-beginning 0))
  10813. (re-search-forward org-dblock-end-re nil t)
  10814. (> (match-end 0) pos))
  10815. (goto-char beg)
  10816. (goto-char pos)
  10817. (error "Not in a dynamic block"))))
  10818. (defun org-update-all-dblocks ()
  10819. "Update all dynamic blocks in the buffer.
  10820. This function can be used in a hook."
  10821. (interactive)
  10822. (when (derived-mode-p 'org-mode)
  10823. (org-map-dblocks 'org-update-dblock)))
  10824. ;;;; Completion
  10825. (declare-function org-export-backend-name "org-export" (cl-x))
  10826. (declare-function org-export-backend-options "org-export" (cl-x))
  10827. (defun org-get-export-keywords ()
  10828. "Return a list of all currently understood export keywords.
  10829. Export keywords include options, block names, attributes and
  10830. keywords relative to each registered export back-end."
  10831. (let (keywords)
  10832. (dolist (backend
  10833. (org-bound-and-true-p org-export--registered-backends)
  10834. (delq nil keywords))
  10835. ;; Back-end name (for keywords, like #+LATEX:)
  10836. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10837. (dolist (option-entry (org-export-backend-options backend))
  10838. ;; Back-end options.
  10839. (push (nth 1 option-entry) keywords)))))
  10840. (defconst org-options-keywords
  10841. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10842. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10843. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10844. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10845. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10846. (defcustom org-structure-template-alist
  10847. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10848. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10849. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10850. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10851. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10852. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10853. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10854. "<literal style=\"latex\">\n?\n</literal>")
  10855. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10856. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10857. "<literal style=\"html\">\n?\n</literal>")
  10858. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10859. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10860. ("A" "#+ASCII: " "")
  10861. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10862. ("I" "#+INCLUDE: %file ?"
  10863. "<include file=%file markup=\"?\">"))
  10864. "Structure completion elements.
  10865. This is a list of abbreviation keys and values. The value gets inserted
  10866. if you type `<' followed by the key and then press the completion key,
  10867. usually `M-TAB'. %file will be replaced by a file name after prompting
  10868. for the file using completion. The cursor will be placed at the position
  10869. of the `?` in the template.
  10870. There are two templates for each key, the first uses the original Org syntax,
  10871. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10872. the default when the /org-mtags.el/ module has been loaded. See also the
  10873. variable `org-mtags-prefer-muse-templates'."
  10874. :group 'org-completion
  10875. :type '(repeat
  10876. (list
  10877. (string :tag "Key")
  10878. (string :tag "Template")
  10879. (string :tag "Muse Template"))))
  10880. (defun org-try-structure-completion ()
  10881. "Try to complete a structure template before point.
  10882. This looks for strings like \"<e\" on an otherwise empty line and
  10883. expands them."
  10884. (let ((l (buffer-substring (point-at-bol) (point)))
  10885. a)
  10886. (when (and (looking-at "[ \t]*$")
  10887. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10888. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10889. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10890. (match-beginning 1)) a)
  10891. t)))
  10892. (defun org-complete-expand-structure-template (start cell)
  10893. "Expand a structure template."
  10894. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10895. (rpl (nth (if musep 2 1) cell))
  10896. (ind ""))
  10897. (delete-region start (point))
  10898. (when (string-match "\\`#\\+" rpl)
  10899. (cond
  10900. ((bolp))
  10901. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10902. (setq ind (buffer-substring (point-at-bol) (point))))
  10903. (t (newline))))
  10904. (setq start (point))
  10905. (if (string-match "%file" rpl)
  10906. (setq rpl (replace-match
  10907. (concat
  10908. "\""
  10909. (save-match-data
  10910. (abbreviate-file-name (read-file-name "Include file: ")))
  10911. "\"")
  10912. t t rpl)))
  10913. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10914. (concat "\n" ind)))
  10915. (insert rpl)
  10916. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10917. ;;;; TODO, DEADLINE, Comments
  10918. (defun org-toggle-comment ()
  10919. "Change the COMMENT state of an entry."
  10920. (interactive)
  10921. (save-excursion
  10922. (org-back-to-heading)
  10923. (let (case-fold-search)
  10924. (cond
  10925. ((looking-at (format org-heading-keyword-regexp-format
  10926. org-comment-string))
  10927. (goto-char (match-end 1))
  10928. (looking-at (concat " +" org-comment-string))
  10929. (replace-match "" t t)
  10930. (when (eolp) (insert " ")))
  10931. ((looking-at org-outline-regexp)
  10932. (goto-char (match-end 0))
  10933. (insert org-comment-string " "))))))
  10934. (defvar org-last-todo-state-is-todo nil
  10935. "This is non-nil when the last TODO state change led to a TODO state.
  10936. If the last change removed the TODO tag or switched to DONE, then
  10937. this is nil.")
  10938. (defvar org-setting-tags nil) ; dynamically skipped
  10939. (defvar org-todo-setup-filter-hook nil
  10940. "Hook for functions that pre-filter todo specs.
  10941. Each function takes a todo spec and returns either nil or the spec
  10942. transformed into canonical form." )
  10943. (defvar org-todo-get-default-hook nil
  10944. "Hook for functions that get a default item for todo.
  10945. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10946. nil or a string to be used for the todo mark." )
  10947. (defvar org-agenda-headline-snapshot-before-repeat)
  10948. (defun org-current-effective-time ()
  10949. "Return current time adjusted for `org-extend-today-until' variable."
  10950. (let* ((ct (org-current-time))
  10951. (dct (decode-time ct))
  10952. (ct1
  10953. (cond
  10954. (org-use-last-clock-out-time-as-effective-time
  10955. (or (org-clock-get-last-clock-out-time) ct))
  10956. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10957. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10958. (t ct))))
  10959. ct1))
  10960. (defun org-todo-yesterday (&optional arg)
  10961. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10962. (interactive "P")
  10963. (if (eq major-mode 'org-agenda-mode)
  10964. (apply 'org-agenda-todo-yesterday arg)
  10965. (let* ((hour (third (decode-time
  10966. (org-current-time))))
  10967. (org-extend-today-until (1+ hour)))
  10968. (org-todo arg))))
  10969. (defvar org-block-entry-blocking ""
  10970. "First entry preventing the TODO state change.")
  10971. (defun org-todo (&optional arg)
  10972. "Change the TODO state of an item.
  10973. The state of an item is given by a keyword at the start of the heading,
  10974. like
  10975. *** TODO Write paper
  10976. *** DONE Call mom
  10977. The different keywords are specified in the variable `org-todo-keywords'.
  10978. By default the available states are \"TODO\" and \"DONE\".
  10979. So for this example: when the item starts with TODO, it is changed to DONE.
  10980. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10981. DONE are present, add TODO at the beginning of the heading.
  10982. With \\[universal-argument] prefix arg, use completion to determine the new \
  10983. state.
  10984. With numeric prefix arg, switch to that state.
  10985. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10986. keywords (nextset).
  10987. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10988. With a numeric prefix arg of 0, inhibit note taking for the change.
  10989. For calling through lisp, arg is also interpreted in the following way:
  10990. 'none -> empty state
  10991. \"\"(empty string) -> switch to empty state
  10992. 'done -> switch to DONE
  10993. 'nextset -> switch to the next set of keywords
  10994. 'previousset -> switch to the previous set of keywords
  10995. \"WAITING\" -> switch to the specified keyword, but only if it
  10996. really is a member of `org-todo-keywords'."
  10997. (interactive "P")
  10998. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10999. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11000. 'region-start-level 'region))
  11001. org-loop-over-headlines-in-active-region)
  11002. (org-map-entries
  11003. `(org-todo ,arg)
  11004. org-loop-over-headlines-in-active-region
  11005. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11006. (if (equal arg '(16)) (setq arg 'nextset))
  11007. (let ((org-blocker-hook org-blocker-hook)
  11008. commentp
  11009. case-fold-search)
  11010. (when (equal arg '(64))
  11011. (setq arg nil org-blocker-hook nil))
  11012. (when (and org-blocker-hook
  11013. (or org-inhibit-blocking
  11014. (org-entry-get nil "NOBLOCKING")))
  11015. (setq org-blocker-hook nil))
  11016. (save-excursion
  11017. (catch 'exit
  11018. (org-back-to-heading t)
  11019. (when (looking-at (concat "^\\*+ " org-comment-string))
  11020. (org-toggle-comment)
  11021. (setq commentp t))
  11022. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11023. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11024. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11025. (let* ((match-data (match-data))
  11026. (startpos (point-at-bol))
  11027. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11028. (org-log-done org-log-done)
  11029. (org-log-repeat org-log-repeat)
  11030. (org-todo-log-states org-todo-log-states)
  11031. (org-inhibit-logging
  11032. (if (equal arg 0)
  11033. (progn (setq arg nil) 'note) org-inhibit-logging))
  11034. (this (match-string 1))
  11035. (hl-pos (match-beginning 0))
  11036. (head (org-get-todo-sequence-head this))
  11037. (ass (assoc head org-todo-kwd-alist))
  11038. (interpret (nth 1 ass))
  11039. (done-word (nth 3 ass))
  11040. (final-done-word (nth 4 ass))
  11041. (org-last-state (or this ""))
  11042. (completion-ignore-case t)
  11043. (member (member this org-todo-keywords-1))
  11044. (tail (cdr member))
  11045. (org-state (cond
  11046. ((and org-todo-key-trigger
  11047. (or (and (equal arg '(4))
  11048. (eq org-use-fast-todo-selection 'prefix))
  11049. (and (not arg) org-use-fast-todo-selection
  11050. (not (eq org-use-fast-todo-selection
  11051. 'prefix)))))
  11052. ;; Use fast selection
  11053. (org-fast-todo-selection))
  11054. ((and (equal arg '(4))
  11055. (or (not org-use-fast-todo-selection)
  11056. (not org-todo-key-trigger)))
  11057. ;; Read a state with completion
  11058. (org-icompleting-read
  11059. "State: " (mapcar 'list org-todo-keywords-1)
  11060. nil t))
  11061. ((eq arg 'right)
  11062. (if this
  11063. (if tail (car tail) nil)
  11064. (car org-todo-keywords-1)))
  11065. ((eq arg 'left)
  11066. (if (equal member org-todo-keywords-1)
  11067. nil
  11068. (if this
  11069. (nth (- (length org-todo-keywords-1)
  11070. (length tail) 2)
  11071. org-todo-keywords-1)
  11072. (org-last org-todo-keywords-1))))
  11073. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11074. (setq arg nil))) ; hack to fall back to cycling
  11075. (arg
  11076. ;; user or caller requests a specific state
  11077. (cond
  11078. ((equal arg "") nil)
  11079. ((eq arg 'none) nil)
  11080. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11081. ((eq arg 'nextset)
  11082. (or (car (cdr (member head org-todo-heads)))
  11083. (car org-todo-heads)))
  11084. ((eq arg 'previousset)
  11085. (let ((org-todo-heads (reverse org-todo-heads)))
  11086. (or (car (cdr (member head org-todo-heads)))
  11087. (car org-todo-heads))))
  11088. ((car (member arg org-todo-keywords-1)))
  11089. ((stringp arg)
  11090. (user-error "State `%s' not valid in this file" arg))
  11091. ((nth (1- (prefix-numeric-value arg))
  11092. org-todo-keywords-1))))
  11093. ((null member) (or head (car org-todo-keywords-1)))
  11094. ((equal this final-done-word) nil) ;; -> make empty
  11095. ((null tail) nil) ;; -> first entry
  11096. ((memq interpret '(type priority))
  11097. (if (eq this-command last-command)
  11098. (car tail)
  11099. (if (> (length tail) 0)
  11100. (or done-word (car org-done-keywords))
  11101. nil)))
  11102. (t
  11103. (car tail))))
  11104. (org-state (or
  11105. (run-hook-with-args-until-success
  11106. 'org-todo-get-default-hook org-state org-last-state)
  11107. org-state))
  11108. (next (if org-state (concat " " org-state " ") " "))
  11109. (change-plist (list :type 'todo-state-change :from this :to org-state
  11110. :position startpos))
  11111. dolog now-done-p)
  11112. (when org-blocker-hook
  11113. (setq org-last-todo-state-is-todo
  11114. (not (member this org-done-keywords)))
  11115. (unless (save-excursion
  11116. (save-match-data
  11117. (org-with-wide-buffer
  11118. (run-hook-with-args-until-failure
  11119. 'org-blocker-hook change-plist))))
  11120. (if (org-called-interactively-p 'interactive)
  11121. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11122. this org-state org-block-entry-blocking)
  11123. ;; fail silently
  11124. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11125. this org-state org-block-entry-blocking)
  11126. (throw 'exit nil))))
  11127. (store-match-data match-data)
  11128. (replace-match next t t)
  11129. (unless (pos-visible-in-window-p hl-pos)
  11130. (message "TODO state changed to %s" (org-trim next)))
  11131. (unless head
  11132. (setq head (org-get-todo-sequence-head org-state)
  11133. ass (assoc head org-todo-kwd-alist)
  11134. interpret (nth 1 ass)
  11135. done-word (nth 3 ass)
  11136. final-done-word (nth 4 ass)))
  11137. (when (memq arg '(nextset previousset))
  11138. (message "Keyword-Set %d/%d: %s"
  11139. (- (length org-todo-sets) -1
  11140. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11141. (length org-todo-sets)
  11142. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11143. (setq org-last-todo-state-is-todo
  11144. (not (member org-state org-done-keywords)))
  11145. (setq now-done-p (and (member org-state org-done-keywords)
  11146. (not (member this org-done-keywords))))
  11147. (and logging (org-local-logging logging))
  11148. (when (and (or org-todo-log-states org-log-done)
  11149. (not (eq org-inhibit-logging t))
  11150. (not (memq arg '(nextset previousset))))
  11151. ;; we need to look at recording a time and note
  11152. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11153. (nth 2 (assoc this org-todo-log-states))))
  11154. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11155. (setq dolog 'time))
  11156. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11157. (and org-state
  11158. (member org-state org-not-done-keywords)
  11159. (not (member this org-not-done-keywords))))
  11160. ;; This is now a todo state and was not one before
  11161. ;; If there was a CLOSED time stamp, get rid of it.
  11162. (org-add-planning-info nil nil 'closed))
  11163. (when (and now-done-p org-log-done)
  11164. ;; It is now done, and it was not done before
  11165. (org-add-planning-info 'closed (org-current-effective-time))
  11166. (if (and (not dolog) (eq 'note org-log-done))
  11167. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11168. (when (and org-state dolog)
  11169. ;; This is a non-nil state, and we need to log it
  11170. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11171. ;; Fixup tag positioning
  11172. (org-todo-trigger-tag-changes org-state)
  11173. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11174. (when org-provide-todo-statistics
  11175. (org-update-parent-todo-statistics))
  11176. (run-hooks 'org-after-todo-state-change-hook)
  11177. (if (and arg (not (member org-state org-done-keywords)))
  11178. (setq head (org-get-todo-sequence-head org-state)))
  11179. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11180. ;; Do we need to trigger a repeat?
  11181. (when now-done-p
  11182. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11183. ;; This is for the agenda, take a snapshot of the headline.
  11184. (save-match-data
  11185. (setq org-agenda-headline-snapshot-before-repeat
  11186. (org-get-heading))))
  11187. (org-auto-repeat-maybe org-state))
  11188. ;; Fixup cursor location if close to the keyword
  11189. (if (and (outline-on-heading-p)
  11190. (not (bolp))
  11191. (save-excursion (beginning-of-line 1)
  11192. (looking-at org-todo-line-regexp))
  11193. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11194. (progn
  11195. (goto-char (or (match-end 2) (match-end 1)))
  11196. (and (looking-at " ") (just-one-space))))
  11197. (when org-trigger-hook
  11198. (save-excursion
  11199. (run-hook-with-args 'org-trigger-hook change-plist)))
  11200. (when commentp (org-toggle-comment))))))))
  11201. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11202. "Block turning an entry into a TODO, using the hierarchy.
  11203. This checks whether the current task should be blocked from state
  11204. changes. Such blocking occurs when:
  11205. 1. The task has children which are not all in a completed state.
  11206. 2. A task has a parent with the property :ORDERED:, and there
  11207. are siblings prior to the current task with incomplete
  11208. status.
  11209. 3. The parent of the task is blocked because it has siblings that should
  11210. be done first, or is child of a block grandparent TODO entry."
  11211. (if (not org-enforce-todo-dependencies)
  11212. t ; if locally turned off don't block
  11213. (catch 'dont-block
  11214. ;; If this is not a todo state change, or if this entry is already DONE,
  11215. ;; do not block
  11216. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11217. (member (plist-get change-plist :from)
  11218. (cons 'done org-done-keywords))
  11219. (member (plist-get change-plist :to)
  11220. (cons 'todo org-not-done-keywords))
  11221. (not (plist-get change-plist :to)))
  11222. (throw 'dont-block t))
  11223. ;; If this task has children, and any are undone, it's blocked
  11224. (save-excursion
  11225. (org-back-to-heading t)
  11226. (let ((this-level (funcall outline-level)))
  11227. (outline-next-heading)
  11228. (let ((child-level (funcall outline-level)))
  11229. (while (and (not (eobp))
  11230. (> child-level this-level))
  11231. ;; this todo has children, check whether they are all
  11232. ;; completed
  11233. (if (and (not (org-entry-is-done-p))
  11234. (org-entry-is-todo-p))
  11235. (progn (setq org-block-entry-blocking (org-get-heading))
  11236. (throw 'dont-block nil)))
  11237. (outline-next-heading)
  11238. (setq child-level (funcall outline-level))))))
  11239. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11240. ;; any previous siblings are undone, it's blocked
  11241. (save-excursion
  11242. (org-back-to-heading t)
  11243. (let* ((pos (point))
  11244. (parent-pos (and (org-up-heading-safe) (point))))
  11245. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11246. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11247. (forward-line 1)
  11248. (re-search-forward org-not-done-heading-regexp pos t))
  11249. (setq org-block-entry-blocking (match-string 0))
  11250. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11251. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11252. (while t
  11253. (goto-char parent-pos)
  11254. (if (not (looking-at org-not-done-heading-regexp))
  11255. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11256. (setq pos (point))
  11257. (setq parent-pos (and (org-up-heading-safe) (point)))
  11258. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11259. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11260. (forward-line 1)
  11261. (re-search-forward org-not-done-heading-regexp pos t)
  11262. (setq org-block-entry-blocking (org-get-heading)))
  11263. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11264. (defcustom org-track-ordered-property-with-tag nil
  11265. "Should the ORDERED property also be shown as a tag?
  11266. The ORDERED property decides if an entry should require subtasks to be
  11267. completed in sequence. Since a property is not very visible, setting
  11268. this option means that toggling the ORDERED property with the command
  11269. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11270. not relevant for the behavior, but it makes things more visible.
  11271. Note that toggling the tag with tags commands will not change the property
  11272. and therefore not influence behavior!
  11273. This can be t, meaning the tag ORDERED should be used, It can also be a
  11274. string to select a different tag for this task."
  11275. :group 'org-todo
  11276. :type '(choice
  11277. (const :tag "No tracking" nil)
  11278. (const :tag "Track with ORDERED tag" t)
  11279. (string :tag "Use other tag")))
  11280. (defun org-toggle-ordered-property ()
  11281. "Toggle the ORDERED property of the current entry.
  11282. For better visibility, you can track the value of this property with a tag.
  11283. See variable `org-track-ordered-property-with-tag'."
  11284. (interactive)
  11285. (let* ((t1 org-track-ordered-property-with-tag)
  11286. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11287. (save-excursion
  11288. (org-back-to-heading)
  11289. (if (org-entry-get nil "ORDERED")
  11290. (progn
  11291. (org-delete-property "ORDERED")
  11292. (and tag (org-toggle-tag tag 'off))
  11293. (message "Subtasks can be completed in arbitrary order"))
  11294. (org-entry-put nil "ORDERED" "t")
  11295. (and tag (org-toggle-tag tag 'on))
  11296. (message "Subtasks must be completed in sequence")))))
  11297. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11298. (defun org-block-todo-from-checkboxes (change-plist)
  11299. "Block turning an entry into a TODO, using checkboxes.
  11300. This checks whether the current task should be blocked from state
  11301. changes because there are unchecked boxes in this entry."
  11302. (if (not org-enforce-todo-checkbox-dependencies)
  11303. t ; if locally turned off don't block
  11304. (catch 'dont-block
  11305. ;; If this is not a todo state change, or if this entry is already DONE,
  11306. ;; do not block
  11307. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11308. (member (plist-get change-plist :from)
  11309. (cons 'done org-done-keywords))
  11310. (member (plist-get change-plist :to)
  11311. (cons 'todo org-not-done-keywords))
  11312. (not (plist-get change-plist :to)))
  11313. (throw 'dont-block t))
  11314. ;; If this task has checkboxes that are not checked, it's blocked
  11315. (save-excursion
  11316. (org-back-to-heading t)
  11317. (let ((beg (point)) end)
  11318. (outline-next-heading)
  11319. (setq end (point))
  11320. (goto-char beg)
  11321. (if (org-list-search-forward
  11322. (concat (org-item-beginning-re)
  11323. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11324. "\\[[- ]\\]")
  11325. end t)
  11326. (progn
  11327. (if (boundp 'org-blocked-by-checkboxes)
  11328. (setq org-blocked-by-checkboxes t))
  11329. (throw 'dont-block nil)))))
  11330. t))) ; do not block
  11331. (defun org-entry-blocked-p ()
  11332. "Is the current entry blocked?"
  11333. (org-with-silent-modifications
  11334. (if (org-entry-get nil "NOBLOCKING")
  11335. nil ;; Never block this entry
  11336. (not (run-hook-with-args-until-failure
  11337. 'org-blocker-hook
  11338. (list :type 'todo-state-change
  11339. :position (point)
  11340. :from 'todo
  11341. :to 'done))))))
  11342. (defun org-update-statistics-cookies (all)
  11343. "Update the statistics cookie, either from TODO or from checkboxes.
  11344. This should be called with the cursor in a line with a statistics cookie."
  11345. (interactive "P")
  11346. (if all
  11347. (progn
  11348. (org-update-checkbox-count 'all)
  11349. (org-map-entries 'org-update-parent-todo-statistics))
  11350. (if (not (org-at-heading-p))
  11351. (org-update-checkbox-count)
  11352. (let ((pos (point-marker))
  11353. end l1 l2)
  11354. (ignore-errors (org-back-to-heading t))
  11355. (if (not (org-at-heading-p))
  11356. (org-update-checkbox-count)
  11357. (setq l1 (org-outline-level))
  11358. (setq end (save-excursion
  11359. (outline-next-heading)
  11360. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11361. (point)))
  11362. (if (and (save-excursion
  11363. (re-search-forward
  11364. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11365. (not (save-excursion (re-search-forward
  11366. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11367. (org-update-checkbox-count)
  11368. (if (and l2 (> l2 l1))
  11369. (progn
  11370. (goto-char end)
  11371. (org-update-parent-todo-statistics))
  11372. (goto-char pos)
  11373. (beginning-of-line 1)
  11374. (while (re-search-forward
  11375. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11376. (point-at-eol) t)
  11377. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11378. (goto-char pos)
  11379. (move-marker pos nil)))))
  11380. (defvar org-entry-property-inherited-from) ;; defined below
  11381. (defun org-update-parent-todo-statistics ()
  11382. "Update any statistics cookie in the parent of the current headline.
  11383. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11384. the entire subtree and this will travel up the hierarchy and update
  11385. statistics everywhere."
  11386. (let* ((prop (save-excursion (org-up-heading-safe)
  11387. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11388. (recursive (or (not org-hierarchical-todo-statistics)
  11389. (and prop (string-match "\\<recursive\\>" prop))))
  11390. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11391. 0))
  11392. (first t)
  11393. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11394. level ltoggle l1 new ndel
  11395. (cnt-all 0) (cnt-done 0) is-percent kwd
  11396. checkbox-beg ov ovs ove cookie-present)
  11397. (catch 'exit
  11398. (save-excursion
  11399. (beginning-of-line 1)
  11400. (setq ltoggle (funcall outline-level))
  11401. ;; Three situations are to consider:
  11402. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11403. ;; to the top-level ancestor on the headline;
  11404. ;; 2. If parent has "recursive" property, repeat up to the
  11405. ;; headline setting that property, taking inheritance into
  11406. ;; account;
  11407. ;; 3. Else, move up to direct parent and proceed only once.
  11408. (while (and (setq level (org-up-heading-safe))
  11409. (or recursive first)
  11410. (>= (point) lim))
  11411. (setq first nil cookie-present nil)
  11412. (unless (and level
  11413. (not (string-match
  11414. "\\<checkbox\\>"
  11415. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11416. "")))))
  11417. (throw 'exit nil))
  11418. (while (re-search-forward box-re (point-at-eol) t)
  11419. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11420. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11421. (save-match-data
  11422. (unless (outline-next-heading) (throw 'exit nil))
  11423. (while (and (looking-at org-complex-heading-regexp)
  11424. (> (setq l1 (length (match-string 1))) level))
  11425. (setq kwd (and (or recursive (= l1 ltoggle))
  11426. (match-string 2)))
  11427. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11428. (and (listp org-provide-todo-statistics)
  11429. (or (member kwd org-provide-todo-statistics)
  11430. (member kwd org-done-keywords))))
  11431. (setq cnt-all (1+ cnt-all))
  11432. (if (eq org-provide-todo-statistics t)
  11433. (and kwd (setq cnt-all (1+ cnt-all)))))
  11434. (and (member kwd org-done-keywords)
  11435. (setq cnt-done (1+ cnt-done)))
  11436. (outline-next-heading)))
  11437. (setq new
  11438. (if is-percent
  11439. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11440. (format "[%d/%d]" cnt-done cnt-all))
  11441. ndel (- (match-end 0) checkbox-beg))
  11442. ;; handle overlays when updating cookie from column view
  11443. (when (setq ov (car (overlays-at checkbox-beg)))
  11444. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11445. (delete-overlay ov))
  11446. (goto-char checkbox-beg)
  11447. (insert new)
  11448. (delete-region (point) (+ (point) ndel))
  11449. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11450. (when ov (move-overlay ov ovs ove)))
  11451. (when cookie-present
  11452. (run-hook-with-args 'org-after-todo-statistics-hook
  11453. cnt-done (- cnt-all cnt-done))))))
  11454. (run-hooks 'org-todo-statistics-hook)))
  11455. (defvar org-after-todo-statistics-hook nil
  11456. "Hook that is called after a TODO statistics cookie has been updated.
  11457. Each function is called with two arguments: the number of not-done entries
  11458. and the number of done entries.
  11459. For example, the following function, when added to this hook, will switch
  11460. an entry to DONE when all children are done, and back to TODO when new
  11461. entries are set to a TODO status. Note that this hook is only called
  11462. when there is a statistics cookie in the headline!
  11463. (defun org-summary-todo (n-done n-not-done)
  11464. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11465. (let (org-log-done org-log-states) ; turn off logging
  11466. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11467. ")
  11468. (defvar org-todo-statistics-hook nil
  11469. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11470. This hook runs even if there is no statistics cookie present, in which case
  11471. `org-after-todo-statistics-hook' would not run.")
  11472. (defun org-todo-trigger-tag-changes (state)
  11473. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11474. (let ((l org-todo-state-tags-triggers)
  11475. changes)
  11476. (when (or (not state) (equal state ""))
  11477. (setq changes (append changes (cdr (assoc "" l)))))
  11478. (when (and (stringp state) (> (length state) 0))
  11479. (setq changes (append changes (cdr (assoc state l)))))
  11480. (when (member state org-not-done-keywords)
  11481. (setq changes (append changes (cdr (assoc 'todo l)))))
  11482. (when (member state org-done-keywords)
  11483. (setq changes (append changes (cdr (assoc 'done l)))))
  11484. (dolist (c changes)
  11485. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11486. (defun org-local-logging (value)
  11487. "Get logging settings from a property VALUE."
  11488. (let* (words w a)
  11489. ;; directly set the variables, they are already local.
  11490. (setq org-log-done nil
  11491. org-log-repeat nil
  11492. org-todo-log-states nil)
  11493. (setq words (org-split-string value))
  11494. (while (setq w (pop words))
  11495. (cond
  11496. ((setq a (assoc w org-startup-options))
  11497. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11498. (set (nth 1 a) (nth 2 a))))
  11499. ((setq a (org-extract-log-state-settings w))
  11500. (and (member (car a) org-todo-keywords-1)
  11501. (push a org-todo-log-states)))))))
  11502. (defun org-get-todo-sequence-head (kwd)
  11503. "Return the head of the TODO sequence to which KWD belongs.
  11504. If KWD is not set, check if there is a text property remembering the
  11505. right sequence."
  11506. (let (p)
  11507. (cond
  11508. ((not kwd)
  11509. (or (get-text-property (point-at-bol) 'org-todo-head)
  11510. (progn
  11511. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11512. nil (point-at-eol)))
  11513. (get-text-property p 'org-todo-head))))
  11514. ((not (member kwd org-todo-keywords-1))
  11515. (car org-todo-keywords-1))
  11516. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11517. (defun org-fast-todo-selection ()
  11518. "Fast TODO keyword selection with single keys.
  11519. Returns the new TODO keyword, or nil if no state change should occur."
  11520. (let* ((fulltable org-todo-key-alist)
  11521. (done-keywords org-done-keywords) ;; needed for the faces.
  11522. (maxlen (apply 'max (mapcar
  11523. (lambda (x)
  11524. (if (stringp (car x)) (string-width (car x)) 0))
  11525. fulltable)))
  11526. (expert nil)
  11527. (fwidth (+ maxlen 3 1 3))
  11528. (ncol (/ (- (window-width) 4) fwidth))
  11529. tg cnt e c tbl
  11530. groups ingroup)
  11531. (save-excursion
  11532. (save-window-excursion
  11533. (if expert
  11534. (set-buffer (get-buffer-create " *Org todo*"))
  11535. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11536. (erase-buffer)
  11537. (org-set-local 'org-done-keywords done-keywords)
  11538. (setq tbl fulltable cnt 0)
  11539. (while (setq e (pop tbl))
  11540. (cond
  11541. ((equal e '(:startgroup))
  11542. (push '() groups) (setq ingroup t)
  11543. (when (not (= cnt 0))
  11544. (setq cnt 0)
  11545. (insert "\n"))
  11546. (insert "{ "))
  11547. ((equal e '(:endgroup))
  11548. (setq ingroup nil cnt 0)
  11549. (insert "}\n"))
  11550. ((equal e '(:newline))
  11551. (when (not (= cnt 0))
  11552. (setq cnt 0)
  11553. (insert "\n")
  11554. (setq e (car tbl))
  11555. (while (equal (car tbl) '(:newline))
  11556. (insert "\n")
  11557. (setq tbl (cdr tbl)))))
  11558. (t
  11559. (setq tg (car e) c (cdr e))
  11560. (if ingroup (push tg (car groups)))
  11561. (setq tg (org-add-props tg nil 'face
  11562. (org-get-todo-face tg)))
  11563. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11564. (insert "[" c "] " tg (make-string
  11565. (- fwidth 4 (length tg)) ?\ ))
  11566. (when (= (setq cnt (1+ cnt)) ncol)
  11567. (insert "\n")
  11568. (if ingroup (insert " "))
  11569. (setq cnt 0)))))
  11570. (insert "\n")
  11571. (goto-char (point-min))
  11572. (if (not expert) (org-fit-window-to-buffer))
  11573. (message "[a-z..]:Set [SPC]:clear")
  11574. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11575. (cond
  11576. ((or (= c ?\C-g)
  11577. (and (= c ?q) (not (rassoc c fulltable))))
  11578. (setq quit-flag t))
  11579. ((= c ?\ ) nil)
  11580. ((setq e (rassoc c fulltable) tg (car e))
  11581. tg)
  11582. (t (setq quit-flag t)))))))
  11583. (defun org-entry-is-todo-p ()
  11584. (member (org-get-todo-state) org-not-done-keywords))
  11585. (defun org-entry-is-done-p ()
  11586. (member (org-get-todo-state) org-done-keywords))
  11587. (defun org-get-todo-state ()
  11588. "Return the TODO keyword of the current subtree."
  11589. (save-excursion
  11590. (org-back-to-heading t)
  11591. (and (looking-at org-todo-line-regexp)
  11592. (match-end 2)
  11593. (match-string 2))))
  11594. (defun org-at-date-range-p (&optional inactive-ok)
  11595. "Is the cursor inside a date range?"
  11596. (interactive)
  11597. (save-excursion
  11598. (catch 'exit
  11599. (let ((pos (point)))
  11600. (skip-chars-backward "^[<\r\n")
  11601. (skip-chars-backward "<[")
  11602. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11603. (>= (match-end 0) pos)
  11604. (throw 'exit t))
  11605. (skip-chars-backward "^<[\r\n")
  11606. (skip-chars-backward "<[")
  11607. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11608. (>= (match-end 0) pos)
  11609. (throw 'exit t)))
  11610. nil)))
  11611. (defun org-get-repeat (&optional tagline)
  11612. "Check if there is a deadline/schedule with repeater in this entry."
  11613. (save-match-data
  11614. (save-excursion
  11615. (org-back-to-heading t)
  11616. (and (re-search-forward (if tagline
  11617. (concat tagline "\\s-*" org-repeat-re)
  11618. org-repeat-re)
  11619. (org-entry-end-position) t)
  11620. (match-string-no-properties 1)))))
  11621. (defvar org-last-changed-timestamp)
  11622. (defvar org-last-inserted-timestamp)
  11623. (defvar org-log-post-message)
  11624. (defvar org-log-note-purpose)
  11625. (defvar org-log-note-how)
  11626. (defvar org-log-note-extra)
  11627. (defun org-auto-repeat-maybe (done-word)
  11628. "Check if the current headline contains a repeated deadline/schedule.
  11629. If yes, set TODO state back to what it was and change the base date
  11630. of repeating deadline/scheduled time stamps to new date.
  11631. This function is run automatically after each state change to a DONE state."
  11632. ;; last-state is dynamically scoped into this function
  11633. (let* ((repeat (org-get-repeat))
  11634. (aa (assoc org-last-state org-todo-kwd-alist))
  11635. (interpret (nth 1 aa))
  11636. (head (nth 2 aa))
  11637. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11638. (msg "Entry repeats: ")
  11639. (org-log-done nil)
  11640. (org-todo-log-states nil)
  11641. re type n what ts time to-state)
  11642. (when repeat
  11643. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11644. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11645. org-todo-repeat-to-state))
  11646. (unless (and to-state (member to-state org-todo-keywords-1))
  11647. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11648. (org-todo to-state)
  11649. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11650. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11651. (org-time-stamp-format t t))))
  11652. (when org-log-repeat
  11653. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11654. (memq 'org-add-log-note post-command-hook))
  11655. ;; OK, we are already setup for some record
  11656. (if (eq org-log-repeat 'note)
  11657. ;; make sure we take a note, not only a time stamp
  11658. (setq org-log-note-how 'note))
  11659. ;; Set up for taking a record
  11660. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11661. org-last-state
  11662. 'findpos org-log-repeat)))
  11663. (org-back-to-heading t)
  11664. (org-add-planning-info nil nil 'closed)
  11665. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11666. org-deadline-time-regexp "\\)\\|\\("
  11667. org-ts-regexp "\\)"))
  11668. (while (re-search-forward
  11669. re (save-excursion (outline-next-heading) (point)) t)
  11670. (setq type (if (match-end 1) org-scheduled-string
  11671. (if (match-end 3) org-deadline-string "Plain:"))
  11672. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11673. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11674. (setq n (string-to-number (match-string 2 ts))
  11675. what (match-string 3 ts))
  11676. (if (equal what "w") (setq n (* n 7) what "d"))
  11677. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11678. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11679. ;; Preparation, see if we need to modify the start date for the change
  11680. (when (match-end 1)
  11681. (setq time (save-match-data (org-time-string-to-time ts)))
  11682. (cond
  11683. ((equal (match-string 1 ts) ".")
  11684. ;; Shift starting date to today
  11685. (org-timestamp-change
  11686. (- (org-today) (time-to-days time))
  11687. 'day))
  11688. ((equal (match-string 1 ts) "+")
  11689. (let ((nshiftmax 10) (nshift 0))
  11690. (while (or (= nshift 0)
  11691. (<= (time-to-days time)
  11692. (time-to-days (current-time))))
  11693. (when (= (incf nshift) nshiftmax)
  11694. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11695. (error "Abort")))
  11696. (org-timestamp-change n (cdr (assoc what whata)))
  11697. (org-at-timestamp-p t)
  11698. (setq ts (match-string 1))
  11699. (setq time (save-match-data (org-time-string-to-time ts)))))
  11700. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11701. ;; rematch, so that we have everything in place for the real shift
  11702. (org-at-timestamp-p t)
  11703. (setq ts (match-string 1))
  11704. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11705. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11706. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11707. (setq org-log-post-message msg)
  11708. (message "%s" msg))))
  11709. (defun org-show-todo-tree (arg)
  11710. "Make a compact tree which shows all headlines marked with TODO.
  11711. The tree will show the lines where the regexp matches, and all higher
  11712. headlines above the match.
  11713. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11714. With a numeric prefix N, construct a sparse tree for the Nth element
  11715. of `org-todo-keywords-1'."
  11716. (interactive "P")
  11717. (let ((case-fold-search nil)
  11718. (kwd-re
  11719. (cond ((null arg) org-not-done-regexp)
  11720. ((equal arg '(4))
  11721. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11722. (mapcar 'list org-todo-keywords-1))))
  11723. (concat "\\("
  11724. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11725. "\\)\\>")))
  11726. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11727. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11728. org-todo-keywords-1)))
  11729. (t (user-error "Invalid prefix argument: %s" arg)))))
  11730. (message "%d TODO entries found"
  11731. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11732. (defun org-deadline (arg &optional time)
  11733. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11734. With one universal prefix argument, remove any deadline from the item.
  11735. With two universal prefix arguments, prompt for a warning delay.
  11736. With argument TIME, set the deadline at the corresponding date. TIME
  11737. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11738. (interactive "P")
  11739. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11740. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11741. 'region-start-level 'region))
  11742. org-loop-over-headlines-in-active-region)
  11743. (org-map-entries
  11744. `(org-deadline ',arg ,time)
  11745. org-loop-over-headlines-in-active-region
  11746. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11747. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11748. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11749. (repeater (and old-date
  11750. (string-match
  11751. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11752. old-date)
  11753. (match-string 1 old-date))))
  11754. (cond
  11755. ((equal arg '(4))
  11756. (when (and old-date org-log-redeadline)
  11757. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11758. org-log-redeadline))
  11759. (org-remove-timestamp-with-keyword org-deadline-string)
  11760. (message "Item no longer has a deadline."))
  11761. ((equal arg '(16))
  11762. (save-excursion
  11763. (if (re-search-forward
  11764. org-deadline-time-regexp
  11765. (save-excursion (outline-next-heading) (point)) t)
  11766. (let* ((rpl0 (match-string 1))
  11767. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11768. (replace-match
  11769. (concat org-deadline-string
  11770. " <" rpl
  11771. (format " -%dd"
  11772. (abs
  11773. (- (time-to-days
  11774. (save-match-data
  11775. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11776. (time-to-days old-date-time))))
  11777. ">") t t))
  11778. (user-error "No deadline information to update"))))
  11779. (t
  11780. (org-add-planning-info 'deadline time 'closed)
  11781. (when (and old-date org-log-redeadline
  11782. (not (equal old-date
  11783. (substring org-last-inserted-timestamp 1 -1))))
  11784. (org-add-log-setup 'redeadline nil old-date 'findpos
  11785. org-log-redeadline))
  11786. (when repeater
  11787. (save-excursion
  11788. (org-back-to-heading t)
  11789. (when (re-search-forward (concat org-deadline-string " "
  11790. org-last-inserted-timestamp)
  11791. (save-excursion
  11792. (outline-next-heading) (point)) t)
  11793. (goto-char (1- (match-end 0)))
  11794. (insert " " repeater)
  11795. (setq org-last-inserted-timestamp
  11796. (concat (substring org-last-inserted-timestamp 0 -1)
  11797. " " repeater
  11798. (substring org-last-inserted-timestamp -1))))))
  11799. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11800. (defun org-schedule (arg &optional time)
  11801. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11802. With one universal prefix argument, remove any scheduling date from the item.
  11803. With two universal prefix arguments, prompt for a delay cookie.
  11804. With argument TIME, scheduled at the corresponding date. TIME can
  11805. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11806. (interactive "P")
  11807. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11808. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11809. 'region-start-level 'region))
  11810. org-loop-over-headlines-in-active-region)
  11811. (org-map-entries
  11812. `(org-schedule ',arg ,time)
  11813. org-loop-over-headlines-in-active-region
  11814. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11815. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11816. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11817. (repeater (and old-date
  11818. (string-match
  11819. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11820. old-date)
  11821. (match-string 1 old-date))))
  11822. (cond
  11823. ((equal arg '(4))
  11824. (progn
  11825. (when (and old-date org-log-reschedule)
  11826. (org-add-log-setup 'delschedule nil old-date 'findpos
  11827. org-log-reschedule))
  11828. (org-remove-timestamp-with-keyword org-scheduled-string)
  11829. (message "Item is no longer scheduled.")))
  11830. ((equal arg '(16))
  11831. (save-excursion
  11832. (if (re-search-forward
  11833. org-scheduled-time-regexp
  11834. (save-excursion (outline-next-heading) (point)) t)
  11835. (let* ((rpl0 (match-string 1))
  11836. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11837. (replace-match
  11838. (concat org-scheduled-string
  11839. " <" rpl
  11840. (format " -%dd"
  11841. (abs
  11842. (- (time-to-days
  11843. (save-match-data
  11844. (org-read-date nil t nil "Delay until" old-date-time)))
  11845. (time-to-days old-date-time))))
  11846. ">") t t))
  11847. (user-error "No scheduled information to update"))))
  11848. (t
  11849. (org-add-planning-info 'scheduled time 'closed)
  11850. (when (and old-date org-log-reschedule
  11851. (not (equal old-date
  11852. (substring org-last-inserted-timestamp 1 -1))))
  11853. (org-add-log-setup 'reschedule nil old-date 'findpos
  11854. org-log-reschedule))
  11855. (when repeater
  11856. (save-excursion
  11857. (org-back-to-heading t)
  11858. (when (re-search-forward (concat org-scheduled-string " "
  11859. org-last-inserted-timestamp)
  11860. (save-excursion
  11861. (outline-next-heading) (point)) t)
  11862. (goto-char (1- (match-end 0)))
  11863. (insert " " repeater)
  11864. (setq org-last-inserted-timestamp
  11865. (concat (substring org-last-inserted-timestamp 0 -1)
  11866. " " repeater
  11867. (substring org-last-inserted-timestamp -1))))))
  11868. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11869. (defun org-get-scheduled-time (pom &optional inherit)
  11870. "Get the scheduled time as a time tuple, of a format suitable
  11871. for calling org-schedule with, or if there is no scheduling,
  11872. returns nil."
  11873. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11874. (when time
  11875. (apply 'encode-time (org-parse-time-string time)))))
  11876. (defun org-get-deadline-time (pom &optional inherit)
  11877. "Get the deadline as a time tuple, of a format suitable for
  11878. calling org-deadline with, or if there is no scheduling, returns
  11879. nil."
  11880. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11881. (when time
  11882. (apply 'encode-time (org-parse-time-string time)))))
  11883. (defun org-remove-timestamp-with-keyword (keyword)
  11884. "Remove all time stamps with KEYWORD in the current entry."
  11885. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11886. beg)
  11887. (save-excursion
  11888. (org-back-to-heading t)
  11889. (setq beg (point))
  11890. (outline-next-heading)
  11891. (while (re-search-backward re beg t)
  11892. (replace-match "")
  11893. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11894. (equal (char-before) ?\ ))
  11895. (backward-delete-char 1)
  11896. (if (string-match "^[ \t]*$" (buffer-substring
  11897. (point-at-bol) (point-at-eol)))
  11898. (delete-region (point-at-bol)
  11899. (min (point-max) (1+ (point-at-eol))))))))))
  11900. (defun org-add-planning-info (what &optional time &rest remove)
  11901. "Insert new timestamp with keyword in the line directly after the headline.
  11902. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11903. If non is given, the user is prompted for a date.
  11904. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11905. be removed."
  11906. (interactive)
  11907. (let (org-time-was-given org-end-time-was-given ts
  11908. end default-time default-input)
  11909. (catch 'exit
  11910. (when (and (memq what '(scheduled deadline))
  11911. (or (not time)
  11912. (and (stringp time)
  11913. (string-match "^[-+]+[0-9]" time))))
  11914. ;; Try to get a default date/time from existing timestamp
  11915. (save-excursion
  11916. (org-back-to-heading t)
  11917. (setq end (save-excursion (outline-next-heading) (point)))
  11918. (when (re-search-forward (if (eq what 'scheduled)
  11919. org-scheduled-time-regexp
  11920. org-deadline-time-regexp)
  11921. end t)
  11922. (setq ts (match-string 1)
  11923. default-time
  11924. (apply 'encode-time (org-parse-time-string ts))
  11925. default-input (and ts (org-get-compact-tod ts))))))
  11926. (when what
  11927. (setq time
  11928. (if (stringp time)
  11929. ;; This is a string (relative or absolute), set proper date
  11930. (apply 'encode-time
  11931. (org-read-date-analyze
  11932. time default-time (decode-time default-time)))
  11933. ;; If necessary, get the time from the user
  11934. (or time (org-read-date nil 'to-time nil nil
  11935. default-time default-input)))))
  11936. (when (and org-insert-labeled-timestamps-at-point
  11937. (member what '(scheduled deadline)))
  11938. (insert
  11939. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11940. (org-insert-time-stamp time org-time-was-given
  11941. nil nil nil (list org-end-time-was-given))
  11942. (setq what nil))
  11943. (save-excursion
  11944. (save-restriction
  11945. (let (col list elt ts buffer-invisibility-spec)
  11946. (org-back-to-heading t)
  11947. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11948. (goto-char (match-end 1))
  11949. (setq col (current-column))
  11950. (goto-char (match-end 0))
  11951. (if (eobp) (insert "\n") (forward-char 1))
  11952. (when (and (not what)
  11953. (not (looking-at
  11954. (concat "[ \t]*"
  11955. org-keyword-time-not-clock-regexp))))
  11956. ;; Nothing to add, nothing to remove...... :-)
  11957. (throw 'exit nil))
  11958. (if (and (not (looking-at org-outline-regexp))
  11959. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11960. "[^\r\n]*"))
  11961. (not (equal (match-string 1) org-clock-string)))
  11962. (narrow-to-region (match-beginning 0) (match-end 0))
  11963. (insert-before-markers "\n")
  11964. (backward-char 1)
  11965. (narrow-to-region (point) (point))
  11966. (and org-adapt-indentation (org-indent-to-column col)))
  11967. ;; Check if we have to remove something.
  11968. (setq list (cons what remove))
  11969. (while list
  11970. (setq elt (pop list))
  11971. (when (or (and (eq elt 'scheduled)
  11972. (re-search-forward org-scheduled-time-regexp nil t))
  11973. (and (eq elt 'deadline)
  11974. (re-search-forward org-deadline-time-regexp nil t))
  11975. (and (eq elt 'closed)
  11976. (re-search-forward org-closed-time-regexp nil t)))
  11977. (replace-match "")
  11978. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11979. (and (looking-at "[ \t]+") (replace-match ""))
  11980. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11981. (when what
  11982. (insert
  11983. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11984. (cond ((eq what 'scheduled) org-scheduled-string)
  11985. ((eq what 'deadline) org-deadline-string)
  11986. ((eq what 'closed) org-closed-string))
  11987. " ")
  11988. (setq ts (org-insert-time-stamp
  11989. time
  11990. (or org-time-was-given
  11991. (and (eq what 'closed) org-log-done-with-time))
  11992. (eq what 'closed)
  11993. nil nil (list org-end-time-was-given)))
  11994. (insert
  11995. (if (not (or (bolp) (eq (char-before) ?\ )
  11996. (memq (char-after) '(32 10))
  11997. (eobp))) " " ""))
  11998. (end-of-line 1))
  11999. (goto-char (point-min))
  12000. (widen)
  12001. (if (and (looking-at "[ \t]*\n")
  12002. (equal (char-before) ?\n))
  12003. (delete-region (1- (point)) (point-at-eol)))
  12004. ts))))))
  12005. (defvar org-log-note-marker (make-marker))
  12006. (defvar org-log-note-purpose nil)
  12007. (defvar org-log-note-state nil)
  12008. (defvar org-log-note-previous-state nil)
  12009. (defvar org-log-note-how nil)
  12010. (defvar org-log-note-extra nil)
  12011. (defvar org-log-note-window-configuration nil)
  12012. (defvar org-log-note-return-to (make-marker))
  12013. (defvar org-log-note-effective-time nil
  12014. "Remembered current time so that dynamically scoped
  12015. `org-extend-today-until' affects tha timestamps in state change
  12016. log")
  12017. (defvar org-log-post-message nil
  12018. "Message to be displayed after a log note has been stored.
  12019. The auto-repeater uses this.")
  12020. (defun org-add-note ()
  12021. "Add a note to the current entry.
  12022. This is done in the same way as adding a state change note."
  12023. (interactive)
  12024. (org-add-log-setup 'note nil nil 'findpos nil))
  12025. (defvar org-property-end-re)
  12026. (defun org-add-log-setup (&optional purpose state prev-state
  12027. findpos how extra)
  12028. "Set up the post command hook to take a note.
  12029. If this is about to TODO state change, the new state is expected in STATE.
  12030. When FINDPOS is non-nil, find the correct position for the note in
  12031. the current entry. If not, assume that it can be inserted at point.
  12032. HOW is an indicator what kind of note should be created.
  12033. EXTRA is additional text that will be inserted into the notes buffer."
  12034. (let* ((org-log-into-drawer (org-log-into-drawer))
  12035. (drawer (cond ((stringp org-log-into-drawer)
  12036. org-log-into-drawer)
  12037. (org-log-into-drawer "LOGBOOK"))))
  12038. (save-restriction
  12039. (save-excursion
  12040. (when findpos
  12041. (org-back-to-heading t)
  12042. (narrow-to-region (point) (save-excursion
  12043. (outline-next-heading) (point)))
  12044. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12045. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12046. "[^\r\n]*\\)?"))
  12047. (goto-char (match-end 0))
  12048. (cond
  12049. (drawer
  12050. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12051. nil t)
  12052. (progn
  12053. (goto-char (match-end 0))
  12054. (or org-log-states-order-reversed
  12055. (and (re-search-forward org-property-end-re nil t)
  12056. (goto-char (1- (match-beginning 0))))))
  12057. (insert "\n:" drawer ":\n:END:")
  12058. (beginning-of-line 0)
  12059. (org-indent-line)
  12060. (beginning-of-line 2)
  12061. (org-indent-line)
  12062. (end-of-line 0)))
  12063. ((and org-log-state-notes-insert-after-drawers
  12064. (save-excursion
  12065. (forward-line) (looking-at org-drawer-regexp)))
  12066. (forward-line)
  12067. (while (looking-at org-drawer-regexp)
  12068. (goto-char (match-end 0))
  12069. (re-search-forward org-property-end-re (point-max) t)
  12070. (forward-line))
  12071. (forward-line -1)))
  12072. (unless org-log-states-order-reversed
  12073. (and (= (char-after) ?\n) (forward-char 1))
  12074. (org-skip-over-state-notes)
  12075. (skip-chars-backward " \t\n\r")))
  12076. (move-marker org-log-note-marker (point))
  12077. (setq org-log-note-purpose purpose
  12078. org-log-note-state state
  12079. org-log-note-previous-state prev-state
  12080. org-log-note-how how
  12081. org-log-note-extra extra
  12082. org-log-note-effective-time (org-current-effective-time))
  12083. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12084. (defun org-skip-over-state-notes ()
  12085. "Skip past the list of State notes in an entry."
  12086. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12087. (when (ignore-errors (goto-char (org-in-item-p)))
  12088. (let* ((struct (org-list-struct))
  12089. (prevs (org-list-prevs-alist struct)))
  12090. (while (looking-at "[ \t]*- State")
  12091. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12092. (org-list-get-item-end (point) struct)))))))
  12093. (defun org-add-log-note (&optional purpose)
  12094. "Pop up a window for taking a note, and add this note later at point."
  12095. (remove-hook 'post-command-hook 'org-add-log-note)
  12096. (setq org-log-note-window-configuration (current-window-configuration))
  12097. (delete-other-windows)
  12098. (move-marker org-log-note-return-to (point))
  12099. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12100. (goto-char org-log-note-marker)
  12101. (org-switch-to-buffer-other-window "*Org Note*")
  12102. (erase-buffer)
  12103. (if (memq org-log-note-how '(time state))
  12104. (let (current-prefix-arg) (org-store-log-note))
  12105. (let ((org-inhibit-startup t)) (org-mode))
  12106. (insert (format "# Insert note for %s.
  12107. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12108. (cond
  12109. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12110. ((eq org-log-note-purpose 'done) "closed todo item")
  12111. ((eq org-log-note-purpose 'state)
  12112. (format "state change from \"%s\" to \"%s\""
  12113. (or org-log-note-previous-state "")
  12114. (or org-log-note-state "")))
  12115. ((eq org-log-note-purpose 'reschedule)
  12116. "rescheduling")
  12117. ((eq org-log-note-purpose 'delschedule)
  12118. "no longer scheduled")
  12119. ((eq org-log-note-purpose 'redeadline)
  12120. "changing deadline")
  12121. ((eq org-log-note-purpose 'deldeadline)
  12122. "removing deadline")
  12123. ((eq org-log-note-purpose 'refile)
  12124. "refiling")
  12125. ((eq org-log-note-purpose 'note)
  12126. "this entry")
  12127. (t (error "This should not happen")))))
  12128. (if org-log-note-extra (insert org-log-note-extra))
  12129. (org-set-local 'org-finish-function 'org-store-log-note)
  12130. (run-hooks 'org-log-buffer-setup-hook)))
  12131. (defvar org-note-abort nil) ; dynamically scoped
  12132. (defun org-store-log-note ()
  12133. "Finish taking a log note, and insert it to where it belongs."
  12134. (let ((txt (buffer-string)))
  12135. (kill-buffer (current-buffer))
  12136. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12137. lines ind bul)
  12138. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12139. (setq txt (replace-match "" t t txt)))
  12140. (if (string-match "\\s-+\\'" txt)
  12141. (setq txt (replace-match "" t t txt)))
  12142. (setq lines (org-split-string txt "\n"))
  12143. (when (and note (string-match "\\S-" note))
  12144. (setq note
  12145. (org-replace-escapes
  12146. note
  12147. (list (cons "%u" (user-login-name))
  12148. (cons "%U" user-full-name)
  12149. (cons "%t" (format-time-string
  12150. (org-time-stamp-format 'long 'inactive)
  12151. org-log-note-effective-time))
  12152. (cons "%T" (format-time-string
  12153. (org-time-stamp-format 'long nil)
  12154. org-log-note-effective-time))
  12155. (cons "%d" (format-time-string
  12156. (org-time-stamp-format nil 'inactive)
  12157. org-log-note-effective-time))
  12158. (cons "%D" (format-time-string
  12159. (org-time-stamp-format nil nil)
  12160. org-log-note-effective-time))
  12161. (cons "%s" (if org-log-note-state
  12162. (concat "\"" org-log-note-state "\"")
  12163. ""))
  12164. (cons "%S" (if org-log-note-previous-state
  12165. (concat "\"" org-log-note-previous-state "\"")
  12166. "\"\"")))))
  12167. (if lines (setq note (concat note " \\\\")))
  12168. (push note lines))
  12169. (when (or current-prefix-arg org-note-abort)
  12170. (when org-log-into-drawer
  12171. (org-remove-empty-drawer-at org-log-note-marker))
  12172. (setq lines nil))
  12173. (when lines
  12174. (with-current-buffer (marker-buffer org-log-note-marker)
  12175. (save-excursion
  12176. (goto-char org-log-note-marker)
  12177. (move-marker org-log-note-marker nil)
  12178. (end-of-line 1)
  12179. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12180. (setq ind (save-excursion
  12181. (if (ignore-errors (goto-char (org-in-item-p)))
  12182. (let ((struct (org-list-struct)))
  12183. (org-list-get-ind
  12184. (org-list-get-top-point struct) struct))
  12185. (skip-chars-backward " \r\t\n")
  12186. (cond
  12187. ((and (org-at-heading-p)
  12188. org-adapt-indentation)
  12189. (1+ (org-current-level)))
  12190. ((org-at-heading-p) 0)
  12191. (t (org-get-indentation))))))
  12192. (setq bul (org-list-bullet-string "-"))
  12193. (org-indent-line-to ind)
  12194. (insert bul (pop lines))
  12195. (let ((ind-body (+ (length bul) ind)))
  12196. (while lines
  12197. (insert "\n")
  12198. (org-indent-line-to ind-body)
  12199. (insert (pop lines))))
  12200. (message "Note stored")
  12201. (org-back-to-heading t)
  12202. (org-cycle-hide-drawers 'children))
  12203. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12204. ;; from within `org-add-log-note' because `buffer-undo-list'
  12205. ;; is then modified outside of `org-with-remote-undo'.
  12206. (when (eq this-command 'org-agenda-todo)
  12207. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12208. ;; Don't add undo information when called from `org-agenda-todo'
  12209. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12210. (set-window-configuration org-log-note-window-configuration)
  12211. (with-current-buffer (marker-buffer org-log-note-return-to)
  12212. (goto-char org-log-note-return-to))
  12213. (move-marker org-log-note-return-to nil)
  12214. (and org-log-post-message (message "%s" org-log-post-message))))
  12215. (defun org-remove-empty-drawer-at (pos)
  12216. "Remove an empty drawer at position POS.
  12217. POS may also be a marker."
  12218. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12219. (org-with-wide-buffer
  12220. (goto-char pos)
  12221. (let ((drawer (org-element-at-point)))
  12222. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12223. (not (org-element-property :contents-begin drawer)))
  12224. (delete-region (org-element-property :begin drawer)
  12225. (progn (goto-char (org-element-property :end drawer))
  12226. (skip-chars-backward " \r\t\n")
  12227. (forward-line)
  12228. (point))))))))
  12229. (defvar org-ts-type nil)
  12230. (defun org-sparse-tree (&optional arg type)
  12231. "Create a sparse tree, prompt for the details.
  12232. This command can create sparse trees. You first need to select the type
  12233. of match used to create the tree:
  12234. t Show all TODO entries.
  12235. T Show entries with a specific TODO keyword.
  12236. m Show entries selected by a tags/property match.
  12237. p Enter a property name and its value (both with completion on existing
  12238. names/values) and show entries with that property.
  12239. r Show entries matching a regular expression (`/' can be used as well).
  12240. b Show deadlines and scheduled items before a date.
  12241. a Show deadlines and scheduled items after a date.
  12242. d Show deadlines due within `org-deadline-warning-days'.
  12243. D Show deadlines and scheduled items between a date range."
  12244. (interactive "P")
  12245. (let (ans kwd value ts-type)
  12246. (setq type (or type org-sparse-tree-default-date-type))
  12247. (setq org-ts-type type)
  12248. (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"
  12249. (cond ((eq type 'all) "all timestamps")
  12250. ((eq type 'scheduled) "only scheduled")
  12251. ((eq type 'deadline) "only deadline")
  12252. ((eq type 'active) "only active timestamps")
  12253. ((eq type 'inactive) "only inactive timestamps")
  12254. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12255. ((eq type 'closed) "with a closed time-stamp")
  12256. (t "scheduled/deadline")))
  12257. (setq ans (read-char-exclusive))
  12258. (cond
  12259. ((equal ans ?c)
  12260. (org-sparse-tree
  12261. arg (cadr (member type '(scheduled-or-deadline
  12262. all scheduled deadline active inactive closed)))))
  12263. ((equal ans ?d)
  12264. (call-interactively 'org-check-deadlines))
  12265. ((equal ans ?b)
  12266. (call-interactively 'org-check-before-date))
  12267. ((equal ans ?a)
  12268. (call-interactively 'org-check-after-date))
  12269. ((equal ans ?D)
  12270. (call-interactively 'org-check-dates-range))
  12271. ((equal ans ?t)
  12272. (call-interactively 'org-show-todo-tree))
  12273. ((equal ans ?T)
  12274. (org-show-todo-tree '(4)))
  12275. ((member ans '(?T ?m))
  12276. (call-interactively 'org-match-sparse-tree))
  12277. ((member ans '(?p ?P))
  12278. (setq kwd (org-icompleting-read "Property: "
  12279. (mapcar 'list (org-buffer-property-keys))))
  12280. (setq value (org-icompleting-read "Value: "
  12281. (mapcar 'list (org-property-values kwd))))
  12282. (unless (string-match "\\`{.*}\\'" value)
  12283. (setq value (concat "\"" value "\"")))
  12284. (org-match-sparse-tree arg (concat kwd "=" value)))
  12285. ((member ans '(?r ?R ?/))
  12286. (call-interactively 'org-occur))
  12287. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12288. (defvar org-occur-highlights nil
  12289. "List of overlays used for occur matches.")
  12290. (make-variable-buffer-local 'org-occur-highlights)
  12291. (defvar org-occur-parameters nil
  12292. "Parameters of the active org-occur calls.
  12293. This is a list, each call to org-occur pushes as cons cell,
  12294. containing the regular expression and the callback, onto the list.
  12295. The list can contain several entries if `org-occur' has been called
  12296. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12297. will only contain one set of parameters. When the highlights are
  12298. removed (for example with `C-c C-c', or with the next edit (depending
  12299. on `org-remove-highlights-with-change'), this variable is emptied
  12300. as well.")
  12301. (make-variable-buffer-local 'org-occur-parameters)
  12302. (defun org-occur (regexp &optional keep-previous callback)
  12303. "Make a compact tree which shows all matches of REGEXP.
  12304. The tree will show the lines where the regexp matches, and all higher
  12305. headlines above the match. It will also show the heading after the match,
  12306. to make sure editing the matching entry is easy.
  12307. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12308. call to `org-occur' will be kept, to allow stacking of calls to this
  12309. command.
  12310. If CALLBACK is non-nil, it is a function which is called to confirm
  12311. that the match should indeed be shown."
  12312. (interactive "sRegexp: \nP")
  12313. (when (equal regexp "")
  12314. (user-error "Regexp cannot be empty"))
  12315. (unless keep-previous
  12316. (org-remove-occur-highlights nil nil t))
  12317. (push (cons regexp callback) org-occur-parameters)
  12318. (let ((cnt 0))
  12319. (save-excursion
  12320. (goto-char (point-min))
  12321. (if (or (not keep-previous) ; do not want to keep
  12322. (not org-occur-highlights)) ; no previous matches
  12323. ;; hide everything
  12324. (org-overview))
  12325. (while (re-search-forward regexp nil t)
  12326. (when (or (not callback)
  12327. (save-match-data (funcall callback)))
  12328. (setq cnt (1+ cnt))
  12329. (when org-highlight-sparse-tree-matches
  12330. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12331. (org-show-context 'occur-tree))))
  12332. (when org-remove-highlights-with-change
  12333. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12334. nil 'local))
  12335. (unless org-sparse-tree-open-archived-trees
  12336. (org-hide-archived-subtrees (point-min) (point-max)))
  12337. (run-hooks 'org-occur-hook)
  12338. (if (org-called-interactively-p 'interactive)
  12339. (message "%d match(es) for regexp %s" cnt regexp))
  12340. cnt))
  12341. (defun org-occur-next-match (&optional n reset)
  12342. "Function for `next-error-function' to find sparse tree matches.
  12343. N is the number of matches to move, when negative move backwards.
  12344. RESET is entirely ignored - this function always goes back to the
  12345. starting point when no match is found."
  12346. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12347. (search-func (if (< n 0)
  12348. 'previous-single-char-property-change
  12349. 'next-single-char-property-change))
  12350. (n (abs n))
  12351. (pos (point))
  12352. p1)
  12353. (catch 'exit
  12354. (while (setq p1 (funcall search-func (point) 'org-type))
  12355. (when (equal p1 limit)
  12356. (goto-char pos)
  12357. (error "No more matches"))
  12358. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12359. (setq n (1- n))
  12360. (when (= n 0)
  12361. (goto-char p1)
  12362. (throw 'exit (point))))
  12363. (goto-char p1))
  12364. (goto-char p1)
  12365. (error "No more matches"))))
  12366. (defun org-show-context (&optional key)
  12367. "Make sure point and context are visible.
  12368. How much context is shown depends upon the variables
  12369. `org-show-hierarchy-above', `org-show-following-heading',
  12370. `org-show-entry-below' and `org-show-siblings'."
  12371. (let ((heading-p (org-at-heading-p t))
  12372. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12373. (following-p (org-get-alist-option org-show-following-heading key))
  12374. (entry-p (org-get-alist-option org-show-entry-below key))
  12375. (siblings-p (org-get-alist-option org-show-siblings key)))
  12376. ;; Show heading or entry text
  12377. (if (and heading-p (not entry-p))
  12378. (org-flag-heading nil) ; only show the heading
  12379. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12380. (org-show-hidden-entry))) ; show entire entry
  12381. (when following-p
  12382. ;; Show next sibling, or heading below text
  12383. (save-excursion
  12384. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12385. (org-flag-heading nil))))
  12386. (when siblings-p (org-show-siblings))
  12387. (when hierarchy-p
  12388. ;; show all higher headings, possibly with siblings
  12389. (save-excursion
  12390. (while (and (condition-case nil
  12391. (progn (org-up-heading-all 1) t)
  12392. (error nil))
  12393. (not (bobp)))
  12394. (org-flag-heading nil)
  12395. (when siblings-p (org-show-siblings)))))
  12396. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12397. (defvar org-reveal-start-hook nil
  12398. "Hook run before revealing a location.")
  12399. (defun org-reveal (&optional siblings)
  12400. "Show current entry, hierarchy above it, and the following headline.
  12401. This can be used to show a consistent set of context around locations
  12402. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12403. not t for the search context.
  12404. With optional argument SIBLINGS, on each level of the hierarchy all
  12405. siblings are shown. This repairs the tree structure to what it would
  12406. look like when opened with hierarchical calls to `org-cycle'.
  12407. With double optional argument \\[universal-argument] \\[universal-argument], \
  12408. go to the parent and show the
  12409. entire tree."
  12410. (interactive "P")
  12411. (run-hooks 'org-reveal-start-hook)
  12412. (let ((org-show-hierarchy-above t)
  12413. (org-show-following-heading t)
  12414. (org-show-siblings (if siblings t org-show-siblings)))
  12415. (org-show-context nil))
  12416. (when (equal siblings '(16))
  12417. (save-excursion
  12418. (when (org-up-heading-safe)
  12419. (org-show-subtree)
  12420. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12421. (defun org-highlight-new-match (beg end)
  12422. "Highlight from BEG to END and mark the highlight is an occur headline."
  12423. (let ((ov (make-overlay beg end)))
  12424. (overlay-put ov 'face 'secondary-selection)
  12425. (overlay-put ov 'org-type 'org-occur)
  12426. (push ov org-occur-highlights)))
  12427. (defun org-remove-occur-highlights (&optional beg end noremove)
  12428. "Remove the occur highlights from the buffer.
  12429. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12430. from the `before-change-functions' in the current buffer."
  12431. (interactive)
  12432. (unless org-inhibit-highlight-removal
  12433. (mapc 'delete-overlay org-occur-highlights)
  12434. (setq org-occur-highlights nil)
  12435. (setq org-occur-parameters nil)
  12436. (unless noremove
  12437. (remove-hook 'before-change-functions
  12438. 'org-remove-occur-highlights 'local))))
  12439. ;;;; Priorities
  12440. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12441. "Regular expression matching the priority indicator.")
  12442. (defvar org-remove-priority-next-time nil)
  12443. (defun org-priority-up ()
  12444. "Increase the priority of the current item."
  12445. (interactive)
  12446. (org-priority 'up))
  12447. (defun org-priority-down ()
  12448. "Decrease the priority of the current item."
  12449. (interactive)
  12450. (org-priority 'down))
  12451. (defun org-priority (&optional action show)
  12452. "Change the priority of an item.
  12453. ACTION can be `set', `up', `down', or a character."
  12454. (interactive "P")
  12455. (if (equal action '(4))
  12456. (org-show-priority)
  12457. (unless org-enable-priority-commands
  12458. (user-error "Priority commands are disabled"))
  12459. (setq action (or action 'set))
  12460. (let (current new news have remove)
  12461. (save-excursion
  12462. (org-back-to-heading t)
  12463. (if (looking-at org-priority-regexp)
  12464. (setq current (string-to-char (match-string 2))
  12465. have t))
  12466. (cond
  12467. ((eq action 'remove)
  12468. (setq remove t new ?\ ))
  12469. ((or (eq action 'set)
  12470. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12471. (if (not (eq action 'set))
  12472. (setq new action)
  12473. (message "Priority %c-%c, SPC to remove: "
  12474. org-highest-priority org-lowest-priority)
  12475. (save-match-data
  12476. (setq new (read-char-exclusive))))
  12477. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12478. (= (upcase org-lowest-priority) org-lowest-priority))
  12479. (setq new (upcase new)))
  12480. (cond ((equal new ?\ ) (setq remove t))
  12481. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12482. (user-error "Priority must be between `%c' and `%c'"
  12483. org-highest-priority org-lowest-priority))))
  12484. ((eq action 'up)
  12485. (setq new (if have
  12486. (1- current) ; normal cycling
  12487. ;; last priority was empty
  12488. (if (eq last-command this-command)
  12489. org-lowest-priority ; wrap around empty to lowest
  12490. ;; default
  12491. (if org-priority-start-cycle-with-default
  12492. org-default-priority
  12493. (1- org-default-priority))))))
  12494. ((eq action 'down)
  12495. (setq new (if have
  12496. (1+ current) ; normal cycling
  12497. ;; last priority was empty
  12498. (if (eq last-command this-command)
  12499. org-highest-priority ; wrap around empty to highest
  12500. ;; default
  12501. (if org-priority-start-cycle-with-default
  12502. org-default-priority
  12503. (1+ org-default-priority))))))
  12504. (t (user-error "Invalid action")))
  12505. (if (or (< (upcase new) org-highest-priority)
  12506. (> (upcase new) org-lowest-priority))
  12507. (if (and (memq action '(up down))
  12508. (not have) (not (eq last-command this-command)))
  12509. ;; `new' is from default priority
  12510. (error
  12511. "The default can not be set, see `org-default-priority' why")
  12512. ;; normal cycling: `new' is beyond highest/lowest priority
  12513. ;; and is wrapped around to the empty priority
  12514. (setq remove t)))
  12515. (setq news (format "%c" new))
  12516. (if have
  12517. (if remove
  12518. (replace-match "" t t nil 1)
  12519. (replace-match news t t nil 2))
  12520. (if remove
  12521. (user-error "No priority cookie found in line")
  12522. (let ((case-fold-search nil))
  12523. (looking-at org-todo-line-regexp))
  12524. (if (match-end 2)
  12525. (progn
  12526. (goto-char (match-end 2))
  12527. (insert " [#" news "]"))
  12528. (goto-char (match-beginning 3))
  12529. (insert "[#" news "] "))))
  12530. (org-preserve-lc (org-set-tags nil 'align)))
  12531. (if remove
  12532. (message "Priority removed")
  12533. (message "Priority of current item set to %s" news)))))
  12534. (defun org-show-priority ()
  12535. "Show the priority of the current item.
  12536. This priority is composed of the main priority given with the [#A] cookies,
  12537. and by additional input from the age of a schedules or deadline entry."
  12538. (interactive)
  12539. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12540. (org-get-at-bol 'priority)
  12541. (save-excursion
  12542. (save-match-data
  12543. (beginning-of-line)
  12544. (and (looking-at org-heading-regexp)
  12545. (org-get-priority (match-string 0))))))))
  12546. (message "Priority is %d" (if pri pri -1000))))
  12547. (defun org-get-priority (s)
  12548. "Find priority cookie and return priority."
  12549. (save-match-data
  12550. (if (functionp org-get-priority-function)
  12551. (funcall org-get-priority-function)
  12552. (if (not (string-match org-priority-regexp s))
  12553. (* 1000 (- org-lowest-priority org-default-priority))
  12554. (* 1000 (- org-lowest-priority
  12555. (string-to-char (match-string 2 s))))))))
  12556. ;;;; Tags
  12557. (defvar org-agenda-archives-mode)
  12558. (defvar org-map-continue-from nil
  12559. "Position from where mapping should continue.
  12560. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12561. (defvar org-scanner-tags nil
  12562. "The current tag list while the tags scanner is running.")
  12563. (defvar org-trust-scanner-tags nil
  12564. "Should `org-get-tags-at' use the tags for the scanner.
  12565. This is for internal dynamical scoping only.
  12566. When this is non-nil, the function `org-get-tags-at' will return the value
  12567. of `org-scanner-tags' instead of building the list by itself. This
  12568. can lead to large speed-ups when the tags scanner is used in a file with
  12569. many entries, and when the list of tags is retrieved, for example to
  12570. obtain a list of properties. Building the tags list for each entry in such
  12571. a file becomes an N^2 operation - but with this variable set, it scales
  12572. as N.")
  12573. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12574. "Sca headline tags with inheritance and produce output ACTION.
  12575. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12576. or `agenda' to produce an entry list for an agenda view. It can also be
  12577. a Lisp form or a function that should be called at each matched headline, in
  12578. this case the return value is a list of all return values from these calls.
  12579. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12580. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12581. only lines with a not-done TODO keyword are included in the output.
  12582. This should be the same variable that was scoped into
  12583. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12584. START-LEVEL can be a string with asterisks, reducing the scope to
  12585. headlines matching this string."
  12586. (require 'org-agenda)
  12587. (let* ((re (concat "^"
  12588. (if start-level
  12589. ;; Get the correct level to match
  12590. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12591. org-outline-regexp)
  12592. " *\\(\\<\\("
  12593. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12594. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12595. (props (list 'face 'default
  12596. 'done-face 'org-agenda-done
  12597. 'undone-face 'default
  12598. 'mouse-face 'highlight
  12599. 'org-not-done-regexp org-not-done-regexp
  12600. 'org-todo-regexp org-todo-regexp
  12601. 'org-complex-heading-regexp org-complex-heading-regexp
  12602. 'help-echo
  12603. (format "mouse-2 or RET jump to org file %s"
  12604. (abbreviate-file-name
  12605. (or (buffer-file-name (buffer-base-buffer))
  12606. (buffer-name (buffer-base-buffer)))))))
  12607. (org-map-continue-from nil)
  12608. lspos tags tags-list
  12609. (tags-alist (list (cons 0 org-file-tags)))
  12610. (llast 0) rtn rtn1 level category i txt
  12611. todo marker entry priority)
  12612. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12613. (setq action (list 'lambda nil action)))
  12614. (save-excursion
  12615. (goto-char (point-min))
  12616. (when (eq action 'sparse-tree)
  12617. (org-overview)
  12618. (org-remove-occur-highlights))
  12619. (while (let (case-fold-search)
  12620. (re-search-forward re nil t))
  12621. (setq org-map-continue-from nil)
  12622. (catch :skip
  12623. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12624. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12625. (goto-char (setq lspos (match-beginning 0)))
  12626. (setq level (org-reduced-level (org-outline-level))
  12627. category (org-get-category))
  12628. (setq i llast llast level)
  12629. ;; remove tag lists from same and sublevels
  12630. (while (>= i level)
  12631. (when (setq entry (assoc i tags-alist))
  12632. (setq tags-alist (delete entry tags-alist)))
  12633. (setq i (1- i)))
  12634. ;; add the next tags
  12635. (when tags
  12636. (setq tags (org-split-string tags ":")
  12637. tags-alist
  12638. (cons (cons level tags) tags-alist)))
  12639. ;; compile tags for current headline
  12640. (setq tags-list
  12641. (if org-use-tag-inheritance
  12642. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12643. tags)
  12644. org-scanner-tags tags-list)
  12645. (when org-use-tag-inheritance
  12646. (setcdr (car tags-alist)
  12647. (mapcar (lambda (x)
  12648. (setq x (copy-sequence x))
  12649. (org-add-prop-inherited x))
  12650. (cdar tags-alist))))
  12651. (when (and tags org-use-tag-inheritance
  12652. (or (not (eq t org-use-tag-inheritance))
  12653. org-tags-exclude-from-inheritance))
  12654. ;; selective inheritance, remove uninherited ones
  12655. (setcdr (car tags-alist)
  12656. (org-remove-uninherited-tags (cdar tags-alist))))
  12657. (when (and
  12658. ;; eval matcher only when the todo condition is OK
  12659. (and (or (not todo-only) (member todo org-not-done-keywords))
  12660. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12661. (eval matcher)))
  12662. ;; Call the skipper, but return t if it does not skip,
  12663. ;; so that the `and' form continues evaluating
  12664. (progn
  12665. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12666. t)
  12667. ;; Check if timestamps are deselecting this entry
  12668. (or (not todo-only)
  12669. (and (member todo org-not-done-keywords)
  12670. (or (not org-agenda-tags-todo-honor-ignore-options)
  12671. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12672. ;; select this headline
  12673. (cond
  12674. ((eq action 'sparse-tree)
  12675. (and org-highlight-sparse-tree-matches
  12676. (org-get-heading) (match-end 0)
  12677. (org-highlight-new-match
  12678. (match-beginning 1) (match-end 1)))
  12679. (org-show-context 'tags-tree))
  12680. ((eq action 'agenda)
  12681. (setq txt (org-agenda-format-item
  12682. ""
  12683. (concat
  12684. (if (eq org-tags-match-list-sublevels 'indented)
  12685. (make-string (1- level) ?.) "")
  12686. (org-get-heading))
  12687. level category
  12688. tags-list)
  12689. priority (org-get-priority txt))
  12690. (goto-char lspos)
  12691. (setq marker (org-agenda-new-marker))
  12692. (org-add-props txt props
  12693. 'org-marker marker 'org-hd-marker marker 'org-category category
  12694. 'todo-state todo
  12695. 'priority priority 'type "tagsmatch")
  12696. (push txt rtn))
  12697. ((functionp action)
  12698. (setq org-map-continue-from nil)
  12699. (save-excursion
  12700. (setq rtn1 (funcall action))
  12701. (push rtn1 rtn)))
  12702. (t (user-error "Invalid action")))
  12703. ;; if we are to skip sublevels, jump to end of subtree
  12704. (unless org-tags-match-list-sublevels
  12705. (org-end-of-subtree t)
  12706. (backward-char 1))))
  12707. ;; Get the correct position from where to continue
  12708. (if org-map-continue-from
  12709. (goto-char org-map-continue-from)
  12710. (and (= (point) lspos) (end-of-line 1)))))
  12711. (when (and (eq action 'sparse-tree)
  12712. (not org-sparse-tree-open-archived-trees))
  12713. (org-hide-archived-subtrees (point-min) (point-max)))
  12714. (nreverse rtn)))
  12715. (defun org-remove-uninherited-tags (tags)
  12716. "Remove all tags that are not inherited from the list TAGS."
  12717. (cond
  12718. ((eq org-use-tag-inheritance t)
  12719. (if org-tags-exclude-from-inheritance
  12720. (org-delete-all org-tags-exclude-from-inheritance tags)
  12721. tags))
  12722. ((not org-use-tag-inheritance) nil)
  12723. ((stringp org-use-tag-inheritance)
  12724. (delq nil (mapcar
  12725. (lambda (x)
  12726. (if (and (string-match org-use-tag-inheritance x)
  12727. (not (member x org-tags-exclude-from-inheritance)))
  12728. x nil))
  12729. tags)))
  12730. ((listp org-use-tag-inheritance)
  12731. (delq nil (mapcar
  12732. (lambda (x)
  12733. (if (member x org-use-tag-inheritance) x nil))
  12734. tags)))))
  12735. (defun org-match-sparse-tree (&optional todo-only match)
  12736. "Create a sparse tree according to tags string MATCH.
  12737. MATCH can contain positive and negative selection of tags, like
  12738. \"+WORK+URGENT-WITHBOSS\".
  12739. If optional argument TODO-ONLY is non-nil, only select lines that are
  12740. also TODO lines."
  12741. (interactive "P")
  12742. (org-agenda-prepare-buffers (list (current-buffer)))
  12743. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12744. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12745. (defvar org-cached-props nil)
  12746. (defun org-cached-entry-get (pom property)
  12747. (if (or (eq t org-use-property-inheritance)
  12748. (and (stringp org-use-property-inheritance)
  12749. (string-match org-use-property-inheritance property))
  12750. (and (listp org-use-property-inheritance)
  12751. (member property org-use-property-inheritance)))
  12752. ;; Caching is not possible, check it directly
  12753. (org-entry-get pom property 'inherit)
  12754. ;; Get all properties, so that we can do complicated checks easily
  12755. (cdr (assoc property (or org-cached-props
  12756. (setq org-cached-props
  12757. (org-entry-properties pom)))))))
  12758. (defun org-global-tags-completion-table (&optional files)
  12759. "Return the list of all tags in all agenda buffer/files.
  12760. Optional FILES argument is a list of files which can be used
  12761. instead of the agenda files."
  12762. (save-excursion
  12763. (org-uniquify
  12764. (delq nil
  12765. (apply 'append
  12766. (mapcar
  12767. (lambda (file)
  12768. (set-buffer (find-file-noselect file))
  12769. (append (org-get-buffer-tags)
  12770. (mapcar (lambda (x) (if (stringp (car-safe x))
  12771. (list (car-safe x)) nil))
  12772. org-tag-alist)))
  12773. (if (and files (car files))
  12774. files
  12775. (org-agenda-files))))))))
  12776. (defun org-make-tags-matcher (match)
  12777. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12778. The variable `todo-only' is scoped dynamically into this function.
  12779. It will be set to t if the matcher restricts matching to TODO entries,
  12780. otherwise will not be touched.
  12781. Returns a cons of the selection string MATCH and the constructed
  12782. lisp form implementing the matcher. The matcher is to be evaluated
  12783. at an Org entry, with point on the headline, and returns t if the
  12784. entry matches the selection string MATCH. The returned lisp form
  12785. references two variables with information about the entry, which
  12786. must be bound around the form's evaluation: todo, the TODO keyword
  12787. at the entry (or nil of none); and tags-list, the list of all tags
  12788. at the entry including inherited ones. Additionally, the category
  12789. of the entry (if any) must be specified as the text property
  12790. 'org-category on the headline.
  12791. See also `org-scan-tags'.
  12792. "
  12793. (declare (special todo-only))
  12794. (unless (boundp 'todo-only)
  12795. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12796. (unless match
  12797. ;; Get a new match request, with completion against the global
  12798. ;; tags table and the local tags in current buffer
  12799. (let ((org-last-tags-completion-table
  12800. (org-uniquify
  12801. (delq nil (append (org-get-buffer-tags)
  12802. (org-global-tags-completion-table))))))
  12803. (setq match (org-completing-read-no-i
  12804. "Match: " 'org-tags-completion-function nil nil nil
  12805. 'org-tags-history))))
  12806. ;; Parse the string and create a lisp form
  12807. (let ((match0 match)
  12808. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12809. minus tag mm
  12810. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12811. orterms term orlist re-p str-p level-p level-op time-p
  12812. prop-p pn pv po gv rest (start 0) (ss 0))
  12813. ;; Expand group tags
  12814. (setq match (org-tags-expand match))
  12815. ;; Check if there is a TODO part of this match, which would be the
  12816. ;; part after a "/". TO make sure that this slash is not part of
  12817. ;; a property value to be matched against, we also check that there
  12818. ;; is no " after that slash.
  12819. ;; First, find the last slash
  12820. (while (string-match "/+" match ss)
  12821. (setq start (match-beginning 0) ss (match-end 0)))
  12822. (if (and (string-match "/+" match start)
  12823. (not (save-match-data (string-match "\"" match start))))
  12824. ;; match contains also a todo-matching request
  12825. (progn
  12826. (setq tagsmatch (substring match 0 (match-beginning 0))
  12827. todomatch (substring match (match-end 0)))
  12828. (if (string-match "^!" todomatch)
  12829. (setq todo-only t todomatch (substring todomatch 1)))
  12830. (if (string-match "^\\s-*$" todomatch)
  12831. (setq todomatch nil)))
  12832. ;; only matching tags
  12833. (setq tagsmatch match todomatch nil))
  12834. ;; Make the tags matcher
  12835. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12836. (setq tagsmatcher t)
  12837. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12838. (while (setq term (pop orterms))
  12839. (while (and (equal (substring term -1) "\\") orterms)
  12840. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12841. (while (string-match re term)
  12842. (setq rest (substring term (match-end 0))
  12843. minus (and (match-end 1)
  12844. (equal (match-string 1 term) "-"))
  12845. tag (save-match-data (replace-regexp-in-string
  12846. "\\\\-" "-"
  12847. (match-string 2 term)))
  12848. re-p (equal (string-to-char tag) ?{)
  12849. level-p (match-end 4)
  12850. prop-p (match-end 5)
  12851. mm (cond
  12852. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12853. (level-p
  12854. (setq level-op (org-op-to-function (match-string 3 term)))
  12855. `(,level-op level ,(string-to-number
  12856. (match-string 4 term))))
  12857. (prop-p
  12858. (setq pn (match-string 5 term)
  12859. po (match-string 6 term)
  12860. pv (match-string 7 term)
  12861. re-p (equal (string-to-char pv) ?{)
  12862. str-p (equal (string-to-char pv) ?\")
  12863. time-p (save-match-data
  12864. (string-match "^\"[[<].*[]>]\"$" pv))
  12865. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12866. (if time-p (setq pv (org-matcher-time pv)))
  12867. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12868. (cond
  12869. ((equal pn "CATEGORY")
  12870. (setq gv '(get-text-property (point) 'org-category)))
  12871. ((equal pn "TODO")
  12872. (setq gv 'todo))
  12873. (t
  12874. (setq gv `(org-cached-entry-get nil ,pn))))
  12875. (if re-p
  12876. (if (eq po 'org<>)
  12877. `(not (string-match ,pv (or ,gv "")))
  12878. `(string-match ,pv (or ,gv "")))
  12879. (if str-p
  12880. `(,po (or ,gv "") ,pv)
  12881. `(,po (string-to-number (or ,gv ""))
  12882. ,(string-to-number pv) ))))
  12883. (t `(member ,tag tags-list)))
  12884. mm (if minus (list 'not mm) mm)
  12885. term rest)
  12886. (push mm tagsmatcher))
  12887. (push (if (> (length tagsmatcher) 1)
  12888. (cons 'and tagsmatcher)
  12889. (car tagsmatcher))
  12890. orlist)
  12891. (setq tagsmatcher nil))
  12892. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12893. (setq tagsmatcher
  12894. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12895. ;; Make the todo matcher
  12896. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12897. (setq todomatcher t)
  12898. (setq orterms (org-split-string todomatch "|") orlist nil)
  12899. (while (setq term (pop orterms))
  12900. (while (string-match re term)
  12901. (setq minus (and (match-end 1)
  12902. (equal (match-string 1 term) "-"))
  12903. kwd (match-string 2 term)
  12904. re-p (equal (string-to-char kwd) ?{)
  12905. term (substring term (match-end 0))
  12906. mm (if re-p
  12907. `(string-match ,(substring kwd 1 -1) todo)
  12908. (list 'equal 'todo kwd))
  12909. mm (if minus (list 'not mm) mm))
  12910. (push mm todomatcher))
  12911. (push (if (> (length todomatcher) 1)
  12912. (cons 'and todomatcher)
  12913. (car todomatcher))
  12914. orlist)
  12915. (setq todomatcher nil))
  12916. (setq todomatcher (if (> (length orlist) 1)
  12917. (cons 'or orlist) (car orlist))))
  12918. ;; Return the string and lisp forms of the matcher
  12919. (setq matcher (if todomatcher
  12920. (list 'and tagsmatcher todomatcher)
  12921. tagsmatcher))
  12922. (when todo-only
  12923. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12924. matcher)))
  12925. (cons match0 matcher)))
  12926. (defun org-tags-expand (match &optional single-as-list downcased)
  12927. "Expand group tags in MATCH.
  12928. This replaces every group tag in MATCH with a regexp tag search.
  12929. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12930. will be replaced like this:
  12931. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12932. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12933. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12934. Replacing by a regexp preserves the structure of the match.
  12935. E.g., this expansion
  12936. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12937. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12938. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12939. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12940. assumed to be a single group tag, and the function will return
  12941. the list of tags in this group.
  12942. When DOWNCASE is non-nil, expand downcased TAGS."
  12943. (if org-group-tags
  12944. (let* ((case-fold-search t)
  12945. (stable org-mode-syntax-table)
  12946. (tal (or org-tag-groups-alist-for-agenda
  12947. org-tag-groups-alist))
  12948. (tal (if downcased
  12949. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12950. (tml (mapcar 'car tal))
  12951. (rtnmatch match) rpl)
  12952. ;; @ and _ are allowed as word-components in tags
  12953. (modify-syntax-entry ?@ "w" stable)
  12954. (modify-syntax-entry ?_ "w" stable)
  12955. (while (and tml
  12956. (with-syntax-table stable
  12957. (string-match
  12958. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12959. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12960. (let* ((dir (match-string 1 rtnmatch))
  12961. (tag (match-string 2 rtnmatch))
  12962. (tag (if downcased (downcase tag) tag)))
  12963. (setq tml (delete tag tml))
  12964. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12965. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12966. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12967. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12968. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12969. (if single-as-list
  12970. (or (reverse rpl) (list rtnmatch))
  12971. rtnmatch))
  12972. (if single-as-list (list (if downcased (downcase match) match))
  12973. match)))
  12974. (defun org-op-to-function (op &optional stringp)
  12975. "Turn an operator into the appropriate function."
  12976. (setq op
  12977. (cond
  12978. ((equal op "<" ) '(< string< org-time<))
  12979. ((equal op ">" ) '(> org-string> org-time>))
  12980. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12981. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12982. ((member op '("=" "==")) '(= string= org-time=))
  12983. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12984. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12985. (defun org<> (a b) (not (= a b)))
  12986. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12987. (defun org-string>= (a b) (not (string< a b)))
  12988. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12989. (defun org-string<> (a b) (not (string= a b)))
  12990. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12991. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12992. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12993. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12994. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12995. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12996. (defun org-2ft (s)
  12997. "Convert S to a floating point time.
  12998. If S is already a number, just return it. If it is a string, parse
  12999. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13000. (cond
  13001. ((numberp s) s)
  13002. ((stringp s)
  13003. (condition-case nil
  13004. (float-time (apply 'encode-time (org-parse-time-string s)))
  13005. (error 0.)))
  13006. (t 0.)))
  13007. (defun org-time-today ()
  13008. "Time in seconds today at 0:00.
  13009. Returns the float number of seconds since the beginning of the
  13010. epoch to the beginning of today (00:00)."
  13011. (float-time (apply 'encode-time
  13012. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13013. (defun org-matcher-time (s)
  13014. "Interpret a time comparison value."
  13015. (save-match-data
  13016. (cond
  13017. ((string= s "<now>") (float-time))
  13018. ((string= s "<today>") (org-time-today))
  13019. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13020. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13021. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13022. (+ (org-time-today)
  13023. (* (string-to-number (match-string 1 s))
  13024. (cdr (assoc (match-string 2 s)
  13025. '(("d" . 86400.0) ("w" . 604800.0)
  13026. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13027. (t (org-2ft s)))))
  13028. (defun org-match-any-p (re list)
  13029. "Does re match any element of list?"
  13030. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13031. (delq nil list))
  13032. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13033. (defvar org-tags-overlay (make-overlay 1 1))
  13034. (org-detach-overlay org-tags-overlay)
  13035. (defun org-get-local-tags-at (&optional pos)
  13036. "Get a list of tags defined in the current headline."
  13037. (org-get-tags-at pos 'local))
  13038. (defun org-get-local-tags ()
  13039. "Get a list of tags defined in the current headline."
  13040. (org-get-tags-at nil 'local))
  13041. (defun org-get-tags-at (&optional pos local)
  13042. "Get a list of all headline tags applicable at POS.
  13043. POS defaults to point. If tags are inherited, the list contains
  13044. the targets in the same sequence as the headlines appear, i.e.
  13045. the tags of the current headline come last.
  13046. When LOCAL is non-nil, only return tags from the current headline,
  13047. ignore inherited ones."
  13048. (interactive)
  13049. (if (and org-trust-scanner-tags
  13050. (or (not pos) (equal pos (point)))
  13051. (not local))
  13052. org-scanner-tags
  13053. (let (tags ltags lastpos parent)
  13054. (save-excursion
  13055. (save-restriction
  13056. (widen)
  13057. (goto-char (or pos (point)))
  13058. (save-match-data
  13059. (catch 'done
  13060. (condition-case nil
  13061. (progn
  13062. (org-back-to-heading t)
  13063. (while (not (equal lastpos (point)))
  13064. (setq lastpos (point))
  13065. (when (looking-at
  13066. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13067. (setq ltags (org-split-string
  13068. (org-match-string-no-properties 1) ":"))
  13069. (when parent
  13070. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13071. (setq tags (append
  13072. (if parent
  13073. (org-remove-uninherited-tags ltags)
  13074. ltags)
  13075. tags)))
  13076. (or org-use-tag-inheritance (throw 'done t))
  13077. (if local (throw 'done t))
  13078. (or (org-up-heading-safe) (error nil))
  13079. (setq parent t)))
  13080. (error nil)))))
  13081. (if local
  13082. tags
  13083. (reverse (delete-dups
  13084. (reverse (append
  13085. (org-remove-uninherited-tags
  13086. org-file-tags) tags)))))))))
  13087. (defun org-add-prop-inherited (s)
  13088. (add-text-properties 0 (length s) '(inherited t) s)
  13089. s)
  13090. (defun org-toggle-tag (tag &optional onoff)
  13091. "Toggle the tag TAG for the current line.
  13092. If ONOFF is `on' or `off', don't toggle but set to this state."
  13093. (let (res current)
  13094. (save-excursion
  13095. (org-back-to-heading t)
  13096. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13097. (point-at-eol) t)
  13098. (progn
  13099. (setq current (match-string 1))
  13100. (replace-match ""))
  13101. (setq current ""))
  13102. (setq current (nreverse (org-split-string current ":")))
  13103. (cond
  13104. ((eq onoff 'on)
  13105. (setq res t)
  13106. (or (member tag current) (push tag current)))
  13107. ((eq onoff 'off)
  13108. (or (not (member tag current)) (setq current (delete tag current))))
  13109. (t (if (member tag current)
  13110. (setq current (delete tag current))
  13111. (setq res t)
  13112. (push tag current))))
  13113. (end-of-line 1)
  13114. (if current
  13115. (progn
  13116. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13117. (org-set-tags nil t))
  13118. (delete-horizontal-space))
  13119. (run-hooks 'org-after-tags-change-hook))
  13120. res))
  13121. (defun org-align-tags-here (to-col)
  13122. ;; Assumes that this is a headline
  13123. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13124. (beginning-of-line 1)
  13125. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13126. (< pos (match-beginning 2)))
  13127. (progn
  13128. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13129. (goto-char (match-beginning 1))
  13130. (insert " ")
  13131. (delete-region (point) (1+ (match-beginning 2)))
  13132. (setq ncol (max (current-column)
  13133. (1+ col)
  13134. (if (> to-col 0)
  13135. to-col
  13136. (- (abs to-col) tags-l))))
  13137. (setq p (point))
  13138. (insert (make-string (- ncol (current-column)) ?\ ))
  13139. (setq ncol (current-column))
  13140. (when indent-tabs-mode (tabify p (point-at-eol)))
  13141. (org-move-to-column (min ncol col) t))
  13142. (goto-char pos))))
  13143. (defun org-set-tags-command (&optional arg just-align)
  13144. "Call the set-tags command for the current entry."
  13145. (interactive "P")
  13146. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13147. (org-set-tags arg just-align)
  13148. (save-excursion
  13149. (unless (and (org-region-active-p)
  13150. org-loop-over-headlines-in-active-region)
  13151. (org-back-to-heading t))
  13152. (org-set-tags arg just-align))))
  13153. (defun org-set-tags-to (data)
  13154. "Set the tags of the current entry to DATA, replacing the current tags.
  13155. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13156. If DATA is nil or the empty string, any tags will be removed."
  13157. (interactive "sTags: ")
  13158. (setq data
  13159. (cond
  13160. ((eq data nil) "")
  13161. ((equal data "") "")
  13162. ((stringp data)
  13163. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13164. ":"))
  13165. ((listp data)
  13166. (concat ":" (mapconcat 'identity data ":") ":"))))
  13167. (when data
  13168. (save-excursion
  13169. (org-back-to-heading t)
  13170. (when (looking-at org-complex-heading-regexp)
  13171. (if (match-end 5)
  13172. (progn
  13173. (goto-char (match-beginning 5))
  13174. (insert data)
  13175. (delete-region (point) (point-at-eol))
  13176. (org-set-tags nil 'align))
  13177. (goto-char (point-at-eol))
  13178. (insert " " data)
  13179. (org-set-tags nil 'align)))
  13180. (beginning-of-line 1)
  13181. (if (looking-at ".*?\\([ \t]+\\)$")
  13182. (delete-region (match-beginning 1) (match-end 1))))))
  13183. (defun org-align-all-tags ()
  13184. "Align the tags i all headings."
  13185. (interactive)
  13186. (save-excursion
  13187. (or (ignore-errors (org-back-to-heading t))
  13188. (outline-next-heading))
  13189. (if (org-at-heading-p)
  13190. (org-set-tags t)
  13191. (message "No headings"))))
  13192. (defvar org-indent-indentation-per-level)
  13193. (defun org-set-tags (&optional arg just-align)
  13194. "Set the tags for the current headline.
  13195. With prefix ARG, realign all tags in headings in the current buffer."
  13196. (interactive "P")
  13197. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13198. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13199. 'region-start-level 'region))
  13200. org-loop-over-headlines-in-active-region)
  13201. (org-map-entries
  13202. ;; We don't use ARG and JUST-ALIGN here these args are not
  13203. ;; useful when looping over headlines
  13204. `(org-set-tags)
  13205. org-loop-over-headlines-in-active-region
  13206. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13207. (let* ((re org-outline-regexp-bol)
  13208. (current (unless arg (org-get-tags-string)))
  13209. (col (current-column))
  13210. (org-setting-tags t)
  13211. table current-tags inherited-tags ; computed below when needed
  13212. tags p0 c0 c1 rpl di tc level)
  13213. (if arg
  13214. (save-excursion
  13215. (goto-char (point-min))
  13216. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13217. (while (re-search-forward re nil t)
  13218. (org-set-tags nil t)
  13219. (end-of-line 1)))
  13220. (message "All tags realigned to column %d" org-tags-column))
  13221. (if just-align
  13222. (setq tags current)
  13223. ;; Get a new set of tags from the user
  13224. (save-excursion
  13225. (setq table (append org-tag-persistent-alist
  13226. (or org-tag-alist (org-get-buffer-tags))
  13227. (and
  13228. org-complete-tags-always-offer-all-agenda-tags
  13229. (org-global-tags-completion-table
  13230. (org-agenda-files))))
  13231. org-last-tags-completion-table table
  13232. current-tags (org-split-string current ":")
  13233. inherited-tags (nreverse
  13234. (nthcdr (length current-tags)
  13235. (nreverse (org-get-tags-at))))
  13236. tags
  13237. (if (or (eq t org-use-fast-tag-selection)
  13238. (and org-use-fast-tag-selection
  13239. (delq nil (mapcar 'cdr table))))
  13240. (org-fast-tag-selection
  13241. current-tags inherited-tags table
  13242. (if org-fast-tag-selection-include-todo
  13243. org-todo-key-alist))
  13244. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13245. (org-trim
  13246. (org-icompleting-read "Tags: "
  13247. 'org-tags-completion-function
  13248. nil nil current 'org-tags-history))))))
  13249. (while (string-match "[-+&]+" tags)
  13250. ;; No boolean logic, just a list
  13251. (setq tags (replace-match ":" t t tags))))
  13252. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13253. (if org-tags-sort-function
  13254. (setq tags (mapconcat 'identity
  13255. (sort (org-split-string
  13256. tags (org-re "[^[:alnum:]_@#%]+"))
  13257. org-tags-sort-function) ":")))
  13258. (if (string-match "\\`[\t ]*\\'" tags)
  13259. (setq tags "")
  13260. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13261. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13262. ;; Insert new tags at the correct column
  13263. (beginning-of-line 1)
  13264. (setq level (or (and (looking-at org-outline-regexp)
  13265. (- (match-end 0) (point) 1))
  13266. 1))
  13267. (cond
  13268. ((and (equal current "") (equal tags "")))
  13269. ((re-search-forward
  13270. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13271. (point-at-eol) t)
  13272. (if (equal tags "")
  13273. (setq rpl "")
  13274. (goto-char (match-beginning 0))
  13275. (setq c0 (current-column)
  13276. ;; compute offset for the case of org-indent-mode active
  13277. di (if org-indent-mode
  13278. (* (1- org-indent-indentation-per-level) (1- level))
  13279. 0)
  13280. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13281. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13282. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13283. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13284. (replace-match rpl t t)
  13285. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13286. tags)
  13287. (t (error "Tags alignment failed")))
  13288. (org-move-to-column col)
  13289. (unless just-align
  13290. (run-hooks 'org-after-tags-change-hook))))))
  13291. (defun org-change-tag-in-region (beg end tag off)
  13292. "Add or remove TAG for each entry in the region.
  13293. This works in the agenda, and also in an org-mode buffer."
  13294. (interactive
  13295. (list (region-beginning) (region-end)
  13296. (let ((org-last-tags-completion-table
  13297. (if (derived-mode-p 'org-mode)
  13298. (org-uniquify
  13299. (delq nil (append (org-get-buffer-tags)
  13300. (org-global-tags-completion-table))))
  13301. (org-global-tags-completion-table))))
  13302. (org-icompleting-read
  13303. "Tag: " 'org-tags-completion-function nil nil nil
  13304. 'org-tags-history))
  13305. (progn
  13306. (message "[s]et or [r]emove? ")
  13307. (equal (read-char-exclusive) ?r))))
  13308. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13309. (let ((agendap (equal major-mode 'org-agenda-mode))
  13310. l1 l2 m buf pos newhead (cnt 0))
  13311. (goto-char end)
  13312. (setq l2 (1- (org-current-line)))
  13313. (goto-char beg)
  13314. (setq l1 (org-current-line))
  13315. (loop for l from l1 to l2 do
  13316. (org-goto-line l)
  13317. (setq m (get-text-property (point) 'org-hd-marker))
  13318. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13319. (and agendap m))
  13320. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13321. pos (if agendap m (point)))
  13322. (with-current-buffer buf
  13323. (save-excursion
  13324. (save-restriction
  13325. (goto-char pos)
  13326. (setq cnt (1+ cnt))
  13327. (org-toggle-tag tag (if off 'off 'on))
  13328. (setq newhead (org-get-heading)))))
  13329. (and agendap (org-agenda-change-all-lines newhead m))))
  13330. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13331. (defun org-tags-completion-function (string predicate &optional flag)
  13332. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13333. (confirm (lambda (x) (stringp (car x)))))
  13334. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13335. (setq s1 (match-string 1 string)
  13336. s2 (match-string 2 string))
  13337. (setq s1 "" s2 string))
  13338. (cond
  13339. ((eq flag nil)
  13340. ;; try completion
  13341. (setq rtn (try-completion s2 ctable confirm))
  13342. (if (stringp rtn)
  13343. (setq rtn
  13344. (concat s1 s2 (substring rtn (length s2))
  13345. (if (and org-add-colon-after-tag-completion
  13346. (assoc rtn ctable))
  13347. ":" ""))))
  13348. rtn)
  13349. ((eq flag t)
  13350. ;; all-completions
  13351. (all-completions s2 ctable confirm))
  13352. ((eq flag 'lambda)
  13353. ;; exact match?
  13354. (assoc s2 ctable)))))
  13355. (defun org-fast-tag-insert (kwd tags face &optional end)
  13356. "Insert KDW, and the TAGS, the latter with face FACE.
  13357. Also insert END."
  13358. (insert (format "%-12s" (concat kwd ":"))
  13359. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13360. (or end "")))
  13361. (defun org-fast-tag-show-exit (flag)
  13362. (save-excursion
  13363. (org-goto-line 3)
  13364. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13365. (replace-match ""))
  13366. (when flag
  13367. (end-of-line 1)
  13368. (org-move-to-column (- (window-width) 19) t)
  13369. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13370. (defun org-set-current-tags-overlay (current prefix)
  13371. "Add an overlay to CURRENT tag with PREFIX."
  13372. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13373. (if (featurep 'xemacs)
  13374. (org-overlay-display org-tags-overlay (concat prefix s)
  13375. 'secondary-selection)
  13376. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13377. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13378. (defvar org-last-tag-selection-key nil)
  13379. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13380. "Fast tag selection with single keys.
  13381. CURRENT is the current list of tags in the headline, INHERITED is the
  13382. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13383. possibly with grouping information. TODO-TABLE is a similar table with
  13384. TODO keywords, should these have keys assigned to them.
  13385. If the keys are nil, a-z are automatically assigned.
  13386. Returns the new tags string, or nil to not change the current settings."
  13387. (let* ((fulltable (append table todo-table))
  13388. (maxlen (apply 'max (mapcar
  13389. (lambda (x)
  13390. (if (stringp (car x)) (string-width (car x)) 0))
  13391. fulltable)))
  13392. (buf (current-buffer))
  13393. (expert (eq org-fast-tag-selection-single-key 'expert))
  13394. (buffer-tags nil)
  13395. (fwidth (+ maxlen 3 1 3))
  13396. (ncol (/ (- (window-width) 4) fwidth))
  13397. (i-face 'org-done)
  13398. (c-face 'org-todo)
  13399. tg cnt e c char c1 c2 ntable tbl rtn
  13400. ov-start ov-end ov-prefix
  13401. (exit-after-next org-fast-tag-selection-single-key)
  13402. (done-keywords org-done-keywords)
  13403. groups ingroup)
  13404. (save-excursion
  13405. (beginning-of-line 1)
  13406. (if (looking-at
  13407. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13408. (setq ov-start (match-beginning 1)
  13409. ov-end (match-end 1)
  13410. ov-prefix "")
  13411. (setq ov-start (1- (point-at-eol))
  13412. ov-end (1+ ov-start))
  13413. (skip-chars-forward "^\n\r")
  13414. (setq ov-prefix
  13415. (concat
  13416. (buffer-substring (1- (point)) (point))
  13417. (if (> (current-column) org-tags-column)
  13418. " "
  13419. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13420. (move-overlay org-tags-overlay ov-start ov-end)
  13421. (save-window-excursion
  13422. (if expert
  13423. (set-buffer (get-buffer-create " *Org tags*"))
  13424. (delete-other-windows)
  13425. (split-window-vertically)
  13426. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13427. (erase-buffer)
  13428. (org-set-local 'org-done-keywords done-keywords)
  13429. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13430. (org-fast-tag-insert "Current" current c-face "\n\n")
  13431. (org-fast-tag-show-exit exit-after-next)
  13432. (org-set-current-tags-overlay current ov-prefix)
  13433. (setq tbl fulltable char ?a cnt 0)
  13434. (while (setq e (pop tbl))
  13435. (cond
  13436. ((equal (car e) :startgroup)
  13437. (push '() groups) (setq ingroup t)
  13438. (when (not (= cnt 0))
  13439. (setq cnt 0)
  13440. (insert "\n"))
  13441. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13442. ((equal (car e) :endgroup)
  13443. (setq ingroup nil cnt 0)
  13444. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13445. ((equal e '(:newline))
  13446. (when (not (= cnt 0))
  13447. (setq cnt 0)
  13448. (insert "\n")
  13449. (setq e (car tbl))
  13450. (while (equal (car tbl) '(:newline))
  13451. (insert "\n")
  13452. (setq tbl (cdr tbl)))))
  13453. ((equal e '(:grouptags)) nil)
  13454. (t
  13455. (setq tg (copy-sequence (car e)) c2 nil)
  13456. (if (cdr e)
  13457. (setq c (cdr e))
  13458. ;; automatically assign a character.
  13459. (setq c1 (string-to-char
  13460. (downcase (substring
  13461. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13462. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13463. (while (or (rassoc char ntable) (rassoc char table))
  13464. (setq char (1+ char)))
  13465. (setq c2 c1))
  13466. (setq c (or c2 char)))
  13467. (if ingroup (push tg (car groups)))
  13468. (setq tg (org-add-props tg nil 'face
  13469. (cond
  13470. ((not (assoc tg table))
  13471. (org-get-todo-face tg))
  13472. ((member tg current) c-face)
  13473. ((member tg inherited) i-face))))
  13474. (if (equal (caar tbl) :grouptags)
  13475. (org-add-props tg nil 'face 'org-tag-group))
  13476. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13477. (insert "[" c "] " tg (make-string
  13478. (- fwidth 4 (length tg)) ?\ ))
  13479. (push (cons tg c) ntable)
  13480. (when (= (setq cnt (1+ cnt)) ncol)
  13481. (insert "\n")
  13482. (if ingroup (insert " "))
  13483. (setq cnt 0)))))
  13484. (setq ntable (nreverse ntable))
  13485. (insert "\n")
  13486. (goto-char (point-min))
  13487. (if (not expert) (org-fit-window-to-buffer))
  13488. (setq rtn
  13489. (catch 'exit
  13490. (while t
  13491. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13492. (if (not groups) "no " "")
  13493. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13494. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13495. (setq org-last-tag-selection-key c)
  13496. (cond
  13497. ((= c ?\r) (throw 'exit t))
  13498. ((= c ?!)
  13499. (setq groups (not groups))
  13500. (goto-char (point-min))
  13501. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13502. ((= c ?\C-c)
  13503. (if (not expert)
  13504. (org-fast-tag-show-exit
  13505. (setq exit-after-next (not exit-after-next)))
  13506. (setq expert nil)
  13507. (delete-other-windows)
  13508. (set-window-buffer (split-window-vertically) " *Org tags*")
  13509. (org-switch-to-buffer-other-window " *Org tags*")
  13510. (org-fit-window-to-buffer)))
  13511. ((or (= c ?\C-g)
  13512. (and (= c ?q) (not (rassoc c ntable))))
  13513. (org-detach-overlay org-tags-overlay)
  13514. (setq quit-flag t))
  13515. ((= c ?\ )
  13516. (setq current nil)
  13517. (if exit-after-next (setq exit-after-next 'now)))
  13518. ((= c ?\t)
  13519. (condition-case nil
  13520. (setq tg (org-icompleting-read
  13521. "Tag: "
  13522. (or buffer-tags
  13523. (with-current-buffer buf
  13524. (org-get-buffer-tags)))))
  13525. (quit (setq tg "")))
  13526. (when (string-match "\\S-" tg)
  13527. (add-to-list 'buffer-tags (list tg))
  13528. (if (member tg current)
  13529. (setq current (delete tg current))
  13530. (push tg current)))
  13531. (if exit-after-next (setq exit-after-next 'now)))
  13532. ((setq e (rassoc c todo-table) tg (car e))
  13533. (with-current-buffer buf
  13534. (save-excursion (org-todo tg)))
  13535. (if exit-after-next (setq exit-after-next 'now)))
  13536. ((setq e (rassoc c ntable) tg (car e))
  13537. (if (member tg current)
  13538. (setq current (delete tg current))
  13539. (loop for g in groups do
  13540. (if (member tg g)
  13541. (mapc (lambda (x)
  13542. (setq current (delete x current)))
  13543. g)))
  13544. (push tg current))
  13545. (if exit-after-next (setq exit-after-next 'now))))
  13546. ;; Create a sorted list
  13547. (setq current
  13548. (sort current
  13549. (lambda (a b)
  13550. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13551. (if (eq exit-after-next 'now) (throw 'exit t))
  13552. (goto-char (point-min))
  13553. (beginning-of-line 2)
  13554. (delete-region (point) (point-at-eol))
  13555. (org-fast-tag-insert "Current" current c-face)
  13556. (org-set-current-tags-overlay current ov-prefix)
  13557. (while (re-search-forward
  13558. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13559. (setq tg (match-string 1))
  13560. (add-text-properties
  13561. (match-beginning 1) (match-end 1)
  13562. (list 'face
  13563. (cond
  13564. ((member tg current) c-face)
  13565. ((member tg inherited) i-face)
  13566. (t (get-text-property (match-beginning 1) 'face))))))
  13567. (goto-char (point-min)))))
  13568. (org-detach-overlay org-tags-overlay)
  13569. (if rtn
  13570. (mapconcat 'identity current ":")
  13571. nil))))
  13572. (defun org-get-tags-string ()
  13573. "Get the TAGS string in the current headline."
  13574. (unless (org-at-heading-p t)
  13575. (user-error "Not on a heading"))
  13576. (save-excursion
  13577. (beginning-of-line 1)
  13578. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13579. (org-match-string-no-properties 1)
  13580. "")))
  13581. (defun org-get-tags ()
  13582. "Get the list of tags specified in the current headline."
  13583. (org-split-string (org-get-tags-string) ":"))
  13584. (defun org-get-buffer-tags ()
  13585. "Get a table of all tags used in the buffer, for completion."
  13586. (let (tags)
  13587. (save-excursion
  13588. (goto-char (point-min))
  13589. (while (re-search-forward
  13590. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13591. (when (equal (char-after (point-at-bol 0)) ?*)
  13592. (mapc (lambda (x) (add-to-list 'tags x))
  13593. (org-split-string (org-match-string-no-properties 1) ":")))))
  13594. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13595. (mapcar 'list tags)))
  13596. ;;;; The mapping API
  13597. (defun org-map-entries (func &optional match scope &rest skip)
  13598. "Call FUNC at each headline selected by MATCH in SCOPE.
  13599. FUNC is a function or a lisp form. The function will be called without
  13600. arguments, with the cursor positioned at the beginning of the headline.
  13601. The return values of all calls to the function will be collected and
  13602. returned as a list.
  13603. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13604. does not need to preserve point. After evaluation, the cursor will be
  13605. moved to the end of the line (presumably of the headline of the
  13606. processed entry) and search continues from there. Under some
  13607. circumstances, this may not produce the wanted results. For example,
  13608. if you have removed (e.g. archived) the current (sub)tree it could
  13609. mean that the next entry will be skipped entirely. In such cases, you
  13610. can specify the position from where search should continue by making
  13611. FUNC set the variable `org-map-continue-from' to the desired buffer
  13612. position.
  13613. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13614. Only headlines that are matched by this query will be considered during
  13615. the iteration. When MATCH is nil or t, all headlines will be
  13616. visited by the iteration.
  13617. SCOPE determines the scope of this command. It can be any of:
  13618. nil The current buffer, respecting the restriction if any
  13619. tree The subtree started with the entry at point
  13620. region The entries within the active region, if any
  13621. region-start-level
  13622. The entries within the active region, but only those at
  13623. the same level than the first one.
  13624. file The current buffer, without restriction
  13625. file-with-archives
  13626. The current buffer, and any archives associated with it
  13627. agenda All agenda files
  13628. agenda-with-archives
  13629. All agenda files with any archive files associated with them
  13630. \(file1 file2 ...)
  13631. If this is a list, all files in the list will be scanned
  13632. The remaining args are treated as settings for the skipping facilities of
  13633. the scanner. The following items can be given here:
  13634. archive skip trees with the archive tag
  13635. comment skip trees with the COMMENT keyword
  13636. function or Emacs Lisp form:
  13637. will be used as value for `org-agenda-skip-function', so
  13638. whenever the function returns a position, FUNC will not be
  13639. called for that entry and search will continue from the
  13640. position returned
  13641. If your function needs to retrieve the tags including inherited tags
  13642. at the *current* entry, you can use the value of the variable
  13643. `org-scanner-tags' which will be much faster than getting the value
  13644. with `org-get-tags-at'. If your function gets properties with
  13645. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13646. to t around the call to `org-entry-properties' to get the same speedup.
  13647. Note that if your function moves around to retrieve tags and properties at
  13648. a *different* entry, you cannot use these techniques."
  13649. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13650. (not (org-region-active-p)))
  13651. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13652. (org-agenda-skip-archived-trees (memq 'archive skip))
  13653. (org-agenda-skip-comment-trees (memq 'comment skip))
  13654. (org-agenda-skip-function
  13655. (car (org-delete-all '(comment archive) skip)))
  13656. (org-tags-match-list-sublevels t)
  13657. (start-level (eq scope 'region-start-level))
  13658. matcher file res
  13659. org-todo-keywords-for-agenda
  13660. org-done-keywords-for-agenda
  13661. org-todo-keyword-alist-for-agenda
  13662. org-tag-alist-for-agenda
  13663. todo-only)
  13664. (cond
  13665. ((eq match t) (setq matcher t))
  13666. ((eq match nil) (setq matcher t))
  13667. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13668. (save-window-excursion
  13669. (save-restriction
  13670. (cond ((eq scope 'tree)
  13671. (org-back-to-heading t)
  13672. (org-narrow-to-subtree)
  13673. (setq scope nil))
  13674. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13675. (org-region-active-p))
  13676. ;; If needed, set start-level to a string like "2"
  13677. (when start-level
  13678. (save-excursion
  13679. (goto-char (region-beginning))
  13680. (unless (org-at-heading-p) (outline-next-heading))
  13681. (setq start-level (org-current-level))))
  13682. (narrow-to-region (region-beginning)
  13683. (save-excursion
  13684. (goto-char (region-end))
  13685. (unless (and (bolp) (org-at-heading-p))
  13686. (outline-next-heading))
  13687. (point)))
  13688. (setq scope nil)))
  13689. (if (not scope)
  13690. (progn
  13691. (org-agenda-prepare-buffers
  13692. (list (buffer-file-name (current-buffer))))
  13693. (setq res (org-scan-tags func matcher todo-only start-level)))
  13694. ;; Get the right scope
  13695. (cond
  13696. ((and scope (listp scope) (symbolp (car scope)))
  13697. (setq scope (eval scope)))
  13698. ((eq scope 'agenda)
  13699. (setq scope (org-agenda-files t)))
  13700. ((eq scope 'agenda-with-archives)
  13701. (setq scope (org-agenda-files t))
  13702. (setq scope (org-add-archive-files scope)))
  13703. ((eq scope 'file)
  13704. (setq scope (list (buffer-file-name))))
  13705. ((eq scope 'file-with-archives)
  13706. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13707. (org-agenda-prepare-buffers scope)
  13708. (while (setq file (pop scope))
  13709. (with-current-buffer (org-find-base-buffer-visiting file)
  13710. (save-excursion
  13711. (save-restriction
  13712. (widen)
  13713. (goto-char (point-min))
  13714. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13715. res)))
  13716. ;;;; Properties
  13717. ;;; Setting and retrieving properties
  13718. (defconst org-special-properties
  13719. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13720. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13721. "The special properties valid in Org-mode.
  13722. These are properties that are not defined in the property drawer,
  13723. but in some other way.")
  13724. (defconst org-default-properties
  13725. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13726. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13727. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13728. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13729. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13730. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13731. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13732. "Some properties that are used by Org-mode for various purposes.
  13733. Being in this list makes sure that they are offered for completion.")
  13734. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13735. "Regular expression matching the first line of a property drawer.")
  13736. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13737. "Regular expression matching the last line of a property drawer.")
  13738. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13739. "Regular expression matching the first line of a property drawer.")
  13740. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13741. "Regular expression matching the first line of a property drawer.")
  13742. (defconst org-property-drawer-re
  13743. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  13744. org-property-end-re "\\)\n?")
  13745. "Matches an entire property drawer.")
  13746. (defconst org-clock-drawer-re
  13747. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  13748. org-property-end-re "\\)\n?")
  13749. "Matches an entire clock drawer.")
  13750. (defun org-property-action ()
  13751. "Do an action on properties."
  13752. (interactive)
  13753. (let (c)
  13754. (org-at-property-p)
  13755. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13756. (setq c (read-char-exclusive))
  13757. (cond
  13758. ((equal c ?s)
  13759. (call-interactively 'org-set-property))
  13760. ((equal c ?d)
  13761. (call-interactively 'org-delete-property))
  13762. ((equal c ?D)
  13763. (call-interactively 'org-delete-property-globally))
  13764. ((equal c ?c)
  13765. (call-interactively 'org-compute-property-at-point))
  13766. (t (user-error "No such property action %c" c)))))
  13767. (defun org-inc-effort ()
  13768. "Increment the value of the effort property in the current entry."
  13769. (interactive)
  13770. (org-set-effort nil t))
  13771. (defvar org-clock-effort) ;; Defined in org-clock.el
  13772. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13773. (defun org-set-effort (&optional value increment)
  13774. "Set the effort property of the current entry.
  13775. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13776. 10th allowed value.
  13777. When INCREMENT is non-nil, set the property to the next allowed value."
  13778. (interactive "P")
  13779. (if (equal value 0) (setq value 10))
  13780. (let* ((completion-ignore-case t)
  13781. (prop org-effort-property)
  13782. (cur (org-entry-get nil prop))
  13783. (allowed (org-property-get-allowed-values nil prop 'table))
  13784. (existing (mapcar 'list (org-property-values prop)))
  13785. (heading (nth 4 (org-heading-components)))
  13786. rpl
  13787. (val (cond
  13788. ((stringp value) value)
  13789. ((and allowed (integerp value))
  13790. (or (car (nth (1- value) allowed))
  13791. (car (org-last allowed))))
  13792. ((and allowed increment)
  13793. (or (caadr (member (list cur) allowed))
  13794. (user-error "Allowed effort values are not set")))
  13795. (allowed
  13796. (message "Select 1-9,0, [RET%s]: %s"
  13797. (if cur (concat "=" cur) "")
  13798. (mapconcat 'car allowed " "))
  13799. (setq rpl (read-char-exclusive))
  13800. (if (equal rpl ?\r)
  13801. cur
  13802. (setq rpl (- rpl ?0))
  13803. (if (equal rpl 0) (setq rpl 10))
  13804. (if (and (> rpl 0) (<= rpl (length allowed)))
  13805. (car (nth (1- rpl) allowed))
  13806. (org-completing-read "Effort: " allowed nil))))
  13807. (t
  13808. (let (org-completion-use-ido org-completion-use-iswitchb)
  13809. (org-completing-read
  13810. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13811. (concat "[" cur "]") "")
  13812. ": ")
  13813. existing nil nil "" nil cur))))))
  13814. (unless (equal (org-entry-get nil prop) val)
  13815. (org-entry-put nil prop val))
  13816. (save-excursion
  13817. (org-back-to-heading t)
  13818. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13819. (when (string= heading org-clock-current-task)
  13820. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13821. (org-clock-update-mode-line))
  13822. (message "%s is now %s" prop val)))
  13823. (defun org-at-property-p ()
  13824. "Is cursor inside a property drawer?"
  13825. (save-excursion
  13826. (when (equal 'node-property (car (org-element-at-point)))
  13827. (beginning-of-line 1)
  13828. (looking-at org-property-re))))
  13829. (defun org-get-property-block (&optional beg end force)
  13830. "Return the (beg . end) range of the body of the property drawer.
  13831. BEG and END are the beginning and end of the current subtree, or of
  13832. the part before the first headline. If they are not given, they will
  13833. be found. If the drawer does not exist and FORCE is non-nil, create
  13834. the drawer."
  13835. (catch 'exit
  13836. (save-excursion
  13837. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13838. (progn (org-back-to-heading t) (point))))
  13839. (end (or end (and (not (outline-next-heading)) (point-max))
  13840. (point))))
  13841. (goto-char beg)
  13842. (if (re-search-forward org-property-start-re end t)
  13843. (setq beg (1+ (match-end 0)))
  13844. (if force
  13845. (save-excursion
  13846. (org-insert-property-drawer)
  13847. (setq end (progn (outline-next-heading) (point))))
  13848. (throw 'exit nil))
  13849. (goto-char beg)
  13850. (if (re-search-forward org-property-start-re end t)
  13851. (setq beg (1+ (match-end 0)))))
  13852. (if (re-search-forward org-property-end-re end t)
  13853. (setq end (match-beginning 0))
  13854. (or force (throw 'exit nil))
  13855. (goto-char beg)
  13856. (setq end beg)
  13857. (org-indent-line)
  13858. (insert ":END:\n"))
  13859. (cons beg end)))))
  13860. (defun org-entry-properties (&optional pom which specific)
  13861. "Get all properties of the entry at point-or-marker POM.
  13862. This includes the TODO keyword, the tags, time strings for deadline,
  13863. scheduled, and clocking, and any additional properties defined in the
  13864. entry. The return value is an alist, keys may occur multiple times
  13865. if the property key was used several times.
  13866. POM may also be nil, in which case the current entry is used.
  13867. If WHICH is nil or `all', get all properties. If WHICH is
  13868. `special' or `standard', only get that subclass. If WHICH
  13869. is a string only get exactly this property. SPECIFIC can be a string, the
  13870. specific property we are interested in. Specifying it can speed
  13871. things up because then unnecessary parsing is avoided."
  13872. (setq which (or which 'all))
  13873. (org-with-point-at pom
  13874. (let ((clockstr (substring org-clock-string 0 -1))
  13875. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13876. (case-fold-search nil)
  13877. beg end range props sum-props key key1 value string clocksum clocksumt)
  13878. (save-excursion
  13879. (when (condition-case nil
  13880. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13881. (error nil))
  13882. (setq beg (point))
  13883. (setq sum-props (get-text-property (point) 'org-summaries))
  13884. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13885. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13886. (outline-next-heading)
  13887. (setq end (point))
  13888. (when (memq which '(all special))
  13889. ;; Get the special properties, like TODO and tags
  13890. (goto-char beg)
  13891. (when (and (or (not specific) (string= specific "TODO"))
  13892. (looking-at org-todo-line-regexp) (match-end 2))
  13893. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13894. (when (and (or (not specific) (string= specific "PRIORITY"))
  13895. (looking-at org-priority-regexp))
  13896. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13897. (when (or (not specific) (string= specific "FILE"))
  13898. (push (cons "FILE" buffer-file-name) props))
  13899. (when (and (or (not specific) (string= specific "TAGS"))
  13900. (setq value (org-get-tags-string))
  13901. (string-match "\\S-" value))
  13902. (push (cons "TAGS" value) props))
  13903. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13904. (setq value (org-get-tags-at)))
  13905. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13906. ":"))
  13907. props))
  13908. (when (or (not specific) (string= specific "BLOCKED"))
  13909. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13910. (when (or (not specific)
  13911. (member specific
  13912. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13913. "TIMESTAMP" "TIMESTAMP_IA")))
  13914. (catch 'match
  13915. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13916. (setq key (if (match-end 1)
  13917. (substring (org-match-string-no-properties 1)
  13918. 0 -1))
  13919. string (if (equal key clockstr)
  13920. (org-trim
  13921. (buffer-substring-no-properties
  13922. (match-beginning 3) (goto-char
  13923. (point-at-eol))))
  13924. (substring (org-match-string-no-properties 3)
  13925. 1 -1)))
  13926. ;; Get the correct property name from the key. This is
  13927. ;; necessary if the user has configured time keywords.
  13928. (setq key1 (concat key ":"))
  13929. (cond
  13930. ((not key)
  13931. (setq key
  13932. (if (= (char-after (match-beginning 3)) ?\[)
  13933. "TIMESTAMP_IA" "TIMESTAMP")))
  13934. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13935. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13936. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13937. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13938. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13939. (progn
  13940. (push (cons key string) props)
  13941. ;; no need to search further if match is found
  13942. (throw 'match t))
  13943. (when (or (equal key "CLOCK") (not (assoc key props)))
  13944. (push (cons key string) props)))))))
  13945. (when (memq which '(all standard))
  13946. ;; Get the standard properties, like :PROP: ...
  13947. (setq range (org-get-property-block beg end))
  13948. (when range
  13949. (goto-char (car range))
  13950. (while (re-search-forward org-property-re
  13951. (cdr range) t)
  13952. (setq key (org-match-string-no-properties 2)
  13953. value (org-trim (or (org-match-string-no-properties 3) "")))
  13954. (unless (member key excluded)
  13955. (push (cons key (or value "")) props)))))
  13956. (if clocksum
  13957. (push (cons "CLOCKSUM"
  13958. (org-columns-number-to-string (/ (float clocksum) 60.)
  13959. 'add_times))
  13960. props))
  13961. (if clocksumt
  13962. (push (cons "CLOCKSUM_T"
  13963. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13964. 'add_times))
  13965. props))
  13966. (unless (assoc "CATEGORY" props)
  13967. (push (cons "CATEGORY" (org-get-category)) props))
  13968. (append sum-props (nreverse props)))))))
  13969. (defun org-entry-get (pom property &optional inherit literal-nil)
  13970. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13971. If INHERIT is non-nil and the entry does not have the property,
  13972. then also check higher levels of the hierarchy.
  13973. If INHERIT is the symbol `selective', use inheritance only if the setting
  13974. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13975. If the property is present but empty, the return value is the empty string.
  13976. If the property is not present at all, nil is returned.
  13977. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13978. do not interpret it as the list atom nil. This is used for inheritance
  13979. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13980. (org-with-point-at pom
  13981. (if (and inherit (if (eq inherit 'selective)
  13982. (org-property-inherit-p property)
  13983. t))
  13984. (org-entry-get-with-inheritance property literal-nil)
  13985. (if (member property org-special-properties)
  13986. ;; We need a special property. Use `org-entry-properties' to
  13987. ;; retrieve it, but specify the wanted property
  13988. (cdr (assoc property (org-entry-properties nil 'special property)))
  13989. (let ((range (org-get-property-block)))
  13990. (when (and range (not (eq (car range) (cdr range))))
  13991. (let* ((props (list (or (assoc property org-file-properties)
  13992. (assoc property org-global-properties)
  13993. (assoc property org-global-properties-fixed))))
  13994. (ap (lambda (key)
  13995. (when (re-search-forward
  13996. (org-re-property key) (cdr range) t)
  13997. (setq props
  13998. (org-update-property-plist
  13999. key
  14000. (if (match-end 3)
  14001. (org-match-string-no-properties 3) "")
  14002. props)))))
  14003. val)
  14004. (goto-char (car range))
  14005. (funcall ap property)
  14006. (goto-char (car range))
  14007. (while (funcall ap (concat property "+")))
  14008. (setq val (cdr (assoc property props)))
  14009. (when val (if literal-nil val (org-not-nil val))))))))))
  14010. (defun org-property-or-variable-value (var &optional inherit)
  14011. "Check if there is a property fixing the value of VAR.
  14012. If yes, return this value. If not, return the current value of the variable."
  14013. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14014. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14015. (read prop)
  14016. (symbol-value var))))
  14017. (defun org-entry-delete (pom property)
  14018. "Delete the property PROPERTY from entry at point-or-marker POM."
  14019. (unless (member property org-special-properties)
  14020. (org-with-point-at pom
  14021. (let ((range (org-get-property-block)))
  14022. (if (and range
  14023. (goto-char (car range))
  14024. (re-search-forward
  14025. (org-re-property property)
  14026. (cdr range) t))
  14027. (progn
  14028. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14029. (org-remove-empty-drawer-at (car range))
  14030. t)
  14031. nil)))))
  14032. ;; Multi-values properties are properties that contain multiple values
  14033. ;; These values are assumed to be single words, separated by whitespace.
  14034. (defun org-entry-add-to-multivalued-property (pom property value)
  14035. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14036. (let* ((old (org-entry-get pom property))
  14037. (values (and old (org-split-string old "[ \t]"))))
  14038. (setq value (org-entry-protect-space value))
  14039. (unless (member value values)
  14040. (setq values (append values (list value)))
  14041. (org-entry-put pom property
  14042. (mapconcat 'identity values " ")))))
  14043. (defun org-entry-remove-from-multivalued-property (pom property value)
  14044. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14045. (let* ((old (org-entry-get pom property))
  14046. (values (and old (org-split-string old "[ \t]"))))
  14047. (setq value (org-entry-protect-space value))
  14048. (when (member value values)
  14049. (setq values (delete value values))
  14050. (org-entry-put pom property
  14051. (mapconcat 'identity values " ")))))
  14052. (defun org-entry-member-in-multivalued-property (pom property value)
  14053. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14054. (let* ((old (org-entry-get pom property))
  14055. (values (and old (org-split-string old "[ \t]"))))
  14056. (setq value (org-entry-protect-space value))
  14057. (member value values)))
  14058. (defun org-entry-get-multivalued-property (pom property)
  14059. "Return a list of values in a multivalued property."
  14060. (let* ((value (org-entry-get pom property))
  14061. (values (and value (org-split-string value "[ \t]"))))
  14062. (mapcar 'org-entry-restore-space values)))
  14063. (defun org-entry-put-multivalued-property (pom property &rest values)
  14064. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14065. VALUES should be a list of strings. Spaces will be protected."
  14066. (org-entry-put pom property
  14067. (mapconcat 'org-entry-protect-space values " "))
  14068. (let* ((value (org-entry-get pom property))
  14069. (values (and value (org-split-string value "[ \t]"))))
  14070. (mapcar 'org-entry-restore-space values)))
  14071. (defun org-entry-protect-space (s)
  14072. "Protect spaces and newline in string S."
  14073. (while (string-match " " s)
  14074. (setq s (replace-match "%20" t t s)))
  14075. (while (string-match "\n" s)
  14076. (setq s (replace-match "%0A" t t s)))
  14077. s)
  14078. (defun org-entry-restore-space (s)
  14079. "Restore spaces and newline in string S."
  14080. (while (string-match "%20" s)
  14081. (setq s (replace-match " " t t s)))
  14082. (while (string-match "%0A" s)
  14083. (setq s (replace-match "\n" t t s)))
  14084. s)
  14085. (defvar org-entry-property-inherited-from (make-marker)
  14086. "Marker pointing to the entry from where a property was inherited.
  14087. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14088. location of the entry where the inheritance search matched. If there was
  14089. no match, the marker will point nowhere.
  14090. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14091. is set.")
  14092. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14093. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14094. The search will stop at the first ancestor which has the property defined.
  14095. If the value found is \"nil\", return nil to show that the property
  14096. should be considered as undefined (this is the meaning of nil here).
  14097. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14098. (move-marker org-entry-property-inherited-from nil)
  14099. (let (tmp)
  14100. (save-excursion
  14101. (save-restriction
  14102. (widen)
  14103. (catch 'ex
  14104. (while t
  14105. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14106. (or (ignore-errors (org-back-to-heading t))
  14107. (goto-char (point-min)))
  14108. (move-marker org-entry-property-inherited-from (point))
  14109. (throw 'ex tmp))
  14110. (or (ignore-errors (org-up-heading-safe))
  14111. (throw 'ex nil))))))
  14112. (setq tmp (or tmp
  14113. (cdr (assoc property org-file-properties))
  14114. (cdr (assoc property org-global-properties))
  14115. (cdr (assoc property org-global-properties-fixed))))
  14116. (if literal-nil tmp (org-not-nil tmp))))
  14117. (defvar org-property-changed-functions nil
  14118. "Hook called when the value of a property has changed.
  14119. Each hook function should accept two arguments, the name of the property
  14120. and the new value.")
  14121. (defun org-entry-put (pom property value)
  14122. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14123. (org-with-point-at pom
  14124. (org-back-to-heading t)
  14125. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14126. range)
  14127. (cond
  14128. ((equal property "TODO")
  14129. (when (and (stringp value) (string-match "\\S-" value)
  14130. (not (member value org-todo-keywords-1)))
  14131. (user-error "\"%s\" is not a valid TODO state" value))
  14132. (if (or (not value)
  14133. (not (string-match "\\S-" value)))
  14134. (setq value 'none))
  14135. (org-todo value)
  14136. (org-set-tags nil 'align))
  14137. ((equal property "PRIORITY")
  14138. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14139. (string-to-char value) ?\ ))
  14140. (org-set-tags nil 'align))
  14141. ((equal property "CLOCKSUM")
  14142. (if (not (re-search-forward
  14143. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14144. (error "Cannot find a clock log")
  14145. (goto-char (- (match-end 1) 2))
  14146. (cond
  14147. ((eq value 'earlier) (org-timestamp-down))
  14148. ((eq value 'later) (org-timestamp-up)))
  14149. (org-clock-sum-current-item)))
  14150. ((equal property "SCHEDULED")
  14151. (if (re-search-forward org-scheduled-time-regexp end t)
  14152. (cond
  14153. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14154. ((eq value 'later) (org-timestamp-change 1 'day))
  14155. (t (call-interactively 'org-schedule)))
  14156. (call-interactively 'org-schedule)))
  14157. ((equal property "DEADLINE")
  14158. (if (re-search-forward org-deadline-time-regexp end t)
  14159. (cond
  14160. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14161. ((eq value 'later) (org-timestamp-change 1 'day))
  14162. (t (call-interactively 'org-deadline)))
  14163. (call-interactively 'org-deadline)))
  14164. ((member property org-special-properties)
  14165. (error "The %s property can not yet be set with `org-entry-put'"
  14166. property))
  14167. (t ; a non-special property
  14168. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14169. (setq range (org-get-property-block beg end 'force))
  14170. (goto-char (car range))
  14171. (if (re-search-forward
  14172. (org-re-property property) (cdr range) t)
  14173. (progn
  14174. (delete-region (match-beginning 0) (match-end 0))
  14175. (goto-char (match-beginning 0)))
  14176. (goto-char (cdr range))
  14177. (insert "\n")
  14178. (backward-char 1)
  14179. (org-indent-line))
  14180. (insert ":" property ":")
  14181. (and value (insert " " value))
  14182. (org-indent-line)))))
  14183. (run-hook-with-args 'org-property-changed-functions property value)))
  14184. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14185. "Get all property keys in the current buffer.
  14186. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14187. like tags and TODO state.
  14188. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14189. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14190. and others.
  14191. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14192. formats in the current buffer."
  14193. (let (rtn range cfmt s p)
  14194. (save-excursion
  14195. (save-restriction
  14196. (widen)
  14197. (goto-char (point-min))
  14198. (while (re-search-forward org-property-start-re nil t)
  14199. (setq range (org-get-property-block))
  14200. (goto-char (car range))
  14201. (while (re-search-forward org-property-re
  14202. (cdr range) t)
  14203. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14204. (outline-next-heading))))
  14205. (when include-specials
  14206. (setq rtn (append org-special-properties rtn)))
  14207. (when include-defaults
  14208. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14209. (add-to-list 'rtn org-effort-property))
  14210. (when include-columns
  14211. (save-excursion
  14212. (save-restriction
  14213. (widen)
  14214. (goto-char (point-min))
  14215. (while (re-search-forward
  14216. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14217. nil t)
  14218. (setq cfmt (match-string 2) s 0)
  14219. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14220. cfmt s)
  14221. (setq s (match-end 0)
  14222. p (match-string 1 cfmt))
  14223. (unless (or (equal p "ITEM")
  14224. (member p org-special-properties))
  14225. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14226. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14227. (defun org-property-values (key)
  14228. "Return a list of all values of property KEY in the current buffer."
  14229. (save-excursion
  14230. (save-restriction
  14231. (widen)
  14232. (goto-char (point-min))
  14233. (let ((re (org-re-property key))
  14234. values)
  14235. (while (re-search-forward re nil t)
  14236. (add-to-list 'values (org-trim (match-string 3))))
  14237. (delete "" values)))))
  14238. (defun org-insert-property-drawer ()
  14239. "Insert a property drawer into the current entry."
  14240. (org-back-to-heading t)
  14241. (looking-at org-outline-regexp)
  14242. (let ((indent (if org-adapt-indentation
  14243. (- (match-end 0) (match-beginning 0))
  14244. 0))
  14245. (beg (point))
  14246. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14247. end hiddenp)
  14248. (outline-next-heading)
  14249. (setq end (point))
  14250. (goto-char beg)
  14251. (while (re-search-forward re end t))
  14252. (setq hiddenp (outline-invisible-p))
  14253. (end-of-line 1)
  14254. (and (equal (char-after) ?\n) (forward-char 1))
  14255. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14256. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14257. ;; just skip this line
  14258. (beginning-of-line 2)
  14259. ;; Drawer start, find the end
  14260. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14261. (beginning-of-line 1)))
  14262. (org-skip-over-state-notes)
  14263. (skip-chars-backward " \t\n\r")
  14264. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14265. (forward-char 1))
  14266. (goto-char (point-at-eol))
  14267. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14268. (beginning-of-line 0)
  14269. (org-indent-to-column indent)
  14270. (beginning-of-line 2)
  14271. (org-indent-to-column indent)
  14272. (beginning-of-line 0)
  14273. (if hiddenp
  14274. (save-excursion
  14275. (org-back-to-heading t)
  14276. (hide-entry))
  14277. (org-flag-drawer t))))
  14278. (defun org-insert-drawer (&optional arg drawer)
  14279. "Insert a drawer at point.
  14280. Optional argument DRAWER, when non-nil, is a string representing
  14281. drawer's name. Otherwise, the user is prompted for a name.
  14282. If a region is active, insert the drawer around that region
  14283. instead.
  14284. Point is left between drawer's boundaries."
  14285. (interactive "P")
  14286. (let* ((drawer (if arg "PROPERTIES"
  14287. (or drawer (read-from-minibuffer "Drawer: ")))))
  14288. (cond
  14289. ;; With C-u, fall back on `org-insert-property-drawer'
  14290. (arg (org-insert-property-drawer))
  14291. ;;
  14292. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14293. (user-error "Invalid drawer name"))
  14294. ;; With an active region, insert a drawer at point.
  14295. ((not (org-region-active-p))
  14296. (progn
  14297. (unless (bolp) (insert "\n"))
  14298. (insert (format ":%s:\n\n:END:\n" drawer))
  14299. (forward-line -2)))
  14300. ;; Otherwise, insert the drawer at point
  14301. (t
  14302. (let ((rbeg (region-beginning))
  14303. (rend (copy-marker (region-end))))
  14304. (unwind-protect
  14305. (progn
  14306. (goto-char rbeg)
  14307. (beginning-of-line)
  14308. (when (save-excursion
  14309. (re-search-forward org-outline-regexp-bol rend t))
  14310. (user-error "Drawers cannot contain headlines"))
  14311. ;; Position point at the beginning of the first
  14312. ;; non-blank line in region. Insert drawer's opening
  14313. ;; there, then indent it.
  14314. (org-skip-whitespace)
  14315. (beginning-of-line)
  14316. (insert ":" drawer ":\n")
  14317. (forward-line -1)
  14318. (indent-for-tab-command)
  14319. ;; Move point to the beginning of the first blank line
  14320. ;; after the last non-blank line in region. Insert
  14321. ;; drawer's closing, then indent it.
  14322. (goto-char rend)
  14323. (skip-chars-backward " \r\t\n")
  14324. (insert "\n:END:")
  14325. (deactivate-mark t)
  14326. (indent-for-tab-command)
  14327. (unless (eolp) (insert "\n")))
  14328. ;; Clear marker, whatever the outcome of insertion is.
  14329. (set-marker rend nil)))))))
  14330. (defvar org-property-set-functions-alist nil
  14331. "Property set function alist.
  14332. Each entry should have the following format:
  14333. (PROPERTY . READ-FUNCTION)
  14334. The read function will be called with the same argument as
  14335. `org-completing-read'.")
  14336. (defun org-set-property-function (property)
  14337. "Get the function that should be used to set PROPERTY.
  14338. This is computed according to `org-property-set-functions-alist'."
  14339. (or (cdr (assoc property org-property-set-functions-alist))
  14340. 'org-completing-read))
  14341. (defun org-read-property-value (property)
  14342. "Read PROPERTY value from user."
  14343. (let* ((completion-ignore-case t)
  14344. (allowed (org-property-get-allowed-values nil property 'table))
  14345. (cur (org-entry-get nil property))
  14346. (prompt (concat property " value"
  14347. (if (and cur (string-match "\\S-" cur))
  14348. (concat " [" cur "]") "") ": "))
  14349. (set-function (org-set-property-function property))
  14350. (val (if allowed
  14351. (funcall set-function prompt allowed nil
  14352. (not (get-text-property 0 'org-unrestricted
  14353. (caar allowed))))
  14354. (let (org-completion-use-ido org-completion-use-iswitchb)
  14355. (funcall set-function prompt
  14356. (mapcar 'list (org-property-values property))
  14357. nil nil "" nil cur)))))
  14358. (if (equal val "")
  14359. cur
  14360. val)))
  14361. (defvar org-last-set-property nil)
  14362. (defvar org-last-set-property-value nil)
  14363. (defun org-read-property-name ()
  14364. "Read a property name."
  14365. (let* ((completion-ignore-case t)
  14366. (keys (org-buffer-property-keys nil t t))
  14367. (default-prop (or (save-excursion
  14368. (save-match-data
  14369. (beginning-of-line)
  14370. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14371. (null (string= (match-string 1) "END"))
  14372. (match-string 1))))
  14373. org-last-set-property))
  14374. (property (org-icompleting-read
  14375. (concat "Property"
  14376. (if default-prop (concat " [" default-prop "]") "")
  14377. ": ")
  14378. (mapcar 'list keys)
  14379. nil nil nil nil
  14380. default-prop)))
  14381. (if (member property keys)
  14382. property
  14383. (or (cdr (assoc (downcase property)
  14384. (mapcar (lambda (x) (cons (downcase x) x))
  14385. keys)))
  14386. property))))
  14387. (defun org-set-property-and-value (use-last)
  14388. "Allow to set [PROPERTY]: [value] direction from prompt.
  14389. When use-default, don't even ask, just use the last
  14390. \"[PROPERTY]: [value]\" string from the history."
  14391. (interactive "P")
  14392. (let* ((completion-ignore-case t)
  14393. (pv (or (and use-last org-last-set-property-value)
  14394. (org-completing-read
  14395. "Enter a \"[Property]: [value]\" pair: "
  14396. nil nil nil nil nil
  14397. org-last-set-property-value)))
  14398. prop val)
  14399. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14400. (setq prop (match-string 1 pv)
  14401. val (match-string 2 pv))
  14402. (org-set-property prop val))))
  14403. (defun org-set-property (property value)
  14404. "In the current entry, set PROPERTY to VALUE.
  14405. When called interactively, this will prompt for a property name, offering
  14406. completion on existing and default properties. And then it will prompt
  14407. for a value, offering completion either on allowed values (via an inherited
  14408. xxx_ALL property) or on existing values in other instances of this property
  14409. in the current file."
  14410. (interactive (list nil nil))
  14411. (let* ((property (or property (org-read-property-name)))
  14412. (value (or value (org-read-property-value property)))
  14413. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14414. (setq org-last-set-property property)
  14415. (setq org-last-set-property-value (concat property ": " value))
  14416. ;; Possibly postprocess the inserted value:
  14417. (when fn (setq value (funcall fn value)))
  14418. (unless (equal (org-entry-get nil property) value)
  14419. (org-entry-put nil property value))))
  14420. (defun org-delete-property (property)
  14421. "In the current entry, delete PROPERTY."
  14422. (interactive
  14423. (let* ((completion-ignore-case t)
  14424. (prop (org-icompleting-read "Property: "
  14425. (org-entry-properties nil 'standard))))
  14426. (list prop)))
  14427. (message "Property %s %s" property
  14428. (if (org-entry-delete nil property)
  14429. "deleted"
  14430. "was not present in the entry")))
  14431. (defun org-delete-property-globally (property)
  14432. "Remove PROPERTY globally, from all entries."
  14433. (interactive
  14434. (let* ((completion-ignore-case t)
  14435. (prop (org-icompleting-read
  14436. "Globally remove property: "
  14437. (mapcar 'list (org-buffer-property-keys)))))
  14438. (list prop)))
  14439. (save-excursion
  14440. (save-restriction
  14441. (widen)
  14442. (goto-char (point-min))
  14443. (let ((cnt 0))
  14444. (while (re-search-forward
  14445. (org-re-property property)
  14446. nil t)
  14447. (setq cnt (1+ cnt))
  14448. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14449. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14450. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14451. (defun org-compute-property-at-point ()
  14452. "Compute the property at point.
  14453. This looks for an enclosing column format, extracts the operator and
  14454. then applies it to the property in the column format's scope."
  14455. (interactive)
  14456. (unless (org-at-property-p)
  14457. (user-error "Not at a property"))
  14458. (let ((prop (org-match-string-no-properties 2)))
  14459. (org-columns-get-format-and-top-level)
  14460. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14461. (user-error "No operator defined for property %s" prop))
  14462. (org-columns-compute prop)))
  14463. (defvar org-property-allowed-value-functions nil
  14464. "Hook for functions supplying allowed values for a specific property.
  14465. The functions must take a single argument, the name of the property, and
  14466. return a flat list of allowed values. If \":ETC\" is one of
  14467. the values, this means that these values are intended as defaults for
  14468. completion, but that other values should be allowed too.
  14469. The functions must return nil if they are not responsible for this
  14470. property.")
  14471. (defun org-property-get-allowed-values (pom property &optional table)
  14472. "Get allowed values for the property PROPERTY.
  14473. When TABLE is non-nil, return an alist that can directly be used for
  14474. completion."
  14475. (let (vals)
  14476. (cond
  14477. ((equal property "TODO")
  14478. (setq vals (org-with-point-at pom
  14479. (append org-todo-keywords-1 '("")))))
  14480. ((equal property "PRIORITY")
  14481. (let ((n org-lowest-priority))
  14482. (while (>= n org-highest-priority)
  14483. (push (char-to-string n) vals)
  14484. (setq n (1- n)))))
  14485. ((member property org-special-properties))
  14486. ((setq vals (run-hook-with-args-until-success
  14487. 'org-property-allowed-value-functions property)))
  14488. (t
  14489. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14490. (when (and vals (string-match "\\S-" vals))
  14491. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14492. (setq vals (mapcar (lambda (x)
  14493. (cond ((stringp x) x)
  14494. ((numberp x) (number-to-string x))
  14495. ((symbolp x) (symbol-name x))
  14496. (t "???")))
  14497. vals)))))
  14498. (when (member ":ETC" vals)
  14499. (setq vals (remove ":ETC" vals))
  14500. (org-add-props (car vals) '(org-unrestricted t)))
  14501. (if table (mapcar 'list vals) vals)))
  14502. (defun org-property-previous-allowed-value (&optional previous)
  14503. "Switch to the next allowed value for this property."
  14504. (interactive)
  14505. (org-property-next-allowed-value t))
  14506. (defun org-property-next-allowed-value (&optional previous)
  14507. "Switch to the next allowed value for this property."
  14508. (interactive)
  14509. (unless (org-at-property-p)
  14510. (user-error "Not at a property"))
  14511. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14512. (key (match-string 2))
  14513. (value (match-string 3))
  14514. (allowed (or (org-property-get-allowed-values (point) key)
  14515. (and (member value '("[ ]" "[-]" "[X]"))
  14516. '("[ ]" "[X]"))))
  14517. (heading (save-match-data (nth 4 (org-heading-components))))
  14518. nval)
  14519. (unless allowed
  14520. (user-error "Allowed values for this property have not been defined"))
  14521. (if previous (setq allowed (reverse allowed)))
  14522. (if (member value allowed)
  14523. (setq nval (car (cdr (member value allowed)))))
  14524. (setq nval (or nval (car allowed)))
  14525. (if (equal nval value)
  14526. (user-error "Only one allowed value for this property"))
  14527. (org-at-property-p)
  14528. (replace-match (concat " :" key ": " nval) t t)
  14529. (org-indent-line)
  14530. (beginning-of-line 1)
  14531. (skip-chars-forward " \t")
  14532. (when (equal prop org-effort-property)
  14533. (save-excursion
  14534. (org-back-to-heading t)
  14535. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14536. (when (string= org-clock-current-task heading)
  14537. (setq org-clock-effort nval)
  14538. (org-clock-update-mode-line)))
  14539. (run-hook-with-args 'org-property-changed-functions key nval)))
  14540. (defun org-find-olp (path &optional this-buffer)
  14541. "Return a marker pointing to the entry at outline path OLP.
  14542. If anything goes wrong, throw an error.
  14543. You can wrap this call to catch the error like this:
  14544. (condition-case msg
  14545. (org-mobile-locate-entry (match-string 4))
  14546. (error (nth 1 msg)))
  14547. The return value will then be either a string with the error message,
  14548. or a marker if everything is OK.
  14549. If THIS-BUFFER is set, the outline path does not contain a file,
  14550. only headings."
  14551. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14552. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14553. (level 1)
  14554. (lmin 1)
  14555. (lmax 1)
  14556. limit re end found pos heading cnt flevel)
  14557. (unless buffer (error "File not found :%s" file))
  14558. (with-current-buffer buffer
  14559. (save-excursion
  14560. (save-restriction
  14561. (widen)
  14562. (setq limit (point-max))
  14563. (goto-char (point-min))
  14564. (while (setq heading (pop path))
  14565. (setq re (format org-complex-heading-regexp-format
  14566. (regexp-quote heading)))
  14567. (setq cnt 0 pos (point))
  14568. (while (re-search-forward re end t)
  14569. (setq level (- (match-end 1) (match-beginning 1)))
  14570. (if (and (>= level lmin) (<= level lmax))
  14571. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14572. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14573. lmax heading))
  14574. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14575. lmax heading))
  14576. (goto-char found)
  14577. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14578. (setq end (save-excursion (org-end-of-subtree t t))))
  14579. (when (org-at-heading-p)
  14580. (point-marker)))))))
  14581. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14582. "Find node HEADING in BUFFER.
  14583. Return a marker to the heading if it was found, or nil if not.
  14584. If POS-ONLY is set, return just the position instead of a marker.
  14585. The heading text must match exact, but it may have a TODO keyword,
  14586. a priority cookie and tags in the standard locations."
  14587. (with-current-buffer (or buffer (current-buffer))
  14588. (save-excursion
  14589. (save-restriction
  14590. (widen)
  14591. (goto-char (point-min))
  14592. (let (case-fold-search)
  14593. (if (re-search-forward
  14594. (format org-complex-heading-regexp-format
  14595. (regexp-quote heading)) nil t)
  14596. (if pos-only
  14597. (match-beginning 0)
  14598. (move-marker (make-marker) (match-beginning 0)))))))))
  14599. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14600. "Find Org node headline HEADING in all .org files in directory DIR.
  14601. When the target headline is found, return a marker to this location."
  14602. (let ((files (directory-files (or dir default-directory)
  14603. nil "\\`[^.#].*\\.org\\'"))
  14604. file visiting m buffer)
  14605. (catch 'found
  14606. (while (setq file (pop files))
  14607. (message "trying %s" file)
  14608. (setq visiting (org-find-base-buffer-visiting file))
  14609. (setq buffer (or visiting (find-file-noselect file)))
  14610. (setq m (org-find-exact-headline-in-buffer
  14611. heading buffer))
  14612. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14613. (and m (throw 'found m))))))
  14614. (defun org-find-entry-with-id (ident)
  14615. "Locate the entry that contains the ID property with exact value IDENT.
  14616. IDENT can be a string, a symbol or a number, this function will search for
  14617. the string representation of it.
  14618. Return the position where this entry starts, or nil if there is no such entry."
  14619. (interactive "sID: ")
  14620. (let ((id (cond
  14621. ((stringp ident) ident)
  14622. ((symbol-name ident) (symbol-name ident))
  14623. ((numberp ident) (number-to-string ident))
  14624. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14625. (case-fold-search nil))
  14626. (save-excursion
  14627. (save-restriction
  14628. (widen)
  14629. (goto-char (point-min))
  14630. (when (re-search-forward
  14631. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14632. nil t)
  14633. (org-back-to-heading t)
  14634. (point))))))
  14635. ;;;; Timestamps
  14636. (defvar org-last-changed-timestamp nil)
  14637. (defvar org-last-inserted-timestamp nil
  14638. "The last time stamp inserted with `org-insert-time-stamp'.")
  14639. (defvar org-time-was-given) ; dynamically scoped parameter
  14640. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14641. (defvar org-ts-what) ; dynamically scoped parameter
  14642. (defun org-time-stamp (arg &optional inactive)
  14643. "Prompt for a date/time and insert a time stamp.
  14644. If the user specifies a time like HH:MM or if this command is
  14645. called with at least one prefix argument, the time stamp contains
  14646. the date and the time. Otherwise, only the date is be included.
  14647. All parts of a date not specified by the user is filled in from
  14648. the current date/time. So if you just press return without
  14649. typing anything, the time stamp will represent the current
  14650. date/time.
  14651. If there is already a timestamp at the cursor, it will be
  14652. modified.
  14653. With two universal prefix arguments, insert an active timestamp
  14654. with the current time without prompting the user.
  14655. When called from lisp, the timestamp is inactive if INACTIVE is
  14656. non-nil."
  14657. (interactive "P")
  14658. (let* ((ts nil)
  14659. (default-time
  14660. ;; Default time is either today, or, when entering a range,
  14661. ;; the range start.
  14662. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14663. (save-excursion
  14664. (re-search-backward
  14665. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14666. (- (point) 20) t)))
  14667. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14668. (current-time)))
  14669. (default-input (and ts (org-get-compact-tod ts)))
  14670. (repeater (save-excursion
  14671. (save-match-data
  14672. (beginning-of-line)
  14673. (when (re-search-forward
  14674. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14675. (save-excursion (progn (end-of-line) (point))) t)
  14676. (match-string 0)))))
  14677. org-time-was-given org-end-time-was-given time)
  14678. (cond
  14679. ((and (org-at-timestamp-p t)
  14680. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14681. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14682. (insert "--")
  14683. (setq time (let ((this-command this-command))
  14684. (org-read-date arg 'totime nil nil
  14685. default-time default-input inactive)))
  14686. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14687. ((org-at-timestamp-p t)
  14688. (setq time (let ((this-command this-command))
  14689. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14690. (when (org-at-timestamp-p t) ; just to get the match data
  14691. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14692. (replace-match "")
  14693. (setq org-last-changed-timestamp
  14694. (org-insert-time-stamp
  14695. time (or org-time-was-given arg)
  14696. inactive nil nil (list org-end-time-was-given)))
  14697. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14698. (setq org-last-changed-timestamp
  14699. (concat (substring org-last-inserted-timestamp 0 -1)
  14700. " " repeater ">"))))
  14701. (message "Timestamp updated"))
  14702. ((equal arg '(16))
  14703. (org-insert-time-stamp (current-time) t inactive))
  14704. (t
  14705. (setq time (let ((this-command this-command))
  14706. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14707. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14708. nil nil (list org-end-time-was-given))))))
  14709. ;; FIXME: can we use this for something else, like computing time differences?
  14710. (defun org-get-compact-tod (s)
  14711. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14712. (let* ((t1 (match-string 1 s))
  14713. (h1 (string-to-number (match-string 2 s)))
  14714. (m1 (string-to-number (match-string 3 s)))
  14715. (t2 (and (match-end 4) (match-string 5 s)))
  14716. (h2 (and t2 (string-to-number (match-string 6 s))))
  14717. (m2 (and t2 (string-to-number (match-string 7 s))))
  14718. dh dm)
  14719. (if (not t2)
  14720. t1
  14721. (setq dh (- h2 h1) dm (- m2 m1))
  14722. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14723. (concat t1 "+" (number-to-string dh)
  14724. (and (/= 0 dm) (format ":%02d" dm)))))))
  14725. (defun org-time-stamp-inactive (&optional arg)
  14726. "Insert an inactive time stamp.
  14727. An inactive time stamp is enclosed in square brackets instead of angle
  14728. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14729. does not link to the calendar and cannot be changed with the S-cursor keys.
  14730. So these are more for recording a certain time/date."
  14731. (interactive "P")
  14732. (org-time-stamp arg 'inactive))
  14733. (defvar org-date-ovl (make-overlay 1 1))
  14734. (overlay-put org-date-ovl 'face 'org-date-selected)
  14735. (org-detach-overlay org-date-ovl)
  14736. (defvar org-ans1) ; dynamically scoped parameter
  14737. (defvar org-ans2) ; dynamically scoped parameter
  14738. (defvar org-plain-time-of-day-regexp) ; defined below
  14739. (defvar org-overriding-default-time nil) ; dynamically scoped
  14740. (defvar org-read-date-overlay nil)
  14741. (defvar org-dcst nil) ; dynamically scoped
  14742. (defvar org-read-date-history nil)
  14743. (defvar org-read-date-final-answer nil)
  14744. (defvar org-read-date-analyze-futurep nil)
  14745. (defvar org-read-date-analyze-forced-year nil)
  14746. (defvar org-read-date-inactive)
  14747. (defvar org-read-date-minibuffer-local-map
  14748. (let* ((org-replace-disputed-keys nil)
  14749. (map (make-sparse-keymap)))
  14750. (set-keymap-parent map minibuffer-local-map)
  14751. (org-defkey map (kbd ".")
  14752. (lambda () (interactive)
  14753. ;; Are we at the beginning of the prompt?
  14754. (if (looking-back "^[^:]+: ")
  14755. (org-eval-in-calendar '(calendar-goto-today))
  14756. (insert "."))))
  14757. (org-defkey map (kbd "C-.")
  14758. (lambda () (interactive)
  14759. (org-eval-in-calendar '(calendar-goto-today))))
  14760. (org-defkey map [(meta shift left)]
  14761. (lambda () (interactive)
  14762. (org-eval-in-calendar '(calendar-backward-month 1))))
  14763. (org-defkey map [(meta shift right)]
  14764. (lambda () (interactive)
  14765. (org-eval-in-calendar '(calendar-forward-month 1))))
  14766. (org-defkey map [(meta shift up)]
  14767. (lambda () (interactive)
  14768. (org-eval-in-calendar '(calendar-backward-year 1))))
  14769. (org-defkey map [(meta shift down)]
  14770. (lambda () (interactive)
  14771. (org-eval-in-calendar '(calendar-forward-year 1))))
  14772. (org-defkey map [?\e (shift left)]
  14773. (lambda () (interactive)
  14774. (org-eval-in-calendar '(calendar-backward-month 1))))
  14775. (org-defkey map [?\e (shift right)]
  14776. (lambda () (interactive)
  14777. (org-eval-in-calendar '(calendar-forward-month 1))))
  14778. (org-defkey map [?\e (shift up)]
  14779. (lambda () (interactive)
  14780. (org-eval-in-calendar '(calendar-backward-year 1))))
  14781. (org-defkey map [?\e (shift down)]
  14782. (lambda () (interactive)
  14783. (org-eval-in-calendar '(calendar-forward-year 1))))
  14784. (org-defkey map [(shift up)]
  14785. (lambda () (interactive)
  14786. (org-eval-in-calendar '(calendar-backward-week 1))))
  14787. (org-defkey map [(shift down)]
  14788. (lambda () (interactive)
  14789. (org-eval-in-calendar '(calendar-forward-week 1))))
  14790. (org-defkey map [(shift left)]
  14791. (lambda () (interactive)
  14792. (org-eval-in-calendar '(calendar-backward-day 1))))
  14793. (org-defkey map [(shift right)]
  14794. (lambda () (interactive)
  14795. (org-eval-in-calendar '(calendar-forward-day 1))))
  14796. (org-defkey map "!"
  14797. (lambda () (interactive)
  14798. (org-eval-in-calendar '(diary-view-entries))
  14799. (message "")))
  14800. (org-defkey map ">"
  14801. (lambda () (interactive)
  14802. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14803. (org-defkey map "<"
  14804. (lambda () (interactive)
  14805. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14806. (org-defkey map "\C-v"
  14807. (lambda () (interactive)
  14808. (org-eval-in-calendar
  14809. '(calendar-scroll-left-three-months 1))))
  14810. (org-defkey map "\M-v"
  14811. (lambda () (interactive)
  14812. (org-eval-in-calendar
  14813. '(calendar-scroll-right-three-months 1))))
  14814. map)
  14815. "Keymap for minibuffer commands when using `org-read-date'.")
  14816. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14817. default-time default-input inactive)
  14818. "Read a date, possibly a time, and make things smooth for the user.
  14819. The prompt will suggest to enter an ISO date, but you can also enter anything
  14820. which will at least partially be understood by `parse-time-string'.
  14821. Unrecognized parts of the date will default to the current day, month, year,
  14822. hour and minute. If this command is called to replace a timestamp at point,
  14823. or to enter the second timestamp of a range, the default time is taken
  14824. from the existing stamp. Furthermore, the command prefers the future,
  14825. so if you are giving a date where the year is not given, and the day-month
  14826. combination is already past in the current year, it will assume you
  14827. mean next year. For details, see the manual. A few examples:
  14828. 3-2-5 --> 2003-02-05
  14829. feb 15 --> currentyear-02-15
  14830. 2/15 --> currentyear-02-15
  14831. sep 12 9 --> 2009-09-12
  14832. 12:45 --> today 12:45
  14833. 22 sept 0:34 --> currentyear-09-22 0:34
  14834. 12 --> currentyear-currentmonth-12
  14835. Fri --> nearest Friday after today
  14836. -Tue --> last Tuesday
  14837. etc.
  14838. Furthermore you can specify a relative date by giving, as the *first* thing
  14839. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14840. change in days weeks, months, years.
  14841. With a single plus or minus, the date is relative to today. With a double
  14842. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14843. +4d --> four days from today
  14844. +4 --> same as above
  14845. +2w --> two weeks from today
  14846. ++5 --> five days from default date
  14847. The function understands only English month and weekday abbreviations.
  14848. While prompting, a calendar is popped up - you can also select the
  14849. date with the mouse (button 1). The calendar shows a period of three
  14850. months. To scroll it to other months, use the keys `>' and `<'.
  14851. If you don't like the calendar, turn it off with
  14852. \(setq org-read-date-popup-calendar nil)
  14853. With optional argument TO-TIME, the date will immediately be converted
  14854. to an internal time.
  14855. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14856. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14857. still enter a time, and this function will inform the calling routine
  14858. about this change. The calling routine may then choose to change the
  14859. format used to insert the time stamp into the buffer to include the time.
  14860. With optional argument FROM-STRING, read from this string instead from
  14861. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14862. the time/date that is used for everything that is not specified by the
  14863. user."
  14864. (require 'parse-time)
  14865. (let* ((org-time-stamp-rounding-minutes
  14866. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14867. (org-dcst org-display-custom-times)
  14868. (ct (org-current-time))
  14869. (org-def (or org-overriding-default-time default-time ct))
  14870. (org-defdecode (decode-time org-def))
  14871. (dummy (progn
  14872. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14873. (setcar (nthcdr 2 org-defdecode) -1)
  14874. (setcar (nthcdr 1 org-defdecode) 59)
  14875. (setq org-def (apply 'encode-time org-defdecode)
  14876. org-defdecode (decode-time org-def)))))
  14877. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14878. (calendar-frame-setup nil)
  14879. (calendar-setup nil)
  14880. (calendar-move-hook nil)
  14881. (calendar-view-diary-initially-flag nil)
  14882. (calendar-view-holidays-initially-flag nil)
  14883. (timestr (format-time-string
  14884. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14885. (prompt (concat (if prompt (concat prompt " ") "")
  14886. (format "Date+time [%s]: " timestr)))
  14887. ans (org-ans0 "") org-ans1 org-ans2 final)
  14888. (cond
  14889. (from-string (setq ans from-string))
  14890. (org-read-date-popup-calendar
  14891. (save-excursion
  14892. (save-window-excursion
  14893. (calendar)
  14894. (org-eval-in-calendar '(setq cursor-type nil) t)
  14895. (unwind-protect
  14896. (progn
  14897. (calendar-forward-day (- (time-to-days org-def)
  14898. (calendar-absolute-from-gregorian
  14899. (calendar-current-date))))
  14900. (org-eval-in-calendar nil t)
  14901. (let* ((old-map (current-local-map))
  14902. (map (copy-keymap calendar-mode-map))
  14903. (minibuffer-local-map
  14904. (copy-keymap org-read-date-minibuffer-local-map)))
  14905. (org-defkey map (kbd "RET") 'org-calendar-select)
  14906. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14907. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14908. (unwind-protect
  14909. (progn
  14910. (use-local-map map)
  14911. (setq org-read-date-inactive inactive)
  14912. (add-hook 'post-command-hook 'org-read-date-display)
  14913. (setq org-ans0 (read-string prompt default-input
  14914. 'org-read-date-history nil))
  14915. ;; org-ans0: from prompt
  14916. ;; org-ans1: from mouse click
  14917. ;; org-ans2: from calendar motion
  14918. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14919. (remove-hook 'post-command-hook 'org-read-date-display)
  14920. (use-local-map old-map)
  14921. (when org-read-date-overlay
  14922. (delete-overlay org-read-date-overlay)
  14923. (setq org-read-date-overlay nil)))))
  14924. (bury-buffer "*Calendar*")))))
  14925. (t ; Naked prompt only
  14926. (unwind-protect
  14927. (setq ans (read-string prompt default-input
  14928. 'org-read-date-history timestr))
  14929. (when org-read-date-overlay
  14930. (delete-overlay org-read-date-overlay)
  14931. (setq org-read-date-overlay nil)))))
  14932. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14933. (when org-read-date-analyze-forced-year
  14934. (message "Year was forced into %s"
  14935. (if org-read-date-force-compatible-dates
  14936. "compatible range (1970-2037)"
  14937. "range representable on this machine"))
  14938. (ding))
  14939. ;; One round trip to get rid of 34th of August and stuff like that....
  14940. (setq final (decode-time (apply 'encode-time final)))
  14941. (setq org-read-date-final-answer ans)
  14942. (if to-time
  14943. (apply 'encode-time final)
  14944. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14945. (format "%04d-%02d-%02d %02d:%02d"
  14946. (nth 5 final) (nth 4 final) (nth 3 final)
  14947. (nth 2 final) (nth 1 final))
  14948. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14949. (defvar org-def)
  14950. (defvar org-defdecode)
  14951. (defvar org-with-time)
  14952. (defun org-read-date-display ()
  14953. "Display the current date prompt interpretation in the minibuffer."
  14954. (when org-read-date-display-live
  14955. (when org-read-date-overlay
  14956. (delete-overlay org-read-date-overlay))
  14957. (when (minibufferp (current-buffer))
  14958. (save-excursion
  14959. (end-of-line 1)
  14960. (while (not (equal (buffer-substring
  14961. (max (point-min) (- (point) 4)) (point))
  14962. " "))
  14963. (insert " ")))
  14964. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14965. " " (or org-ans1 org-ans2)))
  14966. (org-end-time-was-given nil)
  14967. (f (org-read-date-analyze ans org-def org-defdecode))
  14968. (fmts (if org-dcst
  14969. org-time-stamp-custom-formats
  14970. org-time-stamp-formats))
  14971. (fmt (if (or org-with-time
  14972. (and (boundp 'org-time-was-given) org-time-was-given))
  14973. (cdr fmts)
  14974. (car fmts)))
  14975. (txt (format-time-string fmt (apply 'encode-time f)))
  14976. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14977. (txt (concat "=> " txt)))
  14978. (when (and org-end-time-was-given
  14979. (string-match org-plain-time-of-day-regexp txt))
  14980. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14981. org-end-time-was-given
  14982. (substring txt (match-end 0)))))
  14983. (when org-read-date-analyze-futurep
  14984. (setq txt (concat txt " (=>F)")))
  14985. (setq org-read-date-overlay
  14986. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14987. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14988. (defun org-read-date-analyze (ans org-def org-defdecode)
  14989. "Analyze the combined answer of the date prompt."
  14990. ;; FIXME: cleanup and comment
  14991. (let ((nowdecode (decode-time (current-time)))
  14992. delta deltan deltaw deltadef year month day
  14993. hour minute second wday pm h2 m2 tl wday1
  14994. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14995. (setq org-read-date-analyze-futurep nil
  14996. org-read-date-analyze-forced-year nil)
  14997. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14998. (setq ans "+0"))
  14999. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15000. (setq ans (replace-match "" t t ans)
  15001. deltan (car delta)
  15002. deltaw (nth 1 delta)
  15003. deltadef (nth 2 delta)))
  15004. ;; Check if there is an iso week date in there. If yes, store the
  15005. ;; info and postpone interpreting it until the rest of the parsing
  15006. ;; is done.
  15007. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15008. (setq iso-year (if (match-end 1)
  15009. (org-small-year-to-year
  15010. (string-to-number (match-string 1 ans))))
  15011. iso-weekday (if (match-end 3)
  15012. (string-to-number (match-string 3 ans)))
  15013. iso-week (string-to-number (match-string 2 ans)))
  15014. (setq ans (replace-match "" t t ans)))
  15015. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15016. (when (string-match
  15017. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15018. (setq year (if (match-end 2)
  15019. (string-to-number (match-string 2 ans))
  15020. (progn (setq kill-year t)
  15021. (string-to-number (format-time-string "%Y"))))
  15022. month (string-to-number (match-string 3 ans))
  15023. day (string-to-number (match-string 4 ans)))
  15024. (if (< year 100) (setq year (+ 2000 year)))
  15025. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15026. t nil ans)))
  15027. ;; Help matching dotted european dates
  15028. (when (string-match
  15029. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15030. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15031. (setq kill-year t)
  15032. (string-to-number (format-time-string "%Y")))
  15033. day (string-to-number (match-string 1 ans))
  15034. month (string-to-number (match-string 2 ans))
  15035. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15036. t nil ans)))
  15037. ;; Help matching american dates, like 5/30 or 5/30/7
  15038. (when (string-match
  15039. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15040. (setq year (if (match-end 4)
  15041. (string-to-number (match-string 4 ans))
  15042. (progn (setq kill-year t)
  15043. (string-to-number (format-time-string "%Y"))))
  15044. month (string-to-number (match-string 1 ans))
  15045. day (string-to-number (match-string 2 ans)))
  15046. (if (< year 100) (setq year (+ 2000 year)))
  15047. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15048. t nil ans)))
  15049. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15050. ;; If there is a time with am/pm, and *no* time without it, we convert
  15051. ;; so that matching will be successful.
  15052. (loop for i from 1 to 2 do ; twice, for end time as well
  15053. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15054. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15055. (setq hour (string-to-number (match-string 1 ans))
  15056. minute (if (match-end 3)
  15057. (string-to-number (match-string 3 ans))
  15058. 0)
  15059. pm (equal ?p
  15060. (string-to-char (downcase (match-string 4 ans)))))
  15061. (if (and (= hour 12) (not pm))
  15062. (setq hour 0)
  15063. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15064. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15065. t t ans))))
  15066. ;; Check if a time range is given as a duration
  15067. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15068. (setq hour (string-to-number (match-string 1 ans))
  15069. h2 (+ hour (string-to-number (match-string 3 ans)))
  15070. minute (string-to-number (match-string 2 ans))
  15071. m2 (+ minute (if (match-end 5) (string-to-number
  15072. (match-string 5 ans))0)))
  15073. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15074. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15075. t t ans)))
  15076. ;; Check if there is a time range
  15077. (when (boundp 'org-end-time-was-given)
  15078. (setq org-time-was-given nil)
  15079. (when (and (string-match org-plain-time-of-day-regexp ans)
  15080. (match-end 8))
  15081. (setq org-end-time-was-given (match-string 8 ans))
  15082. (setq ans (concat (substring ans 0 (match-beginning 7))
  15083. (substring ans (match-end 7))))))
  15084. (setq tl (parse-time-string ans)
  15085. day (or (nth 3 tl) (nth 3 org-defdecode))
  15086. month (or (nth 4 tl)
  15087. (if (and org-read-date-prefer-future
  15088. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15089. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15090. (nth 4 org-defdecode)))
  15091. year (or (and (not kill-year) (nth 5 tl))
  15092. (if (and org-read-date-prefer-future
  15093. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15094. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15095. (nth 5 org-defdecode)))
  15096. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15097. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15098. second (or (nth 0 tl) 0)
  15099. wday (nth 6 tl))
  15100. (when (and (eq org-read-date-prefer-future 'time)
  15101. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15102. (equal day (nth 3 nowdecode))
  15103. (equal month (nth 4 nowdecode))
  15104. (equal year (nth 5 nowdecode))
  15105. (nth 2 tl)
  15106. (or (< (nth 2 tl) (nth 2 nowdecode))
  15107. (and (= (nth 2 tl) (nth 2 nowdecode))
  15108. (nth 1 tl)
  15109. (< (nth 1 tl) (nth 1 nowdecode)))))
  15110. (setq day (1+ day)
  15111. futurep t))
  15112. ;; Special date definitions below
  15113. (cond
  15114. (iso-week
  15115. ;; There was an iso week
  15116. (require 'cal-iso)
  15117. (setq futurep nil)
  15118. (setq year (or iso-year year)
  15119. day (or iso-weekday wday 1)
  15120. wday nil ; to make sure that the trigger below does not match
  15121. iso-date (calendar-gregorian-from-absolute
  15122. (calendar-absolute-from-iso
  15123. (list iso-week day year))))
  15124. ; FIXME: Should we also push ISO weeks into the future?
  15125. ; (when (and org-read-date-prefer-future
  15126. ; (not iso-year)
  15127. ; (< (calendar-absolute-from-gregorian iso-date)
  15128. ; (time-to-days (current-time))))
  15129. ; (setq year (1+ year)
  15130. ; iso-date (calendar-gregorian-from-absolute
  15131. ; (calendar-absolute-from-iso
  15132. ; (list iso-week day year)))))
  15133. (setq month (car iso-date)
  15134. year (nth 2 iso-date)
  15135. day (nth 1 iso-date)))
  15136. (deltan
  15137. (setq futurep nil)
  15138. (unless deltadef
  15139. (let ((now (decode-time (current-time))))
  15140. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15141. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15142. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15143. ((equal deltaw "m") (setq month (+ month deltan)))
  15144. ((equal deltaw "y") (setq year (+ year deltan)))))
  15145. ((and wday (not (nth 3 tl)))
  15146. ;; Weekday was given, but no day, so pick that day in the week
  15147. ;; on or after the derived date.
  15148. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15149. (unless (equal wday wday1)
  15150. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15151. (if (and (boundp 'org-time-was-given)
  15152. (nth 2 tl))
  15153. (setq org-time-was-given t))
  15154. (if (< year 100) (setq year (+ 2000 year)))
  15155. ;; Check of the date is representable
  15156. (if org-read-date-force-compatible-dates
  15157. (progn
  15158. (if (< year 1970)
  15159. (setq year 1970 org-read-date-analyze-forced-year t))
  15160. (if (> year 2037)
  15161. (setq year 2037 org-read-date-analyze-forced-year t)))
  15162. (condition-case nil
  15163. (ignore (encode-time second minute hour day month year))
  15164. (error
  15165. (setq year (nth 5 org-defdecode))
  15166. (setq org-read-date-analyze-forced-year t))))
  15167. (setq org-read-date-analyze-futurep futurep)
  15168. (list second minute hour day month year)))
  15169. (defvar parse-time-weekdays)
  15170. (defun org-read-date-get-relative (s today default)
  15171. "Check string S for special relative date string.
  15172. TODAY and DEFAULT are internal times, for today and for a default.
  15173. Return shift list (N what def-flag)
  15174. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15175. N is the number of WHATs to shift.
  15176. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15177. the DEFAULT date rather than TODAY."
  15178. (require 'parse-time)
  15179. (when (and
  15180. (string-match
  15181. (concat
  15182. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15183. "\\([0-9]+\\)?"
  15184. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15185. "\\([ \t]\\|$\\)") s)
  15186. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15187. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15188. (string-to-char (substring (match-string 1 s) -1))
  15189. ?+))
  15190. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15191. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15192. (what (if (match-end 3) (match-string 3 s) "d"))
  15193. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15194. (date (if rel default today))
  15195. (wday (nth 6 (decode-time date)))
  15196. delta)
  15197. (if wday1
  15198. (progn
  15199. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15200. (if (= delta 0) (setq delta 7))
  15201. (if (= dir ?-)
  15202. (progn
  15203. (setq delta (- delta 7))
  15204. (if (= delta 0) (setq delta -7))))
  15205. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15206. (list delta "d" rel))
  15207. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15208. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15209. "Turn a user-specified date into the internal representation.
  15210. The internal representation needed by the calendar is (month day year).
  15211. This is a wrapper to handle the brain-dead convention in calendar that
  15212. user function argument order change dependent on argument order."
  15213. (if (boundp 'calendar-date-style)
  15214. (cond
  15215. ((eq calendar-date-style 'american)
  15216. (list arg1 arg2 arg3))
  15217. ((eq calendar-date-style 'european)
  15218. (list arg2 arg1 arg3))
  15219. ((eq calendar-date-style 'iso)
  15220. (list arg2 arg3 arg1)))
  15221. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15222. (if (org-bound-and-true-p european-calendar-style)
  15223. (list arg2 arg1 arg3)
  15224. (list arg1 arg2 arg3)))))
  15225. (defun org-eval-in-calendar (form &optional keepdate)
  15226. "Eval FORM in the calendar window and return to current window.
  15227. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15228. otherwise stick to the current value of `org-ans2'."
  15229. (let ((sf (selected-frame))
  15230. (sw (selected-window)))
  15231. (select-window (get-buffer-window "*Calendar*" t))
  15232. (eval form)
  15233. (when (and (not keepdate) (calendar-cursor-to-date))
  15234. (let* ((date (calendar-cursor-to-date))
  15235. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15236. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15237. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15238. (select-window sw)
  15239. (org-select-frame-set-input-focus sf)))
  15240. (defun org-calendar-select ()
  15241. "Return to `org-read-date' with the date currently selected.
  15242. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15243. (interactive)
  15244. (when (calendar-cursor-to-date)
  15245. (let* ((date (calendar-cursor-to-date))
  15246. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15247. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15248. (if (active-minibuffer-window) (exit-minibuffer))))
  15249. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15250. "Insert a date stamp for the date given by the internal TIME.
  15251. WITH-HM means use the stamp format that includes the time of the day.
  15252. INACTIVE means use square brackets instead of angular ones, so that the
  15253. stamp will not contribute to the agenda.
  15254. PRE and POST are optional strings to be inserted before and after the
  15255. stamp.
  15256. The command returns the inserted time stamp."
  15257. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15258. stamp)
  15259. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15260. (insert-before-markers (or pre ""))
  15261. (when (listp extra)
  15262. (setq extra (car extra))
  15263. (if (and (stringp extra)
  15264. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15265. (setq extra (format "-%02d:%02d"
  15266. (string-to-number (match-string 1 extra))
  15267. (string-to-number (match-string 2 extra))))
  15268. (setq extra nil)))
  15269. (when extra
  15270. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15271. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15272. (insert-before-markers (or post ""))
  15273. (setq org-last-inserted-timestamp stamp)))
  15274. (defun org-toggle-time-stamp-overlays ()
  15275. "Toggle the use of custom time stamp formats."
  15276. (interactive)
  15277. (setq org-display-custom-times (not org-display-custom-times))
  15278. (unless org-display-custom-times
  15279. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15280. (while (setq p (next-single-property-change p 'display))
  15281. (if (and (get-text-property p 'display)
  15282. (eq (get-text-property p 'face) 'org-date))
  15283. (remove-text-properties
  15284. p (setq p (next-single-property-change p 'display))
  15285. '(display t))))
  15286. (set-buffer-modified-p bmp)))
  15287. (if (featurep 'xemacs)
  15288. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15289. (org-restart-font-lock)
  15290. (setq org-table-may-need-update t)
  15291. (if org-display-custom-times
  15292. (message "Time stamps are overlaid with custom format")
  15293. (message "Time stamp overlays removed")))
  15294. (defun org-display-custom-time (beg end)
  15295. "Overlay modified time stamp format over timestamp between BEG and END."
  15296. (let* ((ts (buffer-substring beg end))
  15297. t1 w1 with-hm tf time str w2 (off 0))
  15298. (save-match-data
  15299. (setq t1 (org-parse-time-string ts t))
  15300. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15301. (setq off (- (match-end 0) (match-beginning 0)))))
  15302. (setq end (- end off))
  15303. (setq w1 (- end beg)
  15304. with-hm (and (nth 1 t1) (nth 2 t1))
  15305. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15306. time (org-fix-decoded-time t1)
  15307. str (org-add-props
  15308. (format-time-string
  15309. (substring tf 1 -1) (apply 'encode-time time))
  15310. nil 'mouse-face 'highlight)
  15311. w2 (length str))
  15312. (if (not (= w2 w1))
  15313. (add-text-properties (1+ beg) (+ 2 beg)
  15314. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15315. (if (featurep 'xemacs)
  15316. (progn
  15317. (put-text-property beg end 'invisible t)
  15318. (put-text-property beg end 'end-glyph (make-glyph str)))
  15319. (put-text-property beg end 'display str))))
  15320. (defun org-translate-time (string)
  15321. "Translate all timestamps in STRING to custom format.
  15322. But do this only if the variable `org-display-custom-times' is set."
  15323. (when org-display-custom-times
  15324. (save-match-data
  15325. (let* ((start 0)
  15326. (re org-ts-regexp-both)
  15327. t1 with-hm inactive tf time str beg end)
  15328. (while (setq start (string-match re string start))
  15329. (setq beg (match-beginning 0)
  15330. end (match-end 0)
  15331. t1 (save-match-data
  15332. (org-parse-time-string (substring string beg end) t))
  15333. with-hm (and (nth 1 t1) (nth 2 t1))
  15334. inactive (equal (substring string beg (1+ beg)) "[")
  15335. tf (funcall (if with-hm 'cdr 'car)
  15336. org-time-stamp-custom-formats)
  15337. time (org-fix-decoded-time t1)
  15338. str (format-time-string
  15339. (concat
  15340. (if inactive "[" "<") (substring tf 1 -1)
  15341. (if inactive "]" ">"))
  15342. (apply 'encode-time time))
  15343. string (replace-match str t t string)
  15344. start (+ start (length str)))))))
  15345. string)
  15346. (defun org-fix-decoded-time (time)
  15347. "Set 0 instead of nil for the first 6 elements of time.
  15348. Don't touch the rest."
  15349. (let ((n 0))
  15350. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15351. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15352. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15353. "Difference between TIMESTAMP-STRING and now in days.
  15354. If SECONDS is non-nil, return the difference in seconds."
  15355. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15356. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15357. (funcall fdiff (current-time)))))
  15358. (defun org-deadline-close (timestamp-string &optional ndays)
  15359. "Is the time in TIMESTAMP-STRING close to the current date?"
  15360. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15361. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15362. (not (org-entry-is-done-p))))
  15363. (defun org-get-wdays (ts &optional delay zero-delay)
  15364. "Get the deadline lead time appropriate for timestring TS.
  15365. When DELAY is non-nil, get the delay time for scheduled items
  15366. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15367. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15368. don't try to find the delay cookie in the scheduled timestamp."
  15369. (let ((tv (if delay org-scheduled-delay-days
  15370. org-deadline-warning-days)))
  15371. (cond
  15372. ((or (and delay (< tv 0))
  15373. (and delay zero-delay (<= tv 0))
  15374. (and (not delay) (<= tv 0)))
  15375. ;; Enforce this value no matter what
  15376. (- tv))
  15377. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15378. ;; lead time is specified.
  15379. (floor (* (string-to-number (match-string 1 ts))
  15380. (cdr (assoc (match-string 2 ts)
  15381. '(("d" . 1) ("w" . 7)
  15382. ("m" . 30.4) ("y" . 365.25)
  15383. ("h" . 0.041667)))))))
  15384. ;; go for the default.
  15385. (t tv))))
  15386. (defun org-calendar-select-mouse (ev)
  15387. "Return to `org-read-date' with the date currently selected.
  15388. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15389. (interactive "e")
  15390. (mouse-set-point ev)
  15391. (when (calendar-cursor-to-date)
  15392. (let* ((date (calendar-cursor-to-date))
  15393. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15394. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15395. (if (active-minibuffer-window) (exit-minibuffer))))
  15396. (defun org-check-deadlines (ndays)
  15397. "Check if there are any deadlines due or past due.
  15398. A deadline is considered due if it happens within `org-deadline-warning-days'
  15399. days from today's date. If the deadline appears in an entry marked DONE,
  15400. it is not shown. The prefix arg NDAYS can be used to test that many
  15401. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15402. (interactive "P")
  15403. (let* ((org-warn-days
  15404. (cond
  15405. ((equal ndays '(4)) 100000)
  15406. (ndays (prefix-numeric-value ndays))
  15407. (t (abs org-deadline-warning-days))))
  15408. (case-fold-search nil)
  15409. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15410. (callback
  15411. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15412. (message "%d deadlines past-due or due within %d days"
  15413. (org-occur regexp nil callback)
  15414. org-warn-days)))
  15415. (defsubst org-re-timestamp (type)
  15416. "Return a regexp for timestamp TYPE.
  15417. Allowed values for TYPE are:
  15418. all: all timestamps
  15419. active: only active timestamps (<...>)
  15420. inactive: only inactive timestamps ([...])
  15421. scheduled: only scheduled timestamps
  15422. deadline: only deadline timestamps
  15423. closed: only closed time-stamps
  15424. When TYPE is nil, fall back on returning a regexp that matches
  15425. both scheduled and deadline timestamps."
  15426. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15427. ((eq type 'active) org-ts-regexp)
  15428. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15429. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15430. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15431. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15432. ((eq type 'scheduled-or-deadline)
  15433. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15434. (defun org-check-before-date (date)
  15435. "Check if there are deadlines or scheduled entries before DATE."
  15436. (interactive (list (org-read-date)))
  15437. (let ((case-fold-search nil)
  15438. (regexp (org-re-timestamp org-ts-type))
  15439. (callback
  15440. (lambda () (time-less-p
  15441. (org-time-string-to-time (match-string 1))
  15442. (org-time-string-to-time date)))))
  15443. (message "%d entries before %s"
  15444. (org-occur regexp nil callback) date)))
  15445. (defun org-check-after-date (date)
  15446. "Check if there are deadlines or scheduled entries after DATE."
  15447. (interactive (list (org-read-date)))
  15448. (let ((case-fold-search nil)
  15449. (regexp (org-re-timestamp org-ts-type))
  15450. (callback
  15451. (lambda () (not
  15452. (time-less-p
  15453. (org-time-string-to-time (match-string 1))
  15454. (org-time-string-to-time date))))))
  15455. (message "%d entries after %s"
  15456. (org-occur regexp nil callback) date)))
  15457. (defun org-check-dates-range (start-date end-date)
  15458. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15459. (interactive (list (org-read-date nil nil nil "Range starts")
  15460. (org-read-date nil nil nil "Range end")))
  15461. (let ((case-fold-search nil)
  15462. (regexp (org-re-timestamp org-ts-type))
  15463. (callback
  15464. (lambda ()
  15465. (let ((match (match-string 1)))
  15466. (and
  15467. (not (time-less-p
  15468. (org-time-string-to-time match)
  15469. (org-time-string-to-time start-date)))
  15470. (time-less-p
  15471. (org-time-string-to-time match)
  15472. (org-time-string-to-time end-date)))))))
  15473. (message "%d entries between %s and %s"
  15474. (org-occur regexp nil callback) start-date end-date)))
  15475. (defun org-evaluate-time-range (&optional to-buffer)
  15476. "Evaluate a time range by computing the difference between start and end.
  15477. Normally the result is just printed in the echo area, but with prefix arg
  15478. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15479. If the time range is actually in a table, the result is inserted into the
  15480. next column.
  15481. For time difference computation, a year is assumed to be exactly 365
  15482. days in order to avoid rounding problems."
  15483. (interactive "P")
  15484. (or
  15485. (org-clock-update-time-maybe)
  15486. (save-excursion
  15487. (unless (org-at-date-range-p t)
  15488. (goto-char (point-at-bol))
  15489. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15490. (if (not (org-at-date-range-p t))
  15491. (user-error "Not at a time-stamp range, and none found in current line")))
  15492. (let* ((ts1 (match-string 1))
  15493. (ts2 (match-string 2))
  15494. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15495. (match-end (match-end 0))
  15496. (time1 (org-time-string-to-time ts1))
  15497. (time2 (org-time-string-to-time ts2))
  15498. (t1 (org-float-time time1))
  15499. (t2 (org-float-time time2))
  15500. (diff (abs (- t2 t1)))
  15501. (negative (< (- t2 t1) 0))
  15502. ;; (ys (floor (* 365 24 60 60)))
  15503. (ds (* 24 60 60))
  15504. (hs (* 60 60))
  15505. (fy "%dy %dd %02d:%02d")
  15506. (fy1 "%dy %dd")
  15507. (fd "%dd %02d:%02d")
  15508. (fd1 "%dd")
  15509. (fh "%02d:%02d")
  15510. y d h m align)
  15511. (if havetime
  15512. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15513. y 0
  15514. d (floor (/ diff ds)) diff (mod diff ds)
  15515. h (floor (/ diff hs)) diff (mod diff hs)
  15516. m (floor (/ diff 60)))
  15517. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15518. y 0
  15519. d (floor (+ (/ diff ds) 0.5))
  15520. h 0 m 0))
  15521. (if (not to-buffer)
  15522. (message "%s" (org-make-tdiff-string y d h m))
  15523. (if (org-at-table-p)
  15524. (progn
  15525. (goto-char match-end)
  15526. (setq align t)
  15527. (and (looking-at " *|") (goto-char (match-end 0))))
  15528. (goto-char match-end))
  15529. (if (looking-at
  15530. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15531. (replace-match ""))
  15532. (if negative (insert " -"))
  15533. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15534. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15535. (insert " " (format fh h m))))
  15536. (if align (org-table-align))
  15537. (message "Time difference inserted")))))
  15538. (defun org-make-tdiff-string (y d h m)
  15539. (let ((fmt "")
  15540. (l nil))
  15541. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15542. l (push y l)))
  15543. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15544. l (push d l)))
  15545. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15546. l (push h l)))
  15547. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15548. l (push m l)))
  15549. (apply 'format fmt (nreverse l))))
  15550. (defun org-time-string-to-time (s &optional buffer pos)
  15551. "Convert a timestamp string into internal time."
  15552. (condition-case errdata
  15553. (apply 'encode-time (org-parse-time-string s))
  15554. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15555. s (if (not (and buffer pos))
  15556. ""
  15557. (format " at %d in buffer `%s'" pos buffer))
  15558. (cdr errdata)))))
  15559. (defun org-time-string-to-seconds (s)
  15560. "Convert a timestamp string to a number of seconds."
  15561. (org-float-time (org-time-string-to-time s)))
  15562. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15563. "Convert a time stamp to an absolute day number.
  15564. If there is a specifier for a cyclic time stamp, get the closest
  15565. date to DAYNR.
  15566. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15567. The variable `date' is bound by the calendar when this is called."
  15568. (cond
  15569. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15570. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15571. daynr
  15572. (+ daynr 1000)))
  15573. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15574. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15575. (time-to-days (current-time))) (match-string 0 s)
  15576. prefer show-all))
  15577. (t (time-to-days
  15578. (condition-case errdata
  15579. (apply 'encode-time (org-parse-time-string s))
  15580. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15581. s (if (not (and buffer pos))
  15582. ""
  15583. (format " at %d in buffer `%s'" pos buffer))
  15584. (cdr errdata))))))))
  15585. (defun org-days-to-iso-week (days)
  15586. "Return the iso week number."
  15587. (require 'cal-iso)
  15588. (car (calendar-iso-from-absolute days)))
  15589. (defun org-small-year-to-year (year)
  15590. "Convert 2-digit years into 4-digit years.
  15591. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15592. The year 2000 cannot be abbreviated. Any year larger than 99
  15593. is returned unchanged."
  15594. (if (< year 38)
  15595. (setq year (+ 2000 year))
  15596. (if (< year 100)
  15597. (setq year (+ 1900 year))))
  15598. year)
  15599. (defun org-time-from-absolute (d)
  15600. "Return the time corresponding to date D.
  15601. D may be an absolute day number, or a calendar-type list (month day year)."
  15602. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15603. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15604. (defun org-calendar-holiday ()
  15605. "List of holidays, for Diary display in Org-mode."
  15606. (require 'holidays)
  15607. (let ((hl (funcall
  15608. (if (fboundp 'calendar-check-holidays)
  15609. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15610. (if hl (mapconcat 'identity hl "; "))))
  15611. (defun org-diary-sexp-entry (sexp entry date)
  15612. "Process a SEXP diary ENTRY for DATE."
  15613. (require 'diary-lib)
  15614. (let ((result (if calendar-debug-sexp
  15615. (let ((stack-trace-on-error t))
  15616. (eval (car (read-from-string sexp))))
  15617. (condition-case nil
  15618. (eval (car (read-from-string sexp)))
  15619. (error
  15620. (beep)
  15621. (message "Bad sexp at line %d in %s: %s"
  15622. (org-current-line)
  15623. (buffer-file-name) sexp)
  15624. (sleep-for 2))))))
  15625. (cond ((stringp result) (split-string result "; "))
  15626. ((and (consp result)
  15627. (not (consp (cdr result)))
  15628. (stringp (cdr result))) (cdr result))
  15629. ((and (consp result)
  15630. (stringp (car result))) result)
  15631. (result entry))))
  15632. (defun org-diary-to-ical-string (frombuf)
  15633. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15634. This uses the icalendar.el library."
  15635. (let* ((tmpdir (if (featurep 'xemacs)
  15636. (temp-directory)
  15637. temporary-file-directory))
  15638. (tmpfile (make-temp-name
  15639. (expand-file-name "orgics" tmpdir)))
  15640. buf rtn b e)
  15641. (with-current-buffer frombuf
  15642. (icalendar-export-region (point-min) (point-max) tmpfile)
  15643. (setq buf (find-buffer-visiting tmpfile))
  15644. (set-buffer buf)
  15645. (goto-char (point-min))
  15646. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15647. (setq b (match-beginning 0)))
  15648. (goto-char (point-max))
  15649. (if (re-search-backward "^END:VEVENT" nil t)
  15650. (setq e (match-end 0)))
  15651. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15652. (kill-buffer buf)
  15653. (delete-file tmpfile)
  15654. rtn))
  15655. (defun org-closest-date (start current change prefer show-all)
  15656. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15657. When PREFER is `past', return a date that is either CURRENT or past.
  15658. When PREFER is `future', return a date that is either CURRENT or future.
  15659. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15660. ;; Make the proper lists from the dates
  15661. (catch 'exit
  15662. (let ((a1 '(("h" . hour)
  15663. ("d" . day)
  15664. ("w" . week)
  15665. ("m" . month)
  15666. ("y" . year)))
  15667. (shour (nth 2 (org-parse-time-string start)))
  15668. dn dw sday cday n1 n2 n0
  15669. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15670. (setq start (org-date-to-gregorian start)
  15671. current (org-date-to-gregorian
  15672. (if show-all
  15673. current
  15674. (time-to-days (current-time))))
  15675. sday (calendar-absolute-from-gregorian start)
  15676. cday (calendar-absolute-from-gregorian current))
  15677. (if (<= cday sday) (throw 'exit sday))
  15678. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15679. (setq dn (string-to-number (match-string 1 change))
  15680. dw (cdr (assoc (match-string 2 change) a1)))
  15681. (user-error "Invalid change specifier: %s" change))
  15682. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15683. (cond
  15684. ((eq dw 'hour)
  15685. (let ((missing-hours
  15686. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15687. dn)))
  15688. (setq n1 (if (zerop missing-hours) cday
  15689. (- cday (1+ (floor (/ missing-hours 24)))))
  15690. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15691. ((eq dw 'day)
  15692. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15693. n2 (+ n1 dn)))
  15694. ((eq dw 'year)
  15695. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15696. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15697. (setq date1 (list m d y1)
  15698. n1 (calendar-absolute-from-gregorian date1)
  15699. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15700. n2 (calendar-absolute-from-gregorian date2)))
  15701. ((eq dw 'month)
  15702. ;; approx number of month between the two dates
  15703. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15704. ;; How often does dn fit in there?
  15705. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15706. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15707. m (+ m nm)
  15708. ny (floor (/ m 12))
  15709. y (+ y ny)
  15710. m (- m (* ny 12)))
  15711. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15712. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15713. (setq m2 (+ m dn) y2 y)
  15714. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15715. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15716. (while (<= n2 cday)
  15717. (setq n1 n2 m m2 y y2)
  15718. (setq m2 (+ m dn) y2 y)
  15719. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15720. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15721. ;; Make sure n1 is the earlier date
  15722. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15723. (if show-all
  15724. (cond
  15725. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15726. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15727. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15728. (cond
  15729. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15730. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15731. (t (if (= cday n1) n1 n2)))))))
  15732. (defun org-date-to-gregorian (date)
  15733. "Turn any specification of DATE into a Gregorian date for the calendar."
  15734. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15735. ((and (listp date) (= (length date) 3)) date)
  15736. ((stringp date)
  15737. (setq date (org-parse-time-string date))
  15738. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15739. ((listp date)
  15740. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15741. (defun org-parse-time-string (s &optional nodefault)
  15742. "Parse the standard Org-mode time string.
  15743. This should be a lot faster than the normal `parse-time-string'.
  15744. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15745. hour and minute fields will be nil if not given."
  15746. (cond ((string-match org-ts-regexp0 s)
  15747. (list 0
  15748. (if (or (match-beginning 8) (not nodefault))
  15749. (string-to-number (or (match-string 8 s) "0")))
  15750. (if (or (match-beginning 7) (not nodefault))
  15751. (string-to-number (or (match-string 7 s) "0")))
  15752. (string-to-number (match-string 4 s))
  15753. (string-to-number (match-string 3 s))
  15754. (string-to-number (match-string 2 s))
  15755. nil nil nil))
  15756. ((string-match "^<[^>]+>$" s)
  15757. (decode-time (seconds-to-time (org-matcher-time s))))
  15758. (t (error "Not a standard Org-mode time string: %s" s))))
  15759. (defun org-timestamp-up (&optional arg)
  15760. "Increase the date item at the cursor by one.
  15761. If the cursor is on the year, change the year. If it is on the month,
  15762. the day or the time, change that.
  15763. With prefix ARG, change by that many units."
  15764. (interactive "p")
  15765. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15766. (defun org-timestamp-down (&optional arg)
  15767. "Decrease the date item at the cursor by one.
  15768. If the cursor is on the year, change the year. If it is on the month,
  15769. the day or the time, change that.
  15770. With prefix ARG, change by that many units."
  15771. (interactive "p")
  15772. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15773. (defun org-timestamp-up-day (&optional arg)
  15774. "Increase the date in the time stamp by one day.
  15775. With prefix ARG, change that many days."
  15776. (interactive "p")
  15777. (if (and (not (org-at-timestamp-p t))
  15778. (org-at-heading-p))
  15779. (org-todo 'up)
  15780. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15781. (defun org-timestamp-down-day (&optional arg)
  15782. "Decrease the date in the time stamp by one day.
  15783. With prefix ARG, change that many days."
  15784. (interactive "p")
  15785. (if (and (not (org-at-timestamp-p t))
  15786. (org-at-heading-p))
  15787. (org-todo 'down)
  15788. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15789. (defun org-at-timestamp-p (&optional inactive-ok)
  15790. "Determine if the cursor is in or at a timestamp."
  15791. (interactive)
  15792. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15793. (pos (point))
  15794. (ans (or (looking-at tsr)
  15795. (save-excursion
  15796. (skip-chars-backward "^[<\n\r\t")
  15797. (if (> (point) (point-min)) (backward-char 1))
  15798. (and (looking-at tsr)
  15799. (> (- (match-end 0) pos) -1))))))
  15800. (and ans
  15801. (boundp 'org-ts-what)
  15802. (setq org-ts-what
  15803. (cond
  15804. ((= pos (match-beginning 0)) 'bracket)
  15805. ;; Point is considered to be "on the bracket" whether
  15806. ;; it's really on it or right after it.
  15807. ((= pos (1- (match-end 0))) 'bracket)
  15808. ((= pos (match-end 0)) 'after)
  15809. ((org-pos-in-match-range pos 2) 'year)
  15810. ((org-pos-in-match-range pos 3) 'month)
  15811. ((org-pos-in-match-range pos 7) 'hour)
  15812. ((org-pos-in-match-range pos 8) 'minute)
  15813. ((or (org-pos-in-match-range pos 4)
  15814. (org-pos-in-match-range pos 5)) 'day)
  15815. ((and (> pos (or (match-end 8) (match-end 5)))
  15816. (< pos (match-end 0)))
  15817. (- pos (or (match-end 8) (match-end 5))))
  15818. (t 'day))))
  15819. ans))
  15820. (defun org-toggle-timestamp-type ()
  15821. "Toggle the type (<active> or [inactive]) of a time stamp."
  15822. (interactive)
  15823. (when (org-at-timestamp-p t)
  15824. (let ((beg (match-beginning 0)) (end (match-end 0))
  15825. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15826. (save-excursion
  15827. (goto-char beg)
  15828. (while (re-search-forward "[][<>]" end t)
  15829. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15830. t t)))
  15831. (message "Timestamp is now %sactive"
  15832. (if (equal (char-after beg) ?<) "" "in")))))
  15833. (defun org-at-clock-log-p nil
  15834. "Is the cursor on the clock log line?"
  15835. (save-excursion
  15836. (move-beginning-of-line 1)
  15837. (looking-at "^[ \t]*CLOCK:")))
  15838. (defvar org-clock-history) ; defined in org-clock.el
  15839. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15840. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15841. "Change the date in the time stamp at point.
  15842. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15843. `year', `minute', `second'. If WHAT is not given, the cursor position
  15844. in the timestamp determines what will be changed.
  15845. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15846. (let ((origin (point)) origin-cat
  15847. with-hm inactive
  15848. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15849. org-ts-what
  15850. extra rem
  15851. ts time time0 fixnext clrgx)
  15852. (if (not (org-at-timestamp-p t))
  15853. (user-error "Not at a timestamp"))
  15854. (if (and (not what) (eq org-ts-what 'bracket))
  15855. (org-toggle-timestamp-type)
  15856. ;; Point isn't on brackets. Remember the part of the time-stamp
  15857. ;; the point was in. Indeed, size of time-stamps may change,
  15858. ;; but point must be kept in the same category nonetheless.
  15859. (setq origin-cat org-ts-what)
  15860. (if (and (not what) (not (eq org-ts-what 'day))
  15861. org-display-custom-times
  15862. (get-text-property (point) 'display)
  15863. (not (get-text-property (1- (point)) 'display)))
  15864. (setq org-ts-what 'day))
  15865. (setq org-ts-what (or what org-ts-what)
  15866. inactive (= (char-after (match-beginning 0)) ?\[)
  15867. ts (match-string 0))
  15868. (replace-match "")
  15869. (when (string-match
  15870. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15871. ts)
  15872. (setq extra (match-string 1 ts))
  15873. (if suppress-tmp-delay
  15874. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15875. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15876. (setq with-hm t))
  15877. (setq time0 (org-parse-time-string ts))
  15878. (when (and updown
  15879. (eq org-ts-what 'minute)
  15880. (not current-prefix-arg))
  15881. ;; This looks like s-up and s-down. Change by one rounding step.
  15882. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15883. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15884. (setcar (cdr time0) (+ (nth 1 time0)
  15885. (if (> n 0) (- rem) (- dm rem))))))
  15886. (setq time
  15887. (encode-time (or (car time0) 0)
  15888. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15889. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15890. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15891. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15892. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15893. (nthcdr 6 time0)))
  15894. (when (and (member org-ts-what '(hour minute))
  15895. extra
  15896. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15897. (setq extra (org-modify-ts-extra
  15898. extra
  15899. (if (eq org-ts-what 'hour) 2 5)
  15900. n dm)))
  15901. (when (integerp org-ts-what)
  15902. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15903. (if (eq what 'calendar)
  15904. (let ((cal-date (org-get-date-from-calendar)))
  15905. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15906. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15907. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15908. (setcar time0 (or (car time0) 0))
  15909. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15910. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15911. (setq time (apply 'encode-time time0))))
  15912. ;; Insert the new time-stamp, and ensure point stays in the same
  15913. ;; category as before (i.e. not after the last position in that
  15914. ;; category).
  15915. (let ((pos (point)))
  15916. ;; Stay before inserted string. `save-excursion' is of no use.
  15917. (setq org-last-changed-timestamp
  15918. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15919. (goto-char pos))
  15920. (save-match-data
  15921. (looking-at org-ts-regexp3)
  15922. (goto-char (cond
  15923. ;; `day' category ends before `hour' if any, or at
  15924. ;; the end of the day name.
  15925. ((eq origin-cat 'day)
  15926. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15927. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15928. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15929. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15930. ;; `year' and `month' have both fixed size: point
  15931. ;; couldn't have moved into another part.
  15932. (t origin))))
  15933. ;; Update clock if on a CLOCK line.
  15934. (org-clock-update-time-maybe)
  15935. ;; Maybe adjust the closest clock in `org-clock-history'
  15936. (when org-clock-adjust-closest
  15937. (if (not (and (org-at-clock-log-p)
  15938. (< 1 (length (delq nil (mapcar 'marker-position
  15939. org-clock-history))))))
  15940. (message "No clock to adjust")
  15941. (cond ((save-excursion ; fix previous clock?
  15942. (re-search-backward org-ts-regexp0 nil t)
  15943. (org-looking-back (concat org-clock-string " \\[")))
  15944. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15945. ((save-excursion ; fix next clock?
  15946. (re-search-backward org-ts-regexp0 nil t)
  15947. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15948. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15949. (save-window-excursion
  15950. ;; Find closest clock to point, adjust the previous/next one in history
  15951. (let* ((p (save-excursion (org-back-to-heading t)))
  15952. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15953. (clfixnth
  15954. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15955. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15956. (if (not clfixpos)
  15957. (message "No clock to adjust")
  15958. (save-excursion
  15959. (org-goto-marker-or-bmk clfixpos)
  15960. (org-show-subtree)
  15961. (when (re-search-forward clrgx nil t)
  15962. (goto-char (match-beginning 1))
  15963. (let (org-clock-adjust-closest)
  15964. (org-timestamp-change n org-ts-what updown))
  15965. (message "Clock adjusted in %s for heading: %s"
  15966. (file-name-nondirectory (buffer-file-name))
  15967. (org-get-heading t t)))))))))
  15968. ;; Try to recenter the calendar window, if any.
  15969. (if (and org-calendar-follow-timestamp-change
  15970. (get-buffer-window "*Calendar*" t)
  15971. (memq org-ts-what '(day month year)))
  15972. (org-recenter-calendar (time-to-days time))))))
  15973. (defun org-modify-ts-extra (s pos n dm)
  15974. "Change the different parts of the lead-time and repeat fields in timestamp."
  15975. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15976. ng h m new rem)
  15977. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15978. (cond
  15979. ((or (org-pos-in-match-range pos 2)
  15980. (org-pos-in-match-range pos 3))
  15981. (setq m (string-to-number (match-string 3 s))
  15982. h (string-to-number (match-string 2 s)))
  15983. (if (org-pos-in-match-range pos 2)
  15984. (setq h (+ h n))
  15985. (setq n (* dm (org-no-warnings (signum n))))
  15986. (when (not (= 0 (setq rem (% m dm))))
  15987. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15988. (setq m (+ m n)))
  15989. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15990. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15991. (setq h (min 24 (max 0 h)))
  15992. (setq ng 1 new (format "-%02d:%02d" h m)))
  15993. ((org-pos-in-match-range pos 6)
  15994. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15995. ((org-pos-in-match-range pos 5)
  15996. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15997. ((org-pos-in-match-range pos 9)
  15998. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15999. ((org-pos-in-match-range pos 8)
  16000. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16001. (when ng
  16002. (setq s (concat
  16003. (substring s 0 (match-beginning ng))
  16004. new
  16005. (substring s (match-end ng))))))
  16006. s))
  16007. (defun org-recenter-calendar (date)
  16008. "If the calendar is visible, recenter it to DATE."
  16009. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16010. (when cwin
  16011. (let ((calendar-move-hook nil))
  16012. (with-selected-window cwin
  16013. (calendar-goto-date (if (listp date) date
  16014. (calendar-gregorian-from-absolute date))))))))
  16015. (defun org-goto-calendar (&optional arg)
  16016. "Go to the Emacs calendar at the current date.
  16017. If there is a time stamp in the current line, go to that date.
  16018. A prefix ARG can be used to force the current date."
  16019. (interactive "P")
  16020. (let ((tsr org-ts-regexp) diff
  16021. (calendar-move-hook nil)
  16022. (calendar-view-holidays-initially-flag nil)
  16023. (calendar-view-diary-initially-flag nil))
  16024. (if (or (org-at-timestamp-p)
  16025. (save-excursion
  16026. (beginning-of-line 1)
  16027. (looking-at (concat ".*" tsr))))
  16028. (let ((d1 (time-to-days (current-time)))
  16029. (d2 (time-to-days
  16030. (org-time-string-to-time (match-string 1)))))
  16031. (setq diff (- d2 d1))))
  16032. (calendar)
  16033. (calendar-goto-today)
  16034. (if (and diff (not arg)) (calendar-forward-day diff))))
  16035. (defun org-get-date-from-calendar ()
  16036. "Return a list (month day year) of date at point in calendar."
  16037. (with-current-buffer "*Calendar*"
  16038. (save-match-data
  16039. (calendar-cursor-to-date))))
  16040. (defun org-date-from-calendar ()
  16041. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16042. If there is already a time stamp at the cursor position, update it."
  16043. (interactive)
  16044. (if (org-at-timestamp-p t)
  16045. (org-timestamp-change 0 'calendar)
  16046. (let ((cal-date (org-get-date-from-calendar)))
  16047. (org-insert-time-stamp
  16048. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16049. (defcustom org-effort-durations
  16050. `(("h" . 60)
  16051. ("d" . ,(* 60 8))
  16052. ("w" . ,(* 60 8 5))
  16053. ("m" . ,(* 60 8 5 4))
  16054. ("y" . ,(* 60 8 5 40)))
  16055. "Conversion factor to minutes for an effort modifier.
  16056. Each entry has the form (MODIFIER . MINUTES).
  16057. In an effort string, a number followed by MODIFIER is multiplied
  16058. by the specified number of MINUTES to obtain an effort in
  16059. minutes.
  16060. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16061. effort string \"2hours\" is equivalent to 120 minutes."
  16062. :group 'org-agenda
  16063. :version "24.1"
  16064. :type '(alist :key-type (string :tag "Modifier")
  16065. :value-type (number :tag "Minutes")))
  16066. (defun org-minutes-to-clocksum-string (m)
  16067. "Format number of minutes as a clocksum string.
  16068. The format is determined by `org-time-clocksum-format',
  16069. `org-time-clocksum-use-fractional' and
  16070. `org-time-clocksum-fractional-format' and
  16071. `org-time-clocksum-use-effort-durations'."
  16072. (let ((clocksum "")
  16073. (m (round m)) ; Don't allow fractions of minutes
  16074. h d w mo y fmt n)
  16075. (setq h (if org-time-clocksum-use-effort-durations
  16076. (cdr (assoc "h" org-effort-durations)) 60)
  16077. d (if org-time-clocksum-use-effort-durations
  16078. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16079. w (if org-time-clocksum-use-effort-durations
  16080. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16081. mo (if org-time-clocksum-use-effort-durations
  16082. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16083. y (if org-time-clocksum-use-effort-durations
  16084. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16085. ;; fractional format
  16086. (if org-time-clocksum-use-fractional
  16087. (cond
  16088. ;; single format string
  16089. ((stringp org-time-clocksum-fractional-format)
  16090. (format org-time-clocksum-fractional-format (/ m (float h))))
  16091. ;; choice of fractional formats for different time units
  16092. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16093. (> (/ (truncate m) (* y d h)) 0))
  16094. (format fmt (/ m (* y d (float h)))))
  16095. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16096. (> (/ (truncate m) (* mo d h)) 0))
  16097. (format fmt (/ m (* mo d (float h)))))
  16098. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16099. (> (/ (truncate m) (* w d h)) 0))
  16100. (format fmt (/ m (* w d (float h)))))
  16101. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16102. (> (/ (truncate m) (* d h)) 0))
  16103. (format fmt (/ m (* d (float h)))))
  16104. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16105. (> (/ (truncate m) h) 0))
  16106. (format fmt (/ m (float h))))
  16107. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16108. (format fmt m))
  16109. ;; fall back to smallest time unit with a format
  16110. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16111. (format fmt (/ m (float h))))
  16112. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16113. (format fmt (/ m (* d (float h)))))
  16114. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16115. (format fmt (/ m (* w d (float h)))))
  16116. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16117. (format fmt (/ m (* mo d (float h)))))
  16118. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16119. (format fmt (/ m (* y d (float h))))))
  16120. ;; standard (non-fractional) format, with single format string
  16121. (if (stringp org-time-clocksum-format)
  16122. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16123. ;; separate formats components
  16124. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16125. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16126. (plist-get org-time-clocksum-format :require-years))
  16127. (setq clocksum (concat clocksum (format fmt n))
  16128. m (- m (* n y d h))))
  16129. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16130. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16131. (plist-get org-time-clocksum-format :require-months))
  16132. (setq clocksum (concat clocksum (format fmt n))
  16133. m (- m (* n mo d h))))
  16134. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16135. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16136. (plist-get org-time-clocksum-format :require-weeks))
  16137. (setq clocksum (concat clocksum (format fmt n))
  16138. m (- m (* n w d h))))
  16139. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16140. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16141. (plist-get org-time-clocksum-format :require-days))
  16142. (setq clocksum (concat clocksum (format fmt n))
  16143. m (- m (* n d h))))
  16144. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16145. (or (> (setq n (/ (truncate m) h)) 0)
  16146. (plist-get org-time-clocksum-format :require-hours))
  16147. (setq clocksum (concat clocksum (format fmt n))
  16148. m (- m (* n h))))
  16149. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16150. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16151. (setq clocksum (concat clocksum (format fmt m))))
  16152. ;; return formatted time duration
  16153. clocksum))))
  16154. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16155. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16156. "Org mode version 8.0")
  16157. (defun org-hours-to-clocksum-string (n)
  16158. (org-minutes-to-clocksum-string (* n 60)))
  16159. (defun org-hh:mm-string-to-minutes (s)
  16160. "Convert a string H:MM to a number of minutes.
  16161. If the string is just a number, interpret it as minutes.
  16162. In fact, the first hh:mm or number in the string will be taken,
  16163. there can be extra stuff in the string.
  16164. If no number is found, the return value is 0."
  16165. (cond
  16166. ((integerp s) s)
  16167. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16168. (+ (* (string-to-number (match-string 1 s)) 60)
  16169. (string-to-number (match-string 2 s))))
  16170. ((string-match "\\([0-9]+\\)" s)
  16171. (string-to-number (match-string 1 s)))
  16172. (t 0)))
  16173. (defcustom org-image-actual-width t
  16174. "Should we use the actual width of images when inlining them?
  16175. When set to `t', always use the image width.
  16176. When set to a number, use imagemagick (when available) to set
  16177. the image's width to this value.
  16178. When set to a number in a list, try to get the width from any
  16179. #+ATTR.* keyword if it matches a width specification like
  16180. #+ATTR_HTML: :width 300px
  16181. and fall back on that number if none is found.
  16182. When set to nil, try to get the width from an #+ATTR.* keyword
  16183. and fall back on the original width if none is found.
  16184. This requires Emacs >= 24.1, build with imagemagick support."
  16185. :group 'org-appearance
  16186. :version "24.4"
  16187. :package-version '(Org . "8.0")
  16188. :type '(choice
  16189. (const :tag "Use the image width" t)
  16190. (integer :tag "Use a number of pixels")
  16191. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16192. (const :tag "Use #+ATTR* or don't resize" nil)))
  16193. (defcustom org-agenda-inhibit-startup nil
  16194. "Inhibit startup when preparing agenda buffers.
  16195. When this variable is `t' (the default), the initialization of
  16196. the Org agenda buffers is inhibited: e.g. the visibility state
  16197. is not set, the tables are not re-aligned, etc."
  16198. :type 'boolean
  16199. :version "24.3"
  16200. :group 'org-agenda)
  16201. (defcustom org-agenda-ignore-drawer-properties nil
  16202. "Avoid updating text properties when building the agenda.
  16203. Properties are used to prepare buffers for effort estimates, appointments,
  16204. and subtree-local categories.
  16205. If you don't use these in the agenda, you can add them to this list and
  16206. agenda building will be a bit faster.
  16207. The value is a list, with zero or more of the symbols `effort', `appt',
  16208. or `category'."
  16209. :type '(set :greedy t
  16210. (const effort)
  16211. (const appt)
  16212. (const category))
  16213. :version "24.3"
  16214. :group 'org-agenda)
  16215. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16216. "Convert a duration string S to minutes.
  16217. A bare number is interpreted as minutes, modifiers can be set by
  16218. customizing `org-effort-durations' (which see).
  16219. Entries containing a colon are interpreted as H:MM by
  16220. `org-hh:mm-string-to-minutes'."
  16221. (let ((result 0)
  16222. (re (concat "\\([0-9.]+\\) *\\("
  16223. (regexp-opt (mapcar 'car org-effort-durations))
  16224. "\\)")))
  16225. (while (string-match re s)
  16226. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16227. (string-to-number (match-string 1 s))))
  16228. (setq s (replace-match "" nil t s)))
  16229. (setq result (floor result))
  16230. (incf result (org-hh:mm-string-to-minutes s))
  16231. (if output-to-string (number-to-string result) result)))
  16232. ;;;; Files
  16233. (defun org-save-all-org-buffers ()
  16234. "Save all Org-mode buffers without user confirmation."
  16235. (interactive)
  16236. (message "Saving all Org-mode buffers...")
  16237. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16238. (when (featurep 'org-id) (org-id-locations-save))
  16239. (message "Saving all Org-mode buffers... done"))
  16240. (defun org-revert-all-org-buffers ()
  16241. "Revert all Org-mode buffers.
  16242. Prompt for confirmation when there are unsaved changes.
  16243. Be sure you know what you are doing before letting this function
  16244. overwrite your changes.
  16245. This function is useful in a setup where one tracks org files
  16246. with a version control system, to revert on one machine after pulling
  16247. changes from another. I believe the procedure must be like this:
  16248. 1. M-x org-save-all-org-buffers
  16249. 2. Pull changes from the other machine, resolve conflicts
  16250. 3. M-x org-revert-all-org-buffers"
  16251. (interactive)
  16252. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16253. (user-error "Abort"))
  16254. (save-excursion
  16255. (save-window-excursion
  16256. (mapc
  16257. (lambda (b)
  16258. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16259. (with-current-buffer b buffer-file-name))
  16260. (org-pop-to-buffer-same-window b)
  16261. (revert-buffer t 'no-confirm)))
  16262. (buffer-list))
  16263. (when (and (featurep 'org-id) org-id-track-globally)
  16264. (org-id-locations-load)))))
  16265. ;;;; Agenda files
  16266. ;;;###autoload
  16267. (defun org-switchb (&optional arg)
  16268. "Switch between Org buffers.
  16269. With one prefix argument, restrict available buffers to files.
  16270. With two prefix arguments, restrict available buffers to agenda files.
  16271. Defaults to `iswitchb' for buffer name completion.
  16272. Set `org-completion-use-ido' to make it use ido instead."
  16273. (interactive "P")
  16274. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16275. ((equal arg '(16)) (org-buffer-list 'agenda))
  16276. (t (org-buffer-list))))
  16277. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16278. (org-completion-use-ido org-completion-use-ido))
  16279. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16280. (setq org-completion-use-iswitchb t))
  16281. (org-pop-to-buffer-same-window
  16282. (org-icompleting-read "Org buffer: "
  16283. (mapcar 'list (mapcar 'buffer-name blist))
  16284. nil t))))
  16285. ;;; Define some older names previously used for this functionality
  16286. ;;;###autoload
  16287. (defalias 'org-ido-switchb 'org-switchb)
  16288. ;;;###autoload
  16289. (defalias 'org-iswitchb 'org-switchb)
  16290. (defun org-buffer-list (&optional predicate exclude-tmp)
  16291. "Return a list of Org buffers.
  16292. PREDICATE can be `export', `files' or `agenda'.
  16293. export restrict the list to Export buffers.
  16294. files restrict the list to buffers visiting Org files.
  16295. agenda restrict the list to buffers visiting agenda files.
  16296. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16297. (let* ((bfn nil)
  16298. (agenda-files (and (eq predicate 'agenda)
  16299. (mapcar 'file-truename (org-agenda-files t))))
  16300. (filter
  16301. (cond
  16302. ((eq predicate 'files)
  16303. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16304. ((eq predicate 'export)
  16305. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16306. ((eq predicate 'agenda)
  16307. (lambda (b)
  16308. (with-current-buffer b
  16309. (and (derived-mode-p 'org-mode)
  16310. (setq bfn (buffer-file-name b))
  16311. (member (file-truename bfn) agenda-files)))))
  16312. (t (lambda (b) (with-current-buffer b
  16313. (or (derived-mode-p 'org-mode)
  16314. (string-match "\*Org .*Export"
  16315. (buffer-name b)))))))))
  16316. (delq nil
  16317. (mapcar
  16318. (lambda(b)
  16319. (if (and (funcall filter b)
  16320. (or (not exclude-tmp)
  16321. (not (string-match "tmp" (buffer-name b)))))
  16322. b
  16323. nil))
  16324. (buffer-list)))))
  16325. (defun org-agenda-files (&optional unrestricted archives)
  16326. "Get the list of agenda files.
  16327. Optional UNRESTRICTED means return the full list even if a restriction
  16328. is currently in place.
  16329. When ARCHIVES is t, include all archive files that are really being
  16330. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16331. `org-agenda-archives-mode' is t."
  16332. (let ((files
  16333. (cond
  16334. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16335. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16336. ((listp org-agenda-files) org-agenda-files)
  16337. (t (error "Invalid value of `org-agenda-files'")))))
  16338. (setq files (apply 'append
  16339. (mapcar (lambda (f)
  16340. (if (file-directory-p f)
  16341. (directory-files
  16342. f t org-agenda-file-regexp)
  16343. (list f)))
  16344. files)))
  16345. (when org-agenda-skip-unavailable-files
  16346. (setq files (delq nil
  16347. (mapcar (function
  16348. (lambda (file)
  16349. (and (file-readable-p file) file)))
  16350. files))))
  16351. (when (or (eq archives t)
  16352. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16353. (setq files (org-add-archive-files files)))
  16354. files))
  16355. (defun org-agenda-file-p (&optional file)
  16356. "Return non-nil, if FILE is an agenda file.
  16357. If FILE is omitted, use the file associated with the current
  16358. buffer."
  16359. (member (or file (buffer-file-name))
  16360. (org-agenda-files t)))
  16361. (defun org-edit-agenda-file-list ()
  16362. "Edit the list of agenda files.
  16363. Depending on setup, this either uses customize to edit the variable
  16364. `org-agenda-files', or it visits the file that is holding the list. In the
  16365. latter case, the buffer is set up in a way that saving it automatically kills
  16366. the buffer and restores the previous window configuration."
  16367. (interactive)
  16368. (if (stringp org-agenda-files)
  16369. (let ((cw (current-window-configuration)))
  16370. (find-file org-agenda-files)
  16371. (org-set-local 'org-window-configuration cw)
  16372. (org-add-hook 'after-save-hook
  16373. (lambda ()
  16374. (set-window-configuration
  16375. (prog1 org-window-configuration
  16376. (kill-buffer (current-buffer))))
  16377. (org-install-agenda-files-menu)
  16378. (message "New agenda file list installed"))
  16379. nil 'local)
  16380. (message "%s" (substitute-command-keys
  16381. "Edit list and finish with \\[save-buffer]")))
  16382. (customize-variable 'org-agenda-files)))
  16383. (defun org-store-new-agenda-file-list (list)
  16384. "Set new value for the agenda file list and save it correctly."
  16385. (if (stringp org-agenda-files)
  16386. (let ((fe (org-read-agenda-file-list t)) b u)
  16387. (while (setq b (find-buffer-visiting org-agenda-files))
  16388. (kill-buffer b))
  16389. (with-temp-file org-agenda-files
  16390. (insert
  16391. (mapconcat
  16392. (lambda (f) ;; Keep un-expanded entries.
  16393. (if (setq u (assoc f fe))
  16394. (cdr u)
  16395. f))
  16396. list "\n")
  16397. "\n")))
  16398. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16399. (org-insert-mode-line-in-empty-file nil))
  16400. (setq org-agenda-files list)
  16401. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16402. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16403. "Read the list of agenda files from a file.
  16404. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16405. filenames, used by `org-store-new-agenda-file-list' to write back
  16406. un-expanded file names."
  16407. (when (file-directory-p org-agenda-files)
  16408. (error "`org-agenda-files' cannot be a single directory"))
  16409. (when (stringp org-agenda-files)
  16410. (with-temp-buffer
  16411. (insert-file-contents org-agenda-files)
  16412. (mapcar
  16413. (lambda (f)
  16414. (let ((e (expand-file-name (substitute-in-file-name f)
  16415. org-directory)))
  16416. (if pair-with-expansion
  16417. (cons e f)
  16418. e)))
  16419. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16420. ;;;###autoload
  16421. (defun org-cycle-agenda-files ()
  16422. "Cycle through the files in `org-agenda-files'.
  16423. If the current buffer visits an agenda file, find the next one in the list.
  16424. If the current buffer does not, find the first agenda file."
  16425. (interactive)
  16426. (let* ((fs (org-agenda-files t))
  16427. (files (append fs (list (car fs))))
  16428. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16429. file)
  16430. (unless files (user-error "No agenda files"))
  16431. (catch 'exit
  16432. (while (setq file (pop files))
  16433. (if (equal (file-truename file) tcf)
  16434. (when (car files)
  16435. (find-file (car files))
  16436. (throw 'exit t))))
  16437. (find-file (car fs)))
  16438. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16439. (defun org-agenda-file-to-front (&optional to-end)
  16440. "Move/add the current file to the top of the agenda file list.
  16441. If the file is not present in the list, it is added to the front. If it is
  16442. present, it is moved there. With optional argument TO-END, add/move to the
  16443. end of the list."
  16444. (interactive "P")
  16445. (let ((org-agenda-skip-unavailable-files nil)
  16446. (file-alist (mapcar (lambda (x)
  16447. (cons (file-truename x) x))
  16448. (org-agenda-files t)))
  16449. (ctf (file-truename
  16450. (or buffer-file-name
  16451. (user-error "Please save the current buffer to a file"))))
  16452. x had)
  16453. (setq x (assoc ctf file-alist) had x)
  16454. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16455. (if to-end
  16456. (setq file-alist (append (delq x file-alist) (list x)))
  16457. (setq file-alist (cons x (delq x file-alist))))
  16458. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16459. (org-install-agenda-files-menu)
  16460. (message "File %s to %s of agenda file list"
  16461. (if had "moved" "added") (if to-end "end" "front"))))
  16462. (defun org-remove-file (&optional file)
  16463. "Remove current file from the list of files in variable `org-agenda-files'.
  16464. These are the files which are being checked for agenda entries.
  16465. Optional argument FILE means use this file instead of the current."
  16466. (interactive)
  16467. (let* ((org-agenda-skip-unavailable-files nil)
  16468. (file (or file buffer-file-name
  16469. (user-error "Current buffer does not visit a file")))
  16470. (true-file (file-truename file))
  16471. (afile (abbreviate-file-name file))
  16472. (files (delq nil (mapcar
  16473. (lambda (x)
  16474. (if (equal true-file
  16475. (file-truename x))
  16476. nil x))
  16477. (org-agenda-files t)))))
  16478. (if (not (= (length files) (length (org-agenda-files t))))
  16479. (progn
  16480. (org-store-new-agenda-file-list files)
  16481. (org-install-agenda-files-menu)
  16482. (message "Removed file: %s" afile))
  16483. (message "File was not in list: %s (not removed)" afile))))
  16484. (defun org-file-menu-entry (file)
  16485. (vector file (list 'find-file file) t))
  16486. (defun org-check-agenda-file (file)
  16487. "Make sure FILE exists. If not, ask user what to do."
  16488. (when (not (file-exists-p file))
  16489. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16490. (abbreviate-file-name file))
  16491. (let ((r (downcase (read-char-exclusive))))
  16492. (cond
  16493. ((equal r ?r)
  16494. (org-remove-file file)
  16495. (throw 'nextfile t))
  16496. (t (error "Abort"))))))
  16497. (defun org-get-agenda-file-buffer (file)
  16498. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16499. it to the list of buffers which might be released later."
  16500. (let ((buf (org-find-base-buffer-visiting file)))
  16501. (if buf
  16502. buf ; just return it
  16503. ;; Make a new buffer and remember it
  16504. (setq buf (find-file-noselect file))
  16505. (if buf (push buf org-agenda-new-buffers))
  16506. buf)))
  16507. (defun org-release-buffers (blist)
  16508. "Release all buffers in list, asking the user for confirmation when needed.
  16509. When a buffer is unmodified, it is just killed. When modified, it is saved
  16510. \(if the user agrees) and then killed."
  16511. (let (buf file)
  16512. (while (setq buf (pop blist))
  16513. (setq file (buffer-file-name buf))
  16514. (when (and (buffer-modified-p buf)
  16515. file
  16516. (y-or-n-p (format "Save file %s? " file)))
  16517. (with-current-buffer buf (save-buffer)))
  16518. (kill-buffer buf))))
  16519. (defun org-agenda-prepare-buffers (files)
  16520. "Create buffers for all agenda files, protect archived trees and comments."
  16521. (interactive)
  16522. (let ((pa '(:org-archived t))
  16523. (pc '(:org-comment t))
  16524. (pall '(:org-archived t :org-comment t))
  16525. (inhibit-read-only t)
  16526. (org-inhibit-startup org-agenda-inhibit-startup)
  16527. (rea (concat ":" org-archive-tag ":"))
  16528. file re)
  16529. (setq org-tag-alist-for-agenda nil
  16530. org-tag-groups-alist-for-agenda nil)
  16531. (save-excursion
  16532. (save-restriction
  16533. (while (setq file (pop files))
  16534. (catch 'nextfile
  16535. (if (bufferp file)
  16536. (set-buffer file)
  16537. (org-check-agenda-file file)
  16538. (set-buffer (org-get-agenda-file-buffer file)))
  16539. (widen)
  16540. (org-set-regexps-and-options-for-tags)
  16541. (goto-char (point-min))
  16542. (let ((case-fold-search t))
  16543. (when (search-forward "#+setupfile" nil t)
  16544. ;; Don't set all regexps and options systematically as
  16545. ;; this is only run for setting agenda tags from setup
  16546. ;; file
  16547. (org-set-regexps-and-options)))
  16548. (or (memq 'category org-agenda-ignore-drawer-properties)
  16549. (org-refresh-category-properties))
  16550. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16551. (org-refresh-properties org-effort-property 'org-effort))
  16552. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16553. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16554. (setq org-todo-keywords-for-agenda
  16555. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16556. (setq org-done-keywords-for-agenda
  16557. (append org-done-keywords-for-agenda org-done-keywords))
  16558. (setq org-todo-keyword-alist-for-agenda
  16559. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16560. (setq org-tag-alist-for-agenda
  16561. (org-uniquify
  16562. (append org-tag-alist-for-agenda
  16563. org-tag-alist
  16564. org-tag-persistent-alist)))
  16565. (if org-group-tags
  16566. (setq org-tag-groups-alist-for-agenda
  16567. (org-uniquify-alist
  16568. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16569. (org-with-silent-modifications
  16570. (save-excursion
  16571. (remove-text-properties (point-min) (point-max) pall)
  16572. (when org-agenda-skip-archived-trees
  16573. (goto-char (point-min))
  16574. (while (re-search-forward rea nil t)
  16575. (if (org-at-heading-p t)
  16576. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16577. (goto-char (point-min))
  16578. (setq re (format org-heading-keyword-regexp-format
  16579. org-comment-string))
  16580. (while (re-search-forward re nil t)
  16581. (add-text-properties
  16582. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16583. (setq org-todo-keywords-for-agenda
  16584. (org-uniquify org-todo-keywords-for-agenda))
  16585. (setq org-todo-keyword-alist-for-agenda
  16586. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16587. ;;;; CDLaTeX minor mode
  16588. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16589. "Keymap for the minor `org-cdlatex-mode'.")
  16590. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16591. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16592. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16593. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16594. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16595. (defvar org-cdlatex-texmathp-advice-is-done nil
  16596. "Flag remembering if we have applied the advice to texmathp already.")
  16597. (define-minor-mode org-cdlatex-mode
  16598. "Toggle the minor `org-cdlatex-mode'.
  16599. This mode supports entering LaTeX environment and math in LaTeX fragments
  16600. in Org-mode.
  16601. \\{org-cdlatex-mode-map}"
  16602. nil " OCDL" nil
  16603. (when org-cdlatex-mode
  16604. (require 'cdlatex)
  16605. (run-hooks 'cdlatex-mode-hook)
  16606. (cdlatex-compute-tables))
  16607. (unless org-cdlatex-texmathp-advice-is-done
  16608. (setq org-cdlatex-texmathp-advice-is-done t)
  16609. (defadvice texmathp (around org-math-always-on activate)
  16610. "Always return t in org-mode buffers.
  16611. This is because we want to insert math symbols without dollars even outside
  16612. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16613. an embedded LaTeX fragment, let texmathp do its job.
  16614. \\[org-cdlatex-mode-map]"
  16615. (interactive)
  16616. (let (p)
  16617. (cond
  16618. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16619. ((eq this-command 'cdlatex-math-symbol)
  16620. (setq ad-return-value t
  16621. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16622. (t
  16623. (let ((p (org-inside-LaTeX-fragment-p)))
  16624. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16625. (setq ad-return-value t
  16626. texmathp-why '("Org-mode embedded math" . 0))
  16627. (if p ad-do-it)))))))))
  16628. (defun turn-on-org-cdlatex ()
  16629. "Unconditionally turn on `org-cdlatex-mode'."
  16630. (org-cdlatex-mode 1))
  16631. (defun org-try-cdlatex-tab ()
  16632. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16633. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16634. - inside a LaTeX fragment, or
  16635. - after the first word in a line, where an abbreviation expansion could
  16636. insert a LaTeX environment."
  16637. (when org-cdlatex-mode
  16638. (cond
  16639. ;; Before any word on the line: No expansion possible.
  16640. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16641. ;; Just after first word on the line: Expand it. Make sure it
  16642. ;; cannot happen on headlines, though.
  16643. ((save-excursion
  16644. (skip-chars-backward "a-zA-Z0-9*")
  16645. (skip-chars-backward " \t")
  16646. (and (bolp) (not (org-at-heading-p))))
  16647. (cdlatex-tab) t)
  16648. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16649. (defun org-cdlatex-underscore-caret (&optional arg)
  16650. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16651. Revert to the normal definition outside of these fragments."
  16652. (interactive "P")
  16653. (if (org-inside-LaTeX-fragment-p)
  16654. (call-interactively 'cdlatex-sub-superscript)
  16655. (let (org-cdlatex-mode)
  16656. (call-interactively (key-binding (vector last-input-event))))))
  16657. (defun org-cdlatex-math-modify (&optional arg)
  16658. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16659. Revert to the normal definition outside of these fragments."
  16660. (interactive "P")
  16661. (if (org-inside-LaTeX-fragment-p)
  16662. (call-interactively 'cdlatex-math-modify)
  16663. (let (org-cdlatex-mode)
  16664. (call-interactively (key-binding (vector last-input-event))))))
  16665. ;;;; LaTeX fragments
  16666. (defvar org-latex-regexps
  16667. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16668. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16669. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16670. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16671. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16672. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16673. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16674. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16675. "Regular expressions for matching embedded LaTeX.")
  16676. (defun org-inside-LaTeX-fragment-p ()
  16677. "Test if point is inside a LaTeX fragment.
  16678. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16679. sequence appearing also before point.
  16680. Even though the matchers for math are configurable, this function assumes
  16681. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16682. delimiters are skipped when they have been removed by customization.
  16683. The return value is nil, or a cons cell with the delimiter and the
  16684. position of this delimiter.
  16685. This function does a reasonably good job, but can locally be fooled by
  16686. for example currency specifications. For example it will assume being in
  16687. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16688. fragments that are properly closed, but during editing, we have to live
  16689. with the uncertainty caused by missing closing delimiters. This function
  16690. looks only before point, not after."
  16691. (catch 'exit
  16692. (let ((pos (point))
  16693. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16694. (lim (progn
  16695. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16696. (point)))
  16697. dd-on str (start 0) m re)
  16698. (goto-char pos)
  16699. (when dodollar
  16700. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16701. re (nth 1 (assoc "$" org-latex-regexps)))
  16702. (while (string-match re str start)
  16703. (cond
  16704. ((= (match-end 0) (length str))
  16705. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16706. ((= (match-end 0) (- (length str) 5))
  16707. (throw 'exit nil))
  16708. (t (setq start (match-end 0))))))
  16709. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16710. (goto-char pos)
  16711. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16712. (and (match-beginning 2) (throw 'exit nil))
  16713. ;; count $$
  16714. (while (re-search-backward "\\$\\$" lim t)
  16715. (setq dd-on (not dd-on)))
  16716. (goto-char pos)
  16717. (if dd-on (cons "$$" m))))))
  16718. (defun org-inside-latex-macro-p ()
  16719. "Is point inside a LaTeX macro or its arguments?"
  16720. (save-match-data
  16721. (org-in-regexp
  16722. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16723. (defvar org-latex-fragment-image-overlays nil
  16724. "List of overlays carrying the images of latex fragments.")
  16725. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16726. (defun org-remove-latex-fragment-image-overlays ()
  16727. "Remove all overlays with LaTeX fragment images in current buffer."
  16728. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16729. (setq org-latex-fragment-image-overlays nil))
  16730. (defun org-preview-latex-fragment (&optional subtree)
  16731. "Preview the LaTeX fragment at point, or all locally or globally.
  16732. If the cursor is in a LaTeX fragment, create the image and overlay
  16733. it over the source code. If there is no fragment at point, display
  16734. all fragments in the current text, from one headline to the next. With
  16735. prefix SUBTREE, display all fragments in the current subtree. With a
  16736. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16737. the cursor is before the first headline,
  16738. display all fragments in the buffer.
  16739. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16740. (interactive "P")
  16741. (unless buffer-file-name
  16742. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16743. (when (display-graphic-p)
  16744. (org-remove-latex-fragment-image-overlays)
  16745. (save-excursion
  16746. (save-restriction
  16747. (let (beg end at msg)
  16748. (cond
  16749. ((or (equal subtree '(16))
  16750. (not (save-excursion
  16751. (re-search-backward org-outline-regexp-bol nil t))))
  16752. (setq beg (point-min) end (point-max)
  16753. msg "Creating images for buffer...%s"))
  16754. ((equal subtree '(4))
  16755. (org-back-to-heading)
  16756. (setq beg (point) end (org-end-of-subtree t)
  16757. msg "Creating images for subtree...%s"))
  16758. (t
  16759. (if (setq at (org-inside-LaTeX-fragment-p))
  16760. (goto-char (max (point-min) (- (cdr at) 2)))
  16761. (org-back-to-heading))
  16762. (setq beg (point) end (progn (outline-next-heading) (point))
  16763. msg (if at "Creating image...%s"
  16764. "Creating images for entry...%s"))))
  16765. (message msg "")
  16766. (narrow-to-region beg end)
  16767. (goto-char beg)
  16768. (org-format-latex
  16769. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16770. (file-name-nondirectory
  16771. buffer-file-name)))
  16772. default-directory 'overlays msg at 'forbuffer
  16773. org-latex-create-formula-image-program)
  16774. (message msg "done. Use `C-c C-c' to remove images."))))))
  16775. (defun org-format-latex (prefix &optional dir overlays msg at
  16776. forbuffer processing-type)
  16777. "Replace LaTeX fragments with links to an image, and produce images.
  16778. Some of the options can be changed using the variable
  16779. `org-format-latex-options'."
  16780. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16781. (let* ((prefixnodir (file-name-nondirectory prefix))
  16782. (absprefix (expand-file-name prefix dir))
  16783. (todir (file-name-directory absprefix))
  16784. (opt org-format-latex-options)
  16785. (optnew org-format-latex-options)
  16786. (matchers (plist-get opt :matchers))
  16787. (re-list org-latex-regexps)
  16788. (cnt 0) txt hash link beg end re e checkdir
  16789. string
  16790. m n block-type block linkfile movefile ov)
  16791. ;; Check the different regular expressions
  16792. (while (setq e (pop re-list))
  16793. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16794. block (if block-type "\n\n" ""))
  16795. (when (member m matchers)
  16796. (goto-char (point-min))
  16797. (while (re-search-forward re nil t)
  16798. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16799. (or (not overlays)
  16800. (not (eq (get-char-property (match-beginning n)
  16801. 'org-overlay-type)
  16802. 'org-latex-overlay))))
  16803. (cond
  16804. ((eq processing-type 'verbatim))
  16805. ((eq processing-type 'mathjax)
  16806. ;; Prepare for MathJax processing.
  16807. (setq string (match-string n))
  16808. (when (member m '("$" "$1"))
  16809. (save-excursion
  16810. (delete-region (match-beginning n) (match-end n))
  16811. (goto-char (match-beginning n))
  16812. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16813. ((or (eq processing-type 'dvipng)
  16814. (eq processing-type 'imagemagick))
  16815. ;; Process to an image.
  16816. (setq txt (match-string n)
  16817. beg (match-beginning n) end (match-end n)
  16818. cnt (1+ cnt))
  16819. (let ((face (face-at-point))
  16820. (fg (plist-get opt :foreground))
  16821. (bg (plist-get opt :background))
  16822. ;; Ensure full list is printed.
  16823. print-length print-level)
  16824. (when forbuffer
  16825. ;; Get the colors from the face at point.
  16826. (goto-char beg)
  16827. (when (eq fg 'auto)
  16828. (setq fg (face-attribute face :foreground nil 'default)))
  16829. (when (eq bg 'auto)
  16830. (setq bg (face-attribute face :background nil 'default)))
  16831. (setq optnew (copy-sequence opt))
  16832. (plist-put optnew :foreground fg)
  16833. (plist-put optnew :background bg))
  16834. (setq hash (sha1 (prin1-to-string
  16835. (list org-format-latex-header
  16836. org-latex-default-packages-alist
  16837. org-latex-packages-alist
  16838. org-format-latex-options
  16839. forbuffer txt fg bg)))
  16840. linkfile (format "%s_%s.png" prefix hash)
  16841. movefile (format "%s_%s.png" absprefix hash)))
  16842. (setq link (concat block "[[file:" linkfile "]]" block))
  16843. (if msg (message msg cnt))
  16844. (goto-char beg)
  16845. (unless checkdir ; Ensure the directory exists.
  16846. (setq checkdir t)
  16847. (or (file-directory-p todir) (make-directory todir t)))
  16848. (unless (file-exists-p movefile)
  16849. (org-create-formula-image
  16850. txt movefile optnew forbuffer processing-type))
  16851. (if overlays
  16852. (progn
  16853. (mapc (lambda (o)
  16854. (if (eq (overlay-get o 'org-overlay-type)
  16855. 'org-latex-overlay)
  16856. (delete-overlay o)))
  16857. (overlays-in beg end))
  16858. (setq ov (make-overlay beg end))
  16859. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16860. (if (featurep 'xemacs)
  16861. (progn
  16862. (overlay-put ov 'invisible t)
  16863. (overlay-put
  16864. ov 'end-glyph
  16865. (make-glyph (vector 'png :file movefile))))
  16866. (overlay-put
  16867. ov 'display
  16868. (list 'image :type 'png :file movefile :ascent 'center)))
  16869. (push ov org-latex-fragment-image-overlays)
  16870. (goto-char end))
  16871. (delete-region beg end)
  16872. (insert (org-add-props link
  16873. (list 'org-latex-src
  16874. (replace-regexp-in-string
  16875. "\"" "" txt)
  16876. 'org-latex-src-embed-type
  16877. (if block-type 'paragraph 'character))))))
  16878. ((eq processing-type 'mathml)
  16879. ;; Process to MathML
  16880. (unless (save-match-data (org-format-latex-mathml-available-p))
  16881. (user-error "LaTeX to MathML converter not configured"))
  16882. (setq txt (match-string n)
  16883. beg (match-beginning n) end (match-end n)
  16884. cnt (1+ cnt))
  16885. (if msg (message msg cnt))
  16886. (goto-char beg)
  16887. (delete-region beg end)
  16888. (insert (org-format-latex-as-mathml
  16889. txt block-type prefix dir)))
  16890. (t
  16891. (error "Unknown conversion type %s for LaTeX fragments"
  16892. processing-type)))))))))
  16893. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16894. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16895. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16896. sucessful, return the portion between \"<math...> </math>\"
  16897. elements otherwise return nil. When MATHML-FILE is specified,
  16898. write the results in to that file. When invoked as an
  16899. interactive command, prompt for LATEX-FRAG, with initial value
  16900. set to the current active region and echo the results for user
  16901. inspection."
  16902. (interactive (list (let ((frag (when (org-region-active-p)
  16903. (buffer-substring-no-properties
  16904. (region-beginning) (region-end)))))
  16905. (read-string "LaTeX Fragment: " frag nil frag))))
  16906. (unless latex-frag (error "Invalid LaTeX fragment"))
  16907. (let* ((tmp-in-file (file-relative-name
  16908. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16909. (ignore (write-region latex-frag nil tmp-in-file))
  16910. (tmp-out-file (file-relative-name
  16911. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16912. (cmd (format-spec
  16913. org-latex-to-mathml-convert-command
  16914. `((?j . ,(shell-quote-argument
  16915. (expand-file-name org-latex-to-mathml-jar-file)))
  16916. (?I . ,(shell-quote-argument tmp-in-file))
  16917. (?o . ,(shell-quote-argument tmp-out-file)))))
  16918. mathml shell-command-output)
  16919. (when (org-called-interactively-p 'any)
  16920. (unless (org-format-latex-mathml-available-p)
  16921. (user-error "LaTeX to MathML converter not configured")))
  16922. (message "Running %s" cmd)
  16923. (setq shell-command-output (shell-command-to-string cmd))
  16924. (setq mathml
  16925. (when (file-readable-p tmp-out-file)
  16926. (with-current-buffer (find-file-noselect tmp-out-file t)
  16927. (goto-char (point-min))
  16928. (when (re-search-forward
  16929. (concat
  16930. (regexp-quote
  16931. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16932. "\\(.\\|\n\\)*"
  16933. (regexp-quote "</math>")) nil t)
  16934. (prog1 (match-string 0) (kill-buffer))))))
  16935. (cond
  16936. (mathml
  16937. (setq mathml
  16938. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16939. (when mathml-file
  16940. (write-region mathml nil mathml-file))
  16941. (when (org-called-interactively-p 'any)
  16942. (message mathml)))
  16943. ((message "LaTeX to MathML conversion failed")
  16944. (message shell-command-output)))
  16945. (delete-file tmp-in-file)
  16946. (when (file-exists-p tmp-out-file)
  16947. (delete-file tmp-out-file))
  16948. mathml))
  16949. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16950. prefix &optional dir)
  16951. "Use `org-create-math-formula' but check local cache first."
  16952. (let* ((absprefix (expand-file-name prefix dir))
  16953. (print-length nil) (print-level nil)
  16954. (formula-id (concat
  16955. "formula-"
  16956. (sha1
  16957. (prin1-to-string
  16958. (list latex-frag
  16959. org-latex-to-mathml-convert-command)))))
  16960. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16961. (formula-cache-dir (file-name-directory formula-cache)))
  16962. (unless (file-directory-p formula-cache-dir)
  16963. (make-directory formula-cache-dir t))
  16964. (unless (file-exists-p formula-cache)
  16965. (org-create-math-formula latex-frag formula-cache))
  16966. (if (file-exists-p formula-cache)
  16967. ;; Successful conversion. Return the link to MathML file.
  16968. (org-add-props
  16969. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16970. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16971. 'org-latex-src-embed-type (if latex-frag-type
  16972. 'paragraph 'character)))
  16973. ;; Failed conversion. Return the LaTeX fragment verbatim
  16974. latex-frag)))
  16975. (defun org-create-formula-image (string tofile options buffer &optional type)
  16976. "Create an image from LaTeX source using dvipng or convert.
  16977. This function calls either `org-create-formula-image-with-dvipng'
  16978. or `org-create-formula-image-with-imagemagick' depending on the
  16979. value of `org-latex-create-formula-image-program' or on the value
  16980. of the optional TYPE variable.
  16981. Note: ultimately these two function should be combined as they
  16982. share a good deal of logic."
  16983. (org-check-external-command
  16984. "latex" "needed to convert LaTeX fragments to images")
  16985. (funcall
  16986. (case (or type org-latex-create-formula-image-program)
  16987. ('dvipng
  16988. (org-check-external-command
  16989. "dvipng" "needed to convert LaTeX fragments to images")
  16990. #'org-create-formula-image-with-dvipng)
  16991. ('imagemagick
  16992. (org-check-external-command
  16993. "convert" "you need to install imagemagick")
  16994. #'org-create-formula-image-with-imagemagick)
  16995. (t (error
  16996. "Invalid value of `org-latex-create-formula-image-program'")))
  16997. string tofile options buffer))
  16998. (declare-function org-export-get-backend "ox" (name))
  16999. (declare-function org-export--get-global-options "ox" (&optional backend))
  17000. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17001. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17002. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17003. (defun org-create-formula--latex-header ()
  17004. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17005. (let ((info (org-combine-plists (org-export--get-global-options
  17006. (org-export-get-backend 'latex))
  17007. (org-export--get-inbuffer-options
  17008. (org-export-get-backend 'latex)))))
  17009. (org-latex-guess-babel-language
  17010. (org-latex-guess-inputenc
  17011. (org-splice-latex-header
  17012. org-format-latex-header
  17013. org-latex-default-packages-alist
  17014. org-latex-packages-alist t
  17015. (plist-get info :latex-header)))
  17016. info)))
  17017. ;; This function borrows from Ganesh Swami's latex2png.el
  17018. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17019. "This calls dvipng."
  17020. (require 'ox-latex)
  17021. (let* ((tmpdir (if (featurep 'xemacs)
  17022. (temp-directory)
  17023. temporary-file-directory))
  17024. (texfilebase (make-temp-name
  17025. (expand-file-name "orgtex" tmpdir)))
  17026. (texfile (concat texfilebase ".tex"))
  17027. (dvifile (concat texfilebase ".dvi"))
  17028. (pngfile (concat texfilebase ".png"))
  17029. (fnh (if (featurep 'xemacs)
  17030. (font-height (face-font 'default))
  17031. (face-attribute 'default :height nil)))
  17032. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17033. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17034. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17035. "Black"))
  17036. (bg (or (plist-get options (if buffer :background :html-background))
  17037. "Transparent")))
  17038. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17039. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17040. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17041. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17042. (let ((latex-header (org-create-formula--latex-header)))
  17043. (with-temp-file texfile
  17044. (insert latex-header)
  17045. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17046. (let ((dir default-directory))
  17047. (condition-case nil
  17048. (progn
  17049. (cd tmpdir)
  17050. (call-process "latex" nil nil nil texfile))
  17051. (error nil))
  17052. (cd dir))
  17053. (if (not (file-exists-p dvifile))
  17054. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17055. (condition-case nil
  17056. (if (featurep 'xemacs)
  17057. (call-process "dvipng" nil nil nil
  17058. "-fg" fg "-bg" bg
  17059. "-T" "tight"
  17060. "-o" pngfile
  17061. dvifile)
  17062. (call-process "dvipng" nil nil nil
  17063. "-fg" fg "-bg" bg
  17064. "-D" dpi
  17065. ;;"-x" scale "-y" scale
  17066. "-T" "tight"
  17067. "-o" pngfile
  17068. dvifile))
  17069. (error nil))
  17070. (if (not (file-exists-p pngfile))
  17071. (if org-format-latex-signal-error
  17072. (error "Failed to create png file from %s" texfile)
  17073. (message "Failed to create png file from %s" texfile)
  17074. nil)
  17075. ;; Use the requested file name and clean up
  17076. (copy-file pngfile tofile 'replace)
  17077. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17078. (if (file-exists-p (concat texfilebase e))
  17079. (delete-file (concat texfilebase e))))
  17080. pngfile))))
  17081. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17082. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17083. "This calls convert, which is included into imagemagick."
  17084. (require 'ox-latex)
  17085. (let* ((tmpdir (if (featurep 'xemacs)
  17086. (temp-directory)
  17087. temporary-file-directory))
  17088. (texfilebase (make-temp-name
  17089. (expand-file-name "orgtex" tmpdir)))
  17090. (texfile (concat texfilebase ".tex"))
  17091. (pdffile (concat texfilebase ".pdf"))
  17092. (pngfile (concat texfilebase ".png"))
  17093. (fnh (if (featurep 'xemacs)
  17094. (font-height (face-font 'default))
  17095. (face-attribute 'default :height nil)))
  17096. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17097. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17098. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17099. "black"))
  17100. (bg (or (plist-get options (if buffer :background :html-background))
  17101. "white")))
  17102. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17103. (setq fg (org-latex-color-format fg)))
  17104. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17105. (setq bg (org-latex-color-format
  17106. (if (string= bg "Transparent") "white" bg))))
  17107. (let ((latex-header (org-create-formula--latex-header)))
  17108. (with-temp-file texfile
  17109. (insert latex-header)
  17110. (insert "\n\\begin{document}\n"
  17111. "\\definecolor{fg}{rgb}{" fg "}\n"
  17112. "\\definecolor{bg}{rgb}{" bg "}\n"
  17113. "\n\\pagecolor{bg}\n"
  17114. "\n{\\color{fg}\n"
  17115. string
  17116. "\n}\n"
  17117. "\n\\end{document}\n")))
  17118. (org-latex-compile texfile t)
  17119. (if (not (file-exists-p pdffile))
  17120. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17121. (condition-case nil
  17122. (if (featurep 'xemacs)
  17123. (call-process "convert" nil nil nil
  17124. "-density" "96"
  17125. "-trim"
  17126. "-antialias"
  17127. pdffile
  17128. "-quality" "100"
  17129. ;; "-sharpen" "0x1.0"
  17130. pngfile)
  17131. (call-process "convert" nil nil nil
  17132. "-density" dpi
  17133. "-trim"
  17134. "-antialias"
  17135. pdffile
  17136. "-quality" "100"
  17137. ;; "-sharpen" "0x1.0"
  17138. pngfile))
  17139. (error nil))
  17140. (if (not (file-exists-p pngfile))
  17141. (if org-format-latex-signal-error
  17142. (error "Failed to create png file from %s" texfile)
  17143. (message "Failed to create png file from %s" texfile)
  17144. nil)
  17145. ;; Use the requested file name and clean up
  17146. (copy-file pngfile tofile 'replace)
  17147. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17148. (if (file-exists-p (concat texfilebase e))
  17149. (delete-file (concat texfilebase e))))
  17150. pngfile))))
  17151. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17152. "Fill a LaTeX header template TPL.
  17153. In the template, the following place holders will be recognized:
  17154. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17155. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17156. [PACKAGES] \\usepackage statements for PKG
  17157. [NO-PACKAGES] do not include PKG
  17158. [EXTRA] the string EXTRA
  17159. [NO-EXTRA] do not include EXTRA
  17160. For backward compatibility, if both the positive and the negative place
  17161. holder is missing, the positive one (without the \"NO-\") will be
  17162. assumed to be present at the end of the template.
  17163. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17164. EXTRA is a string.
  17165. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17166. (let (rpl (end ""))
  17167. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17168. (setq rpl (if (or (match-end 1) (not def-pkg))
  17169. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17170. tpl (replace-match rpl t t tpl))
  17171. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17172. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17173. (setq rpl (if (or (match-end 1) (not pkg))
  17174. "" (org-latex-packages-to-string pkg snippets-p t))
  17175. tpl (replace-match rpl t t tpl))
  17176. (if pkg (setq end
  17177. (concat end "\n"
  17178. (org-latex-packages-to-string pkg snippets-p)))))
  17179. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17180. (setq rpl (if (or (match-end 1) (not extra))
  17181. "" (concat extra "\n"))
  17182. tpl (replace-match rpl t t tpl))
  17183. (if (and extra (string-match "\\S-" extra))
  17184. (setq end (concat end "\n" extra))))
  17185. (if (string-match "\\S-" end)
  17186. (concat tpl "\n" end)
  17187. tpl)))
  17188. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17189. "Turn an alist of packages into a string with the \\usepackage macros."
  17190. (setq pkg (mapconcat (lambda(p)
  17191. (cond
  17192. ((stringp p) p)
  17193. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17194. (format "%% Package %s omitted" (cadr p)))
  17195. ((equal "" (car p))
  17196. (format "\\usepackage{%s}" (cadr p)))
  17197. (t
  17198. (format "\\usepackage[%s]{%s}"
  17199. (car p) (cadr p)))))
  17200. pkg
  17201. "\n"))
  17202. (if newline (concat pkg "\n") pkg))
  17203. (defun org-dvipng-color (attr)
  17204. "Return a RGB color specification for dvipng."
  17205. (apply 'format "rgb %s %s %s"
  17206. (mapcar 'org-normalize-color
  17207. (if (featurep 'xemacs)
  17208. (color-rgb-components
  17209. (face-property 'default
  17210. (cond ((eq attr :foreground) 'foreground)
  17211. ((eq attr :background) 'background))))
  17212. (color-values (face-attribute 'default attr nil))))))
  17213. (defun org-dvipng-color-format (color-name)
  17214. "Convert COLOR-NAME to a RGB color value for dvipng."
  17215. (apply 'format "rgb %s %s %s"
  17216. (mapcar 'org-normalize-color
  17217. (color-values color-name))))
  17218. (defun org-latex-color (attr)
  17219. "Return a RGB color for the LaTeX color package."
  17220. (apply 'format "%s,%s,%s"
  17221. (mapcar 'org-normalize-color
  17222. (if (featurep 'xemacs)
  17223. (color-rgb-components
  17224. (face-property 'default
  17225. (cond ((eq attr :foreground) 'foreground)
  17226. ((eq attr :background) 'background))))
  17227. (color-values (face-attribute 'default attr nil))))))
  17228. (defun org-latex-color-format (color-name)
  17229. "Convert COLOR-NAME to a RGB color value."
  17230. (apply 'format "%s,%s,%s"
  17231. (mapcar 'org-normalize-color
  17232. (color-values color-name))))
  17233. (defun org-normalize-color (value)
  17234. "Return string to be used as color value for an RGB component."
  17235. (format "%g" (/ value 65535.0)))
  17236. ;; Image display
  17237. (defvar org-inline-image-overlays nil)
  17238. (make-variable-buffer-local 'org-inline-image-overlays)
  17239. (defun org-toggle-inline-images (&optional include-linked)
  17240. "Toggle the display of inline images.
  17241. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17242. (interactive "P")
  17243. (if org-inline-image-overlays
  17244. (progn
  17245. (org-remove-inline-images)
  17246. (message "Inline image display turned off"))
  17247. (org-display-inline-images include-linked)
  17248. (if (and (org-called-interactively-p)
  17249. org-inline-image-overlays)
  17250. (message "%d images displayed inline"
  17251. (length org-inline-image-overlays))
  17252. (message "No images to display inline"))))
  17253. (defun org-redisplay-inline-images ()
  17254. "Refresh the display of inline images."
  17255. (interactive)
  17256. (if (not org-inline-image-overlays)
  17257. (org-toggle-inline-images)
  17258. (org-toggle-inline-images)
  17259. (org-toggle-inline-images)))
  17260. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17261. "Display inline images.
  17262. An inline image is a link which follows either of these
  17263. conventions:
  17264. 1. Its path is a file with an extension matching return value
  17265. from `image-file-name-regexp' and it has no contents.
  17266. 2. Its description consists in a single link of the previous
  17267. type.
  17268. When optional argument INCLUDE-LINKED is non-nil, also links with
  17269. a text description part will be inlined. This can be nice for
  17270. a quick look at those images, but it does not reflect what
  17271. exported files will look like.
  17272. When optional argument REFRESH is non-nil, refresh existing
  17273. images between BEG and END. This will create new image displays
  17274. only if necessary. BEG and END default to the buffer
  17275. boundaries."
  17276. (interactive "P")
  17277. (when (display-graphic-p)
  17278. (unless refresh
  17279. (org-remove-inline-images)
  17280. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17281. (org-with-wide-buffer
  17282. (goto-char (or beg (point-min)))
  17283. (let ((case-fold-search t)
  17284. (file-extension-re (org-image-file-name-regexp)))
  17285. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17286. (let ((link (save-match-data (org-element-context))))
  17287. ;; Check if we're at an inline image.
  17288. (when (and (equal (org-element-property :type link) "file")
  17289. (or include-linked
  17290. (not (org-element-property :contents-begin link)))
  17291. (let ((parent (org-element-property :parent link)))
  17292. (or (not (eq (org-element-type parent) 'link))
  17293. (not (cdr (org-element-contents parent)))))
  17294. (org-string-match-p file-extension-re
  17295. (org-element-property :path link)))
  17296. (let ((file (expand-file-name (org-element-property :path link))))
  17297. (when (file-exists-p file)
  17298. (let ((width
  17299. ;; Apply `org-image-actual-width' specifications.
  17300. (cond
  17301. ((not (image-type-available-p 'imagemagick)) nil)
  17302. ((eq org-image-actual-width t) nil)
  17303. ((listp org-image-actual-width)
  17304. (or
  17305. ;; First try to find a width among
  17306. ;; attributes associated to the paragraph
  17307. ;; containing link.
  17308. (let ((paragraph
  17309. (let ((e link))
  17310. (while (and (setq e (org-element-property
  17311. :parent e))
  17312. (not (eq (org-element-type e)
  17313. 'paragraph))))
  17314. e)))
  17315. (when paragraph
  17316. (save-excursion
  17317. (goto-char (org-element-property :begin paragraph))
  17318. (when (save-match-data
  17319. (re-search-forward
  17320. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17321. (org-element-property
  17322. :post-affiliated paragraph)
  17323. t))
  17324. (string-to-number (match-string 1))))))
  17325. ;; Otherwise, fall-back to provided number.
  17326. (car org-image-actual-width)))
  17327. ((numberp org-image-actual-width)
  17328. org-image-actual-width)))
  17329. (old (get-char-property-and-overlay
  17330. (org-element-property :begin link)
  17331. 'org-image-overlay)))
  17332. (if (and (car-safe old) refresh)
  17333. (image-refresh (overlay-get (cdr old) 'display))
  17334. (let ((image (save-match-data
  17335. (create-image file
  17336. (and width 'imagemagick)
  17337. nil
  17338. :width width))))
  17339. (when image
  17340. (let* ((link
  17341. ;; If inline image is the description
  17342. ;; of another link, be sure to
  17343. ;; consider the latter as the one to
  17344. ;; apply the overlay on.
  17345. (let ((parent
  17346. (org-element-property :parent link)))
  17347. (if (eq (org-element-type parent) 'link)
  17348. parent
  17349. link)))
  17350. (ov (make-overlay
  17351. (org-element-property :begin link)
  17352. (progn
  17353. (goto-char
  17354. (org-element-property :end link))
  17355. (skip-chars-backward " \t")
  17356. (point)))))
  17357. (overlay-put ov 'display image)
  17358. (overlay-put ov 'face 'default)
  17359. (overlay-put ov 'org-image-overlay t)
  17360. (overlay-put
  17361. ov 'modification-hooks
  17362. (list 'org-display-inline-remove-overlay))
  17363. (push ov org-inline-image-overlays)))))))))))))))
  17364. (define-obsolete-function-alias
  17365. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17366. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17367. "Remove inline-display overlay if a corresponding region is modified."
  17368. (let ((inhibit-modification-hooks t))
  17369. (when (and ov after)
  17370. (delete ov org-inline-image-overlays)
  17371. (delete-overlay ov))))
  17372. (defun org-remove-inline-images ()
  17373. "Remove inline display of images."
  17374. (interactive)
  17375. (mapc 'delete-overlay org-inline-image-overlays)
  17376. (setq org-inline-image-overlays nil))
  17377. ;;;; Key bindings
  17378. ;; Outline functions from `outline-mode-prefix-map'
  17379. ;; that can be remapped in Org:
  17380. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17381. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17382. (define-key org-mode-map [remap outline-forward-same-level]
  17383. 'org-forward-heading-same-level)
  17384. (define-key org-mode-map [remap outline-backward-same-level]
  17385. 'org-backward-heading-same-level)
  17386. (define-key org-mode-map [remap show-branches]
  17387. 'org-kill-note-or-show-branches)
  17388. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17389. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17390. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17391. ;; Outline functions from `outline-mode-prefix-map' that can not
  17392. ;; be remapped in Org:
  17393. ;;
  17394. ;; - the column "key binding" shows whether the Outline function is still
  17395. ;; available in Org mode on the same key that it has been bound to in
  17396. ;; Outline mode:
  17397. ;; - "overridden": key used for a different functionality in Org mode
  17398. ;; - else: key still bound to the same Outline function in Org mode
  17399. ;;
  17400. ;; | Outline function | key binding | Org replacement |
  17401. ;; |------------------------------------+-------------+-----------------------|
  17402. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17403. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17404. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17405. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17406. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17407. ;; | `show-entry' | overridden | no replacement |
  17408. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17409. ;; | `show-branches' | `C-c C-k' | still same function |
  17410. ;; | `show-subtree' | overridden | visibility cycling |
  17411. ;; | `show-all' | overridden | no replacement |
  17412. ;; | `hide-subtree' | overridden | visibility cycling |
  17413. ;; | `hide-body' | overridden | no replacement |
  17414. ;; | `hide-entry' | overridden | visibility cycling |
  17415. ;; | `hide-leaves' | overridden | no replacement |
  17416. ;; | `hide-sublevels' | overridden | no replacement |
  17417. ;; | `hide-other' | overridden | no replacement |
  17418. ;; Make `C-c C-x' a prefix key
  17419. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17420. ;; TAB key with modifiers
  17421. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17422. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17423. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17424. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17425. ;; The following line is necessary under Suse GNU/Linux
  17426. (unless (featurep 'xemacs)
  17427. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17428. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17429. (define-key org-mode-map [backtab] 'org-shifttab)
  17430. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17431. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17432. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17433. ;; Cursor keys with modifiers
  17434. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17435. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17436. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17437. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17438. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17439. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17440. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17441. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17442. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17443. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17444. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17445. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17446. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17447. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17448. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17449. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17450. ;; Babel keys
  17451. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17452. (mapc (lambda (pair)
  17453. (define-key org-babel-map (car pair) (cdr pair)))
  17454. org-babel-key-bindings)
  17455. ;;; Extra keys for tty access.
  17456. ;; We only set them when really needed because otherwise the
  17457. ;; menus don't show the simple keys
  17458. (when (or org-use-extra-keys
  17459. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17460. (not window-system))
  17461. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17462. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17463. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17464. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17465. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17466. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17467. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17468. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17469. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17470. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17471. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17472. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17473. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17474. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17475. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17476. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17477. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17478. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17479. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17480. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17481. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17482. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17483. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17484. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17485. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17486. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17487. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17488. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17489. ;; All the other keys
  17490. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17491. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17492. (if (boundp 'narrow-map)
  17493. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17494. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17495. (if (boundp 'narrow-map)
  17496. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17497. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17498. (if (boundp 'narrow-map)
  17499. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17500. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17501. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17502. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17503. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17504. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17505. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17506. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17507. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17508. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17509. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17510. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17511. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17512. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17513. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17514. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17515. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17516. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17517. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17518. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17519. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17520. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17521. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17522. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17523. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17524. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17525. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17526. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17527. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17528. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17529. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17530. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17531. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17532. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17533. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17534. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17535. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17536. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17537. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17538. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17539. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17540. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17541. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17542. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17543. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17544. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17545. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17546. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17547. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17548. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17549. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17550. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17551. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17552. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17553. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17554. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17555. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17556. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17557. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17558. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17559. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17560. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17561. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17562. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17563. (org-defkey org-mode-map "\C-m" 'org-return)
  17564. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17565. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17566. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17567. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17568. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17569. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17570. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17571. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17572. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17573. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17574. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17575. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17576. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17577. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17578. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17579. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17580. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17581. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17582. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17583. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17584. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17585. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17586. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17587. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17588. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17589. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17590. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17591. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17592. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17593. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17594. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17595. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17596. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17597. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17598. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17599. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17600. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17601. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17602. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17603. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17604. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17605. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17606. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17607. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17608. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17609. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17610. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17611. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17612. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17613. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17614. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17615. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17616. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17617. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17618. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17619. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17620. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17621. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17622. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17623. (when (featurep 'xemacs)
  17624. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17625. (defconst org-speed-commands-default
  17626. '(
  17627. ("Outline Navigation")
  17628. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17629. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17630. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17631. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17632. ("F" . org-next-block)
  17633. ("B" . org-previous-block)
  17634. ("u" . (org-speed-move-safe 'outline-up-heading))
  17635. ("j" . org-goto)
  17636. ("g" . (org-refile t))
  17637. ("Outline Visibility")
  17638. ("c" . org-cycle)
  17639. ("C" . org-shifttab)
  17640. (" " . org-display-outline-path)
  17641. ("s" . org-narrow-to-subtree)
  17642. ("=" . org-columns)
  17643. ("Outline Structure Editing")
  17644. ("U" . org-shiftmetaup)
  17645. ("D" . org-shiftmetadown)
  17646. ("r" . org-metaright)
  17647. ("l" . org-metaleft)
  17648. ("R" . org-shiftmetaright)
  17649. ("L" . org-shiftmetaleft)
  17650. ("i" . (progn (forward-char 1) (call-interactively
  17651. 'org-insert-heading-respect-content)))
  17652. ("^" . org-sort)
  17653. ("w" . org-refile)
  17654. ("a" . org-archive-subtree-default-with-confirmation)
  17655. ("@" . org-mark-subtree)
  17656. ("#" . org-toggle-comment)
  17657. ("Clock Commands")
  17658. ("I" . org-clock-in)
  17659. ("O" . org-clock-out)
  17660. ("Meta Data Editing")
  17661. ("t" . org-todo)
  17662. ("," . (org-priority))
  17663. ("0" . (org-priority ?\ ))
  17664. ("1" . (org-priority ?A))
  17665. ("2" . (org-priority ?B))
  17666. ("3" . (org-priority ?C))
  17667. (":" . org-set-tags-command)
  17668. ("e" . org-set-effort)
  17669. ("E" . org-inc-effort)
  17670. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17671. (org-entry-put (point) "APPT_WARNTIME" m)))
  17672. ("Agenda Views etc")
  17673. ("v" . org-agenda)
  17674. ("/" . org-sparse-tree)
  17675. ("Misc")
  17676. ("o" . org-open-at-point)
  17677. ("?" . org-speed-command-help)
  17678. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17679. (">" . (org-agenda-remove-restriction-lock))
  17680. )
  17681. "The default speed commands.")
  17682. (defun org-print-speed-command (e)
  17683. (if (> (length (car e)) 1)
  17684. (progn
  17685. (princ "\n")
  17686. (princ (car e))
  17687. (princ "\n")
  17688. (princ (make-string (length (car e)) ?-))
  17689. (princ "\n"))
  17690. (princ (car e))
  17691. (princ " ")
  17692. (if (symbolp (cdr e))
  17693. (princ (symbol-name (cdr e)))
  17694. (prin1 (cdr e)))
  17695. (princ "\n")))
  17696. (defun org-speed-command-help ()
  17697. "Show the available speed commands."
  17698. (interactive)
  17699. (if (not org-use-speed-commands)
  17700. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17701. (with-output-to-temp-buffer "*Help*"
  17702. (princ "User-defined Speed commands\n===========================\n")
  17703. (mapc 'org-print-speed-command org-speed-commands-user)
  17704. (princ "\n")
  17705. (princ "Built-in Speed commands\n=======================\n")
  17706. (mapc 'org-print-speed-command org-speed-commands-default))
  17707. (with-current-buffer "*Help*"
  17708. (setq truncate-lines t))))
  17709. (defun org-speed-move-safe (cmd)
  17710. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17711. If not, return to the original position and throw an error."
  17712. (interactive)
  17713. (let ((pos (point)))
  17714. (call-interactively cmd)
  17715. (unless (and (bolp) (org-at-heading-p))
  17716. (goto-char pos)
  17717. (error "Boundary reached while executing %s" cmd))))
  17718. (defvar org-self-insert-command-undo-counter 0)
  17719. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17720. (defvar org-speed-command nil)
  17721. (define-obsolete-function-alias
  17722. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17723. (defun org-speed-command-activate (keys)
  17724. "Hook for activating single-letter speed commands.
  17725. `org-speed-commands-default' specifies a minimal command set.
  17726. Use `org-speed-commands-user' for further customization."
  17727. (when (or (and (bolp) (looking-at org-outline-regexp))
  17728. (and (functionp org-use-speed-commands)
  17729. (funcall org-use-speed-commands)))
  17730. (cdr (assoc keys (append org-speed-commands-user
  17731. org-speed-commands-default)))))
  17732. (define-obsolete-function-alias
  17733. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17734. (defun org-babel-speed-command-activate (keys)
  17735. "Hook for activating single-letter code block commands."
  17736. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17737. (cdr (assoc keys org-babel-key-bindings))))
  17738. (defcustom org-speed-command-hook
  17739. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17740. "Hook for activating speed commands at strategic locations.
  17741. Hook functions are called in sequence until a valid handler is
  17742. found.
  17743. Each hook takes a single argument, a user-pressed command key
  17744. which is also a `self-insert-command' from the global map.
  17745. Within the hook, examine the cursor position and the command key
  17746. and return nil or a valid handler as appropriate. Handler could
  17747. be one of an interactive command, a function, or a form.
  17748. Set `org-use-speed-commands' to non-nil value to enable this
  17749. hook. The default setting is `org-speed-command-activate'."
  17750. :group 'org-structure
  17751. :version "24.1"
  17752. :type 'hook)
  17753. (defun org-self-insert-command (N)
  17754. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17755. If the cursor is in a table looking at whitespace, the whitespace is
  17756. overwritten, and the table is not marked as requiring realignment."
  17757. (interactive "p")
  17758. (org-check-before-invisible-edit 'insert)
  17759. (cond
  17760. ((and org-use-speed-commands
  17761. (setq org-speed-command
  17762. (run-hook-with-args-until-success
  17763. 'org-speed-command-hook (this-command-keys))))
  17764. (cond
  17765. ((commandp org-speed-command)
  17766. (setq this-command org-speed-command)
  17767. (call-interactively org-speed-command))
  17768. ((functionp org-speed-command)
  17769. (funcall org-speed-command))
  17770. ((and org-speed-command (listp org-speed-command))
  17771. (eval org-speed-command))
  17772. (t (let (org-use-speed-commands)
  17773. (call-interactively 'org-self-insert-command)))))
  17774. ((and
  17775. (org-table-p)
  17776. (progn
  17777. ;; check if we blank the field, and if that triggers align
  17778. (and (featurep 'org-table) org-table-auto-blank-field
  17779. (member last-command
  17780. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17781. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17782. ;; got extra space, this field does not determine column width
  17783. (let (org-table-may-need-update) (org-table-blank-field))
  17784. ;; no extra space, this field may determine column width
  17785. (org-table-blank-field)))
  17786. t)
  17787. (eq N 1)
  17788. (looking-at "[^|\n]* |"))
  17789. (let (org-table-may-need-update)
  17790. (goto-char (1- (match-end 0)))
  17791. (backward-delete-char 1)
  17792. (goto-char (match-beginning 0))
  17793. (self-insert-command N)))
  17794. (t
  17795. (setq org-table-may-need-update t)
  17796. (self-insert-command N)
  17797. (org-fix-tags-on-the-fly)
  17798. (if org-self-insert-cluster-for-undo
  17799. (if (not (eq last-command 'org-self-insert-command))
  17800. (setq org-self-insert-command-undo-counter 1)
  17801. (if (>= org-self-insert-command-undo-counter 20)
  17802. (setq org-self-insert-command-undo-counter 1)
  17803. (and (> org-self-insert-command-undo-counter 0)
  17804. buffer-undo-list (listp buffer-undo-list)
  17805. (not (cadr buffer-undo-list)) ; remove nil entry
  17806. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17807. (setq org-self-insert-command-undo-counter
  17808. (1+ org-self-insert-command-undo-counter))))))))
  17809. (defun org-check-before-invisible-edit (kind)
  17810. "Check is editing if kind KIND would be dangerous with invisible text around.
  17811. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17812. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17813. (if (and org-catch-invisible-edits
  17814. (or (not (boundp 'visible-mode)) (not visible-mode))
  17815. (or (get-char-property (point) 'invisible)
  17816. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17817. ;; OK, we need to take a closer look
  17818. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17819. (invisible-before-point (if (bobp) nil (get-char-property
  17820. (1- (point)) 'invisible)))
  17821. (border-and-ok-direction
  17822. (or
  17823. ;; Check if we are acting predictably before invisible text
  17824. (and invisible-at-point (not invisible-before-point)
  17825. (memq kind '(insert delete-backward)))
  17826. ;; Check if we are acting predictably after invisible text
  17827. ;; This works not well, and I have turned it off. It seems
  17828. ;; better to always show and stop after invisible text.
  17829. ;; (and (not invisible-at-point) invisible-before-point
  17830. ;; (memq kind '(insert delete)))
  17831. )))
  17832. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17833. (memq invisible-before-point '(outline org-hide-block t)))
  17834. (if (eq org-catch-invisible-edits 'error)
  17835. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17836. (if (and org-custom-properties-overlays
  17837. (y-or-n-p "Display invisible properties in this buffer? "))
  17838. (org-toggle-custom-properties-visibility)
  17839. ;; Make the area visible
  17840. (save-excursion
  17841. (if invisible-before-point
  17842. (goto-char (previous-single-char-property-change
  17843. (point) 'invisible)))
  17844. (org-cycle))
  17845. (cond
  17846. ((eq org-catch-invisible-edits 'show)
  17847. ;; That's it, we do the edit after showing
  17848. (message
  17849. "Unfolding invisible region around point before editing")
  17850. (sit-for 1))
  17851. ((and (eq org-catch-invisible-edits 'smart)
  17852. border-and-ok-direction)
  17853. (message "Unfolding invisible region around point before editing"))
  17854. (t
  17855. ;; Don't do the edit, make the user repeat it in full visibility
  17856. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17857. (defun org-fix-tags-on-the-fly ()
  17858. (when (and (equal (char-after (point-at-bol)) ?*)
  17859. (org-at-heading-p))
  17860. (org-align-tags-here org-tags-column)))
  17861. (defun org-delete-backward-char (N)
  17862. "Like `delete-backward-char', insert whitespace at field end in tables.
  17863. When deleting backwards, in tables this function will insert whitespace in
  17864. front of the next \"|\" separator, to keep the table aligned. The table will
  17865. still be marked for re-alignment if the field did fill the entire column,
  17866. because, in this case the deletion might narrow the column."
  17867. (interactive "p")
  17868. (save-match-data
  17869. (org-check-before-invisible-edit 'delete-backward)
  17870. (if (and (org-table-p)
  17871. (eq N 1)
  17872. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17873. (looking-at ".*?|"))
  17874. (let ((pos (point))
  17875. (noalign (looking-at "[^|\n\r]* |"))
  17876. (c org-table-may-need-update))
  17877. (backward-delete-char N)
  17878. (if (not overwrite-mode)
  17879. (progn
  17880. (skip-chars-forward "^|")
  17881. (insert " ")
  17882. (goto-char (1- pos))))
  17883. ;; noalign: if there were two spaces at the end, this field
  17884. ;; does not determine the width of the column.
  17885. (if noalign (setq org-table-may-need-update c)))
  17886. (backward-delete-char N)
  17887. (org-fix-tags-on-the-fly))))
  17888. (defun org-delete-char (N)
  17889. "Like `delete-char', but insert whitespace at field end in tables.
  17890. When deleting characters, in tables this function will insert whitespace in
  17891. front of the next \"|\" separator, to keep the table aligned. The table will
  17892. still be marked for re-alignment if the field did fill the entire column,
  17893. because, in this case the deletion might narrow the column."
  17894. (interactive "p")
  17895. (save-match-data
  17896. (org-check-before-invisible-edit 'delete)
  17897. (if (and (org-table-p)
  17898. (not (bolp))
  17899. (not (= (char-after) ?|))
  17900. (eq N 1))
  17901. (if (looking-at ".*?|")
  17902. (let ((pos (point))
  17903. (noalign (looking-at "[^|\n\r]* |"))
  17904. (c org-table-may-need-update))
  17905. (replace-match
  17906. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17907. (goto-char pos)
  17908. ;; noalign: if there were two spaces at the end, this field
  17909. ;; does not determine the width of the column.
  17910. (if noalign (setq org-table-may-need-update c)))
  17911. (delete-char N))
  17912. (delete-char N)
  17913. (org-fix-tags-on-the-fly))))
  17914. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17915. (put 'org-self-insert-command 'delete-selection t)
  17916. (put 'orgtbl-self-insert-command 'delete-selection t)
  17917. (put 'org-delete-char 'delete-selection 'supersede)
  17918. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17919. (put 'org-yank 'delete-selection 'yank)
  17920. ;; Make `flyspell-mode' delay after some commands
  17921. (put 'org-self-insert-command 'flyspell-delayed t)
  17922. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17923. (put 'org-delete-char 'flyspell-delayed t)
  17924. (put 'org-delete-backward-char 'flyspell-delayed t)
  17925. ;; Make pabbrev-mode expand after org-mode commands
  17926. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17927. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17928. ;; How to do this: Measure non-white length of current string
  17929. ;; If equal to column width, we should realign.
  17930. (defun org-remap (map &rest commands)
  17931. "In MAP, remap the functions given in COMMANDS.
  17932. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17933. (let (new old)
  17934. (while commands
  17935. (setq old (pop commands) new (pop commands))
  17936. (if (fboundp 'command-remapping)
  17937. (org-defkey map (vector 'remap old) new)
  17938. (substitute-key-definition old new map global-map)))))
  17939. (defun org-transpose-words ()
  17940. "Transpose words for Org.
  17941. This uses the `org-mode-transpose-word-syntax-table' syntax
  17942. table, which interprets characters in `org-emphasis-alist' as
  17943. word constituants."
  17944. (interactive)
  17945. (with-syntax-table org-mode-transpose-word-syntax-table
  17946. (call-interactively 'transpose-words)))
  17947. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17948. (when (eq org-enable-table-editor 'optimized)
  17949. ;; If the user wants maximum table support, we need to hijack
  17950. ;; some standard editing functions
  17951. (org-remap org-mode-map
  17952. 'self-insert-command 'org-self-insert-command
  17953. 'delete-char 'org-delete-char
  17954. 'delete-backward-char 'org-delete-backward-char)
  17955. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17956. (defvar org-ctrl-c-ctrl-c-hook nil
  17957. "Hook for functions attaching themselves to `C-c C-c'.
  17958. This can be used to add additional functionality to the C-c C-c
  17959. key which executes context-dependent commands. This hook is run
  17960. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17961. run after the last test.
  17962. Each function will be called with no arguments. The function
  17963. must check if the context is appropriate for it to act. If yes,
  17964. it should do its thing and then return a non-nil value. If the
  17965. context is wrong, just do nothing and return nil.")
  17966. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17967. "Hook for functions attaching themselves to `C-c C-c'.
  17968. This can be used to add additional functionality to the C-c C-c
  17969. key which executes context-dependent commands. This hook is run
  17970. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17971. before the first test.
  17972. Each function will be called with no arguments. The function
  17973. must check if the context is appropriate for it to act. If yes,
  17974. it should do its thing and then return a non-nil value. If the
  17975. context is wrong, just do nothing and return nil.")
  17976. (defvar org-tab-first-hook nil
  17977. "Hook for functions to attach themselves to TAB.
  17978. See `org-ctrl-c-ctrl-c-hook' for more information.
  17979. This hook runs as the first action when TAB is pressed, even before
  17980. `org-cycle' messes around with the `outline-regexp' to cater for
  17981. inline tasks and plain list item folding.
  17982. If any function in this hook returns t, any other actions that
  17983. would have been caused by TAB (such as table field motion or visibility
  17984. cycling) will not occur.")
  17985. (defvar org-tab-after-check-for-table-hook nil
  17986. "Hook for functions to attach themselves to TAB.
  17987. See `org-ctrl-c-ctrl-c-hook' for more information.
  17988. This hook runs after it has been established that the cursor is not in a
  17989. table, but before checking if the cursor is in a headline or if global cycling
  17990. should be done.
  17991. If any function in this hook returns t, not other actions like visibility
  17992. cycling will be done.")
  17993. (defvar org-tab-after-check-for-cycling-hook nil
  17994. "Hook for functions to attach themselves to TAB.
  17995. See `org-ctrl-c-ctrl-c-hook' for more information.
  17996. This hook runs after it has been established that not table field motion and
  17997. not visibility should be done because of current context. This is probably
  17998. the place where a package like yasnippets can hook in.")
  17999. (defvar org-tab-before-tab-emulation-hook nil
  18000. "Hook for functions to attach themselves to TAB.
  18001. See `org-ctrl-c-ctrl-c-hook' for more information.
  18002. This hook runs after every other options for TAB have been exhausted, but
  18003. before indentation and \t insertion takes place.")
  18004. (defvar org-metaleft-hook nil
  18005. "Hook for functions attaching themselves to `M-left'.
  18006. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18007. (defvar org-metaright-hook nil
  18008. "Hook for functions attaching themselves to `M-right'.
  18009. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18010. (defvar org-metaup-hook nil
  18011. "Hook for functions attaching themselves to `M-up'.
  18012. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18013. (defvar org-metadown-hook nil
  18014. "Hook for functions attaching themselves to `M-down'.
  18015. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18016. (defvar org-shiftmetaleft-hook nil
  18017. "Hook for functions attaching themselves to `M-S-left'.
  18018. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18019. (defvar org-shiftmetaright-hook nil
  18020. "Hook for functions attaching themselves to `M-S-right'.
  18021. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18022. (defvar org-shiftmetaup-hook nil
  18023. "Hook for functions attaching themselves to `M-S-up'.
  18024. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18025. (defvar org-shiftmetadown-hook nil
  18026. "Hook for functions attaching themselves to `M-S-down'.
  18027. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18028. (defvar org-metareturn-hook nil
  18029. "Hook for functions attaching themselves to `M-RET'.
  18030. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18031. (defvar org-shiftup-hook nil
  18032. "Hook for functions attaching themselves to `S-up'.
  18033. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18034. (defvar org-shiftup-final-hook nil
  18035. "Hook for functions attaching themselves to `S-up'.
  18036. This one runs after all other options except shift-select have been excluded.
  18037. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18038. (defvar org-shiftdown-hook nil
  18039. "Hook for functions attaching themselves to `S-down'.
  18040. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18041. (defvar org-shiftdown-final-hook nil
  18042. "Hook for functions attaching themselves to `S-down'.
  18043. This one runs after all other options except shift-select have been excluded.
  18044. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18045. (defvar org-shiftleft-hook nil
  18046. "Hook for functions attaching themselves to `S-left'.
  18047. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18048. (defvar org-shiftleft-final-hook nil
  18049. "Hook for functions attaching themselves to `S-left'.
  18050. This one runs after all other options except shift-select have been excluded.
  18051. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18052. (defvar org-shiftright-hook nil
  18053. "Hook for functions attaching themselves to `S-right'.
  18054. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18055. (defvar org-shiftright-final-hook nil
  18056. "Hook for functions attaching themselves to `S-right'.
  18057. This one runs after all other options except shift-select have been excluded.
  18058. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18059. (defun org-modifier-cursor-error ()
  18060. "Throw an error, a modified cursor command was applied in wrong context."
  18061. (user-error "This command is active in special context like tables, headlines or items"))
  18062. (defun org-shiftselect-error ()
  18063. "Throw an error because Shift-Cursor command was applied in wrong context."
  18064. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18065. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18066. (user-error "This command works only in special context like headlines or timestamps")))
  18067. (defun org-call-for-shift-select (cmd)
  18068. (let ((this-command-keys-shift-translated t))
  18069. (call-interactively cmd)))
  18070. (defun org-shifttab (&optional arg)
  18071. "Global visibility cycling or move to previous table field.
  18072. Call `org-table-previous-field' within a table.
  18073. When ARG is nil, cycle globally through visibility states.
  18074. When ARG is a numeric prefix, show contents of this level."
  18075. (interactive "P")
  18076. (cond
  18077. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18078. ((integerp arg)
  18079. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18080. (message "Content view to level: %d" arg)
  18081. (org-content (prefix-numeric-value arg2))
  18082. (org-cycle-show-empty-lines t)
  18083. (setq org-cycle-global-status 'overview)))
  18084. (t (call-interactively 'org-global-cycle))))
  18085. (defun org-shiftmetaleft ()
  18086. "Promote subtree or delete table column.
  18087. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18088. `org-table-delete-column', depending on context. See the
  18089. individual commands for more information."
  18090. (interactive)
  18091. (cond
  18092. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18093. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18094. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18095. ((if (not (org-region-active-p)) (org-at-item-p)
  18096. (save-excursion (goto-char (region-beginning))
  18097. (org-at-item-p)))
  18098. (call-interactively 'org-outdent-item-tree))
  18099. (t (org-modifier-cursor-error))))
  18100. (defun org-shiftmetaright ()
  18101. "Demote subtree or insert table column.
  18102. Calls `org-demote-subtree', `org-indent-item-tree', or
  18103. `org-table-insert-column', depending on context. See the
  18104. individual commands for more information."
  18105. (interactive)
  18106. (cond
  18107. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18108. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18109. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18110. ((if (not (org-region-active-p)) (org-at-item-p)
  18111. (save-excursion (goto-char (region-beginning))
  18112. (org-at-item-p)))
  18113. (call-interactively 'org-indent-item-tree))
  18114. (t (org-modifier-cursor-error))))
  18115. (defun org-shiftmetaup (&optional arg)
  18116. "Move subtree up or kill table row.
  18117. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18118. `org-move-item-up' or `org-timestamp-up', depending on context.
  18119. See the individual commands for more information."
  18120. (interactive "P")
  18121. (cond
  18122. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18123. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18124. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18125. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18126. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18127. (call-interactively 'org-timestamp-up)))
  18128. (t (call-interactively 'org-drag-line-backward))))
  18129. (defun org-shiftmetadown (&optional arg)
  18130. "Move subtree down or insert table row.
  18131. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18132. `org-move-item-down' or `org-timestamp-up', depending on context.
  18133. See the individual commands for more information."
  18134. (interactive "P")
  18135. (cond
  18136. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18137. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18138. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18139. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18140. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18141. (call-interactively 'org-timestamp-down)))
  18142. (t (call-interactively 'org-drag-line-forward))))
  18143. (defsubst org-hidden-tree-error ()
  18144. (user-error
  18145. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18146. (defun org-metaleft (&optional arg)
  18147. "Promote heading or move table column to left.
  18148. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18149. With no specific context, calls the Emacs default `backward-word'.
  18150. See the individual commands for more information."
  18151. (interactive "P")
  18152. (cond
  18153. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18154. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18155. ((org-with-limited-levels
  18156. (or (org-at-heading-p)
  18157. (and (org-region-active-p)
  18158. (save-excursion
  18159. (goto-char (region-beginning))
  18160. (org-at-heading-p)))))
  18161. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18162. (call-interactively 'org-do-promote))
  18163. ;; At an inline task.
  18164. ((org-at-heading-p)
  18165. (call-interactively 'org-inlinetask-promote))
  18166. ((or (org-at-item-p)
  18167. (and (org-region-active-p)
  18168. (save-excursion
  18169. (goto-char (region-beginning))
  18170. (org-at-item-p))))
  18171. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18172. (call-interactively 'org-outdent-item))
  18173. (t (call-interactively 'backward-word))))
  18174. (defun org-metaright (&optional arg)
  18175. "Demote a subtree, a list item or move table column to right.
  18176. In front of a drawer or a block keyword, indent it correctly.
  18177. With no specific context, calls the Emacs default `forward-word'.
  18178. See the individual commands for more information."
  18179. (interactive "P")
  18180. (cond
  18181. ((run-hook-with-args-until-success 'org-metaright-hook))
  18182. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18183. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18184. ((org-at-block-p) (call-interactively 'org-indent-block))
  18185. ((org-with-limited-levels
  18186. (or (org-at-heading-p)
  18187. (and (org-region-active-p)
  18188. (save-excursion
  18189. (goto-char (region-beginning))
  18190. (org-at-heading-p)))))
  18191. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18192. (call-interactively 'org-do-demote))
  18193. ;; At an inline task.
  18194. ((org-at-heading-p)
  18195. (call-interactively 'org-inlinetask-demote))
  18196. ((or (org-at-item-p)
  18197. (and (org-region-active-p)
  18198. (save-excursion
  18199. (goto-char (region-beginning))
  18200. (org-at-item-p))))
  18201. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18202. (call-interactively 'org-indent-item))
  18203. (t (call-interactively 'forward-word))))
  18204. (defun org-check-for-hidden (what)
  18205. "Check if there are hidden headlines/items in the current visual line.
  18206. WHAT can be either `headlines' or `items'. If the current line is
  18207. an outline or item heading and it has a folded subtree below it,
  18208. this function returns t, nil otherwise."
  18209. (let ((re (cond
  18210. ((eq what 'headlines) org-outline-regexp-bol)
  18211. ((eq what 'items) (org-item-beginning-re))
  18212. (t (error "This should not happen"))))
  18213. beg end)
  18214. (save-excursion
  18215. (catch 'exit
  18216. (unless (org-region-active-p)
  18217. (setq beg (point-at-bol))
  18218. (beginning-of-line 2)
  18219. (while (and (not (eobp)) ;; this is like `next-line'
  18220. (get-char-property (1- (point)) 'invisible))
  18221. (beginning-of-line 2))
  18222. (setq end (point))
  18223. (goto-char beg)
  18224. (goto-char (point-at-eol))
  18225. (setq end (max end (point)))
  18226. (while (re-search-forward re end t)
  18227. (if (get-char-property (match-beginning 0) 'invisible)
  18228. (throw 'exit t))))
  18229. nil))))
  18230. (defun org-metaup (&optional arg)
  18231. "Move subtree up or move table row up.
  18232. Calls `org-move-subtree-up' or `org-table-move-row' or
  18233. `org-move-item-up', depending on context. See the individual commands
  18234. for more information."
  18235. (interactive "P")
  18236. (cond
  18237. ((run-hook-with-args-until-success 'org-metaup-hook))
  18238. ((org-region-active-p)
  18239. (let* ((a (min (region-beginning) (region-end)))
  18240. (b (1- (max (region-beginning) (region-end))))
  18241. (c (save-excursion (goto-char a)
  18242. (move-beginning-of-line 0)))
  18243. (d (save-excursion (goto-char a)
  18244. (move-end-of-line 0) (point))))
  18245. (transpose-regions a b c d)
  18246. (goto-char c)))
  18247. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18248. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18249. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18250. (t (org-drag-element-backward))))
  18251. (defun org-metadown (&optional arg)
  18252. "Move subtree down or move table row down.
  18253. Calls `org-move-subtree-down' or `org-table-move-row' or
  18254. `org-move-item-down', depending on context. See the individual
  18255. commands for more information."
  18256. (interactive "P")
  18257. (cond
  18258. ((run-hook-with-args-until-success 'org-metadown-hook))
  18259. ((org-region-active-p)
  18260. (let* ((a (min (region-beginning) (region-end)))
  18261. (b (max (region-beginning) (region-end)))
  18262. (c (save-excursion (goto-char b)
  18263. (move-beginning-of-line 1)))
  18264. (d (save-excursion (goto-char b)
  18265. (move-end-of-line 1) (1+ (point)))))
  18266. (transpose-regions a b c d)
  18267. (goto-char d)))
  18268. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18269. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18270. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18271. (t (org-drag-element-forward))))
  18272. (defun org-shiftup (&optional arg)
  18273. "Increase item in timestamp or increase priority of current headline.
  18274. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18275. depending on context. See the individual commands for more information."
  18276. (interactive "P")
  18277. (cond
  18278. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18279. ((and org-support-shift-select (org-region-active-p))
  18280. (org-call-for-shift-select 'previous-line))
  18281. ((org-at-timestamp-p t)
  18282. (call-interactively (if org-edit-timestamp-down-means-later
  18283. 'org-timestamp-down 'org-timestamp-up)))
  18284. ((and (not (eq org-support-shift-select 'always))
  18285. org-enable-priority-commands
  18286. (org-at-heading-p))
  18287. (call-interactively 'org-priority-up))
  18288. ((and (not org-support-shift-select) (org-at-item-p))
  18289. (call-interactively 'org-previous-item))
  18290. ((org-clocktable-try-shift 'up arg))
  18291. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18292. (org-support-shift-select
  18293. (org-call-for-shift-select 'previous-line))
  18294. (t (org-shiftselect-error))))
  18295. (defun org-shiftdown (&optional arg)
  18296. "Decrease item in timestamp or decrease priority of current headline.
  18297. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18298. depending on context. See the individual commands for more information."
  18299. (interactive "P")
  18300. (cond
  18301. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18302. ((and org-support-shift-select (org-region-active-p))
  18303. (org-call-for-shift-select 'next-line))
  18304. ((org-at-timestamp-p t)
  18305. (call-interactively (if org-edit-timestamp-down-means-later
  18306. 'org-timestamp-up 'org-timestamp-down)))
  18307. ((and (not (eq org-support-shift-select 'always))
  18308. org-enable-priority-commands
  18309. (org-at-heading-p))
  18310. (call-interactively 'org-priority-down))
  18311. ((and (not org-support-shift-select) (org-at-item-p))
  18312. (call-interactively 'org-next-item))
  18313. ((org-clocktable-try-shift 'down arg))
  18314. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18315. (org-support-shift-select
  18316. (org-call-for-shift-select 'next-line))
  18317. (t (org-shiftselect-error))))
  18318. (defun org-shiftright (&optional arg)
  18319. "Cycle the thing at point or in the current line, depending on context.
  18320. Depending on context, this does one of the following:
  18321. - switch a timestamp at point one day into the future
  18322. - on a headline, switch to the next TODO keyword.
  18323. - on an item, switch entire list to the next bullet type
  18324. - on a property line, switch to the next allowed value
  18325. - on a clocktable definition line, move time block into the future"
  18326. (interactive "P")
  18327. (cond
  18328. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18329. ((and org-support-shift-select (org-region-active-p))
  18330. (org-call-for-shift-select 'forward-char))
  18331. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18332. ((and (not (eq org-support-shift-select 'always))
  18333. (org-at-heading-p))
  18334. (let ((org-inhibit-logging
  18335. (not org-treat-S-cursor-todo-selection-as-state-change))
  18336. (org-inhibit-blocking
  18337. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18338. (org-call-with-arg 'org-todo 'right)))
  18339. ((or (and org-support-shift-select
  18340. (not (eq org-support-shift-select 'always))
  18341. (org-at-item-bullet-p))
  18342. (and (not org-support-shift-select) (org-at-item-p)))
  18343. (org-call-with-arg 'org-cycle-list-bullet nil))
  18344. ((and (not (eq org-support-shift-select 'always))
  18345. (org-at-property-p))
  18346. (call-interactively 'org-property-next-allowed-value))
  18347. ((org-clocktable-try-shift 'right arg))
  18348. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18349. (org-support-shift-select
  18350. (org-call-for-shift-select 'forward-char))
  18351. (t (org-shiftselect-error))))
  18352. (defun org-shiftleft (&optional arg)
  18353. "Cycle the thing at point or in the current line, depending on context.
  18354. Depending on context, this does one of the following:
  18355. - switch a timestamp at point one day into the past
  18356. - on a headline, switch to the previous TODO keyword.
  18357. - on an item, switch entire list to the previous bullet type
  18358. - on a property line, switch to the previous allowed value
  18359. - on a clocktable definition line, move time block into the past"
  18360. (interactive "P")
  18361. (cond
  18362. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18363. ((and org-support-shift-select (org-region-active-p))
  18364. (org-call-for-shift-select 'backward-char))
  18365. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18366. ((and (not (eq org-support-shift-select 'always))
  18367. (org-at-heading-p))
  18368. (let ((org-inhibit-logging
  18369. (not org-treat-S-cursor-todo-selection-as-state-change))
  18370. (org-inhibit-blocking
  18371. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18372. (org-call-with-arg 'org-todo 'left)))
  18373. ((or (and org-support-shift-select
  18374. (not (eq org-support-shift-select 'always))
  18375. (org-at-item-bullet-p))
  18376. (and (not org-support-shift-select) (org-at-item-p)))
  18377. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18378. ((and (not (eq org-support-shift-select 'always))
  18379. (org-at-property-p))
  18380. (call-interactively 'org-property-previous-allowed-value))
  18381. ((org-clocktable-try-shift 'left arg))
  18382. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18383. (org-support-shift-select
  18384. (org-call-for-shift-select 'backward-char))
  18385. (t (org-shiftselect-error))))
  18386. (defun org-shiftcontrolright ()
  18387. "Switch to next TODO set."
  18388. (interactive)
  18389. (cond
  18390. ((and org-support-shift-select (org-region-active-p))
  18391. (org-call-for-shift-select 'forward-word))
  18392. ((and (not (eq org-support-shift-select 'always))
  18393. (org-at-heading-p))
  18394. (org-call-with-arg 'org-todo 'nextset))
  18395. (org-support-shift-select
  18396. (org-call-for-shift-select 'forward-word))
  18397. (t (org-shiftselect-error))))
  18398. (defun org-shiftcontrolleft ()
  18399. "Switch to previous TODO set."
  18400. (interactive)
  18401. (cond
  18402. ((and org-support-shift-select (org-region-active-p))
  18403. (org-call-for-shift-select 'backward-word))
  18404. ((and (not (eq org-support-shift-select 'always))
  18405. (org-at-heading-p))
  18406. (org-call-with-arg 'org-todo 'previousset))
  18407. (org-support-shift-select
  18408. (org-call-for-shift-select 'backward-word))
  18409. (t (org-shiftselect-error))))
  18410. (defun org-shiftcontrolup (&optional n)
  18411. "Change timestamps synchronously up in CLOCK log lines.
  18412. Optional argument N tells to change by that many units."
  18413. (interactive "P")
  18414. (cond ((and (not org-support-shift-select)
  18415. (org-at-clock-log-p)
  18416. (org-at-timestamp-p t))
  18417. (org-clock-timestamps-up n))
  18418. (t (org-shiftselect-error))))
  18419. (defun org-shiftcontroldown (&optional n)
  18420. "Change timestamps synchronously down in CLOCK log lines.
  18421. Optional argument N tells to change by that many units."
  18422. (interactive "P")
  18423. (cond ((and (not org-support-shift-select)
  18424. (org-at-clock-log-p)
  18425. (org-at-timestamp-p t))
  18426. (org-clock-timestamps-down n))
  18427. (t (org-shiftselect-error))))
  18428. (defun org-ctrl-c-ret ()
  18429. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18430. (interactive)
  18431. (cond
  18432. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18433. (t (call-interactively 'org-insert-heading))))
  18434. (defun org-find-visible ()
  18435. (let ((s (point)))
  18436. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18437. (get-char-property s 'invisible)))
  18438. s))
  18439. (defun org-find-invisible ()
  18440. (let ((s (point)))
  18441. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18442. (not (get-char-property s 'invisible))))
  18443. s))
  18444. (defun org-copy-visible (beg end)
  18445. "Copy the visible parts of the region."
  18446. (interactive "r")
  18447. (let (snippets s)
  18448. (save-excursion
  18449. (save-restriction
  18450. (narrow-to-region beg end)
  18451. (setq s (goto-char (point-min)))
  18452. (while (not (= (point) (point-max)))
  18453. (goto-char (org-find-invisible))
  18454. (push (buffer-substring s (point)) snippets)
  18455. (setq s (goto-char (org-find-visible))))))
  18456. (kill-new (apply 'concat (nreverse snippets)))))
  18457. (defun org-copy-special ()
  18458. "Copy region in table or copy current subtree.
  18459. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18460. See the individual commands for more information."
  18461. (interactive)
  18462. (call-interactively
  18463. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18464. (defun org-cut-special ()
  18465. "Cut region in table or cut current subtree.
  18466. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18467. See the individual commands for more information."
  18468. (interactive)
  18469. (call-interactively
  18470. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18471. (defun org-paste-special (arg)
  18472. "Paste rectangular region into table, or past subtree relative to level.
  18473. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18474. See the individual commands for more information."
  18475. (interactive "P")
  18476. (if (org-at-table-p)
  18477. (org-table-paste-rectangle)
  18478. (org-paste-subtree arg)))
  18479. (defsubst org-in-fixed-width-region-p ()
  18480. "Is point in a fixed-width region?"
  18481. (save-match-data
  18482. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18483. (defun org-edit-special (&optional arg)
  18484. "Call a special editor for the element at point.
  18485. When at a table, call the formula editor with `org-table-edit-formulas'.
  18486. When in a source code block, call `org-edit-src-code'.
  18487. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18488. When at an #+INCLUDE keyword, visit the included file.
  18489. On a link, call `ffap' to visit the link at point.
  18490. Otherwise, return a user error."
  18491. (interactive "P")
  18492. (let ((element (org-element-at-point)))
  18493. (assert (not buffer-read-only) nil
  18494. "Buffer is read-only: %s" (buffer-name))
  18495. (case (org-element-type element)
  18496. (src-block
  18497. (if (not arg) (org-edit-src-code)
  18498. (let* ((info (org-babel-get-src-block-info))
  18499. (lang (nth 0 info))
  18500. (params (nth 2 info))
  18501. (session (cdr (assq :session params))))
  18502. (if (not session) (org-edit-src-code)
  18503. ;; At a src-block with a session and function called with
  18504. ;; an ARG: switch to the buffer related to the inferior
  18505. ;; process.
  18506. (switch-to-buffer
  18507. (funcall (intern (concat "org-babel-prep-session:" lang))
  18508. session params))))))
  18509. (keyword
  18510. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18511. (find-file
  18512. (org-remove-double-quotes
  18513. (car (org-split-string (org-element-property :value element)))))
  18514. (user-error "No special environment to edit here")))
  18515. (table
  18516. (if (eq (org-element-property :type element) 'table.el)
  18517. (org-edit-src-code)
  18518. (call-interactively 'org-table-edit-formulas)))
  18519. ;; Only Org tables contain `table-row' type elements.
  18520. (table-row (call-interactively 'org-table-edit-formulas))
  18521. ((example-block export-block) (org-edit-src-code))
  18522. (fixed-width (org-edit-fixed-width-region))
  18523. (otherwise
  18524. ;; No notable element at point. Though, we may be at a link,
  18525. ;; which is an object. Thus, scan deeper.
  18526. (if (eq (org-element-type (org-element-context element)) 'link)
  18527. (call-interactively 'ffap)
  18528. (user-error "No special environment to edit here"))))))
  18529. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18530. (defun org-ctrl-c-ctrl-c (&optional arg)
  18531. "Set tags in headline, or update according to changed information at point.
  18532. This command does many different things, depending on context:
  18533. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18534. this is what we do.
  18535. - If the cursor is on a statistics cookie, update it.
  18536. - If the cursor is in a headline, prompt for tags and insert them
  18537. into the current line, aligned to `org-tags-column'. When called
  18538. with prefix arg, realign all tags in the current buffer.
  18539. - If the cursor is in one of the special #+KEYWORD lines, this
  18540. triggers scanning the buffer for these lines and updating the
  18541. information.
  18542. - If the cursor is inside a table, realign the table. This command
  18543. works even if the automatic table editor has been turned off.
  18544. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18545. the entire table.
  18546. - If the cursor is at a footnote reference or definition, jump to
  18547. the corresponding definition or references, respectively.
  18548. - If the cursor is a the beginning of a dynamic block, update it.
  18549. - If the current buffer is a capture buffer, close note and file it.
  18550. - If the cursor is on a <<<target>>>, update radio targets and
  18551. corresponding links in this buffer.
  18552. - If the cursor is on a numbered item in a plain list, renumber the
  18553. ordered list.
  18554. - If the cursor is on a checkbox, toggle it.
  18555. - If the cursor is on a code block, evaluate it. The variable
  18556. `org-confirm-babel-evaluate' can be used to control prompting
  18557. before code block evaluation, by default every code block
  18558. evaluation requires confirmation. Code block evaluation can be
  18559. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18560. (interactive "P")
  18561. (cond
  18562. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18563. org-occur-highlights
  18564. org-latex-fragment-image-overlays)
  18565. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18566. (org-remove-occur-highlights)
  18567. (org-remove-latex-fragment-image-overlays)
  18568. (message "Temporary highlights/overlays removed from current buffer"))
  18569. ((and (local-variable-p 'org-finish-function (current-buffer))
  18570. (fboundp org-finish-function))
  18571. (funcall org-finish-function))
  18572. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18573. (t
  18574. (let* ((context (org-element-context)) (type (org-element-type context)))
  18575. ;; Test if point is within a blank line.
  18576. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18577. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18578. (user-error "C-c C-c can do nothing useful at this location"))
  18579. ;; When at a link, act according to the parent instead.
  18580. (when (eq type 'link)
  18581. (setq context (org-element-property :parent context))
  18582. (setq type (org-element-type context)))
  18583. ;; For convenience: at the first line of a paragraph on the
  18584. ;; same line as an item, apply function on that item instead.
  18585. (when (eq type 'paragraph)
  18586. (let ((parent (org-element-property :parent context)))
  18587. (when (and (eq (org-element-type parent) 'item)
  18588. (= (point-at-bol) (org-element-property :begin parent)))
  18589. (setq context parent type 'item))))
  18590. ;; Act according to type of element or object at point.
  18591. (case type
  18592. (clock (org-clock-update-time-maybe))
  18593. (dynamic-block
  18594. (save-excursion
  18595. (goto-char (org-element-property :post-affiliated context))
  18596. (org-update-dblock)))
  18597. (footnote-definition
  18598. (goto-char (org-element-property :post-affiliated context))
  18599. (call-interactively 'org-footnote-action))
  18600. (footnote-reference (call-interactively 'org-footnote-action))
  18601. ((headline inlinetask)
  18602. (save-excursion (goto-char (org-element-property :begin context))
  18603. (call-interactively 'org-set-tags)))
  18604. (item
  18605. ;; At an item: a double C-u set checkbox to "[-]"
  18606. ;; unconditionally, whereas a single one will toggle its
  18607. ;; presence. Without an universal argument, if the item
  18608. ;; has a checkbox, toggle it. Otherwise repair the list.
  18609. (let* ((box (org-element-property :checkbox context))
  18610. (struct (org-element-property :structure context))
  18611. (old-struct (copy-tree struct))
  18612. (parents (org-list-parents-alist struct))
  18613. (prevs (org-list-prevs-alist struct))
  18614. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18615. (org-list-set-checkbox
  18616. (org-element-property :begin context) struct
  18617. (cond ((equal arg '(16)) "[-]")
  18618. ((and (not box) (equal arg '(4))) "[ ]")
  18619. ((or (not box) (equal arg '(4))) nil)
  18620. ((eq box 'on) "[ ]")
  18621. (t "[X]")))
  18622. ;; Mimic `org-list-write-struct' but with grabbing
  18623. ;; a return value from `org-list-struct-fix-box'.
  18624. (org-list-struct-fix-ind struct parents 2)
  18625. (org-list-struct-fix-item-end struct)
  18626. (org-list-struct-fix-bul struct prevs)
  18627. (org-list-struct-fix-ind struct parents)
  18628. (let ((block-item
  18629. (org-list-struct-fix-box struct parents prevs orderedp)))
  18630. (if (and box (equal struct old-struct))
  18631. (if (equal arg '(16))
  18632. (message "Checkboxes already reset")
  18633. (user-error "Cannot toggle this checkbox: %s"
  18634. (if (eq box 'on)
  18635. "all subitems checked"
  18636. "unchecked subitems")))
  18637. (org-list-struct-apply-struct struct old-struct)
  18638. (org-update-checkbox-count-maybe))
  18639. (when block-item
  18640. (message "Checkboxes were removed due to empty box at line %d"
  18641. (org-current-line block-item))))))
  18642. (keyword
  18643. (let ((org-inhibit-startup-visibility-stuff t)
  18644. (org-startup-align-all-tables nil))
  18645. (when (boundp 'org-table-coordinate-overlays)
  18646. (mapc 'delete-overlay org-table-coordinate-overlays)
  18647. (setq org-table-coordinate-overlays nil))
  18648. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18649. (message "Local setup has been refreshed"))
  18650. (plain-list
  18651. ;; At a plain list, with a double C-u argument, set
  18652. ;; checkboxes of each item to "[-]", whereas a single one
  18653. ;; will toggle their presence according to the state of the
  18654. ;; first item in the list. Without an argument, repair the
  18655. ;; list.
  18656. (let* ((begin (org-element-property :contents-begin context))
  18657. (beginm (move-marker (make-marker) begin))
  18658. (struct (org-element-property :structure context))
  18659. (old-struct (copy-tree struct))
  18660. (first-box (save-excursion
  18661. (goto-char begin)
  18662. (looking-at org-list-full-item-re)
  18663. (match-string-no-properties 3)))
  18664. (new-box (cond ((equal arg '(16)) "[-]")
  18665. ((equal arg '(4)) (unless first-box "[ ]"))
  18666. ((equal first-box "[X]") "[ ]")
  18667. (t "[X]"))))
  18668. (cond
  18669. (arg
  18670. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18671. (org-list-get-all-items
  18672. begin struct (org-list-prevs-alist struct))))
  18673. ((and first-box (eq (point) begin))
  18674. ;; For convenience, when point is at bol on the first
  18675. ;; item of the list and no argument is provided, simply
  18676. ;; toggle checkbox of that item, if any.
  18677. (org-list-set-checkbox begin struct new-box)))
  18678. (org-list-write-struct
  18679. struct (org-list-parents-alist struct) old-struct)
  18680. (org-update-checkbox-count-maybe)
  18681. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18682. ((property-drawer node-property)
  18683. (call-interactively 'org-property-action))
  18684. ((radio-target target)
  18685. (call-interactively 'org-update-radio-target-regexp))
  18686. (statistics-cookie
  18687. (call-interactively 'org-update-statistics-cookies))
  18688. ((table table-cell table-row)
  18689. ;; At a table, recalculate every field and align it. Also
  18690. ;; send the table if necessary. If the table has
  18691. ;; a `table.el' type, just give up. At a table row or
  18692. ;; cell, maybe recalculate line but always align table.
  18693. (if (eq (org-element-property :type context) 'table.el)
  18694. (message "Use C-c ' to edit table.el tables")
  18695. (let ((org-enable-table-editor t))
  18696. (if (or (eq type 'table)
  18697. ;; Check if point is at a TBLFM line.
  18698. (and (eq type 'table-row)
  18699. (= (point) (org-element-property :end context))))
  18700. (save-excursion
  18701. (if (org-at-TBLFM-p)
  18702. (progn (require 'org-table)
  18703. (org-table-calc-current-TBLFM))
  18704. (goto-char (org-element-property :contents-begin context))
  18705. (org-call-with-arg 'org-table-recalculate (or arg t))
  18706. (orgtbl-send-table 'maybe)))
  18707. (org-table-maybe-eval-formula)
  18708. (cond (arg (call-interactively 'org-table-recalculate))
  18709. ((org-table-maybe-recalculate-line))
  18710. (t (org-table-align)))))))
  18711. (timestamp (org-timestamp-change 0 'day))
  18712. (otherwise
  18713. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18714. (user-error
  18715. "C-c C-c can do nothing useful at this location")))))))))
  18716. (defun org-mode-restart ()
  18717. "Restart Org-mode, to scan again for special lines.
  18718. Also updates the keyword regular expressions."
  18719. (interactive)
  18720. (org-mode)
  18721. (message "Org-mode restarted"))
  18722. (defun org-kill-note-or-show-branches ()
  18723. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18724. (interactive)
  18725. (if (not org-finish-function)
  18726. (progn
  18727. (hide-subtree)
  18728. (call-interactively 'show-branches))
  18729. (let ((org-note-abort t))
  18730. (funcall org-finish-function))))
  18731. (defun org-open-line (n)
  18732. "Insert a new row in tables, call `open-line' elsewhere.
  18733. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18734. (interactive "*p")
  18735. (cond
  18736. ((not org-special-ctrl-o)
  18737. (open-line n))
  18738. ((org-at-table-p)
  18739. (org-table-insert-row))
  18740. (t
  18741. (open-line n))))
  18742. (defun org-return (&optional indent)
  18743. "Goto next table row or insert a newline.
  18744. Calls `org-table-next-row' or `newline', depending on context.
  18745. See the individual commands for more information."
  18746. (interactive)
  18747. (let (org-ts-what)
  18748. (cond
  18749. ((or (bobp) (org-in-src-block-p))
  18750. (if indent (newline-and-indent) (newline)))
  18751. ((org-at-table-p)
  18752. (org-table-justify-field-maybe)
  18753. (call-interactively 'org-table-next-row))
  18754. ;; when `newline-and-indent' is called within a list, make sure
  18755. ;; text moved stays inside the item.
  18756. ((and (org-in-item-p) indent)
  18757. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18758. (progn
  18759. (save-match-data (newline))
  18760. (org-indent-line-to (length (match-string 0))))
  18761. (let ((ind (org-get-indentation)))
  18762. (newline)
  18763. (if (org-looking-back org-list-end-re)
  18764. (org-indent-line)
  18765. (org-indent-line-to ind)))))
  18766. ((and org-return-follows-link
  18767. (org-at-timestamp-p t)
  18768. (not (eq org-ts-what 'after)))
  18769. (org-follow-timestamp-link))
  18770. ((and org-return-follows-link
  18771. (let ((tprop (get-text-property (point) 'face)))
  18772. (or (eq tprop 'org-link)
  18773. (and (listp tprop) (memq 'org-link tprop)))))
  18774. (call-interactively 'org-open-at-point))
  18775. ((and (org-at-heading-p)
  18776. (looking-at
  18777. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18778. (org-show-entry)
  18779. (end-of-line 1)
  18780. (newline))
  18781. (t (if indent (newline-and-indent) (newline))))))
  18782. (defun org-return-indent ()
  18783. "Goto next table row or insert a newline and indent.
  18784. Calls `org-table-next-row' or `newline-and-indent', depending on
  18785. context. See the individual commands for more information."
  18786. (interactive)
  18787. (org-return t))
  18788. (defun org-ctrl-c-star ()
  18789. "Compute table, or change heading status of lines.
  18790. Calls `org-table-recalculate' or `org-toggle-heading',
  18791. depending on context."
  18792. (interactive)
  18793. (cond
  18794. ((org-at-table-p)
  18795. (call-interactively 'org-table-recalculate))
  18796. (t
  18797. ;; Convert all lines in region to list items
  18798. (call-interactively 'org-toggle-heading))))
  18799. (defun org-ctrl-c-minus ()
  18800. "Insert separator line in table or modify bullet status of line.
  18801. Also turns a plain line or a region of lines into list items.
  18802. Calls `org-table-insert-hline', `org-toggle-item', or
  18803. `org-cycle-list-bullet', depending on context."
  18804. (interactive)
  18805. (cond
  18806. ((org-at-table-p)
  18807. (call-interactively 'org-table-insert-hline))
  18808. ((org-region-active-p)
  18809. (call-interactively 'org-toggle-item))
  18810. ((org-in-item-p)
  18811. (call-interactively 'org-cycle-list-bullet))
  18812. (t
  18813. (call-interactively 'org-toggle-item))))
  18814. (defun org-toggle-item (arg)
  18815. "Convert headings or normal lines to items, items to normal lines.
  18816. If there is no active region, only the current line is considered.
  18817. If the first non blank line in the region is a headline, convert
  18818. all headlines to items, shifting text accordingly.
  18819. If it is an item, convert all items to normal lines.
  18820. If it is normal text, change region into a list of items.
  18821. With a prefix argument ARG, change the region in a single item."
  18822. (interactive "P")
  18823. (let ((shift-text
  18824. (function
  18825. ;; Shift text in current section to IND, from point to END.
  18826. ;; The function leaves point to END line.
  18827. (lambda (ind end)
  18828. (let ((min-i 1000) (end (copy-marker end)))
  18829. ;; First determine the minimum indentation (MIN-I) of
  18830. ;; the text.
  18831. (save-excursion
  18832. (catch 'exit
  18833. (while (< (point) end)
  18834. (let ((i (org-get-indentation)))
  18835. (cond
  18836. ;; Skip blank lines and inline tasks.
  18837. ((looking-at "^[ \t]*$"))
  18838. ((looking-at org-outline-regexp-bol))
  18839. ;; We can't find less than 0 indentation.
  18840. ((zerop i) (throw 'exit (setq min-i 0)))
  18841. ((< i min-i) (setq min-i i))))
  18842. (forward-line))))
  18843. ;; Then indent each line so that a line indented to
  18844. ;; MIN-I becomes indented to IND. Ignore blank lines
  18845. ;; and inline tasks in the process.
  18846. (let ((delta (- ind min-i)))
  18847. (while (< (point) end)
  18848. (unless (or (looking-at "^[ \t]*$")
  18849. (looking-at org-outline-regexp-bol))
  18850. (org-indent-line-to (+ (org-get-indentation) delta)))
  18851. (forward-line)))))))
  18852. (skip-blanks
  18853. (function
  18854. ;; Return beginning of first non-blank line, starting from
  18855. ;; line at POS.
  18856. (lambda (pos)
  18857. (save-excursion
  18858. (goto-char pos)
  18859. (skip-chars-forward " \r\t\n")
  18860. (point-at-bol)))))
  18861. beg end)
  18862. ;; Determine boundaries of changes.
  18863. (if (org-region-active-p)
  18864. (setq beg (funcall skip-blanks (region-beginning))
  18865. end (copy-marker (region-end)))
  18866. (setq beg (funcall skip-blanks (point-at-bol))
  18867. end (copy-marker (point-at-eol))))
  18868. ;; Depending on the starting line, choose an action on the text
  18869. ;; between BEG and END.
  18870. (org-with-limited-levels
  18871. (save-excursion
  18872. (goto-char beg)
  18873. (cond
  18874. ;; Case 1. Start at an item: de-itemize. Note that it only
  18875. ;; happens when a region is active: `org-ctrl-c-minus'
  18876. ;; would call `org-cycle-list-bullet' otherwise.
  18877. ((org-at-item-p)
  18878. (while (< (point) end)
  18879. (when (org-at-item-p)
  18880. (skip-chars-forward " \t")
  18881. (delete-region (point) (match-end 0)))
  18882. (forward-line)))
  18883. ;; Case 2. Start at an heading: convert to items.
  18884. ((org-at-heading-p)
  18885. (let* ((bul (org-list-bullet-string "-"))
  18886. (bul-len (length bul))
  18887. ;; Indentation of the first heading. It should be
  18888. ;; relative to the indentation of its parent, if any.
  18889. (start-ind (save-excursion
  18890. (cond
  18891. ((not org-adapt-indentation) 0)
  18892. ((not (outline-previous-heading)) 0)
  18893. (t (length (match-string 0))))))
  18894. ;; Level of first heading. Further headings will be
  18895. ;; compared to it to determine hierarchy in the list.
  18896. (ref-level (org-reduced-level (org-outline-level))))
  18897. (while (< (point) end)
  18898. (let* ((level (org-reduced-level (org-outline-level)))
  18899. (delta (max 0 (- level ref-level))))
  18900. ;; If current headline is less indented than the first
  18901. ;; one, set it as reference, in order to preserve
  18902. ;; subtrees.
  18903. (when (< level ref-level) (setq ref-level level))
  18904. (replace-match bul t t)
  18905. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18906. ;; Ensure all text down to END (or SECTION-END) belongs
  18907. ;; to the newly created item.
  18908. (let ((section-end (save-excursion
  18909. (or (outline-next-heading) (point)))))
  18910. (forward-line)
  18911. (funcall shift-text
  18912. (+ start-ind (* (1+ delta) bul-len))
  18913. (min end section-end)))))))
  18914. ;; Case 3. Normal line with ARG: make the first line of region
  18915. ;; an item, and shift indentation of others lines to
  18916. ;; set them as item's body.
  18917. (arg (let* ((bul (org-list-bullet-string "-"))
  18918. (bul-len (length bul))
  18919. (ref-ind (org-get-indentation)))
  18920. (skip-chars-forward " \t")
  18921. (insert bul)
  18922. (forward-line)
  18923. (while (< (point) end)
  18924. ;; Ensure that lines less indented than first one
  18925. ;; still get included in item body.
  18926. (funcall shift-text
  18927. (+ ref-ind bul-len)
  18928. (min end (save-excursion (or (outline-next-heading)
  18929. (point)))))
  18930. (forward-line))))
  18931. ;; Case 4. Normal line without ARG: turn each non-item line
  18932. ;; into an item.
  18933. (t
  18934. (while (< (point) end)
  18935. (unless (or (org-at-heading-p) (org-at-item-p))
  18936. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18937. (replace-match
  18938. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18939. (forward-line))))))))
  18940. (defun org-toggle-heading (&optional nstars)
  18941. "Convert headings to normal text, or items or text to headings.
  18942. If there is no active region, only convert the current line.
  18943. With a \\[universal-argument] prefix, convert the whole list at
  18944. point into heading.
  18945. In a region:
  18946. - If the first non blank line is a headline, remove the stars
  18947. from all headlines in the region.
  18948. - If it is a normal line, turn each and every normal line (i.e.,
  18949. not an heading or an item) in the region into headings. If you
  18950. want to convert only the first line of this region, use one
  18951. universal prefix argument.
  18952. - If it is a plain list item, turn all plain list items into headings.
  18953. When converting a line into a heading, the number of stars is chosen
  18954. such that the lines become children of the current entry. However,
  18955. when a numeric prefix argument is given, its value determines the
  18956. number of stars to add."
  18957. (interactive "P")
  18958. (let ((skip-blanks
  18959. (function
  18960. ;; Return beginning of first non-blank line, starting from
  18961. ;; line at POS.
  18962. (lambda (pos)
  18963. (save-excursion
  18964. (goto-char pos)
  18965. (while (org-at-comment-p) (forward-line))
  18966. (skip-chars-forward " \r\t\n")
  18967. (point-at-bol)))))
  18968. beg end toggled)
  18969. ;; Determine boundaries of changes. If a universal prefix has
  18970. ;; been given, put the list in a region. If region ends at a bol,
  18971. ;; do not consider the last line to be in the region.
  18972. (when (and current-prefix-arg (org-at-item-p))
  18973. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18974. (org-mark-element))
  18975. (if (org-region-active-p)
  18976. (setq beg (funcall skip-blanks (region-beginning))
  18977. end (copy-marker (save-excursion
  18978. (goto-char (region-end))
  18979. (if (bolp) (point) (point-at-eol)))))
  18980. (setq beg (funcall skip-blanks (point-at-bol))
  18981. end (copy-marker (point-at-eol))))
  18982. ;; Ensure inline tasks don't count as headings.
  18983. (org-with-limited-levels
  18984. (save-excursion
  18985. (goto-char beg)
  18986. (cond
  18987. ;; Case 1. Started at an heading: de-star headings.
  18988. ((org-at-heading-p)
  18989. (while (< (point) end)
  18990. (when (org-at-heading-p t)
  18991. (looking-at org-outline-regexp) (replace-match "")
  18992. (setq toggled t))
  18993. (forward-line)))
  18994. ;; Case 2. Started at an item: change items into headlines.
  18995. ;; One star will be added by `org-list-to-subtree'.
  18996. ((org-at-item-p)
  18997. (let* ((stars (make-string
  18998. ;; subtract the star that will be added again by
  18999. ;; `org-list-to-subtree'
  19000. (if (numberp nstars) (1- nstars)
  19001. (or (org-current-level) 0))
  19002. ?*))
  19003. (add-stars
  19004. (cond (nstars "") ; stars from prefix only
  19005. ((equal stars "") "") ; before first heading
  19006. (org-odd-levels-only "*") ; inside heading, odd
  19007. (t "")))) ; inside heading, oddeven
  19008. (while (< (point) end)
  19009. (when (org-at-item-p)
  19010. ;; Pay attention to cases when region ends before list.
  19011. (let* ((struct (org-list-struct))
  19012. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19013. (save-restriction
  19014. (narrow-to-region (point) list-end)
  19015. (insert
  19016. (org-list-to-subtree
  19017. (org-list-parse-list t)
  19018. '(:istart (concat stars add-stars (funcall get-stars depth))
  19019. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19020. (setq toggled t))
  19021. (forward-line))))
  19022. ;; Case 3. Started at normal text: make every line an heading,
  19023. ;; skipping headlines and items.
  19024. (t (let* ((stars
  19025. (make-string
  19026. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19027. (add-stars
  19028. (cond (nstars "") ; stars from prefix only
  19029. ((equal stars "") "*") ; before first heading
  19030. (org-odd-levels-only "**") ; inside heading, odd
  19031. (t "*"))) ; inside heading, oddeven
  19032. (rpl (concat stars add-stars " "))
  19033. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19034. (while (< (point) (if (equal nstars '(4)) lend end))
  19035. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19036. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19037. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19038. (forward-line)))))))
  19039. (unless toggled (message "Cannot toggle heading from here"))))
  19040. (defun org-meta-return (&optional arg)
  19041. "Insert a new heading or wrap a region in a table.
  19042. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19043. on context. See the individual commands for more information."
  19044. (interactive "P")
  19045. (org-check-before-invisible-edit 'insert)
  19046. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19047. (let* ((element (org-element-at-point))
  19048. (type (org-element-type element)))
  19049. (when (eq type 'table-row)
  19050. (setq element (org-element-property :parent element))
  19051. (setq type 'table))
  19052. (if (and (eq type 'table)
  19053. (eq (org-element-property :type element) 'org)
  19054. (>= (point) (org-element-property :contents-begin element))
  19055. (< (point) (org-element-property :contents-end element)))
  19056. (call-interactively 'org-table-wrap-region)
  19057. (call-interactively 'org-insert-heading)))))
  19058. ;;; Menu entries
  19059. (defsubst org-in-subtree-not-table-p ()
  19060. "Are we in a subtree and not in a table?"
  19061. (and (not (org-before-first-heading-p))
  19062. (not (org-at-table-p))))
  19063. ;; Define the Org-mode menus
  19064. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19065. '("Tbl"
  19066. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19067. ["Next Field" org-cycle (org-at-table-p)]
  19068. ["Previous Field" org-shifttab (org-at-table-p)]
  19069. ["Next Row" org-return (org-at-table-p)]
  19070. "--"
  19071. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19072. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19073. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19074. "--"
  19075. ("Column"
  19076. ["Move Column Left" org-metaleft (org-at-table-p)]
  19077. ["Move Column Right" org-metaright (org-at-table-p)]
  19078. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19079. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19080. ("Row"
  19081. ["Move Row Up" org-metaup (org-at-table-p)]
  19082. ["Move Row Down" org-metadown (org-at-table-p)]
  19083. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19084. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19085. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19086. "--"
  19087. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19088. ("Rectangle"
  19089. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19090. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19091. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19092. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19093. "--"
  19094. ("Calculate"
  19095. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19096. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19097. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19098. "--"
  19099. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19100. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19101. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19102. "--"
  19103. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19104. "--"
  19105. ["Sum Column/Rectangle" org-table-sum
  19106. (or (org-at-table-p) (org-region-active-p))]
  19107. ["Which Column?" org-table-current-column (org-at-table-p)])
  19108. ["Debug Formulas"
  19109. org-table-toggle-formula-debugger
  19110. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19111. ["Show Col/Row Numbers"
  19112. org-table-toggle-coordinate-overlays
  19113. :style toggle
  19114. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19115. "--"
  19116. ["Create" org-table-create (and (not (org-at-table-p))
  19117. org-enable-table-editor)]
  19118. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19119. ["Import from File" org-table-import (not (org-at-table-p))]
  19120. ["Export to File" org-table-export (org-at-table-p)]
  19121. "--"
  19122. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19123. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19124. '("Org"
  19125. ("Show/Hide"
  19126. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19127. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19128. ["Sparse Tree..." org-sparse-tree t]
  19129. ["Reveal Context" org-reveal t]
  19130. ["Show All" show-all t]
  19131. "--"
  19132. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19133. "--"
  19134. ["New Heading" org-insert-heading t]
  19135. ("Navigate Headings"
  19136. ["Up" outline-up-heading t]
  19137. ["Next" outline-next-visible-heading t]
  19138. ["Previous" outline-previous-visible-heading t]
  19139. ["Next Same Level" outline-forward-same-level t]
  19140. ["Previous Same Level" outline-backward-same-level t]
  19141. "--"
  19142. ["Jump" org-goto t])
  19143. ("Edit Structure"
  19144. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19145. "--"
  19146. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19147. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19148. "--"
  19149. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19150. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19151. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19152. "--"
  19153. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19154. "--"
  19155. ["Copy visible text" org-copy-visible t]
  19156. "--"
  19157. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19158. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19159. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19160. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19161. "--"
  19162. ["Sort Region/Children" org-sort t]
  19163. "--"
  19164. ["Convert to odd levels" org-convert-to-odd-levels t]
  19165. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19166. ("Editing"
  19167. ["Emphasis..." org-emphasize t]
  19168. ["Edit Source Example" org-edit-special t]
  19169. "--"
  19170. ["Footnote new/jump" org-footnote-action t]
  19171. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19172. ("Archive"
  19173. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19174. "--"
  19175. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19176. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19177. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19178. )
  19179. "--"
  19180. ("Hyperlinks"
  19181. ["Store Link (Global)" org-store-link t]
  19182. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19183. ["Insert Link" org-insert-link t]
  19184. ["Follow Link" org-open-at-point t]
  19185. "--"
  19186. ["Next link" org-next-link t]
  19187. ["Previous link" org-previous-link t]
  19188. "--"
  19189. ["Descriptive Links"
  19190. org-toggle-link-display
  19191. :style radio
  19192. :selected org-descriptive-links
  19193. ]
  19194. ["Literal Links"
  19195. org-toggle-link-display
  19196. :style radio
  19197. :selected (not org-descriptive-links)])
  19198. "--"
  19199. ("TODO Lists"
  19200. ["TODO/DONE/-" org-todo t]
  19201. ("Select keyword"
  19202. ["Next keyword" org-shiftright (org-at-heading-p)]
  19203. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19204. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19205. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19206. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19207. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19208. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19209. "--"
  19210. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19211. :selected org-enforce-todo-dependencies :style toggle :active t]
  19212. "Settings for tree at point"
  19213. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19214. :selected (org-entry-get nil "ORDERED")
  19215. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19216. ["Do Children parallel" org-toggle-ordered-property :style radio
  19217. :selected (not (org-entry-get nil "ORDERED"))
  19218. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19219. "--"
  19220. ["Set Priority" org-priority t]
  19221. ["Priority Up" org-shiftup t]
  19222. ["Priority Down" org-shiftdown t]
  19223. "--"
  19224. ["Get news from all feeds" org-feed-update-all t]
  19225. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19226. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19227. ("TAGS and Properties"
  19228. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19229. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19230. "--"
  19231. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19232. ["Column view of properties" org-columns t]
  19233. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19234. ("Dates and Scheduling"
  19235. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19236. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19237. ("Change Date"
  19238. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19239. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19240. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19241. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19242. ["Compute Time Range" org-evaluate-time-range t]
  19243. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19244. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19245. "--"
  19246. ["Custom time format" org-toggle-time-stamp-overlays
  19247. :style radio :selected org-display-custom-times]
  19248. "--"
  19249. ["Goto Calendar" org-goto-calendar t]
  19250. ["Date from Calendar" org-date-from-calendar t]
  19251. "--"
  19252. ["Start/Restart Timer" org-timer-start t]
  19253. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19254. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19255. ["Insert Timer String" org-timer t]
  19256. ["Insert Timer Item" org-timer-item t])
  19257. ("Logging work"
  19258. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19259. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19260. ["Clock out" org-clock-out t]
  19261. ["Clock cancel" org-clock-cancel t]
  19262. "--"
  19263. ["Mark as default task" org-clock-mark-default-task t]
  19264. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19265. ["Goto running clock" org-clock-goto t]
  19266. "--"
  19267. ["Display times" org-clock-display t]
  19268. ["Create clock table" org-clock-report t]
  19269. "--"
  19270. ["Record DONE time"
  19271. (progn (setq org-log-done (not org-log-done))
  19272. (message "Switching to %s will %s record a timestamp"
  19273. (car org-done-keywords)
  19274. (if org-log-done "automatically" "not")))
  19275. :style toggle :selected org-log-done])
  19276. "--"
  19277. ["Agenda Command..." org-agenda t]
  19278. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19279. ("File List for Agenda")
  19280. ("Special views current file"
  19281. ["TODO Tree" org-show-todo-tree t]
  19282. ["Check Deadlines" org-check-deadlines t]
  19283. ["Timeline" org-timeline t]
  19284. ["Tags/Property tree" org-match-sparse-tree t])
  19285. "--"
  19286. ["Export/Publish..." org-export-dispatch t]
  19287. ("LaTeX"
  19288. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19289. :selected org-cdlatex-mode]
  19290. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19291. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19292. ["Modify math symbol" org-cdlatex-math-modify
  19293. (org-inside-LaTeX-fragment-p)]
  19294. ["Insert citation" org-reftex-citation t]
  19295. "--"
  19296. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19297. "--"
  19298. ("MobileOrg"
  19299. ["Push Files and Views" org-mobile-push t]
  19300. ["Get Captured and Flagged" org-mobile-pull t]
  19301. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19302. "--"
  19303. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19304. "--"
  19305. ("Documentation"
  19306. ["Show Version" org-version t]
  19307. ["Info Documentation" org-info t])
  19308. ("Customize"
  19309. ["Browse Org Group" org-customize t]
  19310. "--"
  19311. ["Expand This Menu" org-create-customize-menu
  19312. (fboundp 'customize-menu-create)])
  19313. ["Send bug report" org-submit-bug-report t]
  19314. "--"
  19315. ("Refresh/Reload"
  19316. ["Refresh setup current buffer" org-mode-restart t]
  19317. ["Reload Org (after update)" org-reload t]
  19318. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19319. ))
  19320. (defun org-info (&optional node)
  19321. "Read documentation for Org-mode in the info system.
  19322. With optional NODE, go directly to that node."
  19323. (interactive)
  19324. (info (format "(org)%s" (or node ""))))
  19325. ;;;###autoload
  19326. (defun org-submit-bug-report ()
  19327. "Submit a bug report on Org-mode via mail.
  19328. Don't hesitate to report any problems or inaccurate documentation.
  19329. If you don't have setup sending mail from (X)Emacs, please copy the
  19330. output buffer into your mail program, as it gives us important
  19331. information about your Org-mode version and configuration."
  19332. (interactive)
  19333. (require 'reporter)
  19334. (org-load-modules-maybe)
  19335. (org-require-autoloaded-modules)
  19336. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19337. (reporter-submit-bug-report
  19338. "emacs-orgmode@gnu.org"
  19339. (org-version nil 'full)
  19340. (let (list)
  19341. (save-window-excursion
  19342. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19343. (delete-other-windows)
  19344. (erase-buffer)
  19345. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19346. We would like to add your full Org-mode and Outline configuration to the
  19347. bug report. This greatly simplifies the work of the maintainer and
  19348. other experts on the mailing list.
  19349. HOWEVER, some variables you have customized may contain private
  19350. information. The names of customers, colleagues, or friends, might
  19351. appear in the form of file names, tags, todo states, or search strings.
  19352. If you answer yes to the prompt, you might want to check and remove
  19353. such private information before sending the email.")
  19354. (add-text-properties (point-min) (point-max) '(face org-warning))
  19355. (when (yes-or-no-p "Include your Org-mode configuration ")
  19356. (mapatoms
  19357. (lambda (v)
  19358. (and (boundp v)
  19359. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19360. (or (and (symbol-value v)
  19361. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19362. (and
  19363. (get v 'custom-type) (get v 'standard-value)
  19364. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19365. (push v list)))))
  19366. (kill-buffer (get-buffer "*Warn about privacy*"))
  19367. list))
  19368. nil nil
  19369. "Remember to cover the basics, that is, what you expected to happen and
  19370. what in fact did happen. You don't know how to make a good report? See
  19371. http://orgmode.org/manual/Feedback.html#Feedback
  19372. Your bug report will be posted to the Org-mode mailing list.
  19373. ------------------------------------------------------------------------")
  19374. (save-excursion
  19375. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19376. (replace-match "\\1Bug: \\3 [\\2]")))))
  19377. (defun org-install-agenda-files-menu ()
  19378. (let ((bl (buffer-list)))
  19379. (save-excursion
  19380. (while bl
  19381. (set-buffer (pop bl))
  19382. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19383. (when (derived-mode-p 'org-mode)
  19384. (easy-menu-change
  19385. '("Org") "File List for Agenda"
  19386. (append
  19387. (list
  19388. ["Edit File List" (org-edit-agenda-file-list) t]
  19389. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19390. ["Remove Current File from List" org-remove-file t]
  19391. ["Cycle through agenda files" org-cycle-agenda-files t]
  19392. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19393. "--")
  19394. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19395. ;;;; Documentation
  19396. (defun org-require-autoloaded-modules ()
  19397. (interactive)
  19398. (mapc 'require
  19399. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19400. org-remember org-table org-timer)))
  19401. ;;;###autoload
  19402. (defun org-reload (&optional uncompiled)
  19403. "Reload all org lisp files.
  19404. With prefix arg UNCOMPILED, load the uncompiled versions."
  19405. (interactive "P")
  19406. (require 'loadhist)
  19407. (let* ((org-dir (org-find-library-dir "org"))
  19408. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19409. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19410. (remove-re (mapconcat 'identity
  19411. (mapcar (lambda (f) (concat "^" f "$"))
  19412. (list (if (featurep 'xemacs)
  19413. "org-colview"
  19414. "org-colview-xemacs")
  19415. "org" "org-loaddefs" "org-version"))
  19416. "\\|"))
  19417. (feats (delete-dups
  19418. (mapcar 'file-name-sans-extension
  19419. (mapcar 'file-name-nondirectory
  19420. (delq nil
  19421. (mapcar 'feature-file
  19422. features))))))
  19423. (lfeat (append
  19424. (sort
  19425. (setq feats
  19426. (delq nil (mapcar
  19427. (lambda (f)
  19428. (if (and (string-match feature-re f)
  19429. (not (string-match remove-re f)))
  19430. f nil))
  19431. feats)))
  19432. 'string-lessp)
  19433. (list "org-version" "org")))
  19434. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19435. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19436. load-uncore load-misses)
  19437. (setq load-misses
  19438. (delq 't
  19439. (mapcar (lambda (f)
  19440. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19441. (and (string= org-dir contrib-dir)
  19442. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19443. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19444. (add-to-list 'load-uncore f 'append)
  19445. 't)
  19446. f))
  19447. lfeat)))
  19448. (if load-uncore
  19449. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19450. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19451. (if load-misses
  19452. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19453. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19454. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19455. ;;;###autoload
  19456. (defun org-customize ()
  19457. "Call the customize function with org as argument."
  19458. (interactive)
  19459. (org-load-modules-maybe)
  19460. (org-require-autoloaded-modules)
  19461. (customize-browse 'org))
  19462. (defun org-create-customize-menu ()
  19463. "Create a full customization menu for Org-mode, insert it into the menu."
  19464. (interactive)
  19465. (org-load-modules-maybe)
  19466. (org-require-autoloaded-modules)
  19467. (if (fboundp 'customize-menu-create)
  19468. (progn
  19469. (easy-menu-change
  19470. '("Org") "Customize"
  19471. `(["Browse Org group" org-customize t]
  19472. "--"
  19473. ,(customize-menu-create 'org)
  19474. ["Set" Custom-set t]
  19475. ["Save" Custom-save t]
  19476. ["Reset to Current" Custom-reset-current t]
  19477. ["Reset to Saved" Custom-reset-saved t]
  19478. ["Reset to Standard Settings" Custom-reset-standard t]))
  19479. (message "\"Org\"-menu now contains full customization menu"))
  19480. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19481. ;;;; Miscellaneous stuff
  19482. ;;; Generally useful functions
  19483. (defun org-get-at-bol (property)
  19484. "Get text property PROPERTY at beginning of line."
  19485. (get-text-property (point-at-bol) property))
  19486. (defun org-find-text-property-in-string (prop s)
  19487. "Return the first non-nil value of property PROP in string S."
  19488. (or (get-text-property 0 prop s)
  19489. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19490. prop s)))
  19491. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19492. "Display the given MESSAGE as a warning."
  19493. (if (fboundp 'display-warning)
  19494. (display-warning 'org message
  19495. (if (featurep 'xemacs) 'warning :warning))
  19496. (let ((buf (get-buffer-create "*Org warnings*")))
  19497. (with-current-buffer buf
  19498. (goto-char (point-max))
  19499. (insert "Warning (Org): " message)
  19500. (unless (bolp)
  19501. (newline)))
  19502. (display-buffer buf)
  19503. (sit-for 0))))
  19504. (defun org-eval (form)
  19505. "Eval FORM and return result."
  19506. (condition-case error
  19507. (eval form)
  19508. (error (format "%%![Error: %s]" error))))
  19509. (defun org-in-clocktable-p ()
  19510. "Check if the cursor is in a clocktable."
  19511. (let ((pos (point)) start)
  19512. (save-excursion
  19513. (end-of-line 1)
  19514. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19515. (setq start (match-beginning 0))
  19516. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19517. (>= (match-end 0) pos)
  19518. start))))
  19519. (defun org-in-commented-line ()
  19520. "Is point in a line starting with `#'?"
  19521. (equal (char-after (point-at-bol)) ?#))
  19522. (defun org-in-indented-comment-line ()
  19523. "Is point in a line starting with `#' after some white space?"
  19524. (save-excursion
  19525. (save-match-data
  19526. (goto-char (point-at-bol))
  19527. (looking-at "[ \t]*#"))))
  19528. (defun org-in-verbatim-emphasis ()
  19529. (save-match-data
  19530. (and (org-in-regexp org-emph-re 2)
  19531. (>= (point) (match-beginning 3))
  19532. (<= (point) (match-end 4))
  19533. (member (match-string 3) '("=" "~")))))
  19534. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19535. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19536. (if (and marker (marker-buffer marker)
  19537. (buffer-live-p (marker-buffer marker)))
  19538. (progn
  19539. (org-pop-to-buffer-same-window (marker-buffer marker))
  19540. (if (or (> marker (point-max)) (< marker (point-min)))
  19541. (widen))
  19542. (goto-char marker)
  19543. (org-show-context 'org-goto))
  19544. (if bookmark
  19545. (bookmark-jump bookmark)
  19546. (error "Cannot find location"))))
  19547. (defun org-quote-csv-field (s)
  19548. "Quote field for inclusion in CSV material."
  19549. (if (string-match "[\",]" s)
  19550. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19551. s))
  19552. (defun org-force-self-insert (N)
  19553. "Needed to enforce self-insert under remapping."
  19554. (interactive "p")
  19555. (self-insert-command N))
  19556. (defun org-string-width (s)
  19557. "Compute width of string, ignoring invisible characters.
  19558. This ignores character with invisibility property `org-link', and also
  19559. characters with property `org-cwidth', because these will become invisible
  19560. upon the next fontification round."
  19561. (let (b l)
  19562. (when (or (eq t buffer-invisibility-spec)
  19563. (assq 'org-link buffer-invisibility-spec))
  19564. (while (setq b (text-property-any 0 (length s)
  19565. 'invisible 'org-link s))
  19566. (setq s (concat (substring s 0 b)
  19567. (substring s (or (next-single-property-change
  19568. b 'invisible s) (length s)))))))
  19569. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19570. (setq s (concat (substring s 0 b)
  19571. (substring s (or (next-single-property-change
  19572. b 'org-cwidth s) (length s))))))
  19573. (setq l (string-width s) b -1)
  19574. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19575. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19576. l))
  19577. (defun org-shorten-string (s maxlength)
  19578. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19579. If the string is shorter or has length MAXLENGTH, just return the
  19580. original string. If it is longer, the functions finds a space in the
  19581. string, breaks this string off at that locations and adds three dots
  19582. as ellipsis. Including the ellipsis, the string will not be longer
  19583. than MAXLENGTH. If finding a good breaking point in the string does
  19584. not work, the string is just chopped off in the middle of a word
  19585. if necessary."
  19586. (if (<= (length s) maxlength)
  19587. s
  19588. (let* ((n (max (- maxlength 4) 1))
  19589. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19590. (if (string-match re s)
  19591. (concat (match-string 1 s) "...")
  19592. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19593. (defun org-get-indentation (&optional line)
  19594. "Get the indentation of the current line, interpreting tabs.
  19595. When LINE is given, assume it represents a line and compute its indentation."
  19596. (if line
  19597. (if (string-match "^ *" (org-remove-tabs line))
  19598. (match-end 0))
  19599. (save-excursion
  19600. (beginning-of-line 1)
  19601. (skip-chars-forward " \t")
  19602. (current-column))))
  19603. (defun org-get-string-indentation (s)
  19604. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19605. (let ((n -1) (i 0) (w tab-width) c)
  19606. (catch 'exit
  19607. (while (< (setq n (1+ n)) (length s))
  19608. (setq c (aref s n))
  19609. (cond ((= c ?\ ) (setq i (1+ i)))
  19610. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19611. (t (throw 'exit t)))))
  19612. i))
  19613. (defun org-remove-tabs (s &optional width)
  19614. "Replace tabulators in S with spaces.
  19615. Assumes that s is a single line, starting in column 0."
  19616. (setq width (or width tab-width))
  19617. (while (string-match "\t" s)
  19618. (setq s (replace-match
  19619. (make-string
  19620. (- (* width (/ (+ (match-beginning 0) width) width))
  19621. (match-beginning 0)) ?\ )
  19622. t t s)))
  19623. s)
  19624. (defun org-fix-indentation (line ind)
  19625. "Fix indentation in LINE.
  19626. IND is a cons cell with target and minimum indentation.
  19627. If the current indentation in LINE is smaller than the minimum,
  19628. leave it alone. If it is larger than ind, set it to the target."
  19629. (let* ((l (org-remove-tabs line))
  19630. (i (org-get-indentation l))
  19631. (i1 (car ind)) (i2 (cdr ind)))
  19632. (if (>= i i2) (setq l (substring line i2)))
  19633. (if (> i1 0)
  19634. (concat (make-string i1 ?\ ) l)
  19635. l)))
  19636. (defun org-remove-indentation (code &optional n)
  19637. "Remove the maximum common indentation from the lines in CODE.
  19638. N may optionally be the number of spaces to remove."
  19639. (with-temp-buffer
  19640. (insert code)
  19641. (org-do-remove-indentation n)
  19642. (buffer-string)))
  19643. (defun org-do-remove-indentation (&optional n)
  19644. "Remove the maximum common indentation from the buffer."
  19645. (untabify (point-min) (point-max))
  19646. (let ((min 10000) re)
  19647. (if n
  19648. (setq min n)
  19649. (goto-char (point-min))
  19650. (while (re-search-forward "^ *[^ \n]" nil t)
  19651. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19652. (unless (or (= min 0) (= min 10000))
  19653. (setq re (format "^ \\{%d\\}" min))
  19654. (goto-char (point-min))
  19655. (while (re-search-forward re nil t)
  19656. (replace-match "")
  19657. (end-of-line 1))
  19658. min)))
  19659. (defun org-fill-template (template alist)
  19660. "Find each %key of ALIST in TEMPLATE and replace it."
  19661. (let ((case-fold-search nil)
  19662. entry key value)
  19663. (setq alist (sort (copy-sequence alist)
  19664. (lambda (a b) (< (length (car a)) (length (car b))))))
  19665. (while (setq entry (pop alist))
  19666. (setq template
  19667. (replace-regexp-in-string
  19668. (concat "%" (regexp-quote (car entry)))
  19669. (or (cdr entry) "") template t t)))
  19670. template))
  19671. (defun org-base-buffer (buffer)
  19672. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19673. (if (not buffer)
  19674. buffer
  19675. (or (buffer-base-buffer buffer)
  19676. buffer)))
  19677. (defun org-trim (s)
  19678. "Remove whitespace at beginning and end of string."
  19679. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19680. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19681. s)
  19682. (defun org-wrap (string &optional width lines)
  19683. "Wrap string to either a number of lines, or a width in characters.
  19684. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19685. that costs. If there is a word longer than WIDTH, the text is actually
  19686. wrapped to the length of that word.
  19687. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19688. many lines, whatever width that takes.
  19689. The return value is a list of lines, without newlines at the end."
  19690. (let* ((words (org-split-string string "[ \t\n]+"))
  19691. (maxword (apply 'max (mapcar 'org-string-width words)))
  19692. w ll)
  19693. (cond (width
  19694. (org-do-wrap words (max maxword width)))
  19695. (lines
  19696. (setq w maxword)
  19697. (setq ll (org-do-wrap words maxword))
  19698. (if (<= (length ll) lines)
  19699. ll
  19700. (setq ll words)
  19701. (while (> (length ll) lines)
  19702. (setq w (1+ w))
  19703. (setq ll (org-do-wrap words w)))
  19704. ll))
  19705. (t (error "Cannot wrap this")))))
  19706. (defun org-do-wrap (words width)
  19707. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19708. (let (lines line)
  19709. (while words
  19710. (setq line (pop words))
  19711. (while (and words (< (+ (length line) (length (car words))) width))
  19712. (setq line (concat line " " (pop words))))
  19713. (setq lines (push line lines)))
  19714. (nreverse lines)))
  19715. (defun org-split-string (string &optional separators)
  19716. "Splits STRING into substrings at SEPARATORS.
  19717. No empty strings are returned if there are matches at the beginning
  19718. and end of string."
  19719. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19720. (start 0)
  19721. notfirst
  19722. (list nil))
  19723. (while (and (string-match rexp string
  19724. (if (and notfirst
  19725. (= start (match-beginning 0))
  19726. (< start (length string)))
  19727. (1+ start) start))
  19728. (< (match-beginning 0) (length string)))
  19729. (setq notfirst t)
  19730. (or (eq (match-beginning 0) 0)
  19731. (and (eq (match-beginning 0) (match-end 0))
  19732. (eq (match-beginning 0) start))
  19733. (setq list
  19734. (cons (substring string start (match-beginning 0))
  19735. list)))
  19736. (setq start (match-end 0)))
  19737. (or (eq start (length string))
  19738. (setq list
  19739. (cons (substring string start)
  19740. list)))
  19741. (nreverse list)))
  19742. (defun org-quote-vert (s)
  19743. "Replace \"|\" with \"\\vert\"."
  19744. (while (string-match "|" s)
  19745. (setq s (replace-match "\\vert" t t s)))
  19746. s)
  19747. (defun org-uuidgen-p (s)
  19748. "Is S an ID created by UUIDGEN?"
  19749. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19750. (defun org-in-src-block-p (&optional inside)
  19751. "Whether point is in a code source block.
  19752. When INSIDE is non-nil, don't consider we are within a src block
  19753. when point is at #+BEGIN_SRC or #+END_SRC."
  19754. (let ((case-fold-search t) ov)
  19755. (or (and (setq ov (overlays-at (point)))
  19756. (memq 'org-block-background
  19757. (overlay-properties (car ov))))
  19758. (and (not inside)
  19759. (save-match-data
  19760. (save-excursion
  19761. (beginning-of-line)
  19762. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19763. (defun org-context ()
  19764. "Return a list of contexts of the current cursor position.
  19765. If several contexts apply, all are returned.
  19766. Each context entry is a list with a symbol naming the context, and
  19767. two positions indicating start and end of the context. Possible
  19768. contexts are:
  19769. :headline anywhere in a headline
  19770. :headline-stars on the leading stars in a headline
  19771. :todo-keyword on a TODO keyword (including DONE) in a headline
  19772. :tags on the TAGS in a headline
  19773. :priority on the priority cookie in a headline
  19774. :item on the first line of a plain list item
  19775. :item-bullet on the bullet/number of a plain list item
  19776. :checkbox on the checkbox in a plain list item
  19777. :table in an org-mode table
  19778. :table-special on a special filed in a table
  19779. :table-table in a table.el table
  19780. :clocktable in a clocktable
  19781. :src-block in a source block
  19782. :link on a hyperlink
  19783. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19784. :target on a <<target>>
  19785. :radio-target on a <<<radio-target>>>
  19786. :latex-fragment on a LaTeX fragment
  19787. :latex-preview on a LaTeX fragment with overlaid preview image
  19788. This function expects the position to be visible because it uses font-lock
  19789. faces as a help to recognize the following contexts: :table-special, :link,
  19790. and :keyword."
  19791. (let* ((f (get-text-property (point) 'face))
  19792. (faces (if (listp f) f (list f)))
  19793. (case-fold-search t)
  19794. (p (point)) clist o)
  19795. ;; First the large context
  19796. (cond
  19797. ((org-at-heading-p t)
  19798. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19799. (when (progn
  19800. (beginning-of-line 1)
  19801. (looking-at org-todo-line-tags-regexp))
  19802. (push (org-point-in-group p 1 :headline-stars) clist)
  19803. (push (org-point-in-group p 2 :todo-keyword) clist)
  19804. (push (org-point-in-group p 4 :tags) clist))
  19805. (goto-char p)
  19806. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19807. (if (looking-at "\\[#[A-Z0-9]\\]")
  19808. (push (org-point-in-group p 0 :priority) clist)))
  19809. ((org-at-item-p)
  19810. (push (org-point-in-group p 2 :item-bullet) clist)
  19811. (push (list :item (point-at-bol)
  19812. (save-excursion (org-end-of-item) (point)))
  19813. clist)
  19814. (and (org-at-item-checkbox-p)
  19815. (push (org-point-in-group p 0 :checkbox) clist)))
  19816. ((org-at-table-p)
  19817. (push (list :table (org-table-begin) (org-table-end)) clist)
  19818. (if (memq 'org-formula faces)
  19819. (push (list :table-special
  19820. (previous-single-property-change p 'face)
  19821. (next-single-property-change p 'face)) clist)))
  19822. ((org-at-table-p 'any)
  19823. (push (list :table-table) clist)))
  19824. (goto-char p)
  19825. (let ((case-fold-search t))
  19826. ;; New the "medium" contexts: clocktables, source blocks
  19827. (cond ((org-in-clocktable-p)
  19828. (push (list :clocktable
  19829. (and (or (looking-at "#\\+BEGIN: clocktable")
  19830. (search-backward "#+BEGIN: clocktable" nil t))
  19831. (match-beginning 0))
  19832. (and (re-search-forward "#\\+END:?" nil t)
  19833. (match-end 0))) clist))
  19834. ((org-in-src-block-p)
  19835. (push (list :src-block
  19836. (and (or (looking-at "#\\+BEGIN_SRC")
  19837. (search-backward "#+BEGIN_SRC" nil t))
  19838. (match-beginning 0))
  19839. (and (search-forward "#+END_SRC" nil t)
  19840. (match-beginning 0))) clist))))
  19841. (goto-char p)
  19842. ;; Now the small context
  19843. (cond
  19844. ((org-at-timestamp-p)
  19845. (push (org-point-in-group p 0 :timestamp) clist))
  19846. ((memq 'org-link faces)
  19847. (push (list :link
  19848. (previous-single-property-change p 'face)
  19849. (next-single-property-change p 'face)) clist))
  19850. ((memq 'org-special-keyword faces)
  19851. (push (list :keyword
  19852. (previous-single-property-change p 'face)
  19853. (next-single-property-change p 'face)) clist))
  19854. ((org-at-target-p)
  19855. (push (org-point-in-group p 0 :target) clist)
  19856. (goto-char (1- (match-beginning 0)))
  19857. (if (looking-at org-radio-target-regexp)
  19858. (push (org-point-in-group p 0 :radio-target) clist))
  19859. (goto-char p))
  19860. ((setq o (car (delq nil
  19861. (mapcar
  19862. (lambda (x)
  19863. (if (memq x org-latex-fragment-image-overlays) x))
  19864. (overlays-at (point))))))
  19865. (push (list :latex-fragment
  19866. (overlay-start o) (overlay-end o)) clist)
  19867. (push (list :latex-preview
  19868. (overlay-start o) (overlay-end o)) clist))
  19869. ((org-inside-LaTeX-fragment-p)
  19870. ;; FIXME: positions wrong.
  19871. (push (list :latex-fragment (point) (point)) clist)))
  19872. (setq clist (nreverse (delq nil clist)))
  19873. clist))
  19874. ;; FIXME: Compare with at-regexp-p Do we need both?
  19875. (defun org-in-regexp (re &optional nlines visually)
  19876. "Check if point is inside a match of regexp.
  19877. Normally only the current line is checked, but you can include NLINES extra
  19878. lines both before and after point into the search.
  19879. If VISUALLY is set, require that the cursor is not after the match but
  19880. really on, so that the block visually is on the match."
  19881. (catch 'exit
  19882. (let ((pos (point))
  19883. (eol (point-at-eol (+ 1 (or nlines 0))))
  19884. (inc (if visually 1 0)))
  19885. (save-excursion
  19886. (beginning-of-line (- 1 (or nlines 0)))
  19887. (while (re-search-forward re eol t)
  19888. (if (and (<= (match-beginning 0) pos)
  19889. (>= (+ inc (match-end 0)) pos))
  19890. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19891. (defun org-at-regexp-p (regexp)
  19892. "Is point inside a match of REGEXP in the current line?"
  19893. (catch 'exit
  19894. (save-excursion
  19895. (let ((pos (point)) (end (point-at-eol)))
  19896. (beginning-of-line 1)
  19897. (while (re-search-forward regexp end t)
  19898. (if (and (<= (match-beginning 0) pos)
  19899. (>= (match-end 0) pos))
  19900. (throw 'exit t)))
  19901. nil))))
  19902. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19903. "Non-nil when point is between matches of START-RE and END-RE.
  19904. Also return a non-nil value when point is on one of the matches.
  19905. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19906. buffer positions. Default values are the positions of headlines
  19907. surrounding the point.
  19908. The functions returns a cons cell whose car (resp. cdr) is the
  19909. position before START-RE (resp. after END-RE)."
  19910. (save-match-data
  19911. (let ((pos (point))
  19912. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19913. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19914. beg end)
  19915. (save-excursion
  19916. ;; Point is on a block when on START-RE or if START-RE can be
  19917. ;; found before it...
  19918. (and (or (org-at-regexp-p start-re)
  19919. (re-search-backward start-re limit-up t))
  19920. (setq beg (match-beginning 0))
  19921. ;; ... and END-RE after it...
  19922. (goto-char (match-end 0))
  19923. (re-search-forward end-re limit-down t)
  19924. (> (setq end (match-end 0)) pos)
  19925. ;; ... without another START-RE in-between.
  19926. (goto-char (match-beginning 0))
  19927. (not (re-search-backward start-re (1+ beg) t))
  19928. ;; Return value.
  19929. (cons beg end))))))
  19930. (defun org-in-block-p (names)
  19931. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19932. NAMES is a list of strings containing names of blocks.
  19933. Return first block name matched, or nil. Beware that in case of
  19934. nested blocks, the returned name may not belong to the closest
  19935. block from point."
  19936. (save-match-data
  19937. (catch 'exit
  19938. (let ((case-fold-search t)
  19939. (lim-up (save-excursion (outline-previous-heading)))
  19940. (lim-down (save-excursion (outline-next-heading))))
  19941. (mapc (lambda (name)
  19942. (let ((n (regexp-quote name)))
  19943. (when (org-between-regexps-p
  19944. (concat "^[ \t]*#\\+begin_" n)
  19945. (concat "^[ \t]*#\\+end_" n)
  19946. lim-up lim-down)
  19947. (throw 'exit n))))
  19948. names))
  19949. nil)))
  19950. (defun org-in-drawer-p ()
  19951. "Non-nil if point is within a drawer.
  19952. If point is within a drawer, return it, as parsed data."
  19953. (let ((element (save-match-data (org-element-at-point))))
  19954. (while (and element (not (memq (org-element-type element)
  19955. '(drawer property-drawer))))
  19956. (setq element (org-element-property :parent element)))
  19957. element))
  19958. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19959. "Call `multi-occur' with buffers for all agenda files."
  19960. (interactive "sOrg-files matching: \np")
  19961. (let* ((files (org-agenda-files))
  19962. (tnames (mapcar 'file-truename files))
  19963. (extra org-agenda-text-search-extra-files)
  19964. f)
  19965. (when (eq (car extra) 'agenda-archives)
  19966. (setq extra (cdr extra))
  19967. (setq files (org-add-archive-files files)))
  19968. (while (setq f (pop extra))
  19969. (unless (member (file-truename f) tnames)
  19970. (add-to-list 'files f 'append)
  19971. (add-to-list 'tnames (file-truename f) 'append)))
  19972. (multi-occur
  19973. (mapcar (lambda (x)
  19974. (with-current-buffer
  19975. (or (get-file-buffer x) (find-file-noselect x))
  19976. (widen)
  19977. (current-buffer)))
  19978. files)
  19979. regexp)))
  19980. (if (boundp 'occur-mode-find-occurrence-hook)
  19981. ;; Emacs 23
  19982. (add-hook 'occur-mode-find-occurrence-hook
  19983. (lambda ()
  19984. (when (derived-mode-p 'org-mode)
  19985. (org-reveal))))
  19986. ;; Emacs 22
  19987. (defadvice occur-mode-goto-occurrence
  19988. (after org-occur-reveal activate)
  19989. (and (derived-mode-p 'org-mode) (org-reveal)))
  19990. (defadvice occur-mode-goto-occurrence-other-window
  19991. (after org-occur-reveal activate)
  19992. (and (derived-mode-p 'org-mode) (org-reveal)))
  19993. (defadvice occur-mode-display-occurrence
  19994. (after org-occur-reveal activate)
  19995. (when (derived-mode-p 'org-mode)
  19996. (let ((pos (occur-mode-find-occurrence)))
  19997. (with-current-buffer (marker-buffer pos)
  19998. (save-excursion
  19999. (goto-char pos)
  20000. (org-reveal)))))))
  20001. (defun org-occur-link-in-agenda-files ()
  20002. "Create a link and search for it in the agendas.
  20003. The link is not stored in `org-stored-links', it is just created
  20004. for the search purpose."
  20005. (interactive)
  20006. (let ((link (condition-case nil
  20007. (org-store-link nil)
  20008. (error "Unable to create a link to here"))))
  20009. (org-occur-in-agenda-files (regexp-quote link))))
  20010. (defun org-reverse-string (string)
  20011. "Return the reverse of STRING."
  20012. (apply 'string (reverse (string-to-list string))))
  20013. (defsubst org-uniquify (list)
  20014. "Non-destructively remove duplicate elements from LIST."
  20015. (let ((res (copy-sequence list))) (delete-dups res)))
  20016. (defun org-uniquify-alist (alist)
  20017. "Merge elements of ALIST with the same key.
  20018. For example, in this alist:
  20019. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20020. => '((a 1 3) (b 2))
  20021. merge (a 1) and (a 3) into (a 1 3).
  20022. The function returns the new ALIST."
  20023. (let (rtn)
  20024. (mapc
  20025. (lambda (e)
  20026. (let (n)
  20027. (if (not (assoc (car e) rtn))
  20028. (push e rtn)
  20029. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20030. (setq rtn (assq-delete-all (car e) rtn))
  20031. (push n rtn))))
  20032. alist)
  20033. rtn))
  20034. (defun org-delete-all (elts list)
  20035. "Remove all elements in ELTS from LIST."
  20036. (while elts
  20037. (setq list (delete (pop elts) list)))
  20038. list)
  20039. (defun org-count (cl-item cl-seq)
  20040. "Count the number of occurrences of ITEM in SEQ.
  20041. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20042. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20043. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20044. (while (< cl-start cl-end)
  20045. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20046. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20047. (setq cl-start (1+ cl-start)))
  20048. cl-count))
  20049. (defun org-remove-if (predicate seq)
  20050. "Remove everything from SEQ that fulfills PREDICATE."
  20051. (let (res e)
  20052. (while seq
  20053. (setq e (pop seq))
  20054. (if (not (funcall predicate e)) (push e res)))
  20055. (nreverse res)))
  20056. (defun org-remove-if-not (predicate seq)
  20057. "Remove everything from SEQ that does not fulfill PREDICATE."
  20058. (let (res e)
  20059. (while seq
  20060. (setq e (pop seq))
  20061. (if (funcall predicate e) (push e res)))
  20062. (nreverse res)))
  20063. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20064. "Reduce two-argument FUNCTION across SEQ.
  20065. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20066. \":initial-value\" removed."
  20067. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20068. (cadr (memq :initial-value cl-keys)))
  20069. (cl-seq (pop cl-seq))
  20070. (t (funcall cl-func)))))
  20071. (while cl-seq
  20072. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20073. cl-accum))
  20074. (defun org-every (pred seq)
  20075. "Return true if PREDICATE is true of every element of SEQ.
  20076. Adapted from `every' in cl.el."
  20077. (catch 'org-every
  20078. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20079. t))
  20080. (defun org-some (pred seq)
  20081. "Return true if PREDICATE is true of any element of SEQ.
  20082. Adapted from `some' in cl.el."
  20083. (catch 'org-some
  20084. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20085. nil))
  20086. (defun org-back-over-empty-lines ()
  20087. "Move backwards over whitespace, to the beginning of the first empty line.
  20088. Returns the number of empty lines passed."
  20089. (let ((pos (point)))
  20090. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20091. (skip-chars-backward " \t\n\r")
  20092. (unless (eobp)
  20093. (forward-line -1)))
  20094. (beginning-of-line 2)
  20095. (goto-char (min (point) pos))
  20096. (count-lines (point) pos)))
  20097. (defun org-skip-whitespace ()
  20098. (skip-chars-forward " \t\n\r"))
  20099. (defun org-point-in-group (point group &optional context)
  20100. "Check if POINT is in match-group GROUP.
  20101. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20102. match. If the match group does not exist or point is not inside it,
  20103. return nil."
  20104. (and (match-beginning group)
  20105. (>= point (match-beginning group))
  20106. (<= point (match-end group))
  20107. (if context
  20108. (list context (match-beginning group) (match-end group))
  20109. t)))
  20110. (defun org-switch-to-buffer-other-window (&rest args)
  20111. "Switch to buffer in a second window on the current frame.
  20112. In particular, do not allow pop-up frames.
  20113. Returns the newly created buffer."
  20114. (org-no-popups
  20115. (apply 'switch-to-buffer-other-window args)))
  20116. (defun org-combine-plists (&rest plists)
  20117. "Create a single property list from all plists in PLISTS.
  20118. The process starts by copying the first list, and then setting properties
  20119. from the other lists. Settings in the last list are the most significant
  20120. ones and overrule settings in the other lists."
  20121. (let ((rtn (copy-sequence (pop plists)))
  20122. p v ls)
  20123. (while plists
  20124. (setq ls (pop plists))
  20125. (while ls
  20126. (setq p (pop ls) v (pop ls))
  20127. (setq rtn (plist-put rtn p v))))
  20128. rtn))
  20129. (defun org-replace-escapes (string table)
  20130. "Replace %-escapes in STRING with values in TABLE.
  20131. TABLE is an association list with keys like \"%a\" and string values.
  20132. The sequences in STRING may contain normal field width and padding information,
  20133. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20134. so values can contain further %-escapes if they are define later in TABLE."
  20135. (let ((tbl (copy-alist table))
  20136. (case-fold-search nil)
  20137. (pchg 0)
  20138. e re rpl)
  20139. (while (setq e (pop tbl))
  20140. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20141. (when (and (cdr e) (string-match re (cdr e)))
  20142. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20143. (safe "SREF"))
  20144. (add-text-properties 0 3 (list 'sref sref) safe)
  20145. (setcdr e (replace-match safe t t (cdr e)))))
  20146. (while (string-match re string)
  20147. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20148. (cdr e)))
  20149. (setq string (replace-match rpl t t string))))
  20150. (while (setq pchg (next-property-change pchg string))
  20151. (let ((sref (get-text-property pchg 'sref string)))
  20152. (when (and sref (string-match "SREF" string pchg))
  20153. (setq string (replace-match sref t t string)))))
  20154. string))
  20155. (defun org-sublist (list start end)
  20156. "Return a section of LIST, from START to END.
  20157. Counting starts at 1."
  20158. (let (rtn (c start))
  20159. (setq list (nthcdr (1- start) list))
  20160. (while (and list (<= c end))
  20161. (push (pop list) rtn)
  20162. (setq c (1+ c)))
  20163. (nreverse rtn)))
  20164. (defun org-find-base-buffer-visiting (file)
  20165. "Like `find-buffer-visiting' but always return the base buffer and
  20166. not an indirect buffer."
  20167. (let ((buf (or (get-file-buffer file)
  20168. (find-buffer-visiting file))))
  20169. (if buf
  20170. (or (buffer-base-buffer buf) buf)
  20171. nil)))
  20172. (defun org-image-file-name-regexp (&optional extensions)
  20173. "Return regexp matching the file names of images.
  20174. If EXTENSIONS is given, only match these."
  20175. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20176. (image-file-name-regexp)
  20177. (let ((image-file-name-extensions
  20178. (or extensions
  20179. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20180. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20181. (concat "\\."
  20182. (regexp-opt (nconc (mapcar 'upcase
  20183. image-file-name-extensions)
  20184. image-file-name-extensions)
  20185. t)
  20186. "\\'"))))
  20187. (defun org-file-image-p (file &optional extensions)
  20188. "Return non-nil if FILE is an image."
  20189. (save-match-data
  20190. (string-match (org-image-file-name-regexp extensions) file)))
  20191. (defun org-get-cursor-date (&optional with-time)
  20192. "Return the date at cursor in as a time.
  20193. This works in the calendar and in the agenda, anywhere else it just
  20194. returns the current time.
  20195. If WITH-TIME is non-nil, returns the time of the event at point (in
  20196. the agenda) or the current time of the day."
  20197. (let (date day defd tp tm hod mod)
  20198. (when with-time
  20199. (setq tp (get-text-property (point) 'time))
  20200. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20201. (setq hod (string-to-number (match-string 1 tp))
  20202. mod (string-to-number (match-string 2 tp))))
  20203. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20204. mod (nth 1 (decode-time (current-time))))))
  20205. (cond
  20206. ((eq major-mode 'calendar-mode)
  20207. (setq date (calendar-cursor-to-date)
  20208. defd (encode-time 0 (or mod 0) (or hod 0)
  20209. (nth 1 date) (nth 0 date) (nth 2 date))))
  20210. ((eq major-mode 'org-agenda-mode)
  20211. (setq day (get-text-property (point) 'day))
  20212. (if day
  20213. (setq date (calendar-gregorian-from-absolute day)
  20214. defd (encode-time 0 (or mod 0) (or hod 0)
  20215. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20216. (or defd (current-time))))
  20217. (defun org-mark-subtree (&optional up)
  20218. "Mark the current subtree.
  20219. This puts point at the start of the current subtree, and mark at
  20220. the end. If a numeric prefix UP is given, move up into the
  20221. hierarchy of headlines by UP levels before marking the subtree."
  20222. (interactive "P")
  20223. (org-with-limited-levels
  20224. (cond ((org-at-heading-p) (beginning-of-line))
  20225. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20226. (t (outline-previous-visible-heading 1))))
  20227. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20228. (if (org-called-interactively-p 'any)
  20229. (call-interactively 'org-mark-element)
  20230. (org-mark-element)))
  20231. ;;; Indentation
  20232. (defun org-indent-line ()
  20233. "Indent line depending on context."
  20234. (interactive)
  20235. (let* ((pos (point))
  20236. (itemp (org-at-item-p))
  20237. (case-fold-search t)
  20238. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20239. (inline-task-p (and (featurep 'org-inlinetask)
  20240. (org-inlinetask-in-task-p)))
  20241. (inline-re (and inline-task-p
  20242. (org-inlinetask-outline-regexp)))
  20243. column)
  20244. (if (and orgstruct-is-++ (eq pos (point)))
  20245. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20246. (indent-according-to-mode))
  20247. (beginning-of-line 1)
  20248. (cond
  20249. ;; Headings
  20250. ((looking-at org-outline-regexp) (setq column 0))
  20251. ;; Footnote definition
  20252. ((looking-at org-footnote-definition-re) (setq column 0))
  20253. ;; Literal examples
  20254. ((looking-at "[ \t]*:\\( \\|$\\)")
  20255. (setq column (org-get-indentation))) ; do nothing
  20256. ;; Lists
  20257. ((ignore-errors (goto-char (org-in-item-p)))
  20258. (setq column (if itemp
  20259. (org-get-indentation)
  20260. (org-list-item-body-column (point))))
  20261. (goto-char pos))
  20262. ;; Drawers
  20263. ((and (looking-at "[ \t]*:END:")
  20264. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20265. (save-excursion
  20266. (goto-char (1- (match-beginning 1)))
  20267. (setq column (current-column))))
  20268. ;; Special blocks
  20269. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20270. (save-excursion
  20271. (re-search-backward
  20272. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20273. (setq column (org-get-indentation (match-string 0))))
  20274. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20275. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20276. (save-excursion
  20277. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20278. (setq column
  20279. (cond ((equal (downcase (match-string 1)) "src")
  20280. ;; src blocks: let `org-edit-src-exit' handle them
  20281. (org-get-indentation))
  20282. ((equal (downcase (match-string 1)) "example")
  20283. (max (org-get-indentation)
  20284. (org-get-indentation (match-string 0))))
  20285. (t
  20286. (org-get-indentation (match-string 0))))))
  20287. ;; This line has nothing special, look at the previous relevant
  20288. ;; line to compute indentation
  20289. (t
  20290. (beginning-of-line 0)
  20291. (while (and (not (bobp))
  20292. (not (looking-at org-table-line-regexp))
  20293. (not (looking-at org-drawer-regexp))
  20294. ;; When point started in an inline task, do not move
  20295. ;; above task starting line.
  20296. (not (and inline-task-p (looking-at inline-re)))
  20297. ;; Skip drawers, blocks, empty lines, verbatim,
  20298. ;; comments, tables, footnotes definitions, lists,
  20299. ;; inline tasks.
  20300. (or (and (looking-at "[ \t]*:END:")
  20301. (re-search-backward org-drawer-regexp nil t))
  20302. (and (looking-at "[ \t]*#\\+end_")
  20303. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20304. (looking-at "[ \t]*[\n:#|]")
  20305. (looking-at org-footnote-definition-re)
  20306. (and (not inline-task-p)
  20307. (featurep 'org-inlinetask)
  20308. (org-inlinetask-in-task-p)
  20309. (or (org-inlinetask-goto-beginning) t))))
  20310. (beginning-of-line 0))
  20311. (cond
  20312. ;; There was a list item above.
  20313. ((ignore-errors (goto-char (org-in-item-p)))
  20314. (goto-char (org-list-get-top-point (org-list-struct)))
  20315. (setq column (org-get-indentation)))
  20316. ;; There was an heading above.
  20317. ((looking-at "\\*+[ \t]+")
  20318. (if (not org-adapt-indentation)
  20319. (setq column 0)
  20320. (goto-char (match-end 0))
  20321. (setq column (current-column))))
  20322. ;; A drawer had started and is unfinished
  20323. ((looking-at org-drawer-regexp)
  20324. (goto-char (1- (match-beginning 1)))
  20325. (setq column (current-column)))
  20326. ;; Else, nothing noticeable found: get indentation and go on.
  20327. (t (setq column (org-get-indentation))))))
  20328. ;; Now apply indentation and move cursor accordingly
  20329. (goto-char pos)
  20330. (if (<= (current-column) (current-indentation))
  20331. (org-indent-line-to column)
  20332. (save-excursion (org-indent-line-to column)))
  20333. ;; Special polishing for properties, see `org-property-format'
  20334. (setq column (current-column))
  20335. (beginning-of-line 1)
  20336. (if (looking-at org-property-re)
  20337. (replace-match (concat (match-string 4)
  20338. (format org-property-format
  20339. (match-string 1) (match-string 3)))
  20340. t t))
  20341. (org-move-to-column column))))
  20342. (defun org-indent-drawer ()
  20343. "Indent the drawer at point."
  20344. (interactive)
  20345. (let ((p (point))
  20346. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20347. (match-end 0)))
  20348. (folded
  20349. (save-excursion
  20350. (end-of-line)
  20351. (when (overlays-at (point))
  20352. (member 'invisible (overlay-properties
  20353. (car (overlays-at (point)))))))))
  20354. (when folded (org-cycle))
  20355. (indent-for-tab-command)
  20356. (while (and (move-beginning-of-line 2) (< (point) e))
  20357. (indent-for-tab-command))
  20358. (goto-char p)
  20359. (when folded (org-cycle)))
  20360. (message "Drawer at point indented"))
  20361. (defun org-indent-block ()
  20362. "Indent the block at point."
  20363. (interactive)
  20364. (let ((p (point))
  20365. (case-fold-search t)
  20366. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20367. (match-end 0)))
  20368. (folded
  20369. (save-excursion
  20370. (end-of-line)
  20371. (when (overlays-at (point))
  20372. (member 'invisible (overlay-properties
  20373. (car (overlays-at (point)))))))))
  20374. (when folded (org-cycle))
  20375. (indent-for-tab-command)
  20376. (while (and (move-beginning-of-line 2) (< (point) e))
  20377. (indent-for-tab-command))
  20378. (goto-char p)
  20379. (when folded (org-cycle)))
  20380. (message "Block at point indented"))
  20381. (defun org-indent-region (start end)
  20382. "Indent region."
  20383. (interactive "r")
  20384. (save-excursion
  20385. (let ((line-end (org-current-line end)))
  20386. (goto-char start)
  20387. (while (< (org-current-line) line-end)
  20388. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20389. (t (call-interactively 'org-indent-line)))
  20390. (move-beginning-of-line 2)))))
  20391. ;;; Filling
  20392. ;; We use our own fill-paragraph and auto-fill functions.
  20393. ;; `org-fill-paragraph' relies on adaptive filling and context
  20394. ;; checking. Appropriate `fill-prefix' is computed with
  20395. ;; `org-adaptive-fill-function'.
  20396. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20397. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20398. ;; `org-adaptive-fill-function' value. Internally,
  20399. ;; `org-comment-line-break-function' breaks the line.
  20400. ;; `org-setup-filling' installs filling and auto-filling related
  20401. ;; variables during `org-mode' initialization.
  20402. (defvar org-element-paragraph-separate) ; org-element.el
  20403. (defun org-setup-filling ()
  20404. (require 'org-element)
  20405. ;; Prevent auto-fill from inserting unwanted new items.
  20406. (when (boundp 'fill-nobreak-predicate)
  20407. (org-set-local
  20408. 'fill-nobreak-predicate
  20409. (org-uniquify
  20410. (append fill-nobreak-predicate
  20411. '(org-fill-line-break-nobreak-p
  20412. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20413. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20414. (org-set-local 'paragraph-start paragraph-ending)
  20415. (org-set-local 'paragraph-separate paragraph-ending))
  20416. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20417. (org-set-local 'auto-fill-inhibit-regexp nil)
  20418. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20419. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20420. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20421. (defun org-fill-line-break-nobreak-p ()
  20422. "Non-nil when a new line at point would create an Org line break."
  20423. (save-excursion
  20424. (skip-chars-backward "[ \t]")
  20425. (skip-chars-backward "\\\\")
  20426. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20427. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20428. "Non-nil when a new line at point would split a timestamp."
  20429. (and (org-at-timestamp-p t)
  20430. (not (looking-at org-ts-regexp-both))))
  20431. (declare-function message-in-body-p "message" ())
  20432. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20433. (defun org-adaptive-fill-function ()
  20434. "Compute a fill prefix for the current line.
  20435. Return fill prefix, as a string, or nil if current line isn't
  20436. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20437. matches in paragraphs or comments, use it."
  20438. (catch 'exit
  20439. (when (derived-mode-p 'message-mode)
  20440. (save-excursion
  20441. (beginning-of-line)
  20442. (cond ((or (not (message-in-body-p))
  20443. (looking-at orgtbl-line-start-regexp))
  20444. (throw 'exit nil))
  20445. ((looking-at message-cite-prefix-regexp)
  20446. (throw 'exit (match-string-no-properties 0)))
  20447. ((looking-at org-outline-regexp)
  20448. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20449. (org-with-wide-buffer
  20450. (let* ((p (line-beginning-position))
  20451. (element (save-excursion
  20452. (beginning-of-line)
  20453. (or (ignore-errors (org-element-at-point))
  20454. (user-error "An element cannot be parsed line %d"
  20455. (line-number-at-pos (point))))))
  20456. (type (org-element-type element))
  20457. (post-affiliated (org-element-property :post-affiliated element)))
  20458. (unless (and post-affiliated (< p post-affiliated))
  20459. (case type
  20460. (comment
  20461. (save-excursion
  20462. (beginning-of-line)
  20463. (looking-at "[ \t]*")
  20464. (concat (match-string 0) "# ")))
  20465. (footnote-definition "")
  20466. ((item plain-list)
  20467. (make-string (org-list-item-body-column
  20468. (or post-affiliated
  20469. (org-element-property :begin element)))
  20470. ? ))
  20471. (paragraph
  20472. ;; Fill prefix is usually the same as the current line,
  20473. ;; unless the paragraph is at the beginning of an item.
  20474. (let ((parent (org-element-property :parent element)))
  20475. (save-excursion
  20476. (beginning-of-line)
  20477. (cond ((eq (org-element-type parent) 'item)
  20478. (make-string (org-list-item-body-column
  20479. (org-element-property :begin parent))
  20480. ? ))
  20481. ((and adaptive-fill-regexp
  20482. ;; Locally disable
  20483. ;; `adaptive-fill-function' to let
  20484. ;; `fill-context-prefix' handle
  20485. ;; `adaptive-fill-regexp' variable.
  20486. (let (adaptive-fill-function)
  20487. (fill-context-prefix
  20488. post-affiliated
  20489. (org-element-property :end element)))))
  20490. ((looking-at "[ \t]+") (match-string 0))
  20491. (t "")))))
  20492. (comment-block
  20493. ;; Only fill contents if P is within block boundaries.
  20494. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20495. (forward-line)
  20496. (point)))
  20497. (cend (save-excursion
  20498. (goto-char (org-element-property :end element))
  20499. (skip-chars-backward " \r\t\n")
  20500. (line-beginning-position))))
  20501. (when (and (>= p cbeg) (< p cend))
  20502. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20503. (match-string 0)
  20504. ""))))))))))
  20505. (declare-function message-goto-body "message" ())
  20506. (defvar message-cite-prefix-regexp) ; From message.el
  20507. (defun org-fill-paragraph (&optional justify)
  20508. "Fill element at point, when applicable.
  20509. This function only applies to comment blocks, comments, example
  20510. blocks and paragraphs. Also, as a special case, re-align table
  20511. when point is at one.
  20512. If JUSTIFY is non-nil (interactively, with prefix argument),
  20513. justify as well. If `sentence-end-double-space' is non-nil, then
  20514. period followed by one space does not end a sentence, so don't
  20515. break a line there. The variable `fill-column' controls the
  20516. width for filling.
  20517. For convenience, when point is at a plain list, an item or
  20518. a footnote definition, try to fill the first paragraph within."
  20519. (interactive)
  20520. (if (and (derived-mode-p 'message-mode)
  20521. (or (not (message-in-body-p))
  20522. (save-excursion (move-beginning-of-line 1)
  20523. (looking-at message-cite-prefix-regexp))))
  20524. ;; First ensure filling is correct in message-mode.
  20525. (let ((fill-paragraph-function
  20526. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20527. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20528. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20529. (paragraph-separate
  20530. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20531. (fill-paragraph nil))
  20532. (with-syntax-table org-mode-transpose-word-syntax-table
  20533. ;; Move to end of line in order to get the first paragraph
  20534. ;; within a plain list or a footnote definition.
  20535. (let ((element (save-excursion
  20536. (end-of-line)
  20537. (or (ignore-errors (org-element-at-point))
  20538. (user-error "An element cannot be parsed line %d"
  20539. (line-number-at-pos (point)))))))
  20540. ;; First check if point is in a blank line at the beginning of
  20541. ;; the buffer. In that case, ignore filling.
  20542. (case (org-element-type element)
  20543. ;; Use major mode filling function is src blocks.
  20544. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20545. ;; Align Org tables, leave table.el tables as-is.
  20546. (table-row (org-table-align) t)
  20547. (table
  20548. (when (eq (org-element-property :type element) 'org)
  20549. (save-excursion
  20550. (goto-char (org-element-property :post-affiliated element))
  20551. (org-table-align)))
  20552. t)
  20553. (paragraph
  20554. ;; Paragraphs may contain `line-break' type objects.
  20555. (let ((beg (max (point-min)
  20556. (org-element-property :contents-begin element)))
  20557. (end (min (point-max)
  20558. (org-element-property :contents-end element))))
  20559. ;; Do nothing if point is at an affiliated keyword.
  20560. (if (< (line-end-position) beg) t
  20561. (when (derived-mode-p 'message-mode)
  20562. ;; In `message-mode', do not fill following citation
  20563. ;; in current paragraph nor text before message body.
  20564. (let ((body-start (save-excursion (message-goto-body))))
  20565. (when body-start (setq beg (max body-start beg))))
  20566. (when (save-excursion
  20567. (re-search-forward
  20568. (concat "^" message-cite-prefix-regexp) end t))
  20569. (setq end (match-beginning 0))))
  20570. ;; Fill paragraph, taking line breaks into account.
  20571. ;; For that, slice the paragraph using line breaks as
  20572. ;; separators, and fill the parts in reverse order to
  20573. ;; avoid messing with markers.
  20574. (save-excursion
  20575. (goto-char end)
  20576. (mapc
  20577. (lambda (pos)
  20578. (fill-region-as-paragraph pos (point) justify)
  20579. (goto-char pos))
  20580. ;; Find the list of ending positions for line breaks
  20581. ;; in the current paragraph. Add paragraph
  20582. ;; beginning to include first slice.
  20583. (nreverse
  20584. (cons beg
  20585. (org-element-map
  20586. (org-element--parse-objects
  20587. beg end nil (org-element-restriction 'paragraph))
  20588. 'line-break
  20589. (lambda (lb) (org-element-property :end lb)))))))
  20590. t)))
  20591. ;; Contents of `comment-block' type elements should be
  20592. ;; filled as plain text, but only if point is within block
  20593. ;; markers.
  20594. (comment-block
  20595. (let* ((case-fold-search t)
  20596. (beg (save-excursion
  20597. (goto-char (org-element-property :begin element))
  20598. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20599. (forward-line)
  20600. (point)))
  20601. (end (save-excursion
  20602. (goto-char (org-element-property :end element))
  20603. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20604. (line-beginning-position))))
  20605. (if (or (< (point) beg) (> (point) end)) t
  20606. (fill-region-as-paragraph
  20607. (save-excursion (end-of-line)
  20608. (re-search-backward "^[ \t]*$" beg 'move)
  20609. (line-beginning-position))
  20610. (save-excursion (beginning-of-line)
  20611. (re-search-forward "^[ \t]*$" end 'move)
  20612. (line-beginning-position))
  20613. justify))))
  20614. ;; Fill comments.
  20615. (comment
  20616. (let ((begin (org-element-property :post-affiliated element))
  20617. (end (org-element-property :end element)))
  20618. (when (and (>= (point) begin) (<= (point) end))
  20619. (let ((begin (save-excursion
  20620. (end-of-line)
  20621. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20622. (progn (forward-line) (point))
  20623. begin)))
  20624. (end (save-excursion
  20625. (end-of-line)
  20626. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20627. (1- (line-beginning-position))
  20628. (skip-chars-backward " \r\t\n")
  20629. (line-end-position)))))
  20630. ;; Do not fill comments when at a blank line.
  20631. (when (> end begin)
  20632. (let ((fill-prefix
  20633. (save-excursion
  20634. (beginning-of-line)
  20635. (looking-at "[ \t]*#")
  20636. (let ((comment-prefix (match-string 0)))
  20637. (goto-char (match-end 0))
  20638. (if (looking-at adaptive-fill-regexp)
  20639. (concat comment-prefix (match-string 0))
  20640. (concat comment-prefix " "))))))
  20641. (save-excursion
  20642. (fill-region-as-paragraph begin end justify))))))
  20643. t))
  20644. ;; Ignore every other element.
  20645. (otherwise t))))))
  20646. (defun org-auto-fill-function ()
  20647. "Auto-fill function."
  20648. ;; Check if auto-filling is meaningful.
  20649. (let ((fc (current-fill-column)))
  20650. (when (and fc (> (current-column) fc))
  20651. (let* ((fill-prefix (org-adaptive-fill-function))
  20652. ;; Enforce empty fill prefix, if required. Otherwise, it
  20653. ;; will be computed again.
  20654. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20655. (when fill-prefix (do-auto-fill))))))
  20656. (defun org-comment-line-break-function (&optional soft)
  20657. "Break line at point and indent, continuing comment if within one.
  20658. The inserted newline is marked hard if variable
  20659. `use-hard-newlines' is true, unless optional argument SOFT is
  20660. non-nil."
  20661. (if soft (insert-and-inherit ?\n) (newline 1))
  20662. (save-excursion (forward-char -1) (delete-horizontal-space))
  20663. (delete-horizontal-space)
  20664. (indent-to-left-margin)
  20665. (insert-before-markers-and-inherit fill-prefix))
  20666. ;;; Comments
  20667. ;; Org comments syntax is quite complex. It requires the entire line
  20668. ;; to be just a comment. Also, even with the right syntax at the
  20669. ;; beginning of line, some some elements (i.e. verse-block or
  20670. ;; example-block) don't accept comments. Usual Emacs comment commands
  20671. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20672. ;; Org still relies on `comment-dwim', but cannot trust
  20673. ;; `comment-only-p'. So, `comment-region-function' and
  20674. ;; `uncomment-region-function' both point
  20675. ;; to`org-comment-or-uncomment-region'. Eventually,
  20676. ;; `org-insert-comment' takes care of insertion of comments at the
  20677. ;; beginning of line.
  20678. ;; `org-setup-comments-handling' install comments related variables
  20679. ;; during `org-mode' initialization.
  20680. (defun org-setup-comments-handling ()
  20681. (interactive)
  20682. (org-set-local 'comment-use-syntax nil)
  20683. (org-set-local 'comment-start "# ")
  20684. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20685. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20686. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20687. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20688. (defun org-insert-comment ()
  20689. "Insert an empty comment above current line.
  20690. If the line is empty, insert comment at its beginning. When
  20691. point is within a source block, comment according to the related
  20692. major mode."
  20693. (if (let ((element (org-element-at-point)))
  20694. (and (eq (org-element-type element) 'src-block)
  20695. (< (save-excursion
  20696. (goto-char (org-element-property :post-affiliated element))
  20697. (line-end-position))
  20698. (point))
  20699. (> (save-excursion
  20700. (goto-char (org-element-property :end element))
  20701. (skip-chars-backward " \r\t\n")
  20702. (line-beginning-position))
  20703. (point))))
  20704. (progn
  20705. (require 'ob-core)
  20706. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
  20707. (beginning-of-line)
  20708. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  20709. (open-line 1))
  20710. (org-indent-line)
  20711. (insert "# ")))
  20712. (defvar comment-empty-lines) ; From newcomment.el.
  20713. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20714. "Comment or uncomment each non-blank line in the region.
  20715. Uncomment each non-blank line between BEG and END if it only
  20716. contains commented lines. Otherwise, comment them. If region is
  20717. strictly within a source block, use appropriate comment syntax."
  20718. (if (let ((element (org-element-at-point)))
  20719. (and (eq (org-element-type element) 'src-block)
  20720. (< (save-excursion
  20721. (goto-char (org-element-property :post-affiliated element))
  20722. (line-end-position))
  20723. beg)
  20724. (>= (save-excursion
  20725. (goto-char (org-element-property :end element))
  20726. (skip-chars-backward " \r\t\n")
  20727. (line-beginning-position))
  20728. end)))
  20729. (progn
  20730. (require 'ob-core)
  20731. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim)))
  20732. (save-restriction
  20733. ;; Restrict region
  20734. (narrow-to-region (save-excursion (goto-char beg)
  20735. (skip-chars-forward " \r\t\n" end)
  20736. (line-beginning-position))
  20737. (save-excursion (goto-char end)
  20738. (skip-chars-backward " \r\t\n" beg)
  20739. (line-end-position)))
  20740. (let ((uncommentp
  20741. ;; UNCOMMENTP is non-nil when every non blank line between
  20742. ;; BEG and END is a comment.
  20743. (save-excursion
  20744. (goto-char (point-min))
  20745. (while (and (not (eobp))
  20746. (let ((element (org-element-at-point)))
  20747. (and (eq (org-element-type element) 'comment)
  20748. (goto-char (min (point-max)
  20749. (org-element-property
  20750. :end element)))))))
  20751. (eobp))))
  20752. (if uncommentp
  20753. ;; Only blank lines and comments in region: uncomment it.
  20754. (save-excursion
  20755. (goto-char (point-min))
  20756. (while (not (eobp))
  20757. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20758. (replace-match "" nil nil nil 1))
  20759. (forward-line)))
  20760. ;; Comment each line in region.
  20761. (let ((min-indent (point-max)))
  20762. ;; First find the minimum indentation across all lines.
  20763. (save-excursion
  20764. (goto-char (point-min))
  20765. (while (and (not (eobp)) (not (zerop min-indent)))
  20766. (unless (looking-at "[ \t]*$")
  20767. (setq min-indent (min min-indent (current-indentation))))
  20768. (forward-line)))
  20769. ;; Then loop over all lines.
  20770. (save-excursion
  20771. (goto-char (point-min))
  20772. (while (not (eobp))
  20773. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20774. ;; Don't get fooled by invisible text (e.g. link path)
  20775. ;; when moving to column MIN-INDENT.
  20776. (let ((buffer-invisibility-spec nil))
  20777. (org-move-to-column min-indent t))
  20778. (insert comment-start))
  20779. (forward-line)))))))))
  20780. ;;; Planning
  20781. ;; This section contains tools to operate on timestamp objects, as
  20782. ;; returned by, e.g. `org-element-context'.
  20783. (defun org-timestamp-has-time-p (timestamp)
  20784. "Non-nil when TIMESTAMP has a time specified."
  20785. (org-element-property :hour-start timestamp))
  20786. (defun org-timestamp-format (timestamp format &optional end utc)
  20787. "Format a TIMESTAMP element into a string.
  20788. FORMAT is a format specifier to be passed to
  20789. `format-time-string'.
  20790. When optional argument END is non-nil, use end of date-range or
  20791. time-range, if possible.
  20792. When optional argument UTC is non-nil, time will be expressed as
  20793. Universal Time."
  20794. (format-time-string
  20795. format
  20796. (apply 'encode-time
  20797. (cons 0
  20798. (mapcar
  20799. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20800. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20801. '(:minute-start :hour-start :day-start :month-start
  20802. :year-start)))))
  20803. utc))
  20804. (defun org-timestamp-split-range (timestamp &optional end)
  20805. "Extract a timestamp object from a date or time range.
  20806. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20807. the end of the range. Otherwise, extract its start.
  20808. Return a new timestamp object sharing the same parent as
  20809. TIMESTAMP."
  20810. (let ((type (org-element-property :type timestamp)))
  20811. (if (memq type '(active inactive diary)) timestamp
  20812. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20813. ;; Set new type.
  20814. (org-element-put-property
  20815. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20816. ;; Copy start properties over end properties if END is
  20817. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20818. (let ((p-alist '((:minute-start . :minute-end)
  20819. (:hour-start . :hour-end)
  20820. (:day-start . :day-end)
  20821. (:month-start . :month-end)
  20822. (:year-start . :year-end))))
  20823. (dolist (p-cell p-alist)
  20824. (org-element-put-property
  20825. split-ts
  20826. (funcall (if end 'car 'cdr) p-cell)
  20827. (org-element-property
  20828. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20829. ;; Eventually refresh `:raw-value'.
  20830. (org-element-put-property split-ts :raw-value nil)
  20831. (org-element-put-property
  20832. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20833. (defun org-timestamp-translate (timestamp &optional boundary)
  20834. "Apply `org-translate-time' on a TIMESTAMP object.
  20835. When optional argument BOUNDARY is non-nil, it is either the
  20836. symbol `start' or `end'. In this case, only translate the
  20837. starting or ending part of TIMESTAMP if it is a date or time
  20838. range. Otherwise, translate both parts."
  20839. (if (and (not boundary)
  20840. (memq (org-element-property :type timestamp)
  20841. '(active-range inactive-range)))
  20842. (concat
  20843. (org-translate-time
  20844. (org-element-property :raw-value
  20845. (org-timestamp-split-range timestamp)))
  20846. "--"
  20847. (org-translate-time
  20848. (org-element-property :raw-value
  20849. (org-timestamp-split-range timestamp t))))
  20850. (org-translate-time
  20851. (org-element-property
  20852. :raw-value
  20853. (if (not boundary) timestamp
  20854. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20855. ;;; Other stuff.
  20856. (defun org-toggle-fixed-width-section (arg)
  20857. "Toggle the fixed-width export.
  20858. If there is no active region, the QUOTE keyword at the current headline is
  20859. inserted or removed. When present, it causes the text between this headline
  20860. and the next to be exported as fixed-width text, and unmodified.
  20861. If there is an active region, this command adds or removes a colon as the
  20862. first character of this line. If the first character of a line is a colon,
  20863. this line is also exported in fixed-width font."
  20864. (interactive "P")
  20865. (let* ((cc 0)
  20866. (regionp (org-region-active-p))
  20867. (beg (if regionp (region-beginning) (point)))
  20868. (end (if regionp (region-end)))
  20869. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20870. (case-fold-search nil)
  20871. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20872. off)
  20873. (if regionp
  20874. (save-excursion
  20875. (goto-char beg)
  20876. (setq cc (current-column))
  20877. (beginning-of-line 1)
  20878. (setq off (looking-at re))
  20879. (while (> nlines 0)
  20880. (setq nlines (1- nlines))
  20881. (beginning-of-line 1)
  20882. (cond
  20883. (arg
  20884. (org-move-to-column cc t)
  20885. (insert ": \n")
  20886. (forward-line -1))
  20887. ((and off (looking-at re))
  20888. (replace-match "" t t nil 1))
  20889. ((not off) (org-move-to-column cc t) (insert ": ")))
  20890. (forward-line 1)))
  20891. (save-excursion
  20892. (org-back-to-heading)
  20893. (cond
  20894. ((looking-at (format org-heading-keyword-regexp-format
  20895. org-quote-string))
  20896. (goto-char (match-end 1))
  20897. (looking-at (concat " +" org-quote-string))
  20898. (replace-match "" t t)
  20899. (when (eolp) (insert " ")))
  20900. ((looking-at org-outline-regexp)
  20901. (goto-char (match-end 0))
  20902. (insert org-quote-string " ")))))))
  20903. (defun org-reftex-citation ()
  20904. "Use reftex-citation to insert a citation into the buffer.
  20905. This looks for a line like
  20906. #+BIBLIOGRAPHY: foo plain option:-d
  20907. and derives from it that foo.bib is the bibliography file relevant
  20908. for this document. It then installs the necessary environment for RefTeX
  20909. to work in this buffer and calls `reftex-citation' to insert a citation
  20910. into the buffer.
  20911. Export of such citations to both LaTeX and HTML is handled by the contributed
  20912. package ox-bibtex by Taru Karttunen."
  20913. (interactive)
  20914. (let ((reftex-docstruct-symbol 'rds)
  20915. (reftex-cite-format "\\cite{%l}")
  20916. rds bib)
  20917. (save-excursion
  20918. (save-restriction
  20919. (widen)
  20920. (let ((case-fold-search t)
  20921. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20922. (if (not (save-excursion
  20923. (or (re-search-forward re nil t)
  20924. (re-search-backward re nil t))))
  20925. (error "No bibliography defined in file")
  20926. (setq bib (concat (match-string 1) ".bib")
  20927. rds (list (list 'bib bib)))))))
  20928. (call-interactively 'reftex-citation)))
  20929. ;;;; Functions extending outline functionality
  20930. (defun org-beginning-of-line (&optional arg)
  20931. "Go to the beginning of the current line. If that is invisible, continue
  20932. to a visible line beginning. This makes the function of C-a more intuitive.
  20933. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20934. first attempt, and only move to after the tags when the cursor is already
  20935. beyond the end of the headline."
  20936. (interactive "P")
  20937. (let ((pos (point))
  20938. (special (if (consp org-special-ctrl-a/e)
  20939. (car org-special-ctrl-a/e)
  20940. org-special-ctrl-a/e))
  20941. deactivate-mark refpos)
  20942. (if (org-bound-and-true-p visual-line-mode)
  20943. (beginning-of-visual-line 1)
  20944. (beginning-of-line 1))
  20945. (if (and arg (fboundp 'move-beginning-of-line))
  20946. (call-interactively 'move-beginning-of-line)
  20947. (if (bobp)
  20948. nil
  20949. (backward-char 1)
  20950. (if (org-truely-invisible-p)
  20951. (while (and (not (bobp)) (org-truely-invisible-p))
  20952. (backward-char 1)
  20953. (beginning-of-line 1))
  20954. (forward-char 1))))
  20955. (when special
  20956. (cond
  20957. ((and (looking-at org-complex-heading-regexp)
  20958. (= (char-after (match-end 1)) ?\ ))
  20959. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20960. (point-at-eol)))
  20961. (goto-char
  20962. (if (eq special t)
  20963. (cond ((> pos refpos) refpos)
  20964. ((= pos (point)) refpos)
  20965. (t (point)))
  20966. (cond ((> pos (point)) (point))
  20967. ((not (eq last-command this-command)) (point))
  20968. (t refpos)))))
  20969. ((org-at-item-p)
  20970. ;; Being at an item and not looking at an the item means point
  20971. ;; was previously moved to beginning of a visual line, which
  20972. ;; doesn't contain the item. Therefore, do nothing special,
  20973. ;; just stay here.
  20974. (when (looking-at org-list-full-item-re)
  20975. ;; Set special position at first white space character after
  20976. ;; bullet, and check-box, if any.
  20977. (let ((after-bullet
  20978. (let ((box (match-end 3)))
  20979. (if (not box) (match-end 1)
  20980. (let ((after (char-after box)))
  20981. (if (and after (= after ? )) (1+ box) box))))))
  20982. ;; Special case: Move point to special position when
  20983. ;; currently after it or at beginning of line.
  20984. (if (eq special t)
  20985. (when (or (> pos after-bullet) (= (point) pos))
  20986. (goto-char after-bullet))
  20987. ;; Reversed case: Move point to special position when
  20988. ;; point was already at beginning of line and command is
  20989. ;; repeated.
  20990. (when (and (= (point) pos) (eq last-command this-command))
  20991. (goto-char after-bullet))))))))
  20992. (org-no-warnings
  20993. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20994. (setq disable-point-adjustment
  20995. (or (not (invisible-p (point)))
  20996. (not (invisible-p (max (point-min) (1- (point))))))))
  20997. (defun org-end-of-line (&optional arg)
  20998. "Go to the end of the line.
  20999. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21000. tags on the first attempt, and only move to after the tags when
  21001. the cursor is already beyond the end of the headline."
  21002. (interactive "P")
  21003. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21004. org-special-ctrl-a/e))
  21005. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21006. 'end-of-visual-line)
  21007. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21008. (t 'end-of-line)))
  21009. deactivate-mark)
  21010. (if (or (not special) arg) (call-interactively move-fun)
  21011. (let* ((element (save-excursion (beginning-of-line)
  21012. (org-element-at-point)))
  21013. (type (org-element-type element)))
  21014. (cond
  21015. ((memq type '(headline inlinetask))
  21016. (let ((pos (point)))
  21017. (beginning-of-line 1)
  21018. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21019. (if (eq special t)
  21020. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21021. (goto-char (match-beginning 1))
  21022. (goto-char (match-end 0)))
  21023. (if (or (< pos (match-end 0))
  21024. (not (eq this-command last-command)))
  21025. (goto-char (match-end 0))
  21026. (goto-char (match-beginning 1))))
  21027. (call-interactively move-fun))))
  21028. ((outline-invisible-p (line-end-position))
  21029. ;; If element is hidden, `move-end-of-line' would put point
  21030. ;; after it. Use `end-of-line' to stay on current line.
  21031. (call-interactively 'end-of-line))
  21032. (t (call-interactively move-fun)))))
  21033. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21034. (setq disable-point-adjustment
  21035. (or (not (invisible-p (point)))
  21036. (not (invisible-p (max (point-min) (1- (point))))))))
  21037. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21038. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21039. (defun org-backward-sentence (&optional arg)
  21040. "Go to beginning of sentence, or beginning of table field.
  21041. This will call `backward-sentence' or `org-table-beginning-of-field',
  21042. depending on context."
  21043. (interactive "P")
  21044. (cond
  21045. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21046. (t (call-interactively 'backward-sentence))))
  21047. (defun org-forward-sentence (&optional arg)
  21048. "Go to end of sentence, or end of table field.
  21049. This will call `forward-sentence' or `org-table-end-of-field',
  21050. depending on context."
  21051. (interactive "P")
  21052. (cond
  21053. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21054. (t (call-interactively 'forward-sentence))))
  21055. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21056. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21057. (defun org-kill-line (&optional arg)
  21058. "Kill line, to tags or end of line."
  21059. (interactive "P")
  21060. (cond
  21061. ((or (not org-special-ctrl-k)
  21062. (bolp)
  21063. (not (org-at-heading-p)))
  21064. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21065. org-ctrl-k-protect-subtree)
  21066. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21067. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21068. (user-error "C-k aborted as it would kill a hidden subtree")))
  21069. (call-interactively
  21070. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21071. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21072. (kill-region (point) (match-beginning 1))
  21073. (org-set-tags nil t))
  21074. (t (kill-region (point) (point-at-eol)))))
  21075. (define-key org-mode-map "\C-k" 'org-kill-line)
  21076. (defun org-yank (&optional arg)
  21077. "Yank. If the kill is a subtree, treat it specially.
  21078. This command will look at the current kill and check if is a single
  21079. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21080. cursor is at the beginning of a line or after the stars of a currently
  21081. empty headline, then the yank is handled specially. How exactly depends
  21082. on the value of the following variables, both set by default.
  21083. org-yank-folded-subtrees
  21084. When set, the subtree(s) will be folded after insertion, but only
  21085. if doing so would now swallow text after the yanked text.
  21086. org-yank-adjusted-subtrees
  21087. When set, the subtree will be promoted or demoted in order to
  21088. fit into the local outline tree structure, which means that the level
  21089. will be adjusted so that it becomes the smaller one of the two
  21090. *visible* surrounding headings.
  21091. Any prefix to this command will cause `yank' to be called directly with
  21092. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21093. will just
  21094. plainly yank the text as it is.
  21095. \[1] The test checks if the first non-white line is a heading
  21096. and if there are no other headings with fewer stars."
  21097. (interactive "P")
  21098. (org-yank-generic 'yank arg))
  21099. (defun org-yank-generic (command arg)
  21100. "Perform some yank-like command.
  21101. This function implements the behavior described in the `org-yank'
  21102. documentation. However, it has been generalized to work for any
  21103. interactive command with similar behavior."
  21104. ;; pretend to be command COMMAND
  21105. (setq this-command command)
  21106. (if arg
  21107. (call-interactively command)
  21108. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21109. (and (org-kill-is-subtree-p)
  21110. (or (bolp)
  21111. (and (looking-at "[ \t]*$")
  21112. (string-match
  21113. "\\`\\*+\\'"
  21114. (buffer-substring (point-at-bol) (point)))))))
  21115. swallowp)
  21116. (cond
  21117. ((and subtreep org-yank-folded-subtrees)
  21118. (let ((beg (point))
  21119. end)
  21120. (if (and subtreep org-yank-adjusted-subtrees)
  21121. (org-paste-subtree nil nil 'for-yank)
  21122. (call-interactively command))
  21123. (setq end (point))
  21124. (goto-char beg)
  21125. (when (and (bolp) subtreep
  21126. (not (setq swallowp
  21127. (org-yank-folding-would-swallow-text beg end))))
  21128. (org-with-limited-levels
  21129. (or (looking-at org-outline-regexp)
  21130. (re-search-forward org-outline-regexp-bol end t))
  21131. (while (and (< (point) end) (looking-at org-outline-regexp))
  21132. (hide-subtree)
  21133. (org-cycle-show-empty-lines 'folded)
  21134. (condition-case nil
  21135. (outline-forward-same-level 1)
  21136. (error (goto-char end))))))
  21137. (when swallowp
  21138. (message
  21139. "Inserted text not folded because that would swallow text"))
  21140. (goto-char end)
  21141. (skip-chars-forward " \t\n\r")
  21142. (beginning-of-line 1)
  21143. (push-mark beg 'nomsg)))
  21144. ((and subtreep org-yank-adjusted-subtrees)
  21145. (let ((beg (point-at-bol)))
  21146. (org-paste-subtree nil nil 'for-yank)
  21147. (push-mark beg 'nomsg)))
  21148. (t
  21149. (call-interactively command))))))
  21150. (defun org-yank-folding-would-swallow-text (beg end)
  21151. "Would hide-subtree at BEG swallow any text after END?"
  21152. (let (level)
  21153. (org-with-limited-levels
  21154. (save-excursion
  21155. (goto-char beg)
  21156. (when (or (looking-at org-outline-regexp)
  21157. (re-search-forward org-outline-regexp-bol end t))
  21158. (setq level (org-outline-level)))
  21159. (goto-char end)
  21160. (skip-chars-forward " \t\r\n\v\f")
  21161. (if (or (eobp)
  21162. (and (bolp) (looking-at org-outline-regexp)
  21163. (<= (org-outline-level) level)))
  21164. nil ; Nothing would be swallowed
  21165. t))))) ; something would swallow
  21166. (define-key org-mode-map "\C-y" 'org-yank)
  21167. (defun org-truely-invisible-p ()
  21168. "Check if point is at a character currently not visible.
  21169. This version does not only check the character property, but also
  21170. `visible-mode'."
  21171. ;; Early versions of noutline don't have `outline-invisible-p'.
  21172. (if (org-bound-and-true-p visible-mode)
  21173. nil
  21174. (outline-invisible-p)))
  21175. (defun org-invisible-p2 ()
  21176. "Check if point is at a character currently not visible."
  21177. (save-excursion
  21178. (if (and (eolp) (not (bobp))) (backward-char 1))
  21179. ;; Early versions of noutline don't have `outline-invisible-p'.
  21180. (outline-invisible-p)))
  21181. (defun org-back-to-heading (&optional invisible-ok)
  21182. "Call `outline-back-to-heading', but provide a better error message."
  21183. (condition-case nil
  21184. (outline-back-to-heading invisible-ok)
  21185. (error (error "Before first headline at position %d in buffer %s"
  21186. (point) (current-buffer)))))
  21187. (defun org-before-first-heading-p ()
  21188. "Before first heading?"
  21189. (save-excursion
  21190. (end-of-line)
  21191. (null (re-search-backward org-outline-regexp-bol nil t))))
  21192. (defun org-at-heading-p (&optional ignored)
  21193. (outline-on-heading-p t))
  21194. ;; Compatibility alias with Org versions < 7.8.03
  21195. (defalias 'org-on-heading-p 'org-at-heading-p)
  21196. (defun org-at-comment-p nil
  21197. "Is cursor in a line starting with a # character?"
  21198. (save-excursion
  21199. (beginning-of-line)
  21200. (looking-at "^#")))
  21201. (defun org-at-drawer-p nil
  21202. "Is cursor at a drawer keyword?"
  21203. (save-excursion
  21204. (move-beginning-of-line 1)
  21205. (looking-at org-drawer-regexp)))
  21206. (defun org-at-block-p nil
  21207. "Is cursor at a block keyword?"
  21208. (save-excursion
  21209. (move-beginning-of-line 1)
  21210. (looking-at org-block-regexp)))
  21211. (defun org-point-at-end-of-empty-headline ()
  21212. "If point is at the end of an empty headline, return t, else nil.
  21213. If the heading only contains a TODO keyword, it is still still considered
  21214. empty."
  21215. (and (looking-at "[ \t]*$")
  21216. (when org-todo-line-regexp
  21217. (save-excursion
  21218. (beginning-of-line 1)
  21219. (let ((case-fold-search nil))
  21220. (looking-at org-todo-line-regexp)
  21221. (string= (match-string 3) ""))))))
  21222. (defun org-at-heading-or-item-p ()
  21223. (or (org-at-heading-p) (org-at-item-p)))
  21224. (defun org-at-target-p ()
  21225. (or (org-in-regexp org-radio-target-regexp)
  21226. (org-in-regexp org-target-regexp)))
  21227. ;; Compatibility alias with Org versions < 7.8.03
  21228. (defalias 'org-on-target-p 'org-at-target-p)
  21229. (defun org-up-heading-all (arg)
  21230. "Move to the heading line of which the present line is a subheading.
  21231. This function considers both visible and invisible heading lines.
  21232. With argument, move up ARG levels."
  21233. (if (fboundp 'outline-up-heading-all)
  21234. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21235. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21236. (defun org-up-heading-safe ()
  21237. "Move to the heading line of which the present line is a subheading.
  21238. This version will not throw an error. It will return the level of the
  21239. headline found, or nil if no higher level is found.
  21240. Also, this function will be a lot faster than `outline-up-heading',
  21241. because it relies on stars being the outline starters. This can really
  21242. make a significant difference in outlines with very many siblings."
  21243. (let (start-level re)
  21244. (org-back-to-heading t)
  21245. (setq start-level (funcall outline-level))
  21246. (if (equal start-level 1)
  21247. nil
  21248. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21249. (if (re-search-backward re nil t)
  21250. (funcall outline-level)))))
  21251. (defun org-first-sibling-p ()
  21252. "Is this heading the first child of its parents?"
  21253. (interactive)
  21254. (let ((re org-outline-regexp-bol)
  21255. level l)
  21256. (unless (org-at-heading-p t)
  21257. (user-error "Not at a heading"))
  21258. (setq level (funcall outline-level))
  21259. (save-excursion
  21260. (if (not (re-search-backward re nil t))
  21261. t
  21262. (setq l (funcall outline-level))
  21263. (< l level)))))
  21264. (defun org-goto-sibling (&optional previous)
  21265. "Goto the next sibling, even if it is invisible.
  21266. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21267. when a sibling was found. When none is found, return nil and don't
  21268. move point."
  21269. (let ((fun (if previous 're-search-backward 're-search-forward))
  21270. (pos (point))
  21271. (re org-outline-regexp-bol)
  21272. level l)
  21273. (when (condition-case nil (org-back-to-heading t) (error nil))
  21274. (setq level (funcall outline-level))
  21275. (catch 'exit
  21276. (or previous (forward-char 1))
  21277. (while (funcall fun re nil t)
  21278. (setq l (funcall outline-level))
  21279. (when (< l level) (goto-char pos) (throw 'exit nil))
  21280. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21281. (goto-char pos)
  21282. nil))))
  21283. (defun org-show-siblings ()
  21284. "Show all siblings of the current headline."
  21285. (save-excursion
  21286. (while (org-goto-sibling) (org-flag-heading nil)))
  21287. (save-excursion
  21288. (while (org-goto-sibling 'previous)
  21289. (org-flag-heading nil))))
  21290. (defun org-goto-first-child ()
  21291. "Goto the first child, even if it is invisible.
  21292. Return t when a child was found. Otherwise don't move point and
  21293. return nil."
  21294. (let (level (pos (point)) (re org-outline-regexp-bol))
  21295. (when (condition-case nil (org-back-to-heading t) (error nil))
  21296. (setq level (outline-level))
  21297. (forward-char 1)
  21298. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21299. (progn (goto-char (match-beginning 0)) t)
  21300. (goto-char pos) nil))))
  21301. (defun org-show-hidden-entry ()
  21302. "Show an entry where even the heading is hidden."
  21303. (save-excursion
  21304. (org-show-entry)))
  21305. (defun org-flag-heading (flag &optional entry)
  21306. "Flag the current heading. FLAG non-nil means make invisible.
  21307. When ENTRY is non-nil, show the entire entry."
  21308. (save-excursion
  21309. (org-back-to-heading t)
  21310. ;; Check if we should show the entire entry
  21311. (if entry
  21312. (progn
  21313. (org-show-entry)
  21314. (save-excursion
  21315. (and (outline-next-heading)
  21316. (org-flag-heading nil))))
  21317. (outline-flag-region (max (point-min) (1- (point)))
  21318. (save-excursion (outline-end-of-heading) (point))
  21319. flag))))
  21320. (defun org-get-next-sibling ()
  21321. "Move to next heading of the same level, and return point.
  21322. If there is no such heading, return nil.
  21323. This is like outline-next-sibling, but invisible headings are ok."
  21324. (let ((level (funcall outline-level)))
  21325. (outline-next-heading)
  21326. (while (and (not (eobp)) (> (funcall outline-level) level))
  21327. (outline-next-heading))
  21328. (if (or (eobp) (< (funcall outline-level) level))
  21329. nil
  21330. (point))))
  21331. (defun org-get-last-sibling ()
  21332. "Move to previous heading of the same level, and return point.
  21333. If there is no such heading, return nil."
  21334. (let ((opoint (point))
  21335. (level (funcall outline-level)))
  21336. (outline-previous-heading)
  21337. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21338. (while (and (> (funcall outline-level) level)
  21339. (not (bobp)))
  21340. (outline-previous-heading))
  21341. (if (< (funcall outline-level) level)
  21342. nil
  21343. (point)))))
  21344. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21345. "Goto to the end of a subtree."
  21346. ;; This contains an exact copy of the original function, but it uses
  21347. ;; `org-back-to-heading', to make it work also in invisible
  21348. ;; trees. And is uses an invisible-ok argument.
  21349. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21350. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21351. ;; with many children and grandchildren etc, this can be much faster
  21352. ;; than the outline version.
  21353. (org-back-to-heading invisible-ok)
  21354. (let ((first t)
  21355. (level (funcall outline-level)))
  21356. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21357. ;; A true heading (not a plain list item), in Org-mode
  21358. ;; This means we can easily find the end by looking
  21359. ;; only for the right number of stars. Using a regexp to do
  21360. ;; this is so much faster than using a Lisp loop.
  21361. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21362. (forward-char 1)
  21363. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21364. ;; something else, do it the slow way
  21365. (while (and (not (eobp))
  21366. (or first (> (funcall outline-level) level)))
  21367. (setq first nil)
  21368. (outline-next-heading)))
  21369. (unless to-heading
  21370. (if (memq (preceding-char) '(?\n ?\^M))
  21371. (progn
  21372. ;; Go to end of line before heading
  21373. (forward-char -1)
  21374. (if (memq (preceding-char) '(?\n ?\^M))
  21375. ;; leave blank line before heading
  21376. (forward-char -1))))))
  21377. (point))
  21378. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21379. "Use Org version in org-mode, for dramatic speed-up."
  21380. (if (derived-mode-p 'org-mode)
  21381. (progn
  21382. (org-end-of-subtree nil t)
  21383. (unless (eobp) (backward-char 1)))
  21384. ad-do-it))
  21385. (defun org-end-of-meta-data-and-drawers ()
  21386. "Jump to the first text after meta data and drawers in the current entry.
  21387. This will move over empty lines, lines with planning time stamps,
  21388. clocking lines, and drawers."
  21389. (org-back-to-heading t)
  21390. (let ((end (save-excursion (outline-next-heading) (point)))
  21391. (re (concat "\\(" org-drawer-regexp "\\)"
  21392. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21393. (forward-line 1)
  21394. (while (re-search-forward re end t)
  21395. (if (not (match-end 1))
  21396. ;; empty or planning line
  21397. (forward-line 1)
  21398. ;; a drawer, find the end
  21399. (re-search-forward "^[ \t]*:END:" end 'move)
  21400. (forward-line 1)))
  21401. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21402. (point)))
  21403. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21404. "Move forward to the ARG'th subheading at same level as this one.
  21405. Stop at the first and last subheadings of a superior heading.
  21406. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21407. non-nil it will also look at invisible ones."
  21408. (interactive "p")
  21409. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21410. (if (and arg (< arg 0))
  21411. (goto-char (point-min))
  21412. (outline-next-heading))
  21413. (org-at-heading-p)
  21414. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21415. (f (if (and arg (< arg 0))
  21416. 're-search-backward
  21417. 're-search-forward))
  21418. (count (if arg (abs arg) 1))
  21419. (result (point)))
  21420. (while (and (prog1 (> count 0)
  21421. (forward-char (if (and arg (< arg 0)) -1 1)))
  21422. (funcall f org-outline-regexp-bol nil 'move))
  21423. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21424. (cond ((< l level) (setq count 0))
  21425. ((and (= l level)
  21426. (or invisible-ok
  21427. (progn
  21428. (goto-char (line-beginning-position))
  21429. (not (outline-invisible-p)))))
  21430. (setq count (1- count))
  21431. (when (eq l level)
  21432. (setq result (point)))))))
  21433. (goto-char result))
  21434. (beginning-of-line 1)))
  21435. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21436. "Move backward to the ARG'th subheading at same level as this one.
  21437. Stop at the first and last subheadings of a superior heading."
  21438. (interactive "p")
  21439. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21440. (defun org-next-block (arg &optional backward block-regexp)
  21441. "Jump to the next block.
  21442. With a prefix argument ARG, jump forward ARG many source blocks.
  21443. When BACKWARD is non-nil, jump to the previous block.
  21444. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21445. (interactive "p")
  21446. (let ((re (or block-regexp org-block-regexp))
  21447. (re-search-fn (or (and backward 're-search-backward)
  21448. 're-search-forward)))
  21449. (if (looking-at re) (forward-char 1))
  21450. (condition-case nil
  21451. (funcall re-search-fn re nil nil arg)
  21452. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21453. (goto-char (match-beginning 0)) (org-show-context)))
  21454. (defun org-previous-block (arg &optional block-regexp)
  21455. "Jump to the previous block.
  21456. With a prefix argument ARG, jump backward ARG many source blocks.
  21457. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21458. (interactive "p")
  21459. (org-next-block arg t block-regexp))
  21460. (defun org-forward-paragraph ()
  21461. "Move forward to beginning of next paragraph or equivalent.
  21462. The function moves point to the beginning of the next visible
  21463. structural element, which can be a paragraph, a table, a list
  21464. item, etc. It also provides some special moves for convenience:
  21465. - On an affiliated keyword, jump to the beginning of the
  21466. relative element.
  21467. - On an item or a footnote definition, move to the second
  21468. element inside, if any.
  21469. - On a table or a property drawer, jump after it.
  21470. - On a verse or source block, stop after blank lines."
  21471. (interactive)
  21472. (when (eobp) (user-error "Cannot move further down"))
  21473. (let* ((deactivate-mark nil)
  21474. (element (org-element-at-point))
  21475. (type (org-element-type element))
  21476. (post-affiliated (org-element-property :post-affiliated element))
  21477. (contents-begin (org-element-property :contents-begin element))
  21478. (contents-end (org-element-property :contents-end element))
  21479. (end (let ((end (org-element-property :end element)) (parent element))
  21480. (while (and (setq parent (org-element-property :parent parent))
  21481. (= (org-element-property :contents-end parent) end))
  21482. (setq end (org-element-property :end parent)))
  21483. end)))
  21484. (cond ((not element)
  21485. (skip-chars-forward " \r\t\n")
  21486. (or (eobp) (beginning-of-line)))
  21487. ;; On affiliated keywords, move to element's beginning.
  21488. ((and post-affiliated (< (point) post-affiliated))
  21489. (goto-char post-affiliated))
  21490. ;; At a table row, move to the end of the table. Similarly,
  21491. ;; at a node property, move to the end of the property
  21492. ;; drawer.
  21493. ((memq type '(node-property table-row))
  21494. (goto-char (org-element-property
  21495. :end (org-element-property :parent element))))
  21496. ((memq type '(property-drawer table)) (goto-char end))
  21497. ;; Consider blank lines as separators in verse and source
  21498. ;; blocks to ease editing.
  21499. ((memq type '(src-block verse-block))
  21500. (when (eq type 'src-block)
  21501. (setq contents-end
  21502. (save-excursion (goto-char end)
  21503. (skip-chars-backward " \r\t\n")
  21504. (line-beginning-position))))
  21505. (beginning-of-line)
  21506. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21507. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21508. (goto-char end)
  21509. (skip-chars-forward " \r\t\n")
  21510. (if (= (point) contents-end) (goto-char end)
  21511. (beginning-of-line))))
  21512. ;; With no contents, just skip element.
  21513. ((not contents-begin) (goto-char end))
  21514. ;; If contents are invisible, skip the element altogether.
  21515. ((outline-invisible-p (line-end-position))
  21516. (case type
  21517. (headline
  21518. (org-with-limited-levels (outline-next-visible-heading 1)))
  21519. ;; At a plain list, make sure we move to the next item
  21520. ;; instead of skipping the whole list.
  21521. (plain-list (forward-char)
  21522. (org-forward-paragraph))
  21523. (otherwise (goto-char end))))
  21524. ((>= (point) contents-end) (goto-char end))
  21525. ((>= (point) contents-begin)
  21526. ;; This can only happen on paragraphs and plain lists.
  21527. (case type
  21528. (paragraph (goto-char end))
  21529. ;; At a plain list, try to move to second element in
  21530. ;; first item, if possible.
  21531. (plain-list (end-of-line)
  21532. (org-forward-paragraph))))
  21533. ;; When contents start on the middle of a line (e.g. in
  21534. ;; items and footnote definitions), try to reach first
  21535. ;; element starting after current line.
  21536. ((> (line-end-position) contents-begin)
  21537. (end-of-line)
  21538. (org-forward-paragraph))
  21539. (t (goto-char contents-begin)))))
  21540. (defun org-backward-paragraph ()
  21541. "Move backward to start of previous paragraph or equivalent.
  21542. The function moves point to the beginning of the current
  21543. structural element, which can be a paragraph, a table, a list
  21544. item, etc., or to the beginning of the previous visible one if
  21545. point is already there. It also provides some special moves for
  21546. convenience:
  21547. - On an affiliated keyword, jump to the first one.
  21548. - On a table or a property drawer, move to its beginning.
  21549. - On a verse or source block, stop before blank lines."
  21550. (interactive)
  21551. (when (bobp) (user-error "Cannot move further up"))
  21552. (let* ((deactivate-mark nil)
  21553. (element (org-element-at-point))
  21554. (type (org-element-type element))
  21555. (contents-begin (org-element-property :contents-begin element))
  21556. (contents-end (org-element-property :contents-end element))
  21557. (post-affiliated (org-element-property :post-affiliated element))
  21558. (begin (org-element-property :begin element)))
  21559. (cond
  21560. ((not element) (goto-char (point-min)))
  21561. ((= (point) begin)
  21562. (backward-char)
  21563. (org-backward-paragraph))
  21564. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21565. ((memq type '(node-property table-row))
  21566. (goto-char (org-element-property
  21567. :post-affiliated (org-element-property :parent element))))
  21568. ((memq type '(property-drawer table)) (goto-char begin))
  21569. ((memq type '(src-block verse-block))
  21570. (when (eq type 'src-block)
  21571. (setq contents-begin
  21572. (save-excursion (goto-char begin) (forward-line) (point))))
  21573. (if (= (point) contents-begin) (goto-char post-affiliated)
  21574. ;; Inside a verse block, see blank lines as paragraph
  21575. ;; separators.
  21576. (let ((origin (point)))
  21577. (skip-chars-backward " \r\t\n" contents-begin)
  21578. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21579. (skip-chars-forward " \r\t\n" origin)
  21580. (if (= (point) origin) (goto-char contents-begin)
  21581. (beginning-of-line))))))
  21582. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21583. ((eq type 'paragraph)
  21584. (goto-char contents-begin)
  21585. ;; When at first paragraph in an item or a footnote definition,
  21586. ;; move directly to beginning of line.
  21587. (let ((parent-contents
  21588. (org-element-property
  21589. :contents-begin (org-element-property :parent element))))
  21590. (when (and parent-contents (= parent-contents contents-begin))
  21591. (beginning-of-line))))
  21592. ;; At the end of a greater element, move to the beginning of the
  21593. ;; last element within.
  21594. ((>= (point) contents-end)
  21595. (goto-char (1- contents-end))
  21596. (org-backward-paragraph))
  21597. (t (goto-char (or post-affiliated begin))))
  21598. ;; Ensure we never leave point invisible.
  21599. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21600. (defun org-forward-element ()
  21601. "Move forward by one element.
  21602. Move to the next element at the same level, when possible."
  21603. (interactive)
  21604. (cond ((eobp) (user-error "Cannot move further down"))
  21605. ((org-with-limited-levels (org-at-heading-p))
  21606. (let ((origin (point)))
  21607. (goto-char (org-end-of-subtree nil t))
  21608. (unless (org-with-limited-levels (org-at-heading-p))
  21609. (goto-char origin)
  21610. (user-error "Cannot move further down"))))
  21611. (t
  21612. (let* ((elem (org-element-at-point))
  21613. (end (org-element-property :end elem))
  21614. (parent (org-element-property :parent elem)))
  21615. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21616. (goto-char (org-element-property :end parent)))
  21617. ((integer-or-marker-p end) (goto-char end))
  21618. (t (message "No element at point")))))))
  21619. (defun org-backward-element ()
  21620. "Move backward by one element.
  21621. Move to the previous element at the same level, when possible."
  21622. (interactive)
  21623. (cond ((bobp) (user-error "Cannot move further up"))
  21624. ((org-with-limited-levels (org-at-heading-p))
  21625. ;; At a headline, move to the previous one, if any, or stay
  21626. ;; here.
  21627. (let ((origin (point)))
  21628. (org-with-limited-levels (org-backward-heading-same-level 1))
  21629. ;; When current headline has no sibling above, move to its
  21630. ;; parent.
  21631. (when (= (point) origin)
  21632. (or (org-with-limited-levels (org-up-heading-safe))
  21633. (progn (goto-char origin)
  21634. (user-error "Cannot move further up"))))))
  21635. (t
  21636. (let* ((trail (org-element-at-point 'keep-trail))
  21637. (elem (car trail))
  21638. (prev-elem (nth 1 trail))
  21639. (beg (org-element-property :begin elem)))
  21640. (cond
  21641. ;; Move to beginning of current element if point isn't
  21642. ;; there already.
  21643. ((null beg) (message "No element at point"))
  21644. ((/= (point) beg) (goto-char beg))
  21645. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21646. ((org-before-first-heading-p) (goto-char (point-min)))
  21647. (t (org-back-to-heading)))))))
  21648. (defun org-up-element ()
  21649. "Move to upper element."
  21650. (interactive)
  21651. (if (org-with-limited-levels (org-at-heading-p))
  21652. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21653. (let* ((elem (org-element-at-point))
  21654. (parent (org-element-property :parent elem)))
  21655. (if parent (goto-char (org-element-property :begin parent))
  21656. (if (org-with-limited-levels (org-before-first-heading-p))
  21657. (user-error "No surrounding element")
  21658. (org-with-limited-levels (org-back-to-heading)))))))
  21659. (defvar org-element-greater-elements)
  21660. (defun org-down-element ()
  21661. "Move to inner element."
  21662. (interactive)
  21663. (let ((element (org-element-at-point)))
  21664. (cond
  21665. ((memq (org-element-type element) '(plain-list table))
  21666. (goto-char (org-element-property :contents-begin element))
  21667. (forward-char))
  21668. ((memq (org-element-type element) org-element-greater-elements)
  21669. ;; If contents are hidden, first disclose them.
  21670. (when (outline-invisible-p (line-end-position)) (org-cycle))
  21671. (goto-char (or (org-element-property :contents-begin element)
  21672. (user-error "No content for this element"))))
  21673. (t (user-error "No inner element")))))
  21674. (defun org-drag-element-backward ()
  21675. "Move backward element at point."
  21676. (interactive)
  21677. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21678. (let* ((trail (org-element-at-point 'keep-trail))
  21679. (elem (car trail))
  21680. (prev-elem (nth 1 trail)))
  21681. ;; Error out if no previous element or previous element is
  21682. ;; a parent of the current one.
  21683. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21684. (user-error "Cannot drag element backward")
  21685. (let ((pos (point)))
  21686. (org-element-swap-A-B prev-elem elem)
  21687. (goto-char (+ (org-element-property :begin prev-elem)
  21688. (- pos (org-element-property :begin elem)))))))))
  21689. (defun org-drag-element-forward ()
  21690. "Move forward element at point."
  21691. (interactive)
  21692. (let* ((pos (point))
  21693. (elem (org-element-at-point)))
  21694. (when (= (point-max) (org-element-property :end elem))
  21695. (user-error "Cannot drag element forward"))
  21696. (goto-char (org-element-property :end elem))
  21697. (let ((next-elem (org-element-at-point)))
  21698. (when (or (org-element-nested-p elem next-elem)
  21699. (and (eq (org-element-type next-elem) 'headline)
  21700. (not (eq (org-element-type elem) 'headline))))
  21701. (goto-char pos)
  21702. (user-error "Cannot drag element forward"))
  21703. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21704. ;; body's length (without final blanks) and by the length of
  21705. ;; blanks between ELEM and NEXT-ELEM.
  21706. (let ((size-next (- (save-excursion
  21707. (goto-char (org-element-property :end next-elem))
  21708. (skip-chars-backward " \r\t\n")
  21709. (forward-line)
  21710. ;; Small correction if buffer doesn't end
  21711. ;; with a newline character.
  21712. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21713. (org-element-property :begin next-elem)))
  21714. (size-blank (- (org-element-property :end elem)
  21715. (save-excursion
  21716. (goto-char (org-element-property :end elem))
  21717. (skip-chars-backward " \r\t\n")
  21718. (forward-line)
  21719. (point)))))
  21720. (org-element-swap-A-B elem next-elem)
  21721. (goto-char (+ pos size-next size-blank))))))
  21722. (defun org-drag-line-forward (arg)
  21723. "Drag the line at point ARG lines forward."
  21724. (interactive "p")
  21725. (dotimes (n (abs arg))
  21726. (let ((c (current-column)))
  21727. (if (< 0 arg)
  21728. (progn
  21729. (beginning-of-line 2)
  21730. (transpose-lines 1)
  21731. (beginning-of-line 0))
  21732. (transpose-lines 1)
  21733. (beginning-of-line -1))
  21734. (org-move-to-column c))))
  21735. (defun org-drag-line-backward (arg)
  21736. "Drag the line at point ARG lines backward."
  21737. (interactive "p")
  21738. (org-drag-line-forward (- arg)))
  21739. (defun org-mark-element ()
  21740. "Put point at beginning of this element, mark at end.
  21741. Interactively, if this command is repeated or (in Transient Mark
  21742. mode) if the mark is active, it marks the next element after the
  21743. ones already marked."
  21744. (interactive)
  21745. (let (deactivate-mark)
  21746. (if (and (org-called-interactively-p 'any)
  21747. (or (and (eq last-command this-command) (mark t))
  21748. (and transient-mark-mode mark-active)))
  21749. (set-mark
  21750. (save-excursion
  21751. (goto-char (mark))
  21752. (goto-char (org-element-property :end (org-element-at-point)))))
  21753. (let ((element (org-element-at-point)))
  21754. (end-of-line)
  21755. (push-mark (org-element-property :end element) t t)
  21756. (goto-char (org-element-property :begin element))))))
  21757. (defun org-narrow-to-element ()
  21758. "Narrow buffer to current element."
  21759. (interactive)
  21760. (let ((elem (org-element-at-point)))
  21761. (cond
  21762. ((eq (car elem) 'headline)
  21763. (narrow-to-region
  21764. (org-element-property :begin elem)
  21765. (org-element-property :end elem)))
  21766. ((memq (car elem) org-element-greater-elements)
  21767. (narrow-to-region
  21768. (org-element-property :contents-begin elem)
  21769. (org-element-property :contents-end elem)))
  21770. (t
  21771. (narrow-to-region
  21772. (org-element-property :begin elem)
  21773. (org-element-property :end elem))))))
  21774. (defun org-transpose-element ()
  21775. "Transpose current and previous elements, keeping blank lines between.
  21776. Point is moved after both elements."
  21777. (interactive)
  21778. (org-skip-whitespace)
  21779. (let ((end (org-element-property :end (org-element-at-point))))
  21780. (org-drag-element-backward)
  21781. (goto-char end)))
  21782. (defun org-unindent-buffer ()
  21783. "Un-indent the visible part of the buffer.
  21784. Relative indentation (between items, inside blocks, etc.) isn't
  21785. modified."
  21786. (interactive)
  21787. (unless (eq major-mode 'org-mode)
  21788. (user-error "Cannot un-indent a buffer not in Org mode"))
  21789. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21790. unindent-tree ; For byte-compiler.
  21791. (unindent-tree
  21792. (function
  21793. (lambda (contents)
  21794. (mapc
  21795. (lambda (element)
  21796. (if (memq (org-element-type element) '(headline section))
  21797. (funcall unindent-tree (org-element-contents element))
  21798. (save-excursion
  21799. (save-restriction
  21800. (narrow-to-region
  21801. (org-element-property :begin element)
  21802. (org-element-property :end element))
  21803. (org-do-remove-indentation)))))
  21804. (reverse contents))))))
  21805. (funcall unindent-tree (org-element-contents parse-tree))))
  21806. (defun org-show-subtree ()
  21807. "Show everything after this heading at deeper levels."
  21808. (interactive)
  21809. (outline-flag-region
  21810. (point)
  21811. (save-excursion
  21812. (org-end-of-subtree t t))
  21813. nil))
  21814. (defun org-show-entry ()
  21815. "Show the body directly following this heading.
  21816. Show the heading too, if it is currently invisible."
  21817. (interactive)
  21818. (save-excursion
  21819. (condition-case nil
  21820. (progn
  21821. (org-back-to-heading t)
  21822. (outline-flag-region
  21823. (max (point-min) (1- (point)))
  21824. (save-excursion
  21825. (if (re-search-forward
  21826. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21827. (match-beginning 1)
  21828. (point-max)))
  21829. nil)
  21830. (org-cycle-hide-drawers 'children))
  21831. (error nil))))
  21832. (defun org-make-options-regexp (kwds &optional extra)
  21833. "Make a regular expression for keyword lines."
  21834. (concat
  21835. "^#\\+\\("
  21836. (mapconcat 'regexp-quote kwds "\\|")
  21837. (if extra (concat "\\|" extra))
  21838. "\\):[ \t]*\\(.*\\)"))
  21839. ;; Make isearch reveal the necessary context
  21840. (defun org-isearch-end ()
  21841. "Reveal context after isearch exits."
  21842. (when isearch-success ; only if search was successful
  21843. (if (featurep 'xemacs)
  21844. ;; Under XEmacs, the hook is run in the correct place,
  21845. ;; we directly show the context.
  21846. (org-show-context 'isearch)
  21847. ;; In Emacs the hook runs *before* restoring the overlays.
  21848. ;; So we have to use a one-time post-command-hook to do this.
  21849. ;; (Emacs 22 has a special variable, see function `org-mode')
  21850. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21851. isearch-mode-end-hook-quit)
  21852. ;; Only when the isearch was not quitted.
  21853. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21854. 'append 'local)))
  21855. (org-fix-ellipsis-at-bol)))
  21856. (defun org-isearch-post-command ()
  21857. "Remove self from hook, and show context."
  21858. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21859. (org-show-context 'isearch))
  21860. ;;;; Integration with and fixes for other packages
  21861. ;;; Imenu support
  21862. (defvar org-imenu-markers nil
  21863. "All markers currently used by Imenu.")
  21864. (make-variable-buffer-local 'org-imenu-markers)
  21865. (defun org-imenu-new-marker (&optional pos)
  21866. "Return a new marker for use by Imenu, and remember the marker."
  21867. (let ((m (make-marker)))
  21868. (move-marker m (or pos (point)))
  21869. (push m org-imenu-markers)
  21870. m))
  21871. (defun org-imenu-get-tree ()
  21872. "Produce the index for Imenu."
  21873. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21874. (setq org-imenu-markers nil)
  21875. (let* ((n org-imenu-depth)
  21876. (re (concat "^" (org-get-limited-outline-regexp)))
  21877. (subs (make-vector (1+ n) nil))
  21878. (last-level 0)
  21879. m level head0 head)
  21880. (save-excursion
  21881. (save-restriction
  21882. (widen)
  21883. (goto-char (point-max))
  21884. (while (re-search-backward re nil t)
  21885. (setq level (org-reduced-level (funcall outline-level)))
  21886. (when (and (<= level n)
  21887. (looking-at org-complex-heading-regexp)
  21888. (setq head0 (org-match-string-no-properties 4)))
  21889. (setq head (org-link-display-format head0)
  21890. m (org-imenu-new-marker))
  21891. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21892. (if (>= level last-level)
  21893. (push (cons head m) (aref subs level))
  21894. (push (cons head (aref subs (1+ level))) (aref subs level))
  21895. (loop for i from (1+ level) to n do (aset subs i nil)))
  21896. (setq last-level level)))))
  21897. (aref subs 1)))
  21898. (eval-after-load "imenu"
  21899. '(progn
  21900. (add-hook 'imenu-after-jump-hook
  21901. (lambda ()
  21902. (if (derived-mode-p 'org-mode)
  21903. (org-show-context 'org-goto))))))
  21904. (defun org-link-display-format (link)
  21905. "Replace a link with its the description.
  21906. If there is no description, use the link target."
  21907. (save-match-data
  21908. (if (string-match org-bracket-link-analytic-regexp link)
  21909. (replace-match (if (match-end 5)
  21910. (match-string 5 link)
  21911. (concat (match-string 1 link)
  21912. (match-string 3 link)))
  21913. nil t link)
  21914. link)))
  21915. (defun org-toggle-link-display ()
  21916. "Toggle the literal or descriptive display of links."
  21917. (interactive)
  21918. (if org-descriptive-links
  21919. (progn (org-remove-from-invisibility-spec '(org-link))
  21920. (org-restart-font-lock)
  21921. (setq org-descriptive-links nil))
  21922. (progn (add-to-invisibility-spec '(org-link))
  21923. (org-restart-font-lock)
  21924. (setq org-descriptive-links t))))
  21925. ;; Speedbar support
  21926. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21927. "Overlay marking the agenda restriction line in speedbar.")
  21928. (overlay-put org-speedbar-restriction-lock-overlay
  21929. 'face 'org-agenda-restriction-lock)
  21930. (overlay-put org-speedbar-restriction-lock-overlay
  21931. 'help-echo "Agendas are currently limited to this item.")
  21932. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21933. (defun org-speedbar-set-agenda-restriction ()
  21934. "Restrict future agenda commands to the location at point in speedbar.
  21935. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21936. (interactive)
  21937. (require 'org-agenda)
  21938. (let (p m tp np dir txt)
  21939. (cond
  21940. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21941. 'org-imenu t))
  21942. (setq m (get-text-property p 'org-imenu-marker))
  21943. (with-current-buffer (marker-buffer m)
  21944. (goto-char m)
  21945. (org-agenda-set-restriction-lock 'subtree)))
  21946. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21947. 'speedbar-function 'speedbar-find-file))
  21948. (setq tp (previous-single-property-change
  21949. (1+ p) 'speedbar-function)
  21950. np (next-single-property-change
  21951. tp 'speedbar-function)
  21952. dir (speedbar-line-directory)
  21953. txt (buffer-substring-no-properties (or tp (point-min))
  21954. (or np (point-max))))
  21955. (with-current-buffer (find-file-noselect
  21956. (let ((default-directory dir))
  21957. (expand-file-name txt)))
  21958. (unless (derived-mode-p 'org-mode)
  21959. (user-error "Cannot restrict to non-Org-mode file"))
  21960. (org-agenda-set-restriction-lock 'file)))
  21961. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21962. (move-overlay org-speedbar-restriction-lock-overlay
  21963. (point-at-bol) (point-at-eol))
  21964. (setq current-prefix-arg nil)
  21965. (org-agenda-maybe-redo)))
  21966. (defvar speedbar-file-key-map)
  21967. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21968. (eval-after-load "speedbar"
  21969. '(progn
  21970. (speedbar-add-supported-extension ".org")
  21971. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21972. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21973. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21974. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21975. (add-hook 'speedbar-visiting-tag-hook
  21976. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21977. ;;; Fixes and Hacks for problems with other packages
  21978. ;; Make flyspell not check words in links, to not mess up our keymap
  21979. (defvar org-element-affiliated-keywords) ; From org-element.el
  21980. (defvar org-element-block-name-alist) ; From org-element.el
  21981. (defun org-mode-flyspell-verify ()
  21982. "Don't let flyspell put overlays at active buttons, or on
  21983. {todo,all-time,additional-option-like}-keywords."
  21984. (require 'org-element) ; For `org-element-affiliated-keywords'
  21985. (let ((pos (max (1- (point)) (point-min)))
  21986. (word (thing-at-point 'word)))
  21987. (and (not (get-text-property pos 'keymap))
  21988. (not (get-text-property pos 'org-no-flyspell))
  21989. (not (member word org-todo-keywords-1))
  21990. (not (member word org-all-time-keywords))
  21991. (not (member word org-options-keywords))
  21992. (not (member word (mapcar 'car org-startup-options)))
  21993. (not (member-ignore-case word org-element-affiliated-keywords))
  21994. (not (member-ignore-case word (org-get-export-keywords)))
  21995. (not (member-ignore-case
  21996. word (mapcar 'car org-element-block-name-alist)))
  21997. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21998. (not (org-in-src-block-p)))))
  21999. (defun org-remove-flyspell-overlays-in (beg end)
  22000. "Remove flyspell overlays in region."
  22001. (and (org-bound-and-true-p flyspell-mode)
  22002. (fboundp 'flyspell-delete-region-overlays)
  22003. (flyspell-delete-region-overlays beg end))
  22004. (add-text-properties beg end '(org-no-flyspell t)))
  22005. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22006. (eval-after-load "bookmark"
  22007. '(if (boundp 'bookmark-after-jump-hook)
  22008. ;; We can use the hook
  22009. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22010. ;; Hook not available, use advice
  22011. (defadvice bookmark-jump (after org-make-visible activate)
  22012. "Make the position visible."
  22013. (org-bookmark-jump-unhide))))
  22014. ;; Make sure saveplace shows the location if it was hidden
  22015. (eval-after-load "saveplace"
  22016. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22017. "Make the position visible."
  22018. (org-bookmark-jump-unhide)))
  22019. ;; Make sure ecb shows the location if it was hidden
  22020. (eval-after-load "ecb"
  22021. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22022. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22023. (if (derived-mode-p 'org-mode)
  22024. (org-show-context))))
  22025. (defun org-bookmark-jump-unhide ()
  22026. "Unhide the current position, to show the bookmark location."
  22027. (and (derived-mode-p 'org-mode)
  22028. (or (outline-invisible-p)
  22029. (save-excursion (goto-char (max (point-min) (1- (point))))
  22030. (outline-invisible-p)))
  22031. (org-show-context 'bookmark-jump)))
  22032. ;; Make session.el ignore our circular variable
  22033. (defvar session-globals-exclude)
  22034. (eval-after-load "session"
  22035. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22036. ;;;; Finish up
  22037. (provide 'org)
  22038. (run-hooks 'org-load-hook)
  22039. ;;; org.el ends here