org.el 908 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (setq exported-file
  183. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  184. (message "%s %s"
  185. (if compile
  186. (progn (byte-compile-file exported-file 'load)
  187. "Compiled and loaded")
  188. (progn (load-file exported-file) "Loaded"))
  189. exported-file)))
  190. (defcustom org-babel-load-languages '((emacs-lisp . t))
  191. "Languages which can be evaluated in Org-mode buffers.
  192. This list can be used to load support for any of the languages
  193. below, note that each language will depend on a different set of
  194. system executables and/or Emacs modes. When a language is
  195. \"loaded\", then code blocks in that language can be evaluated
  196. with `org-babel-execute-src-block' bound by default to C-c
  197. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  198. be set to remove code block evaluation from the C-c C-c
  199. keybinding. By default only Emacs Lisp (which has no
  200. requirements) is loaded."
  201. :group 'org-babel
  202. :set 'org-babel-do-load-languages
  203. :version "24.1"
  204. :type '(alist :tag "Babel Languages"
  205. :key-type
  206. (choice
  207. (const :tag "Awk" awk)
  208. (const :tag "C" C)
  209. (const :tag "R" R)
  210. (const :tag "Asymptote" asymptote)
  211. (const :tag "Calc" calc)
  212. (const :tag "Clojure" clojure)
  213. (const :tag "CSS" css)
  214. (const :tag "Ditaa" ditaa)
  215. (const :tag "Dot" dot)
  216. (const :tag "Emacs Lisp" emacs-lisp)
  217. (const :tag "Fortran" fortran)
  218. (const :tag "Gnuplot" gnuplot)
  219. (const :tag "Haskell" haskell)
  220. (const :tag "IO" io)
  221. (const :tag "Java" java)
  222. (const :tag "Javascript" js)
  223. (const :tag "LaTeX" latex)
  224. (const :tag "Ledger" ledger)
  225. (const :tag "Lilypond" lilypond)
  226. (const :tag "Lisp" lisp)
  227. (const :tag "Makefile" makefile)
  228. (const :tag "Maxima" maxima)
  229. (const :tag "Matlab" matlab)
  230. (const :tag "Mscgen" mscgen)
  231. (const :tag "Ocaml" ocaml)
  232. (const :tag "Octave" octave)
  233. (const :tag "Org" org)
  234. (const :tag "Perl" perl)
  235. (const :tag "Pico Lisp" picolisp)
  236. (const :tag "PlantUML" plantuml)
  237. (const :tag "Python" python)
  238. (const :tag "Ruby" ruby)
  239. (const :tag "Sass" sass)
  240. (const :tag "Scala" scala)
  241. (const :tag "Scheme" scheme)
  242. (const :tag "Screen" screen)
  243. (const :tag "Shell Script" sh)
  244. (const :tag "Shen" shen)
  245. (const :tag "Sql" sql)
  246. (const :tag "Sqlite" sqlite))
  247. :value-type (boolean :tag "Activate" :value t)))
  248. ;;;; Customization variables
  249. (defcustom org-clone-delete-id nil
  250. "Remove ID property of clones of a subtree.
  251. When non-nil, clones of a subtree don't inherit the ID property.
  252. Otherwise they inherit the ID property with a new unique
  253. identifier."
  254. :type 'boolean
  255. :version "24.1"
  256. :group 'org-id)
  257. ;;; Version
  258. (org-check-version)
  259. ;;;###autoload
  260. (defun org-version (&optional here full message)
  261. "Show the org-mode version in the echo area.
  262. With prefix argument HERE, insert it at point.
  263. When FULL is non-nil, use a verbose version string.
  264. When MESSAGE is non-nil, display a message with the version."
  265. (interactive "P")
  266. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  267. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  268. (load-suffixes (list ".el"))
  269. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  270. (org-trash (or
  271. (and (fboundp 'org-release) (fboundp 'org-git-version))
  272. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  273. (load-suffixes save-load-suffixes)
  274. (org-version (org-release))
  275. (git-version (org-git-version))
  276. (version (format "Org-mode version %s (%s @ %s)"
  277. org-version
  278. git-version
  279. (if org-install-dir
  280. (if (string= org-dir org-install-dir)
  281. org-install-dir
  282. (concat "mixed installation! " org-install-dir " and " org-dir))
  283. "org-loaddefs.el can not be found!")))
  284. (version1 (if full version org-version)))
  285. (if (org-called-interactively-p 'interactive)
  286. (if here
  287. (insert version)
  288. (message version))
  289. (if message (message version1))
  290. version1)))
  291. (defconst org-version (org-version))
  292. ;;; Compatibility constants
  293. ;;; The custom variables
  294. (defgroup org nil
  295. "Outline-based notes management and organizer."
  296. :tag "Org"
  297. :group 'outlines
  298. :group 'calendar)
  299. (defcustom org-mode-hook nil
  300. "Mode hook for Org-mode, run after the mode was turned on."
  301. :group 'org
  302. :type 'hook)
  303. (defcustom org-load-hook nil
  304. "Hook that is run after org.el has been loaded."
  305. :group 'org
  306. :type 'hook)
  307. (defcustom org-log-buffer-setup-hook nil
  308. "Hook that is run after an Org log buffer is created."
  309. :group 'org
  310. :version "24.1"
  311. :type 'hook)
  312. (defvar org-modules) ; defined below
  313. (defvar org-modules-loaded nil
  314. "Have the modules been loaded already?")
  315. (defun org-load-modules-maybe (&optional force)
  316. "Load all extensions listed in `org-modules'."
  317. (when (or force (not org-modules-loaded))
  318. (mapc (lambda (ext)
  319. (condition-case nil (require ext)
  320. (error (message "Problems while trying to load feature `%s'" ext))))
  321. org-modules)
  322. (setq org-modules-loaded t)))
  323. (defun org-set-modules (var value)
  324. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  325. (set var value)
  326. (when (featurep 'org)
  327. (org-load-modules-maybe 'force)))
  328. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  329. "Modules that should always be loaded together with org.el.
  330. If a description starts with <C>, the file is not part of Emacs
  331. and loading it will require that you have downloaded and properly
  332. installed the Org mode distribution.
  333. You can also use this system to load external packages (i.e. neither Org
  334. core modules, nor modules from the CONTRIB directory). Just add symbols
  335. to the end of the list. If the package is called org-xyz.el, then you need
  336. to add the symbol `xyz', and the package must have a call to:
  337. \(provide 'org-xyz)
  338. For export specific modules, see also `org-export-backends'."
  339. :group 'org
  340. :set 'org-set-modules
  341. :version "24.4"
  342. :package-version '(Org . "8.0")
  343. :type
  344. '(set :greedy t
  345. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  346. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  347. (const :tag " crypt: Encryption of subtrees" org-crypt)
  348. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  349. (const :tag " docview: Links to doc-view buffers" org-docview)
  350. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  351. (const :tag " habit: Track your consistency with habits" org-habit)
  352. (const :tag " id: Global IDs for identifying entries" org-id)
  353. (const :tag " info: Links to Info nodes" org-info)
  354. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  355. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  356. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  357. (const :tag " mouse: Additional mouse support" org-mouse)
  358. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  359. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  360. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  361. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  362. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  363. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  364. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  365. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  366. (const :tag "C collector: Collect properties into tables" org-collector)
  367. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  368. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  369. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  370. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  371. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  372. (const :tag "C eval: Include command output as text" org-eval)
  373. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  374. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  375. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  376. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  377. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  378. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  379. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  380. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  381. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export--registered-backends) ; From ox.el.
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (declare-function org-export-backend-name "ox" (backend))
  402. (defcustom org-export-backends '(ascii html icalendar latex)
  403. "List of export back-ends that should be always available.
  404. If a description starts with <C>, the file is not part of Emacs
  405. and loading it will require that you have downloaded and properly
  406. installed the Org mode distribution.
  407. Unlike to `org-modules', libraries in this list will not be
  408. loaded along with Org, but only once the export framework is
  409. needed.
  410. This variable needs to be set before org.el is loaded. If you
  411. need to make a change while Emacs is running, use the customize
  412. interface or run the following code, where VAL stands for the new
  413. value of the variable, after updating it:
  414. \(progn
  415. \(setq org-export--registered-backends
  416. \(org-remove-if-not
  417. \(lambda (backend)
  418. \(let ((name (org-export-backend-name backend)))
  419. \(or (memq name val)
  420. \(catch 'parentp
  421. \(dolist (b val)
  422. \(and (org-export-derived-backend-p b name)
  423. \(throw 'parentp t)))))))
  424. org-export--registered-backends))
  425. \(let ((new-list (mapcar 'org-export-backend-name
  426. org-export--registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default 'org-export-backends new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export--registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (let ((name (org-export-backend-name backend)))
  450. (or (memq name val)
  451. (catch 'parentp
  452. (dolist (b val)
  453. (and (org-export-derived-backend-p b name)
  454. (throw 'parentp t)))))))
  455. org-export--registered-backends))
  456. ;; Now build NEW-LIST of both new back-ends and required
  457. ;; parents.
  458. (let ((new-list (mapcar 'org-export-backend-name
  459. org-export--registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. If you want to control how Org exports those characters, see
  592. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  593. used to be an alias for `org-export-with-sub-superscripts' in
  594. Org <8.0, it is not anymore.
  595. When this option is turned on, you can use TeX-like syntax for
  596. sub- and superscripts within the buffer. Several characters after
  597. \"_\" or \"^\" will be considered as a single item - so grouping
  598. with {} is normally not needed. For example, the following things
  599. will be parsed as single sub- or superscripts:
  600. 10^24 or 10^tau several digits will be considered 1 item.
  601. 10^-12 or 10^-tau a leading sign with digits or a word
  602. x^2-y^3 will be read as x^2 - y^3, because items are
  603. terminated by almost any nonword/nondigit char.
  604. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  605. Still, ambiguity is possible. So when in doubt, use {} to enclose
  606. the sub/superscript. If you set this variable to the symbol `{}',
  607. the braces are *required* in order to trigger interpretations as
  608. sub/superscript. This can be helpful in documents that need \"_\"
  609. frequently in plain text."
  610. :group 'org-startup
  611. :version "24.4"
  612. :package-version '(Org . "8.0")
  613. :type '(choice
  614. (const :tag "Always interpret" t)
  615. (const :tag "Only with braces" {})
  616. (const :tag "Never interpret" nil)))
  617. (defcustom org-startup-with-beamer-mode nil
  618. "Non-nil means turn on `org-beamer-mode' on startup.
  619. This can also be configured on a per-file basis by adding one of
  620. the following lines anywhere in the buffer:
  621. #+STARTUP: beamer"
  622. :group 'org-startup
  623. :version "24.1"
  624. :type 'boolean)
  625. (defcustom org-startup-align-all-tables nil
  626. "Non-nil means align all tables when visiting a file.
  627. This is useful when the column width in tables is forced with <N> cookies
  628. in table fields. Such tables will look correct only after the first re-align.
  629. This can also be configured on a per-file basis by adding one of
  630. the following lines anywhere in the buffer:
  631. #+STARTUP: align
  632. #+STARTUP: noalign"
  633. :group 'org-startup
  634. :type 'boolean)
  635. (defcustom org-startup-with-inline-images nil
  636. "Non-nil means show inline images when loading a new Org file.
  637. This can also be configured on a per-file basis by adding one of
  638. the following lines anywhere in the buffer:
  639. #+STARTUP: inlineimages
  640. #+STARTUP: noinlineimages"
  641. :group 'org-startup
  642. :version "24.1"
  643. :type 'boolean)
  644. (defcustom org-startup-with-latex-preview nil
  645. "Non-nil means preview LaTeX fragments when loading a new Org file.
  646. This can also be configured on a per-file basis by adding one of
  647. the following lines anywhere in the buffer:
  648. #+STARTUP: latexpreview
  649. #+STARTUP: nolatexpreview"
  650. :group 'org-startup
  651. :version "24.4"
  652. :package-version '(Org . "8.0")
  653. :type 'boolean)
  654. (defcustom org-insert-mode-line-in-empty-file nil
  655. "Non-nil means insert the first line setting Org-mode in empty files.
  656. When the function `org-mode' is called interactively in an empty file, this
  657. normally means that the file name does not automatically trigger Org-mode.
  658. To ensure that the file will always be in Org-mode in the future, a
  659. line enforcing Org-mode will be inserted into the buffer, if this option
  660. has been set."
  661. :group 'org-startup
  662. :type 'boolean)
  663. (defcustom org-replace-disputed-keys nil
  664. "Non-nil means use alternative key bindings for some keys.
  665. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  666. These keys are also used by other packages like shift-selection-mode'
  667. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  668. If you want to use Org-mode together with one of these other modes,
  669. or more generally if you would like to move some Org-mode commands to
  670. other keys, set this variable and configure the keys with the variable
  671. `org-disputed-keys'.
  672. This option is only relevant at load-time of Org-mode, and must be set
  673. *before* org.el is loaded. Changing it requires a restart of Emacs to
  674. become effective."
  675. :group 'org-startup
  676. :type 'boolean)
  677. (defcustom org-use-extra-keys nil
  678. "Non-nil means use extra key sequence definitions for certain commands.
  679. This happens automatically if you run XEmacs or if `window-system'
  680. is nil. This variable lets you do the same manually. You must
  681. set it before loading org.
  682. Example: on Carbon Emacs 22 running graphically, with an external
  683. keyboard on a Powerbook, the default way of setting M-left might
  684. not work for either Alt or ESC. Setting this variable will make
  685. it work for ESC."
  686. :group 'org-startup
  687. :type 'boolean)
  688. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  689. (defcustom org-disputed-keys
  690. '(([(shift up)] . [(meta p)])
  691. ([(shift down)] . [(meta n)])
  692. ([(shift left)] . [(meta -)])
  693. ([(shift right)] . [(meta +)])
  694. ([(control shift right)] . [(meta shift +)])
  695. ([(control shift left)] . [(meta shift -)]))
  696. "Keys for which Org-mode and other modes compete.
  697. This is an alist, cars are the default keys, second element specifies
  698. the alternative to use when `org-replace-disputed-keys' is t.
  699. Keys can be specified in any syntax supported by `define-key'.
  700. The value of this option takes effect only at Org-mode's startup,
  701. therefore you'll have to restart Emacs to apply it after changing."
  702. :group 'org-startup
  703. :type 'alist)
  704. (defun org-key (key)
  705. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  706. Or return the original if not disputed.
  707. Also apply the translations defined in `org-xemacs-key-equivalents'."
  708. (when org-replace-disputed-keys
  709. (let* ((nkey (key-description key))
  710. (x (org-find-if (lambda (x)
  711. (equal (key-description (car x)) nkey))
  712. org-disputed-keys)))
  713. (setq key (if x (cdr x) key))))
  714. (when (featurep 'xemacs)
  715. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  716. key)
  717. (defun org-find-if (predicate seq)
  718. (catch 'exit
  719. (while seq
  720. (if (funcall predicate (car seq))
  721. (throw 'exit (car seq))
  722. (pop seq)))))
  723. (defun org-defkey (keymap key def)
  724. "Define a key, possibly translated, as returned by `org-key'."
  725. (define-key keymap (org-key key) def))
  726. (defcustom org-ellipsis nil
  727. "The ellipsis to use in the Org-mode outline.
  728. When nil, just use the standard three dots.
  729. When a string, use that string instead.
  730. When a face, use the standard 3 dots, but with the specified face.
  731. The change affects only Org-mode (which will then use its own display table).
  732. Changing this requires executing `M-x org-mode RET' in a buffer to become
  733. effective."
  734. :group 'org-startup
  735. :type '(choice (const :tag "Default" nil)
  736. (face :tag "Face" :value org-warning)
  737. (string :tag "String" :value "...#")))
  738. (defvar org-display-table nil
  739. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  740. (defgroup org-keywords nil
  741. "Keywords in Org-mode."
  742. :tag "Org Keywords"
  743. :group 'org)
  744. (defcustom org-deadline-string "DEADLINE:"
  745. "String to mark deadline entries.
  746. A deadline is this string, followed by a time stamp. Should be a word,
  747. terminated by a colon. You can insert a schedule keyword and
  748. a timestamp with \\[org-deadline].
  749. Changes become only effective after restarting Emacs."
  750. :group 'org-keywords
  751. :type 'string)
  752. (defcustom org-scheduled-string "SCHEDULED:"
  753. "String to mark scheduled TODO entries.
  754. A schedule is this string, followed by a time stamp. Should be a word,
  755. terminated by a colon. You can insert a schedule keyword and
  756. a timestamp with \\[org-schedule].
  757. Changes become only effective after restarting Emacs."
  758. :group 'org-keywords
  759. :type 'string)
  760. (defcustom org-closed-string "CLOSED:"
  761. "String used as the prefix for timestamps logging closing a TODO entry."
  762. :group 'org-keywords
  763. :type 'string)
  764. (defcustom org-clock-string "CLOCK:"
  765. "String used as prefix for timestamps clocking work hours on an item."
  766. :group 'org-keywords
  767. :type 'string)
  768. (defcustom org-closed-keep-when-no-todo nil
  769. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  770. :group 'org-todo
  771. :group 'org-keywords
  772. :version "24.4"
  773. :package-version '(Org . "8.0")
  774. :type 'boolean)
  775. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  776. org-scheduled-string "\\|"
  777. org-deadline-string "\\|"
  778. org-closed-string "\\|"
  779. org-clock-string "\\)")
  780. "Matches a line with planning or clock info.")
  781. (defcustom org-comment-string "COMMENT"
  782. "Entries starting with this keyword will never be exported.
  783. An entry can be toggled between COMMENT and normal with
  784. \\[org-toggle-comment].
  785. Changes become only effective after restarting Emacs."
  786. :group 'org-keywords
  787. :type 'string)
  788. (defcustom org-quote-string "QUOTE"
  789. "Entries starting with this keyword will be exported in fixed-width font.
  790. Quoting applies only to the text in the entry following the headline, and does
  791. not extend beyond the next headline, even if that is lower level.
  792. An entry can be toggled between QUOTE and normal with
  793. \\[org-toggle-fixed-width-section]."
  794. :group 'org-keywords
  795. :type 'string)
  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 bookmarks 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-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  968. "Names of drawers. Drawers are not opened by cycling on the headline above.
  969. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  970. this:
  971. :DRAWERNAME:
  972. .....
  973. :END:
  974. The drawer \"PROPERTIES\" is special for capturing properties through
  975. the property API.
  976. Drawers can be defined on the per-file basis with a line like:
  977. #+DRAWERS: HIDDEN STATE PROPERTIES"
  978. :group 'org-structure
  979. :group 'org-cycle
  980. :type '(repeat (string :tag "Drawer Name")))
  981. (defcustom org-hide-block-startup nil
  982. "Non-nil means entering Org-mode will fold all blocks.
  983. This can also be set in on a per-file basis with
  984. #+STARTUP: hideblocks
  985. #+STARTUP: showblocks"
  986. :group 'org-startup
  987. :group 'org-cycle
  988. :type 'boolean)
  989. (defcustom org-cycle-global-at-bob nil
  990. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  991. This makes it possible to do global cycling without having to use S-TAB or
  992. \\[universal-argument] TAB. For this special case to work, the first line
  993. of the buffer must not be a headline -- it may be empty or some other text.
  994. When used in this way, `org-cycle-hook' is disabled temporarily to make
  995. sure the cursor stays at the beginning of the buffer. When this option is
  996. nil, don't do anything special at the beginning of the buffer."
  997. :group 'org-cycle
  998. :type 'boolean)
  999. (defcustom org-cycle-level-after-item/entry-creation t
  1000. "Non-nil means cycle entry level or item indentation in new empty entries.
  1001. When the cursor is at the end of an empty headline, i.e., with only stars
  1002. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1003. and then all possible ancestor states, before returning to the original state.
  1004. This makes data entry extremely fast: M-RET to create a new headline,
  1005. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1006. When the cursor is at the end of an empty plain list item, one TAB will
  1007. make it a subitem, two or more tabs will back up to make this an item
  1008. higher up in the item hierarchy."
  1009. :group 'org-cycle
  1010. :type 'boolean)
  1011. (defcustom org-cycle-emulate-tab t
  1012. "Where should `org-cycle' emulate TAB.
  1013. nil Never
  1014. white Only in completely white lines
  1015. whitestart Only at the beginning of lines, before the first non-white char
  1016. t Everywhere except in headlines
  1017. exc-hl-bol Everywhere except at the start of a headline
  1018. If TAB is used in a place where it does not emulate TAB, the current subtree
  1019. visibility is cycled."
  1020. :group 'org-cycle
  1021. :type '(choice (const :tag "Never" nil)
  1022. (const :tag "Only in completely white lines" white)
  1023. (const :tag "Before first char in a line" whitestart)
  1024. (const :tag "Everywhere except in headlines" t)
  1025. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1026. (defcustom org-cycle-separator-lines 2
  1027. "Number of empty lines needed to keep an empty line between collapsed trees.
  1028. If you leave an empty line between the end of a subtree and the following
  1029. headline, this empty line is hidden when the subtree is folded.
  1030. Org-mode will leave (exactly) one empty line visible if the number of
  1031. empty lines is equal or larger to the number given in this variable.
  1032. So the default 2 means at least 2 empty lines after the end of a subtree
  1033. are needed to produce free space between a collapsed subtree and the
  1034. following headline.
  1035. If the number is negative, and the number of empty lines is at least -N,
  1036. all empty lines are shown.
  1037. Special case: when 0, never leave empty lines in collapsed view."
  1038. :group 'org-cycle
  1039. :type 'integer)
  1040. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1041. (defcustom org-pre-cycle-hook nil
  1042. "Hook that is run before visibility cycling is happening.
  1043. The function(s) in this hook must accept a single argument which indicates
  1044. the new state that will be set right after running this hook. The
  1045. argument is a symbol. Before a global state change, it can have the values
  1046. `overview', `content', or `all'. Before a local state change, it can have
  1047. the values `folded', `children', or `subtree'."
  1048. :group 'org-cycle
  1049. :type 'hook)
  1050. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1051. org-cycle-hide-drawers
  1052. org-cycle-hide-inline-tasks
  1053. org-cycle-show-empty-lines
  1054. org-optimize-window-after-visibility-change)
  1055. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1056. The function(s) in this hook must accept a single argument which indicates
  1057. the new state that was set by the most recent `org-cycle' command. The
  1058. argument is a symbol. After a global state change, it can have the values
  1059. `overview', `contents', or `all'. After a local state change, it can have
  1060. the values `folded', `children', or `subtree'."
  1061. :group 'org-cycle
  1062. :type 'hook)
  1063. (defgroup org-edit-structure nil
  1064. "Options concerning structure editing in Org-mode."
  1065. :tag "Org Edit Structure"
  1066. :group 'org-structure)
  1067. (defcustom org-odd-levels-only nil
  1068. "Non-nil means skip even levels and only use odd levels for the outline.
  1069. This has the effect that two stars are being added/taken away in
  1070. promotion/demotion commands. It also influences how levels are
  1071. handled by the exporters.
  1072. Changing it requires restart of `font-lock-mode' to become effective
  1073. for fontification also in regions already fontified.
  1074. You may also set this on a per-file basis by adding one of the following
  1075. lines to the buffer:
  1076. #+STARTUP: odd
  1077. #+STARTUP: oddeven"
  1078. :group 'org-edit-structure
  1079. :group 'org-appearance
  1080. :type 'boolean)
  1081. (defcustom org-adapt-indentation t
  1082. "Non-nil means adapt indentation to outline node level.
  1083. When this variable is set, Org assumes that you write outlines by
  1084. indenting text in each node to align with the headline (after the stars).
  1085. The following issues are influenced by this variable:
  1086. - When this is set and the *entire* text in an entry is indented, the
  1087. indentation is increased by one space in a demotion command, and
  1088. decreased by one in a promotion command. If any line in the entry
  1089. body starts with text at column 0, indentation is not changed at all.
  1090. - Property drawers and planning information is inserted indented when
  1091. this variable s set. When nil, they will not be indented.
  1092. - TAB indents a line relative to context. The lines below a headline
  1093. will be indented when this variable is set.
  1094. Note that this is all about true indentation, by adding and removing
  1095. space characters. See also `org-indent.el' which does level-dependent
  1096. indentation in a virtual way, i.e. at display time in Emacs."
  1097. :group 'org-edit-structure
  1098. :type 'boolean)
  1099. (defcustom org-special-ctrl-a/e nil
  1100. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1101. When t, `C-a' will bring back the cursor to the beginning of the
  1102. headline text, i.e. after the stars and after a possible TODO
  1103. keyword. In an item, this will be the position after bullet and
  1104. check-box, if any. When the cursor is already at that position,
  1105. another `C-a' will bring it to the beginning of the line.
  1106. `C-e' will jump to the end of the headline, ignoring the presence
  1107. of tags in the headline. A second `C-e' will then jump to the
  1108. true end of the line, after any tags. This also means that, when
  1109. this variable is non-nil, `C-e' also will never jump beyond the
  1110. end of the heading of a folded section, i.e. not after the
  1111. ellipses.
  1112. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1113. normally, going to the true line boundary first. Only a directly
  1114. following, identical keypress will bring the cursor to the
  1115. special positions.
  1116. This may also be a cons cell where the behavior for `C-a' and
  1117. `C-e' is set separately."
  1118. :group 'org-edit-structure
  1119. :type '(choice
  1120. (const :tag "off" nil)
  1121. (const :tag "on: after stars/bullet and before tags first" t)
  1122. (const :tag "reversed: true line boundary first" reversed)
  1123. (cons :tag "Set C-a and C-e separately"
  1124. (choice :tag "Special C-a"
  1125. (const :tag "off" nil)
  1126. (const :tag "on: after stars/bullet first" t)
  1127. (const :tag "reversed: before stars/bullet first" reversed))
  1128. (choice :tag "Special C-e"
  1129. (const :tag "off" nil)
  1130. (const :tag "on: before tags first" t)
  1131. (const :tag "reversed: after tags first" reversed)))))
  1132. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1133. (defcustom org-special-ctrl-k nil
  1134. "Non-nil means `C-k' will behave specially in headlines.
  1135. When nil, `C-k' will call the default `kill-line' command.
  1136. When t, the following will happen while the cursor is in the headline:
  1137. - When the cursor is at the beginning of a headline, kill the entire
  1138. line and possible the folded subtree below the line.
  1139. - When in the middle of the headline text, kill the headline up to the tags.
  1140. - When after the headline text, kill the tags."
  1141. :group 'org-edit-structure
  1142. :type 'boolean)
  1143. (defcustom org-ctrl-k-protect-subtree nil
  1144. "Non-nil means, do not delete a hidden subtree with C-k.
  1145. When set to the symbol `error', simply throw an error when C-k is
  1146. used to kill (part-of) a headline that has hidden text behind it.
  1147. Any other non-nil value will result in a query to the user, if it is
  1148. OK to kill that hidden subtree. When nil, kill without remorse."
  1149. :group 'org-edit-structure
  1150. :version "24.1"
  1151. :type '(choice
  1152. (const :tag "Do not protect hidden subtrees" nil)
  1153. (const :tag "Protect hidden subtrees with a security query" t)
  1154. (const :tag "Never kill a hidden subtree with C-k" error)))
  1155. (defcustom org-special-ctrl-o t
  1156. "Non-nil means, make `C-o' insert a row in tables."
  1157. :group 'org-edit-structure
  1158. :type 'boolean)
  1159. (defcustom org-catch-invisible-edits nil
  1160. "Check if in invisible region before inserting or deleting a character.
  1161. Valid values are:
  1162. nil Do not check, so just do invisible edits.
  1163. error Throw an error and do nothing.
  1164. show Make point visible, and do the requested edit.
  1165. show-and-error Make point visible, then throw an error and abort the edit.
  1166. smart Make point visible, and do insertion/deletion if it is
  1167. adjacent to visible text and the change feels predictable.
  1168. Never delete a previously invisible character or add in the
  1169. middle or right after an invisible region. Basically, this
  1170. allows insertion and backward-delete right before ellipses.
  1171. FIXME: maybe in this case we should not even show?"
  1172. :group 'org-edit-structure
  1173. :version "24.1"
  1174. :type '(choice
  1175. (const :tag "Do not check" nil)
  1176. (const :tag "Throw error when trying to edit" error)
  1177. (const :tag "Unhide, but do not do the edit" show-and-error)
  1178. (const :tag "Show invisible part and do the edit" show)
  1179. (const :tag "Be smart and do the right thing" smart)))
  1180. (defcustom org-yank-folded-subtrees t
  1181. "Non-nil means when yanking subtrees, fold them.
  1182. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1183. it starts with a heading and all other headings in it are either children
  1184. or siblings, then fold all the subtrees. However, do this only if no
  1185. text after the yank would be swallowed into a folded tree by this action."
  1186. :group 'org-edit-structure
  1187. :type 'boolean)
  1188. (defcustom org-yank-adjusted-subtrees nil
  1189. "Non-nil means when yanking subtrees, adjust the level.
  1190. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1191. this function for details."
  1192. :group 'org-edit-structure
  1193. :type 'boolean)
  1194. (defcustom org-M-RET-may-split-line '((default . t))
  1195. "Non-nil means M-RET will split the line at the cursor position.
  1196. When nil, it will go to the end of the line before making a
  1197. new line.
  1198. You may also set this option in a different way for different
  1199. contexts. Valid contexts are:
  1200. headline when creating a new headline
  1201. item when creating a new item
  1202. table in a table field
  1203. default the value to be used for all contexts not explicitly
  1204. customized"
  1205. :group 'org-structure
  1206. :group 'org-table
  1207. :type '(choice
  1208. (const :tag "Always" t)
  1209. (const :tag "Never" nil)
  1210. (repeat :greedy t :tag "Individual contexts"
  1211. (cons
  1212. (choice :tag "Context"
  1213. (const headline)
  1214. (const item)
  1215. (const table)
  1216. (const default))
  1217. (boolean)))))
  1218. (defcustom org-insert-heading-respect-content nil
  1219. "Non-nil means insert new headings after the current subtree.
  1220. When nil, the new heading is created directly after the current line.
  1221. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1222. this variable on for the duration of the command."
  1223. :group 'org-structure
  1224. :type 'boolean)
  1225. (defcustom org-blank-before-new-entry '((heading . auto)
  1226. (plain-list-item . auto))
  1227. "Should `org-insert-heading' leave a blank line before new heading/item?
  1228. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1229. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1230. which case Org will look at the surrounding headings/items and try to
  1231. make an intelligent decision whether to insert a blank line or not.
  1232. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1233. the setting here is ignored and no empty line is inserted to avoid breaking
  1234. the list structure."
  1235. :group 'org-edit-structure
  1236. :type '(list
  1237. (cons (const heading)
  1238. (choice (const :tag "Never" nil)
  1239. (const :tag "Always" t)
  1240. (const :tag "Auto" auto)))
  1241. (cons (const plain-list-item)
  1242. (choice (const :tag "Never" nil)
  1243. (const :tag "Always" t)
  1244. (const :tag "Auto" auto)))))
  1245. (defcustom org-insert-heading-hook nil
  1246. "Hook being run after inserting a new heading."
  1247. :group 'org-edit-structure
  1248. :type 'hook)
  1249. (defcustom org-enable-fixed-width-editor t
  1250. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1251. This currently only means they are never auto-wrapped.
  1252. When nil, such lines will be treated like ordinary lines.
  1253. See also the QUOTE keyword."
  1254. :group 'org-edit-structure
  1255. :type 'boolean)
  1256. (defcustom org-goto-auto-isearch t
  1257. "Non-nil means typing characters in `org-goto' starts incremental search.
  1258. When nil, you can use these keybindings to navigate the buffer:
  1259. q Quit the org-goto interface
  1260. n Go to the next visible heading
  1261. p Go to the previous visible heading
  1262. f Go one heading forward on same level
  1263. b Go one heading backward on same level
  1264. u Go one heading up"
  1265. :group 'org-edit-structure
  1266. :type 'boolean)
  1267. (defgroup org-sparse-trees nil
  1268. "Options concerning sparse trees in Org-mode."
  1269. :tag "Org Sparse Trees"
  1270. :group 'org-structure)
  1271. (defcustom org-highlight-sparse-tree-matches t
  1272. "Non-nil means highlight all matches that define a sparse tree.
  1273. The highlights will automatically disappear the next time the buffer is
  1274. changed by an edit command."
  1275. :group 'org-sparse-trees
  1276. :type 'boolean)
  1277. (defcustom org-remove-highlights-with-change t
  1278. "Non-nil means any change to the buffer will remove temporary highlights.
  1279. Such highlights are created by `org-occur' and `org-clock-display'.
  1280. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1281. The highlights created by `org-preview-latex-fragment' always need
  1282. `C-c C-c' to be removed."
  1283. :group 'org-sparse-trees
  1284. :group 'org-time
  1285. :type 'boolean)
  1286. (defcustom org-occur-hook '(org-first-headline-recenter)
  1287. "Hook that is run after `org-occur' has constructed a sparse tree.
  1288. This can be used to recenter the window to show as much of the structure
  1289. as possible."
  1290. :group 'org-sparse-trees
  1291. :type 'hook)
  1292. (defgroup org-imenu-and-speedbar nil
  1293. "Options concerning imenu and speedbar in Org-mode."
  1294. :tag "Org Imenu and Speedbar"
  1295. :group 'org-structure)
  1296. (defcustom org-imenu-depth 2
  1297. "The maximum level for Imenu access to Org-mode headlines.
  1298. This also applied for speedbar access."
  1299. :group 'org-imenu-and-speedbar
  1300. :type 'integer)
  1301. (defgroup org-table nil
  1302. "Options concerning tables in Org-mode."
  1303. :tag "Org Table"
  1304. :group 'org)
  1305. (defcustom org-enable-table-editor 'optimized
  1306. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1307. When nil, such lines will be treated like ordinary lines.
  1308. When equal to the symbol `optimized', the table editor will be optimized to
  1309. do the following:
  1310. - Automatic overwrite mode in front of whitespace in table fields.
  1311. This makes the structure of the table stay in tact as long as the edited
  1312. field does not exceed the column width.
  1313. - Minimize the number of realigns. Normally, the table is aligned each time
  1314. TAB or RET are pressed to move to another field. With optimization this
  1315. happens only if changes to a field might have changed the column width.
  1316. Optimization requires replacing the functions `self-insert-command',
  1317. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1318. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1319. very good at guessing when a re-align will be necessary, but you can always
  1320. force one with \\[org-ctrl-c-ctrl-c].
  1321. If you would like to use the optimized version in Org-mode, but the
  1322. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1323. This variable can be used to turn on and off the table editor during a session,
  1324. but in order to toggle optimization, a restart is required.
  1325. See also the variable `org-table-auto-blank-field'."
  1326. :group 'org-table
  1327. :type '(choice
  1328. (const :tag "off" nil)
  1329. (const :tag "on" t)
  1330. (const :tag "on, optimized" optimized)))
  1331. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1332. (version<= emacs-version "24.1"))
  1333. "Non-nil means cluster self-insert commands for undo when possible.
  1334. If this is set, then, like in the Emacs command loop, 20 consecutive
  1335. characters will be undone together.
  1336. This is configurable, because there is some impact on typing performance."
  1337. :group 'org-table
  1338. :type 'boolean)
  1339. (defcustom org-table-tab-recognizes-table.el t
  1340. "Non-nil means TAB will automatically notice a table.el table.
  1341. When it sees such a table, it moves point into it and - if necessary -
  1342. calls `table-recognize-table'."
  1343. :group 'org-table-editing
  1344. :type 'boolean)
  1345. (defgroup org-link nil
  1346. "Options concerning links in Org-mode."
  1347. :tag "Org Link"
  1348. :group 'org)
  1349. (defvar org-link-abbrev-alist-local nil
  1350. "Buffer-local version of `org-link-abbrev-alist', which see.
  1351. The value of this is taken from the #+LINK lines.")
  1352. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1353. (defcustom org-link-abbrev-alist nil
  1354. "Alist of link abbreviations.
  1355. The car of each element is a string, to be replaced at the start of a link.
  1356. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1357. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1358. [[linkkey:tag][description]]
  1359. The 'linkkey' must be a word word, starting with a letter, followed
  1360. by letters, numbers, '-' or '_'.
  1361. If REPLACE is a string, the tag will simply be appended to create the link.
  1362. If the string contains \"%s\", the tag will be inserted there. If the string
  1363. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1364. at that point (see the function `url-hexify-string'). If the string contains
  1365. the specifier \"%(my-function)\", then the custom function `my-function' will
  1366. be invoked: this function takes the tag as its only argument and must return
  1367. a string.
  1368. REPLACE may also be a function that will be called with the tag as the
  1369. only argument to create the link, which should be returned as a string.
  1370. See the manual for examples."
  1371. :group 'org-link
  1372. :type '(repeat
  1373. (cons
  1374. (string :tag "Protocol")
  1375. (choice
  1376. (string :tag "Format")
  1377. (function)))))
  1378. (defcustom org-descriptive-links t
  1379. "Non-nil means Org will display descriptive links.
  1380. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1381. \"Org Website\", hiding the link itself and just displaying its
  1382. description. When set to `nil', Org will display the full links
  1383. literally.
  1384. You can interactively set the value of this variable by calling
  1385. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1386. :group 'org-link
  1387. :type 'boolean)
  1388. (defcustom org-link-file-path-type 'adaptive
  1389. "How the path name in file links should be stored.
  1390. Valid values are:
  1391. relative Relative to the current directory, i.e. the directory of the file
  1392. into which the link is being inserted.
  1393. absolute Absolute path, if possible with ~ for home directory.
  1394. noabbrev Absolute path, no abbreviation of home directory.
  1395. adaptive Use relative path for files in the current directory and sub-
  1396. directories of it. For other files, use an absolute path."
  1397. :group 'org-link
  1398. :type '(choice
  1399. (const relative)
  1400. (const absolute)
  1401. (const noabbrev)
  1402. (const adaptive)))
  1403. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1404. "Types of links that should be activated in Org-mode files.
  1405. This is a list of symbols, each leading to the activation of a certain link
  1406. type. In principle, it does not hurt to turn on most link types - there may
  1407. be a small gain when turning off unused link types. The types are:
  1408. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1409. angle Links in angular brackets that may contain whitespace like
  1410. <bbdb:Carsten Dominik>.
  1411. plain Plain links in normal text, no whitespace, like http://google.com.
  1412. radio Text that is matched by a radio target, see manual for details.
  1413. tag Tag settings in a headline (link to tag search).
  1414. date Time stamps (link to calendar).
  1415. footnote Footnote labels.
  1416. Changing this variable requires a restart of Emacs to become effective."
  1417. :group 'org-link
  1418. :type '(set :greedy t
  1419. (const :tag "Double bracket links" bracket)
  1420. (const :tag "Angular bracket links" angle)
  1421. (const :tag "Plain text links" plain)
  1422. (const :tag "Radio target matches" radio)
  1423. (const :tag "Tags" tag)
  1424. (const :tag "Timestamps" date)
  1425. (const :tag "Footnotes" footnote)))
  1426. (defcustom org-make-link-description-function nil
  1427. "Function to use for generating link descriptions from links.
  1428. When nil, the link location will be used. This function must take
  1429. two parameters: the first one is the link, the second one is the
  1430. description generated by `org-insert-link'. The function should
  1431. return the description to use."
  1432. :group 'org-link
  1433. :type '(choice (const nil) (function)))
  1434. (defgroup org-link-store nil
  1435. "Options concerning storing links in Org-mode."
  1436. :tag "Org Store Link"
  1437. :group 'org-link)
  1438. (defcustom org-url-hexify-p t
  1439. "When non-nil, hexify URL when creating a link."
  1440. :type 'boolean
  1441. :version "24.3"
  1442. :group 'org-link-store)
  1443. (defcustom org-email-link-description-format "Email %c: %.30s"
  1444. "Format of the description part of a link to an email or usenet message.
  1445. The following %-escapes will be replaced by corresponding information:
  1446. %F full \"From\" field
  1447. %f name, taken from \"From\" field, address if no name
  1448. %T full \"To\" field
  1449. %t first name in \"To\" field, address if no name
  1450. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1451. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1452. %s subject
  1453. %d date
  1454. %m message-id.
  1455. You may use normal field width specification between the % and the letter.
  1456. This is for example useful to limit the length of the subject.
  1457. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1458. :group 'org-link-store
  1459. :type 'string)
  1460. (defcustom org-from-is-user-regexp
  1461. (let (r1 r2)
  1462. (when (and user-mail-address (not (string= user-mail-address "")))
  1463. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1464. (when (and user-full-name (not (string= user-full-name "")))
  1465. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1466. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1467. "Regexp matched against the \"From:\" header of an email or usenet message.
  1468. It should match if the message is from the user him/herself."
  1469. :group 'org-link-store
  1470. :type 'regexp)
  1471. (defcustom org-context-in-file-links t
  1472. "Non-nil means file links from `org-store-link' contain context.
  1473. A search string will be added to the file name with :: as separator and
  1474. used to find the context when the link is activated by the command
  1475. `org-open-at-point'. When this option is t, the entire active region
  1476. will be placed in the search string of the file link. If set to a
  1477. positive integer, only the first n lines of context will be stored.
  1478. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1479. negates this setting for the duration of the command."
  1480. :group 'org-link-store
  1481. :type '(choice boolean integer))
  1482. (defcustom org-keep-stored-link-after-insertion nil
  1483. "Non-nil means keep link in list for entire session.
  1484. The command `org-store-link' adds a link pointing to the current
  1485. location to an internal list. These links accumulate during a session.
  1486. The command `org-insert-link' can be used to insert links into any
  1487. Org-mode file (offering completion for all stored links). When this
  1488. option is nil, every link which has been inserted once using \\[org-insert-link]
  1489. will be removed from the list, to make completing the unused links
  1490. more efficient."
  1491. :group 'org-link-store
  1492. :type 'boolean)
  1493. (defgroup org-link-follow nil
  1494. "Options concerning following links in Org-mode."
  1495. :tag "Org Follow Link"
  1496. :group 'org-link)
  1497. (defcustom org-link-translation-function nil
  1498. "Function to translate links with different syntax to Org syntax.
  1499. This can be used to translate links created for example by the Planner
  1500. or emacs-wiki packages to Org syntax.
  1501. The function must accept two parameters, a TYPE containing the link
  1502. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1503. which is everything after the link protocol. It should return a cons
  1504. with possibly modified values of type and path.
  1505. Org contains a function for this, so if you set this variable to
  1506. `org-translate-link-from-planner', you should be able follow many
  1507. links created by planner."
  1508. :group 'org-link-follow
  1509. :type '(choice (const nil) (function)))
  1510. (defcustom org-follow-link-hook nil
  1511. "Hook that is run after a link has been followed."
  1512. :group 'org-link-follow
  1513. :type 'hook)
  1514. (defcustom org-tab-follows-link nil
  1515. "Non-nil means on links TAB will follow the link.
  1516. Needs to be set before org.el is loaded.
  1517. This really should not be used, it does not make sense, and the
  1518. implementation is bad."
  1519. :group 'org-link-follow
  1520. :type 'boolean)
  1521. (defcustom org-return-follows-link nil
  1522. "Non-nil means on links RET will follow the link.
  1523. In tables, the special behavior of RET has precedence."
  1524. :group 'org-link-follow
  1525. :type 'boolean)
  1526. (defcustom org-mouse-1-follows-link
  1527. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1528. "Non-nil means mouse-1 on a link will follow the link.
  1529. A longer mouse click will still set point. Does not work on XEmacs.
  1530. Needs to be set before org.el is loaded."
  1531. :group 'org-link-follow
  1532. :version "24.4"
  1533. :package-version '(Org . "8.3")
  1534. :type '(choice
  1535. (const :tag "A double click follows the link" double)
  1536. (const :tag "Unconditionally follow the link with mouse-1" t)
  1537. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1538. (defcustom org-mark-ring-length 4
  1539. "Number of different positions to be recorded in the ring.
  1540. Changing this requires a restart of Emacs to work correctly."
  1541. :group 'org-link-follow
  1542. :type 'integer)
  1543. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1544. "Non-nil means internal links in Org files must exactly match a headline.
  1545. When nil, the link search tries to match a phrase with all words
  1546. in the search text."
  1547. :group 'org-link-follow
  1548. :version "24.1"
  1549. :type '(choice
  1550. (const :tag "Use fuzzy text search" nil)
  1551. (const :tag "Match only exact headline" t)
  1552. (const :tag "Match exact headline or query to create it"
  1553. query-to-create)))
  1554. (defcustom org-link-frame-setup
  1555. '((vm . vm-visit-folder-other-frame)
  1556. (vm-imap . vm-visit-imap-folder-other-frame)
  1557. (gnus . org-gnus-no-new-news)
  1558. (file . find-file-other-window)
  1559. (wl . wl-other-frame))
  1560. "Setup the frame configuration for following links.
  1561. When following a link with Emacs, it may often be useful to display
  1562. this link in another window or frame. This variable can be used to
  1563. set this up for the different types of links.
  1564. For VM, use any of
  1565. `vm-visit-folder'
  1566. `vm-visit-folder-other-window'
  1567. `vm-visit-folder-other-frame'
  1568. For Gnus, use any of
  1569. `gnus'
  1570. `gnus-other-frame'
  1571. `org-gnus-no-new-news'
  1572. For FILE, use any of
  1573. `find-file'
  1574. `find-file-other-window'
  1575. `find-file-other-frame'
  1576. For Wanderlust use any of
  1577. `wl'
  1578. `wl-other-frame'
  1579. For the calendar, use the variable `calendar-setup'.
  1580. For BBDB, it is currently only possible to display the matches in
  1581. another window."
  1582. :group 'org-link-follow
  1583. :type '(list
  1584. (cons (const vm)
  1585. (choice
  1586. (const vm-visit-folder)
  1587. (const vm-visit-folder-other-window)
  1588. (const vm-visit-folder-other-frame)))
  1589. (cons (const vm-imap)
  1590. (choice
  1591. (const vm-visit-imap-folder)
  1592. (const vm-visit-imap-folder-other-window)
  1593. (const vm-visit-imap-folder-other-frame)))
  1594. (cons (const gnus)
  1595. (choice
  1596. (const gnus)
  1597. (const gnus-other-frame)
  1598. (const org-gnus-no-new-news)))
  1599. (cons (const file)
  1600. (choice
  1601. (const find-file)
  1602. (const find-file-other-window)
  1603. (const find-file-other-frame)))
  1604. (cons (const wl)
  1605. (choice
  1606. (const wl)
  1607. (const wl-other-frame)))))
  1608. (defcustom org-display-internal-link-with-indirect-buffer nil
  1609. "Non-nil means use indirect buffer to display infile links.
  1610. Activating internal links (from one location in a file to another location
  1611. in the same file) normally just jumps to the location. When the link is
  1612. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1613. is displayed in
  1614. another window. When this option is set, the other window actually displays
  1615. an indirect buffer clone of the current buffer, to avoid any visibility
  1616. changes to the current buffer."
  1617. :group 'org-link-follow
  1618. :type 'boolean)
  1619. (defcustom org-open-non-existing-files nil
  1620. "Non-nil means `org-open-file' will open non-existing files.
  1621. When nil, an error will be generated.
  1622. This variable applies only to external applications because they
  1623. might choke on non-existing files. If the link is to a file that
  1624. will be opened in Emacs, the variable is ignored."
  1625. :group 'org-link-follow
  1626. :type 'boolean)
  1627. (defcustom org-open-directory-means-index-dot-org nil
  1628. "Non-nil means a link to a directory really means to index.org.
  1629. When nil, following a directory link will run dired or open a finder/explorer
  1630. window on that directory."
  1631. :group 'org-link-follow
  1632. :type 'boolean)
  1633. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1634. "Function and arguments to call for following mailto links.
  1635. This is a list with the first element being a Lisp function, and the
  1636. remaining elements being arguments to the function. In string arguments,
  1637. %a will be replaced by the address, and %s will be replaced by the subject
  1638. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1639. :group 'org-link-follow
  1640. :type '(choice
  1641. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1642. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1643. (const :tag "message-mail" (message-mail "%a" "%s"))
  1644. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1645. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1646. "Non-nil means ask for confirmation before executing shell links.
  1647. Shell links can be dangerous: just think about a link
  1648. [[shell:rm -rf ~/*][Google Search]]
  1649. This link would show up in your Org-mode document as \"Google Search\",
  1650. but really it would remove your entire home directory.
  1651. Therefore we advise against setting this variable to nil.
  1652. Just change it to `y-or-n-p' if you want to confirm with a
  1653. single keystroke rather than having to type \"yes\"."
  1654. :group 'org-link-follow
  1655. :type '(choice
  1656. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1657. (const :tag "with y-or-n (faster)" y-or-n-p)
  1658. (const :tag "no confirmation (dangerous)" nil)))
  1659. (put 'org-confirm-shell-link-function
  1660. 'safe-local-variable
  1661. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1662. (defcustom org-confirm-shell-link-not-regexp ""
  1663. "A regexp to skip confirmation for shell links."
  1664. :group 'org-link-follow
  1665. :version "24.1"
  1666. :type 'regexp)
  1667. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1668. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1669. Elisp links can be dangerous: just think about a link
  1670. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1671. This link would show up in your Org-mode document as \"Google Search\",
  1672. but really it would remove your entire home directory.
  1673. Therefore we advise against setting this variable to nil.
  1674. Just change it to `y-or-n-p' if you want to confirm with a
  1675. single keystroke rather than having to type \"yes\"."
  1676. :group 'org-link-follow
  1677. :type '(choice
  1678. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1679. (const :tag "with y-or-n (faster)" y-or-n-p)
  1680. (const :tag "no confirmation (dangerous)" nil)))
  1681. (put 'org-confirm-shell-link-function
  1682. 'safe-local-variable
  1683. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1684. (defcustom org-confirm-elisp-link-not-regexp ""
  1685. "A regexp to skip confirmation for Elisp links."
  1686. :group 'org-link-follow
  1687. :version "24.1"
  1688. :type 'regexp)
  1689. (defconst org-file-apps-defaults-gnu
  1690. '((remote . emacs)
  1691. (system . mailcap)
  1692. (t . mailcap))
  1693. "Default file applications on a UNIX or GNU/Linux system.
  1694. See `org-file-apps'.")
  1695. (defconst org-file-apps-defaults-macosx
  1696. '((remote . emacs)
  1697. (t . "open %s")
  1698. (system . "open %s")
  1699. ("ps.gz" . "gv %s")
  1700. ("eps.gz" . "gv %s")
  1701. ("dvi" . "xdvi %s")
  1702. ("fig" . "xfig %s"))
  1703. "Default file applications on a MacOS X system.
  1704. The system \"open\" is known as a default, but we use X11 applications
  1705. for some files for which the OS does not have a good default.
  1706. See `org-file-apps'.")
  1707. (defconst org-file-apps-defaults-windowsnt
  1708. (list
  1709. '(remote . emacs)
  1710. (cons t
  1711. (list (if (featurep 'xemacs)
  1712. 'mswindows-shell-execute
  1713. 'w32-shell-execute)
  1714. "open" 'file))
  1715. (cons 'system
  1716. (list (if (featurep 'xemacs)
  1717. 'mswindows-shell-execute
  1718. 'w32-shell-execute)
  1719. "open" 'file)))
  1720. "Default file applications on a Windows NT system.
  1721. The system \"open\" is used for most files.
  1722. See `org-file-apps'.")
  1723. (defcustom org-file-apps
  1724. '((auto-mode . emacs)
  1725. ("\\.mm\\'" . default)
  1726. ("\\.x?html?\\'" . default)
  1727. ("\\.pdf\\'" . default))
  1728. "External applications for opening `file:path' items in a document.
  1729. Org-mode uses system defaults for different file types, but
  1730. you can use this variable to set the application for a given file
  1731. extension. The entries in this list are cons cells where the car identifies
  1732. files and the cdr the corresponding command. Possible values for the
  1733. file identifier are
  1734. \"string\" A string as a file identifier can be interpreted in different
  1735. ways, depending on its contents:
  1736. - Alphanumeric characters only:
  1737. Match links with this file extension.
  1738. Example: (\"pdf\" . \"evince %s\")
  1739. to open PDFs with evince.
  1740. - Regular expression: Match links where the
  1741. filename matches the regexp. If you want to
  1742. use groups here, use shy groups.
  1743. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1744. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1745. to open *.html and *.xhtml with firefox.
  1746. - Regular expression which contains (non-shy) groups:
  1747. Match links where the whole link, including \"::\", and
  1748. anything after that, matches the regexp.
  1749. In a custom command string, %1, %2, etc. are replaced with
  1750. the parts of the link that were matched by the groups.
  1751. For backwards compatibility, if a command string is given
  1752. that does not use any of the group matches, this case is
  1753. handled identically to the second one (i.e. match against
  1754. file name only).
  1755. In a custom lisp form, you can access the group matches with
  1756. (match-string n link).
  1757. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1758. to open [[file:document.pdf::5]] with evince at page 5.
  1759. `directory' Matches a directory
  1760. `remote' Matches a remote file, accessible through tramp or efs.
  1761. Remote files most likely should be visited through Emacs
  1762. because external applications cannot handle such paths.
  1763. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1764. so all files Emacs knows how to handle. Using this with
  1765. command `emacs' will open most files in Emacs. Beware that this
  1766. will also open html files inside Emacs, unless you add
  1767. (\"html\" . default) to the list as well.
  1768. t Default for files not matched by any of the other options.
  1769. `system' The system command to open files, like `open' on Windows
  1770. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1771. that will be selected if you call `C-c C-o' with a double
  1772. \\[universal-argument] \\[universal-argument] prefix.
  1773. Possible values for the command are:
  1774. `emacs' The file will be visited by the current Emacs process.
  1775. `default' Use the default application for this file type, which is the
  1776. association for t in the list, most likely in the system-specific
  1777. part.
  1778. This can be used to overrule an unwanted setting in the
  1779. system-specific variable.
  1780. `system' Use the system command for opening files, like \"open\".
  1781. This command is specified by the entry whose car is `system'.
  1782. Most likely, the system-specific version of this variable
  1783. does define this command, but you can overrule/replace it
  1784. here.
  1785. string A command to be executed by a shell; %s will be replaced
  1786. by the path to the file.
  1787. sexp A Lisp form which will be evaluated. The file path will
  1788. be available in the Lisp variable `file'.
  1789. For more examples, see the system specific constants
  1790. `org-file-apps-defaults-macosx'
  1791. `org-file-apps-defaults-windowsnt'
  1792. `org-file-apps-defaults-gnu'."
  1793. :group 'org-link-follow
  1794. :type '(repeat
  1795. (cons (choice :value ""
  1796. (string :tag "Extension")
  1797. (const :tag "System command to open files" system)
  1798. (const :tag "Default for unrecognized files" t)
  1799. (const :tag "Remote file" remote)
  1800. (const :tag "Links to a directory" directory)
  1801. (const :tag "Any files that have Emacs modes"
  1802. auto-mode))
  1803. (choice :value ""
  1804. (const :tag "Visit with Emacs" emacs)
  1805. (const :tag "Use default" default)
  1806. (const :tag "Use the system command" system)
  1807. (string :tag "Command")
  1808. (sexp :tag "Lisp form")))))
  1809. (defcustom org-doi-server-url "http://dx.doi.org/"
  1810. "The URL of the DOI server."
  1811. :type 'string
  1812. :version "24.3"
  1813. :group 'org-link-follow)
  1814. (defgroup org-refile nil
  1815. "Options concerning refiling entries in Org-mode."
  1816. :tag "Org Refile"
  1817. :group 'org)
  1818. (defcustom org-directory "~/org"
  1819. "Directory with org files.
  1820. This is just a default location to look for Org files. There is no need
  1821. at all to put your files into this directory. It is only used in the
  1822. following situations:
  1823. 1. When a capture template specifies a target file that is not an
  1824. absolute path. The path will then be interpreted relative to
  1825. `org-directory'
  1826. 2. When a capture note is filed away in an interactive way (when exiting the
  1827. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1828. with `org-directory' as the default path."
  1829. :group 'org-refile
  1830. :group 'org-capture
  1831. :type 'directory)
  1832. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1833. "Default target for storing notes.
  1834. Used as a fall back file for org-capture.el, for templates that
  1835. do not specify a target file."
  1836. :group 'org-refile
  1837. :group 'org-capture
  1838. :type '(choice
  1839. (const :tag "Default from remember-data-file" nil)
  1840. file))
  1841. (defcustom org-goto-interface 'outline
  1842. "The default interface to be used for `org-goto'.
  1843. Allowed values are:
  1844. outline The interface shows an outline of the relevant file
  1845. and the correct heading is found by moving through
  1846. the outline or by searching with incremental search.
  1847. outline-path-completion Headlines in the current buffer are offered via
  1848. completion. This is the interface also used by
  1849. the refile command."
  1850. :group 'org-refile
  1851. :type '(choice
  1852. (const :tag "Outline" outline)
  1853. (const :tag "Outline-path-completion" outline-path-completion)))
  1854. (defcustom org-goto-max-level 5
  1855. "Maximum target level when running `org-goto' with refile interface."
  1856. :group 'org-refile
  1857. :type 'integer)
  1858. (defcustom org-reverse-note-order nil
  1859. "Non-nil means store new notes at the beginning of a file or entry.
  1860. When nil, new notes will be filed to the end of a file or entry.
  1861. This can also be a list with cons cells of regular expressions that
  1862. are matched against file names, and values."
  1863. :group 'org-capture
  1864. :group 'org-refile
  1865. :type '(choice
  1866. (const :tag "Reverse always" t)
  1867. (const :tag "Reverse never" nil)
  1868. (repeat :tag "By file name regexp"
  1869. (cons regexp boolean))))
  1870. (defcustom org-log-refile nil
  1871. "Information to record when a task is refiled.
  1872. Possible values are:
  1873. nil Don't add anything
  1874. time Add a time stamp to the task
  1875. note Prompt for a note and add it with template `org-log-note-headings'
  1876. This option can also be set with on a per-file-basis with
  1877. #+STARTUP: nologrefile
  1878. #+STARTUP: logrefile
  1879. #+STARTUP: lognoterefile
  1880. You can have local logging settings for a subtree by setting the LOGGING
  1881. property to one or more of these keywords.
  1882. When bulk-refiling from the agenda, the value `note' is forbidden and
  1883. will temporarily be changed to `time'."
  1884. :group 'org-refile
  1885. :group 'org-progress
  1886. :version "24.1"
  1887. :type '(choice
  1888. (const :tag "No logging" nil)
  1889. (const :tag "Record timestamp" time)
  1890. (const :tag "Record timestamp with note." note)))
  1891. (defcustom org-refile-targets nil
  1892. "Targets for refiling entries with \\[org-refile].
  1893. This is a list of cons cells. Each cell contains:
  1894. - a specification of the files to be considered, either a list of files,
  1895. or a symbol whose function or variable value will be used to retrieve
  1896. a file name or a list of file names. If you use `org-agenda-files' for
  1897. that, all agenda files will be scanned for targets. Nil means consider
  1898. headings in the current buffer.
  1899. - A specification of how to find candidate refile targets. This may be
  1900. any of:
  1901. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1902. This tag has to be present in all target headlines, inheritance will
  1903. not be considered.
  1904. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1905. todo keyword.
  1906. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1907. headlines that are refiling targets.
  1908. - a cons cell (:level . N). Any headline of level N is considered a target.
  1909. Note that, when `org-odd-levels-only' is set, level corresponds to
  1910. order in hierarchy, not to the number of stars.
  1911. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1912. Note that, when `org-odd-levels-only' is set, level corresponds to
  1913. order in hierarchy, not to the number of stars.
  1914. Each element of this list generates a set of possible targets.
  1915. The union of these sets is presented (with completion) to
  1916. the user by `org-refile'.
  1917. You can set the variable `org-refile-target-verify-function' to a function
  1918. to verify each headline found by the simple criteria above.
  1919. When this variable is nil, all top-level headlines in the current buffer
  1920. are used, equivalent to the value `((nil . (:level . 1))'."
  1921. :group 'org-refile
  1922. :type '(repeat
  1923. (cons
  1924. (choice :value org-agenda-files
  1925. (const :tag "All agenda files" org-agenda-files)
  1926. (const :tag "Current buffer" nil)
  1927. (function) (variable) (file))
  1928. (choice :tag "Identify target headline by"
  1929. (cons :tag "Specific tag" (const :value :tag) (string))
  1930. (cons :tag "TODO keyword" (const :value :todo) (string))
  1931. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1932. (cons :tag "Level number" (const :value :level) (integer))
  1933. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1934. (defcustom org-refile-target-verify-function nil
  1935. "Function to verify if the headline at point should be a refile target.
  1936. The function will be called without arguments, with point at the
  1937. beginning of the headline. It should return t and leave point
  1938. where it is if the headline is a valid target for refiling.
  1939. If the target should not be selected, the function must return nil.
  1940. In addition to this, it may move point to a place from where the search
  1941. should be continued. For example, the function may decide that the entire
  1942. subtree of the current entry should be excluded and move point to the end
  1943. of the subtree."
  1944. :group 'org-refile
  1945. :type '(choice
  1946. (const nil)
  1947. (function)))
  1948. (defcustom org-refile-use-cache nil
  1949. "Non-nil means cache refile targets to speed up the process.
  1950. The cache for a particular file will be updated automatically when
  1951. the buffer has been killed, or when any of the marker used for flagging
  1952. refile targets no longer points at a live buffer.
  1953. If you have added new entries to a buffer that might themselves be targets,
  1954. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1955. find that easier, `C-u C-u C-u C-c C-w'."
  1956. :group 'org-refile
  1957. :version "24.1"
  1958. :type 'boolean)
  1959. (defcustom org-refile-use-outline-path nil
  1960. "Non-nil means provide refile targets as paths.
  1961. So a level 3 headline will be available as level1/level2/level3.
  1962. When the value is `file', also include the file name (without directory)
  1963. into the path. In this case, you can also stop the completion after
  1964. the file name, to get entries inserted as top level in the file.
  1965. When `full-file-path', include the full file path."
  1966. :group 'org-refile
  1967. :type '(choice
  1968. (const :tag "Not" nil)
  1969. (const :tag "Yes" t)
  1970. (const :tag "Start with file name" file)
  1971. (const :tag "Start with full file path" full-file-path)))
  1972. (defcustom org-outline-path-complete-in-steps t
  1973. "Non-nil means complete the outline path in hierarchical steps.
  1974. When Org-mode uses the refile interface to select an outline path
  1975. \(see variable `org-refile-use-outline-path'), the completion of
  1976. the path can be done is a single go, or if can be done in steps down
  1977. the headline hierarchy. Going in steps is probably the best if you
  1978. do not use a special completion package like `ido' or `icicles'.
  1979. However, when using these packages, going in one step can be very
  1980. fast, while still showing the whole path to the entry."
  1981. :group 'org-refile
  1982. :type 'boolean)
  1983. (defcustom org-refile-allow-creating-parent-nodes nil
  1984. "Non-nil means allow to create new nodes as refile targets.
  1985. New nodes are then created by adding \"/new node name\" to the completion
  1986. of an existing node. When the value of this variable is `confirm',
  1987. new node creation must be confirmed by the user (recommended).
  1988. When nil, the completion must match an existing entry.
  1989. Note that, if the new heading is not seen by the criteria
  1990. listed in `org-refile-targets', multiple instances of the same
  1991. heading would be created by trying again to file under the new
  1992. heading."
  1993. :group 'org-refile
  1994. :type '(choice
  1995. (const :tag "Never" nil)
  1996. (const :tag "Always" t)
  1997. (const :tag "Prompt for confirmation" confirm)))
  1998. (defcustom org-refile-active-region-within-subtree nil
  1999. "Non-nil means also refile active region within a subtree.
  2000. By default `org-refile' doesn't allow refiling regions if they
  2001. don't contain a set of subtrees, but it might be convenient to
  2002. do so sometimes: in that case, the first line of the region is
  2003. converted to a headline before refiling."
  2004. :group 'org-refile
  2005. :version "24.1"
  2006. :type 'boolean)
  2007. (defgroup org-todo nil
  2008. "Options concerning TODO items in Org-mode."
  2009. :tag "Org TODO"
  2010. :group 'org)
  2011. (defgroup org-progress nil
  2012. "Options concerning Progress logging in Org-mode."
  2013. :tag "Org Progress"
  2014. :group 'org-time)
  2015. (defvar org-todo-interpretation-widgets
  2016. '((:tag "Sequence (cycling hits every state)" sequence)
  2017. (:tag "Type (cycling directly to DONE)" type))
  2018. "The available interpretation symbols for customizing `org-todo-keywords'.
  2019. Interested libraries should add to this list.")
  2020. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2021. "List of TODO entry keyword sequences and their interpretation.
  2022. \\<org-mode-map>This is a list of sequences.
  2023. Each sequence starts with a symbol, either `sequence' or `type',
  2024. indicating if the keywords should be interpreted as a sequence of
  2025. action steps, or as different types of TODO items. The first
  2026. keywords are states requiring action - these states will select a headline
  2027. for inclusion into the global TODO list Org-mode produces. If one of
  2028. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2029. signify that no further action is necessary. If \"|\" is not found,
  2030. the last keyword is treated as the only DONE state of the sequence.
  2031. The command \\[org-todo] cycles an entry through these states, and one
  2032. additional state where no keyword is present. For details about this
  2033. cycling, see the manual.
  2034. TODO keywords and interpretation can also be set on a per-file basis with
  2035. the special #+SEQ_TODO and #+TYP_TODO lines.
  2036. Each keyword can optionally specify a character for fast state selection
  2037. \(in combination with the variable `org-use-fast-todo-selection')
  2038. and specifiers for state change logging, using the same syntax that
  2039. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2040. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2041. indicates to record a time stamp each time this state is selected.
  2042. Each keyword may also specify if a timestamp or a note should be
  2043. recorded when entering or leaving the state, by adding additional
  2044. characters in the parenthesis after the keyword. This looks like this:
  2045. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2046. record only the time of the state change. With X and Y being either
  2047. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2048. Y when leaving the state if and only if the *target* state does not
  2049. define X. You may omit any of the fast-selection key or X or /Y,
  2050. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2051. For backward compatibility, this variable may also be just a list
  2052. of keywords. In this case the interpretation (sequence or type) will be
  2053. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2054. :group 'org-todo
  2055. :group 'org-keywords
  2056. :type '(choice
  2057. (repeat :tag "Old syntax, just keywords"
  2058. (string :tag "Keyword"))
  2059. (repeat :tag "New syntax"
  2060. (cons
  2061. (choice
  2062. :tag "Interpretation"
  2063. ;;Quick and dirty way to see
  2064. ;;`org-todo-interpretations'. This takes the
  2065. ;;place of item arguments
  2066. :convert-widget
  2067. (lambda (widget)
  2068. (widget-put widget
  2069. :args (mapcar
  2070. #'(lambda (x)
  2071. (widget-convert
  2072. (cons 'const x)))
  2073. org-todo-interpretation-widgets))
  2074. widget))
  2075. (repeat
  2076. (string :tag "Keyword"))))))
  2077. (defvar org-todo-keywords-1 nil
  2078. "All TODO and DONE keywords active in a buffer.")
  2079. (make-variable-buffer-local 'org-todo-keywords-1)
  2080. (defvar org-todo-keywords-for-agenda nil)
  2081. (defvar org-done-keywords-for-agenda nil)
  2082. (defvar org-drawers-for-agenda nil)
  2083. (defvar org-todo-keyword-alist-for-agenda nil)
  2084. (defvar org-tag-alist-for-agenda nil
  2085. "Alist of all tags from all agenda files.")
  2086. (defvar org-tag-groups-alist-for-agenda nil
  2087. "Alist of all groups tags from all current agenda files.")
  2088. (defvar org-tag-groups-alist nil)
  2089. (make-variable-buffer-local 'org-tag-groups-alist)
  2090. (defvar org-agenda-contributing-files nil)
  2091. (defvar org-not-done-keywords nil)
  2092. (make-variable-buffer-local 'org-not-done-keywords)
  2093. (defvar org-done-keywords nil)
  2094. (make-variable-buffer-local 'org-done-keywords)
  2095. (defvar org-todo-heads nil)
  2096. (make-variable-buffer-local 'org-todo-heads)
  2097. (defvar org-todo-sets nil)
  2098. (make-variable-buffer-local 'org-todo-sets)
  2099. (defvar org-todo-log-states nil)
  2100. (make-variable-buffer-local 'org-todo-log-states)
  2101. (defvar org-todo-kwd-alist nil)
  2102. (make-variable-buffer-local 'org-todo-kwd-alist)
  2103. (defvar org-todo-key-alist nil)
  2104. (make-variable-buffer-local 'org-todo-key-alist)
  2105. (defvar org-todo-key-trigger nil)
  2106. (make-variable-buffer-local 'org-todo-key-trigger)
  2107. (defcustom org-todo-interpretation 'sequence
  2108. "Controls how TODO keywords are interpreted.
  2109. This variable is in principle obsolete and is only used for
  2110. backward compatibility, if the interpretation of todo keywords is
  2111. not given already in `org-todo-keywords'. See that variable for
  2112. more information."
  2113. :group 'org-todo
  2114. :group 'org-keywords
  2115. :type '(choice (const sequence)
  2116. (const type)))
  2117. (defcustom org-use-fast-todo-selection t
  2118. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2119. This variable describes if and under what circumstances the cycling
  2120. mechanism for TODO keywords will be replaced by a single-key, direct
  2121. selection scheme.
  2122. When nil, fast selection is never used.
  2123. When the symbol `prefix', it will be used when `org-todo' is called
  2124. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2125. `C-u t' in an agenda buffer.
  2126. When t, fast selection is used by default. In this case, the prefix
  2127. argument forces cycling instead.
  2128. In all cases, the special interface is only used if access keys have
  2129. actually been assigned by the user, i.e. if keywords in the configuration
  2130. are followed by a letter in parenthesis, like TODO(t)."
  2131. :group 'org-todo
  2132. :type '(choice
  2133. (const :tag "Never" nil)
  2134. (const :tag "By default" t)
  2135. (const :tag "Only with C-u C-c C-t" prefix)))
  2136. (defcustom org-provide-todo-statistics t
  2137. "Non-nil means update todo statistics after insert and toggle.
  2138. ALL-HEADLINES means update todo statistics by including headlines
  2139. with no TODO keyword as well, counting them as not done.
  2140. A list of TODO keywords means the same, but skip keywords that are
  2141. not in this list.
  2142. When this is set, todo statistics is updated in the parent of the
  2143. current entry each time a todo state is changed."
  2144. :group 'org-todo
  2145. :type '(choice
  2146. (const :tag "Yes, only for TODO entries" t)
  2147. (const :tag "Yes, including all entries" all-headlines)
  2148. (repeat :tag "Yes, for TODOs in this list"
  2149. (string :tag "TODO keyword"))
  2150. (other :tag "No TODO statistics" nil)))
  2151. (defcustom org-hierarchical-todo-statistics t
  2152. "Non-nil means TODO statistics covers just direct children.
  2153. When nil, all entries in the subtree are considered.
  2154. This has only an effect if `org-provide-todo-statistics' is set.
  2155. To set this to nil for only a single subtree, use a COOKIE_DATA
  2156. property and include the word \"recursive\" into the value."
  2157. :group 'org-todo
  2158. :type 'boolean)
  2159. (defcustom org-after-todo-state-change-hook nil
  2160. "Hook which is run after the state of a TODO item was changed.
  2161. The new state (a string with a TODO keyword, or nil) is available in the
  2162. Lisp variable `org-state'."
  2163. :group 'org-todo
  2164. :type 'hook)
  2165. (defvar org-blocker-hook nil
  2166. "Hook for functions that are allowed to block a state change.
  2167. Functions in this hook should not modify the buffer.
  2168. Each function gets as its single argument a property list,
  2169. see `org-trigger-hook' for more information about this list.
  2170. If any of the functions in this hook returns nil, the state change
  2171. is blocked.")
  2172. (defvar org-trigger-hook nil
  2173. "Hook for functions that are triggered by a state change.
  2174. Each function gets as its single argument a property list with at
  2175. least the following elements:
  2176. (:type type-of-change :position pos-at-entry-start
  2177. :from old-state :to new-state)
  2178. Depending on the type, more properties may be present.
  2179. This mechanism is currently implemented for:
  2180. TODO state changes
  2181. ------------------
  2182. :type todo-state-change
  2183. :from previous state (keyword as a string), or nil, or a symbol
  2184. 'todo' or 'done', to indicate the general type of state.
  2185. :to new state, like in :from")
  2186. (defcustom org-enforce-todo-dependencies nil
  2187. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2188. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2189. be blocked if any prior sibling is not yet done.
  2190. Finally, if the parent is blocked because of ordered siblings of its own,
  2191. the child will also be blocked."
  2192. :set (lambda (var val)
  2193. (set var val)
  2194. (if val
  2195. (add-hook 'org-blocker-hook
  2196. 'org-block-todo-from-children-or-siblings-or-parent)
  2197. (remove-hook 'org-blocker-hook
  2198. 'org-block-todo-from-children-or-siblings-or-parent)))
  2199. :group 'org-todo
  2200. :type 'boolean)
  2201. (defcustom org-enforce-todo-checkbox-dependencies nil
  2202. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2203. When this is nil, checkboxes have no influence on switching TODO states.
  2204. When non-nil, you first need to check off all check boxes before the TODO
  2205. entry can be switched to DONE.
  2206. This variable needs to be set before org.el is loaded, and you need to
  2207. restart Emacs after a change to make the change effective. The only way
  2208. to change is while Emacs is running is through the customize interface."
  2209. :set (lambda (var val)
  2210. (set var val)
  2211. (if val
  2212. (add-hook 'org-blocker-hook
  2213. 'org-block-todo-from-checkboxes)
  2214. (remove-hook 'org-blocker-hook
  2215. 'org-block-todo-from-checkboxes)))
  2216. :group 'org-todo
  2217. :type 'boolean)
  2218. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2219. "Non-nil means inserting a TODO heading is treated as state change.
  2220. So when the command \\[org-insert-todo-heading] is used, state change
  2221. logging will apply if appropriate. When nil, the new TODO item will
  2222. be inserted directly, and no logging will take place."
  2223. :group 'org-todo
  2224. :type 'boolean)
  2225. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2226. "Non-nil means switching TODO states with S-cursor counts as state change.
  2227. This is the default behavior. However, setting this to nil allows a
  2228. convenient way to select a TODO state and bypass any logging associated
  2229. with that."
  2230. :group 'org-todo
  2231. :type 'boolean)
  2232. (defcustom org-todo-state-tags-triggers nil
  2233. "Tag changes that should be triggered by TODO state changes.
  2234. This is a list. Each entry is
  2235. (state-change (tag . flag) .......)
  2236. State-change can be a string with a state, and empty string to indicate the
  2237. state that has no TODO keyword, or it can be one of the symbols `todo'
  2238. or `done', meaning any not-done or done state, respectively."
  2239. :group 'org-todo
  2240. :group 'org-tags
  2241. :type '(repeat
  2242. (cons (choice :tag "When changing to"
  2243. (const :tag "Not-done state" todo)
  2244. (const :tag "Done state" done)
  2245. (string :tag "State"))
  2246. (repeat
  2247. (cons :tag "Tag action"
  2248. (string :tag "Tag")
  2249. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2250. (defcustom org-log-done nil
  2251. "Information to record when a task moves to the DONE state.
  2252. Possible values are:
  2253. nil Don't add anything, just change the keyword
  2254. time Add a time stamp to the task
  2255. note Prompt for a note and add it with template `org-log-note-headings'
  2256. This option can also be set with on a per-file-basis with
  2257. #+STARTUP: nologdone
  2258. #+STARTUP: logdone
  2259. #+STARTUP: lognotedone
  2260. You can have local logging settings for a subtree by setting the LOGGING
  2261. property to one or more of these keywords."
  2262. :group 'org-todo
  2263. :group 'org-progress
  2264. :type '(choice
  2265. (const :tag "No logging" nil)
  2266. (const :tag "Record CLOSED timestamp" time)
  2267. (const :tag "Record CLOSED timestamp with note." note)))
  2268. ;; Normalize old uses of org-log-done.
  2269. (cond
  2270. ((eq org-log-done t) (setq org-log-done 'time))
  2271. ((and (listp org-log-done) (memq 'done org-log-done))
  2272. (setq org-log-done 'note)))
  2273. (defcustom org-log-reschedule nil
  2274. "Information to record when the scheduling date of a tasks is modified.
  2275. Possible values are:
  2276. nil Don't add anything, just change the date
  2277. time Add a time stamp to the task
  2278. note Prompt for a note and add it with template `org-log-note-headings'
  2279. This option can also be set with on a per-file-basis with
  2280. #+STARTUP: nologreschedule
  2281. #+STARTUP: logreschedule
  2282. #+STARTUP: lognotereschedule"
  2283. :group 'org-todo
  2284. :group 'org-progress
  2285. :type '(choice
  2286. (const :tag "No logging" nil)
  2287. (const :tag "Record timestamp" time)
  2288. (const :tag "Record timestamp with note." note)))
  2289. (defcustom org-log-redeadline nil
  2290. "Information to record when the deadline date of a tasks is modified.
  2291. Possible values are:
  2292. nil Don't add anything, just change the date
  2293. time Add a time stamp to the task
  2294. note Prompt for a note and add it with template `org-log-note-headings'
  2295. This option can also be set with on a per-file-basis with
  2296. #+STARTUP: nologredeadline
  2297. #+STARTUP: logredeadline
  2298. #+STARTUP: lognoteredeadline
  2299. You can have local logging settings for a subtree by setting the LOGGING
  2300. property to one or more of these keywords."
  2301. :group 'org-todo
  2302. :group 'org-progress
  2303. :type '(choice
  2304. (const :tag "No logging" nil)
  2305. (const :tag "Record timestamp" time)
  2306. (const :tag "Record timestamp with note." note)))
  2307. (defcustom org-log-note-clock-out nil
  2308. "Non-nil means record a note when clocking out of an item.
  2309. This can also be configured on a per-file basis by adding one of
  2310. the following lines anywhere in the buffer:
  2311. #+STARTUP: lognoteclock-out
  2312. #+STARTUP: nolognoteclock-out"
  2313. :group 'org-todo
  2314. :group 'org-progress
  2315. :type 'boolean)
  2316. (defcustom org-log-done-with-time t
  2317. "Non-nil means the CLOSED time stamp will contain date and time.
  2318. When nil, only the date will be recorded."
  2319. :group 'org-progress
  2320. :type 'boolean)
  2321. (defcustom org-log-note-headings
  2322. '((done . "CLOSING NOTE %t")
  2323. (state . "State %-12s from %-12S %t")
  2324. (note . "Note taken on %t")
  2325. (reschedule . "Rescheduled from %S on %t")
  2326. (delschedule . "Not scheduled, was %S on %t")
  2327. (redeadline . "New deadline from %S on %t")
  2328. (deldeadline . "Removed deadline, was %S on %t")
  2329. (refile . "Refiled on %t")
  2330. (clock-out . ""))
  2331. "Headings for notes added to entries.
  2332. The value is an alist, with the car being a symbol indicating the note
  2333. context, and the cdr is the heading to be used. The heading may also be the
  2334. empty string.
  2335. %t in the heading will be replaced by a time stamp.
  2336. %T will be an active time stamp instead the default inactive one
  2337. %d will be replaced by a short-format time stamp.
  2338. %D will be replaced by an active short-format time stamp.
  2339. %s will be replaced by the new TODO state, in double quotes.
  2340. %S will be replaced by the old TODO state, in double quotes.
  2341. %u will be replaced by the user name.
  2342. %U will be replaced by the full user name.
  2343. In fact, it is not a good idea to change the `state' entry, because
  2344. agenda log mode depends on the format of these entries."
  2345. :group 'org-todo
  2346. :group 'org-progress
  2347. :type '(list :greedy t
  2348. (cons (const :tag "Heading when closing an item" done) string)
  2349. (cons (const :tag
  2350. "Heading when changing todo state (todo sequence only)"
  2351. state) string)
  2352. (cons (const :tag "Heading when just taking a note" note) string)
  2353. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2354. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2355. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2356. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2357. (cons (const :tag "Heading when refiling" refile) string)
  2358. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2359. (unless (assq 'note org-log-note-headings)
  2360. (push '(note . "%t") org-log-note-headings))
  2361. (defcustom org-log-into-drawer nil
  2362. "Non-nil means insert state change notes and time stamps into a drawer.
  2363. When nil, state changes notes will be inserted after the headline and
  2364. any scheduling and clock lines, but not inside a drawer.
  2365. The value of this variable should be the name of the drawer to use.
  2366. LOGBOOK is proposed as the default drawer for this purpose, you can
  2367. also set this to a string to define the drawer of your choice.
  2368. A value of t is also allowed, representing \"LOGBOOK\".
  2369. A value of t or nil can also be set with on a per-file-basis with
  2370. #+STARTUP: logdrawer
  2371. #+STARTUP: nologdrawer
  2372. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2373. will be ignored.
  2374. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2375. a subtree."
  2376. :group 'org-todo
  2377. :group 'org-progress
  2378. :type '(choice
  2379. (const :tag "Not into a drawer" nil)
  2380. (const :tag "LOGBOOK" t)
  2381. (string :tag "Other")))
  2382. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2383. (defun org-log-into-drawer ()
  2384. "Return the value of `org-log-into-drawer', but let properties overrule.
  2385. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2386. used instead of the default value."
  2387. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2388. (cond
  2389. ((not p) org-log-into-drawer)
  2390. ((equal p "nil") nil)
  2391. ((equal p "t") "LOGBOOK")
  2392. (t p))))
  2393. (defcustom org-log-state-notes-insert-after-drawers nil
  2394. "Non-nil means insert state change notes after any drawers in entry.
  2395. Only the drawers that *immediately* follow the headline and the
  2396. deadline/scheduled line are skipped.
  2397. When nil, insert notes right after the heading and perhaps the line
  2398. with deadline/scheduling if present.
  2399. This variable will have no effect if `org-log-into-drawer' is
  2400. set."
  2401. :group 'org-todo
  2402. :group 'org-progress
  2403. :type 'boolean)
  2404. (defcustom org-log-states-order-reversed t
  2405. "Non-nil means the latest state note will be directly after heading.
  2406. When nil, the state change notes will be ordered according to time.
  2407. This option can also be set with on a per-file-basis with
  2408. #+STARTUP: logstatesreversed
  2409. #+STARTUP: nologstatesreversed"
  2410. :group 'org-todo
  2411. :group 'org-progress
  2412. :type 'boolean)
  2413. (defcustom org-todo-repeat-to-state nil
  2414. "The TODO state to which a repeater should return the repeating task.
  2415. By default this is the first task in a TODO sequence, or the previous state
  2416. in a TODO_TYP set. But you can specify another task here.
  2417. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2418. :group 'org-todo
  2419. :version "24.1"
  2420. :type '(choice (const :tag "Head of sequence" nil)
  2421. (string :tag "Specific state")))
  2422. (defcustom org-log-repeat 'time
  2423. "Non-nil means record moving through the DONE state when triggering repeat.
  2424. An auto-repeating task is immediately switched back to TODO when
  2425. marked DONE. If you are not logging state changes (by adding \"@\"
  2426. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2427. record a closing note, there will be no record of the task moving
  2428. through DONE. This variable forces taking a note anyway.
  2429. nil Don't force a record
  2430. time Record a time stamp
  2431. note Prompt for a note and add it with template `org-log-note-headings'
  2432. This option can also be set with on a per-file-basis with
  2433. #+STARTUP: nologrepeat
  2434. #+STARTUP: logrepeat
  2435. #+STARTUP: lognoterepeat
  2436. You can have local logging settings for a subtree by setting the LOGGING
  2437. property to one or more of these keywords."
  2438. :group 'org-todo
  2439. :group 'org-progress
  2440. :type '(choice
  2441. (const :tag "Don't force a record" nil)
  2442. (const :tag "Force recording the DONE state" time)
  2443. (const :tag "Force recording a note with the DONE state" note)))
  2444. (defgroup org-priorities nil
  2445. "Priorities in Org-mode."
  2446. :tag "Org Priorities"
  2447. :group 'org-todo)
  2448. (defcustom org-enable-priority-commands t
  2449. "Non-nil means priority commands are active.
  2450. When nil, these commands will be disabled, so that you never accidentally
  2451. set a priority."
  2452. :group 'org-priorities
  2453. :type 'boolean)
  2454. (defcustom org-highest-priority ?A
  2455. "The highest priority of TODO items. A character like ?A, ?B etc.
  2456. Must have a smaller ASCII number than `org-lowest-priority'."
  2457. :group 'org-priorities
  2458. :type 'character)
  2459. (defcustom org-lowest-priority ?C
  2460. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2461. Must have a larger ASCII number than `org-highest-priority'."
  2462. :group 'org-priorities
  2463. :type 'character)
  2464. (defcustom org-default-priority ?B
  2465. "The default priority of TODO items.
  2466. This is the priority an item gets if no explicit priority is given.
  2467. When starting to cycle on an empty priority the first step in the cycle
  2468. depends on `org-priority-start-cycle-with-default'. The resulting first
  2469. step priority must not exceed the range from `org-highest-priority' to
  2470. `org-lowest-priority' which means that `org-default-priority' has to be
  2471. in this range exclusive or inclusive the range boundaries. Else the
  2472. first step refuses to set the default and the second will fall back
  2473. to (depending on the command used) the highest or lowest priority."
  2474. :group 'org-priorities
  2475. :type 'character)
  2476. (defcustom org-priority-start-cycle-with-default t
  2477. "Non-nil means start with default priority when starting to cycle.
  2478. When this is nil, the first step in the cycle will be (depending on the
  2479. command used) one higher or lower than the default priority.
  2480. See also `org-default-priority'."
  2481. :group 'org-priorities
  2482. :type 'boolean)
  2483. (defcustom org-get-priority-function nil
  2484. "Function to extract the priority from a string.
  2485. The string is normally the headline. If this is nil Org computes the
  2486. priority from the priority cookie like [#A] in the headline. It returns
  2487. an integer, increasing by 1000 for each priority level.
  2488. The user can set a different function here, which should take a string
  2489. as an argument and return the numeric priority."
  2490. :group 'org-priorities
  2491. :version "24.1"
  2492. :type '(choice
  2493. (const nil)
  2494. (function)))
  2495. (defgroup org-time nil
  2496. "Options concerning time stamps and deadlines in Org-mode."
  2497. :tag "Org Time"
  2498. :group 'org)
  2499. (defcustom org-insert-labeled-timestamps-at-point nil
  2500. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2501. When nil, these labeled time stamps are forces into the second line of an
  2502. entry, just after the headline. When scheduling from the global TODO list,
  2503. the time stamp will always be forced into the second line."
  2504. :group 'org-time
  2505. :type 'boolean)
  2506. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2507. "Formats for `format-time-string' which are used for time stamps.
  2508. It is not recommended to change this constant.")
  2509. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2510. "Number of minutes to round time stamps to.
  2511. These are two values, the first applies when first creating a time stamp.
  2512. The second applies when changing it with the commands `S-up' and `S-down'.
  2513. When changing the time stamp, this means that it will change in steps
  2514. of N minutes, as given by the second value.
  2515. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2516. numbers should be factors of 60, so for example 5, 10, 15.
  2517. When this is larger than 1, you can still force an exact time stamp by using
  2518. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2519. and by using a prefix arg to `S-up/down' to specify the exact number
  2520. of minutes to shift."
  2521. :group 'org-time
  2522. :get #'(lambda (var) ; Make sure both elements are there
  2523. (if (integerp (default-value var))
  2524. (list (default-value var) 5)
  2525. (default-value var)))
  2526. :type '(list
  2527. (integer :tag "when inserting times")
  2528. (integer :tag "when modifying times")))
  2529. ;; Normalize old customizations of this variable.
  2530. (when (integerp org-time-stamp-rounding-minutes)
  2531. (setq org-time-stamp-rounding-minutes
  2532. (list org-time-stamp-rounding-minutes
  2533. org-time-stamp-rounding-minutes)))
  2534. (defcustom org-display-custom-times nil
  2535. "Non-nil means overlay custom formats over all time stamps.
  2536. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2537. To turn this on on a per-file basis, insert anywhere in the file:
  2538. #+STARTUP: customtime"
  2539. :group 'org-time
  2540. :set 'set-default
  2541. :type 'sexp)
  2542. (make-variable-buffer-local 'org-display-custom-times)
  2543. (defcustom org-time-stamp-custom-formats
  2544. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2545. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2546. These are overlaid over the default ISO format if the variable
  2547. `org-display-custom-times' is set. Time like %H:%M should be at the
  2548. end of the second format. The custom formats are also honored by export
  2549. commands, if custom time display is turned on at the time of export."
  2550. :group 'org-time
  2551. :type 'sexp)
  2552. (defun org-time-stamp-format (&optional long inactive)
  2553. "Get the right format for a time string."
  2554. (let ((f (if long (cdr org-time-stamp-formats)
  2555. (car org-time-stamp-formats))))
  2556. (if inactive
  2557. (concat "[" (substring f 1 -1) "]")
  2558. f)))
  2559. (defcustom org-time-clocksum-format
  2560. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2561. "The format string used when creating CLOCKSUM lines.
  2562. This is also used when Org mode generates a time duration.
  2563. The value can be a single format string containing two
  2564. %-sequences, which will be filled with the number of hours and
  2565. minutes in that order.
  2566. Alternatively, the value can be a plist associating any of the
  2567. keys :years, :months, :weeks, :days, :hours or :minutes with
  2568. format strings. The time duration is formatted using only the
  2569. time components that are needed and concatenating the results.
  2570. If a time unit in absent, it falls back to the next smallest
  2571. unit.
  2572. The keys :require-years, :require-months, :require-days,
  2573. :require-weeks, :require-hours, :require-minutes are also
  2574. meaningful. A non-nil value for these keys indicates that the
  2575. corresponding time component should always be included, even if
  2576. its value is 0.
  2577. For example,
  2578. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2579. :require-minutes t)
  2580. means durations longer than a day will be expressed in days,
  2581. hours and minutes, and durations less than a day will always be
  2582. expressed in hours and minutes (even for durations less than an
  2583. hour).
  2584. The value
  2585. \(:days \"%dd\" :minutes \"%dm\")
  2586. means durations longer than a day will be expressed in days and
  2587. minutes, and durations less than a day will be expressed entirely
  2588. in minutes (even for durations longer than an hour)."
  2589. :group 'org-time
  2590. :group 'org-clock
  2591. :version "24.4"
  2592. :package-version '(Org . "8.0")
  2593. :type '(choice (string :tag "Format string")
  2594. (set :tag "Plist"
  2595. (group :inline t (const :tag "Years" :years)
  2596. (string :tag "Format string"))
  2597. (group :inline t
  2598. (const :tag "Always show years" :require-years)
  2599. (const t))
  2600. (group :inline t (const :tag "Months" :months)
  2601. (string :tag "Format string"))
  2602. (group :inline t
  2603. (const :tag "Always show months" :require-months)
  2604. (const t))
  2605. (group :inline t (const :tag "Weeks" :weeks)
  2606. (string :tag "Format string"))
  2607. (group :inline t
  2608. (const :tag "Always show weeks" :require-weeks)
  2609. (const t))
  2610. (group :inline t (const :tag "Days" :days)
  2611. (string :tag "Format string"))
  2612. (group :inline t
  2613. (const :tag "Always show days" :require-days)
  2614. (const t))
  2615. (group :inline t (const :tag "Hours" :hours)
  2616. (string :tag "Format string"))
  2617. (group :inline t
  2618. (const :tag "Always show hours" :require-hours)
  2619. (const t))
  2620. (group :inline t (const :tag "Minutes" :minutes)
  2621. (string :tag "Format string"))
  2622. (group :inline t
  2623. (const :tag "Always show minutes" :require-minutes)
  2624. (const t)))))
  2625. (defcustom org-time-clocksum-use-fractional nil
  2626. "When non-nil, \\[org-clock-display] uses fractional times.
  2627. See `org-time-clocksum-format' for more on time clock formats."
  2628. :group 'org-time
  2629. :group 'org-clock
  2630. :version "24.3"
  2631. :type 'boolean)
  2632. (defcustom org-time-clocksum-use-effort-durations nil
  2633. "When non-nil, \\[org-clock-display] uses effort durations.
  2634. E.g. by default, one day is considered to be a 8 hours effort,
  2635. so a task that has been clocked for 16 hours will be displayed
  2636. as during 2 days in the clock display or in the clocktable.
  2637. See `org-effort-durations' on how to set effort durations
  2638. and `org-time-clocksum-format' for more on time clock formats."
  2639. :group 'org-time
  2640. :group 'org-clock
  2641. :version "24.4"
  2642. :package-version '(Org . "8.0")
  2643. :type 'boolean)
  2644. (defcustom org-time-clocksum-fractional-format "%.2f"
  2645. "The format string used when creating CLOCKSUM lines,
  2646. or when Org mode generates a time duration, if
  2647. `org-time-clocksum-use-fractional' is enabled.
  2648. The value can be a single format string containing one
  2649. %-sequence, which will be filled with the number of hours as
  2650. a float.
  2651. Alternatively, the value can be a plist associating any of the
  2652. keys :years, :months, :weeks, :days, :hours or :minutes with
  2653. a format string. The time duration is formatted using the
  2654. largest time unit which gives a non-zero integer part. If all
  2655. specified formats have zero integer part, the smallest time unit
  2656. is used."
  2657. :group 'org-time
  2658. :type '(choice (string :tag "Format string")
  2659. (set (group :inline t (const :tag "Years" :years)
  2660. (string :tag "Format string"))
  2661. (group :inline t (const :tag "Months" :months)
  2662. (string :tag "Format string"))
  2663. (group :inline t (const :tag "Weeks" :weeks)
  2664. (string :tag "Format string"))
  2665. (group :inline t (const :tag "Days" :days)
  2666. (string :tag "Format string"))
  2667. (group :inline t (const :tag "Hours" :hours)
  2668. (string :tag "Format string"))
  2669. (group :inline t (const :tag "Minutes" :minutes)
  2670. (string :tag "Format string")))))
  2671. (defcustom org-deadline-warning-days 14
  2672. "Number of days before expiration during which a deadline becomes active.
  2673. This variable governs the display in sparse trees and in the agenda.
  2674. When 0 or negative, it means use this number (the absolute value of it)
  2675. even if a deadline has a different individual lead time specified.
  2676. Custom commands can set this variable in the options section."
  2677. :group 'org-time
  2678. :group 'org-agenda-daily/weekly
  2679. :type 'integer)
  2680. (defcustom org-scheduled-delay-days 0
  2681. "Number of days before a scheduled item becomes active.
  2682. This variable governs the display in sparse trees and in the agenda.
  2683. The default value (i.e. 0) means: don't delay scheduled item.
  2684. When negative, it means use this number (the absolute value of it)
  2685. even if a scheduled item has a different individual delay time
  2686. specified.
  2687. Custom commands can set this variable in the options section."
  2688. :group 'org-time
  2689. :group 'org-agenda-daily/weekly
  2690. :version "24.4"
  2691. :package-version '(Org . "8.0")
  2692. :type 'integer)
  2693. (defcustom org-read-date-prefer-future t
  2694. "Non-nil means assume future for incomplete date input from user.
  2695. This affects the following situations:
  2696. 1. The user gives a month but not a year.
  2697. For example, if it is April and you enter \"feb 2\", this will be read
  2698. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2699. 2. The user gives a day, but no month.
  2700. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2701. read this as the third of *next* month. However, if you enter \"17\",
  2702. it will be considered as *this* month.
  2703. If you set this variable to the symbol `time', then also the following
  2704. will work:
  2705. 3. If the user gives a time.
  2706. If the time is before now, it will be interpreted as tomorrow.
  2707. Currently none of this works for ISO week specifications.
  2708. When this option is nil, the current day, month and year will always be
  2709. used as defaults.
  2710. See also `org-agenda-jump-prefer-future'."
  2711. :group 'org-time
  2712. :type '(choice
  2713. (const :tag "Never" nil)
  2714. (const :tag "Check month and day" t)
  2715. (const :tag "Check month, day, and time" time)))
  2716. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2717. "Should the agenda jump command prefer the future for incomplete dates?
  2718. The default is to do the same as configured in `org-read-date-prefer-future'.
  2719. But you can also set a deviating value here.
  2720. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2721. :group 'org-agenda
  2722. :group 'org-time
  2723. :version "24.1"
  2724. :type '(choice
  2725. (const :tag "Use org-read-date-prefer-future"
  2726. org-read-date-prefer-future)
  2727. (const :tag "Never" nil)
  2728. (const :tag "Always" t)))
  2729. (defcustom org-read-date-force-compatible-dates t
  2730. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2731. Depending on the system Emacs is running on, certain dates cannot
  2732. be represented with the type used internally to represent time.
  2733. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2734. correctly. Some systems allow for earlier dates, some for later,
  2735. some for both. One way to find out it to insert any date into an
  2736. Org buffer, putting the cursor on the year and hitting S-up and
  2737. S-down to test the range.
  2738. When this variable is set to t, the date/time prompt will not let
  2739. you specify dates outside the 1970-2037 range, so it is certain that
  2740. these dates will work in whatever version of Emacs you are
  2741. running, and also that you can move a file from one Emacs implementation
  2742. to another. WHenever Org is forcing the year for you, it will display
  2743. a message and beep.
  2744. When this variable is nil, Org will check if the date is
  2745. representable in the specific Emacs implementation you are using.
  2746. If not, it will force a year, usually the current year, and beep
  2747. to remind you. Currently this setting is not recommended because
  2748. the likelihood that you will open your Org files in an Emacs that
  2749. has limited date range is not negligible.
  2750. A workaround for this problem is to use diary sexp dates for time
  2751. stamps outside of this range."
  2752. :group 'org-time
  2753. :version "24.1"
  2754. :type 'boolean)
  2755. (defcustom org-read-date-display-live t
  2756. "Non-nil means display current interpretation of date prompt live.
  2757. This display will be in an overlay, in the minibuffer."
  2758. :group 'org-time
  2759. :type 'boolean)
  2760. (defcustom org-read-date-popup-calendar t
  2761. "Non-nil means pop up a calendar when prompting for a date.
  2762. In the calendar, the date can be selected with mouse-1. However, the
  2763. minibuffer will also be active, and you can simply enter the date as well.
  2764. When nil, only the minibuffer will be available."
  2765. :group 'org-time
  2766. :type 'boolean)
  2767. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2768. 'org-read-date-popup-calendar)
  2769. (make-obsolete-variable
  2770. 'org-read-date-minibuffer-setup-hook
  2771. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2772. (defcustom org-read-date-minibuffer-setup-hook nil
  2773. "Hook to be used to set up keys for the date/time interface.
  2774. Add key definitions to `minibuffer-local-map', which will be a
  2775. temporary copy.
  2776. WARNING: This option is obsolete, you should use
  2777. `org-read-date-minibuffer-local-map' to set up keys."
  2778. :group 'org-time
  2779. :type 'hook)
  2780. (defcustom org-extend-today-until 0
  2781. "The hour when your day really ends. Must be an integer.
  2782. This has influence for the following applications:
  2783. - When switching the agenda to \"today\". It it is still earlier than
  2784. the time given here, the day recognized as TODAY is actually yesterday.
  2785. - When a date is read from the user and it is still before the time given
  2786. here, the current date and time will be assumed to be yesterday, 23:59.
  2787. Also, timestamps inserted in capture templates follow this rule.
  2788. IMPORTANT: This is a feature whose implementation is and likely will
  2789. remain incomplete. Really, it is only here because past midnight seems to
  2790. be the favorite working time of John Wiegley :-)"
  2791. :group 'org-time
  2792. :type 'integer)
  2793. (defcustom org-use-effective-time nil
  2794. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2795. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2796. \"effective time\" of any timestamps between midnight and 8am will be
  2797. 23:59 of the previous day."
  2798. :group 'org-time
  2799. :version "24.1"
  2800. :type 'boolean)
  2801. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2802. "When non-nil, use the last clock out time for `org-todo'.
  2803. Note that this option has precedence over the combined use of
  2804. `org-use-effective-time' and `org-extend-today-until'."
  2805. :group 'org-time
  2806. :version "24.4"
  2807. :package-version '(Org . "8.0")
  2808. :type 'boolean)
  2809. (defcustom org-edit-timestamp-down-means-later nil
  2810. "Non-nil means S-down will increase the time in a time stamp.
  2811. When nil, S-up will increase."
  2812. :group 'org-time
  2813. :type 'boolean)
  2814. (defcustom org-calendar-follow-timestamp-change t
  2815. "Non-nil means make the calendar window follow timestamp changes.
  2816. When a timestamp is modified and the calendar window is visible, it will be
  2817. moved to the new date."
  2818. :group 'org-time
  2819. :type 'boolean)
  2820. (defgroup org-tags nil
  2821. "Options concerning tags in Org-mode."
  2822. :tag "Org Tags"
  2823. :group 'org)
  2824. (defcustom org-tag-alist nil
  2825. "List of tags allowed in Org-mode files.
  2826. When this list is nil, Org-mode will base TAG input on what is already in the
  2827. buffer.
  2828. The value of this variable is an alist, the car of each entry must be a
  2829. keyword as a string, the cdr may be a character that is used to select
  2830. that tag through the fast-tag-selection interface.
  2831. See the manual for details."
  2832. :group 'org-tags
  2833. :type '(repeat
  2834. (choice
  2835. (cons (string :tag "Tag name")
  2836. (character :tag "Access char"))
  2837. (list :tag "Start radio group"
  2838. (const :startgroup)
  2839. (option (string :tag "Group description")))
  2840. (list :tag "Group tags delimiter"
  2841. (const :grouptags))
  2842. (list :tag "End radio group"
  2843. (const :endgroup)
  2844. (option (string :tag "Group description")))
  2845. (const :tag "New line" (:newline)))))
  2846. (defcustom org-tag-persistent-alist nil
  2847. "List of tags that will always appear in all Org-mode files.
  2848. This is in addition to any in buffer settings or customizations
  2849. of `org-tag-alist'.
  2850. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2851. The value of this variable is an alist, the car of each entry must be a
  2852. keyword as a string, the cdr may be a character that is used to select
  2853. that tag through the fast-tag-selection interface.
  2854. See the manual for details.
  2855. To disable these tags on a per-file basis, insert anywhere in the file:
  2856. #+STARTUP: noptag"
  2857. :group 'org-tags
  2858. :type '(repeat
  2859. (choice
  2860. (cons (string :tag "Tag name")
  2861. (character :tag "Access char"))
  2862. (const :tag "Start radio group" (:startgroup))
  2863. (const :tag "Group tags delimiter" (:grouptags))
  2864. (const :tag "End radio group" (:endgroup))
  2865. (const :tag "New line" (:newline)))))
  2866. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2867. "If non-nil, always offer completion for all tags of all agenda files.
  2868. Instead of customizing this variable directly, you might want to
  2869. set it locally for capture buffers, because there no list of
  2870. tags in that file can be created dynamically (there are none).
  2871. (add-hook 'org-capture-mode-hook
  2872. (lambda ()
  2873. (set (make-local-variable
  2874. 'org-complete-tags-always-offer-all-agenda-tags)
  2875. t)))"
  2876. :group 'org-tags
  2877. :version "24.1"
  2878. :type 'boolean)
  2879. (defvar org-file-tags nil
  2880. "List of tags that can be inherited by all entries in the file.
  2881. The tags will be inherited if the variable `org-use-tag-inheritance'
  2882. says they should be.
  2883. This variable is populated from #+FILETAGS lines.")
  2884. (defcustom org-use-fast-tag-selection 'auto
  2885. "Non-nil means use fast tag selection scheme.
  2886. This is a special interface to select and deselect tags with single keys.
  2887. When nil, fast selection is never used.
  2888. When the symbol `auto', fast selection is used if and only if selection
  2889. characters for tags have been configured, either through the variable
  2890. `org-tag-alist' or through a #+TAGS line in the buffer.
  2891. When t, fast selection is always used and selection keys are assigned
  2892. automatically if necessary."
  2893. :group 'org-tags
  2894. :type '(choice
  2895. (const :tag "Always" t)
  2896. (const :tag "Never" nil)
  2897. (const :tag "When selection characters are configured" auto)))
  2898. (defcustom org-fast-tag-selection-single-key nil
  2899. "Non-nil means fast tag selection exits after first change.
  2900. When nil, you have to press RET to exit it.
  2901. During fast tag selection, you can toggle this flag with `C-c'.
  2902. This variable can also have the value `expert'. In this case, the window
  2903. displaying the tags menu is not even shown, until you press C-c again."
  2904. :group 'org-tags
  2905. :type '(choice
  2906. (const :tag "No" nil)
  2907. (const :tag "Yes" t)
  2908. (const :tag "Expert" expert)))
  2909. (defvar org-fast-tag-selection-include-todo nil
  2910. "Non-nil means fast tags selection interface will also offer TODO states.
  2911. This is an undocumented feature, you should not rely on it.")
  2912. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2913. "The column to which tags should be indented in a headline.
  2914. If this number is positive, it specifies the column. If it is negative,
  2915. it means that the tags should be flushright to that column. For example,
  2916. -80 works well for a normal 80 character screen.
  2917. When 0, place tags directly after headline text, with only one space in
  2918. between."
  2919. :group 'org-tags
  2920. :type 'integer)
  2921. (defcustom org-auto-align-tags t
  2922. "Non-nil keeps tags aligned when modifying headlines.
  2923. Some operations (i.e. demoting) change the length of a headline and
  2924. therefore shift the tags around. With this option turned on, after
  2925. each such operation the tags are again aligned to `org-tags-column'."
  2926. :group 'org-tags
  2927. :type 'boolean)
  2928. (defcustom org-use-tag-inheritance t
  2929. "Non-nil means tags in levels apply also for sublevels.
  2930. When nil, only the tags directly given in a specific line apply there.
  2931. This may also be a list of tags that should be inherited, or a regexp that
  2932. matches tags that should be inherited. Additional control is possible
  2933. with the variable `org-tags-exclude-from-inheritance' which gives an
  2934. explicit list of tags to be excluded from inheritance, even if the value of
  2935. `org-use-tag-inheritance' would select it for inheritance.
  2936. If this option is t, a match early-on in a tree can lead to a large
  2937. number of matches in the subtree when constructing the agenda or creating
  2938. a sparse tree. If you only want to see the first match in a tree during
  2939. a search, check out the variable `org-tags-match-list-sublevels'."
  2940. :group 'org-tags
  2941. :type '(choice
  2942. (const :tag "Not" nil)
  2943. (const :tag "Always" t)
  2944. (repeat :tag "Specific tags" (string :tag "Tag"))
  2945. (regexp :tag "Tags matched by regexp")))
  2946. (defcustom org-tags-exclude-from-inheritance nil
  2947. "List of tags that should never be inherited.
  2948. This is a way to exclude a few tags from inheritance. For way to do
  2949. the opposite, to actively allow inheritance for selected tags,
  2950. see the variable `org-use-tag-inheritance'."
  2951. :group 'org-tags
  2952. :type '(repeat (string :tag "Tag")))
  2953. (defun org-tag-inherit-p (tag)
  2954. "Check if TAG is one that should be inherited."
  2955. (cond
  2956. ((member tag org-tags-exclude-from-inheritance) nil)
  2957. ((eq org-use-tag-inheritance t) t)
  2958. ((not org-use-tag-inheritance) nil)
  2959. ((stringp org-use-tag-inheritance)
  2960. (string-match org-use-tag-inheritance tag))
  2961. ((listp org-use-tag-inheritance)
  2962. (member tag org-use-tag-inheritance))
  2963. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2964. (defcustom org-tags-match-list-sublevels t
  2965. "Non-nil means list also sublevels of headlines matching a search.
  2966. This variable applies to tags/property searches, and also to stuck
  2967. projects because this search is based on a tags match as well.
  2968. When set to the symbol `indented', sublevels are indented with
  2969. leading dots.
  2970. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2971. the sublevels of a headline matching a tag search often also match
  2972. the same search. Listing all of them can create very long lists.
  2973. Setting this variable to nil causes subtrees of a match to be skipped.
  2974. This variable is semi-obsolete and probably should always be true. It
  2975. is better to limit inheritance to certain tags using the variables
  2976. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2977. :group 'org-tags
  2978. :type '(choice
  2979. (const :tag "No, don't list them" nil)
  2980. (const :tag "Yes, do list them" t)
  2981. (const :tag "List them, indented with leading dots" indented)))
  2982. (defcustom org-tags-sort-function nil
  2983. "When set, tags are sorted using this function as a comparator."
  2984. :group 'org-tags
  2985. :type '(choice
  2986. (const :tag "No sorting" nil)
  2987. (const :tag "Alphabetical" string<)
  2988. (const :tag "Reverse alphabetical" string>)
  2989. (function :tag "Custom function" nil)))
  2990. (defvar org-tags-history nil
  2991. "History of minibuffer reads for tags.")
  2992. (defvar org-last-tags-completion-table nil
  2993. "The last used completion table for tags.")
  2994. (defvar org-after-tags-change-hook nil
  2995. "Hook that is run after the tags in a line have changed.")
  2996. (defgroup org-properties nil
  2997. "Options concerning properties in Org-mode."
  2998. :tag "Org Properties"
  2999. :group 'org)
  3000. (defcustom org-property-format "%-10s %s"
  3001. "How property key/value pairs should be formatted by `indent-line'.
  3002. When `indent-line' hits a property definition, it will format the line
  3003. according to this format, mainly to make sure that the values are
  3004. lined-up with respect to each other."
  3005. :group 'org-properties
  3006. :type 'string)
  3007. (defcustom org-properties-postprocess-alist nil
  3008. "Alist of properties and functions to adjust inserted values.
  3009. Elements of this alist must be of the form
  3010. ([string] [function])
  3011. where [string] must be a property name and [function] must be a
  3012. lambda expression: this lambda expression must take one argument,
  3013. the value to adjust, and return the new value as a string.
  3014. For example, this element will allow the property \"Remaining\"
  3015. to be updated wrt the relation between the \"Effort\" property
  3016. and the clock summary:
  3017. ((\"Remaining\" (lambda(value)
  3018. (let ((clocksum (org-clock-sum-current-item))
  3019. (effort (org-duration-string-to-minutes
  3020. (org-entry-get (point) \"Effort\"))))
  3021. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3022. :group 'org-properties
  3023. :version "24.1"
  3024. :type '(alist :key-type (string :tag "Property")
  3025. :value-type (function :tag "Function")))
  3026. (defcustom org-use-property-inheritance nil
  3027. "Non-nil means properties apply also for sublevels.
  3028. This setting is chiefly used during property searches. Turning it on can
  3029. cause significant overhead when doing a search, which is why it is not
  3030. on by default.
  3031. When nil, only the properties directly given in the current entry count.
  3032. When t, every property is inherited. The value may also be a list of
  3033. properties that should have inheritance, or a regular expression matching
  3034. properties that should be inherited.
  3035. However, note that some special properties use inheritance under special
  3036. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3037. and the properties ending in \"_ALL\" when they are used as descriptor
  3038. for valid values of a property.
  3039. Note for programmers:
  3040. When querying an entry with `org-entry-get', you can control if inheritance
  3041. should be used. By default, `org-entry-get' looks only at the local
  3042. properties. You can request inheritance by setting the inherit argument
  3043. to t (to force inheritance) or to `selective' (to respect the setting
  3044. in this variable)."
  3045. :group 'org-properties
  3046. :type '(choice
  3047. (const :tag "Not" nil)
  3048. (const :tag "Always" t)
  3049. (repeat :tag "Specific properties" (string :tag "Property"))
  3050. (regexp :tag "Properties matched by regexp")))
  3051. (defun org-property-inherit-p (property)
  3052. "Check if PROPERTY is one that should be inherited."
  3053. (cond
  3054. ((eq org-use-property-inheritance t) t)
  3055. ((not org-use-property-inheritance) nil)
  3056. ((stringp org-use-property-inheritance)
  3057. (string-match org-use-property-inheritance property))
  3058. ((listp org-use-property-inheritance)
  3059. (member property org-use-property-inheritance))
  3060. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3061. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3062. "The default column format, if no other format has been defined.
  3063. This variable can be set on the per-file basis by inserting a line
  3064. #+COLUMNS: %25ITEM ....."
  3065. :group 'org-properties
  3066. :type 'string)
  3067. (defcustom org-columns-ellipses ".."
  3068. "The ellipses to be used when a field in column view is truncated.
  3069. When this is the empty string, as many characters as possible are shown,
  3070. but then there will be no visual indication that the field has been truncated.
  3071. When this is a string of length N, the last N characters of a truncated
  3072. field are replaced by this string. If the column is narrower than the
  3073. ellipses string, only part of the ellipses string will be shown."
  3074. :group 'org-properties
  3075. :type 'string)
  3076. (defcustom org-columns-modify-value-for-display-function nil
  3077. "Function that modifies values for display in column view.
  3078. For example, it can be used to cut out a certain part from a time stamp.
  3079. The function must take 2 arguments:
  3080. column-title The title of the column (*not* the property name)
  3081. value The value that should be modified.
  3082. The function should return the value that should be displayed,
  3083. or nil if the normal value should be used."
  3084. :group 'org-properties
  3085. :type '(choice (const nil) (function)))
  3086. (defcustom org-effort-property "Effort"
  3087. "The property that is being used to keep track of effort estimates.
  3088. Effort estimates given in this property need to have the format H:MM."
  3089. :group 'org-properties
  3090. :group 'org-progress
  3091. :type '(string :tag "Property"))
  3092. (defconst org-global-properties-fixed
  3093. '(("VISIBILITY_ALL" . "folded children content all")
  3094. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3095. "List of property/value pairs that can be inherited by any entry.
  3096. These are fixed values, for the preset properties. The user variable
  3097. that can be used to add to this list is `org-global-properties'.
  3098. The entries in this list are cons cells where the car is a property
  3099. name and cdr is a string with the value. If the value represents
  3100. multiple items like an \"_ALL\" property, separate the items by
  3101. spaces.")
  3102. (defcustom org-global-properties nil
  3103. "List of property/value pairs that can be inherited by any entry.
  3104. This list will be combined with the constant `org-global-properties-fixed'.
  3105. The entries in this list are cons cells where the car is a property
  3106. name and cdr is a string with the value.
  3107. You can set buffer-local values for the same purpose in the variable
  3108. `org-file-properties' this by adding lines like
  3109. #+PROPERTY: NAME VALUE"
  3110. :group 'org-properties
  3111. :type '(repeat
  3112. (cons (string :tag "Property")
  3113. (string :tag "Value"))))
  3114. (defvar org-file-properties nil
  3115. "List of property/value pairs that can be inherited by any entry.
  3116. Valid for the current buffer.
  3117. This variable is populated from #+PROPERTY lines.")
  3118. (make-variable-buffer-local 'org-file-properties)
  3119. (defgroup org-agenda nil
  3120. "Options concerning agenda views in Org-mode."
  3121. :tag "Org Agenda"
  3122. :group 'org)
  3123. (defvar org-category nil
  3124. "Variable used by org files to set a category for agenda display.
  3125. Such files should use a file variable to set it, for example
  3126. # -*- mode: org; org-category: \"ELisp\"
  3127. or contain a special line
  3128. #+CATEGORY: ELisp
  3129. If the file does not specify a category, then file's base name
  3130. is used instead.")
  3131. (make-variable-buffer-local 'org-category)
  3132. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3133. (defcustom org-agenda-files nil
  3134. "The files to be used for agenda display.
  3135. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3136. \\[org-remove-file]. You can also use customize to edit the list.
  3137. If an entry is a directory, all files in that directory that are matched by
  3138. `org-agenda-file-regexp' will be part of the file list.
  3139. If the value of the variable is not a list but a single file name, then
  3140. the list of agenda files is actually stored and maintained in that file, one
  3141. agenda file per line. In this file paths can be given relative to
  3142. `org-directory'. Tilde expansion and environment variable substitution
  3143. are also made."
  3144. :group 'org-agenda
  3145. :type '(choice
  3146. (repeat :tag "List of files and directories" file)
  3147. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3148. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3149. "Regular expression to match files for `org-agenda-files'.
  3150. If any element in the list in that variable contains a directory instead
  3151. of a normal file, all files in that directory that are matched by this
  3152. regular expression will be included."
  3153. :group 'org-agenda
  3154. :type 'regexp)
  3155. (defcustom org-agenda-text-search-extra-files nil
  3156. "List of extra files to be searched by text search commands.
  3157. These files will be searched in addition to the agenda files by the
  3158. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3159. Note that these files will only be searched for text search commands,
  3160. not for the other agenda views like todo lists, tag searches or the weekly
  3161. agenda. This variable is intended to list notes and possibly archive files
  3162. that should also be searched by these two commands.
  3163. In fact, if the first element in the list is the symbol `agenda-archives',
  3164. then all archive files of all agenda files will be added to the search
  3165. scope."
  3166. :group 'org-agenda
  3167. :type '(set :greedy t
  3168. (const :tag "Agenda Archives" agenda-archives)
  3169. (repeat :inline t (file))))
  3170. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3171. 'org-agenda-text-search-extra-files)
  3172. (defcustom org-agenda-skip-unavailable-files nil
  3173. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3174. A nil value means to remove them, after a query, from the list."
  3175. :group 'org-agenda
  3176. :type 'boolean)
  3177. (defcustom org-calendar-to-agenda-key [?c]
  3178. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3179. The command `org-calendar-goto-agenda' will be bound to this key. The
  3180. default is the character `c' because then `c' can be used to switch back and
  3181. forth between agenda and calendar."
  3182. :group 'org-agenda
  3183. :type 'sexp)
  3184. (defcustom org-calendar-insert-diary-entry-key [?i]
  3185. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3186. This option is irrelevant until `org-agenda-diary-file' has been configured
  3187. to point to an Org-mode file. When that is the case, the command
  3188. `org-agenda-diary-entry' will be bound to the key given here, by default
  3189. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3190. if you want to continue doing this, you need to change this to a different
  3191. key."
  3192. :group 'org-agenda
  3193. :type 'sexp)
  3194. (defcustom org-agenda-diary-file 'diary-file
  3195. "File to which to add new entries with the `i' key in agenda and calendar.
  3196. When this is the symbol `diary-file', the functionality in the Emacs
  3197. calendar will be used to add entries to the `diary-file'. But when this
  3198. points to a file, `org-agenda-diary-entry' will be used instead."
  3199. :group 'org-agenda
  3200. :type '(choice
  3201. (const :tag "The standard Emacs diary file" diary-file)
  3202. (file :tag "Special Org file diary entries")))
  3203. (eval-after-load "calendar"
  3204. '(progn
  3205. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3206. 'org-calendar-goto-agenda)
  3207. (add-hook 'calendar-mode-hook
  3208. (lambda ()
  3209. (unless (eq org-agenda-diary-file 'diary-file)
  3210. (define-key calendar-mode-map
  3211. org-calendar-insert-diary-entry-key
  3212. 'org-agenda-diary-entry))))))
  3213. (defgroup org-latex nil
  3214. "Options for embedding LaTeX code into Org-mode."
  3215. :tag "Org LaTeX"
  3216. :group 'org)
  3217. (defcustom org-format-latex-options
  3218. '(:foreground default :background default :scale 1.0
  3219. :html-foreground "Black" :html-background "Transparent"
  3220. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3221. "Options for creating images from LaTeX fragments.
  3222. This is a property list with the following properties:
  3223. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3224. `default' means use the foreground of the default face.
  3225. `auto' means use the foreground from the text face.
  3226. :background the background color, or \"Transparent\".
  3227. `default' means use the background of the default face.
  3228. `auto' means use the background from the text face.
  3229. :scale a scaling factor for the size of the images, to get more pixels
  3230. :html-foreground, :html-background, :html-scale
  3231. the same numbers for HTML export.
  3232. :matchers a list indicating which matchers should be used to
  3233. find LaTeX fragments. Valid members of this list are:
  3234. \"begin\" find environments
  3235. \"$1\" find single characters surrounded by $.$
  3236. \"$\" find math expressions surrounded by $...$
  3237. \"$$\" find math expressions surrounded by $$....$$
  3238. \"\\(\" find math expressions surrounded by \\(...\\)
  3239. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3240. :group 'org-latex
  3241. :type 'plist)
  3242. (defcustom org-format-latex-signal-error t
  3243. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3244. When nil, just push out a message."
  3245. :group 'org-latex
  3246. :version "24.1"
  3247. :type 'boolean)
  3248. (defcustom org-latex-to-mathml-jar-file nil
  3249. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3250. Use this to specify additional executable file say a jar file.
  3251. When using MathToWeb as the converter, specify the full-path to
  3252. your mathtoweb.jar file."
  3253. :group 'org-latex
  3254. :version "24.1"
  3255. :type '(choice
  3256. (const :tag "None" nil)
  3257. (file :tag "JAR file" :must-match t)))
  3258. (defcustom org-latex-to-mathml-convert-command nil
  3259. "Command to convert LaTeX fragments to MathML.
  3260. Replace format-specifiers in the command as noted below and use
  3261. `shell-command' to convert LaTeX to MathML.
  3262. %j: Executable file in fully expanded form as specified by
  3263. `org-latex-to-mathml-jar-file'.
  3264. %I: Input LaTeX file in fully expanded form
  3265. %o: Output MathML file
  3266. This command is used by `org-create-math-formula'.
  3267. When using MathToWeb as the converter, set this to
  3268. \"java -jar %j -unicode -force -df %o %I\"."
  3269. :group 'org-latex
  3270. :version "24.1"
  3271. :type '(choice
  3272. (const :tag "None" nil)
  3273. (string :tag "\nShell command")))
  3274. (defcustom org-latex-create-formula-image-program 'dvipng
  3275. "Program to convert LaTeX fragments with.
  3276. dvipng Process the LaTeX fragments to dvi file, then convert
  3277. dvi files to png files using dvipng.
  3278. This will also include processing of non-math environments.
  3279. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3280. to convert pdf files to png files"
  3281. :group 'org-latex
  3282. :version "24.1"
  3283. :type '(choice
  3284. (const :tag "dvipng" dvipng)
  3285. (const :tag "imagemagick" imagemagick)))
  3286. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3287. "Path to store latex preview images.
  3288. A relative path here creates many directories relative to the
  3289. processed org files paths. An absolute path puts all preview
  3290. images at the same place."
  3291. :group 'org-latex
  3292. :version "24.3"
  3293. :type 'string)
  3294. (defun org-format-latex-mathml-available-p ()
  3295. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3296. (save-match-data
  3297. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3298. org-latex-to-mathml-convert-command)
  3299. (let ((executable (car (split-string
  3300. org-latex-to-mathml-convert-command))))
  3301. (when (executable-find executable)
  3302. (if (string-match
  3303. "%j" org-latex-to-mathml-convert-command)
  3304. (file-readable-p org-latex-to-mathml-jar-file)
  3305. t))))))
  3306. (defcustom org-format-latex-header "\\documentclass{article}
  3307. \\usepackage[usenames]{color}
  3308. \[PACKAGES]
  3309. \[DEFAULT-PACKAGES]
  3310. \\pagestyle{empty} % do not remove
  3311. % The settings below are copied from fullpage.sty
  3312. \\setlength{\\textwidth}{\\paperwidth}
  3313. \\addtolength{\\textwidth}{-3cm}
  3314. \\setlength{\\oddsidemargin}{1.5cm}
  3315. \\addtolength{\\oddsidemargin}{-2.54cm}
  3316. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3317. \\setlength{\\textheight}{\\paperheight}
  3318. \\addtolength{\\textheight}{-\\headheight}
  3319. \\addtolength{\\textheight}{-\\headsep}
  3320. \\addtolength{\\textheight}{-\\footskip}
  3321. \\addtolength{\\textheight}{-3cm}
  3322. \\setlength{\\topmargin}{1.5cm}
  3323. \\addtolength{\\topmargin}{-2.54cm}"
  3324. "The document header used for processing LaTeX fragments.
  3325. It is imperative that this header make sure that no page number
  3326. appears on the page. The package defined in the variables
  3327. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3328. will either replace the placeholder \"[PACKAGES]\" in this
  3329. header, or they will be appended."
  3330. :group 'org-latex
  3331. :type 'string)
  3332. (defun org-set-packages-alist (var val)
  3333. "Set the packages alist and make sure it has 3 elements per entry."
  3334. (set var (mapcar (lambda (x)
  3335. (if (and (consp x) (= (length x) 2))
  3336. (list (car x) (nth 1 x) t)
  3337. x))
  3338. val)))
  3339. (defun org-get-packages-alist (var)
  3340. "Get the packages alist and make sure it has 3 elements per entry."
  3341. (mapcar (lambda (x)
  3342. (if (and (consp x) (= (length x) 2))
  3343. (list (car x) (nth 1 x) t)
  3344. x))
  3345. (default-value var)))
  3346. (defcustom org-latex-default-packages-alist
  3347. '(("AUTO" "inputenc" t)
  3348. ("T1" "fontenc" t)
  3349. ("" "fixltx2e" nil)
  3350. ("" "graphicx" t)
  3351. ("" "longtable" nil)
  3352. ("" "float" nil)
  3353. ("" "wrapfig" nil)
  3354. ("" "rotating" nil)
  3355. ("normalem" "ulem" t)
  3356. ("" "amsmath" t)
  3357. ("" "textcomp" t)
  3358. ("" "marvosym" t)
  3359. ("" "wasysym" t)
  3360. ("" "amssymb" t)
  3361. ("" "hyperref" nil)
  3362. "\\tolerance=1000")
  3363. "Alist of default packages to be inserted in the header.
  3364. Change this only if one of the packages here causes an
  3365. incompatibility with another package you are using.
  3366. The packages in this list are needed by one part or another of
  3367. Org mode to function properly:
  3368. - inputenc, fontenc: for basic font and character selection
  3369. - fixltx2e: Important patches of LaTeX itself
  3370. - graphicx: for including images
  3371. - longtable: For multipage tables
  3372. - float, wrapfig: for figure placement
  3373. - rotating: for sideways figures and tables
  3374. - ulem: for underline and strike-through
  3375. - amsmath: for subscript and superscript and math environments
  3376. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3377. for interpreting the entities in `org-entities'. You can skip
  3378. some of these packages if you don't use any of their symbols.
  3379. - hyperref: for cross references
  3380. Therefore you should not modify this variable unless you know
  3381. what you are doing. The one reason to change it anyway is that
  3382. you might be loading some other package that conflicts with one
  3383. of the default packages. Each element is either a cell or
  3384. a string.
  3385. A cell is of the format:
  3386. \( \"options\" \"package\" SNIPPET-FLAG).
  3387. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3388. when compiling LaTeX snippets into images for inclusion into
  3389. non-LaTeX output.
  3390. A string will be inserted as-is in the header of the document."
  3391. :group 'org-latex
  3392. :group 'org-export-latex
  3393. :set 'org-set-packages-alist
  3394. :get 'org-get-packages-alist
  3395. :version "24.1"
  3396. :type '(repeat
  3397. (choice
  3398. (list :tag "options/package pair"
  3399. (string :tag "options")
  3400. (string :tag "package")
  3401. (boolean :tag "Snippet"))
  3402. (string :tag "A line of LaTeX"))))
  3403. (defcustom org-latex-packages-alist nil
  3404. "Alist of packages to be inserted in every LaTeX header.
  3405. These will be inserted after `org-latex-default-packages-alist'.
  3406. Each element is either a cell or a string.
  3407. A cell is of the format:
  3408. \(\"options\" \"package\" SNIPPET-FLAG)
  3409. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3410. needed when turning LaTeX snippets into images for inclusion into
  3411. non-LaTeX output.
  3412. A string will be inserted as-is in the header of the document.
  3413. Make sure that you only list packages here which:
  3414. - you want in every file;
  3415. - do not conflict with the setup in `org-format-latex-header';
  3416. - do not conflict with the default packages in
  3417. `org-latex-default-packages-alist'."
  3418. :group 'org-latex
  3419. :group 'org-export-latex
  3420. :set 'org-set-packages-alist
  3421. :get 'org-get-packages-alist
  3422. :type '(repeat
  3423. (choice
  3424. (list :tag "options/package pair"
  3425. (string :tag "options")
  3426. (string :tag "package")
  3427. (boolean :tag "Snippet"))
  3428. (string :tag "A line of LaTeX"))))
  3429. (defgroup org-appearance nil
  3430. "Settings for Org-mode appearance."
  3431. :tag "Org Appearance"
  3432. :group 'org)
  3433. (defcustom org-level-color-stars-only nil
  3434. "Non-nil means fontify only the stars in each headline.
  3435. When nil, the entire headline is fontified.
  3436. Changing it requires restart of `font-lock-mode' to become effective
  3437. also in regions already fontified."
  3438. :group 'org-appearance
  3439. :type 'boolean)
  3440. (defcustom org-hide-leading-stars nil
  3441. "Non-nil means hide the first N-1 stars in a headline.
  3442. This works by using the face `org-hide' for these stars. This
  3443. face is white for a light background, and black for a dark
  3444. background. You may have to customize the face `org-hide' to
  3445. make this work.
  3446. Changing it requires restart of `font-lock-mode' to become effective
  3447. also in regions already fontified.
  3448. You may also set this on a per-file basis by adding one of the following
  3449. lines to the buffer:
  3450. #+STARTUP: hidestars
  3451. #+STARTUP: showstars"
  3452. :group 'org-appearance
  3453. :type 'boolean)
  3454. (defcustom org-hidden-keywords nil
  3455. "List of symbols corresponding to keywords to be hidden the org buffer.
  3456. For example, a value '(title) for this list will make the document's title
  3457. appear in the buffer without the initial #+TITLE: keyword."
  3458. :group 'org-appearance
  3459. :version "24.1"
  3460. :type '(set (const :tag "#+AUTHOR" author)
  3461. (const :tag "#+DATE" date)
  3462. (const :tag "#+EMAIL" email)
  3463. (const :tag "#+TITLE" title)))
  3464. (defcustom org-custom-properties nil
  3465. "List of properties (as strings) with a special meaning.
  3466. The default use of these custom properties is to let the user
  3467. hide them with `org-toggle-custom-properties-visibility'."
  3468. :group 'org-properties
  3469. :group 'org-appearance
  3470. :version "24.3"
  3471. :type '(repeat (string :tag "Property Name")))
  3472. (defcustom org-fontify-done-headline nil
  3473. "Non-nil means change the face of a headline if it is marked DONE.
  3474. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3475. When this is non-nil, the headline after the keyword is set to the
  3476. `org-headline-done' as an additional indication."
  3477. :group 'org-appearance
  3478. :type 'boolean)
  3479. (defcustom org-fontify-emphasized-text t
  3480. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3481. Changing this variable requires a restart of Emacs to take effect."
  3482. :group 'org-appearance
  3483. :type 'boolean)
  3484. (defcustom org-fontify-whole-heading-line nil
  3485. "Non-nil means fontify the whole line for headings.
  3486. This is useful when setting a background color for the
  3487. org-level-* faces."
  3488. :group 'org-appearance
  3489. :type 'boolean)
  3490. (defcustom org-highlight-latex-and-related nil
  3491. "Non-nil means highlight LaTeX related syntax in the buffer.
  3492. When non nil, the value should be a list containing any of the
  3493. following symbols:
  3494. `latex' Highlight LaTeX snippets and environments.
  3495. `script' Highlight subscript and superscript.
  3496. `entities' Highlight entities."
  3497. :group 'org-appearance
  3498. :version "24.4"
  3499. :package-version '(Org . "8.0")
  3500. :type '(choice
  3501. (const :tag "No highlighting" nil)
  3502. (set :greedy t :tag "Highlight"
  3503. (const :tag "LaTeX snippets and environments" latex)
  3504. (const :tag "Subscript and superscript" script)
  3505. (const :tag "Entities" entities))))
  3506. (defcustom org-hide-emphasis-markers nil
  3507. "Non-nil mean font-lock should hide the emphasis marker characters."
  3508. :group 'org-appearance
  3509. :type 'boolean)
  3510. (defcustom org-pretty-entities nil
  3511. "Non-nil means show entities as UTF8 characters.
  3512. When nil, the \\name form remains in the buffer."
  3513. :group 'org-appearance
  3514. :version "24.1"
  3515. :type 'boolean)
  3516. (defcustom org-pretty-entities-include-sub-superscripts t
  3517. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3518. :group 'org-appearance
  3519. :version "24.1"
  3520. :type 'boolean)
  3521. (defvar org-emph-re nil
  3522. "Regular expression for matching emphasis.
  3523. After a match, the match groups contain these elements:
  3524. 0 The match of the full regular expression, including the characters
  3525. before and after the proper match
  3526. 1 The character before the proper match, or empty at beginning of line
  3527. 2 The proper match, including the leading and trailing markers
  3528. 3 The leading marker like * or /, indicating the type of highlighting
  3529. 4 The text between the emphasis markers, not including the markers
  3530. 5 The character after the match, empty at the end of a line")
  3531. (defvar org-verbatim-re nil
  3532. "Regular expression for matching verbatim text.")
  3533. (defvar org-emphasis-regexp-components) ; defined just below
  3534. (defvar org-emphasis-alist) ; defined just below
  3535. (defun org-set-emph-re (var val)
  3536. "Set variable and compute the emphasis regular expression."
  3537. (set var val)
  3538. (when (and (boundp 'org-emphasis-alist)
  3539. (boundp 'org-emphasis-regexp-components)
  3540. org-emphasis-alist org-emphasis-regexp-components)
  3541. (let* ((e org-emphasis-regexp-components)
  3542. (pre (car e))
  3543. (post (nth 1 e))
  3544. (border (nth 2 e))
  3545. (body (nth 3 e))
  3546. (nl (nth 4 e))
  3547. (body1 (concat body "*?"))
  3548. (markers (mapconcat 'car org-emphasis-alist ""))
  3549. (vmarkers (mapconcat
  3550. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3551. org-emphasis-alist "")))
  3552. ;; make sure special characters appear at the right position in the class
  3553. (if (string-match "\\^" markers)
  3554. (setq markers (concat (replace-match "" t t markers) "^")))
  3555. (if (string-match "-" markers)
  3556. (setq markers (concat (replace-match "" t t markers) "-")))
  3557. (if (string-match "\\^" vmarkers)
  3558. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3559. (if (string-match "-" vmarkers)
  3560. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3561. (if (> nl 0)
  3562. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3563. (int-to-string nl) "\\}")))
  3564. ;; Make the regexp
  3565. (setq org-emph-re
  3566. (concat "\\([" pre "]\\|^\\)"
  3567. "\\("
  3568. "\\([" markers "]\\)"
  3569. "\\("
  3570. "[^" border "]\\|"
  3571. "[^" border "]"
  3572. body1
  3573. "[^" border "]"
  3574. "\\)"
  3575. "\\3\\)"
  3576. "\\([" post "]\\|$\\)"))
  3577. (setq org-verbatim-re
  3578. (concat "\\([" pre "]\\|^\\)"
  3579. "\\("
  3580. "\\([" vmarkers "]\\)"
  3581. "\\("
  3582. "[^" border "]\\|"
  3583. "[^" border "]"
  3584. body1
  3585. "[^" border "]"
  3586. "\\)"
  3587. "\\3\\)"
  3588. "\\([" post "]\\|$\\)")))))
  3589. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3590. ;; set this option proved cumbersome. See this message/thread:
  3591. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3592. (defvar org-emphasis-regexp-components
  3593. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3594. "Components used to build the regular expression for emphasis.
  3595. This is a list with five entries. Terminology: In an emphasis string
  3596. like \" *strong word* \", we call the initial space PREMATCH, the final
  3597. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3598. and \"trong wor\" is the body. The different components in this variable
  3599. specify what is allowed/forbidden in each part:
  3600. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3601. post Chars allowed as postmatch. End of line will be allowed too.
  3602. border The chars *forbidden* as border characters.
  3603. body-regexp A regexp like \".\" to match a body character. Don't use
  3604. non-shy groups here, and don't allow newline here.
  3605. newline The maximum number of newlines allowed in an emphasis exp.
  3606. You need to reload Org or to restart Emacs after customizing this.")
  3607. (defcustom org-emphasis-alist
  3608. `(("*" bold)
  3609. ("/" italic)
  3610. ("_" underline)
  3611. ("=" org-verbatim verbatim)
  3612. ("~" org-code verbatim)
  3613. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3614. "Alist of characters and faces to emphasize text.
  3615. Text starting and ending with a special character will be emphasized,
  3616. for example *bold*, _underlined_ and /italic/. This variable sets the
  3617. marker characters and the face to be used by font-lock for highlighting
  3618. in Org-mode Emacs buffers.
  3619. You need to reload Org or to restart Emacs after customizing this."
  3620. :group 'org-appearance
  3621. :set 'org-set-emph-re
  3622. :version "24.4"
  3623. :package-version '(Org . "8.0")
  3624. :type '(repeat
  3625. (list
  3626. (string :tag "Marker character")
  3627. (choice
  3628. (face :tag "Font-lock-face")
  3629. (plist :tag "Face property list"))
  3630. (option (const verbatim)))))
  3631. (defvar org-protecting-blocks
  3632. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3633. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3634. This is needed for font-lock setup.")
  3635. ;;; Miscellaneous options
  3636. (defgroup org-completion nil
  3637. "Completion in Org-mode."
  3638. :tag "Org Completion"
  3639. :group 'org)
  3640. (defcustom org-completion-use-ido nil
  3641. "Non-nil means use ido completion wherever possible.
  3642. Note that `ido-mode' must be active for this variable to be relevant.
  3643. If you decide to turn this variable on, you might well want to turn off
  3644. `org-outline-path-complete-in-steps'.
  3645. See also `org-completion-use-iswitchb'."
  3646. :group 'org-completion
  3647. :type 'boolean)
  3648. (defcustom org-completion-use-iswitchb nil
  3649. "Non-nil means use iswitchb completion wherever possible.
  3650. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3651. If you decide to turn this variable on, you might well want to turn off
  3652. `org-outline-path-complete-in-steps'.
  3653. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3654. :group 'org-completion
  3655. :type 'boolean)
  3656. (defcustom org-completion-fallback-command 'hippie-expand
  3657. "The expansion command called by \\[pcomplete] in normal context.
  3658. Normal means, no org-mode-specific context."
  3659. :group 'org-completion
  3660. :type 'function)
  3661. ;;; Functions and variables from their packages
  3662. ;; Declared here to avoid compiler warnings
  3663. ;; XEmacs only
  3664. (defvar outline-mode-menu-heading)
  3665. (defvar outline-mode-menu-show)
  3666. (defvar outline-mode-menu-hide)
  3667. (defvar zmacs-regions) ; XEmacs regions
  3668. ;; Emacs only
  3669. (defvar mark-active)
  3670. ;; Various packages
  3671. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3672. (declare-function calendar-forward-day "cal-move" (arg))
  3673. (declare-function calendar-goto-date "cal-move" (date))
  3674. (declare-function calendar-goto-today "cal-move" ())
  3675. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3676. (defvar calc-embedded-close-formula)
  3677. (defvar calc-embedded-open-formula)
  3678. (declare-function cdlatex-tab "ext:cdlatex" ())
  3679. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3680. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3681. (defvar font-lock-unfontify-region-function)
  3682. (declare-function iswitchb-read-buffer "iswitchb"
  3683. (prompt &optional default require-match start matches-set))
  3684. (defvar iswitchb-temp-buflist)
  3685. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3686. (defvar org-agenda-tags-todo-honor-ignore-options)
  3687. (declare-function org-agenda-skip "org-agenda" ())
  3688. (declare-function
  3689. org-agenda-format-item "org-agenda"
  3690. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3691. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3692. (declare-function org-agenda-change-all-lines "org-agenda"
  3693. (newhead hdmarker &optional fixface just-this))
  3694. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3695. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3696. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3697. (beg end))
  3698. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3699. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3700. "org-agenda" (&optional end))
  3701. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3702. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3703. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3704. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3705. (declare-function org-indent-mode "org-indent" (&optional arg))
  3706. (declare-function parse-time-string "parse-time" (string))
  3707. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3708. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3709. (defvar remember-data-file)
  3710. (defvar texmathp-why)
  3711. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3712. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3713. (defvar org-latex-regexps)
  3714. ;;; Autoload and prepare some org modules
  3715. ;; Some table stuff that needs to be defined here, because it is used
  3716. ;; by the functions setting up org-mode or checking for table context.
  3717. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3718. "Detect an org-type or table-type table.")
  3719. (defconst org-table-line-regexp "^[ \t]*|"
  3720. "Detect an org-type table line.")
  3721. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3722. "Detect an org-type table line.")
  3723. (defconst org-table-hline-regexp "^[ \t]*|-"
  3724. "Detect an org-type table hline.")
  3725. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3726. "Detect a table-type table hline.")
  3727. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3728. "Detect the first line outside a table when searching from within it.
  3729. This works for both table types.")
  3730. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3731. "Detect a #+TBLFM line.")
  3732. ;;;###autoload
  3733. (defun turn-on-orgtbl ()
  3734. "Unconditionally turn on `orgtbl-mode'."
  3735. (require 'org-table)
  3736. (orgtbl-mode 1))
  3737. (defun org-at-table-p (&optional table-type)
  3738. "Return t if the cursor is inside an org-type table.
  3739. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3740. (if org-enable-table-editor
  3741. (save-excursion
  3742. (beginning-of-line 1)
  3743. (looking-at (if table-type org-table-any-line-regexp
  3744. org-table-line-regexp)))
  3745. nil))
  3746. (defsubst org-table-p () (org-at-table-p))
  3747. (defun org-at-table.el-p ()
  3748. "Return t if and only if we are at a table.el table."
  3749. (and (org-at-table-p 'any)
  3750. (save-excursion
  3751. (goto-char (org-table-begin 'any))
  3752. (looking-at org-table1-hline-regexp))))
  3753. (defun org-table-recognize-table.el ()
  3754. "If there is a table.el table nearby, recognize it and move into it."
  3755. (if org-table-tab-recognizes-table.el
  3756. (if (org-at-table.el-p)
  3757. (progn
  3758. (beginning-of-line 1)
  3759. (if (looking-at org-table-dataline-regexp)
  3760. nil
  3761. (if (looking-at org-table1-hline-regexp)
  3762. (progn
  3763. (beginning-of-line 2)
  3764. (if (looking-at org-table-any-border-regexp)
  3765. (beginning-of-line -1)))))
  3766. (if (re-search-forward "|" (org-table-end t) t)
  3767. (progn
  3768. (require 'table)
  3769. (if (table--at-cell-p (point))
  3770. t
  3771. (message "recognizing table.el table...")
  3772. (table-recognize-table)
  3773. (message "recognizing table.el table...done")))
  3774. (error "This should not happen"))
  3775. t)
  3776. nil)
  3777. nil))
  3778. (defun org-at-table-hline-p ()
  3779. "Return t if the cursor is inside a hline in a table."
  3780. (if org-enable-table-editor
  3781. (save-excursion
  3782. (beginning-of-line 1)
  3783. (looking-at org-table-hline-regexp))
  3784. nil))
  3785. (defun org-table-map-tables (function &optional quietly)
  3786. "Apply FUNCTION to the start of all tables in the buffer."
  3787. (save-excursion
  3788. (save-restriction
  3789. (widen)
  3790. (goto-char (point-min))
  3791. (while (re-search-forward org-table-any-line-regexp nil t)
  3792. (unless quietly
  3793. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3794. (beginning-of-line 1)
  3795. (when (and (looking-at org-table-line-regexp)
  3796. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3797. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3798. (save-excursion (funcall function))
  3799. (or (looking-at org-table-line-regexp)
  3800. (forward-char 1)))
  3801. (re-search-forward org-table-any-border-regexp nil 1))))
  3802. (unless quietly (message "Mapping tables: done")))
  3803. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3804. (declare-function org-clock-update-mode-line "org-clock" ())
  3805. (declare-function org-resolve-clocks "org-clock"
  3806. (&optional also-non-dangling-p prompt last-valid))
  3807. (defun org-at-TBLFM-p (&optional pos)
  3808. "Return t when point (or POS) is in #+TBLFM line."
  3809. (save-excursion
  3810. (let ((pos pos)))
  3811. (goto-char (or pos (point)))
  3812. (beginning-of-line 1)
  3813. (looking-at org-TBLFM-regexp)))
  3814. (defvar org-clock-start-time)
  3815. (defvar org-clock-marker (make-marker)
  3816. "Marker recording the last clock-in.")
  3817. (defvar org-clock-hd-marker (make-marker)
  3818. "Marker recording the last clock-in, but the headline position.")
  3819. (defvar org-clock-heading ""
  3820. "The heading of the current clock entry.")
  3821. (defun org-clock-is-active ()
  3822. "Return the buffer where the clock is currently running.
  3823. Return nil if no clock is running."
  3824. (marker-buffer org-clock-marker))
  3825. (defun org-check-running-clock ()
  3826. "Check if the current buffer contains the running clock.
  3827. If yes, offer to stop it and to save the buffer with the changes."
  3828. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3829. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3830. (buffer-name))))
  3831. (org-clock-out)
  3832. (when (y-or-n-p "Save changed buffer?")
  3833. (save-buffer))))
  3834. (defun org-clocktable-try-shift (dir n)
  3835. "Check if this line starts a clock table, if yes, shift the time block."
  3836. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3837. (org-clocktable-shift dir n)))
  3838. ;;;###autoload
  3839. (defun org-clock-persistence-insinuate ()
  3840. "Set up hooks for clock persistence."
  3841. (require 'org-clock)
  3842. (add-hook 'org-mode-hook 'org-clock-load)
  3843. (add-hook 'kill-emacs-hook 'org-clock-save))
  3844. (defgroup org-archive nil
  3845. "Options concerning archiving in Org-mode."
  3846. :tag "Org Archive"
  3847. :group 'org-structure)
  3848. (defcustom org-archive-location "%s_archive::"
  3849. "The location where subtrees should be archived.
  3850. The value of this variable is a string, consisting of two parts,
  3851. separated by a double-colon. The first part is a filename and
  3852. the second part is a headline.
  3853. When the filename is omitted, archiving happens in the same file.
  3854. %s in the filename will be replaced by the current file
  3855. name (without the directory part). Archiving to a different file
  3856. is useful to keep archived entries from contributing to the
  3857. Org-mode Agenda.
  3858. The archived entries will be filed as subtrees of the specified
  3859. headline. When the headline is omitted, the subtrees are simply
  3860. filed away at the end of the file, as top-level entries. Also in
  3861. the heading you can use %s to represent the file name, this can be
  3862. useful when using the same archive for a number of different files.
  3863. Here are a few examples:
  3864. \"%s_archive::\"
  3865. If the current file is Projects.org, archive in file
  3866. Projects.org_archive, as top-level trees. This is the default.
  3867. \"::* Archived Tasks\"
  3868. Archive in the current file, under the top-level headline
  3869. \"* Archived Tasks\".
  3870. \"~/org/archive.org::\"
  3871. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3872. \"~/org/archive.org::* From %s\"
  3873. Archive in file ~/org/archive.org (absolute path), under headlines
  3874. \"From FILENAME\" where file name is the current file name.
  3875. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3876. The \"datetree/\" string is special, signifying to archive
  3877. items to the datetree. Items are placed in either the CLOSED
  3878. date of the item, or the current date if there is no CLOSED date.
  3879. The heading will be a subentry to the current date. There doesn't
  3880. need to be a heading, but there always needs to be a slash after
  3881. datetree. For example, to store archived items directly in the
  3882. datetree, use \"~/org/datetree.org::datetree/\".
  3883. \"basement::** Finished Tasks\"
  3884. Archive in file ./basement (relative path), as level 3 trees
  3885. below the level 2 heading \"** Finished Tasks\".
  3886. You may set this option on a per-file basis by adding to the buffer a
  3887. line like
  3888. #+ARCHIVE: basement::** Finished Tasks
  3889. You may also define it locally for a subtree by setting an ARCHIVE property
  3890. in the entry. If such a property is found in an entry, or anywhere up
  3891. the hierarchy, it will be used."
  3892. :group 'org-archive
  3893. :type 'string)
  3894. (defcustom org-archive-tag "ARCHIVE"
  3895. "The tag that marks a subtree as archived.
  3896. An archived subtree does not open during visibility cycling, and does
  3897. not contribute to the agenda listings.
  3898. After changing this, font-lock must be restarted in the relevant buffers to
  3899. get the proper fontification."
  3900. :group 'org-archive
  3901. :group 'org-keywords
  3902. :type 'string)
  3903. (defcustom org-agenda-skip-archived-trees t
  3904. "Non-nil means the agenda will skip any items located in archived trees.
  3905. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3906. variable is no longer recommended, you should leave it at the value t.
  3907. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3908. :group 'org-archive
  3909. :group 'org-agenda-skip
  3910. :type 'boolean)
  3911. (defcustom org-columns-skip-archived-trees t
  3912. "Non-nil means ignore archived trees when creating column view."
  3913. :group 'org-archive
  3914. :group 'org-properties
  3915. :type 'boolean)
  3916. (defcustom org-cycle-open-archived-trees nil
  3917. "Non-nil means `org-cycle' will open archived trees.
  3918. An archived tree is a tree marked with the tag ARCHIVE.
  3919. When nil, archived trees will stay folded. You can still open them with
  3920. normal outline commands like `show-all', but not with the cycling commands."
  3921. :group 'org-archive
  3922. :group 'org-cycle
  3923. :type 'boolean)
  3924. (defcustom org-sparse-tree-open-archived-trees nil
  3925. "Non-nil means sparse tree construction shows matches in archived trees.
  3926. When nil, matches in these trees are highlighted, but the trees are kept in
  3927. collapsed state."
  3928. :group 'org-archive
  3929. :group 'org-sparse-trees
  3930. :type 'boolean)
  3931. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3932. "The default date type when building a sparse tree.
  3933. When this is nil, a date is a scheduled or a deadline timestamp.
  3934. Otherwise, these types are allowed:
  3935. all: all timestamps
  3936. active: only active timestamps (<...>)
  3937. inactive: only inactive timestamps (<...)
  3938. scheduled: only scheduled timestamps
  3939. deadline: only deadline timestamps"
  3940. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3941. (const :tag "All timestamps" all)
  3942. (const :tag "Only active timestamps" active)
  3943. (const :tag "Only inactive timestamps" inactive)
  3944. (const :tag "Only scheduled timestamps" scheduled)
  3945. (const :tag "Only deadline timestamps" deadline)
  3946. (const :tag "Only closed timestamps" closed))
  3947. :version "24.3"
  3948. :group 'org-sparse-trees)
  3949. (defun org-cycle-hide-archived-subtrees (state)
  3950. "Re-hide all archived subtrees after a visibility state change."
  3951. (when (and (not org-cycle-open-archived-trees)
  3952. (not (memq state '(overview folded))))
  3953. (save-excursion
  3954. (let* ((globalp (memq state '(contents all)))
  3955. (beg (if globalp (point-min) (point)))
  3956. (end (if globalp (point-max) (org-end-of-subtree t))))
  3957. (org-hide-archived-subtrees beg end)
  3958. (goto-char beg)
  3959. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3960. (message "%s" (substitute-command-keys
  3961. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3962. (defun org-force-cycle-archived ()
  3963. "Cycle subtree even if it is archived."
  3964. (interactive)
  3965. (setq this-command 'org-cycle)
  3966. (let ((org-cycle-open-archived-trees t))
  3967. (call-interactively 'org-cycle)))
  3968. (defun org-hide-archived-subtrees (beg end)
  3969. "Re-hide all archived subtrees after a visibility state change."
  3970. (save-excursion
  3971. (let* ((re (concat ":" org-archive-tag ":")))
  3972. (goto-char beg)
  3973. (while (re-search-forward re end t)
  3974. (when (org-at-heading-p)
  3975. (org-flag-subtree t)
  3976. (org-end-of-subtree t))))))
  3977. (declare-function outline-end-of-heading "outline" ())
  3978. (declare-function outline-flag-region "outline" (from to flag))
  3979. (defun org-flag-subtree (flag)
  3980. (save-excursion
  3981. (org-back-to-heading t)
  3982. (outline-end-of-heading)
  3983. (outline-flag-region (point)
  3984. (progn (org-end-of-subtree t) (point))
  3985. flag)))
  3986. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3987. ;; Declare Column View Code
  3988. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3989. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3990. (declare-function org-columns-compute "org-colview" (property))
  3991. ;; Declare ID code
  3992. (declare-function org-id-store-link "org-id")
  3993. (declare-function org-id-locations-load "org-id")
  3994. (declare-function org-id-locations-save "org-id")
  3995. (defvar org-id-track-globally)
  3996. ;;; Variables for pre-computed regular expressions, all buffer local
  3997. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3998. "Matches first line of a hidden block.")
  3999. (make-variable-buffer-local 'org-drawer-regexp)
  4000. (defvar org-todo-regexp nil
  4001. "Matches any of the TODO state keywords.")
  4002. (make-variable-buffer-local 'org-todo-regexp)
  4003. (defvar org-not-done-regexp nil
  4004. "Matches any of the TODO state keywords except the last one.")
  4005. (make-variable-buffer-local 'org-not-done-regexp)
  4006. (defvar org-not-done-heading-regexp nil
  4007. "Matches a TODO headline that is not done.")
  4008. (make-variable-buffer-local 'org-not-done-regexp)
  4009. (defvar org-todo-line-regexp nil
  4010. "Matches a headline and puts TODO state into group 2 if present.")
  4011. (make-variable-buffer-local 'org-todo-line-regexp)
  4012. (defvar org-complex-heading-regexp nil
  4013. "Matches a headline and puts everything into groups:
  4014. group 1: the stars
  4015. group 2: The todo keyword, maybe
  4016. group 3: Priority cookie
  4017. group 4: True headline
  4018. group 5: Tags")
  4019. (make-variable-buffer-local 'org-complex-heading-regexp)
  4020. (defvar org-complex-heading-regexp-format nil
  4021. "Printf format to make regexp to match an exact headline.
  4022. This regexp will match the headline of any node which has the
  4023. exact headline text that is put into the format, but may have any
  4024. TODO state, priority and tags.")
  4025. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4026. (defvar org-todo-line-tags-regexp nil
  4027. "Matches a headline and puts TODO state into group 2 if present.
  4028. Also put tags into group 4 if tags are present.")
  4029. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4030. (defvar org-ds-keyword-length 12
  4031. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4032. (make-variable-buffer-local 'org-ds-keyword-length)
  4033. (defvar org-deadline-regexp nil
  4034. "Matches the DEADLINE keyword.")
  4035. (make-variable-buffer-local 'org-deadline-regexp)
  4036. (defvar org-deadline-time-regexp nil
  4037. "Matches the DEADLINE keyword together with a time stamp.")
  4038. (make-variable-buffer-local 'org-deadline-time-regexp)
  4039. (defvar org-deadline-time-hour-regexp nil
  4040. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4041. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4042. (defvar org-deadline-line-regexp nil
  4043. "Matches the DEADLINE keyword and the rest of the line.")
  4044. (make-variable-buffer-local 'org-deadline-line-regexp)
  4045. (defvar org-scheduled-regexp nil
  4046. "Matches the SCHEDULED keyword.")
  4047. (make-variable-buffer-local 'org-scheduled-regexp)
  4048. (defvar org-scheduled-time-regexp nil
  4049. "Matches the SCHEDULED keyword together with a time stamp.")
  4050. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4051. (defvar org-scheduled-time-hour-regexp nil
  4052. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4053. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4054. (defvar org-closed-time-regexp nil
  4055. "Matches the CLOSED keyword together with a time stamp.")
  4056. (make-variable-buffer-local 'org-closed-time-regexp)
  4057. (defvar org-keyword-time-regexp nil
  4058. "Matches any of the 4 keywords, together with the time stamp.")
  4059. (make-variable-buffer-local 'org-keyword-time-regexp)
  4060. (defvar org-keyword-time-not-clock-regexp nil
  4061. "Matches any of the 3 keywords, together with the time stamp.")
  4062. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4063. (defvar org-maybe-keyword-time-regexp nil
  4064. "Matches a timestamp, possibly preceded by a keyword.")
  4065. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4066. (defvar org-all-time-keywords nil
  4067. "List of time keywords.")
  4068. (make-variable-buffer-local 'org-all-time-keywords)
  4069. (defconst org-plain-time-of-day-regexp
  4070. (concat
  4071. "\\(\\<[012]?[0-9]"
  4072. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4073. "\\(--?"
  4074. "\\(\\<[012]?[0-9]"
  4075. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4076. "\\)?")
  4077. "Regular expression to match a plain time or time range.
  4078. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4079. groups carry important information:
  4080. 0 the full match
  4081. 1 the first time, range or not
  4082. 8 the second time, if it is a range.")
  4083. (defconst org-plain-time-extension-regexp
  4084. (concat
  4085. "\\(\\<[012]?[0-9]"
  4086. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4087. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4088. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4089. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4090. groups carry important information:
  4091. 0 the full match
  4092. 7 hours of duration
  4093. 9 minutes of duration")
  4094. (defconst org-stamp-time-of-day-regexp
  4095. (concat
  4096. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4097. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4098. "\\(--?"
  4099. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4100. "Regular expression to match a timestamp time or time range.
  4101. After a match, the following groups carry important information:
  4102. 0 the full match
  4103. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4104. 2 the first time, range or not
  4105. 4 the second time, if it is a range.")
  4106. (defconst org-startup-options
  4107. '(("fold" org-startup-folded t)
  4108. ("overview" org-startup-folded t)
  4109. ("nofold" org-startup-folded nil)
  4110. ("showall" org-startup-folded nil)
  4111. ("showeverything" org-startup-folded showeverything)
  4112. ("content" org-startup-folded content)
  4113. ("indent" org-startup-indented t)
  4114. ("noindent" org-startup-indented nil)
  4115. ("hidestars" org-hide-leading-stars t)
  4116. ("showstars" org-hide-leading-stars nil)
  4117. ("odd" org-odd-levels-only t)
  4118. ("oddeven" org-odd-levels-only nil)
  4119. ("align" org-startup-align-all-tables t)
  4120. ("noalign" org-startup-align-all-tables nil)
  4121. ("inlineimages" org-startup-with-inline-images t)
  4122. ("noinlineimages" org-startup-with-inline-images nil)
  4123. ("latexpreview" org-startup-with-latex-preview t)
  4124. ("nolatexpreview" org-startup-with-latex-preview nil)
  4125. ("customtime" org-display-custom-times t)
  4126. ("logdone" org-log-done time)
  4127. ("lognotedone" org-log-done note)
  4128. ("nologdone" org-log-done nil)
  4129. ("lognoteclock-out" org-log-note-clock-out t)
  4130. ("nolognoteclock-out" org-log-note-clock-out nil)
  4131. ("logrepeat" org-log-repeat state)
  4132. ("lognoterepeat" org-log-repeat note)
  4133. ("logdrawer" org-log-into-drawer t)
  4134. ("nologdrawer" org-log-into-drawer nil)
  4135. ("logstatesreversed" org-log-states-order-reversed t)
  4136. ("nologstatesreversed" org-log-states-order-reversed nil)
  4137. ("nologrepeat" org-log-repeat nil)
  4138. ("logreschedule" org-log-reschedule time)
  4139. ("lognotereschedule" org-log-reschedule note)
  4140. ("nologreschedule" org-log-reschedule nil)
  4141. ("logredeadline" org-log-redeadline time)
  4142. ("lognoteredeadline" org-log-redeadline note)
  4143. ("nologredeadline" org-log-redeadline nil)
  4144. ("logrefile" org-log-refile time)
  4145. ("lognoterefile" org-log-refile note)
  4146. ("nologrefile" org-log-refile nil)
  4147. ("fninline" org-footnote-define-inline t)
  4148. ("nofninline" org-footnote-define-inline nil)
  4149. ("fnlocal" org-footnote-section nil)
  4150. ("fnauto" org-footnote-auto-label t)
  4151. ("fnprompt" org-footnote-auto-label nil)
  4152. ("fnconfirm" org-footnote-auto-label confirm)
  4153. ("fnplain" org-footnote-auto-label plain)
  4154. ("fnadjust" org-footnote-auto-adjust t)
  4155. ("nofnadjust" org-footnote-auto-adjust nil)
  4156. ("constcgs" constants-unit-system cgs)
  4157. ("constSI" constants-unit-system SI)
  4158. ("noptag" org-tag-persistent-alist nil)
  4159. ("hideblocks" org-hide-block-startup t)
  4160. ("nohideblocks" org-hide-block-startup nil)
  4161. ("beamer" org-startup-with-beamer-mode t)
  4162. ("entitiespretty" org-pretty-entities t)
  4163. ("entitiesplain" org-pretty-entities nil))
  4164. "Variable associated with STARTUP options for org-mode.
  4165. Each element is a list of three items: the startup options (as written
  4166. in the #+STARTUP line), the corresponding variable, and the value to set
  4167. this variable to if the option is found. An optional forth element PUSH
  4168. means to push this value onto the list in the variable.")
  4169. (defun org-update-property-plist (key val props)
  4170. "Update PROPS with KEY and VAL."
  4171. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4172. (key (if appending (substring key 0 (- (length key) 1)) key))
  4173. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4174. (previous (cdr (assoc key props))))
  4175. (if appending
  4176. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4177. (cons (cons key val) remainder))))
  4178. (defconst org-block-regexp
  4179. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4180. "Regular expression for hiding blocks.")
  4181. (defconst org-heading-keyword-regexp-format
  4182. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4183. "Printf format for a regexp matching a headline with some keyword.
  4184. This regexp will match the headline of any node which has the
  4185. exact keyword that is put into the format. The keyword isn't in
  4186. any group by default, but the stars and the body are.")
  4187. (defconst org-heading-keyword-maybe-regexp-format
  4188. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4189. "Printf format for a regexp matching a headline, possibly with some keyword.
  4190. This regexp can match any headline with the specified keyword, or
  4191. without a keyword. The keyword isn't in any group by default,
  4192. but the stars and the body are.")
  4193. (defcustom org-group-tags t
  4194. "When non-nil (the default), use group tags.
  4195. This can be turned on/off through `org-toggle-tags-groups'."
  4196. :group 'org-tags
  4197. :group 'org-startup
  4198. :type 'boolean)
  4199. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4200. (defun org-toggle-tags-groups ()
  4201. "Toggle support for group tags.
  4202. Support for group tags is controlled by the option
  4203. `org-group-tags', which is non-nil by default."
  4204. (interactive)
  4205. (setq org-group-tags (not org-group-tags))
  4206. (cond ((and (derived-mode-p 'org-agenda-mode)
  4207. org-group-tags)
  4208. (org-agenda-redo))
  4209. ((derived-mode-p 'org-mode)
  4210. (let ((org-inhibit-startup t)) (org-mode))))
  4211. (message "Groups tags support has been turned %s"
  4212. (if org-group-tags "on" "off")))
  4213. (defun org-set-regexps-and-options-for-tags ()
  4214. "Precompute variables used for tags."
  4215. (when (derived-mode-p 'org-mode)
  4216. (org-set-local 'org-file-tags nil)
  4217. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4218. (splitre "[ \t]+")
  4219. (start 0)
  4220. tags ftags key value)
  4221. (save-excursion
  4222. (save-restriction
  4223. (widen)
  4224. (goto-char (point-min))
  4225. (while (re-search-forward re nil t)
  4226. (setq key (upcase (org-match-string-no-properties 1))
  4227. value (org-match-string-no-properties 2))
  4228. (if (stringp value) (setq value (org-trim value)))
  4229. (cond
  4230. ((equal key "TAGS")
  4231. (setq tags (append tags (if tags '("\\n") nil)
  4232. (org-split-string value splitre))))
  4233. ((equal key "FILETAGS")
  4234. (when (string-match "\\S-" value)
  4235. (setq ftags
  4236. (append
  4237. ftags
  4238. (apply 'append
  4239. (mapcar (lambda (x) (org-split-string x ":"))
  4240. (org-split-string value)))))))))))
  4241. ;; Process the file tags.
  4242. (and ftags (org-set-local 'org-file-tags
  4243. (mapcar 'org-add-prop-inherited ftags)))
  4244. (org-set-local 'org-tag-groups-alist nil)
  4245. ;; Process the tags.
  4246. (when (and (not tags) org-tag-alist)
  4247. (setq tags
  4248. (mapcar
  4249. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4250. ((eq (car tg) :endgroup) "}")
  4251. ((eq (car tg) :grouptags) ":")
  4252. ((eq (car tg) :newline) "\n")
  4253. (t (concat (car tg)
  4254. (if (characterp (cdr tg))
  4255. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4256. org-tag-alist)))
  4257. (let (e tgs g)
  4258. (while (setq e (pop tags))
  4259. (cond
  4260. ((equal e "{")
  4261. (progn (push '(:startgroup) tgs)
  4262. (when (equal (nth 1 tags) ":")
  4263. (push (list (replace-regexp-in-string
  4264. "(.+)$" "" (nth 0 tags)))
  4265. org-tag-groups-alist)
  4266. (setq g 0))))
  4267. ((equal e ":") (push '(:grouptags) tgs))
  4268. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4269. ((equal e "\\n") (push '(:newline) tgs))
  4270. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4271. (push (cons (match-string 1 e)
  4272. (string-to-char (match-string 2 e))) tgs)
  4273. (if (and g (> g 0))
  4274. (setcar org-tag-groups-alist
  4275. (append (car org-tag-groups-alist)
  4276. (list (match-string 1 e)))))
  4277. (if g (setq g (1+ g))))
  4278. (t (push (list e) tgs)
  4279. (if (and g (> g 0))
  4280. (setcar org-tag-groups-alist
  4281. (append (car org-tag-groups-alist) (list e))))
  4282. (if g (setq g (1+ g))))))
  4283. (org-set-local 'org-tag-alist nil)
  4284. (while (setq e (pop tgs))
  4285. (or (and (stringp (car e))
  4286. (assoc (car e) org-tag-alist))
  4287. (push e org-tag-alist)))
  4288. ;; Return a list with tag variables
  4289. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4290. (defvar org-ota nil)
  4291. (defun org-set-regexps-and-options ()
  4292. "Precompute regular expressions used in the current buffer."
  4293. (when (derived-mode-p 'org-mode)
  4294. (org-set-local 'org-todo-kwd-alist nil)
  4295. (org-set-local 'org-todo-key-alist nil)
  4296. (org-set-local 'org-todo-key-trigger nil)
  4297. (org-set-local 'org-todo-keywords-1 nil)
  4298. (org-set-local 'org-done-keywords nil)
  4299. (org-set-local 'org-todo-heads nil)
  4300. (org-set-local 'org-todo-sets nil)
  4301. (org-set-local 'org-todo-log-states nil)
  4302. (org-set-local 'org-file-properties nil)
  4303. (let ((re (org-make-options-regexp
  4304. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4305. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4306. "SETUPFILE" "OPTIONS")
  4307. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4308. (splitre "[ \t]+")
  4309. (scripts org-use-sub-superscripts)
  4310. kwds kws0 kwsa key log value cat arch const links hw dws
  4311. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4312. (start 0))
  4313. (save-excursion
  4314. (save-restriction
  4315. (widen)
  4316. (goto-char (point-min))
  4317. (while
  4318. (or (and
  4319. ext-setup-or-nil
  4320. (not org-ota)
  4321. (let (ret)
  4322. (with-temp-buffer
  4323. (insert ext-setup-or-nil)
  4324. (let ((major-mode 'org-mode) org-ota)
  4325. (setq ret (save-match-data
  4326. (org-set-regexps-and-options-for-tags)))))
  4327. ;; Append setupfile tags to existing tags
  4328. (setq org-ota t)
  4329. (setq org-file-tags
  4330. (delq nil (append org-file-tags (nth 0 ret)))
  4331. org-tag-alist
  4332. (delq nil (append org-tag-alist (nth 1 ret)))
  4333. org-tag-groups-alist
  4334. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4335. (and ext-setup-or-nil
  4336. (string-match re ext-setup-or-nil start)
  4337. (setq start (match-end 0)))
  4338. (and (setq ext-setup-or-nil nil start 0)
  4339. (re-search-forward re nil t)))
  4340. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4341. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4342. (if (stringp value) (setq value (org-trim value)))
  4343. (cond
  4344. ((equal key "CATEGORY")
  4345. (setq cat value))
  4346. ((member key '("SEQ_TODO" "TODO"))
  4347. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4348. ((equal key "TYP_TODO")
  4349. (push (cons 'type (org-split-string value splitre)) kwds))
  4350. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4351. ;; general TODO-like setup
  4352. (push (cons (intern (downcase (match-string 1 key)))
  4353. (org-split-string value splitre)) kwds))
  4354. ((equal key "COLUMNS")
  4355. (org-set-local 'org-columns-default-format value))
  4356. ((equal key "LINK")
  4357. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4358. (push (cons (match-string 1 value)
  4359. (org-trim (match-string 2 value)))
  4360. links)))
  4361. ((equal key "PRIORITIES")
  4362. (setq prio (org-split-string value " +")))
  4363. ((equal key "PROPERTY")
  4364. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4365. (setq props (org-update-property-plist (match-string 1 value)
  4366. (match-string 2 value)
  4367. props))))
  4368. ((equal key "DRAWERS")
  4369. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4370. ((equal key "CONSTANTS")
  4371. (org-table-set-constants))
  4372. ((equal key "STARTUP")
  4373. (let ((opts (org-split-string value splitre))
  4374. l var val)
  4375. (while (setq l (pop opts))
  4376. (when (setq l (assoc l org-startup-options))
  4377. (setq var (nth 1 l) val (nth 2 l))
  4378. (if (not (nth 3 l))
  4379. (set (make-local-variable var) val)
  4380. (if (not (listp (symbol-value var)))
  4381. (set (make-local-variable var) nil))
  4382. (set (make-local-variable var) (symbol-value var))
  4383. (add-to-list var val))))))
  4384. ((equal key "ARCHIVE")
  4385. (setq arch value)
  4386. (remove-text-properties 0 (length arch)
  4387. '(face t fontified t) arch))
  4388. ((equal key "OPTIONS")
  4389. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4390. (setq scripts (read (match-string 2 value)))))
  4391. ((and (equal key "SETUPFILE")
  4392. ;; Prevent checking in Gnus messages
  4393. (not buffer-read-only))
  4394. (setq setup-contents (org-file-contents
  4395. (expand-file-name
  4396. (org-remove-double-quotes value))
  4397. 'noerror))
  4398. (if (not ext-setup-or-nil)
  4399. (setq ext-setup-or-nil setup-contents start 0)
  4400. (setq ext-setup-or-nil
  4401. (concat (substring ext-setup-or-nil 0 start)
  4402. "\n" setup-contents "\n"
  4403. (substring ext-setup-or-nil start)))))))
  4404. ;; search for property blocks
  4405. (goto-char (point-min))
  4406. (while (re-search-forward org-block-regexp nil t)
  4407. (when (equal "PROPERTY" (upcase (match-string 1)))
  4408. (setq value (replace-regexp-in-string
  4409. "[\n\r]" " " (match-string 4)))
  4410. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4411. (setq props (org-update-property-plist (match-string 1 value)
  4412. (match-string 2 value)
  4413. props)))))))
  4414. (org-set-local 'org-use-sub-superscripts scripts)
  4415. (when cat
  4416. (org-set-local 'org-category (intern cat))
  4417. (push (cons "CATEGORY" cat) props))
  4418. (when prio
  4419. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4420. (setq prio (mapcar 'string-to-char prio))
  4421. (org-set-local 'org-highest-priority (nth 0 prio))
  4422. (org-set-local 'org-lowest-priority (nth 1 prio))
  4423. (org-set-local 'org-default-priority (nth 2 prio)))
  4424. (and props (org-set-local 'org-file-properties (nreverse props)))
  4425. (and drawers (org-set-local 'org-drawers drawers))
  4426. (and arch (org-set-local 'org-archive-location arch))
  4427. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4428. ;; Process the TODO keywords
  4429. (unless kwds
  4430. ;; Use the global values as if they had been given locally.
  4431. (setq kwds (default-value 'org-todo-keywords))
  4432. (if (stringp (car kwds))
  4433. (setq kwds (list (cons org-todo-interpretation
  4434. (default-value 'org-todo-keywords)))))
  4435. (setq kwds (reverse kwds)))
  4436. (setq kwds (nreverse kwds))
  4437. (let (inter kws kw)
  4438. (while (setq kws (pop kwds))
  4439. (let ((kws (or
  4440. (run-hook-with-args-until-success
  4441. 'org-todo-setup-filter-hook kws)
  4442. kws)))
  4443. (setq inter (pop kws) sep (member "|" kws)
  4444. kws0 (delete "|" (copy-sequence kws))
  4445. kwsa nil
  4446. kws1 (mapcar
  4447. (lambda (x)
  4448. ;; 1 2
  4449. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4450. (progn
  4451. (setq kw (match-string 1 x)
  4452. key (and (match-end 2) (match-string 2 x))
  4453. log (org-extract-log-state-settings x))
  4454. (push (cons kw (and key (string-to-char key))) kwsa)
  4455. (and log (push log org-todo-log-states))
  4456. kw)
  4457. (error "Invalid TODO keyword %s" x)))
  4458. kws0)
  4459. kwsa (if kwsa (append '((:startgroup))
  4460. (nreverse kwsa)
  4461. '((:endgroup))))
  4462. hw (car kws1)
  4463. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4464. tail (list inter hw (car dws) (org-last dws))))
  4465. (add-to-list 'org-todo-heads hw 'append)
  4466. (push kws1 org-todo-sets)
  4467. (setq org-done-keywords (append org-done-keywords dws nil))
  4468. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4469. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4470. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4471. (setq org-todo-sets (nreverse org-todo-sets)
  4472. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4473. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4474. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4475. ;; Compute the regular expressions and other local variables.
  4476. ;; Using `org-outline-regexp-bol' would complicate them much,
  4477. ;; because of the fixed white space at the end of that string.
  4478. (if (not org-done-keywords)
  4479. (setq org-done-keywords (and org-todo-keywords-1
  4480. (list (org-last org-todo-keywords-1)))))
  4481. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4482. (length org-scheduled-string)
  4483. (length org-clock-string)
  4484. (length org-closed-string)))
  4485. org-drawer-regexp
  4486. (concat "^[ \t]*:\\("
  4487. (mapconcat 'regexp-quote org-drawers "\\|")
  4488. "\\):[ \t]*$")
  4489. org-not-done-keywords
  4490. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4491. org-todo-regexp
  4492. (concat "\\("
  4493. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4494. "\\)")
  4495. org-not-done-regexp
  4496. (concat "\\("
  4497. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4498. "\\)")
  4499. org-not-done-heading-regexp
  4500. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4501. org-todo-line-regexp
  4502. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4503. org-complex-heading-regexp
  4504. (concat "^\\(\\*+\\)"
  4505. "\\(?: +" org-todo-regexp "\\)?"
  4506. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4507. "\\(?: +\\(.*?\\)\\)??"
  4508. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4509. "[ \t]*$")
  4510. org-complex-heading-regexp-format
  4511. (concat "^\\(\\*+\\)"
  4512. "\\(?: +" org-todo-regexp "\\)?"
  4513. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4514. "\\(?: +"
  4515. ;; Stats cookies can be stuck to body.
  4516. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4517. "\\(%s\\)"
  4518. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4519. "\\)"
  4520. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4521. "[ \t]*$")
  4522. org-todo-line-tags-regexp
  4523. (concat "^\\(\\*+\\)"
  4524. "\\(?: +" org-todo-regexp "\\)?"
  4525. "\\(?: +\\(.*?\\)\\)??"
  4526. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4527. "[ \t]*$")
  4528. org-deadline-regexp (concat "\\<" org-deadline-string)
  4529. org-deadline-time-regexp
  4530. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4531. org-deadline-time-hour-regexp
  4532. (concat "\\<" org-deadline-string
  4533. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4534. org-deadline-line-regexp
  4535. (concat "\\<\\(" org-deadline-string "\\).*")
  4536. org-scheduled-regexp
  4537. (concat "\\<" org-scheduled-string)
  4538. org-scheduled-time-regexp
  4539. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4540. org-scheduled-time-hour-regexp
  4541. (concat "\\<" org-scheduled-string
  4542. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4543. org-closed-time-regexp
  4544. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4545. org-keyword-time-regexp
  4546. (concat "\\<\\(" org-scheduled-string
  4547. "\\|" org-deadline-string
  4548. "\\|" org-closed-string
  4549. "\\|" org-clock-string "\\)"
  4550. " *[[<]\\([^]>]+\\)[]>]")
  4551. org-keyword-time-not-clock-regexp
  4552. (concat "\\<\\(" org-scheduled-string
  4553. "\\|" org-deadline-string
  4554. "\\|" org-closed-string
  4555. "\\)"
  4556. " *[[<]\\([^]>]+\\)[]>]")
  4557. org-maybe-keyword-time-regexp
  4558. (concat "\\(\\<\\(" org-scheduled-string
  4559. "\\|" org-deadline-string
  4560. "\\|" org-closed-string
  4561. "\\|" org-clock-string "\\)\\)?"
  4562. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4563. org-all-time-keywords
  4564. (mapcar (lambda (w) (substring w 0 -1))
  4565. (list org-scheduled-string org-deadline-string
  4566. org-clock-string org-closed-string)))
  4567. (setq org-ota nil)
  4568. (org-compute-latex-and-related-regexp))))
  4569. (defun org-file-contents (file &optional noerror)
  4570. "Return the contents of FILE, as a string."
  4571. (if (or (not file) (not (file-readable-p file)))
  4572. (if (not noerror)
  4573. (error "Cannot read file \"%s\"" file)
  4574. (message "Cannot read file \"%s\"" file)
  4575. "")
  4576. (with-temp-buffer
  4577. (insert-file-contents file)
  4578. (buffer-string))))
  4579. (defun org-extract-log-state-settings (x)
  4580. "Extract the log state setting from a TODO keyword string.
  4581. This will extract info from a string like \"WAIT(w@/!)\"."
  4582. (let (kw key log1 log2)
  4583. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4584. (setq kw (match-string 1 x)
  4585. key (and (match-end 2) (match-string 2 x))
  4586. log1 (and (match-end 3) (match-string 3 x))
  4587. log2 (and (match-end 4) (match-string 4 x)))
  4588. (and (or log1 log2)
  4589. (list kw
  4590. (and log1 (if (equal log1 "!") 'time 'note))
  4591. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4592. (defun org-remove-keyword-keys (list)
  4593. "Remove a pair of parenthesis at the end of each string in LIST."
  4594. (mapcar (lambda (x)
  4595. (if (string-match "(.*)$" x)
  4596. (substring x 0 (match-beginning 0))
  4597. x))
  4598. list))
  4599. (defun org-assign-fast-keys (alist)
  4600. "Assign fast keys to a keyword-key alist.
  4601. Respect keys that are already there."
  4602. (let (new e (alt ?0))
  4603. (while (setq e (pop alist))
  4604. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4605. (cdr e)) ;; Key already assigned.
  4606. (push e new)
  4607. (let ((clist (string-to-list (downcase (car e))))
  4608. (used (append new alist)))
  4609. (when (= (car clist) ?@)
  4610. (pop clist))
  4611. (while (and clist (rassoc (car clist) used))
  4612. (pop clist))
  4613. (unless clist
  4614. (while (rassoc alt used)
  4615. (incf alt)))
  4616. (push (cons (car e) (or (car clist) alt)) new))))
  4617. (nreverse new)))
  4618. ;;; Some variables used in various places
  4619. (defvar org-window-configuration nil
  4620. "Used in various places to store a window configuration.")
  4621. (defvar org-selected-window nil
  4622. "Used in various places to store a window configuration.")
  4623. (defvar org-finish-function nil
  4624. "Function to be called when `C-c C-c' is used.
  4625. This is for getting out of special buffers like capture.")
  4626. ;; FIXME: Occasionally check by commenting these, to make sure
  4627. ;; no other functions uses these, forgetting to let-bind them.
  4628. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4629. (defvar org-last-state)
  4630. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4631. ;; Defined somewhere in this file, but used before definition.
  4632. (defvar org-entities) ;; defined in org-entities.el
  4633. (defvar org-struct-menu)
  4634. (defvar org-org-menu)
  4635. (defvar org-tbl-menu)
  4636. ;;;; Define the Org-mode
  4637. ;; We use a before-change function to check if a table might need
  4638. ;; an update.
  4639. (defvar org-table-may-need-update t
  4640. "Indicates that a table might need an update.
  4641. This variable is set by `org-before-change-function'.
  4642. `org-table-align' sets it back to nil.")
  4643. (defun org-before-change-function (beg end)
  4644. "Every change indicates that a table might need an update."
  4645. (setq org-table-may-need-update t))
  4646. (defvar org-mode-map)
  4647. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4648. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4649. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4650. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4651. (defvar org-table-buffer-is-an nil)
  4652. (defvar bidi-paragraph-direction)
  4653. (defvar buffer-face-mode-face)
  4654. (require 'outline)
  4655. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4656. (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"))
  4657. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4658. ;; Other stuff we need.
  4659. (require 'time-date)
  4660. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4661. (require 'easymenu)
  4662. (require 'overlay)
  4663. ;; (require 'org-macs) moved higher up in the file before it is first used
  4664. (require 'org-entities)
  4665. ;; (require 'org-compat) moved higher up in the file before it is first used
  4666. (require 'org-faces)
  4667. (require 'org-list)
  4668. (require 'org-pcomplete)
  4669. (require 'org-src)
  4670. (require 'org-footnote)
  4671. (require 'org-macro)
  4672. ;; babel
  4673. (require 'ob)
  4674. ;;;###autoload
  4675. (define-derived-mode org-mode outline-mode "Org"
  4676. "Outline-based notes management and organizer, alias
  4677. \"Carsten's outline-mode for keeping track of everything.\"
  4678. Org-mode develops organizational tasks around a NOTES file which
  4679. contains information about projects as plain text. Org-mode is
  4680. implemented on top of outline-mode, which is ideal to keep the content
  4681. of large files well structured. It supports ToDo items, deadlines and
  4682. time stamps, which magically appear in the diary listing of the Emacs
  4683. calendar. Tables are easily created with a built-in table editor.
  4684. Plain text URL-like links connect to websites, emails (VM), Usenet
  4685. messages (Gnus), BBDB entries, and any files related to the project.
  4686. For printing and sharing of notes, an Org-mode file (or a part of it)
  4687. can be exported as a structured ASCII or HTML file.
  4688. The following commands are available:
  4689. \\{org-mode-map}"
  4690. ;; Get rid of Outline menus, they are not needed
  4691. ;; Need to do this here because define-derived-mode sets up
  4692. ;; the keymap so late. Still, it is a waste to call this each time
  4693. ;; we switch another buffer into org-mode.
  4694. (if (featurep 'xemacs)
  4695. (when (boundp 'outline-mode-menu-heading)
  4696. ;; Assume this is Greg's port, it uses easymenu
  4697. (easy-menu-remove outline-mode-menu-heading)
  4698. (easy-menu-remove outline-mode-menu-show)
  4699. (easy-menu-remove outline-mode-menu-hide))
  4700. (define-key org-mode-map [menu-bar headings] 'undefined)
  4701. (define-key org-mode-map [menu-bar hide] 'undefined)
  4702. (define-key org-mode-map [menu-bar show] 'undefined))
  4703. (org-load-modules-maybe)
  4704. (easy-menu-add org-org-menu)
  4705. (easy-menu-add org-tbl-menu)
  4706. (org-install-agenda-files-menu)
  4707. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4708. (add-to-invisibility-spec '(org-cwidth))
  4709. (add-to-invisibility-spec '(org-hide-block . t))
  4710. (when (featurep 'xemacs)
  4711. (org-set-local 'line-move-ignore-invisible t))
  4712. (org-set-local 'outline-regexp org-outline-regexp)
  4713. (org-set-local 'outline-level 'org-outline-level)
  4714. (setq bidi-paragraph-direction 'left-to-right)
  4715. (when (and org-ellipsis
  4716. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4717. (fboundp 'make-glyph-code))
  4718. (unless org-display-table
  4719. (setq org-display-table (make-display-table)))
  4720. (set-display-table-slot
  4721. org-display-table 4
  4722. (vconcat (mapcar
  4723. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4724. org-ellipsis)))
  4725. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4726. (setq buffer-display-table org-display-table))
  4727. (org-set-regexps-and-options-for-tags)
  4728. (org-set-regexps-and-options)
  4729. (org-set-font-lock-defaults)
  4730. (when (and org-tag-faces (not org-tags-special-faces-re))
  4731. ;; tag faces set outside customize.... force initialization.
  4732. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4733. ;; Calc embedded
  4734. (org-set-local 'calc-embedded-open-mode "# ")
  4735. ;; Modify a few syntax entries
  4736. (modify-syntax-entry ?@ "w")
  4737. (modify-syntax-entry ?\" "\"")
  4738. (modify-syntax-entry ?\\ "_")
  4739. (modify-syntax-entry ?~ "_")
  4740. (if org-startup-truncated (setq truncate-lines t))
  4741. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4742. (org-set-local 'font-lock-unfontify-region-function
  4743. 'org-unfontify-region)
  4744. ;; Activate before-change-function
  4745. (org-set-local 'org-table-may-need-update t)
  4746. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4747. 'local)
  4748. ;; Check for running clock before killing a buffer
  4749. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4750. ;; Initialize macros templates.
  4751. (org-macro-initialize-templates)
  4752. ;; Initialize radio targets.
  4753. (org-update-radio-target-regexp)
  4754. ;; Indentation.
  4755. (org-set-local 'indent-line-function 'org-indent-line)
  4756. (org-set-local 'indent-region-function 'org-indent-region)
  4757. ;; Filling and auto-filling.
  4758. (org-setup-filling)
  4759. ;; Comments.
  4760. (org-setup-comments-handling)
  4761. ;; Beginning/end of defun
  4762. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4763. (org-set-local 'end-of-defun-function
  4764. (lambda ()
  4765. (if (not (org-at-heading-p))
  4766. (org-forward-element)
  4767. (org-forward-element)
  4768. (forward-char -1))))
  4769. ;; Next error for sparse trees
  4770. (org-set-local 'next-error-function 'org-occur-next-match)
  4771. ;; Make sure dependence stuff works reliably, even for users who set it
  4772. ;; too late :-(
  4773. (if org-enforce-todo-dependencies
  4774. (add-hook 'org-blocker-hook
  4775. 'org-block-todo-from-children-or-siblings-or-parent)
  4776. (remove-hook 'org-blocker-hook
  4777. 'org-block-todo-from-children-or-siblings-or-parent))
  4778. (if org-enforce-todo-checkbox-dependencies
  4779. (add-hook 'org-blocker-hook
  4780. 'org-block-todo-from-checkboxes)
  4781. (remove-hook 'org-blocker-hook
  4782. 'org-block-todo-from-checkboxes))
  4783. ;; Align options lines
  4784. (org-set-local
  4785. 'align-mode-rules-list
  4786. '((org-in-buffer-settings
  4787. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4788. (modes . '(org-mode)))))
  4789. ;; Imenu
  4790. (org-set-local 'imenu-create-index-function
  4791. 'org-imenu-get-tree)
  4792. ;; Make isearch reveal context
  4793. (if (or (featurep 'xemacs)
  4794. (not (boundp 'outline-isearch-open-invisible-function)))
  4795. ;; Emacs 21 and XEmacs make use of the hook
  4796. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4797. ;; Emacs 22 deals with this through a special variable
  4798. (org-set-local 'outline-isearch-open-invisible-function
  4799. (lambda (&rest ignore) (org-show-context 'isearch)))
  4800. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4801. ;; Setup the pcomplete hooks
  4802. (set (make-local-variable 'pcomplete-command-completion-function)
  4803. 'org-pcomplete-initial)
  4804. (set (make-local-variable 'pcomplete-command-name-function)
  4805. 'org-command-at-point)
  4806. (set (make-local-variable 'pcomplete-default-completion-function)
  4807. 'ignore)
  4808. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4809. 'org-parse-arguments)
  4810. (set (make-local-variable 'pcomplete-termination-string) "")
  4811. (when (>= emacs-major-version 23)
  4812. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4813. ;; If empty file that did not turn on org-mode automatically, make it to.
  4814. (if (and org-insert-mode-line-in-empty-file
  4815. (org-called-interactively-p 'any)
  4816. (= (point-min) (point-max)))
  4817. (insert "# -*- mode: org -*-\n\n"))
  4818. (unless org-inhibit-startup
  4819. (org-unmodified
  4820. (and org-startup-with-beamer-mode (org-beamer-mode))
  4821. (when org-startup-align-all-tables
  4822. (org-table-map-tables 'org-table-align 'quietly))
  4823. (when org-startup-with-inline-images
  4824. (org-display-inline-images))
  4825. (when org-startup-with-latex-preview
  4826. (org-preview-latex-fragment))
  4827. (unless org-inhibit-startup-visibility-stuff
  4828. (org-set-startup-visibility))))
  4829. ;; Try to set org-hide correctly
  4830. (let ((foreground (org-find-invisible-foreground)))
  4831. (if foreground
  4832. (set-face-foreground 'org-hide foreground))))
  4833. ;; Update `customize-package-emacs-version-alist'
  4834. (add-to-list 'customize-package-emacs-version-alist
  4835. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4836. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4837. ("8.2.6" . "24.4")))
  4838. (defvar org-mode-transpose-word-syntax-table
  4839. (let ((st (make-syntax-table)))
  4840. (mapc (lambda(c) (modify-syntax-entry
  4841. (string-to-char (car c)) "w p" st))
  4842. org-emphasis-alist)
  4843. st))
  4844. (when (fboundp 'abbrev-table-put)
  4845. (abbrev-table-put org-mode-abbrev-table
  4846. :parents (list text-mode-abbrev-table)))
  4847. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4848. (defsubst org-fix-ellipsis-at-bol ()
  4849. (save-excursion (goto-char (window-start)) (recenter 0)))
  4850. (defun org-find-invisible-foreground ()
  4851. (let ((candidates (remove
  4852. "unspecified-bg"
  4853. (nconc
  4854. (list (face-background 'default)
  4855. (face-background 'org-default))
  4856. (mapcar
  4857. (lambda (alist)
  4858. (when (boundp alist)
  4859. (cdr (assoc 'background-color (symbol-value alist)))))
  4860. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4861. (list (face-foreground 'org-hide))))))
  4862. (car (remove nil candidates))))
  4863. (defun org-current-time (&optional rounding-minutes past)
  4864. "Current time, possibly rounded to ROUNDING-MINUTES.
  4865. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4866. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4867. the rounding returns a past time."
  4868. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4869. (car org-time-stamp-rounding-minutes)))
  4870. (time (decode-time)) res)
  4871. (if (< r 1)
  4872. (current-time)
  4873. (setq res
  4874. (apply 'encode-time
  4875. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4876. (nthcdr 2 time))))
  4877. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4878. (seconds-to-time (- (org-float-time res) (* r 60)))
  4879. res))))
  4880. (defun org-today ()
  4881. "Return today date, considering `org-extend-today-until'."
  4882. (time-to-days
  4883. (time-subtract (current-time)
  4884. (list 0 (* 3600 org-extend-today-until) 0))))
  4885. ;;;; Font-Lock stuff, including the activators
  4886. (defvar org-mouse-map (make-sparse-keymap))
  4887. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4888. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4889. (when org-mouse-1-follows-link
  4890. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4891. (when org-tab-follows-link
  4892. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4893. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4894. (require 'font-lock)
  4895. (defconst org-non-link-chars "]\t\n\r<>")
  4896. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4897. "shell" "elisp" "doi" "message"))
  4898. (defvar org-link-types-re nil
  4899. "Matches a link that has a url-like prefix like \"http:\"")
  4900. (defvar org-link-re-with-space nil
  4901. "Matches a link with spaces, optional angular brackets around it.")
  4902. (defvar org-link-re-with-space2 nil
  4903. "Matches a link with spaces, optional angular brackets around it.")
  4904. (defvar org-link-re-with-space3 nil
  4905. "Matches a link with spaces, only for internal part in bracket links.")
  4906. (defvar org-angle-link-re nil
  4907. "Matches link with angular brackets, spaces are allowed.")
  4908. (defvar org-plain-link-re nil
  4909. "Matches plain link, without spaces.")
  4910. (defvar org-bracket-link-regexp nil
  4911. "Matches a link in double brackets.")
  4912. (defvar org-bracket-link-analytic-regexp nil
  4913. "Regular expression used to analyze links.
  4914. Here is what the match groups contain after a match:
  4915. 1: http:
  4916. 2: http
  4917. 3: path
  4918. 4: [desc]
  4919. 5: desc")
  4920. (defvar org-bracket-link-analytic-regexp++ nil
  4921. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4922. (defvar org-any-link-re nil
  4923. "Regular expression matching any link.")
  4924. (defconst org-match-sexp-depth 3
  4925. "Number of stacked braces for sub/superscript matching.")
  4926. (defun org-create-multibrace-regexp (left right n)
  4927. "Create a regular expression which will match a balanced sexp.
  4928. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4929. as single character strings.
  4930. The regexp returned will match the entire expression including the
  4931. delimiters. It will also define a single group which contains the
  4932. match except for the outermost delimiters. The maximum depth of
  4933. stacked delimiters is N. Escaping delimiters is not possible."
  4934. (let* ((nothing (concat "[^" left right "]*?"))
  4935. (or "\\|")
  4936. (re nothing)
  4937. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4938. (while (> n 1)
  4939. (setq n (1- n)
  4940. re (concat re or next)
  4941. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4942. (concat left "\\(" re "\\)" right)))
  4943. (defvar org-match-substring-regexp
  4944. (concat
  4945. "\\(\\S-\\)\\([_^]\\)\\("
  4946. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4947. "\\|"
  4948. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4949. "\\|"
  4950. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4951. "The regular expression matching a sub- or superscript.")
  4952. (defvar org-match-substring-with-braces-regexp
  4953. (concat
  4954. "\\(\\S-\\)\\([_^]\\)\\("
  4955. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4956. "\\)")
  4957. "The regular expression matching a sub- or superscript, forcing braces.")
  4958. (defun org-make-link-regexps ()
  4959. "Update the link regular expressions.
  4960. This should be called after the variable `org-link-types' has changed."
  4961. (let ((types-re (regexp-opt org-link-types t)))
  4962. (setq org-link-types-re
  4963. (concat "\\`" types-re ":")
  4964. org-link-re-with-space
  4965. (concat "<?" types-re ":"
  4966. "\\([^" org-non-link-chars " ]"
  4967. "[^" org-non-link-chars "]*"
  4968. "[^" org-non-link-chars " ]\\)>?")
  4969. org-link-re-with-space2
  4970. (concat "<?" types-re ":"
  4971. "\\([^" org-non-link-chars " ]"
  4972. "[^\t\n\r]*"
  4973. "[^" org-non-link-chars " ]\\)>?")
  4974. org-link-re-with-space3
  4975. (concat "<?" types-re ":"
  4976. "\\([^" org-non-link-chars " ]"
  4977. "[^\t\n\r]*\\)")
  4978. org-angle-link-re
  4979. (concat "<" types-re ":"
  4980. "\\([^" org-non-link-chars " ]"
  4981. "[^" org-non-link-chars "]*"
  4982. "\\)>")
  4983. org-plain-link-re
  4984. (concat
  4985. "\\<" types-re ":"
  4986. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4987. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4988. org-bracket-link-regexp
  4989. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4990. org-bracket-link-analytic-regexp
  4991. (concat
  4992. "\\[\\["
  4993. "\\(" types-re ":\\)?"
  4994. "\\([^]]+\\)"
  4995. "\\]"
  4996. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4997. "\\]")
  4998. org-bracket-link-analytic-regexp++
  4999. (concat
  5000. "\\[\\["
  5001. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5002. "\\([^]]+\\)"
  5003. "\\]"
  5004. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5005. "\\]")
  5006. org-any-link-re
  5007. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5008. org-angle-link-re "\\)\\|\\("
  5009. org-plain-link-re "\\)"))))
  5010. (org-make-link-regexps)
  5011. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  5012. "Regular expression for fast time stamp matching.")
  5013. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5014. "Regular expression for fast time stamp matching.")
  5015. (defconst org-ts-regexp0
  5016. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5017. "Regular expression matching time strings for analysis.
  5018. This one does not require the space after the date, so it can be used
  5019. on a string that terminates immediately after the date.")
  5020. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5021. "Regular expression matching time strings for analysis.")
  5022. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5023. "Regular expression matching time stamps, with groups.")
  5024. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5025. "Regular expression matching time stamps (also [..]), with groups.")
  5026. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5027. "Regular expression matching a time stamp range.")
  5028. (defconst org-tr-regexp-both
  5029. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5030. "Regular expression matching a time stamp range.")
  5031. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5032. org-ts-regexp "\\)?")
  5033. "Regular expression matching a time stamp or time stamp range.")
  5034. (defconst org-tsr-regexp-both
  5035. (concat org-ts-regexp-both "\\(--?-?"
  5036. org-ts-regexp-both "\\)?")
  5037. "Regular expression matching a time stamp or time stamp range.
  5038. The time stamps may be either active or inactive.")
  5039. (defvar org-emph-face nil)
  5040. (defun org-do-emphasis-faces (limit)
  5041. "Run through the buffer and add overlays to emphasized strings."
  5042. (let (rtn a)
  5043. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5044. (let* ((border (char-after (match-beginning 3)))
  5045. (bre (regexp-quote (char-to-string border))))
  5046. (if (and (not (= border (char-after (match-beginning 4))))
  5047. (not (save-match-data
  5048. (string-match (concat bre ".*" bre)
  5049. (replace-regexp-in-string
  5050. "\n" " "
  5051. (substring (match-string 2) 1 -1))))))
  5052. (progn
  5053. (setq rtn t)
  5054. (setq a (assoc (match-string 3) org-emphasis-alist))
  5055. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5056. 'face
  5057. (nth 1 a))
  5058. (and (nth 2 a)
  5059. (org-remove-flyspell-overlays-in
  5060. (match-beginning 0) (match-end 0)))
  5061. (add-text-properties (match-beginning 2) (match-end 2)
  5062. '(font-lock-multiline t org-emphasis t))
  5063. (when org-hide-emphasis-markers
  5064. (add-text-properties (match-end 4) (match-beginning 5)
  5065. '(invisible org-link))
  5066. (add-text-properties (match-beginning 3) (match-end 3)
  5067. '(invisible org-link))))))
  5068. (goto-char (1+ (match-beginning 0))))
  5069. rtn))
  5070. (defun org-emphasize (&optional char)
  5071. "Insert or change an emphasis, i.e. a font like bold or italic.
  5072. If there is an active region, change that region to a new emphasis.
  5073. If there is no region, just insert the marker characters and position
  5074. the cursor between them.
  5075. CHAR should be the marker character. If it is a space, it means to
  5076. remove the emphasis of the selected region.
  5077. If CHAR is not given (for example in an interactive call) it will be
  5078. prompted for."
  5079. (interactive)
  5080. (let ((erc org-emphasis-regexp-components)
  5081. (prompt "")
  5082. (string "") beg end move c s)
  5083. (if (org-region-active-p)
  5084. (setq beg (region-beginning) end (region-end)
  5085. string (buffer-substring beg end))
  5086. (setq move t))
  5087. (unless char
  5088. (message "Emphasis marker or tag: [%s]"
  5089. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5090. (setq char (read-char-exclusive)))
  5091. (if (equal char ?\ )
  5092. (setq s "" move nil)
  5093. (unless (assoc (char-to-string char) org-emphasis-alist)
  5094. (user-error "No such emphasis marker: \"%c\"" char))
  5095. (setq s (char-to-string char)))
  5096. (while (and (> (length string) 1)
  5097. (equal (substring string 0 1) (substring string -1))
  5098. (assoc (substring string 0 1) org-emphasis-alist))
  5099. (setq string (substring string 1 -1)))
  5100. (setq string (concat s string s))
  5101. (if beg (delete-region beg end))
  5102. (unless (or (bolp)
  5103. (string-match (concat "[" (nth 0 erc) "\n]")
  5104. (char-to-string (char-before (point)))))
  5105. (insert " "))
  5106. (unless (or (eobp)
  5107. (string-match (concat "[" (nth 1 erc) "\n]")
  5108. (char-to-string (char-after (point)))))
  5109. (insert " ") (backward-char 1))
  5110. (insert string)
  5111. (and move (backward-char 1))))
  5112. (defconst org-nonsticky-props
  5113. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5114. (defsubst org-rear-nonsticky-at (pos)
  5115. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5116. (defun org-activate-plain-links (limit)
  5117. "Run through the buffer and add overlays to links."
  5118. (let (f hl)
  5119. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5120. (not (org-in-src-block-p)))
  5121. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5122. (setq f (get-text-property (match-beginning 0) 'face))
  5123. (setq hl (org-match-string-no-properties 0))
  5124. (if (or (eq f 'org-tag)
  5125. (and (listp f) (memq 'org-tag f)))
  5126. nil
  5127. (add-text-properties (match-beginning 0) (match-end 0)
  5128. (list 'mouse-face 'highlight
  5129. 'face 'org-link
  5130. 'htmlize-link `(:uri ,hl)
  5131. 'keymap org-mouse-map))
  5132. (org-rear-nonsticky-at (match-end 0)))
  5133. t)))
  5134. (defun org-activate-code (limit)
  5135. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5136. (progn
  5137. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5138. (remove-text-properties (match-beginning 0) (match-end 0)
  5139. '(display t invisible t intangible t))
  5140. t)))
  5141. (defcustom org-src-fontify-natively nil
  5142. "When non-nil, fontify code in code blocks."
  5143. :type 'boolean
  5144. :version "24.1"
  5145. :group 'org-appearance
  5146. :group 'org-babel)
  5147. (defcustom org-allow-promoting-top-level-subtree nil
  5148. "When non-nil, allow promoting a top level subtree.
  5149. The leading star of the top level headline will be replaced
  5150. by a #."
  5151. :type 'boolean
  5152. :version "24.1"
  5153. :group 'org-appearance)
  5154. (defun org-fontify-meta-lines-and-blocks (limit)
  5155. (condition-case nil
  5156. (org-fontify-meta-lines-and-blocks-1 limit)
  5157. (error (message "org-mode fontification error"))))
  5158. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5159. "Fontify #+ lines and blocks."
  5160. (let ((case-fold-search t))
  5161. (if (re-search-forward
  5162. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5163. limit t)
  5164. (let ((beg (match-beginning 0))
  5165. (block-start (match-end 0))
  5166. (block-end nil)
  5167. (lang (match-string 7))
  5168. (beg1 (line-beginning-position 2))
  5169. (dc1 (downcase (match-string 2)))
  5170. (dc3 (downcase (match-string 3)))
  5171. end end1 quoting block-type ovl)
  5172. (cond
  5173. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5174. ;; a single line of backend-specific content
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (remove-text-properties (match-beginning 0) (match-end 0)
  5177. '(display t invisible t intangible t))
  5178. (add-text-properties (match-beginning 1) (match-end 3)
  5179. '(font-lock-fontified t face org-meta-line))
  5180. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5181. '(font-lock-fontified t face org-block))
  5182. ; for backend-specific code
  5183. t)
  5184. ((and (match-end 4) (equal dc3 "+begin"))
  5185. ;; Truly a block
  5186. (setq block-type (downcase (match-string 5))
  5187. quoting (member block-type org-protecting-blocks))
  5188. (when (re-search-forward
  5189. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5190. nil t) ;; on purpose, we look further than LIMIT
  5191. (setq end (min (point-max) (match-end 0))
  5192. end1 (min (point-max) (1- (match-beginning 0))))
  5193. (setq block-end (match-beginning 0))
  5194. (when quoting
  5195. (remove-text-properties beg end
  5196. '(display t invisible t intangible t)))
  5197. (add-text-properties
  5198. beg end
  5199. '(font-lock-fontified t font-lock-multiline t))
  5200. (add-text-properties beg beg1 '(face org-meta-line))
  5201. (add-text-properties end1 (min (point-max) (1+ end))
  5202. '(face org-meta-line)) ; for end_src
  5203. (cond
  5204. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5205. (org-src-font-lock-fontify-block lang block-start block-end)
  5206. ;; remove old background overlays
  5207. (mapc (lambda (ov)
  5208. (if (eq (overlay-get ov 'face) 'org-block-background)
  5209. (delete-overlay ov)))
  5210. (overlays-at (/ (+ beg1 block-end) 2)))
  5211. ;; add a background overlay
  5212. (setq ovl (make-overlay beg1 block-end))
  5213. (overlay-put ovl 'face 'org-block-background)
  5214. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5215. (quoting
  5216. (add-text-properties beg1 (min (point-max) (1+ end1))
  5217. '(face org-block))) ; end of source block
  5218. ((not org-fontify-quote-and-verse-blocks))
  5219. ((string= block-type "quote")
  5220. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5221. ((string= block-type "verse")
  5222. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5223. (add-text-properties beg beg1 '(face org-block-begin-line))
  5224. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5225. '(face org-block-end-line))
  5226. t))
  5227. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5228. (add-text-properties
  5229. beg (match-end 3)
  5230. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5231. '(font-lock-fontified t invisible t)
  5232. '(font-lock-fontified t face org-document-info-keyword)))
  5233. (add-text-properties
  5234. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5235. (if (string-equal dc1 "+title:")
  5236. '(font-lock-fontified t face org-document-title)
  5237. '(font-lock-fontified t face org-document-info))))
  5238. ((or (equal dc1 "+results")
  5239. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5240. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5241. "+call:" "+header:" "+headers:" "+name:"))
  5242. (and (match-end 4) (equal dc3 "+attr")))
  5243. (add-text-properties
  5244. beg (match-end 0)
  5245. '(font-lock-fontified t face org-meta-line))
  5246. t)
  5247. ((member dc3 '(" " ""))
  5248. (add-text-properties
  5249. beg (match-end 0)
  5250. '(font-lock-fontified t face font-lock-comment-face)))
  5251. ((not (member (char-after beg) '(?\ ?\t)))
  5252. ;; just any other in-buffer setting, but not indented
  5253. (add-text-properties
  5254. beg (match-end 0)
  5255. '(font-lock-fontified t face org-meta-line))
  5256. t)
  5257. (t nil))))))
  5258. (defun org-activate-angle-links (limit)
  5259. "Run through the buffer and add overlays to links."
  5260. (if (and (re-search-forward org-angle-link-re limit t)
  5261. (not (org-in-src-block-p)))
  5262. (progn
  5263. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5264. (add-text-properties (match-beginning 0) (match-end 0)
  5265. (list 'mouse-face 'highlight
  5266. 'keymap org-mouse-map))
  5267. (org-rear-nonsticky-at (match-end 0))
  5268. t)))
  5269. (defun org-activate-footnote-links (limit)
  5270. "Run through the buffer and add overlays to footnotes."
  5271. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5272. (when fn
  5273. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5274. (org-remove-flyspell-overlays-in beg end)
  5275. (add-text-properties beg end
  5276. (list 'mouse-face 'highlight
  5277. 'keymap org-mouse-map
  5278. 'help-echo
  5279. (if (= (point-at-bol) beg)
  5280. "Footnote definition"
  5281. "Footnote reference")
  5282. 'font-lock-fontified t
  5283. 'font-lock-multiline t
  5284. 'face 'org-footnote))))))
  5285. (defun org-activate-bracket-links (limit)
  5286. "Run through the buffer and add overlays to bracketed links."
  5287. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5288. (not (org-in-src-block-p)))
  5289. (let* ((hl (org-match-string-no-properties 1))
  5290. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5291. (ip (org-maybe-intangible
  5292. (list 'invisible 'org-link
  5293. 'keymap org-mouse-map 'mouse-face 'highlight
  5294. 'font-lock-multiline t 'help-echo help
  5295. 'htmlize-link `(:uri ,hl))))
  5296. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5297. 'font-lock-multiline t 'help-echo help
  5298. 'htmlize-link `(:uri ,hl))))
  5299. ;; We need to remove the invisible property here. Table narrowing
  5300. ;; may have made some of this invisible.
  5301. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5302. (remove-text-properties (match-beginning 0) (match-end 0)
  5303. '(invisible nil))
  5304. (if (match-end 3)
  5305. (progn
  5306. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5307. (org-rear-nonsticky-at (match-beginning 3))
  5308. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5309. (org-rear-nonsticky-at (match-end 3))
  5310. (add-text-properties (match-end 3) (match-end 0) ip)
  5311. (org-rear-nonsticky-at (match-end 0)))
  5312. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5313. (org-rear-nonsticky-at (match-beginning 1))
  5314. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5315. (org-rear-nonsticky-at (match-end 1))
  5316. (add-text-properties (match-end 1) (match-end 0) ip)
  5317. (org-rear-nonsticky-at (match-end 0)))
  5318. t)))
  5319. (defun org-activate-dates (limit)
  5320. "Run through the buffer and add overlays to dates."
  5321. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5322. (not (equal (char-before (match-beginning 0)) 91)))
  5323. (progn
  5324. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5325. (add-text-properties (match-beginning 0) (match-end 0)
  5326. (list 'mouse-face 'highlight
  5327. 'keymap org-mouse-map))
  5328. (org-rear-nonsticky-at (match-end 0))
  5329. (when org-display-custom-times
  5330. (if (match-end 3)
  5331. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5332. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5333. t)))
  5334. (defvar org-target-link-regexp nil
  5335. "Regular expression matching radio targets in plain text.")
  5336. (make-variable-buffer-local 'org-target-link-regexp)
  5337. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5338. "Regular expression matching a link target.")
  5339. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5340. "Regular expression matching a radio target.")
  5341. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5342. "Regular expression matching any target.")
  5343. (defun org-activate-target-links (limit)
  5344. "Run through the buffer and add overlays to target matches."
  5345. (when org-target-link-regexp
  5346. (let ((case-fold-search t))
  5347. (if (re-search-forward org-target-link-regexp limit t)
  5348. (progn
  5349. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5350. (add-text-properties (match-beginning 0) (match-end 0)
  5351. (list 'mouse-face 'highlight
  5352. 'keymap org-mouse-map
  5353. 'help-echo "Radio target link"
  5354. 'org-linked-text t))
  5355. (org-rear-nonsticky-at (match-end 0))
  5356. t)))))
  5357. (defun org-update-radio-target-regexp ()
  5358. "Find all radio targets in this file and update the regular expression."
  5359. (interactive)
  5360. (when (memq 'radio org-activate-links)
  5361. (setq org-target-link-regexp
  5362. (org-make-target-link-regexp (org-all-targets 'radio)))
  5363. (org-restart-font-lock)))
  5364. (defun org-hide-wide-columns (limit)
  5365. (let (s e)
  5366. (setq s (text-property-any (point) (or limit (point-max))
  5367. 'org-cwidth t))
  5368. (when s
  5369. (setq e (next-single-property-change s 'org-cwidth))
  5370. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5371. (goto-char e)
  5372. t)))
  5373. (defvar org-latex-and-related-regexp nil
  5374. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5375. (defvar org-match-substring-regexp)
  5376. (defvar org-match-substring-with-braces-regexp)
  5377. (defun org-compute-latex-and-related-regexp ()
  5378. "Compute regular expression for LaTeX, entities and sub/superscript.
  5379. Result depends on variable `org-highlight-latex-and-related'."
  5380. (org-set-local
  5381. 'org-latex-and-related-regexp
  5382. (let* ((re-sub
  5383. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5384. ((eq org-use-sub-superscripts '{})
  5385. (list org-match-substring-with-braces-regexp))
  5386. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5387. (re-latex
  5388. (when (memq 'latex org-highlight-latex-and-related)
  5389. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5390. (delq nil
  5391. (mapcar (lambda (x)
  5392. (and (member (car x) matchers) (nth 1 x)))
  5393. org-latex-regexps)))))
  5394. (re-entities
  5395. (when (memq 'entities org-highlight-latex-and-related)
  5396. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5397. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5398. (defun org-do-latex-and-related (limit)
  5399. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5400. LIMIT bounds the search for syntax to highlight. Stop at first
  5401. highlighted object, if any. Return t if some highlighting was
  5402. done, nil otherwise."
  5403. (when (org-string-nw-p org-latex-and-related-regexp)
  5404. (catch 'found
  5405. (while (re-search-forward org-latex-and-related-regexp limit t)
  5406. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5407. 'face))
  5408. '(org-code org-verbatim underline))
  5409. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5410. '(?_ ?^))
  5411. 1
  5412. 0)))
  5413. (font-lock-prepend-text-property
  5414. (+ offset (match-beginning 0)) (match-end 0)
  5415. 'face 'org-latex-and-related)
  5416. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5417. '(font-lock-multiline t)))
  5418. (throw 'found t)))
  5419. nil)))
  5420. (defun org-restart-font-lock ()
  5421. "Restart `font-lock-mode', to force refontification."
  5422. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5423. (font-lock-mode -1)
  5424. (font-lock-mode 1)))
  5425. (defun org-all-targets (&optional radio)
  5426. "Return a list of all targets in this file.
  5427. When optional argument RADIO is non-nil, only find radio
  5428. targets."
  5429. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5430. (save-excursion
  5431. (goto-char (point-min))
  5432. (while (re-search-forward re nil t)
  5433. ;; Make sure point is really within the object.
  5434. (backward-char)
  5435. (let ((obj (org-element-context)))
  5436. (when (memq (org-element-type obj) '(radio-target target))
  5437. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5438. rtn)))
  5439. (defun org-make-target-link-regexp (targets)
  5440. "Make regular expression matching all strings in TARGETS.
  5441. The regular expression finds the targets also if there is a line break
  5442. between words."
  5443. (and targets
  5444. (concat
  5445. "\\_<\\("
  5446. (mapconcat
  5447. (lambda (x)
  5448. (setq x (regexp-quote x))
  5449. (while (string-match " +" x)
  5450. (setq x (replace-match "\\s-+" t t x)))
  5451. x)
  5452. targets
  5453. "\\|")
  5454. "\\)\\_>")))
  5455. (defun org-activate-tags (limit)
  5456. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5457. (progn
  5458. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5459. (add-text-properties (match-beginning 1) (match-end 1)
  5460. (list 'mouse-face 'highlight
  5461. 'keymap org-mouse-map))
  5462. (org-rear-nonsticky-at (match-end 1))
  5463. t)))
  5464. (defun org-outline-level ()
  5465. "Compute the outline level of the heading at point.
  5466. If this is called at a normal headline, the level is the number of stars.
  5467. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5468. (save-excursion
  5469. (if (not (condition-case nil
  5470. (org-back-to-heading t)
  5471. (error nil)))
  5472. 0
  5473. (looking-at org-outline-regexp)
  5474. (1- (- (match-end 0) (match-beginning 0))))))
  5475. (defvar org-font-lock-keywords nil)
  5476. (defsubst org-re-property (property &optional literal)
  5477. "Return a regexp matching a PROPERTY line.
  5478. Match group 3 will be set to the value if it exists."
  5479. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5480. (if literal property (regexp-quote property))
  5481. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5482. (defconst org-property-re
  5483. (org-re-property ".*?" 'literal)
  5484. "Regular expression matching a property line.
  5485. There are four matching groups:
  5486. 1: :PROPKEY: including the leading and trailing colon,
  5487. 2: PROPKEY without the leading and trailing colon,
  5488. 3: PROPVAL without leading or trailing spaces,
  5489. 4: the indentation of the current line,
  5490. 5: trailing whitespace.")
  5491. (defvar org-font-lock-hook nil
  5492. "Functions to be called for special font lock stuff.")
  5493. (defvar org-font-lock-set-keywords-hook nil
  5494. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5495. This is called after `org-font-lock-extra-keywords' is defined, but before
  5496. it is installed to be used by font lock. This can be useful if something
  5497. needs to be inserted at a specific position in the font-lock sequence.")
  5498. (defun org-font-lock-hook (limit)
  5499. "Run `org-font-lock-hook' within LIMIT."
  5500. (run-hook-with-args 'org-font-lock-hook limit))
  5501. (defun org-set-font-lock-defaults ()
  5502. "Set font lock defaults for the current buffer."
  5503. (let* ((em org-fontify-emphasized-text)
  5504. (lk org-activate-links)
  5505. (org-font-lock-extra-keywords
  5506. (list
  5507. ;; Call the hook
  5508. '(org-font-lock-hook)
  5509. ;; Headlines
  5510. `(,(if org-fontify-whole-heading-line
  5511. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5512. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5513. (1 (org-get-level-face 1))
  5514. (2 (org-get-level-face 2))
  5515. (3 (org-get-level-face 3)))
  5516. ;; Table lines
  5517. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5518. (1 'org-table t))
  5519. ;; Table internals
  5520. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5521. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5522. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5523. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5524. ;; Drawers
  5525. (list org-drawer-regexp '(0 'org-special-keyword t))
  5526. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5527. ;; Properties
  5528. (list org-property-re
  5529. '(1 'org-special-keyword t)
  5530. '(3 'org-property-value t))
  5531. ;; Links
  5532. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5533. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5534. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5535. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5536. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5537. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5538. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5539. ;; Targets.
  5540. (list org-any-target-regexp '(0 'org-target t))
  5541. ;; Diary sexps.
  5542. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5543. ;; Macro
  5544. '("{{{.+}}}" (0 'org-macro t))
  5545. '(org-hide-wide-columns (0 nil append))
  5546. ;; TODO keyword
  5547. (list (format org-heading-keyword-regexp-format
  5548. org-todo-regexp)
  5549. '(2 (org-get-todo-face 2) t))
  5550. ;; DONE
  5551. (if org-fontify-done-headline
  5552. (list (format org-heading-keyword-regexp-format
  5553. (concat
  5554. "\\(?:"
  5555. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5556. "\\)"))
  5557. '(2 'org-headline-done t))
  5558. nil)
  5559. ;; Priorities
  5560. '(org-font-lock-add-priority-faces)
  5561. ;; Tags
  5562. '(org-font-lock-add-tag-faces)
  5563. ;; Tags groups
  5564. (if (and org-group-tags org-tag-groups-alist)
  5565. (list (concat org-outline-regexp-bol ".+\\(:"
  5566. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5567. ":\\).*$")
  5568. '(1 'org-tag-group prepend)))
  5569. ;; Special keywords
  5570. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5571. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5572. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5573. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5574. ;; Emphasis
  5575. (if em
  5576. (if (featurep 'xemacs)
  5577. '(org-do-emphasis-faces (0 nil append))
  5578. '(org-do-emphasis-faces)))
  5579. ;; Checkboxes
  5580. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5581. 1 'org-checkbox prepend)
  5582. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5583. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5584. (0 (org-get-checkbox-statistics-face) t)))
  5585. ;; Description list items
  5586. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5587. 1 'org-list-dt prepend)
  5588. ;; ARCHIVEd headings
  5589. (list (concat
  5590. org-outline-regexp-bol
  5591. "\\(.*:" org-archive-tag ":.*\\)")
  5592. '(1 'org-archived prepend))
  5593. ;; Specials
  5594. '(org-do-latex-and-related)
  5595. '(org-fontify-entities)
  5596. '(org-raise-scripts)
  5597. ;; Code
  5598. '(org-activate-code (1 'org-code t))
  5599. ;; COMMENT
  5600. (list (format org-heading-keyword-regexp-format
  5601. (concat "\\("
  5602. org-comment-string "\\|" org-quote-string
  5603. "\\)"))
  5604. '(2 'org-special-keyword t))
  5605. ;; Blocks and meta lines
  5606. '(org-fontify-meta-lines-and-blocks))))
  5607. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5608. (run-hooks 'org-font-lock-set-keywords-hook)
  5609. ;; Now set the full font-lock-keywords
  5610. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5611. (org-set-local 'font-lock-defaults
  5612. '(org-font-lock-keywords t nil nil backward-paragraph))
  5613. (kill-local-variable 'font-lock-keywords) nil))
  5614. (defun org-toggle-pretty-entities ()
  5615. "Toggle the composition display of entities as UTF8 characters."
  5616. (interactive)
  5617. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5618. (org-restart-font-lock)
  5619. (if org-pretty-entities
  5620. (message "Entities are now displayed as UTF8 characters")
  5621. (save-restriction
  5622. (widen)
  5623. (org-decompose-region (point-min) (point-max))
  5624. (message "Entities are now displayed as plain text"))))
  5625. (defvar org-custom-properties-overlays nil
  5626. "List of overlays used for custom properties.")
  5627. (make-variable-buffer-local 'org-custom-properties-overlays)
  5628. (defun org-toggle-custom-properties-visibility ()
  5629. "Display or hide properties in `org-custom-properties'."
  5630. (interactive)
  5631. (if org-custom-properties-overlays
  5632. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5633. (setq org-custom-properties-overlays nil))
  5634. (unless (not org-custom-properties)
  5635. (save-excursion
  5636. (save-restriction
  5637. (widen)
  5638. (goto-char (point-min))
  5639. (while (re-search-forward org-property-re nil t)
  5640. (mapc (lambda(p)
  5641. (when (equal p (substring (match-string 1) 1 -1))
  5642. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5643. (overlay-put o 'invisible t)
  5644. (overlay-put o 'org-custom-property t)
  5645. (push o org-custom-properties-overlays))))
  5646. org-custom-properties)))))))
  5647. (defun org-fontify-entities (limit)
  5648. "Find an entity to fontify."
  5649. (let (ee)
  5650. (when org-pretty-entities
  5651. (catch 'match
  5652. (while (re-search-forward
  5653. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5654. limit t)
  5655. (if (and (not (org-in-indented-comment-line))
  5656. (setq ee (org-entity-get (match-string 1)))
  5657. (= (length (nth 6 ee)) 1))
  5658. (let*
  5659. ((end (if (equal (match-string 2) "{}")
  5660. (match-end 2)
  5661. (match-end 1))))
  5662. (add-text-properties
  5663. (match-beginning 0) end
  5664. (list 'font-lock-fontified t))
  5665. (compose-region (match-beginning 0) end
  5666. (nth 6 ee) nil)
  5667. (backward-char 1)
  5668. (throw 'match t))))
  5669. nil))))
  5670. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5671. "Fontify string S like in Org-mode."
  5672. (with-temp-buffer
  5673. (insert s)
  5674. (let ((org-odd-levels-only odd-levels))
  5675. (org-mode)
  5676. (font-lock-fontify-buffer)
  5677. (buffer-string))))
  5678. (defvar org-m nil)
  5679. (defvar org-l nil)
  5680. (defvar org-f nil)
  5681. (defun org-get-level-face (n)
  5682. "Get the right face for match N in font-lock matching of headlines."
  5683. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5684. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5685. (if org-cycle-level-faces
  5686. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5687. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5688. (cond
  5689. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5690. ((eq n 2) org-f)
  5691. (t (if org-level-color-stars-only nil org-f))))
  5692. (defun org-get-todo-face (kwd)
  5693. "Get the right face for a TODO keyword KWD.
  5694. If KWD is a number, get the corresponding match group."
  5695. (if (numberp kwd) (setq kwd (match-string kwd)))
  5696. (or (org-face-from-face-or-color
  5697. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5698. (and (member kwd org-done-keywords) 'org-done)
  5699. 'org-todo))
  5700. (defun org-face-from-face-or-color (context inherit face-or-color)
  5701. "Create a face list that inherits INHERIT, but sets the foreground color.
  5702. When FACE-OR-COLOR is not a string, just return it."
  5703. (if (stringp face-or-color)
  5704. (list :inherit inherit
  5705. (cdr (assoc context org-faces-easy-properties))
  5706. face-or-color)
  5707. face-or-color))
  5708. (defun org-font-lock-add-tag-faces (limit)
  5709. "Add the special tag faces."
  5710. (when (and org-tag-faces org-tags-special-faces-re)
  5711. (while (re-search-forward org-tags-special-faces-re limit t)
  5712. (add-text-properties (match-beginning 1) (match-end 1)
  5713. (list 'face (org-get-tag-face 1)
  5714. 'font-lock-fontified t))
  5715. (backward-char 1))))
  5716. (defun org-font-lock-add-priority-faces (limit)
  5717. "Add the special priority faces."
  5718. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5719. (when (save-match-data (org-at-heading-p))
  5720. (add-text-properties
  5721. (match-beginning 0) (match-end 0)
  5722. (list 'face (or (org-face-from-face-or-color
  5723. 'priority 'org-priority
  5724. (cdr (assoc (char-after (match-beginning 1))
  5725. org-priority-faces)))
  5726. 'org-priority)
  5727. 'font-lock-fontified t)))))
  5728. (defun org-get-tag-face (kwd)
  5729. "Get the right face for a TODO keyword KWD.
  5730. If KWD is a number, get the corresponding match group."
  5731. (if (numberp kwd) (setq kwd (match-string kwd)))
  5732. (or (org-face-from-face-or-color
  5733. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5734. 'org-tag))
  5735. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5736. "Remove fontification and activation overlays from links."
  5737. (font-lock-default-unfontify-region beg end)
  5738. (let* ((buffer-undo-list t)
  5739. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5740. (inhibit-modification-hooks t)
  5741. deactivate-mark buffer-file-name buffer-file-truename)
  5742. (org-decompose-region beg end)
  5743. (remove-text-properties beg end
  5744. '(mouse-face t keymap t org-linked-text t
  5745. invisible t intangible t
  5746. org-no-flyspell t org-emphasis t))
  5747. (org-remove-font-lock-display-properties beg end)))
  5748. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5749. ((raise 0.3) (height 0.7))
  5750. ((raise -0.5))
  5751. ((raise 0.5)))
  5752. "Display properties for showing superscripts and subscripts.")
  5753. (defun org-remove-font-lock-display-properties (beg end)
  5754. "Remove specific display properties that have been added by font lock.
  5755. The will remove the raise properties that are used to show superscripts
  5756. and subscripts."
  5757. (let (next prop)
  5758. (while (< beg end)
  5759. (setq next (next-single-property-change beg 'display nil end)
  5760. prop (get-text-property beg 'display))
  5761. (if (member prop org-script-display)
  5762. (put-text-property beg next 'display nil))
  5763. (setq beg next))))
  5764. (defun org-raise-scripts (limit)
  5765. "Add raise properties to sub/superscripts."
  5766. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5767. (if (re-search-forward
  5768. (if (eq org-use-sub-superscripts t)
  5769. org-match-substring-regexp
  5770. org-match-substring-with-braces-regexp)
  5771. limit t)
  5772. (let* ((pos (point)) table-p comment-p
  5773. (mpos (match-beginning 3))
  5774. (emph-p (get-text-property mpos 'org-emphasis))
  5775. (link-p (get-text-property mpos 'mouse-face))
  5776. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5777. (goto-char (point-at-bol))
  5778. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5779. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5780. (goto-char pos)
  5781. ;; Handle a_b^c
  5782. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5783. (if (or comment-p emph-p link-p keyw-p)
  5784. t
  5785. (put-text-property (match-beginning 3) (match-end 0)
  5786. 'display
  5787. (if (equal (char-after (match-beginning 2)) ?^)
  5788. (nth (if table-p 3 1) org-script-display)
  5789. (nth (if table-p 2 0) org-script-display)))
  5790. (add-text-properties (match-beginning 2) (match-end 2)
  5791. (list 'invisible t
  5792. 'org-dwidth t 'org-dwidth-n 1))
  5793. (if (and (eq (char-after (match-beginning 3)) ?{)
  5794. (eq (char-before (match-end 3)) ?}))
  5795. (progn
  5796. (add-text-properties
  5797. (match-beginning 3) (1+ (match-beginning 3))
  5798. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5799. (add-text-properties
  5800. (1- (match-end 3)) (match-end 3)
  5801. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5802. t)))))
  5803. ;;;; Visibility cycling, including org-goto and indirect buffer
  5804. ;;; Cycling
  5805. (defvar org-cycle-global-status nil)
  5806. (make-variable-buffer-local 'org-cycle-global-status)
  5807. (put 'org-cycle-global-status 'org-state t)
  5808. (defvar org-cycle-subtree-status nil)
  5809. (make-variable-buffer-local 'org-cycle-subtree-status)
  5810. (put 'org-cycle-subtree-status 'org-state t)
  5811. (defvar org-inlinetask-min-level)
  5812. (defun org-unlogged-message (&rest args)
  5813. "Display a message, but avoid logging it in the *Messages* buffer."
  5814. (let ((message-log-max nil))
  5815. (apply 'message args)))
  5816. ;;;###autoload
  5817. (defun org-cycle (&optional arg)
  5818. "TAB-action and visibility cycling for Org-mode.
  5819. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5820. is outline visibility cycling, but it also invokes other actions
  5821. in special contexts.
  5822. - When this function is called with a prefix argument, rotate the entire
  5823. buffer through 3 states (global cycling)
  5824. 1. OVERVIEW: Show only top-level headlines.
  5825. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5826. 3. SHOW ALL: Show everything.
  5827. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5828. determined by the variable `org-startup-folded', and by any VISIBILITY
  5829. properties in the buffer.
  5830. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5831. including any drawers.
  5832. - When inside a table, re-align the table and move to the next field.
  5833. - When point is at the beginning of a headline, rotate the subtree started
  5834. by this line through 3 different states (local cycling)
  5835. 1. FOLDED: Only the main headline is shown.
  5836. 2. CHILDREN: The main headline and the direct children are shown.
  5837. From this state, you can move to one of the children
  5838. and zoom in further.
  5839. 3. SUBTREE: Show the entire subtree, including body text.
  5840. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5841. - When point is at the beginning of an empty headline and the variable
  5842. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5843. of the headline by demoting and promoting it to likely levels. This
  5844. speeds up creation document structure by pressing TAB once or several
  5845. times right after creating a new headline.
  5846. - When there is a numeric prefix, go up to a heading with level ARG, do
  5847. a `show-subtree' and return to the previous cursor position. If ARG
  5848. is negative, go up that many levels.
  5849. - When point is not at the beginning of a headline, execute the global
  5850. binding for TAB, which is re-indenting the line. See the option
  5851. `org-cycle-emulate-tab' for details.
  5852. - Special case: if point is at the beginning of the buffer and there is
  5853. no headline in line 1, this function will act as if called with prefix arg
  5854. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5855. But only if also the variable `org-cycle-global-at-bob' is t."
  5856. (interactive "P")
  5857. (org-load-modules-maybe)
  5858. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5859. (and org-cycle-level-after-item/entry-creation
  5860. (or (org-cycle-level)
  5861. (org-cycle-item-indentation))))
  5862. (let* ((limit-level
  5863. (or org-cycle-max-level
  5864. (and (boundp 'org-inlinetask-min-level)
  5865. org-inlinetask-min-level
  5866. (1- org-inlinetask-min-level))))
  5867. (nstars (and limit-level
  5868. (if org-odd-levels-only
  5869. (and limit-level (1- (* limit-level 2)))
  5870. limit-level)))
  5871. (org-outline-regexp
  5872. (if (not (derived-mode-p 'org-mode))
  5873. outline-regexp
  5874. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5875. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5876. (not (looking-at org-outline-regexp))))
  5877. (org-cycle-hook
  5878. (if bob-special
  5879. (delq 'org-optimize-window-after-visibility-change
  5880. (copy-sequence org-cycle-hook))
  5881. org-cycle-hook))
  5882. (pos (point)))
  5883. (if (or bob-special (equal arg '(4)))
  5884. ;; special case: use global cycling
  5885. (setq arg t))
  5886. (cond
  5887. ((equal arg '(16))
  5888. (setq last-command 'dummy)
  5889. (org-set-startup-visibility)
  5890. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5891. ((equal arg '(64))
  5892. (show-all)
  5893. (org-unlogged-message "Entire buffer visible, including drawers"))
  5894. ;; Try cdlatex TAB completion
  5895. ((org-try-cdlatex-tab))
  5896. ;; Table: enter it or move to the next field.
  5897. ((org-at-table-p 'any)
  5898. (if (org-at-table.el-p)
  5899. (message "Use C-c ' to edit table.el tables")
  5900. (if arg (org-table-edit-field t)
  5901. (org-table-justify-field-maybe)
  5902. (call-interactively 'org-table-next-field))))
  5903. ((run-hook-with-args-until-success
  5904. 'org-tab-after-check-for-table-hook))
  5905. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5906. ((eq arg t) (org-cycle-internal-global))
  5907. ;; Drawers: delegate to `org-flag-drawer'.
  5908. ((and org-drawers org-drawer-regexp
  5909. (save-excursion
  5910. (beginning-of-line 1)
  5911. (looking-at org-drawer-regexp)))
  5912. (org-flag-drawer ; toggle block visibility
  5913. (not (get-char-property (match-end 0) 'invisible))))
  5914. ;; Show-subtree, ARG levels up from here.
  5915. ((integerp arg)
  5916. (save-excursion
  5917. (org-back-to-heading)
  5918. (outline-up-heading (if (< arg 0) (- arg)
  5919. (- (funcall outline-level) arg)))
  5920. (org-show-subtree)))
  5921. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5922. ((and (featurep 'org-inlinetask)
  5923. (org-inlinetask-at-task-p)
  5924. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5925. (org-inlinetask-toggle-visibility))
  5926. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5927. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5928. (save-excursion (beginning-of-line 1)
  5929. (looking-at org-outline-regexp)))
  5930. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5931. (org-cycle-internal-local))
  5932. ;; From there: TAB emulation and template completion.
  5933. (buffer-read-only (org-back-to-heading))
  5934. ((run-hook-with-args-until-success
  5935. 'org-tab-after-check-for-cycling-hook))
  5936. ((org-try-structure-completion))
  5937. ((run-hook-with-args-until-success
  5938. 'org-tab-before-tab-emulation-hook))
  5939. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5940. (or (not (bolp))
  5941. (not (looking-at org-outline-regexp))))
  5942. (call-interactively (global-key-binding "\t")))
  5943. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5944. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5945. (or (and (eq org-cycle-emulate-tab 'white)
  5946. (= (match-end 0) (point-at-eol)))
  5947. (and (eq org-cycle-emulate-tab 'whitestart)
  5948. (>= (match-end 0) pos))))
  5949. t
  5950. (eq org-cycle-emulate-tab t))
  5951. (call-interactively (global-key-binding "\t")))
  5952. (t (save-excursion
  5953. (org-back-to-heading)
  5954. (org-cycle)))))))
  5955. (defun org-cycle-internal-global ()
  5956. "Do the global cycling action."
  5957. ;; Hack to avoid display of messages for .org attachments in Gnus
  5958. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5959. (cond
  5960. ((and (eq last-command this-command)
  5961. (eq org-cycle-global-status 'overview))
  5962. ;; We just created the overview - now do table of contents
  5963. ;; This can be slow in very large buffers, so indicate action
  5964. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5965. (unless ga (org-unlogged-message "CONTENTS..."))
  5966. (org-content)
  5967. (unless ga (org-unlogged-message "CONTENTS...done"))
  5968. (setq org-cycle-global-status 'contents)
  5969. (run-hook-with-args 'org-cycle-hook 'contents))
  5970. ((and (eq last-command this-command)
  5971. (eq org-cycle-global-status 'contents))
  5972. ;; We just showed the table of contents - now show everything
  5973. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5974. (show-all)
  5975. (unless ga (org-unlogged-message "SHOW ALL"))
  5976. (setq org-cycle-global-status 'all)
  5977. (run-hook-with-args 'org-cycle-hook 'all))
  5978. (t
  5979. ;; Default action: go to overview
  5980. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5981. (org-overview)
  5982. (unless ga (org-unlogged-message "OVERVIEW"))
  5983. (setq org-cycle-global-status 'overview)
  5984. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5985. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  5986. (defun org-cycle-internal-local ()
  5987. "Do the local cycling action."
  5988. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5989. ;; First, determine end of headline (EOH), end of subtree or item
  5990. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5991. (save-excursion
  5992. (if (org-at-item-p)
  5993. (progn
  5994. (beginning-of-line)
  5995. (setq struct (org-list-struct))
  5996. (setq eoh (point-at-eol))
  5997. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5998. (setq has-children (org-list-has-child-p (point) struct)))
  5999. (org-back-to-heading)
  6000. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6001. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  6002. (setq has-children
  6003. (or (save-excursion
  6004. (let ((level (funcall outline-level)))
  6005. (outline-next-heading)
  6006. (and (org-at-heading-p t)
  6007. (> (funcall outline-level) level))))
  6008. (save-excursion
  6009. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6010. ;; Determine end invisible part of buffer (EOL)
  6011. (beginning-of-line 2)
  6012. ;; XEmacs doesn't have `next-single-char-property-change'
  6013. (if (featurep 'xemacs)
  6014. (while (and (not (eobp)) ;; this is like `next-line'
  6015. (get-char-property (1- (point)) 'invisible))
  6016. (beginning-of-line 2))
  6017. (while (and (not (eobp)) ;; this is like `next-line'
  6018. (get-char-property (1- (point)) 'invisible))
  6019. (goto-char (next-single-char-property-change (point) 'invisible))
  6020. (and (eolp) (beginning-of-line 2))))
  6021. (setq eol (point)))
  6022. ;; Find out what to do next and set `this-command'
  6023. (cond
  6024. ((= eos eoh)
  6025. ;; Nothing is hidden behind this heading
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6028. (org-unlogged-message "EMPTY ENTRY")
  6029. (setq org-cycle-subtree-status nil)
  6030. (save-excursion
  6031. (goto-char eos)
  6032. (outline-next-heading)
  6033. (if (outline-invisible-p) (org-flag-heading nil))))
  6034. ((and (or (>= eol eos)
  6035. (not (string-match "\\S-" (buffer-substring eol eos))))
  6036. (or has-children
  6037. (not (setq children-skipped
  6038. org-cycle-skip-children-state-if-no-children))))
  6039. ;; Entire subtree is hidden in one line: children view
  6040. (unless (org-before-first-heading-p)
  6041. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6042. (if (org-at-item-p)
  6043. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6044. (org-show-entry)
  6045. (org-with-limited-levels (show-children))
  6046. ;; FIXME: This slows down the func way too much.
  6047. ;; How keep drawers hidden in subtree anyway?
  6048. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6049. ;; (org-cycle-hide-drawers 'subtree))
  6050. ;; Fold every list in subtree to top-level items.
  6051. (when (eq org-cycle-include-plain-lists 'integrate)
  6052. (save-excursion
  6053. (org-back-to-heading)
  6054. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6055. (beginning-of-line 1)
  6056. (let* ((struct (org-list-struct))
  6057. (prevs (org-list-prevs-alist struct))
  6058. (end (org-list-get-bottom-point struct)))
  6059. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6060. (org-list-get-all-items (point) struct prevs))
  6061. (goto-char (if (< end eos) end eos)))))))
  6062. (org-unlogged-message "CHILDREN")
  6063. (save-excursion
  6064. (goto-char eos)
  6065. (outline-next-heading)
  6066. (if (outline-invisible-p) (org-flag-heading nil)))
  6067. (setq org-cycle-subtree-status 'children)
  6068. (unless (org-before-first-heading-p)
  6069. (run-hook-with-args 'org-cycle-hook 'children)))
  6070. ((or children-skipped
  6071. (and (eq last-command this-command)
  6072. (eq org-cycle-subtree-status 'children)))
  6073. ;; We just showed the children, or no children are there,
  6074. ;; now show everything.
  6075. (unless (org-before-first-heading-p)
  6076. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6077. (outline-flag-region eoh eos nil)
  6078. (org-unlogged-message
  6079. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6080. (setq org-cycle-subtree-status 'subtree)
  6081. (unless (org-before-first-heading-p)
  6082. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6083. (t
  6084. ;; Default action: hide the subtree.
  6085. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6086. (outline-flag-region eoh eos t)
  6087. (org-unlogged-message "FOLDED")
  6088. (setq org-cycle-subtree-status 'folded)
  6089. (unless (org-before-first-heading-p)
  6090. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6091. ;;;###autoload
  6092. (defun org-global-cycle (&optional arg)
  6093. "Cycle the global visibility. For details see `org-cycle'.
  6094. With \\[universal-argument] prefix arg, switch to startup visibility.
  6095. With a numeric prefix, show all headlines up to that level."
  6096. (interactive "P")
  6097. (let ((org-cycle-include-plain-lists
  6098. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6099. (cond
  6100. ((integerp arg)
  6101. (show-all)
  6102. (hide-sublevels arg)
  6103. (setq org-cycle-global-status 'contents))
  6104. ((equal arg '(4))
  6105. (org-set-startup-visibility)
  6106. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6107. (t
  6108. (org-cycle '(4))))))
  6109. (defun org-set-startup-visibility ()
  6110. "Set the visibility required by startup options and properties."
  6111. (cond
  6112. ((eq org-startup-folded t)
  6113. (org-overview))
  6114. ((eq org-startup-folded 'content)
  6115. (org-content))
  6116. ((or (eq org-startup-folded 'showeverything)
  6117. (eq org-startup-folded nil))
  6118. (show-all)))
  6119. (unless (eq org-startup-folded 'showeverything)
  6120. (if org-hide-block-startup (org-hide-block-all))
  6121. (org-set-visibility-according-to-property 'no-cleanup)
  6122. (org-cycle-hide-archived-subtrees 'all)
  6123. (org-cycle-hide-drawers 'all)
  6124. (org-cycle-show-empty-lines t)))
  6125. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6126. "Switch subtree visibilities according to :VISIBILITY: property."
  6127. (interactive)
  6128. (let (org-show-entry-below state)
  6129. (save-excursion
  6130. (goto-char (point-min))
  6131. (while (re-search-forward
  6132. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6133. nil t)
  6134. (setq state (match-string 1))
  6135. (save-excursion
  6136. (org-back-to-heading t)
  6137. (hide-subtree)
  6138. (org-reveal)
  6139. (cond
  6140. ((equal state '("fold" "folded"))
  6141. (hide-subtree))
  6142. ((equal state "children")
  6143. (org-show-hidden-entry)
  6144. (show-children))
  6145. ((equal state "content")
  6146. (save-excursion
  6147. (save-restriction
  6148. (org-narrow-to-subtree)
  6149. (org-content))))
  6150. ((member state '("all" "showall"))
  6151. (show-subtree)))))
  6152. (unless no-cleanup
  6153. (org-cycle-hide-archived-subtrees 'all)
  6154. (org-cycle-hide-drawers 'all)
  6155. (org-cycle-show-empty-lines 'all)))))
  6156. ;; This function uses outline-regexp instead of the more fundamental
  6157. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6158. ;; buffers, where outline-regexp is needed.
  6159. (defun org-overview ()
  6160. "Switch to overview mode, showing only top-level headlines.
  6161. Really, this shows all headlines with level equal or greater than the level
  6162. of the first headline in the buffer. This is important, because if the
  6163. first headline is not level one, then (hide-sublevels 1) gives confusing
  6164. results."
  6165. (interactive)
  6166. (let ((pos (point))
  6167. (level (save-excursion
  6168. (goto-char (point-min))
  6169. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6170. (progn
  6171. (goto-char (match-beginning 0))
  6172. (funcall outline-level))))))
  6173. (and level (hide-sublevels level))
  6174. (recenter '(4))
  6175. (goto-char pos)))
  6176. (defun org-content (&optional arg)
  6177. "Show all headlines in the buffer, like a table of contents.
  6178. With numerical argument N, show content up to level N."
  6179. (interactive "P")
  6180. (org-overview)
  6181. (save-excursion
  6182. ;; Visit all headings and show their offspring
  6183. (and (integerp arg) (org-overview))
  6184. (goto-char (point-max))
  6185. (catch 'exit
  6186. (while (and (progn (condition-case nil
  6187. (outline-previous-visible-heading 1)
  6188. (error (goto-char (point-min))))
  6189. t)
  6190. (looking-at org-outline-regexp))
  6191. (if (integerp arg)
  6192. (show-children (1- arg))
  6193. (show-branches))
  6194. (if (bobp) (throw 'exit nil))))))
  6195. (defun org-optimize-window-after-visibility-change (state)
  6196. "Adjust the window after a change in outline visibility.
  6197. This function is the default value of the hook `org-cycle-hook'."
  6198. (when (get-buffer-window (current-buffer))
  6199. (cond
  6200. ((eq state 'content) nil)
  6201. ((eq state 'all) nil)
  6202. ((eq state 'folded) nil)
  6203. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6204. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6205. (defun org-remove-empty-overlays-at (pos)
  6206. "Remove outline overlays that do not contain non-white stuff."
  6207. (mapc
  6208. (lambda (o)
  6209. (and (eq 'outline (overlay-get o 'invisible))
  6210. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6211. (overlay-end o))))
  6212. (delete-overlay o)))
  6213. (overlays-at pos)))
  6214. (defun org-clean-visibility-after-subtree-move ()
  6215. "Fix visibility issues after moving a subtree."
  6216. ;; First, find a reasonable region to look at:
  6217. ;; Start two siblings above, end three below
  6218. (let* ((beg (save-excursion
  6219. (and (org-get-last-sibling)
  6220. (org-get-last-sibling))
  6221. (point)))
  6222. (end (save-excursion
  6223. (and (org-get-next-sibling)
  6224. (org-get-next-sibling)
  6225. (org-get-next-sibling))
  6226. (if (org-at-heading-p)
  6227. (point-at-eol)
  6228. (point))))
  6229. (level (looking-at "\\*+"))
  6230. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6231. (save-excursion
  6232. (save-restriction
  6233. (narrow-to-region beg end)
  6234. (when re
  6235. ;; Properly fold already folded siblings
  6236. (goto-char (point-min))
  6237. (while (re-search-forward re nil t)
  6238. (if (and (not (outline-invisible-p))
  6239. (save-excursion
  6240. (goto-char (point-at-eol)) (outline-invisible-p)))
  6241. (hide-entry))))
  6242. (org-cycle-show-empty-lines 'overview)
  6243. (org-cycle-hide-drawers 'overview)))))
  6244. (defun org-cycle-show-empty-lines (state)
  6245. "Show empty lines above all visible headlines.
  6246. The region to be covered depends on STATE when called through
  6247. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6248. entire buffer covered. Note that an empty line is only shown if there
  6249. are at least `org-cycle-separator-lines' empty lines before the headline."
  6250. (when (not (= org-cycle-separator-lines 0))
  6251. (save-excursion
  6252. (let* ((n (abs org-cycle-separator-lines))
  6253. (re (cond
  6254. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6255. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6256. (t (let ((ns (number-to-string (- n 2))))
  6257. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6258. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6259. beg end b e)
  6260. (cond
  6261. ((memq state '(overview contents t))
  6262. (setq beg (point-min) end (point-max)))
  6263. ((memq state '(children folded))
  6264. (setq beg (point) end (progn (org-end-of-subtree t t)
  6265. (beginning-of-line 2)
  6266. (point)))))
  6267. (when beg
  6268. (goto-char beg)
  6269. (while (re-search-forward re end t)
  6270. (unless (get-char-property (match-end 1) 'invisible)
  6271. (setq e (match-end 1))
  6272. (if (< org-cycle-separator-lines 0)
  6273. (setq b (save-excursion
  6274. (goto-char (match-beginning 0))
  6275. (org-back-over-empty-lines)
  6276. (if (save-excursion
  6277. (goto-char (max (point-min) (1- (point))))
  6278. (org-at-heading-p))
  6279. (1- (point))
  6280. (point))))
  6281. (setq b (match-beginning 1)))
  6282. (outline-flag-region b e nil)))))))
  6283. ;; Never hide empty lines at the end of the file.
  6284. (save-excursion
  6285. (goto-char (point-max))
  6286. (outline-previous-heading)
  6287. (outline-end-of-heading)
  6288. (if (and (looking-at "[ \t\n]+")
  6289. (= (match-end 0) (point-max)))
  6290. (outline-flag-region (point) (match-end 0) nil))))
  6291. (defun org-show-empty-lines-in-parent ()
  6292. "Move to the parent and re-show empty lines before visible headlines."
  6293. (save-excursion
  6294. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6295. (org-cycle-show-empty-lines context))))
  6296. (defun org-files-list ()
  6297. "Return `org-agenda-files' list, plus all open org-mode files.
  6298. This is useful for operations that need to scan all of a user's
  6299. open and agenda-wise Org files."
  6300. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6301. (dolist (buf (buffer-list))
  6302. (with-current-buffer buf
  6303. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6304. (let ((file (expand-file-name (buffer-file-name))))
  6305. (unless (member file files)
  6306. (push file files))))))
  6307. files))
  6308. (defsubst org-entry-beginning-position ()
  6309. "Return the beginning position of the current entry."
  6310. (save-excursion (outline-back-to-heading t) (point)))
  6311. (defsubst org-entry-end-position ()
  6312. "Return the end position of the current entry."
  6313. (save-excursion (outline-next-heading) (point)))
  6314. (defun org-cycle-hide-drawers (state)
  6315. "Re-hide all drawers after a visibility state change."
  6316. (when (and (derived-mode-p 'org-mode)
  6317. (not (memq state '(overview folded contents))))
  6318. (save-excursion
  6319. (let* ((globalp (memq state '(contents all)))
  6320. (beg (if globalp (point-min) (point)))
  6321. (end (if globalp (point-max)
  6322. (if (eq state 'children)
  6323. (save-excursion (outline-next-heading) (point))
  6324. (org-end-of-subtree t)))))
  6325. (goto-char beg)
  6326. (while (re-search-forward org-drawer-regexp end t)
  6327. (org-flag-drawer t))))))
  6328. (defun org-cycle-hide-inline-tasks (state)
  6329. "Re-hide inline tasks when switching to 'contents or 'children
  6330. visibility state."
  6331. (case state
  6332. (contents
  6333. (when (org-bound-and-true-p org-inlinetask-min-level)
  6334. (hide-sublevels (1- org-inlinetask-min-level))))
  6335. (children
  6336. (when (featurep 'org-inlinetask)
  6337. (save-excursion
  6338. (while (and (outline-next-heading)
  6339. (org-inlinetask-at-task-p))
  6340. (org-inlinetask-toggle-visibility)
  6341. (org-inlinetask-goto-end)))))))
  6342. (defun org-flag-drawer (flag)
  6343. "When FLAG is non-nil, hide the drawer we are within.
  6344. Otherwise make it visible."
  6345. (save-excursion
  6346. (beginning-of-line 1)
  6347. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6348. (let ((b (match-end 0)))
  6349. (if (re-search-forward
  6350. "^[ \t]*:END:"
  6351. (save-excursion (outline-next-heading) (point)) t)
  6352. (outline-flag-region b (point-at-eol) flag)
  6353. (user-error ":END: line missing at position %s" b))))))
  6354. (defun org-subtree-end-visible-p ()
  6355. "Is the end of the current subtree visible?"
  6356. (pos-visible-in-window-p
  6357. (save-excursion (org-end-of-subtree t) (point))))
  6358. (defun org-first-headline-recenter (&optional N)
  6359. "Move cursor to the first headline and recenter the headline.
  6360. Optional argument N means put the headline into the Nth line of the window."
  6361. (goto-char (point-min))
  6362. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6363. (beginning-of-line)
  6364. (recenter (prefix-numeric-value N))))
  6365. ;;; Saving and restoring visibility
  6366. (defun org-outline-overlay-data (&optional use-markers)
  6367. "Return a list of the locations of all outline overlays.
  6368. These are overlays with the `invisible' property value `outline'.
  6369. The return value is a list of cons cells, with start and stop
  6370. positions for each overlay.
  6371. If USE-MARKERS is set, return the positions as markers."
  6372. (let (beg end)
  6373. (save-excursion
  6374. (save-restriction
  6375. (widen)
  6376. (delq nil
  6377. (mapcar (lambda (o)
  6378. (when (eq (overlay-get o 'invisible) 'outline)
  6379. (setq beg (overlay-start o)
  6380. end (overlay-end o))
  6381. (and beg end (> end beg)
  6382. (if use-markers
  6383. (cons (move-marker (make-marker) beg)
  6384. (move-marker (make-marker) end))
  6385. (cons beg end)))))
  6386. (overlays-in (point-min) (point-max))))))))
  6387. (defun org-set-outline-overlay-data (data)
  6388. "Create visibility overlays for all positions in DATA.
  6389. DATA should have been made by `org-outline-overlay-data'."
  6390. (let (o)
  6391. (save-excursion
  6392. (save-restriction
  6393. (widen)
  6394. (show-all)
  6395. (mapc (lambda (c)
  6396. (outline-flag-region (car c) (cdr c) t))
  6397. data)))))
  6398. ;;; Folding of blocks
  6399. (defvar org-hide-block-overlays nil
  6400. "Overlays hiding blocks.")
  6401. (make-variable-buffer-local 'org-hide-block-overlays)
  6402. (defun org-block-map (function &optional start end)
  6403. "Call FUNCTION at the head of all source blocks in the current buffer.
  6404. Optional arguments START and END can be used to limit the range."
  6405. (let ((start (or start (point-min)))
  6406. (end (or end (point-max))))
  6407. (save-excursion
  6408. (goto-char start)
  6409. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6410. (save-excursion
  6411. (save-match-data
  6412. (goto-char (match-beginning 0))
  6413. (funcall function)))))))
  6414. (defun org-hide-block-toggle-all ()
  6415. "Toggle the visibility of all blocks in the current buffer."
  6416. (org-block-map #'org-hide-block-toggle))
  6417. (defun org-hide-block-all ()
  6418. "Fold all blocks in the current buffer."
  6419. (interactive)
  6420. (org-show-block-all)
  6421. (org-block-map #'org-hide-block-toggle-maybe))
  6422. (defun org-show-block-all ()
  6423. "Unfold all blocks in the current buffer."
  6424. (interactive)
  6425. (mapc 'delete-overlay org-hide-block-overlays)
  6426. (setq org-hide-block-overlays nil))
  6427. (defun org-hide-block-toggle-maybe ()
  6428. "Toggle visibility of block at point."
  6429. (interactive)
  6430. (let ((case-fold-search t))
  6431. (if (save-excursion
  6432. (beginning-of-line 1)
  6433. (looking-at org-block-regexp))
  6434. (progn (org-hide-block-toggle)
  6435. t) ;; to signal that we took action
  6436. nil))) ;; to signal that we did not
  6437. (defun org-hide-block-toggle (&optional force)
  6438. "Toggle the visibility of the current block."
  6439. (interactive)
  6440. (save-excursion
  6441. (beginning-of-line)
  6442. (if (re-search-forward org-block-regexp nil t)
  6443. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6444. (end (match-end 0)) ;; end of entire body
  6445. ov)
  6446. (if (memq t (mapcar (lambda (overlay)
  6447. (eq (overlay-get overlay 'invisible)
  6448. 'org-hide-block))
  6449. (overlays-at start)))
  6450. (if (or (not force) (eq force 'off))
  6451. (mapc (lambda (ov)
  6452. (when (member ov org-hide-block-overlays)
  6453. (setq org-hide-block-overlays
  6454. (delq ov org-hide-block-overlays)))
  6455. (when (eq (overlay-get ov 'invisible)
  6456. 'org-hide-block)
  6457. (delete-overlay ov)))
  6458. (overlays-at start)))
  6459. (setq ov (make-overlay start end))
  6460. (overlay-put ov 'invisible 'org-hide-block)
  6461. ;; make the block accessible to isearch
  6462. (overlay-put
  6463. ov 'isearch-open-invisible
  6464. (lambda (ov)
  6465. (when (member ov org-hide-block-overlays)
  6466. (setq org-hide-block-overlays
  6467. (delq ov org-hide-block-overlays)))
  6468. (when (eq (overlay-get ov 'invisible)
  6469. 'org-hide-block)
  6470. (delete-overlay ov))))
  6471. (push ov org-hide-block-overlays)))
  6472. (user-error "Not looking at a source block"))))
  6473. ;; org-tab-after-check-for-cycling-hook
  6474. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6475. ;; Remove overlays when changing major mode
  6476. (add-hook 'org-mode-hook
  6477. (lambda () (org-add-hook 'change-major-mode-hook
  6478. 'org-show-block-all 'append 'local)))
  6479. ;;; Org-goto
  6480. (defvar org-goto-window-configuration nil)
  6481. (defvar org-goto-marker nil)
  6482. (defvar org-goto-map)
  6483. (defun org-goto-map ()
  6484. "Set the keymap `org-goto'."
  6485. (setq org-goto-map
  6486. (let ((map (make-sparse-keymap)))
  6487. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6488. mouse-drag-region universal-argument org-occur))
  6489. cmd)
  6490. (while (setq cmd (pop cmds))
  6491. (substitute-key-definition cmd cmd map global-map)))
  6492. (suppress-keymap map)
  6493. (org-defkey map "\C-m" 'org-goto-ret)
  6494. (org-defkey map [(return)] 'org-goto-ret)
  6495. (org-defkey map [(left)] 'org-goto-left)
  6496. (org-defkey map [(right)] 'org-goto-right)
  6497. (org-defkey map [(control ?g)] 'org-goto-quit)
  6498. (org-defkey map "\C-i" 'org-cycle)
  6499. (org-defkey map [(tab)] 'org-cycle)
  6500. (org-defkey map [(down)] 'outline-next-visible-heading)
  6501. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6502. (if org-goto-auto-isearch
  6503. (if (fboundp 'define-key-after)
  6504. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6505. nil)
  6506. (org-defkey map "q" 'org-goto-quit)
  6507. (org-defkey map "n" 'outline-next-visible-heading)
  6508. (org-defkey map "p" 'outline-previous-visible-heading)
  6509. (org-defkey map "f" 'outline-forward-same-level)
  6510. (org-defkey map "b" 'outline-backward-same-level)
  6511. (org-defkey map "u" 'outline-up-heading))
  6512. (org-defkey map "/" 'org-occur)
  6513. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6514. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6515. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6516. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6517. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6518. map)))
  6519. (defconst org-goto-help
  6520. "Browse buffer copy, to find location or copy text.%s
  6521. RET=jump to location C-g=quit and return to previous location
  6522. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6523. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6524. (defun org-goto (&optional alternative-interface)
  6525. "Look up a different location in the current file, keeping current visibility.
  6526. When you want look-up or go to a different location in a
  6527. document, the fastest way is often to fold the entire buffer and
  6528. then dive into the tree. This method has the disadvantage, that
  6529. the previous location will be folded, which may not be what you
  6530. want.
  6531. This command works around this by showing a copy of the current
  6532. buffer in an indirect buffer, in overview mode. You can dive
  6533. into the tree in that copy, use org-occur and incremental search
  6534. to find a location. When pressing RET or `Q', the command
  6535. returns to the original buffer in which the visibility is still
  6536. unchanged. After RET it will also jump to the location selected
  6537. in the indirect buffer and expose the headline hierarchy above.
  6538. With a prefix argument, use the alternative interface: e.g. if
  6539. `org-goto-interface' is 'outline use 'outline-path-completion."
  6540. (interactive "P")
  6541. (org-goto-map)
  6542. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6543. (org-refile-use-outline-path t)
  6544. (org-refile-target-verify-function nil)
  6545. (interface
  6546. (if (not alternative-interface)
  6547. org-goto-interface
  6548. (if (eq org-goto-interface 'outline)
  6549. 'outline-path-completion
  6550. 'outline)))
  6551. (org-goto-start-pos (point))
  6552. (selected-point
  6553. (if (eq interface 'outline)
  6554. (car (org-get-location (current-buffer) org-goto-help))
  6555. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6556. (org-refile-check-position pa)
  6557. (nth 3 pa)))))
  6558. (if selected-point
  6559. (progn
  6560. (org-mark-ring-push org-goto-start-pos)
  6561. (goto-char selected-point)
  6562. (if (or (outline-invisible-p) (org-invisible-p2))
  6563. (org-show-context 'org-goto)))
  6564. (message "Quit"))))
  6565. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6566. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6567. (defvar org-goto-local-auto-isearch-map) ; defined below
  6568. (defun org-get-location (buf help)
  6569. "Let the user select a location in the Org-mode buffer BUF.
  6570. This function uses a recursive edit. It returns the selected position
  6571. or nil."
  6572. (org-no-popups
  6573. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6574. (isearch-hide-immediately nil)
  6575. (isearch-search-fun-function
  6576. (lambda () 'org-goto-local-search-headings))
  6577. (org-goto-selected-point org-goto-exit-command))
  6578. (save-excursion
  6579. (save-window-excursion
  6580. (delete-other-windows)
  6581. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6582. (org-pop-to-buffer-same-window
  6583. (condition-case nil
  6584. (make-indirect-buffer (current-buffer) "*org-goto*")
  6585. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6586. (with-output-to-temp-buffer "*Org Help*"
  6587. (princ (format help (if org-goto-auto-isearch
  6588. " Just type for auto-isearch."
  6589. " n/p/f/b/u to navigate, q to quit."))))
  6590. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6591. (setq buffer-read-only nil)
  6592. (let ((org-startup-truncated t)
  6593. (org-startup-folded nil)
  6594. (org-startup-align-all-tables nil))
  6595. (org-mode)
  6596. (org-overview))
  6597. (setq buffer-read-only t)
  6598. (if (and (boundp 'org-goto-start-pos)
  6599. (integer-or-marker-p org-goto-start-pos))
  6600. (let ((org-show-hierarchy-above t)
  6601. (org-show-siblings t)
  6602. (org-show-following-heading t))
  6603. (goto-char org-goto-start-pos)
  6604. (and (outline-invisible-p) (org-show-context)))
  6605. (goto-char (point-min)))
  6606. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6607. (message "Select location and press RET")
  6608. (use-local-map org-goto-map)
  6609. (recursive-edit)))
  6610. (kill-buffer "*org-goto*")
  6611. (cons org-goto-selected-point org-goto-exit-command))))
  6612. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6613. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6614. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6615. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6616. (defun org-goto-local-search-headings (string bound noerror)
  6617. "Search and make sure that any matches are in headlines."
  6618. (catch 'return
  6619. (while (if isearch-forward
  6620. (search-forward string bound noerror)
  6621. (search-backward string bound noerror))
  6622. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6623. (and (member :headline context)
  6624. (not (member :tags context))))
  6625. (throw 'return (point))))))
  6626. (defun org-goto-local-auto-isearch ()
  6627. "Start isearch."
  6628. (interactive)
  6629. (goto-char (point-min))
  6630. (let ((keys (this-command-keys)))
  6631. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6632. (isearch-mode t)
  6633. (isearch-process-search-char (string-to-char keys)))))
  6634. (defun org-goto-ret (&optional arg)
  6635. "Finish `org-goto' by going to the new location."
  6636. (interactive "P")
  6637. (setq org-goto-selected-point (point)
  6638. org-goto-exit-command 'return)
  6639. (throw 'exit nil))
  6640. (defun org-goto-left ()
  6641. "Finish `org-goto' by going to the new location."
  6642. (interactive)
  6643. (if (org-at-heading-p)
  6644. (progn
  6645. (beginning-of-line 1)
  6646. (setq org-goto-selected-point (point)
  6647. org-goto-exit-command 'left)
  6648. (throw 'exit nil))
  6649. (user-error "Not on a heading")))
  6650. (defun org-goto-right ()
  6651. "Finish `org-goto' by going to the new location."
  6652. (interactive)
  6653. (if (org-at-heading-p)
  6654. (progn
  6655. (setq org-goto-selected-point (point)
  6656. org-goto-exit-command 'right)
  6657. (throw 'exit nil))
  6658. (user-error "Not on a heading")))
  6659. (defun org-goto-quit ()
  6660. "Finish `org-goto' without cursor motion."
  6661. (interactive)
  6662. (setq org-goto-selected-point nil)
  6663. (setq org-goto-exit-command 'quit)
  6664. (throw 'exit nil))
  6665. ;;; Indirect buffer display of subtrees
  6666. (defvar org-indirect-dedicated-frame nil
  6667. "This is the frame being used for indirect tree display.")
  6668. (defvar org-last-indirect-buffer nil)
  6669. (defun org-tree-to-indirect-buffer (&optional arg)
  6670. "Create indirect buffer and narrow it to current subtree.
  6671. With a numerical prefix ARG, go up to this level and then take that tree.
  6672. If ARG is negative, go up that many levels.
  6673. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6674. indirect buffer previously made with this command, to avoid proliferation of
  6675. indirect buffers. However, when you call the command with a \
  6676. \\[universal-argument] prefix, or
  6677. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6678. is kept so that you can work with several indirect buffers at the same time.
  6679. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6680. \\[universal-argument] prefix also
  6681. requests that a new frame be made for the new buffer, so that the dedicated
  6682. frame is not changed."
  6683. (interactive "P")
  6684. (let ((cbuf (current-buffer))
  6685. (cwin (selected-window))
  6686. (pos (point))
  6687. beg end level heading ibuf)
  6688. (save-excursion
  6689. (org-back-to-heading t)
  6690. (when (numberp arg)
  6691. (setq level (org-outline-level))
  6692. (if (< arg 0) (setq arg (+ level arg)))
  6693. (while (> (setq level (org-outline-level)) arg)
  6694. (org-up-heading-safe)))
  6695. (setq beg (point)
  6696. heading (org-get-heading))
  6697. (org-end-of-subtree t t)
  6698. (if (org-at-heading-p) (backward-char 1))
  6699. (setq end (point)))
  6700. (if (and (buffer-live-p org-last-indirect-buffer)
  6701. (not (eq org-indirect-buffer-display 'new-frame))
  6702. (not arg))
  6703. (kill-buffer org-last-indirect-buffer))
  6704. (setq ibuf (org-get-indirect-buffer cbuf)
  6705. org-last-indirect-buffer ibuf)
  6706. (cond
  6707. ((or (eq org-indirect-buffer-display 'new-frame)
  6708. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6709. (select-frame (make-frame))
  6710. (delete-other-windows)
  6711. (org-pop-to-buffer-same-window ibuf)
  6712. (org-set-frame-title heading))
  6713. ((eq org-indirect-buffer-display 'dedicated-frame)
  6714. (raise-frame
  6715. (select-frame (or (and org-indirect-dedicated-frame
  6716. (frame-live-p org-indirect-dedicated-frame)
  6717. org-indirect-dedicated-frame)
  6718. (setq org-indirect-dedicated-frame (make-frame)))))
  6719. (delete-other-windows)
  6720. (org-pop-to-buffer-same-window ibuf)
  6721. (org-set-frame-title (concat "Indirect: " heading)))
  6722. ((eq org-indirect-buffer-display 'current-window)
  6723. (org-pop-to-buffer-same-window ibuf))
  6724. ((eq org-indirect-buffer-display 'other-window)
  6725. (pop-to-buffer ibuf))
  6726. (t (error "Invalid value")))
  6727. (if (featurep 'xemacs)
  6728. (save-excursion (org-mode) (turn-on-font-lock)))
  6729. (narrow-to-region beg end)
  6730. (show-all)
  6731. (goto-char pos)
  6732. (run-hook-with-args 'org-cycle-hook 'all)
  6733. (and (window-live-p cwin) (select-window cwin))))
  6734. (defun org-get-indirect-buffer (&optional buffer)
  6735. (setq buffer (or buffer (current-buffer)))
  6736. (let ((n 1) (base (buffer-name buffer)) bname)
  6737. (while (buffer-live-p
  6738. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6739. (setq n (1+ n)))
  6740. (condition-case nil
  6741. (make-indirect-buffer buffer bname 'clone)
  6742. (error (make-indirect-buffer buffer bname)))))
  6743. (defun org-set-frame-title (title)
  6744. "Set the title of the current frame to the string TITLE."
  6745. ;; FIXME: how to name a single frame in XEmacs???
  6746. (unless (featurep 'xemacs)
  6747. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6748. ;;;; Structure editing
  6749. ;;; Inserting headlines
  6750. (defun org-previous-line-empty-p (&optional next)
  6751. "Is the previous line a blank line?
  6752. When NEXT is non-nil, check the next line instead."
  6753. (save-excursion
  6754. (and (not (bobp))
  6755. (or (beginning-of-line (if next 2 0)) t)
  6756. (save-match-data
  6757. (looking-at "[ \t]*$")))))
  6758. (defun org-insert-heading (&optional arg invisible-ok)
  6759. "Insert a new heading or item with same depth at point.
  6760. If point is at the beginning of a heading or a list item, insert
  6761. a heading or a list item before it.
  6762. If point is at the beginning of a normal line, turn this line
  6763. into a heading.
  6764. If point is in the middle of a headline or a list item, split the
  6765. headline or the item and create a new headline/item with the text
  6766. in the current line after point \(see `org-M-RET-may-split-line'
  6767. on how to modify this behavior).
  6768. With one universal prefix argument: If point is within a list,
  6769. insert a heading instead of a list item. Otherwise, set the
  6770. value of `org-insert-heading-respect-content' to `t' for the
  6771. duration of the command.
  6772. With two universal prefix arguments, insert the heading at the
  6773. end of the grandparent subtree. For example, if point is within
  6774. a 2nd-level heading, then it will insert a 2nd-level heading at
  6775. the end of the 1st-level parent heading.
  6776. When INVISIBLE-OK is set, stop at invisible headlines when going
  6777. back. This is important for non-interactive uses of the
  6778. command."
  6779. (interactive "P")
  6780. (if (org-called-interactively-p 'any) (org-reveal))
  6781. (let ((itemp (org-in-item-p))
  6782. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6783. (respect-content (or org-insert-heading-respect-content
  6784. (equal arg '(4))))
  6785. (initial-content "")
  6786. (adjust-empty-lines t))
  6787. (cond
  6788. ((or (= (buffer-size) 0)
  6789. (and (not (save-excursion
  6790. (and (ignore-errors (org-back-to-heading invisible-ok))
  6791. (org-at-heading-p))))
  6792. (or arg (not itemp))))
  6793. ;; At beginning of buffer or so high up that only a heading
  6794. ;; makes sense.
  6795. (insert
  6796. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6797. (if (org-in-src-block-p) ",* " "* "))
  6798. (run-hooks 'org-insert-heading-hook))
  6799. ((and itemp (not (equal arg '(4))))
  6800. ;; Insert an item
  6801. (org-insert-item))
  6802. (t
  6803. ;; Maybe move at the end of the subtree
  6804. (when (equal arg '(16))
  6805. (org-up-heading-safe)
  6806. (org-end-of-subtree t))
  6807. ;; Insert a heading
  6808. (save-restriction
  6809. (widen)
  6810. (let* ((level nil)
  6811. (on-heading (org-at-heading-p))
  6812. (empty-line-p (if on-heading
  6813. (org-previous-line-empty-p)
  6814. ;; We will decide later
  6815. nil))
  6816. ;; Get a level string to fall back on
  6817. (fix-level
  6818. (save-excursion
  6819. (org-back-to-heading t)
  6820. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6821. (looking-at org-outline-regexp)
  6822. (make-string (1- (length (match-string 0))) ?*)))
  6823. (stars
  6824. (save-excursion
  6825. (condition-case nil
  6826. (progn
  6827. (org-back-to-heading invisible-ok)
  6828. (when (and (not on-heading)
  6829. (featurep 'org-inlinetask)
  6830. (integerp org-inlinetask-min-level)
  6831. (>= (length (match-string 0))
  6832. org-inlinetask-min-level))
  6833. ;; Find a heading level before the inline task
  6834. (while (and (setq level (org-up-heading-safe))
  6835. (>= level org-inlinetask-min-level)))
  6836. (if (org-at-heading-p)
  6837. (org-back-to-heading invisible-ok)
  6838. (error "This should not happen")))
  6839. (unless (and (save-excursion
  6840. (save-match-data
  6841. (org-backward-heading-same-level
  6842. 1 invisible-ok))
  6843. (= (point) (match-beginning 0)))
  6844. (not (org-previous-line-empty-p t)))
  6845. (setq empty-line-p (or empty-line-p
  6846. (org-previous-line-empty-p))))
  6847. (match-string 0))
  6848. (error (or fix-level "* ")))))
  6849. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6850. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6851. pos hide-previous previous-pos)
  6852. ;; If we insert after content, move there and clean up whitespace
  6853. (when (and respect-content
  6854. (not (org-looking-at-p org-outline-regexp-bol)))
  6855. (org-end-of-subtree nil t)
  6856. (skip-chars-backward " \r\n")
  6857. (and (not (looking-back "^\*+"))
  6858. (looking-at "[ \t]+") (replace-match ""))
  6859. (unless (eobp) (forward-char 1))
  6860. (when (looking-at "^\\*")
  6861. (unless (bobp) (backward-char 1))
  6862. (insert "\n")))
  6863. ;; If we are splitting, grab the text that should be moved to the new headline
  6864. (when may-split
  6865. (if (org-on-heading-p)
  6866. ;; This is a heading, we split intelligently (keeping tags)
  6867. (let ((pos (point)))
  6868. (goto-char (point-at-bol))
  6869. (unless (looking-at org-complex-heading-regexp)
  6870. (error "This should not happen"))
  6871. (when (and (match-beginning 4)
  6872. (> pos (match-beginning 4))
  6873. (< pos (match-end 4)))
  6874. (setq initial-content (buffer-substring pos (match-end 4)))
  6875. (goto-char pos)
  6876. (delete-region (point) (match-end 4))
  6877. (if (looking-at "[ \t]*$")
  6878. (replace-match "")
  6879. (insert (make-string (length initial-content) ?\ )))
  6880. (setq initial-content (org-trim initial-content)))
  6881. (goto-char pos))
  6882. ;; a normal line
  6883. (unless (bolp)
  6884. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6885. (delete-region (point) (point-at-eol))
  6886. (setq initial-content (org-trim initial-content)))))
  6887. ;; If we are at the beginning of the line, insert before it. Else after
  6888. (cond
  6889. ((and (bolp) (looking-at "[ \t]*$")))
  6890. ((and (bolp) (not (looking-at "[ \t]*$")))
  6891. (open-line 1))
  6892. (t
  6893. (goto-char (point-at-eol))
  6894. (insert "\n")))
  6895. ;; Insert the new heading
  6896. (insert stars)
  6897. (just-one-space)
  6898. (insert initial-content)
  6899. (when adjust-empty-lines
  6900. (if (or (not blank)
  6901. (and blank (not (org-previous-line-empty-p))))
  6902. (org-N-empty-lines-before-current (if blank 1 0))))
  6903. (run-hooks 'org-insert-heading-hook)))))))
  6904. (defun org-N-empty-lines-before-current (N)
  6905. "Make the number of empty lines before current exactly N.
  6906. So this will delete or add empty lines."
  6907. (save-excursion
  6908. (goto-char (point-at-bol))
  6909. (if (looking-back "\\s-+" nil 'greedy)
  6910. (replace-match ""))
  6911. (or (bobp) (insert "\n"))
  6912. (while (> N 0)
  6913. (insert "\n")
  6914. (setq N (1- N)))))
  6915. (defun org-get-heading (&optional no-tags no-todo)
  6916. "Return the heading of the current entry, without the stars.
  6917. When NO-TAGS is non-nil, don't include tags.
  6918. When NO-TODO is non-nil, don't include TODO keywords."
  6919. (save-excursion
  6920. (org-back-to-heading t)
  6921. (cond
  6922. ((and no-tags no-todo)
  6923. (looking-at org-complex-heading-regexp)
  6924. (match-string 4))
  6925. (no-tags
  6926. (looking-at (concat org-outline-regexp
  6927. "\\(.*?\\)"
  6928. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6929. (match-string 1))
  6930. (no-todo
  6931. (looking-at org-todo-line-regexp)
  6932. (match-string 3))
  6933. (t (looking-at org-heading-regexp)
  6934. (match-string 2)))))
  6935. (defvar orgstruct-mode) ; defined below
  6936. (defun org-heading-components ()
  6937. "Return the components of the current heading.
  6938. This is a list with the following elements:
  6939. - the level as an integer
  6940. - the reduced level, different if `org-odd-levels-only' is set.
  6941. - the TODO keyword, or nil
  6942. - the priority character, like ?A, or nil if no priority is given
  6943. - the headline text itself, or the tags string if no headline text
  6944. - the tags string, or nil."
  6945. (save-excursion
  6946. (org-back-to-heading t)
  6947. (if (let (case-fold-search)
  6948. (looking-at
  6949. (if orgstruct-mode
  6950. org-heading-regexp
  6951. org-complex-heading-regexp)))
  6952. (if orgstruct-mode
  6953. (list (length (match-string 1))
  6954. (org-reduced-level (length (match-string 1)))
  6955. nil
  6956. nil
  6957. (match-string 2)
  6958. nil)
  6959. (list (length (match-string 1))
  6960. (org-reduced-level (length (match-string 1)))
  6961. (org-match-string-no-properties 2)
  6962. (and (match-end 3) (aref (match-string 3) 2))
  6963. (org-match-string-no-properties 4)
  6964. (org-match-string-no-properties 5))))))
  6965. (defun org-get-entry ()
  6966. "Get the entry text, after heading, entire subtree."
  6967. (save-excursion
  6968. (org-back-to-heading t)
  6969. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6970. (defun org-insert-heading-after-current ()
  6971. "Insert a new heading with same level as current, after current subtree."
  6972. (interactive)
  6973. (org-back-to-heading)
  6974. (org-insert-heading)
  6975. (org-move-subtree-down)
  6976. (end-of-line 1))
  6977. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6978. "Insert heading with `org-insert-heading-respect-content' set to t."
  6979. (interactive "P")
  6980. (let ((org-insert-heading-respect-content t))
  6981. (org-insert-heading '(4) invisible-ok)))
  6982. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6983. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6984. (interactive "P")
  6985. (let ((org-insert-heading-respect-content t))
  6986. (org-insert-todo-heading force-state '(4))))
  6987. (defun org-insert-todo-heading (arg &optional force-heading)
  6988. "Insert a new heading with the same level and TODO state as current heading.
  6989. If the heading has no TODO state, or if the state is DONE, use the first
  6990. state (TODO by default). Also with one prefix arg, force first state. With
  6991. two prefix args, force inserting at the end of the parent subtree."
  6992. (interactive "P")
  6993. (when (or force-heading (not (org-insert-item 'checkbox)))
  6994. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6995. force-heading))
  6996. (save-excursion
  6997. (org-back-to-heading)
  6998. (outline-previous-heading)
  6999. (looking-at org-todo-line-regexp))
  7000. (let*
  7001. ((new-mark-x
  7002. (if (or (equal arg '(4))
  7003. (not (match-beginning 2))
  7004. (member (match-string 2) org-done-keywords))
  7005. (car org-todo-keywords-1)
  7006. (match-string 2)))
  7007. (new-mark
  7008. (or
  7009. (run-hook-with-args-until-success
  7010. 'org-todo-get-default-hook new-mark-x nil)
  7011. new-mark-x)))
  7012. (beginning-of-line 1)
  7013. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7014. (if org-treat-insert-todo-heading-as-state-change
  7015. (org-todo new-mark)
  7016. (insert new-mark " "))))
  7017. (when org-provide-todo-statistics
  7018. (org-update-parent-todo-statistics))))
  7019. (defun org-insert-subheading (arg)
  7020. "Insert a new subheading and demote it.
  7021. Works for outline headings and for plain lists alike."
  7022. (interactive "P")
  7023. (org-insert-heading arg)
  7024. (cond
  7025. ((org-at-heading-p) (org-do-demote))
  7026. ((org-at-item-p) (org-indent-item))))
  7027. (defun org-insert-todo-subheading (arg)
  7028. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7029. Works for outline headings and for plain lists alike."
  7030. (interactive "P")
  7031. (org-insert-todo-heading arg)
  7032. (cond
  7033. ((org-at-heading-p) (org-do-demote))
  7034. ((org-at-item-p) (org-indent-item))))
  7035. ;;; Promotion and Demotion
  7036. (defvar org-after-demote-entry-hook nil
  7037. "Hook run after an entry has been demoted.
  7038. The cursor will be at the beginning of the entry.
  7039. When a subtree is being demoted, the hook will be called for each node.")
  7040. (defvar org-after-promote-entry-hook nil
  7041. "Hook run after an entry has been promoted.
  7042. The cursor will be at the beginning of the entry.
  7043. When a subtree is being promoted, the hook will be called for each node.")
  7044. (defun org-promote-subtree ()
  7045. "Promote the entire subtree.
  7046. See also `org-promote'."
  7047. (interactive)
  7048. (save-excursion
  7049. (org-with-limited-levels (org-map-tree 'org-promote)))
  7050. (org-fix-position-after-promote))
  7051. (defun org-demote-subtree ()
  7052. "Demote the entire subtree. See `org-demote'.
  7053. See also `org-promote'."
  7054. (interactive)
  7055. (save-excursion
  7056. (org-with-limited-levels (org-map-tree 'org-demote)))
  7057. (org-fix-position-after-promote))
  7058. (defun org-do-promote ()
  7059. "Promote the current heading higher up the tree.
  7060. If the region is active in `transient-mark-mode', promote all headings
  7061. in the region."
  7062. (interactive)
  7063. (save-excursion
  7064. (if (org-region-active-p)
  7065. (org-map-region 'org-promote (region-beginning) (region-end))
  7066. (org-promote)))
  7067. (org-fix-position-after-promote))
  7068. (defun org-do-demote ()
  7069. "Demote the current heading lower down the tree.
  7070. If the region is active in `transient-mark-mode', demote all headings
  7071. in the region."
  7072. (interactive)
  7073. (save-excursion
  7074. (if (org-region-active-p)
  7075. (org-map-region 'org-demote (region-beginning) (region-end))
  7076. (org-demote)))
  7077. (org-fix-position-after-promote))
  7078. (defun org-fix-position-after-promote ()
  7079. "Make sure that after pro/demotion cursor position is right."
  7080. (let ((pos (point)))
  7081. (when (save-excursion
  7082. (beginning-of-line 1)
  7083. (looking-at org-todo-line-regexp)
  7084. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7085. (cond ((eobp) (insert " "))
  7086. ((eolp) (insert " "))
  7087. ((equal (char-after) ?\ ) (forward-char 1))))))
  7088. (defun org-current-level ()
  7089. "Return the level of the current entry, or nil if before the first headline.
  7090. The level is the number of stars at the beginning of the headline."
  7091. (save-excursion
  7092. (org-with-limited-levels
  7093. (if (ignore-errors (org-back-to-heading t))
  7094. (funcall outline-level)))))
  7095. (defun org-get-previous-line-level ()
  7096. "Return the outline depth of the last headline before the current line.
  7097. Returns 0 for the first headline in the buffer, and nil if before the
  7098. first headline."
  7099. (and (org-current-level)
  7100. (or (and (/= (line-beginning-position) (point-min))
  7101. (save-excursion (beginning-of-line 0) (org-current-level)))
  7102. 0)))
  7103. (defun org-reduced-level (l)
  7104. "Compute the effective level of a heading.
  7105. This takes into account the setting of `org-odd-levels-only'."
  7106. (cond
  7107. ((zerop l) 0)
  7108. (org-odd-levels-only (1+ (floor (/ l 2))))
  7109. (t l)))
  7110. (defun org-level-increment ()
  7111. "Return the number of stars that will be added or removed at a
  7112. time to headlines when structure editing, based on the value of
  7113. `org-odd-levels-only'."
  7114. (if org-odd-levels-only 2 1))
  7115. (defun org-get-valid-level (level &optional change)
  7116. "Rectify a level change under the influence of `org-odd-levels-only'
  7117. LEVEL is a current level, CHANGE is by how much the level should be
  7118. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7119. even level numbers will become the next higher odd number."
  7120. (if org-odd-levels-only
  7121. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7122. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7123. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7124. (max 1 (+ level (or change 0)))))
  7125. (if (boundp 'define-obsolete-function-alias)
  7126. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7127. (define-obsolete-function-alias 'org-get-legal-level
  7128. 'org-get-valid-level)
  7129. (define-obsolete-function-alias 'org-get-legal-level
  7130. 'org-get-valid-level "23.1")))
  7131. (defun org-promote ()
  7132. "Promote the current heading higher up the tree.
  7133. If the region is active in `transient-mark-mode', promote all headings
  7134. in the region."
  7135. (org-back-to-heading t)
  7136. (let* ((level (save-match-data (funcall outline-level)))
  7137. (after-change-functions (remove 'flyspell-after-change-function
  7138. after-change-functions))
  7139. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7140. (diff (abs (- level (length up-head) -1))))
  7141. (cond ((and (= level 1) org-called-with-limited-levels
  7142. org-allow-promoting-top-level-subtree)
  7143. (replace-match "# " nil t))
  7144. ((= level 1)
  7145. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7146. (t (replace-match up-head nil t)))
  7147. ;; Fixup tag positioning
  7148. (unless (= level 1)
  7149. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7150. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7151. (run-hooks 'org-after-promote-entry-hook)))
  7152. (defun org-demote ()
  7153. "Demote the current heading lower down the tree.
  7154. If the region is active in `transient-mark-mode', demote all headings
  7155. in the region."
  7156. (org-back-to-heading t)
  7157. (let* ((level (save-match-data (funcall outline-level)))
  7158. (after-change-functions (remove 'flyspell-after-change-function
  7159. after-change-functions))
  7160. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7161. (diff (abs (- level (length down-head) -1))))
  7162. (replace-match down-head nil t)
  7163. ;; Fixup tag positioning
  7164. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7165. (if org-adapt-indentation (org-fixup-indentation diff))
  7166. (run-hooks 'org-after-demote-entry-hook)))
  7167. (defun org-cycle-level ()
  7168. "Cycle the level of an empty headline through possible states.
  7169. This goes first to child, then to parent, level, then up the hierarchy.
  7170. After top level, it switches back to sibling level."
  7171. (interactive)
  7172. (let ((org-adapt-indentation nil))
  7173. (when (org-point-at-end-of-empty-headline)
  7174. (setq this-command 'org-cycle-level) ; Only needed for caching
  7175. (let ((cur-level (org-current-level))
  7176. (prev-level (org-get-previous-line-level)))
  7177. (cond
  7178. ;; If first headline in file, promote to top-level.
  7179. ((= prev-level 0)
  7180. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7181. do (org-do-promote)))
  7182. ;; If same level as prev, demote one.
  7183. ((= prev-level cur-level)
  7184. (org-do-demote))
  7185. ;; If parent is top-level, promote to top level if not already.
  7186. ((= prev-level 1)
  7187. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7188. do (org-do-promote)))
  7189. ;; If top-level, return to prev-level.
  7190. ((= cur-level 1)
  7191. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7192. do (org-do-demote)))
  7193. ;; If less than prev-level, promote one.
  7194. ((< cur-level prev-level)
  7195. (org-do-promote))
  7196. ;; If deeper than prev-level, promote until higher than
  7197. ;; prev-level.
  7198. ((> cur-level prev-level)
  7199. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7200. do (org-do-promote))))
  7201. t))))
  7202. (defun org-map-tree (fun)
  7203. "Call FUN for every heading underneath the current one."
  7204. (org-back-to-heading)
  7205. (let ((level (funcall outline-level)))
  7206. (save-excursion
  7207. (funcall fun)
  7208. (while (and (progn
  7209. (outline-next-heading)
  7210. (> (funcall outline-level) level))
  7211. (not (eobp)))
  7212. (funcall fun)))))
  7213. (defun org-map-region (fun beg end)
  7214. "Call FUN for every heading between BEG and END."
  7215. (let ((org-ignore-region t))
  7216. (save-excursion
  7217. (setq end (copy-marker end))
  7218. (goto-char beg)
  7219. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7220. (< (point) end))
  7221. (funcall fun))
  7222. (while (and (progn
  7223. (outline-next-heading)
  7224. (< (point) end))
  7225. (not (eobp)))
  7226. (funcall fun)))))
  7227. (defvar org-property-end-re) ; silence byte-compiler
  7228. (defun org-fixup-indentation (diff)
  7229. "Change the indentation in the current entry by DIFF.
  7230. However, if any line in the current entry has no indentation, or if it
  7231. would end up with no indentation after the change, nothing at all is done."
  7232. (save-excursion
  7233. (let ((end (save-excursion (outline-next-heading)
  7234. (point-marker)))
  7235. (prohibit (if (> diff 0)
  7236. "^\\S-"
  7237. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7238. col)
  7239. (unless (save-excursion (end-of-line 1)
  7240. (re-search-forward prohibit end t))
  7241. (while (and (< (point) end)
  7242. (re-search-forward "^[ \t]+" end t))
  7243. (goto-char (match-end 0))
  7244. (setq col (current-column))
  7245. (if (< diff 0) (replace-match ""))
  7246. (org-indent-to-column (+ diff col))))
  7247. (move-marker end nil))))
  7248. (defun org-convert-to-odd-levels ()
  7249. "Convert an org-mode file with all levels allowed to one with odd levels.
  7250. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7251. level 5 etc."
  7252. (interactive)
  7253. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7254. (let ((outline-level 'org-outline-level)
  7255. (org-odd-levels-only nil) n)
  7256. (save-excursion
  7257. (goto-char (point-min))
  7258. (while (re-search-forward "^\\*\\*+ " nil t)
  7259. (setq n (- (length (match-string 0)) 2))
  7260. (while (>= (setq n (1- n)) 0)
  7261. (org-demote))
  7262. (end-of-line 1))))))
  7263. (defun org-convert-to-oddeven-levels ()
  7264. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7265. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7266. file contains a section with an even level, conversion would
  7267. destroy the structure of the file. An error is signaled in this
  7268. case."
  7269. (interactive)
  7270. (goto-char (point-min))
  7271. ;; First check if there are no even levels
  7272. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7273. (org-show-context t)
  7274. (error "Not all levels are odd in this file. Conversion not possible"))
  7275. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7276. (let ((outline-regexp org-outline-regexp)
  7277. (outline-level 'org-outline-level)
  7278. (org-odd-levels-only nil) n)
  7279. (save-excursion
  7280. (goto-char (point-min))
  7281. (while (re-search-forward "^\\*\\*+ " nil t)
  7282. (setq n (/ (1- (length (match-string 0))) 2))
  7283. (while (>= (setq n (1- n)) 0)
  7284. (org-promote))
  7285. (end-of-line 1))))))
  7286. (defun org-tr-level (n)
  7287. "Make N odd if required."
  7288. (if org-odd-levels-only (1+ (/ n 2)) n))
  7289. ;;; Vertical tree motion, cutting and pasting of subtrees
  7290. (defun org-move-subtree-up (&optional arg)
  7291. "Move the current subtree up past ARG headlines of the same level."
  7292. (interactive "p")
  7293. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7294. (defun org-move-subtree-down (&optional arg)
  7295. "Move the current subtree down past ARG headlines of the same level."
  7296. (interactive "p")
  7297. (setq arg (prefix-numeric-value arg))
  7298. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7299. 'org-get-last-sibling))
  7300. (ins-point (make-marker))
  7301. (cnt (abs arg))
  7302. (col (current-column))
  7303. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7304. ;; Select the tree
  7305. (org-back-to-heading)
  7306. (setq beg0 (point))
  7307. (save-excursion
  7308. (setq ne-beg (org-back-over-empty-lines))
  7309. (setq beg (point)))
  7310. (save-match-data
  7311. (save-excursion (outline-end-of-heading)
  7312. (setq folded (outline-invisible-p)))
  7313. (outline-end-of-subtree))
  7314. (outline-next-heading)
  7315. (setq ne-end (org-back-over-empty-lines))
  7316. (setq end (point))
  7317. (goto-char beg0)
  7318. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7319. ;; include less whitespace
  7320. (save-excursion
  7321. (goto-char beg)
  7322. (forward-line (- ne-beg ne-end))
  7323. (setq beg (point))))
  7324. ;; Find insertion point, with error handling
  7325. (while (> cnt 0)
  7326. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7327. (progn (goto-char beg0)
  7328. (user-error "Cannot move past superior level or buffer limit")))
  7329. (setq cnt (1- cnt)))
  7330. (if (> arg 0)
  7331. ;; Moving forward - still need to move over subtree
  7332. (progn (org-end-of-subtree t t)
  7333. (save-excursion
  7334. (org-back-over-empty-lines)
  7335. (or (bolp) (newline)))))
  7336. (setq ne-ins (org-back-over-empty-lines))
  7337. (move-marker ins-point (point))
  7338. (setq txt (buffer-substring beg end))
  7339. (org-save-markers-in-region beg end)
  7340. (delete-region beg end)
  7341. (org-remove-empty-overlays-at beg)
  7342. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7343. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7344. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7345. (let ((bbb (point)))
  7346. (insert-before-markers txt)
  7347. (org-reinstall-markers-in-region bbb)
  7348. (move-marker ins-point bbb))
  7349. (or (bolp) (insert "\n"))
  7350. (setq ins-end (point))
  7351. (goto-char ins-point)
  7352. (org-skip-whitespace)
  7353. (when (and (< arg 0)
  7354. (org-first-sibling-p)
  7355. (> ne-ins ne-beg))
  7356. ;; Move whitespace back to beginning
  7357. (save-excursion
  7358. (goto-char ins-end)
  7359. (let ((kill-whole-line t))
  7360. (kill-line (- ne-ins ne-beg)) (point)))
  7361. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7362. (move-marker ins-point nil)
  7363. (if folded
  7364. (hide-subtree)
  7365. (org-show-entry)
  7366. (show-children)
  7367. (org-cycle-hide-drawers 'children))
  7368. (org-clean-visibility-after-subtree-move)
  7369. ;; move back to the initial column we were at
  7370. (move-to-column col)))
  7371. (defvar org-subtree-clip ""
  7372. "Clipboard for cut and paste of subtrees.
  7373. This is actually only a copy of the kill, because we use the normal kill
  7374. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7375. (defvar org-subtree-clip-folded nil
  7376. "Was the last copied subtree folded?
  7377. This is used to fold the tree back after pasting.")
  7378. (defun org-cut-subtree (&optional n)
  7379. "Cut the current subtree into the clipboard.
  7380. With prefix arg N, cut this many sequential subtrees.
  7381. This is a short-hand for marking the subtree and then cutting it."
  7382. (interactive "p")
  7383. (org-copy-subtree n 'cut))
  7384. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7385. "Copy the current subtree it in the clipboard.
  7386. With prefix arg N, copy this many sequential subtrees.
  7387. This is a short-hand for marking the subtree and then copying it.
  7388. If CUT is non-nil, actually cut the subtree.
  7389. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7390. of some markers in the region, even if CUT is non-nil. This is
  7391. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7392. (interactive "p")
  7393. (let (beg end folded (beg0 (point)))
  7394. (if (org-called-interactively-p 'any)
  7395. (org-back-to-heading nil) ; take what looks like a subtree
  7396. (org-back-to-heading t)) ; take what is really there
  7397. (setq beg (point))
  7398. (skip-chars-forward " \t\r\n")
  7399. (save-match-data
  7400. (if nosubtrees
  7401. (outline-next-heading)
  7402. (save-excursion (outline-end-of-heading)
  7403. (setq folded (outline-invisible-p)))
  7404. (condition-case nil
  7405. (org-forward-heading-same-level (1- n) t)
  7406. (error nil))
  7407. (org-end-of-subtree t t)))
  7408. (setq end (point))
  7409. (goto-char beg0)
  7410. (when (> end beg)
  7411. (setq org-subtree-clip-folded folded)
  7412. (when (or cut force-store-markers)
  7413. (org-save-markers-in-region beg end))
  7414. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7415. (setq org-subtree-clip (current-kill 0))
  7416. (message "%s: Subtree(s) with %d characters"
  7417. (if cut "Cut" "Copied")
  7418. (length org-subtree-clip)))))
  7419. (defun org-paste-subtree (&optional level tree for-yank)
  7420. "Paste the clipboard as a subtree, with modification of headline level.
  7421. The entire subtree is promoted or demoted in order to match a new headline
  7422. level.
  7423. If the cursor is at the beginning of a headline, the same level as
  7424. that headline is used to paste the tree.
  7425. If not, the new level is derived from the *visible* headings
  7426. before and after the insertion point, and taken to be the inferior headline
  7427. level of the two. So if the previous visible heading is level 3 and the
  7428. next is level 4 (or vice versa), level 4 will be used for insertion.
  7429. This makes sure that the subtree remains an independent subtree and does
  7430. not swallow low level entries.
  7431. You can also force a different level, either by using a numeric prefix
  7432. argument, or by inserting the heading marker by hand. For example, if the
  7433. cursor is after \"*****\", then the tree will be shifted to level 5.
  7434. If optional TREE is given, use this text instead of the kill ring.
  7435. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7436. move back over whitespace before inserting, and move point to the end of
  7437. the inserted text when done."
  7438. (interactive "P")
  7439. (setq tree (or tree (and kill-ring (current-kill 0))))
  7440. (unless (org-kill-is-subtree-p tree)
  7441. (user-error "%s"
  7442. (substitute-command-keys
  7443. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7444. (org-with-limited-levels
  7445. (let* ((visp (not (outline-invisible-p)))
  7446. (txt tree)
  7447. (^re_ "\\(\\*+\\)[ \t]*")
  7448. (old-level (if (string-match org-outline-regexp-bol txt)
  7449. (- (match-end 0) (match-beginning 0) 1)
  7450. -1))
  7451. (force-level (cond (level (prefix-numeric-value level))
  7452. ((and (looking-at "[ \t]*$")
  7453. (string-match
  7454. "^\\*+$" (buffer-substring
  7455. (point-at-bol) (point))))
  7456. (- (match-end 1) (match-beginning 1)))
  7457. ((and (bolp)
  7458. (looking-at org-outline-regexp))
  7459. (- (match-end 0) (point) 1))))
  7460. (previous-level (save-excursion
  7461. (condition-case nil
  7462. (progn
  7463. (outline-previous-visible-heading 1)
  7464. (if (looking-at ^re_)
  7465. (- (match-end 0) (match-beginning 0) 1)
  7466. 1))
  7467. (error 1))))
  7468. (next-level (save-excursion
  7469. (condition-case nil
  7470. (progn
  7471. (or (looking-at org-outline-regexp)
  7472. (outline-next-visible-heading 1))
  7473. (if (looking-at ^re_)
  7474. (- (match-end 0) (match-beginning 0) 1)
  7475. 1))
  7476. (error 1))))
  7477. (new-level (or force-level (max previous-level next-level)))
  7478. (shift (if (or (= old-level -1)
  7479. (= new-level -1)
  7480. (= old-level new-level))
  7481. 0
  7482. (- new-level old-level)))
  7483. (delta (if (> shift 0) -1 1))
  7484. (func (if (> shift 0) 'org-demote 'org-promote))
  7485. (org-odd-levels-only nil)
  7486. beg end newend)
  7487. ;; Remove the forced level indicator
  7488. (if force-level
  7489. (delete-region (point-at-bol) (point)))
  7490. ;; Paste
  7491. (beginning-of-line (if (bolp) 1 2))
  7492. (setq beg (point))
  7493. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7494. (insert-before-markers txt)
  7495. (unless (string-match "\n\\'" txt) (insert "\n"))
  7496. (setq newend (point))
  7497. (org-reinstall-markers-in-region beg)
  7498. (setq end (point))
  7499. (goto-char beg)
  7500. (skip-chars-forward " \t\n\r")
  7501. (setq beg (point))
  7502. (if (and (outline-invisible-p) visp)
  7503. (save-excursion (outline-show-heading)))
  7504. ;; Shift if necessary
  7505. (unless (= shift 0)
  7506. (save-restriction
  7507. (narrow-to-region beg end)
  7508. (while (not (= shift 0))
  7509. (org-map-region func (point-min) (point-max))
  7510. (setq shift (+ delta shift)))
  7511. (goto-char (point-min))
  7512. (setq newend (point-max))))
  7513. (when (or (org-called-interactively-p 'interactive) for-yank)
  7514. (message "Clipboard pasted as level %d subtree" new-level))
  7515. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7516. kill-ring
  7517. (eq org-subtree-clip (current-kill 0))
  7518. org-subtree-clip-folded)
  7519. ;; The tree was folded before it was killed/copied
  7520. (hide-subtree))
  7521. (and for-yank (goto-char newend)))))
  7522. (defun org-kill-is-subtree-p (&optional txt)
  7523. "Check if the current kill is an outline subtree, or a set of trees.
  7524. Returns nil if kill does not start with a headline, or if the first
  7525. headline level is not the largest headline level in the tree.
  7526. So this will actually accept several entries of equal levels as well,
  7527. which is OK for `org-paste-subtree'.
  7528. If optional TXT is given, check this string instead of the current kill."
  7529. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7530. (re (org-get-limited-outline-regexp))
  7531. (^re (concat "^" re))
  7532. (start-level (and kill
  7533. (string-match
  7534. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7535. kill)
  7536. (- (match-end 2) (match-beginning 2) 1)))
  7537. (start (1+ (or (match-beginning 2) -1))))
  7538. (if (not start-level)
  7539. (progn
  7540. nil) ;; does not even start with a heading
  7541. (catch 'exit
  7542. (while (setq start (string-match ^re kill (1+ start)))
  7543. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7544. (throw 'exit nil)))
  7545. t))))
  7546. (defvar org-markers-to-move nil
  7547. "Markers that should be moved with a cut-and-paste operation.
  7548. Those markers are stored together with their positions relative to
  7549. the start of the region.")
  7550. (defun org-save-markers-in-region (beg end)
  7551. "Check markers in region.
  7552. If these markers are between BEG and END, record their position relative
  7553. to BEG, so that after moving the block of text, we can put the markers back
  7554. into place.
  7555. This function gets called just before an entry or tree gets cut from the
  7556. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7557. called immediately, to move the markers with the entries."
  7558. (setq org-markers-to-move nil)
  7559. (when (featurep 'org-clock)
  7560. (org-clock-save-markers-for-cut-and-paste beg end))
  7561. (when (featurep 'org-agenda)
  7562. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7563. (defun org-check-and-save-marker (marker beg end)
  7564. "Check if MARKER is between BEG and END.
  7565. If yes, remember the marker and the distance to BEG."
  7566. (when (and (marker-buffer marker)
  7567. (equal (marker-buffer marker) (current-buffer)))
  7568. (if (and (>= marker beg) (< marker end))
  7569. (push (cons marker (- marker beg)) org-markers-to-move))))
  7570. (defun org-reinstall-markers-in-region (beg)
  7571. "Move all remembered markers to their position relative to BEG."
  7572. (mapc (lambda (x)
  7573. (move-marker (car x) (+ beg (cdr x))))
  7574. org-markers-to-move)
  7575. (setq org-markers-to-move nil))
  7576. (defun org-narrow-to-subtree ()
  7577. "Narrow buffer to the current subtree."
  7578. (interactive)
  7579. (save-excursion
  7580. (save-match-data
  7581. (org-with-limited-levels
  7582. (narrow-to-region
  7583. (progn (org-back-to-heading t) (point))
  7584. (progn (org-end-of-subtree t t)
  7585. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7586. (point)))))))
  7587. (defun org-narrow-to-block ()
  7588. "Narrow buffer to the current block."
  7589. (interactive)
  7590. (let* ((case-fold-search t)
  7591. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7592. "^[ \t]*#\\+end_.*")))
  7593. (if blockp
  7594. (narrow-to-region (car blockp) (cdr blockp))
  7595. (user-error "Not in a block"))))
  7596. (eval-when-compile
  7597. (defvar org-property-drawer-re))
  7598. (defvar org-property-start-re) ;; defined below
  7599. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7600. "Clone the task (subtree) at point N times.
  7601. The clones will be inserted as siblings.
  7602. In interactive use, the user will be prompted for the number of
  7603. clones to be produced. If the entry has a timestamp, the user
  7604. will also be prompted for a time shift, which may be a repeater
  7605. as used in time stamps, for example `+3d'. To disable this,
  7606. you can call the function with a universal prefix argument.
  7607. When a valid repeater is given and the entry contains any time
  7608. stamps, the clones will become a sequence in time, with time
  7609. stamps in the subtree shifted for each clone produced. If SHIFT
  7610. is nil or the empty string, time stamps will be left alone. The
  7611. ID property of the original subtree is removed.
  7612. If the original subtree did contain time stamps with a repeater,
  7613. the following will happen:
  7614. - the repeater will be removed in each clone
  7615. - an additional clone will be produced, with the current, unshifted
  7616. date(s) in the entry.
  7617. - the original entry will be placed *after* all the clones, with
  7618. repeater intact.
  7619. - the start days in the repeater in the original entry will be shifted
  7620. to past the last clone.
  7621. In this way you can spell out a number of instances of a repeating task,
  7622. and still retain the repeater to cover future instances of the task."
  7623. (interactive "nNumber of clones to produce: ")
  7624. (let ((shift
  7625. (or shift
  7626. (if (and (not (equal current-prefix-arg '(4)))
  7627. (save-excursion
  7628. (re-search-forward org-ts-regexp-both
  7629. (save-excursion
  7630. (org-end-of-subtree t)
  7631. (point)) t)))
  7632. (read-from-minibuffer
  7633. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7634. ""))) ;; No time shift
  7635. (n-no-remove -1)
  7636. (drawer-re org-drawer-regexp)
  7637. beg end template task idprop
  7638. shift-n shift-what doshift nmin nmax)
  7639. (if (not (and (integerp n) (> n 0)))
  7640. (error "Invalid number of replications %s" n))
  7641. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7642. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7643. shift)))
  7644. (error "Invalid shift specification %s" shift))
  7645. (when doshift
  7646. (setq shift-n (string-to-number (match-string 1 shift))
  7647. shift-what (cdr (assoc (match-string 2 shift)
  7648. '(("d" . day) ("w" . week)
  7649. ("m" . month) ("y" . year))))))
  7650. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7651. (setq nmin 1 nmax n)
  7652. (org-back-to-heading t)
  7653. (setq beg (point))
  7654. (setq idprop (org-entry-get nil "ID"))
  7655. (org-end-of-subtree t t)
  7656. (or (bolp) (insert "\n"))
  7657. (setq end (point))
  7658. (setq template (buffer-substring beg end))
  7659. (when (and doshift
  7660. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7661. (delete-region beg end)
  7662. (setq end beg)
  7663. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7664. (goto-char end)
  7665. (loop for n from nmin to nmax do
  7666. ;; prepare clone
  7667. (with-temp-buffer
  7668. (insert template)
  7669. (org-mode)
  7670. (goto-char (point-min))
  7671. (org-show-subtree)
  7672. (and idprop (if org-clone-delete-id
  7673. (org-entry-delete nil "ID")
  7674. (org-id-get-create t)))
  7675. (unless (= n 0)
  7676. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7677. (kill-whole-line))
  7678. (goto-char (point-min))
  7679. (while (re-search-forward drawer-re nil t)
  7680. (mapc (lambda (d)
  7681. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7682. (goto-char (point-min))
  7683. (when doshift
  7684. (while (re-search-forward org-ts-regexp-both nil t)
  7685. (org-timestamp-change (* n shift-n) shift-what))
  7686. (unless (= n n-no-remove)
  7687. (goto-char (point-min))
  7688. (while (re-search-forward org-ts-regexp nil t)
  7689. (save-excursion
  7690. (goto-char (match-beginning 0))
  7691. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7692. (delete-region (match-beginning 1) (match-end 1)))))))
  7693. (setq task (buffer-string)))
  7694. (insert task))
  7695. (goto-char beg)))
  7696. ;;; Outline Sorting
  7697. (defun org-sort (with-case)
  7698. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7699. Optional argument WITH-CASE means sort case-sensitively."
  7700. (interactive "P")
  7701. (cond
  7702. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7703. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7704. (t
  7705. (org-call-with-arg 'org-sort-entries with-case))))
  7706. (defun org-sort-remove-invisible (s)
  7707. "Remove invisible links from string S."
  7708. (remove-text-properties 0 (length s) org-rm-props s)
  7709. (while (string-match org-bracket-link-regexp s)
  7710. (setq s (replace-match (if (match-end 2)
  7711. (match-string 3 s)
  7712. (match-string 1 s)) t t s)))
  7713. (let ((st (format " %s " s)))
  7714. (while (string-match org-emph-re st)
  7715. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7716. (setq s (substring st 1 -1)))
  7717. s)
  7718. (defvar org-priority-regexp) ; defined later in the file
  7719. (defvar org-after-sorting-entries-or-items-hook nil
  7720. "Hook that is run after a bunch of entries or items have been sorted.
  7721. When children are sorted, the cursor is in the parent line when this
  7722. hook gets called. When a region or a plain list is sorted, the cursor
  7723. will be in the first entry of the sorted region/list.")
  7724. (defun org-sort-entries
  7725. (&optional with-case sorting-type getkey-func compare-func property)
  7726. "Sort entries on a certain level of an outline tree.
  7727. If there is an active region, the entries in the region are sorted.
  7728. Else, if the cursor is before the first entry, sort the top-level items.
  7729. Else, the children of the entry at point are sorted.
  7730. Sorting can be alphabetically, numerically, by date/time as given by
  7731. a time stamp, by a property, by priority order, or by a custom function.
  7732. The command prompts for the sorting type unless it has been given to the
  7733. function through the SORTING-TYPE argument, which needs to be a character,
  7734. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7735. precise meaning of each character:
  7736. n Numerically, by converting the beginning of the entry/item to a number.
  7737. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7738. o By order of TODO keywords.
  7739. t By date/time, either the first active time stamp in the entry, or, if
  7740. none exist, by the first inactive one.
  7741. s By the scheduled date/time.
  7742. d By deadline date/time.
  7743. c By creation time, which is assumed to be the first inactive time stamp
  7744. at the beginning of a line.
  7745. p By priority according to the cookie.
  7746. r By the value of a property.
  7747. Capital letters will reverse the sort order.
  7748. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7749. called with point at the beginning of the record. It must return either
  7750. a string or a number that should serve as the sorting key for that record.
  7751. Comparing entries ignores case by default. However, with an optional argument
  7752. WITH-CASE, the sorting considers case as well.
  7753. Sorting is done against the visible part of the headlines, it ignores hidden
  7754. links."
  7755. (interactive "P")
  7756. (let ((case-func (if with-case 'identity 'downcase))
  7757. (cmstr
  7758. ;; The clock marker is lost when using `sort-subr', let's
  7759. ;; store the clocking string.
  7760. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7761. (save-excursion
  7762. (goto-char org-clock-marker)
  7763. (looking-back "^.*") (match-string-no-properties 0))))
  7764. start beg end stars re re2
  7765. txt what tmp)
  7766. ;; Find beginning and end of region to sort
  7767. (cond
  7768. ((org-region-active-p)
  7769. ;; we will sort the region
  7770. (setq end (region-end)
  7771. what "region")
  7772. (goto-char (region-beginning))
  7773. (if (not (org-at-heading-p)) (outline-next-heading))
  7774. (setq start (point)))
  7775. ((or (org-at-heading-p)
  7776. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7777. ;; we will sort the children of the current headline
  7778. (org-back-to-heading)
  7779. (setq start (point)
  7780. end (progn (org-end-of-subtree t t)
  7781. (or (bolp) (insert "\n"))
  7782. (when (>= (org-back-over-empty-lines) 1)
  7783. (forward-line 1))
  7784. (point))
  7785. what "children")
  7786. (goto-char start)
  7787. (show-subtree)
  7788. (outline-next-heading))
  7789. (t
  7790. ;; we will sort the top-level entries in this file
  7791. (goto-char (point-min))
  7792. (or (org-at-heading-p) (outline-next-heading))
  7793. (setq start (point))
  7794. (goto-char (point-max))
  7795. (beginning-of-line 1)
  7796. (when (looking-at ".*?\\S-")
  7797. ;; File ends in a non-white line
  7798. (end-of-line 1)
  7799. (insert "\n"))
  7800. (setq end (point-max))
  7801. (setq what "top-level")
  7802. (goto-char start)
  7803. (show-all)))
  7804. (setq beg (point))
  7805. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7806. (looking-at "\\(\\*+\\)")
  7807. (setq stars (match-string 1)
  7808. re (concat "^" (regexp-quote stars) " +")
  7809. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7810. txt (buffer-substring beg end))
  7811. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7812. (if (and (not (equal stars "*")) (string-match re2 txt))
  7813. (user-error "Region to sort contains a level above the first entry"))
  7814. (unless sorting-type
  7815. (message
  7816. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7817. [t]ime [s]cheduled [d]eadline [c]reated
  7818. A/N/P/R/O/F/T/S/D/C means reversed:"
  7819. what)
  7820. (setq sorting-type (read-char-exclusive))
  7821. (unless getkey-func
  7822. (and (= (downcase sorting-type) ?f)
  7823. (setq getkey-func
  7824. (org-icompleting-read "Sort using function: "
  7825. obarray 'fboundp t nil nil))
  7826. (setq getkey-func (intern getkey-func))))
  7827. (and (= (downcase sorting-type) ?r)
  7828. (not property)
  7829. (setq property
  7830. (org-icompleting-read "Property: "
  7831. (mapcar 'list (org-buffer-property-keys t))
  7832. nil t))))
  7833. (message "Sorting entries...")
  7834. (save-restriction
  7835. (narrow-to-region start end)
  7836. (let ((dcst (downcase sorting-type))
  7837. (case-fold-search nil)
  7838. (now (current-time)))
  7839. (sort-subr
  7840. (/= dcst sorting-type)
  7841. ;; This function moves to the beginning character of the "record" to
  7842. ;; be sorted.
  7843. (lambda nil
  7844. (if (re-search-forward re nil t)
  7845. (goto-char (match-beginning 0))
  7846. (goto-char (point-max))))
  7847. ;; This function moves to the last character of the "record" being
  7848. ;; sorted.
  7849. (lambda nil
  7850. (save-match-data
  7851. (condition-case nil
  7852. (outline-forward-same-level 1)
  7853. (error
  7854. (goto-char (point-max))))))
  7855. ;; This function returns the value that gets sorted against.
  7856. (lambda nil
  7857. (cond
  7858. ((= dcst ?n)
  7859. (if (looking-at org-complex-heading-regexp)
  7860. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7861. nil))
  7862. ((= dcst ?a)
  7863. (if (looking-at org-complex-heading-regexp)
  7864. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7865. nil))
  7866. ((= dcst ?t)
  7867. (let ((end (save-excursion (outline-next-heading) (point))))
  7868. (if (or (re-search-forward org-ts-regexp end t)
  7869. (re-search-forward org-ts-regexp-both end t))
  7870. (org-time-string-to-seconds (match-string 0))
  7871. (org-float-time now))))
  7872. ((= dcst ?c)
  7873. (let ((end (save-excursion (outline-next-heading) (point))))
  7874. (if (re-search-forward
  7875. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7876. end t)
  7877. (org-time-string-to-seconds (match-string 0))
  7878. (org-float-time now))))
  7879. ((= dcst ?s)
  7880. (let ((end (save-excursion (outline-next-heading) (point))))
  7881. (if (re-search-forward org-scheduled-time-regexp end t)
  7882. (org-time-string-to-seconds (match-string 1))
  7883. (org-float-time now))))
  7884. ((= dcst ?d)
  7885. (let ((end (save-excursion (outline-next-heading) (point))))
  7886. (if (re-search-forward org-deadline-time-regexp end t)
  7887. (org-time-string-to-seconds (match-string 1))
  7888. (org-float-time now))))
  7889. ((= dcst ?p)
  7890. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7891. (string-to-char (match-string 2))
  7892. org-default-priority))
  7893. ((= dcst ?r)
  7894. (or (org-entry-get nil property) ""))
  7895. ((= dcst ?o)
  7896. (if (looking-at org-complex-heading-regexp)
  7897. (- 9999 (length (member (match-string 2)
  7898. org-todo-keywords-1)))))
  7899. ((= dcst ?f)
  7900. (if getkey-func
  7901. (progn
  7902. (setq tmp (funcall getkey-func))
  7903. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7904. tmp)
  7905. (error "Invalid key function `%s'" getkey-func)))
  7906. (t (error "Invalid sorting type `%c'" sorting-type))))
  7907. nil
  7908. (cond
  7909. ((= dcst ?a) 'string<)
  7910. ((= dcst ?f) compare-func)
  7911. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7912. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7913. ;; Reset the clock marker if needed
  7914. (when cmstr
  7915. (save-excursion
  7916. (goto-char start)
  7917. (search-forward cmstr nil t)
  7918. (move-marker org-clock-marker (point))))
  7919. (message "Sorting entries...done")))
  7920. (defun org-do-sort (table what &optional with-case sorting-type)
  7921. "Sort TABLE of WHAT according to SORTING-TYPE.
  7922. The user will be prompted for the SORTING-TYPE if the call to this
  7923. function does not specify it. WHAT is only for the prompt, to indicate
  7924. what is being sorted. The sorting key will be extracted from
  7925. the car of the elements of the table.
  7926. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7927. (unless sorting-type
  7928. (message
  7929. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7930. what)
  7931. (setq sorting-type (read-char-exclusive)))
  7932. (let ((dcst (downcase sorting-type))
  7933. extractfun comparefun)
  7934. ;; Define the appropriate functions
  7935. (cond
  7936. ((= dcst ?n)
  7937. (setq extractfun 'string-to-number
  7938. comparefun (if (= dcst sorting-type) '< '>)))
  7939. ((= dcst ?a)
  7940. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7941. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7942. comparefun (if (= dcst sorting-type)
  7943. 'string<
  7944. (lambda (a b) (and (not (string< a b))
  7945. (not (string= a b)))))))
  7946. ((= dcst ?t)
  7947. (setq extractfun
  7948. (lambda (x)
  7949. (if (or (string-match org-ts-regexp x)
  7950. (string-match org-ts-regexp-both x))
  7951. (org-float-time
  7952. (org-time-string-to-time (match-string 0 x)))
  7953. 0))
  7954. comparefun (if (= dcst sorting-type) '< '>)))
  7955. (t (error "Invalid sorting type `%c'" sorting-type)))
  7956. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7957. table)
  7958. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7959. ;;; The orgstruct minor mode
  7960. ;; Define a minor mode which can be used in other modes in order to
  7961. ;; integrate the org-mode structure editing commands.
  7962. ;; This is really a hack, because the org-mode structure commands use
  7963. ;; keys which normally belong to the major mode. Here is how it
  7964. ;; works: The minor mode defines all the keys necessary to operate the
  7965. ;; structure commands, but wraps the commands into a function which
  7966. ;; tests if the cursor is currently at a headline or a plain list
  7967. ;; item. If that is the case, the structure command is used,
  7968. ;; temporarily setting many Org-mode variables like regular
  7969. ;; expressions for filling etc. However, when any of those keys is
  7970. ;; used at a different location, function uses `key-binding' to look
  7971. ;; up if the key has an associated command in another currently active
  7972. ;; keymap (minor modes, major mode, global), and executes that
  7973. ;; command. There might be problems if any of the keys is otherwise
  7974. ;; used as a prefix key.
  7975. (defcustom orgstruct-heading-prefix-regexp ""
  7976. "Regexp that matches the custom prefix of Org headlines in
  7977. orgstruct(++)-mode."
  7978. :group 'org
  7979. :version "24.4"
  7980. :package-version '(Org . "8.3")
  7981. :type 'regexp)
  7982. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7983. (defcustom orgstruct-setup-hook nil
  7984. "Hook run after orgstruct-mode-map is filled."
  7985. :group 'org
  7986. :version "24.4"
  7987. :package-version '(Org . "8.0")
  7988. :type 'hook)
  7989. (defvar orgstruct-initialized nil)
  7990. (defvar org-local-vars nil
  7991. "List of local variables, for use by `orgstruct-mode'.")
  7992. ;;;###autoload
  7993. (define-minor-mode orgstruct-mode
  7994. "Toggle the minor mode `orgstruct-mode'.
  7995. This mode is for using Org-mode structure commands in other
  7996. modes. The following keys behave as if Org-mode were active, if
  7997. the cursor is on a headline, or on a plain list item (both as
  7998. defined by Org-mode)."
  7999. nil " OrgStruct" (make-sparse-keymap)
  8000. (funcall (if orgstruct-mode
  8001. 'add-to-invisibility-spec
  8002. 'remove-from-invisibility-spec)
  8003. '(outline . t))
  8004. (when orgstruct-mode
  8005. (org-load-modules-maybe)
  8006. (unless orgstruct-initialized
  8007. (orgstruct-setup)
  8008. (setq orgstruct-initialized t))))
  8009. ;;;###autoload
  8010. (defun turn-on-orgstruct ()
  8011. "Unconditionally turn on `orgstruct-mode'."
  8012. (orgstruct-mode 1))
  8013. (defvar org-fb-vars nil)
  8014. (make-variable-buffer-local 'org-fb-vars)
  8015. (defun orgstruct++-mode (&optional arg)
  8016. "Toggle `orgstruct-mode', the enhanced version of it.
  8017. In addition to setting orgstruct-mode, this also exports all
  8018. indentation and autofilling variables from org-mode into the
  8019. buffer. It will also recognize item context in multiline items."
  8020. (interactive "P")
  8021. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8022. (if (< arg 1)
  8023. (progn (orgstruct-mode -1)
  8024. (mapc (lambda(v)
  8025. (org-set-local (car v)
  8026. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8027. org-fb-vars))
  8028. (orgstruct-mode 1)
  8029. (setq org-fb-vars nil)
  8030. (unless org-local-vars
  8031. (setq org-local-vars (org-get-local-variables)))
  8032. (let (var val)
  8033. (mapc
  8034. (lambda (x)
  8035. (when (string-match
  8036. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8037. (symbol-name (car x)))
  8038. (setq var (car x) val (nth 1 x))
  8039. (push (list var `(quote ,(eval var))) org-fb-vars)
  8040. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8041. org-local-vars)
  8042. (org-set-local 'orgstruct-is-++ t))))
  8043. (defvar orgstruct-is-++ nil
  8044. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8045. (make-variable-buffer-local 'orgstruct-is-++)
  8046. ;;;###autoload
  8047. (defun turn-on-orgstruct++ ()
  8048. "Unconditionally turn on `orgstruct++-mode'."
  8049. (orgstruct++-mode 1))
  8050. (defun orgstruct-error ()
  8051. "Error when there is no default binding for a structure key."
  8052. (interactive)
  8053. (funcall (if (fboundp 'user-error)
  8054. 'user-error
  8055. 'error)
  8056. "This key has no function outside structure elements"))
  8057. (defun orgstruct-setup ()
  8058. "Setup orgstruct keymap."
  8059. (dolist (cell '((org-demote . t)
  8060. (org-metaleft . t)
  8061. (org-metaright . t)
  8062. (org-promote . t)
  8063. (org-shiftmetaleft . t)
  8064. (org-shiftmetaright . t)
  8065. org-backward-element
  8066. org-backward-heading-same-level
  8067. org-ctrl-c-ret
  8068. org-ctrl-c-minus
  8069. org-ctrl-c-star
  8070. org-cycle
  8071. org-forward-heading-same-level
  8072. org-insert-heading
  8073. org-insert-heading-respect-content
  8074. org-kill-note-or-show-branches
  8075. org-mark-subtree
  8076. org-meta-return
  8077. org-metadown
  8078. org-metaup
  8079. org-narrow-to-subtree
  8080. org-promote-subtree
  8081. org-reveal
  8082. org-shiftdown
  8083. org-shiftleft
  8084. org-shiftmetadown
  8085. org-shiftmetaup
  8086. org-shiftright
  8087. org-shifttab
  8088. org-shifttab
  8089. org-shiftup
  8090. org-show-subtree
  8091. org-sort
  8092. org-up-element
  8093. outline-demote
  8094. outline-next-visible-heading
  8095. outline-previous-visible-heading
  8096. outline-promote
  8097. outline-up-heading
  8098. show-children))
  8099. (let ((f (or (car-safe cell) cell))
  8100. (disable-when-heading-prefix (cdr-safe cell)))
  8101. (when (fboundp f)
  8102. (let ((new-bindings))
  8103. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8104. (where-is-internal f outline-mode-map)))
  8105. (push binding new-bindings)
  8106. ;; TODO use local-function-key-map
  8107. (dolist (rep '(("<tab>" . "TAB")
  8108. ("<return>" . "RET")
  8109. ("<escape>" . "ESC")
  8110. ("<delete>" . "DEL")))
  8111. (setq binding (read-kbd-macro
  8112. (let ((case-fold-search))
  8113. (replace-regexp-in-string
  8114. (regexp-quote (cdr rep))
  8115. (car rep)
  8116. (key-description binding)))))
  8117. (pushnew binding new-bindings :test 'equal)))
  8118. (dolist (binding new-bindings)
  8119. (let ((key (lookup-key orgstruct-mode-map binding)))
  8120. (when (or (not key) (numberp key))
  8121. (condition-case nil
  8122. (org-defkey orgstruct-mode-map
  8123. binding
  8124. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8125. (error nil)))))))))
  8126. (run-hooks 'orgstruct-setup-hook))
  8127. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8128. "Create a function for binding in the structure minor mode.
  8129. FUN is the command to call inside a table. KEY is the key that
  8130. should be checked in for a command to execute outside of tables.
  8131. Non-nil `disable-when-heading-prefix' means to disable the command
  8132. if `orgstruct-heading-prefix-regexp' is not empty."
  8133. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8134. (let ((nname name)
  8135. (i 0))
  8136. (while (fboundp (intern nname))
  8137. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8138. (setq name (intern nname)))
  8139. (eval
  8140. (let ((bindings '((org-heading-regexp
  8141. (concat "^"
  8142. orgstruct-heading-prefix-regexp
  8143. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8144. (org-outline-regexp
  8145. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8146. (org-outline-regexp-bol
  8147. (concat "^" org-outline-regexp))
  8148. (outline-regexp org-outline-regexp)
  8149. (outline-heading-end-regexp "\n")
  8150. (outline-level 'org-outline-level)
  8151. (outline-heading-alist))))
  8152. `(defun ,name (arg)
  8153. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8154. "Outside of structure, run the binding of `"
  8155. (key-description key) "'."
  8156. (when disable-when-heading-prefix
  8157. (concat
  8158. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8159. "back to the default binding due to limitations of Org's implementation of\n"
  8160. "`" (symbol-name fun) "'.")))
  8161. (interactive "p")
  8162. (let* ((disable
  8163. ,(and disable-when-heading-prefix
  8164. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8165. (fallback
  8166. (or disable
  8167. (not
  8168. (let* ,bindings
  8169. (org-context-p 'headline 'item
  8170. ,(when (memq fun
  8171. '(org-insert-heading
  8172. org-insert-heading-respect-content
  8173. org-meta-return))
  8174. '(when orgstruct-is-++
  8175. 'item-body))))))))
  8176. (if fallback
  8177. (let* ((orgstruct-mode)
  8178. (binding
  8179. (loop with key = ,key
  8180. for rep in
  8181. '(nil
  8182. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8183. ("<\\([^>]*\\)return>" . "\\1RET")
  8184. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8185. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8186. do
  8187. (when rep
  8188. (setq key (read-kbd-macro
  8189. (let ((case-fold-search))
  8190. (replace-regexp-in-string
  8191. (car rep)
  8192. (cdr rep)
  8193. (key-description key))))))
  8194. thereis (key-binding key))))
  8195. (if (keymapp binding)
  8196. (org-set-transient-map binding)
  8197. (let ((func (or binding
  8198. (unless disable
  8199. 'orgstruct-error))))
  8200. (when func
  8201. (call-interactively func)))))
  8202. (org-run-like-in-org-mode
  8203. (lambda ()
  8204. (interactive)
  8205. (let* ,bindings
  8206. (call-interactively ',fun)))))))))
  8207. name))
  8208. (defun org-contextualize-keys (alist contexts)
  8209. "Return valid elements in ALIST depending on CONTEXTS.
  8210. `org-agenda-custom-commands' or `org-capture-templates' are the
  8211. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8212. or `org-capture-templates-contexts' are the associated contexts
  8213. definitions."
  8214. (let ((contexts
  8215. ;; normalize contexts
  8216. (mapcar
  8217. (lambda(c) (cond ((listp (cadr c))
  8218. (list (car c) (car c) (cadr c)))
  8219. ((string= "" (cadr c))
  8220. (list (car c) (car c) (caddr c)))
  8221. (t c))) contexts))
  8222. (a alist) c r s)
  8223. ;; loop over all commands or templates
  8224. (while (setq c (pop a))
  8225. (let (vrules repl)
  8226. (cond
  8227. ((not (assoc (car c) contexts))
  8228. (push c r))
  8229. ((and (assoc (car c) contexts)
  8230. (setq vrules (org-contextualize-validate-key
  8231. (car c) contexts)))
  8232. (mapc (lambda (vr)
  8233. (when (not (equal (car vr) (cadr vr)))
  8234. (setq repl vr))) vrules)
  8235. (if (not repl) (push c r)
  8236. (push (cadr repl) s)
  8237. (push
  8238. (cons (car c)
  8239. (cdr (or (assoc (cadr repl) alist)
  8240. (error "Undefined key `%s' as contextual replacement for `%s'"
  8241. (cadr repl) (car c)))))
  8242. r))))))
  8243. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8244. (delq
  8245. nil
  8246. (delete-dups
  8247. (mapcar (lambda (x)
  8248. (let ((tpl (car x)))
  8249. (when (not (delq
  8250. nil
  8251. (mapcar (lambda(y)
  8252. (equal y tpl)) s))) x)))
  8253. (reverse r))))))
  8254. (defun org-contextualize-validate-key (key contexts)
  8255. "Check CONTEXTS for agenda or capture KEY."
  8256. (let (r rr res)
  8257. (while (setq r (pop contexts))
  8258. (mapc
  8259. (lambda (rr)
  8260. (when
  8261. (and (equal key (car r))
  8262. (if (functionp rr) (funcall rr)
  8263. (or (and (eq (car rr) 'in-file)
  8264. (buffer-file-name)
  8265. (string-match (cdr rr) (buffer-file-name)))
  8266. (and (eq (car rr) 'in-mode)
  8267. (string-match (cdr rr) (symbol-name major-mode)))
  8268. (and (eq (car rr) 'in-buffer)
  8269. (string-match (cdr rr) (buffer-name)))
  8270. (when (and (eq (car rr) 'not-in-file)
  8271. (buffer-file-name))
  8272. (not (string-match (cdr rr) (buffer-file-name))))
  8273. (when (eq (car rr) 'not-in-mode)
  8274. (not (string-match (cdr rr) (symbol-name major-mode))))
  8275. (when (eq (car rr) 'not-in-buffer)
  8276. (not (string-match (cdr rr) (buffer-name)))))))
  8277. (push r res)))
  8278. (car (last r))))
  8279. (delete-dups (delq nil res))))
  8280. (defun org-context-p (&rest contexts)
  8281. "Check if local context is any of CONTEXTS.
  8282. Possible values in the list of contexts are `table', `headline', and `item'."
  8283. (let ((pos (point)))
  8284. (goto-char (point-at-bol))
  8285. (prog1 (or (and (memq 'table contexts)
  8286. (looking-at "[ \t]*|"))
  8287. (and (memq 'headline contexts)
  8288. (looking-at org-outline-regexp))
  8289. (and (memq 'item contexts)
  8290. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8291. (and (memq 'item-body contexts)
  8292. (org-in-item-p)))
  8293. (goto-char pos))))
  8294. (defun org-get-local-variables ()
  8295. "Return a list of all local variables in an Org mode buffer."
  8296. (let (varlist)
  8297. (with-current-buffer (get-buffer-create "*Org tmp*")
  8298. (erase-buffer)
  8299. (org-mode)
  8300. (setq varlist (buffer-local-variables)))
  8301. (kill-buffer "*Org tmp*")
  8302. (delq nil
  8303. (mapcar
  8304. (lambda (x)
  8305. (setq x
  8306. (if (symbolp x)
  8307. (list x)
  8308. (list (car x) (cdr x))))
  8309. (if (and (not (get (car x) 'org-state))
  8310. (string-match
  8311. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8312. (symbol-name (car x))))
  8313. x nil))
  8314. varlist))))
  8315. (defun org-clone-local-variables (from-buffer &optional regexp)
  8316. "Clone local variables from FROM-BUFFER.
  8317. Optional argument REGEXP selects variables to clone."
  8318. (mapc
  8319. (lambda (pair)
  8320. (and (symbolp (car pair))
  8321. (or (null regexp)
  8322. (string-match regexp (symbol-name (car pair))))
  8323. (set (make-local-variable (car pair))
  8324. (cdr pair))))
  8325. (buffer-local-variables from-buffer)))
  8326. ;;;###autoload
  8327. (defun org-run-like-in-org-mode (cmd)
  8328. "Run a command, pretending that the current buffer is in Org-mode.
  8329. This will temporarily bind local variables that are typically bound in
  8330. Org-mode to the values they have in Org-mode, and then interactively
  8331. call CMD."
  8332. (org-load-modules-maybe)
  8333. (unless org-local-vars
  8334. (setq org-local-vars (org-get-local-variables)))
  8335. (let (binds)
  8336. (dolist (var org-local-vars)
  8337. (when (or (not (boundp (car var)))
  8338. (eq (symbol-value (car var))
  8339. (default-value (car var))))
  8340. (push (list (car var) `(quote ,(cadr var))) binds)))
  8341. (eval `(let ,binds
  8342. (call-interactively (quote ,cmd))))))
  8343. ;;;; Archiving
  8344. (defun org-get-category (&optional pos force-refresh)
  8345. "Get the category applying to position POS."
  8346. (save-match-data
  8347. (if force-refresh (org-refresh-category-properties))
  8348. (let ((pos (or pos (point))))
  8349. (or (get-text-property pos 'org-category)
  8350. (progn (org-refresh-category-properties)
  8351. (get-text-property pos 'org-category))))))
  8352. (defun org-refresh-category-properties ()
  8353. "Refresh category text properties in the buffer."
  8354. (let ((case-fold-search t)
  8355. (inhibit-read-only t)
  8356. (def-cat (cond
  8357. ((null org-category)
  8358. (if buffer-file-name
  8359. (file-name-sans-extension
  8360. (file-name-nondirectory buffer-file-name))
  8361. "???"))
  8362. ((symbolp org-category) (symbol-name org-category))
  8363. (t org-category)))
  8364. beg end cat pos optionp)
  8365. (org-with-silent-modifications
  8366. (save-excursion
  8367. (save-restriction
  8368. (widen)
  8369. (goto-char (point-min))
  8370. (put-text-property (point) (point-max) 'org-category def-cat)
  8371. (while (re-search-forward
  8372. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8373. (setq pos (match-end 0)
  8374. optionp (equal (char-after (match-beginning 0)) ?#)
  8375. cat (org-trim (match-string 2)))
  8376. (if optionp
  8377. (setq beg (point-at-bol) end (point-max))
  8378. (org-back-to-heading t)
  8379. (setq beg (point) end (org-end-of-subtree t t)))
  8380. (put-text-property beg end 'org-category cat)
  8381. (put-text-property beg end 'org-category-position beg)
  8382. (goto-char pos)))))))
  8383. (defun org-refresh-properties (dprop tprop)
  8384. "Refresh buffer text properties.
  8385. DPROP is the drawer property and TPROP is the corresponding text
  8386. property to set."
  8387. (let ((case-fold-search t)
  8388. (inhibit-read-only t) p)
  8389. (org-with-silent-modifications
  8390. (save-excursion
  8391. (save-restriction
  8392. (widen)
  8393. (goto-char (point-min))
  8394. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8395. (setq p (org-match-string-no-properties 1))
  8396. (save-excursion
  8397. (org-back-to-heading t)
  8398. (put-text-property
  8399. (point-at-bol) (or (outline-next-heading) (point-max)) tprop p))))))))
  8400. ;;;; Link Stuff
  8401. ;;; Link abbreviations
  8402. (defun org-link-expand-abbrev (link)
  8403. "Apply replacements as defined in `org-link-abbrev-alist'."
  8404. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8405. (let* ((key (match-string 1 link))
  8406. (as (or (assoc key org-link-abbrev-alist-local)
  8407. (assoc key org-link-abbrev-alist)))
  8408. (tag (and (match-end 2) (match-string 3 link)))
  8409. rpl)
  8410. (if (not as)
  8411. link
  8412. (setq rpl (cdr as))
  8413. (cond
  8414. ((symbolp rpl) (funcall rpl tag))
  8415. ((string-match "%(\\([^)]+\\))" rpl)
  8416. (replace-match
  8417. (save-match-data
  8418. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8419. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8420. ((string-match "%h" rpl)
  8421. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8422. (t (concat rpl tag)))))
  8423. link))
  8424. ;;; Storing and inserting links
  8425. (defvar org-insert-link-history nil
  8426. "Minibuffer history for links inserted with `org-insert-link'.")
  8427. (defvar org-stored-links nil
  8428. "Contains the links stored with `org-store-link'.")
  8429. (defvar org-store-link-plist nil
  8430. "Plist with info about the most recently link created with `org-store-link'.")
  8431. (defvar org-link-protocols nil
  8432. "Link protocols added to Org-mode using `org-add-link-type'.")
  8433. (defvar org-store-link-functions nil
  8434. "List of functions that are called to create and store a link.
  8435. Each function will be called in turn until one returns a non-nil
  8436. value. Each function should check if it is responsible for creating
  8437. this link (for example by looking at the major mode).
  8438. If not, it must exit and return nil.
  8439. If yes, it should return a non-nil value after a calling
  8440. `org-store-link-props' with a list of properties and values.
  8441. Special properties are:
  8442. :type The link prefix, like \"http\". This must be given.
  8443. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8444. This is obligatory as well.
  8445. :description Optional default description for the second pair
  8446. of brackets in an Org-mode link. The user can still change
  8447. this when inserting this link into an Org-mode buffer.
  8448. In addition to these, any additional properties can be specified
  8449. and then used in capture templates.")
  8450. (defun org-add-link-type (type &optional follow export)
  8451. "Add TYPE to the list of `org-link-types'.
  8452. Re-compute all regular expressions depending on `org-link-types'
  8453. FOLLOW and EXPORT are two functions.
  8454. FOLLOW should take the link path as the single argument and do whatever
  8455. is necessary to follow the link, for example find a file or display
  8456. a mail message.
  8457. EXPORT should format the link path for export to one of the export formats.
  8458. It should be a function accepting three arguments:
  8459. path the path of the link, the text after the prefix (like \"http:\")
  8460. desc the description of the link, if any, or a description added by
  8461. org-export-normalize-links if there is none
  8462. format the export format, a symbol like `html' or `latex' or `ascii'..
  8463. The function may use the FORMAT information to return different values
  8464. depending on the format. The return value will be put literally into
  8465. the exported file. If the return value is nil, this means Org should
  8466. do what it normally does with links which do not have EXPORT defined.
  8467. Org-mode has a built-in default for exporting links. If you are happy with
  8468. this default, there is no need to define an export function for the link
  8469. type. For a simple example of an export function, see `org-bbdb.el'."
  8470. (add-to-list 'org-link-types type t)
  8471. (org-make-link-regexps)
  8472. (if (assoc type org-link-protocols)
  8473. (setcdr (assoc type org-link-protocols) (list follow export))
  8474. (push (list type follow export) org-link-protocols)))
  8475. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8476. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8477. ;;;###autoload
  8478. (defun org-store-link (arg)
  8479. "\\<org-mode-map>Store an org-link to the current location.
  8480. This link is added to `org-stored-links' and can later be inserted
  8481. into an org-buffer with \\[org-insert-link].
  8482. For some link types, a prefix arg is interpreted.
  8483. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8484. For file links, arg negates `org-context-in-file-links'.
  8485. A double prefix arg force skipping storing functions that are not
  8486. part of Org's core.
  8487. A triple prefix arg force storing a link for each line in the
  8488. active region."
  8489. (interactive "P")
  8490. (org-load-modules-maybe)
  8491. (if (and (equal arg '(64)) (org-region-active-p))
  8492. (save-excursion
  8493. (let ((end (region-end)))
  8494. (goto-char (region-beginning))
  8495. (set-mark (point))
  8496. (while (< (point-at-eol) end)
  8497. (move-end-of-line 1) (activate-mark)
  8498. (let (current-prefix-arg)
  8499. (call-interactively 'org-store-link))
  8500. (move-beginning-of-line 2)
  8501. (set-mark (point)))))
  8502. (org-with-limited-levels
  8503. (setq org-store-link-plist nil)
  8504. (let (link cpltxt desc description search
  8505. txt custom-id agenda-link sfuns sfunsn)
  8506. (cond
  8507. ;; Store a link using an external link type
  8508. ((and (not (equal arg '(16)))
  8509. (setq sfuns
  8510. (delq
  8511. nil (mapcar (lambda (f)
  8512. (let (fs) (if (funcall f) (push f fs))))
  8513. org-store-link-functions))
  8514. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8515. (or (and (cdr sfuns)
  8516. (funcall (intern
  8517. (completing-read
  8518. "Which function for creating the link? "
  8519. sfunsn nil t (car sfunsn)))))
  8520. (funcall (caar sfuns)))
  8521. (setq link (plist-get org-store-link-plist :link)
  8522. desc (or (plist-get org-store-link-plist
  8523. :description) link))))
  8524. ;; Store a link from a source code buffer
  8525. ((org-src-edit-buffer-p)
  8526. (let (label gc)
  8527. (while (or (not label)
  8528. (save-excursion
  8529. (save-restriction
  8530. (widen)
  8531. (goto-char (point-min))
  8532. (re-search-forward
  8533. (regexp-quote (format org-coderef-label-format label))
  8534. nil t))))
  8535. (when label (message "Label exists already") (sit-for 2))
  8536. (setq label (read-string "Code line label: " label)))
  8537. (end-of-line 1)
  8538. (setq link (format org-coderef-label-format label))
  8539. (setq gc (- 79 (length link)))
  8540. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8541. (insert link)
  8542. (setq link (concat "(" label ")") desc nil)))
  8543. ;; We are in the agenda, link to referenced location
  8544. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8545. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8546. (get-text-property (point) 'org-marker))))
  8547. (when m
  8548. (org-with-point-at m
  8549. (setq agenda-link
  8550. (if (org-called-interactively-p 'any)
  8551. (call-interactively 'org-store-link)
  8552. (org-store-link nil)))))))
  8553. ((eq major-mode 'calendar-mode)
  8554. (let ((cd (calendar-cursor-to-date)))
  8555. (setq link
  8556. (format-time-string
  8557. (car org-time-stamp-formats)
  8558. (apply 'encode-time
  8559. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8560. nil nil nil))))
  8561. (org-store-link-props :type "calendar" :date cd)))
  8562. ((eq major-mode 'help-mode)
  8563. (setq link (concat "help:" (save-excursion
  8564. (goto-char (point-min))
  8565. (looking-at "^[^ ]+")
  8566. (match-string 0))))
  8567. (org-store-link-props :type "help"))
  8568. ((eq major-mode 'w3-mode)
  8569. (setq cpltxt (if (and (buffer-name)
  8570. (not (string-match "Untitled" (buffer-name))))
  8571. (buffer-name)
  8572. (url-view-url t))
  8573. link (url-view-url t))
  8574. (org-store-link-props :type "w3" :url (url-view-url t)))
  8575. ((eq major-mode 'image-mode)
  8576. (setq cpltxt (concat "file:"
  8577. (abbreviate-file-name buffer-file-name))
  8578. link cpltxt)
  8579. (org-store-link-props :type "image" :file buffer-file-name))
  8580. ;; In dired, store a link to the file of the current line
  8581. ((eq major-mode 'dired-mode)
  8582. (let ((file (dired-get-filename nil t)))
  8583. (setq file (if file
  8584. (abbreviate-file-name
  8585. (expand-file-name (dired-get-filename nil t)))
  8586. ;; otherwise, no file so use current directory.
  8587. default-directory))
  8588. (setq cpltxt (concat "file:" file)
  8589. link cpltxt)))
  8590. ((setq search (run-hook-with-args-until-success
  8591. 'org-create-file-search-functions))
  8592. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8593. "::" search))
  8594. (setq cpltxt (or description link)))
  8595. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8596. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8597. (cond
  8598. ;; Store a link using the target at point
  8599. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8600. (setq cpltxt
  8601. (concat "file:"
  8602. (abbreviate-file-name
  8603. (buffer-file-name (buffer-base-buffer)))
  8604. "::" (match-string 1))
  8605. link cpltxt))
  8606. ((and (featurep 'org-id)
  8607. (or (eq org-id-link-to-org-use-id t)
  8608. (and (org-called-interactively-p 'any)
  8609. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8610. (and (eq org-id-link-to-org-use-id
  8611. 'create-if-interactive-and-no-custom-id)
  8612. (not custom-id))))
  8613. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8614. ;; Store a link using the ID at point
  8615. (setq link (condition-case nil
  8616. (prog1 (org-id-store-link)
  8617. (setq desc (or (plist-get org-store-link-plist
  8618. :description)
  8619. "")))
  8620. (error
  8621. ;; Probably before first headline, link only to file
  8622. (concat "file:"
  8623. (abbreviate-file-name
  8624. (buffer-file-name (buffer-base-buffer))))))))
  8625. (t
  8626. ;; Just link to current headline
  8627. (setq cpltxt (concat "file:"
  8628. (abbreviate-file-name
  8629. (buffer-file-name (buffer-base-buffer)))))
  8630. ;; Add a context search string
  8631. (when (org-xor org-context-in-file-links arg)
  8632. (let* ((ee (org-element-at-point))
  8633. (et (org-element-type ee))
  8634. (ev (plist-get (cadr ee) :value))
  8635. (ek (plist-get (cadr ee) :key))
  8636. (eok (and (stringp ek) (string-match "name" ek))))
  8637. (setq txt (cond
  8638. ((org-at-heading-p) nil)
  8639. ((and (eq et 'keyword) eok) ev)
  8640. ((org-region-active-p)
  8641. (buffer-substring (region-beginning) (region-end)))))
  8642. (when (or (null txt) (string-match "\\S-" txt))
  8643. (setq cpltxt
  8644. (concat cpltxt "::"
  8645. (condition-case nil
  8646. (org-make-org-heading-search-string txt)
  8647. (error "")))
  8648. desc (or (and (eq et 'keyword) eok ev)
  8649. (nth 4 (ignore-errors (org-heading-components)))
  8650. "NONE")))))
  8651. (if (string-match "::\\'" cpltxt)
  8652. (setq cpltxt (substring cpltxt 0 -2)))
  8653. (setq link cpltxt))))
  8654. ((buffer-file-name (buffer-base-buffer))
  8655. ;; Just link to this file here.
  8656. (setq cpltxt (concat "file:"
  8657. (abbreviate-file-name
  8658. (buffer-file-name (buffer-base-buffer)))))
  8659. ;; Add a context string.
  8660. (when (org-xor org-context-in-file-links arg)
  8661. (setq txt (if (org-region-active-p)
  8662. (buffer-substring (region-beginning) (region-end))
  8663. (buffer-substring (point-at-bol) (point-at-eol))))
  8664. ;; Only use search option if there is some text.
  8665. (when (string-match "\\S-" txt)
  8666. (setq cpltxt
  8667. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8668. desc "NONE")))
  8669. (setq link cpltxt))
  8670. ((org-called-interactively-p 'interactive)
  8671. (user-error "No method for storing a link from this buffer"))
  8672. (t (setq link nil)))
  8673. ;; We're done setting link and desc, clean up
  8674. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8675. (setq link (or link cpltxt)
  8676. desc (or desc cpltxt))
  8677. (cond ((equal desc "NONE") (setq desc nil))
  8678. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8679. (let ((d0 (match-string 3 desc))
  8680. (p0 (match-string 5 desc)))
  8681. (setq desc
  8682. (replace-regexp-in-string
  8683. org-bracket-link-regexp
  8684. (concat (or p0 d0)
  8685. (if (equal (length (match-string 0 desc))
  8686. (length desc)) "*" "")) desc)))))
  8687. ;; Return the link
  8688. (if (not (and (or (org-called-interactively-p 'any)
  8689. executing-kbd-macro) link))
  8690. (or agenda-link (and link (org-make-link-string link desc)))
  8691. (push (list link desc) org-stored-links)
  8692. (message "Stored: %s" (or desc link))
  8693. (when custom-id
  8694. (setq link (concat "file:" (abbreviate-file-name
  8695. (buffer-file-name)) "::#" custom-id))
  8696. (push (list link desc) org-stored-links))
  8697. (car org-stored-links))))))
  8698. (defun org-store-link-props (&rest plist)
  8699. "Store link properties, extract names and addresses."
  8700. (let (x adr)
  8701. (when (setq x (plist-get plist :from))
  8702. (setq adr (mail-extract-address-components x))
  8703. (setq plist (plist-put plist :fromname (car adr)))
  8704. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8705. (when (setq x (plist-get plist :to))
  8706. (setq adr (mail-extract-address-components x))
  8707. (setq plist (plist-put plist :toname (car adr)))
  8708. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8709. (let ((from (plist-get plist :from))
  8710. (to (plist-get plist :to)))
  8711. (when (and from to org-from-is-user-regexp)
  8712. (setq plist
  8713. (plist-put plist :fromto
  8714. (if (string-match org-from-is-user-regexp from)
  8715. (concat "to %t")
  8716. (concat "from %f"))))))
  8717. (setq org-store-link-plist plist))
  8718. (defun org-add-link-props (&rest plist)
  8719. "Add these properties to the link property list."
  8720. (let (key value)
  8721. (while plist
  8722. (setq key (pop plist) value (pop plist))
  8723. (setq org-store-link-plist
  8724. (plist-put org-store-link-plist key value)))))
  8725. (defun org-email-link-description (&optional fmt)
  8726. "Return the description part of an email link.
  8727. This takes information from `org-store-link-plist' and formats it
  8728. according to FMT (default from `org-email-link-description-format')."
  8729. (setq fmt (or fmt org-email-link-description-format))
  8730. (let* ((p org-store-link-plist)
  8731. (to (plist-get p :toaddress))
  8732. (from (plist-get p :fromaddress))
  8733. (table
  8734. (list
  8735. (cons "%c" (plist-get p :fromto))
  8736. (cons "%F" (plist-get p :from))
  8737. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8738. (cons "%T" (plist-get p :to))
  8739. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8740. (cons "%s" (plist-get p :subject))
  8741. (cons "%d" (plist-get p :date))
  8742. (cons "%m" (plist-get p :message-id)))))
  8743. (when (string-match "%c" fmt)
  8744. ;; Check if the user wrote this message
  8745. (if (and org-from-is-user-regexp from to
  8746. (save-match-data (string-match org-from-is-user-regexp from)))
  8747. (setq fmt (replace-match "to %t" t t fmt))
  8748. (setq fmt (replace-match "from %f" t t fmt))))
  8749. (org-replace-escapes fmt table)))
  8750. (defun org-make-org-heading-search-string (&optional string)
  8751. "Make search string for the current headline or STRING."
  8752. (let ((s (or string
  8753. (and (derived-mode-p 'org-mode)
  8754. (save-excursion
  8755. (org-back-to-heading t)
  8756. (org-element-property :raw-value (org-element-at-point))))))
  8757. (lines org-context-in-file-links))
  8758. (or string (setq s (concat "*" s))) ; Add * for headlines
  8759. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8760. (when (and string (integerp lines) (> lines 0))
  8761. (let ((slines (org-split-string s "\n")))
  8762. (when (< lines (length slines))
  8763. (setq s (mapconcat
  8764. 'identity
  8765. (reverse (nthcdr (- (length slines) lines)
  8766. (reverse slines))) "\n")))))
  8767. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8768. (defun org-make-link-string (link &optional description)
  8769. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8770. (unless (string-match "\\S-" link)
  8771. (error "Empty link"))
  8772. (when (and description
  8773. (stringp description)
  8774. (not (string-match "\\S-" description)))
  8775. (setq description nil))
  8776. (when (stringp description)
  8777. ;; Remove brackets from the description, they are fatal.
  8778. (while (string-match "\\[" description)
  8779. (setq description (replace-match "{" t t description)))
  8780. (while (string-match "\\]" description)
  8781. (setq description (replace-match "}" t t description))))
  8782. (when (equal link description)
  8783. ;; No description needed, it is identical
  8784. (setq description nil))
  8785. (when (and (not description)
  8786. (not (string-match (org-image-file-name-regexp) link))
  8787. (not (equal link (org-link-escape link))))
  8788. (setq description (org-extract-attributes link)))
  8789. (setq link
  8790. (cond ((string-match (org-image-file-name-regexp) link) link)
  8791. ((string-match org-link-types-re link)
  8792. (concat (match-string 1 link)
  8793. (org-link-escape (substring link (match-end 1)))))
  8794. (t (org-link-escape link))))
  8795. (concat "[[" link "]"
  8796. (if description (concat "[" description "]") "")
  8797. "]"))
  8798. (defconst org-link-escape-chars
  8799. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8800. "List of characters that should be escaped in link.
  8801. This is the list that is used for internal purposes.")
  8802. (defconst org-link-escape-chars-browser
  8803. '(?\ ?\")
  8804. "List of escapes for characters that are problematic in links.
  8805. This is the list that is used before handing over to the browser.")
  8806. (defun org-link-escape (text &optional table merge)
  8807. "Return percent escaped representation of TEXT.
  8808. TEXT is a string with the text to escape.
  8809. Optional argument TABLE is a list with characters that should be
  8810. escaped. When nil, `org-link-escape-chars' is used.
  8811. If optional argument MERGE is set, merge TABLE into
  8812. `org-link-escape-chars'."
  8813. (cond
  8814. ((and table merge)
  8815. (mapc (lambda (defchr)
  8816. (unless (member defchr table)
  8817. (setq table (cons defchr table)))) org-link-escape-chars))
  8818. ((null table)
  8819. (setq table org-link-escape-chars)))
  8820. (mapconcat
  8821. (lambda (char)
  8822. (if (or (member char table)
  8823. (and (or (< char 32) (= char 37) (> char 126))
  8824. org-url-hexify-p))
  8825. (mapconcat (lambda (sequence-element)
  8826. (format "%%%.2X" sequence-element))
  8827. (or (encode-coding-char char 'utf-8)
  8828. (error "Unable to percent escape character: %s"
  8829. (char-to-string char))) "")
  8830. (char-to-string char))) text ""))
  8831. (defun org-link-unescape (str)
  8832. "Unhex hexified Unicode strings as returned from the JavaScript function
  8833. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8834. (unless (and (null str) (string= "" str))
  8835. (let ((pos 0) (case-fold-search t) unhexed)
  8836. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8837. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8838. (setq str (replace-match unhexed t t str))
  8839. (setq pos (+ pos (length unhexed))))))
  8840. str)
  8841. (defun org-link-unescape-compound (hex)
  8842. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8843. Note: this function also decodes single byte encodings like
  8844. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8845. (save-match-data
  8846. (let* ((bytes (cdr (split-string hex "%")))
  8847. (ret "")
  8848. (eat 0)
  8849. (sum 0))
  8850. (while bytes
  8851. (let* ((val (string-to-number (pop bytes) 16))
  8852. (shift-xor
  8853. (if (= 0 eat)
  8854. (cond
  8855. ((>= val 252) (cons 6 252))
  8856. ((>= val 248) (cons 5 248))
  8857. ((>= val 240) (cons 4 240))
  8858. ((>= val 224) (cons 3 224))
  8859. ((>= val 192) (cons 2 192))
  8860. (t (cons 0 0)))
  8861. (cons 6 128))))
  8862. (if (>= val 192) (setq eat (car shift-xor)))
  8863. (setq val (logxor val (cdr shift-xor)))
  8864. (setq sum (+ (lsh sum (car shift-xor)) val))
  8865. (if (> eat 0) (setq eat (- eat 1)))
  8866. (cond
  8867. ((= 0 eat) ;multi byte
  8868. (setq ret (concat ret (org-char-to-string sum)))
  8869. (setq sum 0))
  8870. ((not bytes) ; single byte(s)
  8871. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8872. )) ;; end (while bytes
  8873. ret )))
  8874. (defun org-link-unescape-single-byte-sequence (hex)
  8875. "Unhexify hex-encoded single byte character sequences."
  8876. (mapconcat (lambda (byte)
  8877. (char-to-string (string-to-number byte 16)))
  8878. (cdr (split-string hex "%")) ""))
  8879. (defun org-xor (a b)
  8880. "Exclusive or."
  8881. (if a (not b) b))
  8882. (defun org-fixup-message-id-for-http (s)
  8883. "Replace special characters in a message id, so it can be used in an http query."
  8884. (when (string-match "%" s)
  8885. (setq s (mapconcat (lambda (c)
  8886. (if (eq c ?%)
  8887. "%25"
  8888. (char-to-string c)))
  8889. s "")))
  8890. (while (string-match "<" s)
  8891. (setq s (replace-match "%3C" t t s)))
  8892. (while (string-match ">" s)
  8893. (setq s (replace-match "%3E" t t s)))
  8894. (while (string-match "@" s)
  8895. (setq s (replace-match "%40" t t s)))
  8896. s)
  8897. (defun org-link-prettify (link)
  8898. "Return a human-readable representation of LINK.
  8899. The car of LINK must be a raw link the cdr of LINK must be either
  8900. a link description or nil."
  8901. (let ((desc (or (cadr link) "<no description>")))
  8902. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8903. "<" (car link) ">")))
  8904. ;;;###autoload
  8905. (defun org-insert-link-global ()
  8906. "Insert a link like Org-mode does.
  8907. This command can be called in any mode to insert a link in Org-mode syntax."
  8908. (interactive)
  8909. (org-load-modules-maybe)
  8910. (org-run-like-in-org-mode 'org-insert-link))
  8911. (defun org-insert-all-links (&optional keep)
  8912. "Insert all links in `org-stored-links'."
  8913. (interactive "P")
  8914. (let ((links (copy-sequence org-stored-links)) l)
  8915. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8916. (insert "- ")
  8917. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8918. (insert "\n"))))
  8919. (defun org-link-fontify-links-to-this-file ()
  8920. "Fontify links to the current file in `org-stored-links'."
  8921. (let ((f (buffer-file-name)) a b)
  8922. (setq a (mapcar (lambda(l)
  8923. (let ((ll (car l)))
  8924. (when (and (string-match "^file:\\(.+\\)::" ll)
  8925. (equal f (expand-file-name (match-string 1 ll))))
  8926. ll)))
  8927. org-stored-links))
  8928. (when (featurep 'org-id)
  8929. (setq b (mapcar (lambda(l)
  8930. (let ((ll (car l)))
  8931. (when (and (string-match "^id:\\(.+\\)$" ll)
  8932. (equal f (expand-file-name
  8933. (or (org-id-find-id-file
  8934. (match-string 1 ll)) ""))))
  8935. ll)))
  8936. org-stored-links)))
  8937. (mapcar (lambda(l)
  8938. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8939. (delq nil (append a b)))))
  8940. (defvar org-link-links-in-this-file nil)
  8941. (defun org-insert-link (&optional complete-file link-location default-description)
  8942. "Insert a link. At the prompt, enter the link.
  8943. Completion can be used to insert any of the link protocol prefixes like
  8944. http or ftp in use.
  8945. The history can be used to select a link previously stored with
  8946. `org-store-link'. When the empty string is entered (i.e. if you just
  8947. press RET at the prompt), the link defaults to the most recently
  8948. stored link. As SPC triggers completion in the minibuffer, you need to
  8949. use M-SPC or C-q SPC to force the insertion of a space character.
  8950. You will also be prompted for a description, and if one is given, it will
  8951. be displayed in the buffer instead of the link.
  8952. If there is already a link at point, this command will allow you to edit link
  8953. and description parts.
  8954. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8955. be selected using completion. The path to the file will be relative to the
  8956. current directory if the file is in the current directory or a subdirectory.
  8957. Otherwise, the link will be the absolute path as completed in the minibuffer
  8958. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8959. option `org-link-file-path-type'.
  8960. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8961. the current directory or below.
  8962. With three \\[universal-argument] prefixes, negate the meaning of
  8963. `org-keep-stored-link-after-insertion'.
  8964. If `org-make-link-description-function' is non-nil, this function will be
  8965. called with the link target, and the result will be the default
  8966. link description.
  8967. If the LINK-LOCATION parameter is non-nil, this value will be
  8968. used as the link location instead of reading one interactively.
  8969. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8970. be used as the default description."
  8971. (interactive "P")
  8972. (let* ((wcf (current-window-configuration))
  8973. (origbuf (current-buffer))
  8974. (region (if (org-region-active-p)
  8975. (buffer-substring (region-beginning) (region-end))))
  8976. (remove (and region (list (region-beginning) (region-end))))
  8977. (desc region)
  8978. tmphist ; byte-compile incorrectly complains about this
  8979. (link link-location)
  8980. (abbrevs org-link-abbrev-alist-local)
  8981. entry file all-prefixes auto-desc)
  8982. (cond
  8983. (link-location) ; specified by arg, just use it.
  8984. ((org-in-regexp org-bracket-link-regexp 1)
  8985. ;; We do have a link at point, and we are going to edit it.
  8986. (setq remove (list (match-beginning 0) (match-end 0)))
  8987. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8988. (setq link (read-string "Link: "
  8989. (org-link-unescape
  8990. (org-match-string-no-properties 1)))))
  8991. ((or (org-in-regexp org-angle-link-re)
  8992. (org-in-regexp org-plain-link-re))
  8993. ;; Convert to bracket link
  8994. (setq remove (list (match-beginning 0) (match-end 0))
  8995. link (read-string "Link: "
  8996. (org-remove-angle-brackets (match-string 0)))))
  8997. ((member complete-file '((4) (16)))
  8998. ;; Completing read for file names.
  8999. (setq link (org-file-complete-link complete-file)))
  9000. (t
  9001. ;; Read link, with completion for stored links.
  9002. (org-link-fontify-links-to-this-file)
  9003. (org-switch-to-buffer-other-window "*Org Links*")
  9004. (with-current-buffer "*Org Links*"
  9005. (erase-buffer)
  9006. (insert "Insert a link.
  9007. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9008. (when org-stored-links
  9009. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9010. (insert (mapconcat 'org-link-prettify
  9011. (reverse org-stored-links) "\n")))
  9012. (goto-char (point-min)))
  9013. (let ((cw (selected-window)))
  9014. (select-window (get-buffer-window "*Org Links*" 'visible))
  9015. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9016. (unless (pos-visible-in-window-p (point-max))
  9017. (org-fit-window-to-buffer))
  9018. (and (window-live-p cw) (select-window cw)))
  9019. ;; Fake a link history, containing the stored links.
  9020. (setq tmphist (append (mapcar 'car org-stored-links)
  9021. org-insert-link-history))
  9022. (setq all-prefixes (append (mapcar 'car abbrevs)
  9023. (mapcar 'car org-link-abbrev-alist)
  9024. org-link-types))
  9025. (unwind-protect
  9026. (progn
  9027. (setq link
  9028. (org-completing-read
  9029. "Link: "
  9030. (append
  9031. (mapcar (lambda (x) (concat x ":"))
  9032. all-prefixes)
  9033. (mapcar 'car org-stored-links))
  9034. nil nil nil
  9035. 'tmphist
  9036. (caar org-stored-links)))
  9037. (if (not (string-match "\\S-" link))
  9038. (user-error "No link selected"))
  9039. (mapc (lambda(l)
  9040. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9041. org-stored-links)
  9042. (if (or (member link all-prefixes)
  9043. (and (equal ":" (substring link -1))
  9044. (member (substring link 0 -1) all-prefixes)
  9045. (setq link (substring link 0 -1))))
  9046. (setq link (with-current-buffer origbuf
  9047. (org-link-try-special-completion link)))))
  9048. (set-window-configuration wcf)
  9049. (kill-buffer "*Org Links*"))
  9050. (setq entry (assoc link org-stored-links))
  9051. (or entry (push link org-insert-link-history))
  9052. (setq desc (or desc (nth 1 entry)))))
  9053. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9054. (not org-keep-stored-link-after-insertion))
  9055. (setq org-stored-links (delq (assoc link org-stored-links)
  9056. org-stored-links)))
  9057. (if (and (string-match org-plain-link-re link)
  9058. (not (string-match org-ts-regexp link)))
  9059. ;; URL-like link, normalize the use of angular brackets.
  9060. (setq link (org-remove-angle-brackets link)))
  9061. ;; Check if we are linking to the current file with a search
  9062. ;; option If yes, simplify the link by using only the search
  9063. ;; option.
  9064. (when (and buffer-file-name
  9065. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9066. (let* ((path (match-string 1 link))
  9067. (case-fold-search nil)
  9068. (search (match-string 2 link)))
  9069. (save-match-data
  9070. (if (equal (file-truename buffer-file-name) (file-truename path))
  9071. ;; We are linking to this same file, with a search option
  9072. (setq link search)))))
  9073. ;; Check if we can/should use a relative path. If yes, simplify the link
  9074. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9075. (let* ((type (match-string 1 link))
  9076. (path (match-string 2 link))
  9077. (origpath path)
  9078. (case-fold-search nil))
  9079. (cond
  9080. ((or (eq org-link-file-path-type 'absolute)
  9081. (equal complete-file '(16)))
  9082. (setq path (abbreviate-file-name (expand-file-name path))))
  9083. ((eq org-link-file-path-type 'noabbrev)
  9084. (setq path (expand-file-name path)))
  9085. ((eq org-link-file-path-type 'relative)
  9086. (setq path (file-relative-name path)))
  9087. (t
  9088. (save-match-data
  9089. (if (string-match (concat "^" (regexp-quote
  9090. (expand-file-name
  9091. (file-name-as-directory
  9092. default-directory))))
  9093. (expand-file-name path))
  9094. ;; We are linking a file with relative path name.
  9095. (setq path (substring (expand-file-name path)
  9096. (match-end 0)))
  9097. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9098. (setq link (concat type path))
  9099. (if (equal desc origpath)
  9100. (setq desc path))))
  9101. (if org-make-link-description-function
  9102. (setq desc
  9103. (or (condition-case nil
  9104. (funcall org-make-link-description-function link desc)
  9105. (error (progn (message "Can't get link description from `%s'"
  9106. (symbol-name org-make-link-description-function))
  9107. (sit-for 2) nil)))
  9108. (read-string "Description: " default-description)))
  9109. (if default-description (setq desc default-description)
  9110. (setq desc (or (and auto-desc desc)
  9111. (read-string "Description: " desc)))))
  9112. (unless (string-match "\\S-" desc) (setq desc nil))
  9113. (if remove (apply 'delete-region remove))
  9114. (insert (org-make-link-string link desc))))
  9115. (defun org-link-try-special-completion (type)
  9116. "If there is completion support for link type TYPE, offer it."
  9117. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9118. (if (functionp fun)
  9119. (funcall fun)
  9120. (read-string "Link (no completion support): " (concat type ":")))))
  9121. (defun org-file-complete-link (&optional arg)
  9122. "Create a file link using completion."
  9123. (let (file link)
  9124. (setq file (org-iread-file-name "File: "))
  9125. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9126. (pwd1 (file-name-as-directory (abbreviate-file-name
  9127. (expand-file-name ".")))))
  9128. (cond
  9129. ((equal arg '(16))
  9130. (setq link (concat
  9131. "file:"
  9132. (abbreviate-file-name (expand-file-name file)))))
  9133. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9134. (setq link (concat "file:" (match-string 1 file))))
  9135. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9136. (expand-file-name file))
  9137. (setq link (concat
  9138. "file:" (match-string 1 (expand-file-name file)))))
  9139. (t (setq link (concat "file:" file)))))
  9140. link))
  9141. (defun org-iread-file-name (&rest args)
  9142. "Read-file-name using `ido-mode' speedup if available.
  9143. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9144. See `read-file-name' for a description of parameters."
  9145. (org-without-partial-completion
  9146. (if (and org-completion-use-ido
  9147. (fboundp 'ido-read-file-name)
  9148. (boundp 'ido-mode) ido-mode
  9149. (listp (second args)))
  9150. (let ((ido-enter-matching-directory nil))
  9151. (apply 'ido-read-file-name args))
  9152. (apply 'read-file-name args))))
  9153. (defun org-completing-read (&rest args)
  9154. "Completing-read with SPACE being a normal character."
  9155. (let ((enable-recursive-minibuffers t)
  9156. (minibuffer-local-completion-map
  9157. (copy-keymap minibuffer-local-completion-map)))
  9158. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9159. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9160. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9161. (apply 'org-icompleting-read args)))
  9162. (defun org-completing-read-no-i (&rest args)
  9163. (let (org-completion-use-ido org-completion-use-iswitchb)
  9164. (apply 'org-completing-read args)))
  9165. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9166. "Use iswitch as a completing-read replacement to choose from choices.
  9167. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9168. from."
  9169. (let* ((iswitchb-use-virtual-buffers nil)
  9170. (iswitchb-make-buflist-hook
  9171. (lambda ()
  9172. (setq iswitchb-temp-buflist choices))))
  9173. (iswitchb-read-buffer prompt)))
  9174. (defun org-icompleting-read (&rest args)
  9175. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9176. (org-without-partial-completion
  9177. (if (and org-completion-use-ido
  9178. (fboundp 'ido-completing-read)
  9179. (boundp 'ido-mode) ido-mode
  9180. (listp (second args)))
  9181. (let ((ido-enter-matching-directory nil))
  9182. (apply 'ido-completing-read (concat (car args))
  9183. (if (consp (car (nth 1 args)))
  9184. (mapcar 'car (nth 1 args))
  9185. (nth 1 args))
  9186. (cddr args)))
  9187. (if (and org-completion-use-iswitchb
  9188. (boundp 'iswitchb-mode) iswitchb-mode
  9189. (listp (second args)))
  9190. (apply 'org-iswitchb-completing-read (concat (car args))
  9191. (if (consp (car (nth 1 args)))
  9192. (mapcar 'car (nth 1 args))
  9193. (nth 1 args))
  9194. (cddr args))
  9195. (apply 'completing-read args)))))
  9196. (defun org-extract-attributes (s)
  9197. "Extract the attributes cookie from a string and set as text property."
  9198. (let (a attr (start 0) key value)
  9199. (save-match-data
  9200. (when (string-match "{{\\([^}]+\\)}}$" s)
  9201. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9202. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9203. (setq key (match-string 1 a) value (match-string 2 a)
  9204. start (match-end 0)
  9205. attr (plist-put attr (intern key) value))))
  9206. (org-add-props s nil 'org-attr attr))
  9207. s))
  9208. ;;; Opening/following a link
  9209. (defvar org-link-search-failed nil)
  9210. (defvar org-open-link-functions nil
  9211. "Hook for functions finding a plain text link.
  9212. These functions must take a single argument, the link content.
  9213. They will be called for links that look like [[link text][description]]
  9214. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9215. like a filename (e.g. \"./blue.png\").
  9216. These functions will be called *before* Org attempts to resolve the
  9217. link by doing text searches in the current buffer - so if you want a
  9218. link \"[[target]]\" to still find \"<<target>>\", your function should
  9219. handle this as a special case.
  9220. When the function does handle the link, it must return a non-nil value.
  9221. If it decides that it is not responsible for this link, it must return
  9222. nil to indicate that that Org-mode can continue with other options
  9223. like exact and fuzzy text search.")
  9224. (defun org-next-link (&optional search-backward)
  9225. "Move forward to the next link.
  9226. If the link is in hidden text, expose it."
  9227. (interactive "P")
  9228. (when (and org-link-search-failed (eq this-command last-command))
  9229. (goto-char (point-min))
  9230. (message "Link search wrapped back to beginning of buffer"))
  9231. (setq org-link-search-failed nil)
  9232. (let* ((pos (point))
  9233. (ct (org-context))
  9234. (a (assoc :link ct))
  9235. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9236. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9237. ((looking-at org-any-link-re)
  9238. ;; Don't stay stuck at link without an org-link face
  9239. (forward-char (if search-backward -1 1))))
  9240. (if (funcall srch-fun org-any-link-re nil t)
  9241. (progn
  9242. (goto-char (match-beginning 0))
  9243. (if (outline-invisible-p) (org-show-context)))
  9244. (goto-char pos)
  9245. (setq org-link-search-failed t)
  9246. (message "No further link found"))))
  9247. (defun org-previous-link ()
  9248. "Move backward to the previous link.
  9249. If the link is in hidden text, expose it."
  9250. (interactive)
  9251. (funcall 'org-next-link t))
  9252. (defun org-translate-link (s)
  9253. "Translate a link string if a translation function has been defined."
  9254. (if (and org-link-translation-function
  9255. (fboundp org-link-translation-function)
  9256. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9257. (progn
  9258. (setq s (funcall org-link-translation-function
  9259. (match-string 1 s) (match-string 2 s)))
  9260. (concat (car s) ":" (cdr s)))
  9261. s))
  9262. (defun org-translate-link-from-planner (type path)
  9263. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9264. This is still an experimental function, your mileage may vary."
  9265. (cond
  9266. ((member type '("http" "https" "news" "ftp"))
  9267. ;; standard Internet links are the same.
  9268. nil)
  9269. ((and (equal type "irc") (string-match "^//" path))
  9270. ;; Planner has two / at the beginning of an irc link, we have 1.
  9271. ;; We should have zero, actually....
  9272. (setq path (substring path 1)))
  9273. ((and (equal type "lisp") (string-match "^/" path))
  9274. ;; Planner has a slash, we do not.
  9275. (setq type "elisp" path (substring path 1)))
  9276. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9277. ;; A typical message link. Planner has the id after the final slash,
  9278. ;; we separate it with a hash mark
  9279. (setq path (concat (match-string 1 path) "#"
  9280. (org-remove-angle-brackets (match-string 2 path))))))
  9281. (cons type path))
  9282. (defun org-find-file-at-mouse (ev)
  9283. "Open file link or URL at mouse."
  9284. (interactive "e")
  9285. (mouse-set-point ev)
  9286. (org-open-at-point 'in-emacs))
  9287. (defun org-open-at-mouse (ev)
  9288. "Open file link or URL at mouse.
  9289. See the docstring of `org-open-file' for details."
  9290. (interactive "e")
  9291. (mouse-set-point ev)
  9292. (if (eq major-mode 'org-agenda-mode)
  9293. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9294. (org-open-at-point))
  9295. (defvar org-window-config-before-follow-link nil
  9296. "The window configuration before following a link.
  9297. This is saved in case the need arises to restore it.")
  9298. (defvar org-open-link-marker (make-marker)
  9299. "Marker pointing to the location where `org-open-at-point' was called.")
  9300. ;;;###autoload
  9301. (defun org-open-at-point-global ()
  9302. "Follow a link like Org-mode does.
  9303. This command can be called in any mode to follow a link that has
  9304. Org-mode syntax."
  9305. (interactive)
  9306. (org-run-like-in-org-mode 'org-open-at-point))
  9307. ;;;###autoload
  9308. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9309. "Open a link in the string S, as if it was in Org-mode."
  9310. (interactive "sLink: \nP")
  9311. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9312. (with-temp-buffer
  9313. (let ((org-inhibit-startup (not reference-buffer)))
  9314. (org-mode)
  9315. (insert s)
  9316. (goto-char (point-min))
  9317. (when reference-buffer
  9318. (setq org-link-abbrev-alist-local
  9319. (with-current-buffer reference-buffer
  9320. org-link-abbrev-alist-local)))
  9321. (org-open-at-point arg reference-buffer)))))
  9322. (defvar org-open-at-point-functions nil
  9323. "Hook that is run when following a link at point.
  9324. Functions in this hook must return t if they identify and follow
  9325. a link at point. If they don't find anything interesting at point,
  9326. they must return nil.")
  9327. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9328. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9329. (defun org-open-at-point (&optional arg reference-buffer)
  9330. "Open link at or after point.
  9331. If there is no link at point, this function will search forward up to
  9332. the end of the current line.
  9333. Normally, files will be opened by an appropriate application. If the
  9334. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9335. With a double prefix argument, try to open outside of Emacs, in the
  9336. application the system uses for this file type."
  9337. (interactive "P")
  9338. ;; if in a code block, then open the block's results
  9339. (unless (call-interactively #'org-babel-open-src-block-result)
  9340. (org-load-modules-maybe)
  9341. (move-marker org-open-link-marker (point))
  9342. (setq org-window-config-before-follow-link (current-window-configuration))
  9343. (org-remove-occur-highlights nil nil t)
  9344. (cond
  9345. ((and (org-at-heading-p)
  9346. (not (org-at-timestamp-p t))
  9347. (not (org-in-regexp
  9348. (concat org-plain-link-re "\\|"
  9349. org-bracket-link-regexp "\\|"
  9350. org-angle-link-re "\\|"
  9351. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9352. (not (get-text-property (point) 'org-linked-text)))
  9353. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9354. (lk0 (car lkall))
  9355. (lk (if (stringp lk0) (list lk0) lk0))
  9356. (lkend (cdr lkall)))
  9357. (mapcar (lambda(l)
  9358. (search-forward l nil lkend)
  9359. (goto-char (match-beginning 0))
  9360. (org-open-at-point))
  9361. lk))
  9362. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9363. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9364. ((and (org-at-timestamp-p t)
  9365. (not (org-in-regexp org-bracket-link-regexp)))
  9366. (org-follow-timestamp-link))
  9367. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9368. (not (org-in-regexp org-any-link-re)))
  9369. (org-footnote-action))
  9370. (t
  9371. (let (type path link line search (pos (point)))
  9372. (catch 'match
  9373. (save-excursion
  9374. (or (org-in-regexp org-plain-link-re)
  9375. (skip-chars-forward "^]\n\r"))
  9376. (when (org-in-regexp org-bracket-link-regexp 1)
  9377. (setq link (org-extract-attributes
  9378. (org-link-unescape (org-match-string-no-properties 1))))
  9379. (while (string-match " *\n *" link)
  9380. (setq link (replace-match " " t t link)))
  9381. (setq link (org-link-expand-abbrev link))
  9382. (cond
  9383. ((or (file-name-absolute-p link)
  9384. (string-match "^\\.\\.?/" link))
  9385. (setq type "file" path link))
  9386. ((string-match org-link-re-with-space3 link)
  9387. (setq type (match-string 1 link) path (match-string 2 link)))
  9388. ((string-match "^help:+\\(.+\\)" link)
  9389. (setq type "help" path (match-string 1 link)))
  9390. (t (setq type "thisfile" path link)))
  9391. (throw 'match t)))
  9392. (when (get-text-property (point) 'org-linked-text)
  9393. (setq type "thisfile"
  9394. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9395. (1+ (point)) (point))
  9396. path (buffer-substring
  9397. (or (previous-single-property-change pos 'org-linked-text)
  9398. (point-min))
  9399. (or (next-single-property-change pos 'org-linked-text)
  9400. (point-max)))
  9401. ;; Ensure we will search for a <<<radio>>> link, not
  9402. ;; a simple reference like <<ref>>
  9403. path (concat "<" path))
  9404. (throw 'match t))
  9405. (save-excursion
  9406. (when (or (org-in-regexp org-angle-link-re)
  9407. (let ((match (org-in-regexp org-plain-link-re)))
  9408. ;; Check a plain link is not within a bracket link
  9409. (and match
  9410. (save-excursion
  9411. (save-match-data
  9412. (progn
  9413. (goto-char (car match))
  9414. (not (org-in-regexp org-bracket-link-regexp)))))))
  9415. (let ((line_ending (save-excursion (end-of-line) (point))))
  9416. ;; We are in a line before a plain or bracket link
  9417. (or (re-search-forward org-plain-link-re line_ending t)
  9418. (re-search-forward org-bracket-link-regexp line_ending t))))
  9419. (setq type (match-string 1)
  9420. path (org-link-unescape (match-string 2)))
  9421. (throw 'match t)))
  9422. (save-excursion
  9423. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9424. (setq type "tags"
  9425. path (match-string 1))
  9426. (while (string-match ":" path)
  9427. (setq path (replace-match "+" t t path)))
  9428. (throw 'match t)))
  9429. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9430. (setq type "tree-match"
  9431. path (match-string 1))
  9432. (throw 'match t)))
  9433. (unless path
  9434. (user-error "No link found"))
  9435. ;; switch back to reference buffer
  9436. ;; needed when if called in a temporary buffer through
  9437. ;; org-open-link-from-string
  9438. (with-current-buffer (or reference-buffer (current-buffer))
  9439. ;; Remove any trailing spaces in path
  9440. (if (string-match " +\\'" path)
  9441. (setq path (replace-match "" t t path)))
  9442. (if (and org-link-translation-function
  9443. (fboundp org-link-translation-function))
  9444. ;; Check if we need to translate the link
  9445. (let ((tmp (funcall org-link-translation-function type path)))
  9446. (setq type (car tmp) path (cdr tmp))))
  9447. (cond
  9448. ((assoc type org-link-protocols)
  9449. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9450. ((equal type "help")
  9451. (let ((f-or-v (intern path)))
  9452. (cond ((fboundp f-or-v)
  9453. (describe-function f-or-v))
  9454. ((boundp f-or-v)
  9455. (describe-variable f-or-v))
  9456. (t (error "Not a known function or variable")))))
  9457. ((equal type "mailto")
  9458. (let ((cmd (car org-link-mailto-program))
  9459. (args (cdr org-link-mailto-program)) args1
  9460. (address path) (subject "") a)
  9461. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9462. (setq address (match-string 1 path)
  9463. subject (org-link-escape (match-string 2 path))))
  9464. (while args
  9465. (cond
  9466. ((not (stringp (car args))) (push (pop args) args1))
  9467. (t (setq a (pop args))
  9468. (if (string-match "%a" a)
  9469. (setq a (replace-match address t t a)))
  9470. (if (string-match "%s" a)
  9471. (setq a (replace-match subject t t a)))
  9472. (push a args1))))
  9473. (apply cmd (nreverse args1))))
  9474. ((member type '("http" "https" "ftp" "news"))
  9475. (browse-url
  9476. (concat type ":"
  9477. (if (org-string-match-p
  9478. (concat "[[:nonascii:]"
  9479. org-link-escape-chars-browser "]")
  9480. path)
  9481. (org-link-escape path org-link-escape-chars-browser)
  9482. path))))
  9483. ((string= type "doi")
  9484. (browse-url
  9485. (concat org-doi-server-url
  9486. (if (org-string-match-p
  9487. (concat "[[:nonascii:]"
  9488. org-link-escape-chars-browser "]")
  9489. path)
  9490. (org-link-escape path org-link-escape-chars-browser)
  9491. path))))
  9492. ((member type '("message"))
  9493. (browse-url (concat type ":" path)))
  9494. ((string= type "tags")
  9495. (org-tags-view arg path))
  9496. ((string= type "tree-match")
  9497. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9498. ((string= type "file")
  9499. (if (string-match "::\\([0-9]+\\)\\'" path)
  9500. (setq line (string-to-number (match-string 1 path))
  9501. path (substring path 0 (match-beginning 0)))
  9502. (if (string-match "::\\(.+\\)\\'" path)
  9503. (setq search (match-string 1 path)
  9504. path (substring path 0 (match-beginning 0)))))
  9505. (if (string-match "[*?{]" (file-name-nondirectory path))
  9506. (dired path)
  9507. (org-open-file path arg line search)))
  9508. ((string= type "shell")
  9509. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9510. (cmd path))
  9511. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9512. (string-match org-confirm-shell-link-not-regexp cmd))
  9513. (not org-confirm-shell-link-function)
  9514. (funcall org-confirm-shell-link-function
  9515. (format "Execute \"%s\" in shell? "
  9516. (org-add-props cmd nil
  9517. 'face 'org-warning))))
  9518. (progn
  9519. (message "Executing %s" cmd)
  9520. (shell-command cmd buf)
  9521. (if (featurep 'midnight)
  9522. (setq clean-buffer-list-kill-buffer-names
  9523. (cons buf clean-buffer-list-kill-buffer-names))))
  9524. (error "Abort"))))
  9525. ((string= type "elisp")
  9526. (let ((cmd path))
  9527. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9528. (string-match org-confirm-elisp-link-not-regexp cmd))
  9529. (not org-confirm-elisp-link-function)
  9530. (funcall org-confirm-elisp-link-function
  9531. (format "Execute \"%s\" as elisp? "
  9532. (org-add-props cmd nil
  9533. 'face 'org-warning))))
  9534. (message "%s => %s" cmd
  9535. (if (equal (string-to-char cmd) ?\()
  9536. (eval (read cmd))
  9537. (call-interactively (read cmd))))
  9538. (error "Abort"))))
  9539. ((and (string= type "thisfile")
  9540. (or (run-hook-with-args-until-success
  9541. 'org-open-link-functions path)
  9542. (and link
  9543. (string-match "^id:" link)
  9544. (or (featurep 'org-id) (require 'org-id))
  9545. (progn
  9546. (funcall (nth 1 (assoc "id" org-link-protocols))
  9547. (substring path 3))
  9548. t)))))
  9549. ((string= type "thisfile")
  9550. (if arg
  9551. (switch-to-buffer-other-window
  9552. (org-get-buffer-for-internal-link (current-buffer)))
  9553. (org-mark-ring-push))
  9554. (let ((cmd `(org-link-search
  9555. ,path
  9556. ,(cond ((equal arg '(4)) ''occur)
  9557. ((equal arg '(16)) ''org-occur))
  9558. ,pos)))
  9559. (condition-case nil (let ((org-link-search-inhibit-query t))
  9560. (eval cmd))
  9561. (error (progn (widen) (eval cmd))))))
  9562. (t (browse-url-at-point)))))))
  9563. (move-marker org-open-link-marker nil)
  9564. (run-hook-with-args 'org-follow-link-hook)))
  9565. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9566. "Offer links in the current entry and return the selected link.
  9567. If there is only one link, return it.
  9568. If NTH is an integer, return the NTH link found.
  9569. If ZERO is a string, check also this string for a link, and if
  9570. there is one, return it."
  9571. (with-current-buffer buffer
  9572. (save-excursion
  9573. (save-restriction
  9574. (widen)
  9575. (goto-char marker)
  9576. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9577. "\\(" org-angle-link-re "\\)\\|"
  9578. "\\(" org-plain-link-re "\\)"))
  9579. (cnt ?0)
  9580. (in-emacs (if (integerp nth) nil nth))
  9581. have-zero end links link c)
  9582. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9583. (push (match-string 0 zero) links)
  9584. (setq cnt (1- cnt) have-zero t))
  9585. (save-excursion
  9586. (org-back-to-heading t)
  9587. (setq end (save-excursion (outline-next-heading) (point)))
  9588. (while (re-search-forward re end t)
  9589. (push (match-string 0) links))
  9590. (setq links (org-uniquify (reverse links))))
  9591. (cond
  9592. ((null links)
  9593. (message "No links"))
  9594. ((equal (length links) 1)
  9595. (setq link (car links)))
  9596. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9597. (setq link (nth (if have-zero nth (1- nth)) links)))
  9598. (t ; we have to select a link
  9599. (save-excursion
  9600. (save-window-excursion
  9601. (delete-other-windows)
  9602. (with-output-to-temp-buffer "*Select Link*"
  9603. (mapc (lambda (l)
  9604. (if (not (string-match org-bracket-link-regexp l))
  9605. (princ (format "[%c] %s\n" (incf cnt)
  9606. (org-remove-angle-brackets l)))
  9607. (if (match-end 3)
  9608. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9609. (match-string 3 l) (match-string 1 l)))
  9610. (princ (format "[%c] %s\n" (incf cnt)
  9611. (match-string 1 l))))))
  9612. links))
  9613. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9614. (message "Select link to open, RET to open all:")
  9615. (setq c (read-char-exclusive))
  9616. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9617. (when (equal c ?q) (error "Abort"))
  9618. (if (equal c ?\C-m)
  9619. (setq link links)
  9620. (setq nth (- c ?0))
  9621. (if have-zero (setq nth (1+ nth)))
  9622. (unless (and (integerp nth) (>= (length links) nth))
  9623. (user-error "Invalid link selection"))
  9624. (setq link (nth (1- nth) links)))))
  9625. (cons link end))))))
  9626. ;; Add special file links that specify the way of opening
  9627. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9628. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9629. (defun org-open-file-with-system (path)
  9630. "Open file at PATH using the system way of opening it."
  9631. (org-open-file path 'system))
  9632. (defun org-open-file-with-emacs (path)
  9633. "Open file at PATH in Emacs."
  9634. (org-open-file path 'emacs))
  9635. ;;; File search
  9636. (defvar org-create-file-search-functions nil
  9637. "List of functions to construct the right search string for a file link.
  9638. These functions are called in turn with point at the location to
  9639. which the link should point.
  9640. A function in the hook should first test if it would like to
  9641. handle this file type, for example by checking the `major-mode'
  9642. or the file extension. If it decides not to handle this file, it
  9643. should just return nil to give other functions a chance. If it
  9644. does handle the file, it must return the search string to be used
  9645. when following the link. The search string will be part of the
  9646. file link, given after a double colon, and `org-open-at-point'
  9647. will automatically search for it. If special measures must be
  9648. taken to make the search successful, another function should be
  9649. added to the companion hook `org-execute-file-search-functions',
  9650. which see.
  9651. A function in this hook may also use `setq' to set the variable
  9652. `description' to provide a suggestion for the descriptive text to
  9653. be used for this link when it gets inserted into an Org-mode
  9654. buffer with \\[org-insert-link].")
  9655. (defvar org-execute-file-search-functions nil
  9656. "List of functions to execute a file search triggered by a link.
  9657. Functions added to this hook must accept a single argument, the
  9658. search string that was part of the file link, the part after the
  9659. double colon. The function must first check if it would like to
  9660. handle this search, for example by checking the `major-mode' or
  9661. the file extension. If it decides not to handle this search, it
  9662. should just return nil to give other functions a chance. If it
  9663. does handle the search, it must return a non-nil value to keep
  9664. other functions from trying.
  9665. Each function can access the current prefix argument through the
  9666. variable `current-prefix-arg'. Note that a single prefix is used
  9667. to force opening a link in Emacs, so it may be good to only use a
  9668. numeric or double prefix to guide the search function.
  9669. In case this is needed, a function in this hook can also restore
  9670. the window configuration before `org-open-at-point' was called using:
  9671. (set-window-configuration org-window-config-before-follow-link)")
  9672. (defun org-link-search (s &optional type avoid-pos stealth)
  9673. "Search for a link search option.
  9674. If S is surrounded by forward slashes, it is interpreted as a
  9675. regular expression. In org-mode files, this will create an `org-occur'
  9676. sparse tree. In ordinary files, `occur' will be used to list matches.
  9677. If the current buffer is in `dired-mode', grep will be used to search
  9678. in all files. If AVOID-POS is given, ignore matches near that position.
  9679. When optional argument STEALTH is non-nil, do not modify
  9680. visibility around point, thus ignoring
  9681. `org-show-hierarchy-above', `org-show-following-heading' and
  9682. `org-show-siblings' variables."
  9683. (let ((case-fold-search t)
  9684. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9685. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9686. (append '(("") (" ") ("\t") ("\n"))
  9687. org-emphasis-alist)
  9688. "\\|") "\\)"))
  9689. (pos (point))
  9690. (pre nil) (post nil)
  9691. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9692. (cond
  9693. ;; First check if there are any special search functions
  9694. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9695. ;; Now try the builtin stuff
  9696. ((and (equal (string-to-char s0) ?#)
  9697. (> (length s0) 1)
  9698. (save-excursion
  9699. (goto-char (point-min))
  9700. (and
  9701. (re-search-forward
  9702. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9703. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9704. (setq type 'dedicated
  9705. pos (match-beginning 0))))
  9706. ;; There is an exact target for this
  9707. (goto-char pos)
  9708. (org-back-to-heading t)))
  9709. ((save-excursion
  9710. (goto-char (point-min))
  9711. (and
  9712. (re-search-forward
  9713. (concat "<<" (regexp-quote s0) ">>") nil t)
  9714. (setq type 'dedicated
  9715. pos (match-beginning 0))))
  9716. ;; There is an exact target for this
  9717. (goto-char pos))
  9718. ((save-excursion
  9719. (goto-char (point-min))
  9720. (and
  9721. (re-search-forward
  9722. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9723. (setq type 'dedicated pos (match-beginning 0))))
  9724. ;; Found an element with a matching #+name affiliated keyword.
  9725. (goto-char pos))
  9726. ((and (string-match "^(\\(.*\\))$" s0)
  9727. (save-excursion
  9728. (goto-char (point-min))
  9729. (and
  9730. (re-search-forward
  9731. (concat "[^[]" (regexp-quote
  9732. (format org-coderef-label-format
  9733. (match-string 1 s0))))
  9734. nil t)
  9735. (setq type 'dedicated
  9736. pos (1+ (match-beginning 0))))))
  9737. ;; There is a coderef target for this
  9738. (goto-char pos))
  9739. ((string-match "^/\\(.*\\)/$" s)
  9740. ;; A regular expression
  9741. (cond
  9742. ((derived-mode-p 'org-mode)
  9743. (org-occur (match-string 1 s)))
  9744. (t (org-do-occur (match-string 1 s)))))
  9745. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9746. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9747. (goto-char (point-min))
  9748. (cond
  9749. ((let (case-fold-search)
  9750. (re-search-forward (format org-complex-heading-regexp-format
  9751. (regexp-quote s))
  9752. nil t))
  9753. ;; OK, found a match
  9754. (setq type 'dedicated)
  9755. (goto-char (match-beginning 0)))
  9756. ((and (not org-link-search-inhibit-query)
  9757. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9758. (y-or-n-p "No match - create this as a new heading? "))
  9759. (goto-char (point-max))
  9760. (or (bolp) (newline))
  9761. (insert "* " s "\n")
  9762. (beginning-of-line 0))
  9763. (t
  9764. (goto-char pos)
  9765. (error "No match"))))
  9766. (t
  9767. ;; A normal search string
  9768. (when (equal (string-to-char s) ?*)
  9769. ;; Anchor on headlines, post may include tags.
  9770. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9771. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9772. s (substring s 1)))
  9773. (remove-text-properties
  9774. 0 (length s)
  9775. '(face nil mouse-face nil keymap nil fontified nil) s)
  9776. ;; Make a series of regular expressions to find a match
  9777. (setq words (org-split-string s "[ \n\r\t]+")
  9778. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9779. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9780. "\\)" markers)
  9781. re2a_ (concat "\\(" (mapconcat 'downcase words
  9782. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9783. re2a (concat "[ \t\r\n]" re2a_)
  9784. re4_ (concat "\\(" (mapconcat 'downcase words
  9785. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9786. re4 (concat "[^a-zA-Z_]" re4_)
  9787. re1 (concat pre re2 post)
  9788. re3 (concat pre (if pre re4_ re4) post)
  9789. re5 (concat pre ".*" re4)
  9790. re2 (concat pre re2)
  9791. re2a (concat pre (if pre re2a_ re2a))
  9792. re4 (concat pre (if pre re4_ re4))
  9793. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9794. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9795. re5 "\\)"))
  9796. (cond
  9797. ((eq type 'org-occur) (org-occur reall))
  9798. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9799. (t (goto-char (point-min))
  9800. (setq type 'fuzzy)
  9801. (if (or (and (org-search-not-self 1 re0 nil t)
  9802. (setq type 'dedicated))
  9803. (org-search-not-self 1 re1 nil t)
  9804. (org-search-not-self 1 re2 nil t)
  9805. (org-search-not-self 1 re2a nil t)
  9806. (org-search-not-self 1 re3 nil t)
  9807. (org-search-not-self 1 re4 nil t)
  9808. (org-search-not-self 1 re5 nil t))
  9809. (goto-char (match-beginning 1))
  9810. (goto-char pos)
  9811. (error "No match"))))))
  9812. (and (derived-mode-p 'org-mode)
  9813. (not stealth)
  9814. (org-show-context 'link-search))
  9815. type))
  9816. (defun org-search-not-self (group &rest args)
  9817. "Execute `re-search-forward', but only accept matches that do not
  9818. enclose the position of `org-open-link-marker'."
  9819. (let ((m org-open-link-marker))
  9820. (catch 'exit
  9821. (while (apply 're-search-forward args)
  9822. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9823. (goto-char (match-end group))
  9824. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9825. (> (match-beginning 0) (marker-position m))
  9826. (< (match-end 0) (marker-position m)))
  9827. (save-match-data
  9828. (or (not (org-in-regexp
  9829. org-bracket-link-analytic-regexp 1))
  9830. (not (match-end 4)) ; no description
  9831. (and (<= (match-beginning 4) (point))
  9832. (>= (match-end 4) (point))))))
  9833. (throw 'exit (point))))))))
  9834. (defun org-get-buffer-for-internal-link (buffer)
  9835. "Return a buffer to be used for displaying the link target of internal links."
  9836. (cond
  9837. ((not org-display-internal-link-with-indirect-buffer)
  9838. buffer)
  9839. ((string-match "(Clone)$" (buffer-name buffer))
  9840. (message "Buffer is already a clone, not making another one")
  9841. ;; we also do not modify visibility in this case
  9842. buffer)
  9843. (t ; make a new indirect buffer for displaying the link
  9844. (let* ((bn (buffer-name buffer))
  9845. (ibn (concat bn "(Clone)"))
  9846. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9847. (with-current-buffer ib (org-overview))
  9848. ib))))
  9849. (defun org-do-occur (regexp &optional cleanup)
  9850. "Call the Emacs command `occur'.
  9851. If CLEANUP is non-nil, remove the printout of the regular expression
  9852. in the *Occur* buffer. This is useful if the regex is long and not useful
  9853. to read."
  9854. (occur regexp)
  9855. (when cleanup
  9856. (let ((cwin (selected-window)) win beg end)
  9857. (when (setq win (get-buffer-window "*Occur*"))
  9858. (select-window win))
  9859. (goto-char (point-min))
  9860. (when (re-search-forward "match[a-z]+" nil t)
  9861. (setq beg (match-end 0))
  9862. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9863. (setq end (1- (match-beginning 0)))))
  9864. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9865. (goto-char (point-min))
  9866. (select-window cwin))))
  9867. ;;; The mark ring for links jumps
  9868. (defvar org-mark-ring nil
  9869. "Mark ring for positions before jumps in Org-mode.")
  9870. (defvar org-mark-ring-last-goto nil
  9871. "Last position in the mark ring used to go back.")
  9872. ;; Fill and close the ring
  9873. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9874. (loop for i from 1 to org-mark-ring-length do
  9875. (push (make-marker) org-mark-ring))
  9876. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9877. org-mark-ring)
  9878. (defun org-mark-ring-push (&optional pos buffer)
  9879. "Put the current position or POS into the mark ring and rotate it."
  9880. (interactive)
  9881. (setq pos (or pos (point)))
  9882. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9883. (move-marker (car org-mark-ring)
  9884. (or pos (point))
  9885. (or buffer (current-buffer)))
  9886. (message "%s"
  9887. (substitute-command-keys
  9888. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9889. (defun org-mark-ring-goto (&optional n)
  9890. "Jump to the previous position in the mark ring.
  9891. With prefix arg N, jump back that many stored positions. When
  9892. called several times in succession, walk through the entire ring.
  9893. Org-mode commands jumping to a different position in the current file,
  9894. or to another Org-mode file, automatically push the old position
  9895. onto the ring."
  9896. (interactive "p")
  9897. (let (p m)
  9898. (if (eq last-command this-command)
  9899. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9900. (setq p org-mark-ring))
  9901. (setq org-mark-ring-last-goto p)
  9902. (setq m (car p))
  9903. (org-pop-to-buffer-same-window (marker-buffer m))
  9904. (goto-char m)
  9905. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9906. (defun org-remove-angle-brackets (s)
  9907. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9908. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9909. s)
  9910. (defun org-add-angle-brackets (s)
  9911. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9912. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9913. s)
  9914. (defun org-remove-double-quotes (s)
  9915. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9916. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9917. s)
  9918. ;;; Following specific links
  9919. (defun org-follow-timestamp-link ()
  9920. "Open an agenda view for the time-stamp date/range at point."
  9921. (cond
  9922. ((org-at-date-range-p t)
  9923. (let ((org-agenda-start-on-weekday)
  9924. (t1 (match-string 1))
  9925. (t2 (match-string 2)) tt1 tt2)
  9926. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9927. tt2 (time-to-days (org-time-string-to-time t2)))
  9928. (let ((org-agenda-buffer-tmp-name
  9929. (format "*Org Agenda(a:%s)"
  9930. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9931. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9932. ((org-at-timestamp-p t)
  9933. (let ((org-agenda-buffer-tmp-name
  9934. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9935. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9936. (substring (match-string 1) 0 10)))
  9937. 1)))
  9938. (t (error "This should not happen"))))
  9939. ;;; Following file links
  9940. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9941. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9942. (declare-function mailcap-mime-info
  9943. "mailcap" (string &optional request no-decode))
  9944. (defvar org-wait nil)
  9945. (defun org-open-file (path &optional in-emacs line search)
  9946. "Open the file at PATH.
  9947. First, this expands any special file name abbreviations. Then the
  9948. configuration variable `org-file-apps' is checked if it contains an
  9949. entry for this file type, and if yes, the corresponding command is launched.
  9950. If no application is found, Emacs simply visits the file.
  9951. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9952. With a double \\[universal-argument] \\[universal-argument] \
  9953. prefix arg, Org tries to avoid opening in Emacs
  9954. and to use an external application to visit the file.
  9955. Optional LINE specifies a line to go to, optional SEARCH a string
  9956. to search for. If LINE or SEARCH is given, the file will be
  9957. opened in Emacs, unless an entry from org-file-apps that makes
  9958. use of groups in a regexp matches.
  9959. If you want to change the way frames are used when following a
  9960. link, please customize `org-link-frame-setup'.
  9961. If the file does not exist, an error is thrown."
  9962. (let* ((file (if (equal path "")
  9963. buffer-file-name
  9964. (substitute-in-file-name (expand-file-name path))))
  9965. (file-apps (append org-file-apps (org-default-apps)))
  9966. (apps (org-remove-if
  9967. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9968. (apps-dlink (org-remove-if-not
  9969. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9970. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9971. (dirp (if remp nil (file-directory-p file)))
  9972. (file (if (and dirp org-open-directory-means-index-dot-org)
  9973. (concat (file-name-as-directory file) "index.org")
  9974. file))
  9975. (a-m-a-p (assq 'auto-mode apps))
  9976. (dfile (downcase file))
  9977. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9978. (link (cond ((and (eq line nil)
  9979. (eq search nil))
  9980. file)
  9981. (line
  9982. (concat file "::" (number-to-string line)))
  9983. (search
  9984. (concat file "::" search))))
  9985. (dlink (downcase link))
  9986. (old-buffer (current-buffer))
  9987. (old-pos (point))
  9988. (old-mode major-mode)
  9989. ext cmd link-match-data)
  9990. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9991. (setq ext (match-string 1 dfile))
  9992. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9993. (setq ext (match-string 1 dfile))))
  9994. (cond
  9995. ((member in-emacs '((16) system))
  9996. (setq cmd (cdr (assoc 'system apps))))
  9997. (in-emacs (setq cmd 'emacs))
  9998. (t
  9999. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10000. (and dirp (cdr (assoc 'directory apps)))
  10001. ; first, try matching against apps-dlink
  10002. ; if we get a match here, store the match data for later
  10003. (let ((match (assoc-default dlink apps-dlink
  10004. 'string-match)))
  10005. (if match
  10006. (progn (setq link-match-data (match-data))
  10007. match)
  10008. (progn (setq in-emacs (or in-emacs line search))
  10009. nil))) ; if we have no match in apps-dlink,
  10010. ; always open the file in emacs if line or search
  10011. ; is given (for backwards compatibility)
  10012. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10013. 'string-match)
  10014. (cdr (assoc ext apps))
  10015. (cdr (assoc t apps))))))
  10016. (when (eq cmd 'system)
  10017. (setq cmd (cdr (assoc 'system apps))))
  10018. (when (eq cmd 'default)
  10019. (setq cmd (cdr (assoc t apps))))
  10020. (when (eq cmd 'mailcap)
  10021. (require 'mailcap)
  10022. (mailcap-parse-mailcaps)
  10023. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10024. (command (mailcap-mime-info mime-type)))
  10025. (if (stringp command)
  10026. (setq cmd command)
  10027. (setq cmd 'emacs))))
  10028. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10029. (not (file-exists-p file))
  10030. (not org-open-non-existing-files))
  10031. (user-error "No such file: %s" file))
  10032. (cond
  10033. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10034. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10035. (while (string-match "['\"]%s['\"]" cmd)
  10036. (setq cmd (replace-match "%s" t t cmd)))
  10037. (while (string-match "%s" cmd)
  10038. (setq cmd (replace-match
  10039. (save-match-data
  10040. (shell-quote-argument
  10041. (convert-standard-filename file)))
  10042. t t cmd)))
  10043. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10044. (save-match-data
  10045. (let ((match-index 1)
  10046. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10047. (set-match-data link-match-data)
  10048. (while (<= match-index number-of-groups)
  10049. (let ((regex (concat "%" (number-to-string match-index)))
  10050. (replace-with (match-string match-index dlink)))
  10051. (while (string-match regex cmd)
  10052. (setq cmd (replace-match replace-with t t cmd))))
  10053. (setq match-index (+ match-index 1)))))
  10054. (save-window-excursion
  10055. (message "Running %s...done" cmd)
  10056. (start-process-shell-command cmd nil cmd)
  10057. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10058. ((or (stringp cmd)
  10059. (eq cmd 'emacs))
  10060. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10061. (widen)
  10062. (if line (org-goto-line line)
  10063. (if search (org-link-search search))))
  10064. ((consp cmd)
  10065. (let ((file (convert-standard-filename file)))
  10066. (save-match-data
  10067. (set-match-data link-match-data)
  10068. (eval cmd))))
  10069. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10070. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10071. (or (not (equal old-buffer (current-buffer)))
  10072. (not (equal old-pos (point))))
  10073. (org-mark-ring-push old-pos old-buffer))))
  10074. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10075. "This function returns non-nil if `entry' uses a regular
  10076. expression which should be matched against the whole link by
  10077. org-open-file.
  10078. It assumes that is the case when the entry uses a regular
  10079. expression which has at least one grouping construct and the
  10080. action is either a lisp form or a command string containing
  10081. '%1', i.e. using at least one subexpression match as a
  10082. parameter."
  10083. (let ((selector (car entry))
  10084. (action (cdr entry)))
  10085. (if (stringp selector)
  10086. (and (> (regexp-opt-depth selector) 0)
  10087. (or (and (stringp action)
  10088. (string-match "%[0-9]" action))
  10089. (consp action)))
  10090. nil)))
  10091. (defun org-default-apps ()
  10092. "Return the default applications for this operating system."
  10093. (cond
  10094. ((eq system-type 'darwin)
  10095. org-file-apps-defaults-macosx)
  10096. ((eq system-type 'windows-nt)
  10097. org-file-apps-defaults-windowsnt)
  10098. (t org-file-apps-defaults-gnu)))
  10099. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10100. "Convert extensions to regular expressions in the cars of LIST.
  10101. Also, weed out any non-string entries, because the return value is used
  10102. only for regexp matching.
  10103. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10104. point to the symbol `emacs', indicating that the file should
  10105. be opened in Emacs."
  10106. (append
  10107. (delq nil
  10108. (mapcar (lambda (x)
  10109. (if (not (stringp (car x)))
  10110. nil
  10111. (if (string-match "\\W" (car x))
  10112. x
  10113. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10114. list))
  10115. (if add-auto-mode
  10116. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10117. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10118. (defun org-file-remote-p (file)
  10119. "Test whether FILE specifies a location on a remote system.
  10120. Return non-nil if the location is indeed remote.
  10121. For example, the filename \"/user@host:/foo\" specifies a location
  10122. on the system \"/user@host:\"."
  10123. (cond ((fboundp 'file-remote-p)
  10124. (file-remote-p file))
  10125. ((fboundp 'tramp-handle-file-remote-p)
  10126. (tramp-handle-file-remote-p file))
  10127. ((and (boundp 'ange-ftp-name-format)
  10128. (string-match (car ange-ftp-name-format) file))
  10129. t)))
  10130. ;;;; Refiling
  10131. (defun org-get-org-file ()
  10132. "Read a filename, with default directory `org-directory'."
  10133. (let ((default (or org-default-notes-file remember-data-file)))
  10134. (read-file-name (format "File name [%s]: " default)
  10135. (file-name-as-directory org-directory)
  10136. default)))
  10137. (defun org-notes-order-reversed-p ()
  10138. "Check if the current file should receive notes in reversed order."
  10139. (cond
  10140. ((not org-reverse-note-order) nil)
  10141. ((eq t org-reverse-note-order) t)
  10142. ((not (listp org-reverse-note-order)) nil)
  10143. (t (catch 'exit
  10144. (let ((all org-reverse-note-order)
  10145. entry)
  10146. (while (setq entry (pop all))
  10147. (if (string-match (car entry) buffer-file-name)
  10148. (throw 'exit (cdr entry))))
  10149. nil)))))
  10150. (defvar org-refile-target-table nil
  10151. "The list of refile targets, created by `org-refile'.")
  10152. (defvar org-agenda-new-buffers nil
  10153. "Buffers created to visit agenda files.")
  10154. (defvar org-refile-cache nil
  10155. "Cache for refile targets.")
  10156. (defvar org-refile-markers nil
  10157. "All the markers used for caching refile locations.")
  10158. (defun org-refile-marker (pos)
  10159. "Get a new refile marker, but only if caching is in use."
  10160. (if (not org-refile-use-cache)
  10161. pos
  10162. (let ((m (make-marker)))
  10163. (move-marker m pos)
  10164. (push m org-refile-markers)
  10165. m)))
  10166. (defun org-refile-cache-clear ()
  10167. "Clear the refile cache and disable all the markers."
  10168. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10169. (setq org-refile-markers nil)
  10170. (setq org-refile-cache nil)
  10171. (message "Refile cache has been cleared"))
  10172. (defun org-refile-cache-check-set (set)
  10173. "Check if all the markers in the cache still have live buffers."
  10174. (let (marker)
  10175. (catch 'exit
  10176. (while (and set (setq marker (nth 3 (pop set))))
  10177. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10178. (when (and marker (null (marker-buffer marker)))
  10179. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10180. (sit-for 3)
  10181. (throw 'exit nil)))
  10182. t)))
  10183. (defun org-refile-cache-put (set &rest identifiers)
  10184. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10185. (let* ((key (sha1 (prin1-to-string identifiers)))
  10186. (entry (assoc key org-refile-cache)))
  10187. (if entry
  10188. (setcdr entry set)
  10189. (push (cons key set) org-refile-cache))))
  10190. (defun org-refile-cache-get (&rest identifiers)
  10191. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10192. (cond
  10193. ((not org-refile-cache) nil)
  10194. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10195. (t
  10196. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10197. org-refile-cache))))
  10198. (and set (org-refile-cache-check-set set) set)))))
  10199. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10200. "Produce a table with refile targets."
  10201. (let ((case-fold-search nil)
  10202. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10203. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10204. targets tgs txt re files f desc descre fast-path-p level pos0)
  10205. (message "Getting targets...")
  10206. (with-current-buffer (or default-buffer (current-buffer))
  10207. (while (setq entry (pop entries))
  10208. (setq files (car entry) desc (cdr entry))
  10209. (setq fast-path-p nil)
  10210. (cond
  10211. ((null files) (setq files (list (current-buffer))))
  10212. ((eq files 'org-agenda-files)
  10213. (setq files (org-agenda-files 'unrestricted)))
  10214. ((and (symbolp files) (fboundp files))
  10215. (setq files (funcall files)))
  10216. ((and (symbolp files) (boundp files))
  10217. (setq files (symbol-value files))))
  10218. (if (stringp files) (setq files (list files)))
  10219. (cond
  10220. ((eq (car desc) :tag)
  10221. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10222. ((eq (car desc) :todo)
  10223. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10224. ((eq (car desc) :regexp)
  10225. (setq descre (cdr desc)))
  10226. ((eq (car desc) :level)
  10227. (setq descre (concat "^\\*\\{" (number-to-string
  10228. (if org-odd-levels-only
  10229. (1- (* 2 (cdr desc)))
  10230. (cdr desc)))
  10231. "\\}[ \t]")))
  10232. ((eq (car desc) :maxlevel)
  10233. (setq fast-path-p t)
  10234. (setq descre (concat "^\\*\\{1," (number-to-string
  10235. (if org-odd-levels-only
  10236. (1- (* 2 (cdr desc)))
  10237. (cdr desc)))
  10238. "\\}[ \t]")))
  10239. (t (error "Bad refiling target description %s" desc)))
  10240. (while (setq f (pop files))
  10241. (with-current-buffer
  10242. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10243. (or
  10244. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10245. (progn
  10246. (if (bufferp f) (setq f (buffer-file-name
  10247. (buffer-base-buffer f))))
  10248. (setq f (and f (expand-file-name f)))
  10249. (if (eq org-refile-use-outline-path 'file)
  10250. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10251. (save-excursion
  10252. (save-restriction
  10253. (widen)
  10254. (goto-char (point-min))
  10255. (while (re-search-forward descre nil t)
  10256. (goto-char (setq pos0 (point-at-bol)))
  10257. (catch 'next
  10258. (when org-refile-target-verify-function
  10259. (save-match-data
  10260. (or (funcall org-refile-target-verify-function)
  10261. (throw 'next t))))
  10262. (when (and (looking-at org-complex-heading-regexp)
  10263. (not (member (match-string 4) excluded-entries))
  10264. (match-string 4))
  10265. (setq level (org-reduced-level
  10266. (- (match-end 1) (match-beginning 1)))
  10267. txt (org-link-display-format (match-string 4))
  10268. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10269. re (format org-complex-heading-regexp-format
  10270. (regexp-quote (match-string 4))))
  10271. (when org-refile-use-outline-path
  10272. (setq txt (mapconcat
  10273. 'org-protect-slash
  10274. (append
  10275. (if (eq org-refile-use-outline-path
  10276. 'file)
  10277. (list (file-name-nondirectory
  10278. (buffer-file-name
  10279. (buffer-base-buffer))))
  10280. (if (eq org-refile-use-outline-path
  10281. 'full-file-path)
  10282. (list (buffer-file-name
  10283. (buffer-base-buffer)))))
  10284. (org-get-outline-path fast-path-p
  10285. level txt)
  10286. (list txt))
  10287. "/")))
  10288. (push (list txt f re (org-refile-marker (point)))
  10289. tgs)))
  10290. (when (= (point) pos0)
  10291. ;; verification function has not moved point
  10292. (goto-char (point-at-eol))))))))
  10293. (when org-refile-use-cache
  10294. (org-refile-cache-put tgs (buffer-file-name) descre))
  10295. (setq targets (append tgs targets))))))
  10296. (message "Getting targets...done")
  10297. (nreverse targets)))
  10298. (defun org-protect-slash (s)
  10299. (while (string-match "/" s)
  10300. (setq s (replace-match "\\" t t s)))
  10301. s)
  10302. (defvar org-olpa (make-vector 20 nil))
  10303. (defun org-get-outline-path (&optional fastp level heading)
  10304. "Return the outline path to the current entry, as a list.
  10305. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10306. routine which makes outline path derivations for an entire file,
  10307. avoiding backtracing. Refile target collection makes use of that."
  10308. (if fastp
  10309. (progn
  10310. (if (> level 19)
  10311. (error "Outline path failure, more than 19 levels"))
  10312. (loop for i from level upto 19 do
  10313. (aset org-olpa i nil))
  10314. (prog1
  10315. (delq nil (append org-olpa nil))
  10316. (aset org-olpa level heading)))
  10317. (let (rtn case-fold-search)
  10318. (save-excursion
  10319. (save-restriction
  10320. (widen)
  10321. (while (org-up-heading-safe)
  10322. (when (looking-at org-complex-heading-regexp)
  10323. (push (org-trim
  10324. (replace-regexp-in-string
  10325. ;; Remove statistical/checkboxes cookies
  10326. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10327. (org-match-string-no-properties 4)))
  10328. rtn)))
  10329. rtn)))))
  10330. (defun org-format-outline-path (path &optional width prefix separator)
  10331. "Format the outline path PATH for display.
  10332. WIDTH is the maximum number of characters that is available.
  10333. PREFIX is a prefix to be included in the returned string,
  10334. such as the file name.
  10335. SEPARATOR is inserted between the different parts of the path,
  10336. the default is \"/\"."
  10337. (setq width (or width 79))
  10338. (if prefix (setq width (- width (length prefix))))
  10339. (if (not path)
  10340. (or prefix "")
  10341. (let* ((nsteps (length path))
  10342. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10343. (maxwidth (if (<= total-width width)
  10344. 10000 ;; everything fits
  10345. ;; we need to shorten the level headings
  10346. (/ (- width nsteps) nsteps)))
  10347. (org-odd-levels-only nil)
  10348. (n 0)
  10349. (total (1+ (length prefix))))
  10350. (setq maxwidth (max maxwidth 10))
  10351. (concat prefix
  10352. (if prefix (or separator "/"))
  10353. (mapconcat
  10354. (lambda (h)
  10355. (setq n (1+ n))
  10356. (if (and (= n nsteps) (< maxwidth 10000))
  10357. (setq maxwidth (- total-width total)))
  10358. (if (< (length h) maxwidth)
  10359. (progn (setq total (+ total (length h) 1)) h)
  10360. (setq h (substring h 0 (- maxwidth 2))
  10361. total (+ total maxwidth 1))
  10362. (if (string-match "[ \t]+\\'" h)
  10363. (setq h (substring h 0 (match-beginning 0))))
  10364. (setq h (concat h "..")))
  10365. (org-add-props h nil 'face
  10366. (nth (% (1- n) org-n-level-faces)
  10367. org-level-faces))
  10368. h)
  10369. path (or separator "/"))))))
  10370. (defun org-display-outline-path (&optional file current separator just-return-string)
  10371. "Display the current outline path in the echo area.
  10372. If FILE is non-nil, prepend the output with the file name.
  10373. If CURRENT is non-nil, append the current heading to the output.
  10374. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10375. the different parts of the path and defaults to \"/\".
  10376. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10377. (interactive "P")
  10378. (let* (case-fold-search
  10379. (bfn (buffer-file-name (buffer-base-buffer)))
  10380. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10381. res)
  10382. (if current (setq path (append path
  10383. (save-excursion
  10384. (org-back-to-heading t)
  10385. (if (looking-at org-complex-heading-regexp)
  10386. (list (match-string 4)))))))
  10387. (setq res
  10388. (org-format-outline-path
  10389. path
  10390. (1- (frame-width))
  10391. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10392. separator))
  10393. (if just-return-string
  10394. (org-no-properties res)
  10395. (org-unlogged-message "%s" res))))
  10396. (defvar org-refile-history nil
  10397. "History for refiling operations.")
  10398. (defvar org-after-refile-insert-hook nil
  10399. "Hook run after `org-refile' has inserted its stuff at the new location.
  10400. Note that this is still *before* the stuff will be removed from
  10401. the *old* location.")
  10402. (defvar org-capture-last-stored-marker)
  10403. (defvar org-refile-keep nil
  10404. "Non-nil means `org-refile' will copy instead of refile.")
  10405. (defun org-copy ()
  10406. "Like `org-refile', but copy."
  10407. (interactive)
  10408. (let ((org-refile-keep t))
  10409. (funcall 'org-refile nil nil nil "Copy")))
  10410. (defun org-refile (&optional goto default-buffer rfloc msg)
  10411. "Move the entry or entries at point to another heading.
  10412. The list of target headings is compiled using the information in
  10413. `org-refile-targets', which see.
  10414. At the target location, the entry is filed as a subitem of the target
  10415. heading. Depending on `org-reverse-note-order', the new subitem will
  10416. either be the first or the last subitem.
  10417. If there is an active region, all entries in that region will be moved.
  10418. However, the region must fulfill the requirement that the first heading
  10419. is the first one sets the top-level of the moved text - at most siblings
  10420. below it are allowed.
  10421. With prefix arg GOTO, the command will only visit the target location
  10422. and not actually move anything.
  10423. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10424. go to the location where the last refiling operation has put the subtree.
  10425. With a numeric prefix argument of `2', refile to the running clock.
  10426. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10427. being set to `t' and copy to the target location, don't move it.
  10428. Beware that keeping refiled entries may result in duplicated ID
  10429. properties.
  10430. RFLOC can be a refile location obtained in a different way.
  10431. MSG is a string to replace \"Refile\" in the default prompt with
  10432. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10433. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10434. If you are using target caching (see `org-refile-use-cache'),
  10435. you have to clear the target cache in order to find new targets.
  10436. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10437. prefix argument (`C-u C-u C-u C-c C-w')."
  10438. (interactive "P")
  10439. (if (member goto '(0 (64)))
  10440. (org-refile-cache-clear)
  10441. (let* ((actionmsg (cond (msg msg)
  10442. ((equal goto 3) "Refile (and keep)")
  10443. (t "Refile")))
  10444. (cbuf (current-buffer))
  10445. (regionp (org-region-active-p))
  10446. (region-start (and regionp (region-beginning)))
  10447. (region-end (and regionp (region-end)))
  10448. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10449. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10450. pos it nbuf file re level reversed)
  10451. (setq last-command nil)
  10452. (when regionp
  10453. (goto-char region-start)
  10454. (or (bolp) (goto-char (point-at-bol)))
  10455. (setq region-start (point))
  10456. (unless (or (org-kill-is-subtree-p
  10457. (buffer-substring region-start region-end))
  10458. (prog1 org-refile-active-region-within-subtree
  10459. (let ((s (point-at-eol)))
  10460. (org-toggle-heading)
  10461. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10462. (user-error "The region is not a (sequence of) subtree(s)")))
  10463. (if (equal goto '(16))
  10464. (org-refile-goto-last-stored)
  10465. (when (or
  10466. (and (equal goto 2)
  10467. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10468. (prog1
  10469. (setq it (list (or org-clock-heading "running clock")
  10470. (buffer-file-name
  10471. (marker-buffer org-clock-hd-marker))
  10472. ""
  10473. (marker-position org-clock-hd-marker)))
  10474. (setq goto nil)))
  10475. (setq it (or rfloc
  10476. (let (heading-text)
  10477. (save-excursion
  10478. (unless (and goto (listp goto))
  10479. (org-back-to-heading t)
  10480. (setq heading-text
  10481. (nth 4 (org-heading-components))))
  10482. (org-refile-get-location
  10483. (cond ((and goto (listp goto)) "Goto")
  10484. (regionp (concat actionmsg " region to"))
  10485. (t (concat actionmsg " subtree \""
  10486. heading-text "\" to")))
  10487. default-buffer
  10488. (and (not (equal '(4) goto))
  10489. org-refile-allow-creating-parent-nodes)
  10490. goto))))))
  10491. (setq file (nth 1 it)
  10492. re (nth 2 it)
  10493. pos (nth 3 it))
  10494. (if (and (not goto)
  10495. pos
  10496. (equal (buffer-file-name) file)
  10497. (if regionp
  10498. (and (>= pos region-start)
  10499. (<= pos region-end))
  10500. (and (>= pos (point))
  10501. (< pos (save-excursion
  10502. (org-end-of-subtree t t))))))
  10503. (error "Cannot refile to position inside the tree or region"))
  10504. (setq nbuf (or (find-buffer-visiting file)
  10505. (find-file-noselect file)))
  10506. (if (and goto (not (equal goto 3)))
  10507. (progn
  10508. (org-pop-to-buffer-same-window nbuf)
  10509. (goto-char pos)
  10510. (org-show-context 'org-goto))
  10511. (if regionp
  10512. (progn
  10513. (org-kill-new (buffer-substring region-start region-end))
  10514. (org-save-markers-in-region region-start region-end))
  10515. (org-copy-subtree 1 nil t))
  10516. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10517. (find-file-noselect file)))
  10518. (setq reversed (org-notes-order-reversed-p))
  10519. (save-excursion
  10520. (save-restriction
  10521. (widen)
  10522. (if pos
  10523. (progn
  10524. (goto-char pos)
  10525. (looking-at org-outline-regexp)
  10526. (setq level (org-get-valid-level (funcall outline-level) 1))
  10527. (goto-char
  10528. (if reversed
  10529. (or (outline-next-heading) (point-max))
  10530. (or (save-excursion (org-get-next-sibling))
  10531. (org-end-of-subtree t t)
  10532. (point-max)))))
  10533. (setq level 1)
  10534. (if (not reversed)
  10535. (goto-char (point-max))
  10536. (goto-char (point-min))
  10537. (or (outline-next-heading) (goto-char (point-max)))))
  10538. (if (not (bolp)) (newline))
  10539. (org-paste-subtree level)
  10540. (when org-log-refile
  10541. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10542. (unless (eq org-log-refile 'note)
  10543. (save-excursion (org-add-log-note))))
  10544. (and org-auto-align-tags
  10545. (let ((org-loop-over-headlines-in-active-region nil))
  10546. (org-set-tags nil t)))
  10547. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10548. :last-refile)))
  10549. (when bookmark-name
  10550. (with-demoted-errors
  10551. (bookmark-set bookmark-name))))
  10552. ;; If we are refiling for capture, make sure that the
  10553. ;; last-capture pointers point here
  10554. (when (org-bound-and-true-p org-refile-for-capture)
  10555. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10556. :last-capture-marker)))
  10557. (when bookmark-name
  10558. (with-demoted-errors
  10559. (bookmark-set bookmark-name))))
  10560. (move-marker org-capture-last-stored-marker (point)))
  10561. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10562. (run-hooks 'org-after-refile-insert-hook))))
  10563. (unless org-refile-keep
  10564. (if regionp
  10565. (delete-region (point) (+ (point) (- region-end region-start)))
  10566. (delete-region
  10567. (and (org-back-to-heading t) (point))
  10568. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10569. (when (featurep 'org-inlinetask)
  10570. (org-inlinetask-remove-END-maybe))
  10571. (setq org-markers-to-move nil)
  10572. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10573. (defun org-refile-goto-last-stored ()
  10574. "Go to the location where the last refile was stored."
  10575. (interactive)
  10576. (bookmark-jump "org-refile-last-stored")
  10577. (message "This is the location of the last refile"))
  10578. (defun org-refile--get-location (refloc tbl)
  10579. "When user refile to REFLOC, find the associated target in TBL.
  10580. Also check `org-refile-target-table'."
  10581. (car (delq
  10582. nil
  10583. (mapcar
  10584. (lambda (r) (or (assoc r tbl)
  10585. (assoc r org-refile-target-table)))
  10586. (list (replace-regexp-in-string "/$" "" refloc)
  10587. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10588. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10589. no-exclude)
  10590. "Prompt the user for a refile location, using PROMPT.
  10591. PROMPT should not be suffixed with a colon and a space, because
  10592. this function appends the default value from
  10593. `org-refile-history' automatically, if that is not empty.
  10594. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10595. this is used for the GOTO interface."
  10596. (let ((org-refile-targets org-refile-targets)
  10597. (org-refile-use-outline-path org-refile-use-outline-path)
  10598. excluded-entries)
  10599. (when (and (derived-mode-p 'org-mode)
  10600. (not org-refile-use-cache)
  10601. (not no-exclude))
  10602. (org-map-tree
  10603. (lambda()
  10604. (setq excluded-entries
  10605. (append excluded-entries (list (org-get-heading t t)))))))
  10606. (setq org-refile-target-table
  10607. (org-refile-get-targets default-buffer excluded-entries)))
  10608. (unless org-refile-target-table
  10609. (user-error "No refile targets"))
  10610. (let* ((cbuf (current-buffer))
  10611. (partial-completion-mode nil)
  10612. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10613. (cfunc (if (and org-refile-use-outline-path
  10614. org-outline-path-complete-in-steps)
  10615. 'org-olpath-completing-read
  10616. 'org-icompleting-read))
  10617. (extra (if org-refile-use-outline-path "/" ""))
  10618. (cbnex (concat (buffer-name) extra))
  10619. (filename (and cfn (expand-file-name cfn)))
  10620. (tbl (mapcar
  10621. (lambda (x)
  10622. (if (and (not (member org-refile-use-outline-path
  10623. '(file full-file-path)))
  10624. (not (equal filename (nth 1 x))))
  10625. (cons (concat (car x) extra " ("
  10626. (file-name-nondirectory (nth 1 x)) ")")
  10627. (cdr x))
  10628. (cons (concat (car x) extra) (cdr x))))
  10629. org-refile-target-table))
  10630. (completion-ignore-case t)
  10631. cdef
  10632. (prompt (concat prompt
  10633. (or (and (car org-refile-history)
  10634. (concat " (default " (car org-refile-history) ")"))
  10635. (and (assoc cbnex tbl) (setq cdef cbnex)
  10636. (concat " (default " cbnex ")"))) ": "))
  10637. pa answ parent-target child parent old-hist)
  10638. (setq old-hist org-refile-history)
  10639. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10640. nil 'org-refile-history (or cdef (car org-refile-history))))
  10641. (if (setq pa (org-refile--get-location answ tbl))
  10642. (progn
  10643. (org-refile-check-position pa)
  10644. (when (or (not org-refile-history)
  10645. (not (eq old-hist org-refile-history))
  10646. (not (equal (car pa) (car org-refile-history))))
  10647. (setq org-refile-history
  10648. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10649. org-refile-history
  10650. (cdr org-refile-history))))
  10651. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10652. (pop org-refile-history)))
  10653. pa)
  10654. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10655. (progn
  10656. (setq parent (match-string 1 answ)
  10657. child (match-string 2 answ))
  10658. (setq parent-target (org-refile--get-location parent tbl))
  10659. (when (and parent-target
  10660. (or (eq new-nodes t)
  10661. (and (eq new-nodes 'confirm)
  10662. (y-or-n-p (format "Create new node \"%s\"? "
  10663. child)))))
  10664. (org-refile-new-child parent-target child)))
  10665. (user-error "Invalid target location")))))
  10666. (declare-function org-string-nw-p "org-macs" (s))
  10667. (defun org-refile-check-position (refile-pointer)
  10668. "Check if the refile pointer matches the headline to which it points."
  10669. (let* ((file (nth 1 refile-pointer))
  10670. (re (nth 2 refile-pointer))
  10671. (pos (nth 3 refile-pointer))
  10672. buffer)
  10673. (if (and (not (markerp pos)) (not file))
  10674. (user-error "Please save the buffer to a file before refiling")
  10675. (when (org-string-nw-p re)
  10676. (setq buffer (if (markerp pos)
  10677. (marker-buffer pos)
  10678. (or (find-buffer-visiting file)
  10679. (find-file-noselect file))))
  10680. (with-current-buffer buffer
  10681. (save-excursion
  10682. (save-restriction
  10683. (widen)
  10684. (goto-char pos)
  10685. (beginning-of-line 1)
  10686. (unless (org-looking-at-p re)
  10687. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10688. (defun org-refile-new-child (parent-target child)
  10689. "Use refile target PARENT-TARGET to add new CHILD below it."
  10690. (unless parent-target
  10691. (error "Cannot find parent for new node"))
  10692. (let ((file (nth 1 parent-target))
  10693. (pos (nth 3 parent-target))
  10694. level)
  10695. (with-current-buffer (or (find-buffer-visiting file)
  10696. (find-file-noselect file))
  10697. (save-excursion
  10698. (save-restriction
  10699. (widen)
  10700. (if pos
  10701. (goto-char pos)
  10702. (goto-char (point-max))
  10703. (if (not (bolp)) (newline)))
  10704. (when (looking-at org-outline-regexp)
  10705. (setq level (funcall outline-level))
  10706. (org-end-of-subtree t t))
  10707. (org-back-over-empty-lines)
  10708. (insert "\n" (make-string
  10709. (if pos (org-get-valid-level level 1) 1) ?*)
  10710. " " child "\n")
  10711. (beginning-of-line 0)
  10712. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10713. (defun org-olpath-completing-read (prompt collection &rest args)
  10714. "Read an outline path like a file name."
  10715. (let ((thetable collection)
  10716. (org-completion-use-ido nil) ; does not work with ido.
  10717. (org-completion-use-iswitchb nil)) ; or iswitchb
  10718. (apply
  10719. 'org-icompleting-read prompt
  10720. (lambda (string predicate &optional flag)
  10721. (let (rtn r f (l (length string)))
  10722. (cond
  10723. ((eq flag nil)
  10724. ;; try completion
  10725. (try-completion string thetable))
  10726. ((eq flag t)
  10727. ;; all-completions
  10728. (setq rtn (all-completions string thetable predicate))
  10729. (mapcar
  10730. (lambda (x)
  10731. (setq r (substring x l))
  10732. (if (string-match " ([^)]*)$" x)
  10733. (setq f (match-string 0 x))
  10734. (setq f ""))
  10735. (if (string-match "/" r)
  10736. (concat string (substring r 0 (match-end 0)) f)
  10737. x))
  10738. rtn))
  10739. ((eq flag 'lambda)
  10740. ;; exact match?
  10741. (assoc string thetable)))))
  10742. args)))
  10743. ;;;; Dynamic blocks
  10744. (defun org-find-dblock (name)
  10745. "Find the first dynamic block with name NAME in the buffer.
  10746. If not found, stay at current position and return nil."
  10747. (let ((case-fold-search t) pos)
  10748. (save-excursion
  10749. (goto-char (point-min))
  10750. (setq pos (and (re-search-forward
  10751. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10752. (match-beginning 0))))
  10753. (if pos (goto-char pos))
  10754. pos))
  10755. (defconst org-dblock-start-re
  10756. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10757. "Matches the start line of a dynamic block, with parameters.")
  10758. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10759. "Matches the end of a dynamic block.")
  10760. (defun org-create-dblock (plist)
  10761. "Create a dynamic block section, with parameters taken from PLIST.
  10762. PLIST must contain a :name entry which is used as name of the block."
  10763. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10764. (end-of-line 1)
  10765. (newline))
  10766. (let ((col (current-column))
  10767. (name (plist-get plist :name)))
  10768. (insert "#+BEGIN: " name)
  10769. (while plist
  10770. (if (eq (car plist) :name)
  10771. (setq plist (cddr plist))
  10772. (insert " " (prin1-to-string (pop plist)))))
  10773. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10774. (beginning-of-line -2)))
  10775. (defun org-prepare-dblock ()
  10776. "Prepare dynamic block for refresh.
  10777. This empties the block, puts the cursor at the insert position and returns
  10778. the property list including an extra property :name with the block name."
  10779. (unless (looking-at org-dblock-start-re)
  10780. (user-error "Not at a dynamic block"))
  10781. (let* ((begdel (1+ (match-end 0)))
  10782. (name (org-no-properties (match-string 1)))
  10783. (params (append (list :name name)
  10784. (read (concat "(" (match-string 3) ")")))))
  10785. (save-excursion
  10786. (beginning-of-line 1)
  10787. (skip-chars-forward " \t")
  10788. (setq params (plist-put params :indentation-column (current-column))))
  10789. (unless (re-search-forward org-dblock-end-re nil t)
  10790. (error "Dynamic block not terminated"))
  10791. (setq params
  10792. (append params
  10793. (list :content (buffer-substring
  10794. begdel (match-beginning 0)))))
  10795. (delete-region begdel (match-beginning 0))
  10796. (goto-char begdel)
  10797. (open-line 1)
  10798. params))
  10799. (defun org-map-dblocks (&optional command)
  10800. "Apply COMMAND to all dynamic blocks in the current buffer.
  10801. If COMMAND is not given, use `org-update-dblock'."
  10802. (let ((cmd (or command 'org-update-dblock)))
  10803. (save-excursion
  10804. (goto-char (point-min))
  10805. (while (re-search-forward org-dblock-start-re nil t)
  10806. (goto-char (match-beginning 0))
  10807. (save-excursion
  10808. (condition-case nil
  10809. (funcall cmd)
  10810. (error (message "Error during update of dynamic block"))))
  10811. (unless (re-search-forward org-dblock-end-re nil t)
  10812. (error "Dynamic block not terminated"))))))
  10813. (defun org-dblock-update (&optional arg)
  10814. "User command for updating dynamic blocks.
  10815. Update the dynamic block at point. With prefix ARG, update all dynamic
  10816. blocks in the buffer."
  10817. (interactive "P")
  10818. (if arg
  10819. (org-update-all-dblocks)
  10820. (or (looking-at org-dblock-start-re)
  10821. (org-beginning-of-dblock))
  10822. (org-update-dblock)))
  10823. (defun org-update-dblock ()
  10824. "Update the dynamic block at point.
  10825. This means to empty the block, parse for parameters and then call
  10826. the correct writing function."
  10827. (interactive)
  10828. (save-excursion
  10829. (let* ((win (selected-window))
  10830. (pos (point))
  10831. (line (org-current-line))
  10832. (params (org-prepare-dblock))
  10833. (name (plist-get params :name))
  10834. (indent (plist-get params :indentation-column))
  10835. (cmd (intern (concat "org-dblock-write:" name))))
  10836. (message "Updating dynamic block `%s' at line %d..." name line)
  10837. (funcall cmd params)
  10838. (message "Updating dynamic block `%s' at line %d...done" name line)
  10839. (goto-char pos)
  10840. (when (and indent (> indent 0))
  10841. (setq indent (make-string indent ?\ ))
  10842. (save-excursion
  10843. (select-window win)
  10844. (org-beginning-of-dblock)
  10845. (forward-line 1)
  10846. (while (not (looking-at org-dblock-end-re))
  10847. (insert indent)
  10848. (beginning-of-line 2))
  10849. (when (looking-at org-dblock-end-re)
  10850. (and (looking-at "[ \t]+")
  10851. (replace-match ""))
  10852. (insert indent)))))))
  10853. (defun org-beginning-of-dblock ()
  10854. "Find the beginning of the dynamic block at point.
  10855. Error if there is no such block at point."
  10856. (let ((pos (point))
  10857. beg)
  10858. (end-of-line 1)
  10859. (if (and (re-search-backward org-dblock-start-re nil t)
  10860. (setq beg (match-beginning 0))
  10861. (re-search-forward org-dblock-end-re nil t)
  10862. (> (match-end 0) pos))
  10863. (goto-char beg)
  10864. (goto-char pos)
  10865. (error "Not in a dynamic block"))))
  10866. (defun org-update-all-dblocks ()
  10867. "Update all dynamic blocks in the buffer.
  10868. This function can be used in a hook."
  10869. (interactive)
  10870. (when (derived-mode-p 'org-mode)
  10871. (org-map-dblocks 'org-update-dblock)))
  10872. ;;;; Completion
  10873. (declare-function org-export-backend-name "org-export" (cl-x))
  10874. (declare-function org-export-backend-options "org-export" (cl-x))
  10875. (defun org-get-export-keywords ()
  10876. "Return a list of all currently understood export keywords.
  10877. Export keywords include options, block names, attributes and
  10878. keywords relative to each registered export back-end."
  10879. (let (keywords)
  10880. (dolist (backend
  10881. (org-bound-and-true-p org-export--registered-backends)
  10882. (delq nil keywords))
  10883. ;; Back-end name (for keywords, like #+LATEX:)
  10884. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10885. (dolist (option-entry (org-export-backend-options backend))
  10886. ;; Back-end options.
  10887. (push (nth 1 option-entry) keywords)))))
  10888. (defconst org-options-keywords
  10889. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10890. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10891. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10892. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10893. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10894. (defcustom org-structure-template-alist
  10895. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10896. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10897. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10898. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10899. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10900. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10901. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10902. "<literal style=\"latex\">\n?\n</literal>")
  10903. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10904. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10905. "<literal style=\"html\">\n?\n</literal>")
  10906. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10907. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10908. ("A" "#+ASCII: " "")
  10909. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10910. ("I" "#+INCLUDE: %file ?"
  10911. "<include file=%file markup=\"?\">"))
  10912. "Structure completion elements.
  10913. This is a list of abbreviation keys and values. The value gets inserted
  10914. if you type `<' followed by the key and then press the completion key,
  10915. usually `M-TAB'. %file will be replaced by a file name after prompting
  10916. for the file using completion. The cursor will be placed at the position
  10917. of the `?` in the template.
  10918. There are two templates for each key, the first uses the original Org syntax,
  10919. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10920. the default when the /org-mtags.el/ module has been loaded. See also the
  10921. variable `org-mtags-prefer-muse-templates'."
  10922. :group 'org-completion
  10923. :type '(repeat
  10924. (list
  10925. (string :tag "Key")
  10926. (string :tag "Template")
  10927. (string :tag "Muse Template"))))
  10928. (defun org-try-structure-completion ()
  10929. "Try to complete a structure template before point.
  10930. This looks for strings like \"<e\" on an otherwise empty line and
  10931. expands them."
  10932. (let ((l (buffer-substring (point-at-bol) (point)))
  10933. a)
  10934. (when (and (looking-at "[ \t]*$")
  10935. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10936. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10937. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10938. (match-beginning 1)) a)
  10939. t)))
  10940. (defun org-complete-expand-structure-template (start cell)
  10941. "Expand a structure template."
  10942. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10943. (rpl (nth (if musep 2 1) cell))
  10944. (ind ""))
  10945. (delete-region start (point))
  10946. (when (string-match "\\`#\\+" rpl)
  10947. (cond
  10948. ((bolp))
  10949. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10950. (setq ind (buffer-substring (point-at-bol) (point))))
  10951. (t (newline))))
  10952. (setq start (point))
  10953. (if (string-match "%file" rpl)
  10954. (setq rpl (replace-match
  10955. (concat
  10956. "\""
  10957. (save-match-data
  10958. (abbreviate-file-name (read-file-name "Include file: ")))
  10959. "\"")
  10960. t t rpl)))
  10961. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10962. (concat "\n" ind)))
  10963. (insert rpl)
  10964. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10965. ;;;; TODO, DEADLINE, Comments
  10966. (defun org-toggle-comment ()
  10967. "Change the COMMENT state of an entry."
  10968. (interactive)
  10969. (save-excursion
  10970. (org-back-to-heading)
  10971. (let (case-fold-search)
  10972. (cond
  10973. ((looking-at (format org-heading-keyword-regexp-format
  10974. org-comment-string))
  10975. (goto-char (match-end 1))
  10976. (looking-at (concat " +" org-comment-string))
  10977. (replace-match "" t t)
  10978. (when (eolp) (insert " ")))
  10979. ((looking-at org-outline-regexp)
  10980. (goto-char (match-end 0))
  10981. (insert org-comment-string " "))))))
  10982. (defvar org-last-todo-state-is-todo nil
  10983. "This is non-nil when the last TODO state change led to a TODO state.
  10984. If the last change removed the TODO tag or switched to DONE, then
  10985. this is nil.")
  10986. (defvar org-setting-tags nil) ; dynamically skipped
  10987. (defvar org-todo-setup-filter-hook nil
  10988. "Hook for functions that pre-filter todo specs.
  10989. Each function takes a todo spec and returns either nil or the spec
  10990. transformed into canonical form." )
  10991. (defvar org-todo-get-default-hook nil
  10992. "Hook for functions that get a default item for todo.
  10993. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10994. nil or a string to be used for the todo mark." )
  10995. (defvar org-agenda-headline-snapshot-before-repeat)
  10996. (defun org-current-effective-time ()
  10997. "Return current time adjusted for `org-extend-today-until' variable."
  10998. (let* ((ct (org-current-time))
  10999. (dct (decode-time ct))
  11000. (ct1
  11001. (cond
  11002. (org-use-last-clock-out-time-as-effective-time
  11003. (or (org-clock-get-last-clock-out-time) ct))
  11004. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11005. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11006. (t ct))))
  11007. ct1))
  11008. (defun org-todo-yesterday (&optional arg)
  11009. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11010. (interactive "P")
  11011. (if (eq major-mode 'org-agenda-mode)
  11012. (apply 'org-agenda-todo-yesterday arg)
  11013. (let* ((hour (third (decode-time
  11014. (org-current-time))))
  11015. (org-extend-today-until (1+ hour)))
  11016. (org-todo arg))))
  11017. (defvar org-block-entry-blocking ""
  11018. "First entry preventing the TODO state change.")
  11019. (defun org-todo (&optional arg)
  11020. "Change the TODO state of an item.
  11021. The state of an item is given by a keyword at the start of the heading,
  11022. like
  11023. *** TODO Write paper
  11024. *** DONE Call mom
  11025. The different keywords are specified in the variable `org-todo-keywords'.
  11026. By default the available states are \"TODO\" and \"DONE\".
  11027. So for this example: when the item starts with TODO, it is changed to DONE.
  11028. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11029. DONE are present, add TODO at the beginning of the heading.
  11030. With \\[universal-argument] prefix arg, use completion to determine the new \
  11031. state.
  11032. With numeric prefix arg, switch to that state.
  11033. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11034. keywords (nextset).
  11035. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11036. With a numeric prefix arg of 0, inhibit note taking for the change.
  11037. For calling through lisp, arg is also interpreted in the following way:
  11038. 'none -> empty state
  11039. \"\"(empty string) -> switch to empty state
  11040. 'done -> switch to DONE
  11041. 'nextset -> switch to the next set of keywords
  11042. 'previousset -> switch to the previous set of keywords
  11043. \"WAITING\" -> switch to the specified keyword, but only if it
  11044. really is a member of `org-todo-keywords'."
  11045. (interactive "P")
  11046. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11047. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11048. 'region-start-level 'region))
  11049. org-loop-over-headlines-in-active-region)
  11050. (org-map-entries
  11051. `(org-todo ,arg)
  11052. org-loop-over-headlines-in-active-region
  11053. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11054. (if (equal arg '(16)) (setq arg 'nextset))
  11055. (let ((org-blocker-hook org-blocker-hook)
  11056. commentp
  11057. case-fold-search)
  11058. (when (equal arg '(64))
  11059. (setq arg nil org-blocker-hook nil))
  11060. (when (and org-blocker-hook
  11061. (or org-inhibit-blocking
  11062. (org-entry-get nil "NOBLOCKING")))
  11063. (setq org-blocker-hook nil))
  11064. (save-excursion
  11065. (catch 'exit
  11066. (org-back-to-heading t)
  11067. (when (looking-at (concat "^\\*+ " org-comment-string))
  11068. (org-toggle-comment)
  11069. (setq commentp t))
  11070. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11071. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11072. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11073. (let* ((match-data (match-data))
  11074. (startpos (point-at-bol))
  11075. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11076. (org-log-done org-log-done)
  11077. (org-log-repeat org-log-repeat)
  11078. (org-todo-log-states org-todo-log-states)
  11079. (org-inhibit-logging
  11080. (if (equal arg 0)
  11081. (progn (setq arg nil) 'note) org-inhibit-logging))
  11082. (this (match-string 1))
  11083. (hl-pos (match-beginning 0))
  11084. (head (org-get-todo-sequence-head this))
  11085. (ass (assoc head org-todo-kwd-alist))
  11086. (interpret (nth 1 ass))
  11087. (done-word (nth 3 ass))
  11088. (final-done-word (nth 4 ass))
  11089. (org-last-state (or this ""))
  11090. (completion-ignore-case t)
  11091. (member (member this org-todo-keywords-1))
  11092. (tail (cdr member))
  11093. (org-state (cond
  11094. ((and org-todo-key-trigger
  11095. (or (and (equal arg '(4))
  11096. (eq org-use-fast-todo-selection 'prefix))
  11097. (and (not arg) org-use-fast-todo-selection
  11098. (not (eq org-use-fast-todo-selection
  11099. 'prefix)))))
  11100. ;; Use fast selection
  11101. (org-fast-todo-selection))
  11102. ((and (equal arg '(4))
  11103. (or (not org-use-fast-todo-selection)
  11104. (not org-todo-key-trigger)))
  11105. ;; Read a state with completion
  11106. (org-icompleting-read
  11107. "State: " (mapcar 'list org-todo-keywords-1)
  11108. nil t))
  11109. ((eq arg 'right)
  11110. (if this
  11111. (if tail (car tail) nil)
  11112. (car org-todo-keywords-1)))
  11113. ((eq arg 'left)
  11114. (if (equal member org-todo-keywords-1)
  11115. nil
  11116. (if this
  11117. (nth (- (length org-todo-keywords-1)
  11118. (length tail) 2)
  11119. org-todo-keywords-1)
  11120. (org-last org-todo-keywords-1))))
  11121. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11122. (setq arg nil))) ; hack to fall back to cycling
  11123. (arg
  11124. ;; user or caller requests a specific state
  11125. (cond
  11126. ((equal arg "") nil)
  11127. ((eq arg 'none) nil)
  11128. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11129. ((eq arg 'nextset)
  11130. (or (car (cdr (member head org-todo-heads)))
  11131. (car org-todo-heads)))
  11132. ((eq arg 'previousset)
  11133. (let ((org-todo-heads (reverse org-todo-heads)))
  11134. (or (car (cdr (member head org-todo-heads)))
  11135. (car org-todo-heads))))
  11136. ((car (member arg org-todo-keywords-1)))
  11137. ((stringp arg)
  11138. (user-error "State `%s' not valid in this file" arg))
  11139. ((nth (1- (prefix-numeric-value arg))
  11140. org-todo-keywords-1))))
  11141. ((null member) (or head (car org-todo-keywords-1)))
  11142. ((equal this final-done-word) nil) ;; -> make empty
  11143. ((null tail) nil) ;; -> first entry
  11144. ((memq interpret '(type priority))
  11145. (if (eq this-command last-command)
  11146. (car tail)
  11147. (if (> (length tail) 0)
  11148. (or done-word (car org-done-keywords))
  11149. nil)))
  11150. (t
  11151. (car tail))))
  11152. (org-state (or
  11153. (run-hook-with-args-until-success
  11154. 'org-todo-get-default-hook org-state org-last-state)
  11155. org-state))
  11156. (next (if org-state (concat " " org-state " ") " "))
  11157. (change-plist (list :type 'todo-state-change :from this :to org-state
  11158. :position startpos))
  11159. dolog now-done-p)
  11160. (when org-blocker-hook
  11161. (setq org-last-todo-state-is-todo
  11162. (not (member this org-done-keywords)))
  11163. (unless (save-excursion
  11164. (save-match-data
  11165. (org-with-wide-buffer
  11166. (run-hook-with-args-until-failure
  11167. 'org-blocker-hook change-plist))))
  11168. (if (org-called-interactively-p 'interactive)
  11169. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11170. this org-state org-block-entry-blocking)
  11171. ;; fail silently
  11172. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11173. this org-state org-block-entry-blocking)
  11174. (throw 'exit nil))))
  11175. (store-match-data match-data)
  11176. (replace-match next t t)
  11177. (unless (pos-visible-in-window-p hl-pos)
  11178. (message "TODO state changed to %s" (org-trim next)))
  11179. (unless head
  11180. (setq head (org-get-todo-sequence-head org-state)
  11181. ass (assoc head org-todo-kwd-alist)
  11182. interpret (nth 1 ass)
  11183. done-word (nth 3 ass)
  11184. final-done-word (nth 4 ass)))
  11185. (when (memq arg '(nextset previousset))
  11186. (message "Keyword-Set %d/%d: %s"
  11187. (- (length org-todo-sets) -1
  11188. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11189. (length org-todo-sets)
  11190. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11191. (setq org-last-todo-state-is-todo
  11192. (not (member org-state org-done-keywords)))
  11193. (setq now-done-p (and (member org-state org-done-keywords)
  11194. (not (member this org-done-keywords))))
  11195. (and logging (org-local-logging logging))
  11196. (when (and (or org-todo-log-states org-log-done)
  11197. (not (eq org-inhibit-logging t))
  11198. (not (memq arg '(nextset previousset))))
  11199. ;; we need to look at recording a time and note
  11200. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11201. (nth 2 (assoc this org-todo-log-states))))
  11202. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11203. (setq dolog 'time))
  11204. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11205. (and org-state
  11206. (member org-state org-not-done-keywords)
  11207. (not (member this org-not-done-keywords))))
  11208. ;; This is now a todo state and was not one before
  11209. ;; If there was a CLOSED time stamp, get rid of it.
  11210. (org-add-planning-info nil nil 'closed))
  11211. (when (and now-done-p org-log-done)
  11212. ;; It is now done, and it was not done before
  11213. (org-add-planning-info 'closed (org-current-effective-time))
  11214. (if (and (not dolog) (eq 'note org-log-done))
  11215. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11216. (when (and org-state dolog)
  11217. ;; This is a non-nil state, and we need to log it
  11218. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11219. ;; Fixup tag positioning
  11220. (org-todo-trigger-tag-changes org-state)
  11221. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11222. (when org-provide-todo-statistics
  11223. (org-update-parent-todo-statistics))
  11224. (run-hooks 'org-after-todo-state-change-hook)
  11225. (if (and arg (not (member org-state org-done-keywords)))
  11226. (setq head (org-get-todo-sequence-head org-state)))
  11227. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11228. ;; Do we need to trigger a repeat?
  11229. (when now-done-p
  11230. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11231. ;; This is for the agenda, take a snapshot of the headline.
  11232. (save-match-data
  11233. (setq org-agenda-headline-snapshot-before-repeat
  11234. (org-get-heading))))
  11235. (org-auto-repeat-maybe org-state))
  11236. ;; Fixup cursor location if close to the keyword
  11237. (if (and (outline-on-heading-p)
  11238. (not (bolp))
  11239. (save-excursion (beginning-of-line 1)
  11240. (looking-at org-todo-line-regexp))
  11241. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11242. (progn
  11243. (goto-char (or (match-end 2) (match-end 1)))
  11244. (and (looking-at " ") (just-one-space))))
  11245. (when org-trigger-hook
  11246. (save-excursion
  11247. (run-hook-with-args 'org-trigger-hook change-plist)))
  11248. (when commentp (org-toggle-comment))))))))
  11249. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11250. "Block turning an entry into a TODO, using the hierarchy.
  11251. This checks whether the current task should be blocked from state
  11252. changes. Such blocking occurs when:
  11253. 1. The task has children which are not all in a completed state.
  11254. 2. A task has a parent with the property :ORDERED:, and there
  11255. are siblings prior to the current task with incomplete
  11256. status.
  11257. 3. The parent of the task is blocked because it has siblings that should
  11258. be done first, or is child of a block grandparent TODO entry."
  11259. (if (not org-enforce-todo-dependencies)
  11260. t ; if locally turned off don't block
  11261. (catch 'dont-block
  11262. ;; If this is not a todo state change, or if this entry is already DONE,
  11263. ;; do not block
  11264. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11265. (member (plist-get change-plist :from)
  11266. (cons 'done org-done-keywords))
  11267. (member (plist-get change-plist :to)
  11268. (cons 'todo org-not-done-keywords))
  11269. (not (plist-get change-plist :to)))
  11270. (throw 'dont-block t))
  11271. ;; If this task has children, and any are undone, it's blocked
  11272. (save-excursion
  11273. (org-back-to-heading t)
  11274. (let ((this-level (funcall outline-level)))
  11275. (outline-next-heading)
  11276. (let ((child-level (funcall outline-level)))
  11277. (while (and (not (eobp))
  11278. (> child-level this-level))
  11279. ;; this todo has children, check whether they are all
  11280. ;; completed
  11281. (if (and (not (org-entry-is-done-p))
  11282. (org-entry-is-todo-p))
  11283. (progn (setq org-block-entry-blocking (org-get-heading))
  11284. (throw 'dont-block nil)))
  11285. (outline-next-heading)
  11286. (setq child-level (funcall outline-level))))))
  11287. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11288. ;; any previous siblings are undone, it's blocked
  11289. (save-excursion
  11290. (org-back-to-heading t)
  11291. (let* ((pos (point))
  11292. (parent-pos (and (org-up-heading-safe) (point))))
  11293. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11294. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11295. (forward-line 1)
  11296. (re-search-forward org-not-done-heading-regexp pos t))
  11297. (setq org-block-entry-blocking (match-string 0))
  11298. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11299. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11300. (while t
  11301. (goto-char parent-pos)
  11302. (if (not (looking-at org-not-done-heading-regexp))
  11303. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11304. (setq pos (point))
  11305. (setq parent-pos (and (org-up-heading-safe) (point)))
  11306. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11307. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11308. (forward-line 1)
  11309. (re-search-forward org-not-done-heading-regexp pos t)
  11310. (setq org-block-entry-blocking (org-get-heading)))
  11311. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11312. (defcustom org-track-ordered-property-with-tag nil
  11313. "Should the ORDERED property also be shown as a tag?
  11314. The ORDERED property decides if an entry should require subtasks to be
  11315. completed in sequence. Since a property is not very visible, setting
  11316. this option means that toggling the ORDERED property with the command
  11317. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11318. not relevant for the behavior, but it makes things more visible.
  11319. Note that toggling the tag with tags commands will not change the property
  11320. and therefore not influence behavior!
  11321. This can be t, meaning the tag ORDERED should be used, It can also be a
  11322. string to select a different tag for this task."
  11323. :group 'org-todo
  11324. :type '(choice
  11325. (const :tag "No tracking" nil)
  11326. (const :tag "Track with ORDERED tag" t)
  11327. (string :tag "Use other tag")))
  11328. (defun org-toggle-ordered-property ()
  11329. "Toggle the ORDERED property of the current entry.
  11330. For better visibility, you can track the value of this property with a tag.
  11331. See variable `org-track-ordered-property-with-tag'."
  11332. (interactive)
  11333. (let* ((t1 org-track-ordered-property-with-tag)
  11334. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11335. (save-excursion
  11336. (org-back-to-heading)
  11337. (if (org-entry-get nil "ORDERED")
  11338. (progn
  11339. (org-delete-property "ORDERED" "PROPERTIES")
  11340. (and tag (org-toggle-tag tag 'off))
  11341. (message "Subtasks can be completed in arbitrary order"))
  11342. (org-entry-put nil "ORDERED" "t")
  11343. (and tag (org-toggle-tag tag 'on))
  11344. (message "Subtasks must be completed in sequence")))))
  11345. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11346. (defun org-block-todo-from-checkboxes (change-plist)
  11347. "Block turning an entry into a TODO, using checkboxes.
  11348. This checks whether the current task should be blocked from state
  11349. changes because there are unchecked boxes in this entry."
  11350. (if (not org-enforce-todo-checkbox-dependencies)
  11351. t ; if locally turned off don't block
  11352. (catch 'dont-block
  11353. ;; If this is not a todo state change, or if this entry is already DONE,
  11354. ;; do not block
  11355. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11356. (member (plist-get change-plist :from)
  11357. (cons 'done org-done-keywords))
  11358. (member (plist-get change-plist :to)
  11359. (cons 'todo org-not-done-keywords))
  11360. (not (plist-get change-plist :to)))
  11361. (throw 'dont-block t))
  11362. ;; If this task has checkboxes that are not checked, it's blocked
  11363. (save-excursion
  11364. (org-back-to-heading t)
  11365. (let ((beg (point)) end)
  11366. (outline-next-heading)
  11367. (setq end (point))
  11368. (goto-char beg)
  11369. (if (org-list-search-forward
  11370. (concat (org-item-beginning-re)
  11371. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11372. "\\[[- ]\\]")
  11373. end t)
  11374. (progn
  11375. (if (boundp 'org-blocked-by-checkboxes)
  11376. (setq org-blocked-by-checkboxes t))
  11377. (throw 'dont-block nil)))))
  11378. t))) ; do not block
  11379. (defun org-entry-blocked-p ()
  11380. "Is the current entry blocked?"
  11381. (org-with-silent-modifications
  11382. (if (org-entry-get nil "NOBLOCKING")
  11383. nil ;; Never block this entry
  11384. (not (run-hook-with-args-until-failure
  11385. 'org-blocker-hook
  11386. (list :type 'todo-state-change
  11387. :position (point)
  11388. :from 'todo
  11389. :to 'done))))))
  11390. (defun org-update-statistics-cookies (all)
  11391. "Update the statistics cookie, either from TODO or from checkboxes.
  11392. This should be called with the cursor in a line with a statistics cookie."
  11393. (interactive "P")
  11394. (if all
  11395. (progn
  11396. (org-update-checkbox-count 'all)
  11397. (org-map-entries 'org-update-parent-todo-statistics))
  11398. (if (not (org-at-heading-p))
  11399. (org-update-checkbox-count)
  11400. (let ((pos (point-marker))
  11401. end l1 l2)
  11402. (ignore-errors (org-back-to-heading t))
  11403. (if (not (org-at-heading-p))
  11404. (org-update-checkbox-count)
  11405. (setq l1 (org-outline-level))
  11406. (setq end (save-excursion
  11407. (outline-next-heading)
  11408. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11409. (point)))
  11410. (if (and (save-excursion
  11411. (re-search-forward
  11412. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11413. (not (save-excursion (re-search-forward
  11414. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11415. (org-update-checkbox-count)
  11416. (if (and l2 (> l2 l1))
  11417. (progn
  11418. (goto-char end)
  11419. (org-update-parent-todo-statistics))
  11420. (goto-char pos)
  11421. (beginning-of-line 1)
  11422. (while (re-search-forward
  11423. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11424. (point-at-eol) t)
  11425. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11426. (goto-char pos)
  11427. (move-marker pos nil)))))
  11428. (defvar org-entry-property-inherited-from) ;; defined below
  11429. (defun org-update-parent-todo-statistics ()
  11430. "Update any statistics cookie in the parent of the current headline.
  11431. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11432. the entire subtree and this will travel up the hierarchy and update
  11433. statistics everywhere."
  11434. (let* ((prop (save-excursion (org-up-heading-safe)
  11435. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11436. (recursive (or (not org-hierarchical-todo-statistics)
  11437. (and prop (string-match "\\<recursive\\>" prop))))
  11438. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11439. 0))
  11440. (first t)
  11441. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11442. level ltoggle l1 new ndel
  11443. (cnt-all 0) (cnt-done 0) is-percent kwd
  11444. checkbox-beg ov ovs ove cookie-present)
  11445. (catch 'exit
  11446. (save-excursion
  11447. (beginning-of-line 1)
  11448. (setq ltoggle (funcall outline-level))
  11449. ;; Three situations are to consider:
  11450. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11451. ;; to the top-level ancestor on the headline;
  11452. ;; 2. If parent has "recursive" property, repeat up to the
  11453. ;; headline setting that property, taking inheritance into
  11454. ;; account;
  11455. ;; 3. Else, move up to direct parent and proceed only once.
  11456. (while (and (setq level (org-up-heading-safe))
  11457. (or recursive first)
  11458. (>= (point) lim))
  11459. (setq first nil cookie-present nil)
  11460. (unless (and level
  11461. (not (string-match
  11462. "\\<checkbox\\>"
  11463. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11464. "")))))
  11465. (throw 'exit nil))
  11466. (while (re-search-forward box-re (point-at-eol) t)
  11467. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11468. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11469. (save-match-data
  11470. (unless (outline-next-heading) (throw 'exit nil))
  11471. (while (and (looking-at org-complex-heading-regexp)
  11472. (> (setq l1 (length (match-string 1))) level))
  11473. (setq kwd (and (or recursive (= l1 ltoggle))
  11474. (match-string 2)))
  11475. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11476. (and (listp org-provide-todo-statistics)
  11477. (or (member kwd org-provide-todo-statistics)
  11478. (member kwd org-done-keywords))))
  11479. (setq cnt-all (1+ cnt-all))
  11480. (if (eq org-provide-todo-statistics t)
  11481. (and kwd (setq cnt-all (1+ cnt-all)))))
  11482. (and (member kwd org-done-keywords)
  11483. (setq cnt-done (1+ cnt-done)))
  11484. (outline-next-heading)))
  11485. (setq new
  11486. (if is-percent
  11487. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11488. (format "[%d/%d]" cnt-done cnt-all))
  11489. ndel (- (match-end 0) checkbox-beg))
  11490. ;; handle overlays when updating cookie from column view
  11491. (when (setq ov (car (overlays-at checkbox-beg)))
  11492. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11493. (delete-overlay ov))
  11494. (goto-char checkbox-beg)
  11495. (insert new)
  11496. (delete-region (point) (+ (point) ndel))
  11497. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11498. (when ov (move-overlay ov ovs ove)))
  11499. (when cookie-present
  11500. (run-hook-with-args 'org-after-todo-statistics-hook
  11501. cnt-done (- cnt-all cnt-done))))))
  11502. (run-hooks 'org-todo-statistics-hook)))
  11503. (defvar org-after-todo-statistics-hook nil
  11504. "Hook that is called after a TODO statistics cookie has been updated.
  11505. Each function is called with two arguments: the number of not-done entries
  11506. and the number of done entries.
  11507. For example, the following function, when added to this hook, will switch
  11508. an entry to DONE when all children are done, and back to TODO when new
  11509. entries are set to a TODO status. Note that this hook is only called
  11510. when there is a statistics cookie in the headline!
  11511. (defun org-summary-todo (n-done n-not-done)
  11512. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11513. (let (org-log-done org-log-states) ; turn off logging
  11514. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11515. ")
  11516. (defvar org-todo-statistics-hook nil
  11517. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11518. This hook runs even if there is no statistics cookie present, in which case
  11519. `org-after-todo-statistics-hook' would not run.")
  11520. (defun org-todo-trigger-tag-changes (state)
  11521. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11522. (let ((l org-todo-state-tags-triggers)
  11523. changes)
  11524. (when (or (not state) (equal state ""))
  11525. (setq changes (append changes (cdr (assoc "" l)))))
  11526. (when (and (stringp state) (> (length state) 0))
  11527. (setq changes (append changes (cdr (assoc state l)))))
  11528. (when (member state org-not-done-keywords)
  11529. (setq changes (append changes (cdr (assoc 'todo l)))))
  11530. (when (member state org-done-keywords)
  11531. (setq changes (append changes (cdr (assoc 'done l)))))
  11532. (dolist (c changes)
  11533. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11534. (defun org-local-logging (value)
  11535. "Get logging settings from a property VALUE."
  11536. (let* (words w a)
  11537. ;; directly set the variables, they are already local.
  11538. (setq org-log-done nil
  11539. org-log-repeat nil
  11540. org-todo-log-states nil)
  11541. (setq words (org-split-string value))
  11542. (while (setq w (pop words))
  11543. (cond
  11544. ((setq a (assoc w org-startup-options))
  11545. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11546. (set (nth 1 a) (nth 2 a))))
  11547. ((setq a (org-extract-log-state-settings w))
  11548. (and (member (car a) org-todo-keywords-1)
  11549. (push a org-todo-log-states)))))))
  11550. (defun org-get-todo-sequence-head (kwd)
  11551. "Return the head of the TODO sequence to which KWD belongs.
  11552. If KWD is not set, check if there is a text property remembering the
  11553. right sequence."
  11554. (let (p)
  11555. (cond
  11556. ((not kwd)
  11557. (or (get-text-property (point-at-bol) 'org-todo-head)
  11558. (progn
  11559. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11560. nil (point-at-eol)))
  11561. (get-text-property p 'org-todo-head))))
  11562. ((not (member kwd org-todo-keywords-1))
  11563. (car org-todo-keywords-1))
  11564. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11565. (defun org-fast-todo-selection ()
  11566. "Fast TODO keyword selection with single keys.
  11567. Returns the new TODO keyword, or nil if no state change should occur."
  11568. (let* ((fulltable org-todo-key-alist)
  11569. (done-keywords org-done-keywords) ;; needed for the faces.
  11570. (maxlen (apply 'max (mapcar
  11571. (lambda (x)
  11572. (if (stringp (car x)) (string-width (car x)) 0))
  11573. fulltable)))
  11574. (expert nil)
  11575. (fwidth (+ maxlen 3 1 3))
  11576. (ncol (/ (- (window-width) 4) fwidth))
  11577. tg cnt e c tbl
  11578. groups ingroup)
  11579. (save-excursion
  11580. (save-window-excursion
  11581. (if expert
  11582. (set-buffer (get-buffer-create " *Org todo*"))
  11583. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11584. (erase-buffer)
  11585. (org-set-local 'org-done-keywords done-keywords)
  11586. (setq tbl fulltable cnt 0)
  11587. (while (setq e (pop tbl))
  11588. (cond
  11589. ((equal e '(:startgroup))
  11590. (push '() groups) (setq ingroup t)
  11591. (when (not (= cnt 0))
  11592. (setq cnt 0)
  11593. (insert "\n"))
  11594. (insert "{ "))
  11595. ((equal e '(:endgroup))
  11596. (setq ingroup nil cnt 0)
  11597. (insert "}\n"))
  11598. ((equal e '(:newline))
  11599. (when (not (= cnt 0))
  11600. (setq cnt 0)
  11601. (insert "\n")
  11602. (setq e (car tbl))
  11603. (while (equal (car tbl) '(:newline))
  11604. (insert "\n")
  11605. (setq tbl (cdr tbl)))))
  11606. (t
  11607. (setq tg (car e) c (cdr e))
  11608. (if ingroup (push tg (car groups)))
  11609. (setq tg (org-add-props tg nil 'face
  11610. (org-get-todo-face tg)))
  11611. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11612. (insert "[" c "] " tg (make-string
  11613. (- fwidth 4 (length tg)) ?\ ))
  11614. (when (= (setq cnt (1+ cnt)) ncol)
  11615. (insert "\n")
  11616. (if ingroup (insert " "))
  11617. (setq cnt 0)))))
  11618. (insert "\n")
  11619. (goto-char (point-min))
  11620. (if (not expert) (org-fit-window-to-buffer))
  11621. (message "[a-z..]:Set [SPC]:clear")
  11622. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11623. (cond
  11624. ((or (= c ?\C-g)
  11625. (and (= c ?q) (not (rassoc c fulltable))))
  11626. (setq quit-flag t))
  11627. ((= c ?\ ) nil)
  11628. ((setq e (rassoc c fulltable) tg (car e))
  11629. tg)
  11630. (t (setq quit-flag t)))))))
  11631. (defun org-entry-is-todo-p ()
  11632. (member (org-get-todo-state) org-not-done-keywords))
  11633. (defun org-entry-is-done-p ()
  11634. (member (org-get-todo-state) org-done-keywords))
  11635. (defun org-get-todo-state ()
  11636. "Return the TODO keyword of the current subtree."
  11637. (save-excursion
  11638. (org-back-to-heading t)
  11639. (and (looking-at org-todo-line-regexp)
  11640. (match-end 2)
  11641. (match-string 2))))
  11642. (defun org-at-date-range-p (&optional inactive-ok)
  11643. "Is the cursor inside a date range?"
  11644. (interactive)
  11645. (save-excursion
  11646. (catch 'exit
  11647. (let ((pos (point)))
  11648. (skip-chars-backward "^[<\r\n")
  11649. (skip-chars-backward "<[")
  11650. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11651. (>= (match-end 0) pos)
  11652. (throw 'exit t))
  11653. (skip-chars-backward "^<[\r\n")
  11654. (skip-chars-backward "<[")
  11655. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11656. (>= (match-end 0) pos)
  11657. (throw 'exit t)))
  11658. nil)))
  11659. (defun org-get-repeat (&optional tagline)
  11660. "Check if there is a deadline/schedule with repeater in this entry."
  11661. (save-match-data
  11662. (save-excursion
  11663. (org-back-to-heading t)
  11664. (and (re-search-forward (if tagline
  11665. (concat tagline "\\s-*" org-repeat-re)
  11666. org-repeat-re)
  11667. (org-entry-end-position) t)
  11668. (match-string-no-properties 1)))))
  11669. (defvar org-last-changed-timestamp)
  11670. (defvar org-last-inserted-timestamp)
  11671. (defvar org-log-post-message)
  11672. (defvar org-log-note-purpose)
  11673. (defvar org-log-note-how)
  11674. (defvar org-log-note-extra)
  11675. (defun org-auto-repeat-maybe (done-word)
  11676. "Check if the current headline contains a repeated deadline/schedule.
  11677. If yes, set TODO state back to what it was and change the base date
  11678. of repeating deadline/scheduled time stamps to new date.
  11679. This function is run automatically after each state change to a DONE state."
  11680. ;; last-state is dynamically scoped into this function
  11681. (let* ((repeat (org-get-repeat))
  11682. (aa (assoc org-last-state org-todo-kwd-alist))
  11683. (interpret (nth 1 aa))
  11684. (head (nth 2 aa))
  11685. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11686. (msg "Entry repeats: ")
  11687. (org-log-done nil)
  11688. (org-todo-log-states nil)
  11689. re type n what ts time to-state)
  11690. (when repeat
  11691. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11692. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11693. org-todo-repeat-to-state))
  11694. (unless (and to-state (member to-state org-todo-keywords-1))
  11695. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11696. (org-todo to-state)
  11697. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11698. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11699. (org-time-stamp-format t t))))
  11700. (when org-log-repeat
  11701. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11702. (memq 'org-add-log-note post-command-hook))
  11703. ;; OK, we are already setup for some record
  11704. (if (eq org-log-repeat 'note)
  11705. ;; make sure we take a note, not only a time stamp
  11706. (setq org-log-note-how 'note))
  11707. ;; Set up for taking a record
  11708. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11709. org-last-state
  11710. 'findpos org-log-repeat)))
  11711. (org-back-to-heading t)
  11712. (org-add-planning-info nil nil 'closed)
  11713. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11714. org-deadline-time-regexp "\\)\\|\\("
  11715. org-ts-regexp "\\)"))
  11716. (while (re-search-forward
  11717. re (save-excursion (outline-next-heading) (point)) t)
  11718. (setq type (if (match-end 1) org-scheduled-string
  11719. (if (match-end 3) org-deadline-string "Plain:"))
  11720. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11721. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11722. (setq n (string-to-number (match-string 2 ts))
  11723. what (match-string 3 ts))
  11724. (if (equal what "w") (setq n (* n 7) what "d"))
  11725. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11726. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11727. ;; Preparation, see if we need to modify the start date for the change
  11728. (when (match-end 1)
  11729. (setq time (save-match-data (org-time-string-to-time ts)))
  11730. (cond
  11731. ((equal (match-string 1 ts) ".")
  11732. ;; Shift starting date to today
  11733. (org-timestamp-change
  11734. (- (org-today) (time-to-days time))
  11735. 'day))
  11736. ((equal (match-string 1 ts) "+")
  11737. (let ((nshiftmax 10) (nshift 0))
  11738. (while (or (= nshift 0)
  11739. (<= (time-to-days time)
  11740. (time-to-days (current-time))))
  11741. (when (= (incf nshift) nshiftmax)
  11742. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11743. (error "Abort")))
  11744. (org-timestamp-change n (cdr (assoc what whata)))
  11745. (org-at-timestamp-p t)
  11746. (setq ts (match-string 1))
  11747. (setq time (save-match-data (org-time-string-to-time ts)))))
  11748. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11749. ;; rematch, so that we have everything in place for the real shift
  11750. (org-at-timestamp-p t)
  11751. (setq ts (match-string 1))
  11752. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11753. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11754. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11755. (setq org-log-post-message msg)
  11756. (message "%s" msg))))
  11757. (defun org-show-todo-tree (arg)
  11758. "Make a compact tree which shows all headlines marked with TODO.
  11759. The tree will show the lines where the regexp matches, and all higher
  11760. headlines above the match.
  11761. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11762. With a numeric prefix N, construct a sparse tree for the Nth element
  11763. of `org-todo-keywords-1'."
  11764. (interactive "P")
  11765. (let ((case-fold-search nil)
  11766. (kwd-re
  11767. (cond ((null arg) org-not-done-regexp)
  11768. ((equal arg '(4))
  11769. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11770. (mapcar 'list org-todo-keywords-1))))
  11771. (concat "\\("
  11772. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11773. "\\)\\>")))
  11774. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11775. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11776. org-todo-keywords-1)))
  11777. (t (user-error "Invalid prefix argument: %s" arg)))))
  11778. (message "%d TODO entries found"
  11779. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11780. (defun org-deadline (arg &optional time)
  11781. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11782. With one universal prefix argument, remove any deadline from the item.
  11783. With two universal prefix arguments, prompt for a warning delay.
  11784. With argument TIME, set the deadline at the corresponding date. TIME
  11785. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11786. (interactive "P")
  11787. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11788. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11789. 'region-start-level 'region))
  11790. org-loop-over-headlines-in-active-region)
  11791. (org-map-entries
  11792. `(org-deadline ',arg ,time)
  11793. org-loop-over-headlines-in-active-region
  11794. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11795. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11796. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11797. (repeater (and old-date
  11798. (string-match
  11799. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11800. old-date)
  11801. (match-string 1 old-date))))
  11802. (cond
  11803. ((equal arg '(4))
  11804. (when (and old-date org-log-redeadline)
  11805. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11806. org-log-redeadline))
  11807. (org-remove-timestamp-with-keyword org-deadline-string)
  11808. (message "Item no longer has a deadline."))
  11809. ((equal arg '(16))
  11810. (save-excursion
  11811. (org-back-to-heading t)
  11812. (if (re-search-forward
  11813. org-deadline-time-regexp
  11814. (save-excursion (outline-next-heading) (point)) t)
  11815. (let* ((rpl0 (match-string 1))
  11816. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11817. (replace-match
  11818. (concat org-deadline-string
  11819. " <" rpl
  11820. (format " -%dd"
  11821. (abs
  11822. (- (time-to-days
  11823. (save-match-data
  11824. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11825. (time-to-days old-date-time))))
  11826. ">") t t))
  11827. (user-error "No deadline information to update"))))
  11828. (t
  11829. (org-add-planning-info 'deadline time 'closed)
  11830. (when (and old-date org-log-redeadline
  11831. (not (equal old-date
  11832. (substring org-last-inserted-timestamp 1 -1))))
  11833. (org-add-log-setup 'redeadline nil old-date 'findpos
  11834. org-log-redeadline))
  11835. (when repeater
  11836. (save-excursion
  11837. (org-back-to-heading t)
  11838. (when (re-search-forward (concat org-deadline-string " "
  11839. org-last-inserted-timestamp)
  11840. (save-excursion
  11841. (outline-next-heading) (point)) t)
  11842. (goto-char (1- (match-end 0)))
  11843. (insert " " repeater)
  11844. (setq org-last-inserted-timestamp
  11845. (concat (substring org-last-inserted-timestamp 0 -1)
  11846. " " repeater
  11847. (substring org-last-inserted-timestamp -1))))))
  11848. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11849. (defun org-schedule (arg &optional time)
  11850. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11851. With one universal prefix argument, remove any scheduling date from the item.
  11852. With two universal prefix arguments, prompt for a delay cookie.
  11853. With argument TIME, scheduled at the corresponding date. TIME can
  11854. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11855. (interactive "P")
  11856. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11857. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11858. 'region-start-level 'region))
  11859. org-loop-over-headlines-in-active-region)
  11860. (org-map-entries
  11861. `(org-schedule ',arg ,time)
  11862. org-loop-over-headlines-in-active-region
  11863. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11864. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11865. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11866. (repeater (and old-date
  11867. (string-match
  11868. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11869. old-date)
  11870. (match-string 1 old-date))))
  11871. (cond
  11872. ((equal arg '(4))
  11873. (progn
  11874. (when (and old-date org-log-reschedule)
  11875. (org-add-log-setup 'delschedule nil old-date 'findpos
  11876. org-log-reschedule))
  11877. (org-remove-timestamp-with-keyword org-scheduled-string)
  11878. (message "Item is no longer scheduled.")))
  11879. ((equal arg '(16))
  11880. (save-excursion
  11881. (org-back-to-heading t)
  11882. (if (re-search-forward
  11883. org-scheduled-time-regexp
  11884. (save-excursion (outline-next-heading) (point)) t)
  11885. (let* ((rpl0 (match-string 1))
  11886. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11887. (replace-match
  11888. (concat org-scheduled-string
  11889. " <" rpl
  11890. (format " -%dd"
  11891. (abs
  11892. (- (time-to-days
  11893. (save-match-data
  11894. (org-read-date nil t nil "Delay until" old-date-time)))
  11895. (time-to-days old-date-time))))
  11896. ">") t t))
  11897. (user-error "No scheduled information to update"))))
  11898. (t
  11899. (org-add-planning-info 'scheduled time 'closed)
  11900. (when (and old-date org-log-reschedule
  11901. (not (equal old-date
  11902. (substring org-last-inserted-timestamp 1 -1))))
  11903. (org-add-log-setup 'reschedule nil old-date 'findpos
  11904. org-log-reschedule))
  11905. (when repeater
  11906. (save-excursion
  11907. (org-back-to-heading t)
  11908. (when (re-search-forward (concat org-scheduled-string " "
  11909. org-last-inserted-timestamp)
  11910. (save-excursion
  11911. (outline-next-heading) (point)) t)
  11912. (goto-char (1- (match-end 0)))
  11913. (insert " " repeater)
  11914. (setq org-last-inserted-timestamp
  11915. (concat (substring org-last-inserted-timestamp 0 -1)
  11916. " " repeater
  11917. (substring org-last-inserted-timestamp -1))))))
  11918. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11919. (defun org-get-scheduled-time (pom &optional inherit)
  11920. "Get the scheduled time as a time tuple, of a format suitable
  11921. for calling org-schedule with, or if there is no scheduling,
  11922. returns nil."
  11923. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11924. (when time
  11925. (apply 'encode-time (org-parse-time-string time)))))
  11926. (defun org-get-deadline-time (pom &optional inherit)
  11927. "Get the deadline as a time tuple, of a format suitable for
  11928. calling org-deadline with, or if there is no scheduling, returns
  11929. nil."
  11930. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11931. (when time
  11932. (apply 'encode-time (org-parse-time-string time)))))
  11933. (defun org-remove-timestamp-with-keyword (keyword)
  11934. "Remove all time stamps with KEYWORD in the current entry."
  11935. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11936. beg)
  11937. (save-excursion
  11938. (org-back-to-heading t)
  11939. (setq beg (point))
  11940. (outline-next-heading)
  11941. (while (re-search-backward re beg t)
  11942. (replace-match "")
  11943. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11944. (equal (char-before) ?\ ))
  11945. (backward-delete-char 1)
  11946. (if (string-match "^[ \t]*$" (buffer-substring
  11947. (point-at-bol) (point-at-eol)))
  11948. (delete-region (point-at-bol)
  11949. (min (point-max) (1+ (point-at-eol))))))))))
  11950. (defvar org-time-was-given) ; dynamically scoped parameter
  11951. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11952. (defun org-add-planning-info (what &optional time &rest remove)
  11953. "Insert new timestamp with keyword in the line directly after the headline.
  11954. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11955. If non is given, the user is prompted for a date.
  11956. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11957. be removed."
  11958. (interactive)
  11959. (let (org-time-was-given org-end-time-was-given ts
  11960. end default-time default-input)
  11961. (catch 'exit
  11962. (when (and (memq what '(scheduled deadline))
  11963. (or (not time)
  11964. (and (stringp time)
  11965. (string-match "^[-+]+[0-9]" time))))
  11966. ;; Try to get a default date/time from existing timestamp
  11967. (save-excursion
  11968. (org-back-to-heading t)
  11969. (setq end (save-excursion (outline-next-heading) (point)))
  11970. (when (re-search-forward (if (eq what 'scheduled)
  11971. org-scheduled-time-regexp
  11972. org-deadline-time-regexp)
  11973. end t)
  11974. (setq ts (match-string 1)
  11975. default-time
  11976. (apply 'encode-time (org-parse-time-string ts))
  11977. default-input (and ts (org-get-compact-tod ts))))))
  11978. (when what
  11979. (setq time
  11980. (if (stringp time)
  11981. ;; This is a string (relative or absolute), set proper date
  11982. (apply 'encode-time
  11983. (org-read-date-analyze
  11984. time default-time (decode-time default-time)))
  11985. ;; If necessary, get the time from the user
  11986. (or time (org-read-date nil 'to-time nil nil
  11987. default-time default-input)))))
  11988. (when (and org-insert-labeled-timestamps-at-point
  11989. (member what '(scheduled deadline)))
  11990. (insert
  11991. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11992. (org-insert-time-stamp time org-time-was-given
  11993. nil nil nil (list org-end-time-was-given))
  11994. (setq what nil))
  11995. (save-excursion
  11996. (save-restriction
  11997. (let (col list elt ts buffer-invisibility-spec)
  11998. (org-back-to-heading t)
  11999. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12000. (goto-char (match-end 1))
  12001. (setq col (current-column))
  12002. (goto-char (match-end 0))
  12003. (if (eobp) (insert "\n") (forward-char 1))
  12004. (when (and (not what)
  12005. (not (looking-at
  12006. (concat "[ \t]*"
  12007. org-keyword-time-not-clock-regexp))))
  12008. ;; Nothing to add, nothing to remove...... :-)
  12009. (throw 'exit nil))
  12010. (if (and (not (looking-at org-outline-regexp))
  12011. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12012. "[^\r\n]*"))
  12013. (not (equal (match-string 1) org-clock-string)))
  12014. (narrow-to-region (match-beginning 0) (match-end 0))
  12015. (insert-before-markers "\n")
  12016. (backward-char 1)
  12017. (narrow-to-region (point) (point))
  12018. (and org-adapt-indentation (org-indent-to-column col)))
  12019. ;; Check if we have to remove something.
  12020. (setq list (cons what remove))
  12021. (while list
  12022. (setq elt (pop list))
  12023. (when (or (and (eq elt 'scheduled)
  12024. (re-search-forward org-scheduled-time-regexp nil t))
  12025. (and (eq elt 'deadline)
  12026. (re-search-forward org-deadline-time-regexp nil t))
  12027. (and (eq elt 'closed)
  12028. (re-search-forward org-closed-time-regexp nil t)))
  12029. (replace-match "")
  12030. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12031. (and (looking-at "[ \t]+") (replace-match ""))
  12032. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12033. (when what
  12034. (insert
  12035. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12036. (cond ((eq what 'scheduled) org-scheduled-string)
  12037. ((eq what 'deadline) org-deadline-string)
  12038. ((eq what 'closed) org-closed-string))
  12039. " ")
  12040. (setq ts (org-insert-time-stamp
  12041. time
  12042. (or org-time-was-given
  12043. (and (eq what 'closed) org-log-done-with-time))
  12044. (eq what 'closed)
  12045. nil nil (list org-end-time-was-given)))
  12046. (insert
  12047. (if (not (or (bolp) (eq (char-before) ?\ )
  12048. (memq (char-after) '(32 10))
  12049. (eobp))) " " ""))
  12050. (end-of-line 1))
  12051. (goto-char (point-min))
  12052. (widen)
  12053. (if (and (looking-at "[ \t]*\n")
  12054. (equal (char-before) ?\n))
  12055. (delete-region (1- (point)) (point-at-eol)))
  12056. ts))))))
  12057. (defvar org-log-note-marker (make-marker))
  12058. (defvar org-log-note-purpose nil)
  12059. (defvar org-log-note-state nil)
  12060. (defvar org-log-note-previous-state nil)
  12061. (defvar org-log-note-how nil)
  12062. (defvar org-log-note-extra nil)
  12063. (defvar org-log-note-window-configuration nil)
  12064. (defvar org-log-note-return-to (make-marker))
  12065. (defvar org-log-note-effective-time nil
  12066. "Remembered current time so that dynamically scoped
  12067. `org-extend-today-until' affects tha timestamps in state change
  12068. log")
  12069. (defvar org-log-post-message nil
  12070. "Message to be displayed after a log note has been stored.
  12071. The auto-repeater uses this.")
  12072. (defun org-add-note ()
  12073. "Add a note to the current entry.
  12074. This is done in the same way as adding a state change note."
  12075. (interactive)
  12076. (org-add-log-setup 'note nil nil 'findpos nil))
  12077. (defvar org-property-end-re)
  12078. (defun org-add-log-setup (&optional purpose state prev-state
  12079. findpos how extra)
  12080. "Set up the post command hook to take a note.
  12081. If this is about to TODO state change, the new state is expected in STATE.
  12082. When FINDPOS is non-nil, find the correct position for the note in
  12083. the current entry. If not, assume that it can be inserted at point.
  12084. HOW is an indicator what kind of note should be created.
  12085. EXTRA is additional text that will be inserted into the notes buffer."
  12086. (let* ((org-log-into-drawer (org-log-into-drawer))
  12087. (drawer (cond ((stringp org-log-into-drawer)
  12088. org-log-into-drawer)
  12089. (org-log-into-drawer "LOGBOOK"))))
  12090. (save-restriction
  12091. (save-excursion
  12092. (when findpos
  12093. (org-back-to-heading t)
  12094. (narrow-to-region (point) (save-excursion
  12095. (outline-next-heading) (point)))
  12096. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12097. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12098. "[^\r\n]*\\)?"))
  12099. (goto-char (match-end 0))
  12100. (cond
  12101. (drawer
  12102. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12103. nil t)
  12104. (progn
  12105. (goto-char (match-end 0))
  12106. (or org-log-states-order-reversed
  12107. (and (re-search-forward org-property-end-re nil t)
  12108. (goto-char (1- (match-beginning 0))))))
  12109. (insert "\n:" drawer ":\n:END:")
  12110. (beginning-of-line 0)
  12111. (org-indent-line)
  12112. (beginning-of-line 2)
  12113. (org-indent-line)
  12114. (end-of-line 0)))
  12115. ((and org-log-state-notes-insert-after-drawers
  12116. (save-excursion
  12117. (forward-line) (looking-at org-drawer-regexp)))
  12118. (forward-line)
  12119. (while (looking-at org-drawer-regexp)
  12120. (goto-char (match-end 0))
  12121. (re-search-forward org-property-end-re (point-max) t)
  12122. (forward-line))
  12123. (forward-line -1)))
  12124. (unless org-log-states-order-reversed
  12125. (and (= (char-after) ?\n) (forward-char 1))
  12126. (org-skip-over-state-notes)
  12127. (skip-chars-backward " \t\n\r")))
  12128. (move-marker org-log-note-marker (point))
  12129. (setq org-log-note-purpose purpose
  12130. org-log-note-state state
  12131. org-log-note-previous-state prev-state
  12132. org-log-note-how how
  12133. org-log-note-extra extra
  12134. org-log-note-effective-time (org-current-effective-time))
  12135. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12136. (defun org-skip-over-state-notes ()
  12137. "Skip past the list of State notes in an entry."
  12138. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12139. (when (ignore-errors (goto-char (org-in-item-p)))
  12140. (let* ((struct (org-list-struct))
  12141. (prevs (org-list-prevs-alist struct)))
  12142. (while (looking-at "[ \t]*- State")
  12143. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12144. (org-list-get-item-end (point) struct)))))))
  12145. (defun org-add-log-note (&optional purpose)
  12146. "Pop up a window for taking a note, and add this note later at point."
  12147. (remove-hook 'post-command-hook 'org-add-log-note)
  12148. (setq org-log-note-window-configuration (current-window-configuration))
  12149. (delete-other-windows)
  12150. (move-marker org-log-note-return-to (point))
  12151. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12152. (goto-char org-log-note-marker)
  12153. (org-switch-to-buffer-other-window "*Org Note*")
  12154. (erase-buffer)
  12155. (if (memq org-log-note-how '(time state))
  12156. (let (current-prefix-arg) (org-store-log-note))
  12157. (let ((org-inhibit-startup t)) (org-mode))
  12158. (insert (format "# Insert note for %s.
  12159. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12160. (cond
  12161. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12162. ((eq org-log-note-purpose 'done) "closed todo item")
  12163. ((eq org-log-note-purpose 'state)
  12164. (format "state change from \"%s\" to \"%s\""
  12165. (or org-log-note-previous-state "")
  12166. (or org-log-note-state "")))
  12167. ((eq org-log-note-purpose 'reschedule)
  12168. "rescheduling")
  12169. ((eq org-log-note-purpose 'delschedule)
  12170. "no longer scheduled")
  12171. ((eq org-log-note-purpose 'redeadline)
  12172. "changing deadline")
  12173. ((eq org-log-note-purpose 'deldeadline)
  12174. "removing deadline")
  12175. ((eq org-log-note-purpose 'refile)
  12176. "refiling")
  12177. ((eq org-log-note-purpose 'note)
  12178. "this entry")
  12179. (t (error "This should not happen")))))
  12180. (if org-log-note-extra (insert org-log-note-extra))
  12181. (org-set-local 'org-finish-function 'org-store-log-note)
  12182. (run-hooks 'org-log-buffer-setup-hook)))
  12183. (defvar org-note-abort nil) ; dynamically scoped
  12184. (defun org-store-log-note ()
  12185. "Finish taking a log note, and insert it to where it belongs."
  12186. (let ((txt (buffer-string)))
  12187. (kill-buffer (current-buffer))
  12188. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12189. lines ind bul)
  12190. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12191. (setq txt (replace-match "" t t txt)))
  12192. (if (string-match "\\s-+\\'" txt)
  12193. (setq txt (replace-match "" t t txt)))
  12194. (setq lines (org-split-string txt "\n"))
  12195. (when (and note (string-match "\\S-" note))
  12196. (setq note
  12197. (org-replace-escapes
  12198. note
  12199. (list (cons "%u" (user-login-name))
  12200. (cons "%U" user-full-name)
  12201. (cons "%t" (format-time-string
  12202. (org-time-stamp-format 'long 'inactive)
  12203. org-log-note-effective-time))
  12204. (cons "%T" (format-time-string
  12205. (org-time-stamp-format 'long nil)
  12206. org-log-note-effective-time))
  12207. (cons "%d" (format-time-string
  12208. (org-time-stamp-format nil 'inactive)
  12209. org-log-note-effective-time))
  12210. (cons "%D" (format-time-string
  12211. (org-time-stamp-format nil nil)
  12212. org-log-note-effective-time))
  12213. (cons "%s" (if org-log-note-state
  12214. (concat "\"" org-log-note-state "\"")
  12215. ""))
  12216. (cons "%S" (if org-log-note-previous-state
  12217. (concat "\"" org-log-note-previous-state "\"")
  12218. "\"\"")))))
  12219. (if lines (setq note (concat note " \\\\")))
  12220. (push note lines))
  12221. (when (or current-prefix-arg org-note-abort)
  12222. (when org-log-into-drawer
  12223. (org-remove-empty-drawer-at
  12224. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12225. org-log-note-marker))
  12226. (setq lines nil))
  12227. (when lines
  12228. (with-current-buffer (marker-buffer org-log-note-marker)
  12229. (save-excursion
  12230. (goto-char org-log-note-marker)
  12231. (move-marker org-log-note-marker nil)
  12232. (end-of-line 1)
  12233. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12234. (setq ind (save-excursion
  12235. (if (ignore-errors (goto-char (org-in-item-p)))
  12236. (let ((struct (org-list-struct)))
  12237. (org-list-get-ind
  12238. (org-list-get-top-point struct) struct))
  12239. (skip-chars-backward " \r\t\n")
  12240. (cond
  12241. ((and (org-at-heading-p)
  12242. org-adapt-indentation)
  12243. (1+ (org-current-level)))
  12244. ((org-at-heading-p) 0)
  12245. (t (org-get-indentation))))))
  12246. (setq bul (org-list-bullet-string "-"))
  12247. (org-indent-line-to ind)
  12248. (insert bul (pop lines))
  12249. (let ((ind-body (+ (length bul) ind)))
  12250. (while lines
  12251. (insert "\n")
  12252. (org-indent-line-to ind-body)
  12253. (insert (pop lines))))
  12254. (message "Note stored")
  12255. (org-back-to-heading t)
  12256. (org-cycle-hide-drawers 'children))
  12257. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12258. ;; from within `org-add-log-note' because `buffer-undo-list'
  12259. ;; is then modified outside of `org-with-remote-undo'.
  12260. (when (eq this-command 'org-agenda-todo)
  12261. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12262. ;; Don't add undo information when called from `org-agenda-todo'
  12263. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12264. (set-window-configuration org-log-note-window-configuration)
  12265. (with-current-buffer (marker-buffer org-log-note-return-to)
  12266. (goto-char org-log-note-return-to))
  12267. (move-marker org-log-note-return-to nil)
  12268. (and org-log-post-message (message "%s" org-log-post-message))))
  12269. (defun org-remove-empty-drawer-at (drawer pos)
  12270. "Remove an empty drawer DRAWER at position POS.
  12271. POS may also be a marker."
  12272. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12273. (save-excursion
  12274. (save-restriction
  12275. (widen)
  12276. (goto-char pos)
  12277. (if (org-in-regexp
  12278. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12279. (replace-match ""))))))
  12280. (defvar org-ts-type nil)
  12281. (defun org-sparse-tree (&optional arg type)
  12282. "Create a sparse tree, prompt for the details.
  12283. This command can create sparse trees. You first need to select the type
  12284. of match used to create the tree:
  12285. t Show all TODO entries.
  12286. T Show entries with a specific TODO keyword.
  12287. m Show entries selected by a tags/property match.
  12288. p Enter a property name and its value (both with completion on existing
  12289. names/values) and show entries with that property.
  12290. r Show entries matching a regular expression (`/' can be used as well).
  12291. b Show deadlines and scheduled items before a date.
  12292. a Show deadlines and scheduled items after a date.
  12293. d Show deadlines due within `org-deadline-warning-days'.
  12294. D Show deadlines and scheduled items between a date range."
  12295. (interactive "P")
  12296. (setq type (or type org-sparse-tree-default-date-type))
  12297. (setq org-ts-type type)
  12298. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12299. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12300. [c]ycle through date types: %s"
  12301. (case type
  12302. (all "all timestamps")
  12303. (scheduled "only scheduled")
  12304. (deadline "only deadline")
  12305. (active "only active timestamps")
  12306. (inactive "only inactive timestamps")
  12307. (scheduled-or-deadline "scheduled/deadline")
  12308. (closed "with a closed time-stamp")
  12309. (otherwise "scheduled/deadline")))
  12310. (let ((answer (read-char-exclusive)))
  12311. (case answer
  12312. (?c
  12313. (org-sparse-tree
  12314. arg
  12315. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12316. inactive closed)))))
  12317. (?d (call-interactively #'org-check-deadlines))
  12318. (?b (call-interactively #'org-check-before-date))
  12319. (?a (call-interactively #'org-check-after-date))
  12320. (?D (call-interactively #'org-check-dates-range))
  12321. (?t (call-interactively #'org-show-todo-tree))
  12322. (?T (org-show-todo-tree '(4)))
  12323. (?m (call-interactively #'org-match-sparse-tree))
  12324. ((?p ?P)
  12325. (let* ((kwd (org-icompleting-read
  12326. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12327. (value (org-icompleting-read
  12328. "Value: " (mapcar #'list (org-property-values kwd)))))
  12329. (unless (string-match "\\`{.*}\\'" value)
  12330. (setq value (concat "\"" value "\"")))
  12331. (org-match-sparse-tree arg (concat kwd "=" value))))
  12332. ((?r ?R ?/) (call-interactively #'org-occur))
  12333. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12334. (defvar org-occur-highlights nil
  12335. "List of overlays used for occur matches.")
  12336. (make-variable-buffer-local 'org-occur-highlights)
  12337. (defvar org-occur-parameters nil
  12338. "Parameters of the active org-occur calls.
  12339. This is a list, each call to org-occur pushes as cons cell,
  12340. containing the regular expression and the callback, onto the list.
  12341. The list can contain several entries if `org-occur' has been called
  12342. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12343. will only contain one set of parameters. When the highlights are
  12344. removed (for example with `C-c C-c', or with the next edit (depending
  12345. on `org-remove-highlights-with-change'), this variable is emptied
  12346. as well.")
  12347. (make-variable-buffer-local 'org-occur-parameters)
  12348. (defun org-occur (regexp &optional keep-previous callback)
  12349. "Make a compact tree which shows all matches of REGEXP.
  12350. The tree will show the lines where the regexp matches, and all higher
  12351. headlines above the match. It will also show the heading after the match,
  12352. to make sure editing the matching entry is easy.
  12353. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12354. call to `org-occur' will be kept, to allow stacking of calls to this
  12355. command.
  12356. If CALLBACK is non-nil, it is a function which is called to confirm
  12357. that the match should indeed be shown."
  12358. (interactive "sRegexp: \nP")
  12359. (when (equal regexp "")
  12360. (user-error "Regexp cannot be empty"))
  12361. (unless keep-previous
  12362. (org-remove-occur-highlights nil nil t))
  12363. (push (cons regexp callback) org-occur-parameters)
  12364. (let ((cnt 0))
  12365. (save-excursion
  12366. (goto-char (point-min))
  12367. (if (or (not keep-previous) ; do not want to keep
  12368. (not org-occur-highlights)) ; no previous matches
  12369. ;; hide everything
  12370. (org-overview))
  12371. (while (re-search-forward regexp nil t)
  12372. (when (or (not callback)
  12373. (save-match-data (funcall callback)))
  12374. (setq cnt (1+ cnt))
  12375. (when org-highlight-sparse-tree-matches
  12376. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12377. (org-show-context 'occur-tree))))
  12378. (when org-remove-highlights-with-change
  12379. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12380. nil 'local))
  12381. (unless org-sparse-tree-open-archived-trees
  12382. (org-hide-archived-subtrees (point-min) (point-max)))
  12383. (run-hooks 'org-occur-hook)
  12384. (if (org-called-interactively-p 'interactive)
  12385. (message "%d match(es) for regexp %s" cnt regexp))
  12386. cnt))
  12387. (defun org-occur-next-match (&optional n reset)
  12388. "Function for `next-error-function' to find sparse tree matches.
  12389. N is the number of matches to move, when negative move backwards.
  12390. RESET is entirely ignored - this function always goes back to the
  12391. starting point when no match is found."
  12392. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12393. (search-func (if (< n 0)
  12394. 'previous-single-char-property-change
  12395. 'next-single-char-property-change))
  12396. (n (abs n))
  12397. (pos (point))
  12398. p1)
  12399. (catch 'exit
  12400. (while (setq p1 (funcall search-func (point) 'org-type))
  12401. (when (equal p1 limit)
  12402. (goto-char pos)
  12403. (error "No more matches"))
  12404. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12405. (setq n (1- n))
  12406. (when (= n 0)
  12407. (goto-char p1)
  12408. (throw 'exit (point))))
  12409. (goto-char p1))
  12410. (goto-char p1)
  12411. (error "No more matches"))))
  12412. (defun org-show-context (&optional key)
  12413. "Make sure point and context are visible.
  12414. How much context is shown depends upon the variables
  12415. `org-show-hierarchy-above', `org-show-following-heading',
  12416. `org-show-entry-below' and `org-show-siblings'."
  12417. (let ((heading-p (org-at-heading-p t))
  12418. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12419. (following-p (org-get-alist-option org-show-following-heading key))
  12420. (entry-p (org-get-alist-option org-show-entry-below key))
  12421. (siblings-p (org-get-alist-option org-show-siblings key)))
  12422. ;; Show heading or entry text
  12423. (if (and heading-p (not entry-p))
  12424. (org-flag-heading nil) ; only show the heading
  12425. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12426. (org-show-hidden-entry))) ; show entire entry
  12427. (when following-p
  12428. ;; Show next sibling, or heading below text
  12429. (save-excursion
  12430. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12431. (org-flag-heading nil))))
  12432. (when siblings-p (org-show-siblings))
  12433. (when hierarchy-p
  12434. ;; show all higher headings, possibly with siblings
  12435. (save-excursion
  12436. (while (and (condition-case nil
  12437. (progn (org-up-heading-all 1) t)
  12438. (error nil))
  12439. (not (bobp)))
  12440. (org-flag-heading nil)
  12441. (when siblings-p (org-show-siblings)))))
  12442. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12443. (defvar org-reveal-start-hook nil
  12444. "Hook run before revealing a location.")
  12445. (defun org-reveal (&optional siblings)
  12446. "Show current entry, hierarchy above it, and the following headline.
  12447. This can be used to show a consistent set of context around locations
  12448. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12449. not t for the search context.
  12450. With optional argument SIBLINGS, on each level of the hierarchy all
  12451. siblings are shown. This repairs the tree structure to what it would
  12452. look like when opened with hierarchical calls to `org-cycle'.
  12453. With double optional argument \\[universal-argument] \\[universal-argument], \
  12454. go to the parent and show the
  12455. entire tree."
  12456. (interactive "P")
  12457. (run-hooks 'org-reveal-start-hook)
  12458. (let ((org-show-hierarchy-above t)
  12459. (org-show-following-heading t)
  12460. (org-show-siblings (if siblings t org-show-siblings)))
  12461. (org-show-context nil))
  12462. (when (equal siblings '(16))
  12463. (save-excursion
  12464. (when (org-up-heading-safe)
  12465. (org-show-subtree)
  12466. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12467. (defun org-highlight-new-match (beg end)
  12468. "Highlight from BEG to END and mark the highlight is an occur headline."
  12469. (let ((ov (make-overlay beg end)))
  12470. (overlay-put ov 'face 'secondary-selection)
  12471. (overlay-put ov 'org-type 'org-occur)
  12472. (push ov org-occur-highlights)))
  12473. (defun org-remove-occur-highlights (&optional beg end noremove)
  12474. "Remove the occur highlights from the buffer.
  12475. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12476. from the `before-change-functions' in the current buffer."
  12477. (interactive)
  12478. (unless org-inhibit-highlight-removal
  12479. (mapc 'delete-overlay org-occur-highlights)
  12480. (setq org-occur-highlights nil)
  12481. (setq org-occur-parameters nil)
  12482. (unless noremove
  12483. (remove-hook 'before-change-functions
  12484. 'org-remove-occur-highlights 'local))))
  12485. ;;;; Priorities
  12486. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12487. "Regular expression matching the priority indicator.")
  12488. (defvar org-remove-priority-next-time nil)
  12489. (defun org-priority-up ()
  12490. "Increase the priority of the current item."
  12491. (interactive)
  12492. (org-priority 'up))
  12493. (defun org-priority-down ()
  12494. "Decrease the priority of the current item."
  12495. (interactive)
  12496. (org-priority 'down))
  12497. (defun org-priority (&optional action show)
  12498. "Change the priority of an item.
  12499. ACTION can be `set', `up', `down', or a character."
  12500. (interactive "P")
  12501. (if (equal action '(4))
  12502. (org-show-priority)
  12503. (unless org-enable-priority-commands
  12504. (user-error "Priority commands are disabled"))
  12505. (setq action (or action 'set))
  12506. (let (current new news have remove)
  12507. (save-excursion
  12508. (org-back-to-heading t)
  12509. (if (looking-at org-priority-regexp)
  12510. (setq current (string-to-char (match-string 2))
  12511. have t))
  12512. (cond
  12513. ((eq action 'remove)
  12514. (setq remove t new ?\ ))
  12515. ((or (eq action 'set)
  12516. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12517. (if (not (eq action 'set))
  12518. (setq new action)
  12519. (message "Priority %c-%c, SPC to remove: "
  12520. org-highest-priority org-lowest-priority)
  12521. (save-match-data
  12522. (setq new (read-char-exclusive))))
  12523. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12524. (= (upcase org-lowest-priority) org-lowest-priority))
  12525. (setq new (upcase new)))
  12526. (cond ((equal new ?\ ) (setq remove t))
  12527. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12528. (user-error "Priority must be between `%c' and `%c'"
  12529. org-highest-priority org-lowest-priority))))
  12530. ((eq action 'up)
  12531. (setq new (if have
  12532. (1- current) ; normal cycling
  12533. ;; last priority was empty
  12534. (if (eq last-command this-command)
  12535. org-lowest-priority ; wrap around empty to lowest
  12536. ;; default
  12537. (if org-priority-start-cycle-with-default
  12538. org-default-priority
  12539. (1- org-default-priority))))))
  12540. ((eq action 'down)
  12541. (setq new (if have
  12542. (1+ current) ; normal cycling
  12543. ;; last priority was empty
  12544. (if (eq last-command this-command)
  12545. org-highest-priority ; wrap around empty to highest
  12546. ;; default
  12547. (if org-priority-start-cycle-with-default
  12548. org-default-priority
  12549. (1+ org-default-priority))))))
  12550. (t (user-error "Invalid action")))
  12551. (if (or (< (upcase new) org-highest-priority)
  12552. (> (upcase new) org-lowest-priority))
  12553. (if (and (memq action '(up down))
  12554. (not have) (not (eq last-command this-command)))
  12555. ;; `new' is from default priority
  12556. (error
  12557. "The default can not be set, see `org-default-priority' why")
  12558. ;; normal cycling: `new' is beyond highest/lowest priority
  12559. ;; and is wrapped around to the empty priority
  12560. (setq remove t)))
  12561. (setq news (format "%c" new))
  12562. (if have
  12563. (if remove
  12564. (replace-match "" t t nil 1)
  12565. (replace-match news t t nil 2))
  12566. (if remove
  12567. (user-error "No priority cookie found in line")
  12568. (let ((case-fold-search nil))
  12569. (looking-at org-todo-line-regexp))
  12570. (if (match-end 2)
  12571. (progn
  12572. (goto-char (match-end 2))
  12573. (insert " [#" news "]"))
  12574. (goto-char (match-beginning 3))
  12575. (insert "[#" news "] "))))
  12576. (org-preserve-lc (org-set-tags nil 'align)))
  12577. (if remove
  12578. (message "Priority removed")
  12579. (message "Priority of current item set to %s" news)))))
  12580. (defun org-show-priority ()
  12581. "Show the priority of the current item.
  12582. This priority is composed of the main priority given with the [#A] cookies,
  12583. and by additional input from the age of a schedules or deadline entry."
  12584. (interactive)
  12585. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12586. (org-get-at-bol 'priority)
  12587. (save-excursion
  12588. (save-match-data
  12589. (beginning-of-line)
  12590. (and (looking-at org-heading-regexp)
  12591. (org-get-priority (match-string 0))))))))
  12592. (message "Priority is %d" (if pri pri -1000))))
  12593. (defun org-get-priority (s)
  12594. "Find priority cookie and return priority."
  12595. (save-match-data
  12596. (if (functionp org-get-priority-function)
  12597. (funcall org-get-priority-function)
  12598. (if (not (string-match org-priority-regexp s))
  12599. (* 1000 (- org-lowest-priority org-default-priority))
  12600. (* 1000 (- org-lowest-priority
  12601. (string-to-char (match-string 2 s))))))))
  12602. ;;;; Tags
  12603. (defvar org-agenda-archives-mode)
  12604. (defvar org-map-continue-from nil
  12605. "Position from where mapping should continue.
  12606. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12607. (defvar org-scanner-tags nil
  12608. "The current tag list while the tags scanner is running.")
  12609. (defvar org-trust-scanner-tags nil
  12610. "Should `org-get-tags-at' use the tags for the scanner.
  12611. This is for internal dynamical scoping only.
  12612. When this is non-nil, the function `org-get-tags-at' will return the value
  12613. of `org-scanner-tags' instead of building the list by itself. This
  12614. can lead to large speed-ups when the tags scanner is used in a file with
  12615. many entries, and when the list of tags is retrieved, for example to
  12616. obtain a list of properties. Building the tags list for each entry in such
  12617. a file becomes an N^2 operation - but with this variable set, it scales
  12618. as N.")
  12619. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12620. "Sca headline tags with inheritance and produce output ACTION.
  12621. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12622. or `agenda' to produce an entry list for an agenda view. It can also be
  12623. a Lisp form or a function that should be called at each matched headline, in
  12624. this case the return value is a list of all return values from these calls.
  12625. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12626. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12627. only lines with a not-done TODO keyword are included in the output.
  12628. This should be the same variable that was scoped into
  12629. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12630. START-LEVEL can be a string with asterisks, reducing the scope to
  12631. headlines matching this string."
  12632. (require 'org-agenda)
  12633. (let* ((re (concat "^"
  12634. (if start-level
  12635. ;; Get the correct level to match
  12636. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12637. org-outline-regexp)
  12638. " *\\(\\<\\("
  12639. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12640. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12641. (props (list 'face 'default
  12642. 'done-face 'org-agenda-done
  12643. 'undone-face 'default
  12644. 'mouse-face 'highlight
  12645. 'org-not-done-regexp org-not-done-regexp
  12646. 'org-todo-regexp org-todo-regexp
  12647. 'org-complex-heading-regexp org-complex-heading-regexp
  12648. 'help-echo
  12649. (format "mouse-2 or RET jump to org file %s"
  12650. (abbreviate-file-name
  12651. (or (buffer-file-name (buffer-base-buffer))
  12652. (buffer-name (buffer-base-buffer)))))))
  12653. (org-map-continue-from nil)
  12654. lspos tags tags-list
  12655. (tags-alist (list (cons 0 org-file-tags)))
  12656. (llast 0) rtn rtn1 level category i txt
  12657. todo marker entry priority)
  12658. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12659. (setq action (list 'lambda nil action)))
  12660. (save-excursion
  12661. (goto-char (point-min))
  12662. (when (eq action 'sparse-tree)
  12663. (org-overview)
  12664. (org-remove-occur-highlights))
  12665. (while (let (case-fold-search)
  12666. (re-search-forward re nil t))
  12667. (setq org-map-continue-from nil)
  12668. (catch :skip
  12669. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12670. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12671. (goto-char (setq lspos (match-beginning 0)))
  12672. (setq level (org-reduced-level (org-outline-level))
  12673. category (org-get-category))
  12674. (setq i llast llast level)
  12675. ;; remove tag lists from same and sublevels
  12676. (while (>= i level)
  12677. (when (setq entry (assoc i tags-alist))
  12678. (setq tags-alist (delete entry tags-alist)))
  12679. (setq i (1- i)))
  12680. ;; add the next tags
  12681. (when tags
  12682. (setq tags (org-split-string tags ":")
  12683. tags-alist
  12684. (cons (cons level tags) tags-alist)))
  12685. ;; compile tags for current headline
  12686. (setq tags-list
  12687. (if org-use-tag-inheritance
  12688. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12689. tags)
  12690. org-scanner-tags tags-list)
  12691. (when org-use-tag-inheritance
  12692. (setcdr (car tags-alist)
  12693. (mapcar (lambda (x)
  12694. (setq x (copy-sequence x))
  12695. (org-add-prop-inherited x))
  12696. (cdar tags-alist))))
  12697. (when (and tags org-use-tag-inheritance
  12698. (or (not (eq t org-use-tag-inheritance))
  12699. org-tags-exclude-from-inheritance))
  12700. ;; selective inheritance, remove uninherited ones
  12701. (setcdr (car tags-alist)
  12702. (org-remove-uninherited-tags (cdar tags-alist))))
  12703. (when (and
  12704. ;; eval matcher only when the todo condition is OK
  12705. (and (or (not todo-only) (member todo org-not-done-keywords))
  12706. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12707. (eval matcher)))
  12708. ;; Call the skipper, but return t if it does not skip,
  12709. ;; so that the `and' form continues evaluating
  12710. (progn
  12711. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12712. t)
  12713. ;; Check if timestamps are deselecting this entry
  12714. (or (not todo-only)
  12715. (and (member todo org-not-done-keywords)
  12716. (or (not org-agenda-tags-todo-honor-ignore-options)
  12717. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12718. ;; select this headline
  12719. (cond
  12720. ((eq action 'sparse-tree)
  12721. (and org-highlight-sparse-tree-matches
  12722. (org-get-heading) (match-end 0)
  12723. (org-highlight-new-match
  12724. (match-beginning 1) (match-end 1)))
  12725. (org-show-context 'tags-tree))
  12726. ((eq action 'agenda)
  12727. (setq txt (org-agenda-format-item
  12728. ""
  12729. (concat
  12730. (if (eq org-tags-match-list-sublevels 'indented)
  12731. (make-string (1- level) ?.) "")
  12732. (org-get-heading))
  12733. level category
  12734. tags-list)
  12735. priority (org-get-priority txt))
  12736. (goto-char lspos)
  12737. (setq marker (org-agenda-new-marker))
  12738. (org-add-props txt props
  12739. 'org-marker marker 'org-hd-marker marker 'org-category category
  12740. 'todo-state todo
  12741. 'priority priority 'type "tagsmatch")
  12742. (push txt rtn))
  12743. ((functionp action)
  12744. (setq org-map-continue-from nil)
  12745. (save-excursion
  12746. (setq rtn1 (funcall action))
  12747. (push rtn1 rtn)))
  12748. (t (user-error "Invalid action")))
  12749. ;; if we are to skip sublevels, jump to end of subtree
  12750. (unless org-tags-match-list-sublevels
  12751. (org-end-of-subtree t)
  12752. (backward-char 1))))
  12753. ;; Get the correct position from where to continue
  12754. (if org-map-continue-from
  12755. (goto-char org-map-continue-from)
  12756. (and (= (point) lspos) (end-of-line 1)))))
  12757. (when (and (eq action 'sparse-tree)
  12758. (not org-sparse-tree-open-archived-trees))
  12759. (org-hide-archived-subtrees (point-min) (point-max)))
  12760. (nreverse rtn)))
  12761. (defun org-remove-uninherited-tags (tags)
  12762. "Remove all tags that are not inherited from the list TAGS."
  12763. (cond
  12764. ((eq org-use-tag-inheritance t)
  12765. (if org-tags-exclude-from-inheritance
  12766. (org-delete-all org-tags-exclude-from-inheritance tags)
  12767. tags))
  12768. ((not org-use-tag-inheritance) nil)
  12769. ((stringp org-use-tag-inheritance)
  12770. (delq nil (mapcar
  12771. (lambda (x)
  12772. (if (and (string-match org-use-tag-inheritance x)
  12773. (not (member x org-tags-exclude-from-inheritance)))
  12774. x nil))
  12775. tags)))
  12776. ((listp org-use-tag-inheritance)
  12777. (delq nil (mapcar
  12778. (lambda (x)
  12779. (if (member x org-use-tag-inheritance) x nil))
  12780. tags)))))
  12781. (defun org-match-sparse-tree (&optional todo-only match)
  12782. "Create a sparse tree according to tags string MATCH.
  12783. MATCH can contain positive and negative selection of tags, like
  12784. \"+WORK+URGENT-WITHBOSS\".
  12785. If optional argument TODO-ONLY is non-nil, only select lines that are
  12786. also TODO lines."
  12787. (interactive "P")
  12788. (org-agenda-prepare-buffers (list (current-buffer)))
  12789. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12790. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12791. (defvar org-cached-props nil)
  12792. (defun org-cached-entry-get (pom property)
  12793. (if (or (eq t org-use-property-inheritance)
  12794. (and (stringp org-use-property-inheritance)
  12795. (string-match org-use-property-inheritance property))
  12796. (and (listp org-use-property-inheritance)
  12797. (member property org-use-property-inheritance)))
  12798. ;; Caching is not possible, check it directly
  12799. (org-entry-get pom property 'inherit)
  12800. ;; Get all properties, so that we can do complicated checks easily
  12801. (cdr (assoc property (or org-cached-props
  12802. (setq org-cached-props
  12803. (org-entry-properties pom)))))))
  12804. (defun org-global-tags-completion-table (&optional files)
  12805. "Return the list of all tags in all agenda buffer/files.
  12806. Optional FILES argument is a list of files which can be used
  12807. instead of the agenda files."
  12808. (save-excursion
  12809. (org-uniquify
  12810. (delq nil
  12811. (apply 'append
  12812. (mapcar
  12813. (lambda (file)
  12814. (set-buffer (find-file-noselect file))
  12815. (append (org-get-buffer-tags)
  12816. (mapcar (lambda (x) (if (stringp (car-safe x))
  12817. (list (car-safe x)) nil))
  12818. org-tag-alist)))
  12819. (if (and files (car files))
  12820. files
  12821. (org-agenda-files))))))))
  12822. (defun org-make-tags-matcher (match)
  12823. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12824. The variable `todo-only' is scoped dynamically into this function.
  12825. It will be set to t if the matcher restricts matching to TODO entries,
  12826. otherwise will not be touched.
  12827. Returns a cons of the selection string MATCH and the constructed
  12828. lisp form implementing the matcher. The matcher is to be evaluated
  12829. at an Org entry, with point on the headline, and returns t if the
  12830. entry matches the selection string MATCH. The returned lisp form
  12831. references two variables with information about the entry, which
  12832. must be bound around the form's evaluation: todo, the TODO keyword
  12833. at the entry (or nil of none); and tags-list, the list of all tags
  12834. at the entry including inherited ones. Additionally, the category
  12835. of the entry (if any) must be specified as the text property
  12836. 'org-category on the headline.
  12837. See also `org-scan-tags'.
  12838. "
  12839. (declare (special todo-only))
  12840. (unless (boundp 'todo-only)
  12841. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12842. (unless match
  12843. ;; Get a new match request, with completion against the global
  12844. ;; tags table and the local tags in current buffer
  12845. (let ((org-last-tags-completion-table
  12846. (org-uniquify
  12847. (delq nil (append (org-get-buffer-tags)
  12848. (org-global-tags-completion-table))))))
  12849. (setq match (org-completing-read-no-i
  12850. "Match: " 'org-tags-completion-function nil nil nil
  12851. 'org-tags-history))))
  12852. ;; Parse the string and create a lisp form
  12853. (let ((match0 match)
  12854. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12855. minus tag mm
  12856. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12857. orterms term orlist re-p str-p level-p level-op time-p
  12858. prop-p pn pv po gv rest (start 0) (ss 0))
  12859. ;; Expand group tags
  12860. (setq match (org-tags-expand match))
  12861. ;; Check if there is a TODO part of this match, which would be the
  12862. ;; part after a "/". TO make sure that this slash is not part of
  12863. ;; a property value to be matched against, we also check that there
  12864. ;; is no " after that slash.
  12865. ;; First, find the last slash
  12866. (while (string-match "/+" match ss)
  12867. (setq start (match-beginning 0) ss (match-end 0)))
  12868. (if (and (string-match "/+" match start)
  12869. (not (save-match-data (string-match "\"" match start))))
  12870. ;; match contains also a todo-matching request
  12871. (progn
  12872. (setq tagsmatch (substring match 0 (match-beginning 0))
  12873. todomatch (substring match (match-end 0)))
  12874. (if (string-match "^!" todomatch)
  12875. (setq todo-only t todomatch (substring todomatch 1)))
  12876. (if (string-match "^\\s-*$" todomatch)
  12877. (setq todomatch nil)))
  12878. ;; only matching tags
  12879. (setq tagsmatch match todomatch nil))
  12880. ;; Make the tags matcher
  12881. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12882. (setq tagsmatcher t)
  12883. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12884. (while (setq term (pop orterms))
  12885. (while (and (equal (substring term -1) "\\") orterms)
  12886. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12887. (while (string-match re term)
  12888. (setq rest (substring term (match-end 0))
  12889. minus (and (match-end 1)
  12890. (equal (match-string 1 term) "-"))
  12891. tag (save-match-data (replace-regexp-in-string
  12892. "\\\\-" "-"
  12893. (match-string 2 term)))
  12894. re-p (equal (string-to-char tag) ?{)
  12895. level-p (match-end 4)
  12896. prop-p (match-end 5)
  12897. mm (cond
  12898. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12899. (level-p
  12900. (setq level-op (org-op-to-function (match-string 3 term)))
  12901. `(,level-op level ,(string-to-number
  12902. (match-string 4 term))))
  12903. (prop-p
  12904. (setq pn (match-string 5 term)
  12905. po (match-string 6 term)
  12906. pv (match-string 7 term)
  12907. re-p (equal (string-to-char pv) ?{)
  12908. str-p (equal (string-to-char pv) ?\")
  12909. time-p (save-match-data
  12910. (string-match "^\"[[<].*[]>]\"$" pv))
  12911. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12912. (if time-p (setq pv (org-matcher-time pv)))
  12913. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12914. (cond
  12915. ((equal pn "CATEGORY")
  12916. (setq gv '(get-text-property (point) 'org-category)))
  12917. ((equal pn "TODO")
  12918. (setq gv 'todo))
  12919. (t
  12920. (setq gv `(org-cached-entry-get nil ,pn))))
  12921. (if re-p
  12922. (if (eq po 'org<>)
  12923. `(not (string-match ,pv (or ,gv "")))
  12924. `(string-match ,pv (or ,gv "")))
  12925. (if str-p
  12926. `(,po (or ,gv "") ,pv)
  12927. `(,po (string-to-number (or ,gv ""))
  12928. ,(string-to-number pv) ))))
  12929. (t `(member ,tag tags-list)))
  12930. mm (if minus (list 'not mm) mm)
  12931. term rest)
  12932. (push mm tagsmatcher))
  12933. (push (if (> (length tagsmatcher) 1)
  12934. (cons 'and tagsmatcher)
  12935. (car tagsmatcher))
  12936. orlist)
  12937. (setq tagsmatcher nil))
  12938. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12939. (setq tagsmatcher
  12940. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12941. ;; Make the todo matcher
  12942. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12943. (setq todomatcher t)
  12944. (setq orterms (org-split-string todomatch "|") orlist nil)
  12945. (while (setq term (pop orterms))
  12946. (while (string-match re term)
  12947. (setq minus (and (match-end 1)
  12948. (equal (match-string 1 term) "-"))
  12949. kwd (match-string 2 term)
  12950. re-p (equal (string-to-char kwd) ?{)
  12951. term (substring term (match-end 0))
  12952. mm (if re-p
  12953. `(string-match ,(substring kwd 1 -1) todo)
  12954. (list 'equal 'todo kwd))
  12955. mm (if minus (list 'not mm) mm))
  12956. (push mm todomatcher))
  12957. (push (if (> (length todomatcher) 1)
  12958. (cons 'and todomatcher)
  12959. (car todomatcher))
  12960. orlist)
  12961. (setq todomatcher nil))
  12962. (setq todomatcher (if (> (length orlist) 1)
  12963. (cons 'or orlist) (car orlist))))
  12964. ;; Return the string and lisp forms of the matcher
  12965. (setq matcher (if todomatcher
  12966. (list 'and tagsmatcher todomatcher)
  12967. tagsmatcher))
  12968. (when todo-only
  12969. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12970. matcher)))
  12971. (cons match0 matcher)))
  12972. (defun org-tags-expand (match &optional single-as-list downcased)
  12973. "Expand group tags in MATCH.
  12974. This replaces every group tag in MATCH with a regexp tag search.
  12975. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12976. will be replaced like this:
  12977. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12978. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12979. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12980. Replacing by a regexp preserves the structure of the match.
  12981. E.g., this expansion
  12982. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12983. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12984. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12985. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12986. assumed to be a single group tag, and the function will return
  12987. the list of tags in this group.
  12988. When DOWNCASE is non-nil, expand downcased TAGS."
  12989. (if org-group-tags
  12990. (let* ((case-fold-search t)
  12991. (stable org-mode-syntax-table)
  12992. (tal (or org-tag-groups-alist-for-agenda
  12993. org-tag-groups-alist))
  12994. (tal (if downcased
  12995. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12996. (tml (mapcar 'car tal))
  12997. (rtnmatch match) rpl)
  12998. ;; @ and _ are allowed as word-components in tags
  12999. (modify-syntax-entry ?@ "w" stable)
  13000. (modify-syntax-entry ?_ "w" stable)
  13001. (while (and tml
  13002. (with-syntax-table stable
  13003. (string-match
  13004. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13005. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13006. (let* ((dir (match-string 1 rtnmatch))
  13007. (tag (match-string 2 rtnmatch))
  13008. (tag (if downcased (downcase tag) tag)))
  13009. (setq tml (delete tag tml))
  13010. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13011. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13012. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13013. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13014. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13015. (if single-as-list
  13016. (or (reverse rpl) (list rtnmatch))
  13017. rtnmatch))
  13018. (if single-as-list (list (if downcased (downcase match) match))
  13019. match)))
  13020. (defun org-op-to-function (op &optional stringp)
  13021. "Turn an operator into the appropriate function."
  13022. (setq op
  13023. (cond
  13024. ((equal op "<" ) '(< string< org-time<))
  13025. ((equal op ">" ) '(> org-string> org-time>))
  13026. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13027. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13028. ((member op '("=" "==")) '(= string= org-time=))
  13029. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13030. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13031. (defun org<> (a b) (not (= a b)))
  13032. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13033. (defun org-string>= (a b) (not (string< a b)))
  13034. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13035. (defun org-string<> (a b) (not (string= a b)))
  13036. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13037. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13038. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13039. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13040. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13041. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13042. (defun org-2ft (s)
  13043. "Convert S to a floating point time.
  13044. If S is already a number, just return it. If it is a string, parse
  13045. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13046. (cond
  13047. ((numberp s) s)
  13048. ((stringp s)
  13049. (condition-case nil
  13050. (float-time (apply 'encode-time (org-parse-time-string s)))
  13051. (error 0.)))
  13052. (t 0.)))
  13053. (defun org-time-today ()
  13054. "Time in seconds today at 0:00.
  13055. Returns the float number of seconds since the beginning of the
  13056. epoch to the beginning of today (00:00)."
  13057. (float-time (apply 'encode-time
  13058. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13059. (defun org-matcher-time (s)
  13060. "Interpret a time comparison value."
  13061. (save-match-data
  13062. (cond
  13063. ((string= s "<now>") (float-time))
  13064. ((string= s "<today>") (org-time-today))
  13065. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13066. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13067. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13068. (+ (org-time-today)
  13069. (* (string-to-number (match-string 1 s))
  13070. (cdr (assoc (match-string 2 s)
  13071. '(("d" . 86400.0) ("w" . 604800.0)
  13072. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13073. (t (org-2ft s)))))
  13074. (defun org-match-any-p (re list)
  13075. "Does re match any element of list?"
  13076. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13077. (delq nil list))
  13078. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13079. (defvar org-tags-overlay (make-overlay 1 1))
  13080. (org-detach-overlay org-tags-overlay)
  13081. (defun org-get-local-tags-at (&optional pos)
  13082. "Get a list of tags defined in the current headline."
  13083. (org-get-tags-at pos 'local))
  13084. (defun org-get-local-tags ()
  13085. "Get a list of tags defined in the current headline."
  13086. (org-get-tags-at nil 'local))
  13087. (defun org-get-tags-at (&optional pos local)
  13088. "Get a list of all headline tags applicable at POS.
  13089. POS defaults to point. If tags are inherited, the list contains
  13090. the targets in the same sequence as the headlines appear, i.e.
  13091. the tags of the current headline come last.
  13092. When LOCAL is non-nil, only return tags from the current headline,
  13093. ignore inherited ones."
  13094. (interactive)
  13095. (if (and org-trust-scanner-tags
  13096. (or (not pos) (equal pos (point)))
  13097. (not local))
  13098. org-scanner-tags
  13099. (let (tags ltags lastpos parent)
  13100. (save-excursion
  13101. (save-restriction
  13102. (widen)
  13103. (goto-char (or pos (point)))
  13104. (save-match-data
  13105. (catch 'done
  13106. (condition-case nil
  13107. (progn
  13108. (org-back-to-heading t)
  13109. (while (not (equal lastpos (point)))
  13110. (setq lastpos (point))
  13111. (when (looking-at
  13112. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13113. (setq ltags (org-split-string
  13114. (org-match-string-no-properties 1) ":"))
  13115. (when parent
  13116. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13117. (setq tags (append
  13118. (if parent
  13119. (org-remove-uninherited-tags ltags)
  13120. ltags)
  13121. tags)))
  13122. (or org-use-tag-inheritance (throw 'done t))
  13123. (if local (throw 'done t))
  13124. (or (org-up-heading-safe) (error nil))
  13125. (setq parent t)))
  13126. (error nil)))))
  13127. (if local
  13128. tags
  13129. (reverse (delete-dups
  13130. (reverse (append
  13131. (org-remove-uninherited-tags
  13132. org-file-tags) tags)))))))))
  13133. (defun org-add-prop-inherited (s)
  13134. (add-text-properties 0 (length s) '(inherited t) s)
  13135. s)
  13136. (defun org-toggle-tag (tag &optional onoff)
  13137. "Toggle the tag TAG for the current line.
  13138. If ONOFF is `on' or `off', don't toggle but set to this state."
  13139. (let (res current)
  13140. (save-excursion
  13141. (org-back-to-heading t)
  13142. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13143. (point-at-eol) t)
  13144. (progn
  13145. (setq current (match-string 1))
  13146. (replace-match ""))
  13147. (setq current ""))
  13148. (setq current (nreverse (org-split-string current ":")))
  13149. (cond
  13150. ((eq onoff 'on)
  13151. (setq res t)
  13152. (or (member tag current) (push tag current)))
  13153. ((eq onoff 'off)
  13154. (or (not (member tag current)) (setq current (delete tag current))))
  13155. (t (if (member tag current)
  13156. (setq current (delete tag current))
  13157. (setq res t)
  13158. (push tag current))))
  13159. (end-of-line 1)
  13160. (if current
  13161. (progn
  13162. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13163. (org-set-tags nil t))
  13164. (delete-horizontal-space))
  13165. (run-hooks 'org-after-tags-change-hook))
  13166. res))
  13167. (defun org-align-tags-here (to-col)
  13168. ;; Assumes that this is a headline
  13169. "Align tags on the current headline to TO-COL."
  13170. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13171. (beginning-of-line 1)
  13172. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13173. (< pos (match-beginning 2)))
  13174. (progn
  13175. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13176. (goto-char (match-beginning 1))
  13177. (insert " ")
  13178. (delete-region (point) (1+ (match-beginning 2)))
  13179. (setq ncol (max (current-column)
  13180. (1+ col)
  13181. (if (> to-col 0)
  13182. to-col
  13183. (- (abs to-col) tags-l))))
  13184. (setq p (point))
  13185. (insert (make-string (- ncol (current-column)) ?\ ))
  13186. (setq ncol (current-column))
  13187. (when indent-tabs-mode (tabify p (point-at-eol)))
  13188. (org-move-to-column (min ncol col)))
  13189. (goto-char pos))))
  13190. (defun org-set-tags-command (&optional arg just-align)
  13191. "Call the set-tags command for the current entry."
  13192. (interactive "P")
  13193. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13194. (org-set-tags arg just-align)
  13195. (save-excursion
  13196. (unless (and (org-region-active-p)
  13197. org-loop-over-headlines-in-active-region)
  13198. (org-back-to-heading t))
  13199. (org-set-tags arg just-align))))
  13200. (defun org-set-tags-to (data)
  13201. "Set the tags of the current entry to DATA, replacing the current tags.
  13202. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13203. If DATA is nil or the empty string, any tags will be removed."
  13204. (interactive "sTags: ")
  13205. (setq data
  13206. (cond
  13207. ((eq data nil) "")
  13208. ((equal data "") "")
  13209. ((stringp data)
  13210. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13211. ":"))
  13212. ((listp data)
  13213. (concat ":" (mapconcat 'identity data ":") ":"))))
  13214. (when data
  13215. (save-excursion
  13216. (org-back-to-heading t)
  13217. (when (looking-at org-complex-heading-regexp)
  13218. (if (match-end 5)
  13219. (progn
  13220. (goto-char (match-beginning 5))
  13221. (insert data)
  13222. (delete-region (point) (point-at-eol))
  13223. (org-set-tags nil 'align))
  13224. (goto-char (point-at-eol))
  13225. (insert " " data)
  13226. (org-set-tags nil 'align)))
  13227. (beginning-of-line 1)
  13228. (if (looking-at ".*?\\([ \t]+\\)$")
  13229. (delete-region (match-beginning 1) (match-end 1))))))
  13230. (defun org-align-all-tags ()
  13231. "Align the tags i all headings."
  13232. (interactive)
  13233. (save-excursion
  13234. (or (ignore-errors (org-back-to-heading t))
  13235. (outline-next-heading))
  13236. (if (org-at-heading-p)
  13237. (org-set-tags t)
  13238. (message "No headings"))))
  13239. (defvar org-indent-indentation-per-level)
  13240. (defun org-set-tags (&optional arg just-align)
  13241. "Set the tags for the current headline.
  13242. With prefix ARG, realign all tags in headings in the current buffer.
  13243. When JUST-ALIGN is non-nil, only align tags."
  13244. (interactive "P")
  13245. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13246. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13247. 'region-start-level 'region))
  13248. org-loop-over-headlines-in-active-region)
  13249. (org-map-entries
  13250. ;; We don't use ARG and JUST-ALIGN here because these args
  13251. ;; are not useful when looping over headlines.
  13252. `(org-set-tags)
  13253. org-loop-over-headlines-in-active-region
  13254. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13255. (let* ((re org-outline-regexp-bol)
  13256. (current (unless arg (org-get-tags-string)))
  13257. (col (current-column))
  13258. (org-setting-tags t)
  13259. table current-tags inherited-tags ; computed below when needed
  13260. tags p0 c0 c1 rpl di tc level)
  13261. (if arg
  13262. (save-excursion
  13263. (goto-char (point-min))
  13264. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13265. (while (re-search-forward re nil t)
  13266. (org-set-tags nil t)
  13267. (end-of-line 1)))
  13268. (message "All tags realigned to column %d" org-tags-column))
  13269. (if just-align
  13270. (setq tags current)
  13271. ;; Get a new set of tags from the user
  13272. (save-excursion
  13273. (setq table (append org-tag-persistent-alist
  13274. (or org-tag-alist (org-get-buffer-tags))
  13275. (and
  13276. org-complete-tags-always-offer-all-agenda-tags
  13277. (org-global-tags-completion-table
  13278. (org-agenda-files))))
  13279. org-last-tags-completion-table table
  13280. current-tags (org-split-string current ":")
  13281. inherited-tags (nreverse
  13282. (nthcdr (length current-tags)
  13283. (nreverse (org-get-tags-at))))
  13284. tags
  13285. (if (or (eq t org-use-fast-tag-selection)
  13286. (and org-use-fast-tag-selection
  13287. (delq nil (mapcar 'cdr table))))
  13288. (org-fast-tag-selection
  13289. current-tags inherited-tags table
  13290. (if org-fast-tag-selection-include-todo
  13291. org-todo-key-alist))
  13292. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13293. (org-trim
  13294. (org-icompleting-read "Tags: "
  13295. 'org-tags-completion-function
  13296. nil nil current 'org-tags-history))))))
  13297. (while (string-match "[-+&]+" tags)
  13298. ;; No boolean logic, just a list
  13299. (setq tags (replace-match ":" t t tags))))
  13300. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13301. (if org-tags-sort-function
  13302. (setq tags (mapconcat 'identity
  13303. (sort (org-split-string
  13304. tags (org-re "[^[:alnum:]_@#%]+"))
  13305. org-tags-sort-function) ":")))
  13306. (if (string-match "\\`[\t ]*\\'" tags)
  13307. (setq tags "")
  13308. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13309. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13310. ;; Insert new tags at the correct column
  13311. (beginning-of-line 1)
  13312. (setq level (or (and (looking-at org-outline-regexp)
  13313. (- (match-end 0) (point) 1))
  13314. 1))
  13315. (cond
  13316. ((and (equal current "") (equal tags "")))
  13317. ((re-search-forward
  13318. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13319. (point-at-eol) t)
  13320. (if (equal tags "")
  13321. (setq rpl "")
  13322. (goto-char (match-beginning 0))
  13323. (setq c0 (current-column)
  13324. ;; compute offset for the case of org-indent-mode active
  13325. di (if (org-bound-and-true-p org-indent-mode)
  13326. (* (1- org-indent-indentation-per-level) (1- level))
  13327. 0)
  13328. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13329. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13330. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13331. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13332. (replace-match rpl t t)
  13333. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13334. tags)
  13335. (t (error "Tags alignment failed")))
  13336. (org-move-to-column col)
  13337. (unless just-align
  13338. (run-hooks 'org-after-tags-change-hook))))))
  13339. (defun org-change-tag-in-region (beg end tag off)
  13340. "Add or remove TAG for each entry in the region.
  13341. This works in the agenda, and also in an org-mode buffer."
  13342. (interactive
  13343. (list (region-beginning) (region-end)
  13344. (let ((org-last-tags-completion-table
  13345. (if (derived-mode-p 'org-mode)
  13346. (org-uniquify
  13347. (delq nil (append (org-get-buffer-tags)
  13348. (org-global-tags-completion-table))))
  13349. (org-global-tags-completion-table))))
  13350. (org-icompleting-read
  13351. "Tag: " 'org-tags-completion-function nil nil nil
  13352. 'org-tags-history))
  13353. (progn
  13354. (message "[s]et or [r]emove? ")
  13355. (equal (read-char-exclusive) ?r))))
  13356. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13357. (let ((agendap (equal major-mode 'org-agenda-mode))
  13358. l1 l2 m buf pos newhead (cnt 0))
  13359. (goto-char end)
  13360. (setq l2 (1- (org-current-line)))
  13361. (goto-char beg)
  13362. (setq l1 (org-current-line))
  13363. (loop for l from l1 to l2 do
  13364. (org-goto-line l)
  13365. (setq m (get-text-property (point) 'org-hd-marker))
  13366. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13367. (and agendap m))
  13368. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13369. pos (if agendap m (point)))
  13370. (with-current-buffer buf
  13371. (save-excursion
  13372. (save-restriction
  13373. (goto-char pos)
  13374. (setq cnt (1+ cnt))
  13375. (org-toggle-tag tag (if off 'off 'on))
  13376. (setq newhead (org-get-heading)))))
  13377. (and agendap (org-agenda-change-all-lines newhead m))))
  13378. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13379. (defun org-tags-completion-function (string predicate &optional flag)
  13380. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13381. (confirm (lambda (x) (stringp (car x)))))
  13382. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13383. (setq s1 (match-string 1 string)
  13384. s2 (match-string 2 string))
  13385. (setq s1 "" s2 string))
  13386. (cond
  13387. ((eq flag nil)
  13388. ;; try completion
  13389. (setq rtn (try-completion s2 ctable confirm))
  13390. (if (stringp rtn)
  13391. (setq rtn
  13392. (concat s1 s2 (substring rtn (length s2))
  13393. (if (and org-add-colon-after-tag-completion
  13394. (assoc rtn ctable))
  13395. ":" ""))))
  13396. rtn)
  13397. ((eq flag t)
  13398. ;; all-completions
  13399. (all-completions s2 ctable confirm))
  13400. ((eq flag 'lambda)
  13401. ;; exact match?
  13402. (assoc s2 ctable)))))
  13403. (defun org-fast-tag-insert (kwd tags face &optional end)
  13404. "Insert KDW, and the TAGS, the latter with face FACE.
  13405. Also insert END."
  13406. (insert (format "%-12s" (concat kwd ":"))
  13407. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13408. (or end "")))
  13409. (defun org-fast-tag-show-exit (flag)
  13410. (save-excursion
  13411. (org-goto-line 3)
  13412. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13413. (replace-match ""))
  13414. (when flag
  13415. (end-of-line 1)
  13416. (org-move-to-column (- (window-width) 19) t)
  13417. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13418. (defun org-set-current-tags-overlay (current prefix)
  13419. "Add an overlay to CURRENT tag with PREFIX."
  13420. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13421. (if (featurep 'xemacs)
  13422. (org-overlay-display org-tags-overlay (concat prefix s)
  13423. 'secondary-selection)
  13424. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13425. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13426. (defvar org-last-tag-selection-key nil)
  13427. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13428. "Fast tag selection with single keys.
  13429. CURRENT is the current list of tags in the headline, INHERITED is the
  13430. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13431. possibly with grouping information. TODO-TABLE is a similar table with
  13432. TODO keywords, should these have keys assigned to them.
  13433. If the keys are nil, a-z are automatically assigned.
  13434. Returns the new tags string, or nil to not change the current settings."
  13435. (let* ((fulltable (append table todo-table))
  13436. (maxlen (apply 'max (mapcar
  13437. (lambda (x)
  13438. (if (stringp (car x)) (string-width (car x)) 0))
  13439. fulltable)))
  13440. (buf (current-buffer))
  13441. (expert (eq org-fast-tag-selection-single-key 'expert))
  13442. (buffer-tags nil)
  13443. (fwidth (+ maxlen 3 1 3))
  13444. (ncol (/ (- (window-width) 4) fwidth))
  13445. (i-face 'org-done)
  13446. (c-face 'org-todo)
  13447. tg cnt e c char c1 c2 ntable tbl rtn
  13448. ov-start ov-end ov-prefix
  13449. (exit-after-next org-fast-tag-selection-single-key)
  13450. (done-keywords org-done-keywords)
  13451. groups ingroup)
  13452. (save-excursion
  13453. (beginning-of-line 1)
  13454. (if (looking-at
  13455. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13456. (setq ov-start (match-beginning 1)
  13457. ov-end (match-end 1)
  13458. ov-prefix "")
  13459. (setq ov-start (1- (point-at-eol))
  13460. ov-end (1+ ov-start))
  13461. (skip-chars-forward "^\n\r")
  13462. (setq ov-prefix
  13463. (concat
  13464. (buffer-substring (1- (point)) (point))
  13465. (if (> (current-column) org-tags-column)
  13466. " "
  13467. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13468. (move-overlay org-tags-overlay ov-start ov-end)
  13469. (save-window-excursion
  13470. (if expert
  13471. (set-buffer (get-buffer-create " *Org tags*"))
  13472. (delete-other-windows)
  13473. (split-window-vertically)
  13474. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13475. (erase-buffer)
  13476. (org-set-local 'org-done-keywords done-keywords)
  13477. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13478. (org-fast-tag-insert "Current" current c-face "\n\n")
  13479. (org-fast-tag-show-exit exit-after-next)
  13480. (org-set-current-tags-overlay current ov-prefix)
  13481. (setq tbl fulltable char ?a cnt 0)
  13482. (while (setq e (pop tbl))
  13483. (cond
  13484. ((equal (car e) :startgroup)
  13485. (push '() groups) (setq ingroup t)
  13486. (when (not (= cnt 0))
  13487. (setq cnt 0)
  13488. (insert "\n"))
  13489. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13490. ((equal (car e) :endgroup)
  13491. (setq ingroup nil cnt 0)
  13492. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13493. ((equal e '(:newline))
  13494. (when (not (= cnt 0))
  13495. (setq cnt 0)
  13496. (insert "\n")
  13497. (setq e (car tbl))
  13498. (while (equal (car tbl) '(:newline))
  13499. (insert "\n")
  13500. (setq tbl (cdr tbl)))))
  13501. ((equal e '(:grouptags)) nil)
  13502. (t
  13503. (setq tg (copy-sequence (car e)) c2 nil)
  13504. (if (cdr e)
  13505. (setq c (cdr e))
  13506. ;; automatically assign a character.
  13507. (setq c1 (string-to-char
  13508. (downcase (substring
  13509. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13510. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13511. (while (or (rassoc char ntable) (rassoc char table))
  13512. (setq char (1+ char)))
  13513. (setq c2 c1))
  13514. (setq c (or c2 char)))
  13515. (if ingroup (push tg (car groups)))
  13516. (setq tg (org-add-props tg nil 'face
  13517. (cond
  13518. ((not (assoc tg table))
  13519. (org-get-todo-face tg))
  13520. ((member tg current) c-face)
  13521. ((member tg inherited) i-face))))
  13522. (if (equal (caar tbl) :grouptags)
  13523. (org-add-props tg nil 'face 'org-tag-group))
  13524. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13525. (insert "[" c "] " tg (make-string
  13526. (- fwidth 4 (length tg)) ?\ ))
  13527. (push (cons tg c) ntable)
  13528. (when (= (setq cnt (1+ cnt)) ncol)
  13529. (insert "\n")
  13530. (if ingroup (insert " "))
  13531. (setq cnt 0)))))
  13532. (setq ntable (nreverse ntable))
  13533. (insert "\n")
  13534. (goto-char (point-min))
  13535. (if (not expert) (org-fit-window-to-buffer))
  13536. (setq rtn
  13537. (catch 'exit
  13538. (while t
  13539. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13540. (if (not groups) "no " "")
  13541. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13542. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13543. (setq org-last-tag-selection-key c)
  13544. (cond
  13545. ((= c ?\r) (throw 'exit t))
  13546. ((= c ?!)
  13547. (setq groups (not groups))
  13548. (goto-char (point-min))
  13549. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13550. ((= c ?\C-c)
  13551. (if (not expert)
  13552. (org-fast-tag-show-exit
  13553. (setq exit-after-next (not exit-after-next)))
  13554. (setq expert nil)
  13555. (delete-other-windows)
  13556. (set-window-buffer (split-window-vertically) " *Org tags*")
  13557. (org-switch-to-buffer-other-window " *Org tags*")
  13558. (org-fit-window-to-buffer)))
  13559. ((or (= c ?\C-g)
  13560. (and (= c ?q) (not (rassoc c ntable))))
  13561. (org-detach-overlay org-tags-overlay)
  13562. (setq quit-flag t))
  13563. ((= c ?\ )
  13564. (setq current nil)
  13565. (if exit-after-next (setq exit-after-next 'now)))
  13566. ((= c ?\t)
  13567. (condition-case nil
  13568. (setq tg (org-icompleting-read
  13569. "Tag: "
  13570. (or buffer-tags
  13571. (with-current-buffer buf
  13572. (org-get-buffer-tags)))))
  13573. (quit (setq tg "")))
  13574. (when (string-match "\\S-" tg)
  13575. (add-to-list 'buffer-tags (list tg))
  13576. (if (member tg current)
  13577. (setq current (delete tg current))
  13578. (push tg current)))
  13579. (if exit-after-next (setq exit-after-next 'now)))
  13580. ((setq e (rassoc c todo-table) tg (car e))
  13581. (with-current-buffer buf
  13582. (save-excursion (org-todo tg)))
  13583. (if exit-after-next (setq exit-after-next 'now)))
  13584. ((setq e (rassoc c ntable) tg (car e))
  13585. (if (member tg current)
  13586. (setq current (delete tg current))
  13587. (loop for g in groups do
  13588. (if (member tg g)
  13589. (mapc (lambda (x)
  13590. (setq current (delete x current)))
  13591. g)))
  13592. (push tg current))
  13593. (if exit-after-next (setq exit-after-next 'now))))
  13594. ;; Create a sorted list
  13595. (setq current
  13596. (sort current
  13597. (lambda (a b)
  13598. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13599. (if (eq exit-after-next 'now) (throw 'exit t))
  13600. (goto-char (point-min))
  13601. (beginning-of-line 2)
  13602. (delete-region (point) (point-at-eol))
  13603. (org-fast-tag-insert "Current" current c-face)
  13604. (org-set-current-tags-overlay current ov-prefix)
  13605. (while (re-search-forward
  13606. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13607. (setq tg (match-string 1))
  13608. (add-text-properties
  13609. (match-beginning 1) (match-end 1)
  13610. (list 'face
  13611. (cond
  13612. ((member tg current) c-face)
  13613. ((member tg inherited) i-face)
  13614. (t (get-text-property (match-beginning 1) 'face))))))
  13615. (goto-char (point-min)))))
  13616. (org-detach-overlay org-tags-overlay)
  13617. (if rtn
  13618. (mapconcat 'identity current ":")
  13619. nil))))
  13620. (defun org-get-tags-string ()
  13621. "Get the TAGS string in the current headline."
  13622. (unless (org-at-heading-p t)
  13623. (user-error "Not on a heading"))
  13624. (save-excursion
  13625. (beginning-of-line 1)
  13626. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13627. (org-match-string-no-properties 1)
  13628. "")))
  13629. (defun org-get-tags ()
  13630. "Get the list of tags specified in the current headline."
  13631. (org-split-string (org-get-tags-string) ":"))
  13632. (defun org-get-buffer-tags ()
  13633. "Get a table of all tags used in the buffer, for completion."
  13634. (let (tags)
  13635. (save-excursion
  13636. (goto-char (point-min))
  13637. (while (re-search-forward
  13638. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13639. (when (equal (char-after (point-at-bol 0)) ?*)
  13640. (mapc (lambda (x) (add-to-list 'tags x))
  13641. (org-split-string (org-match-string-no-properties 1) ":")))))
  13642. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13643. (mapcar 'list tags)))
  13644. ;;;; The mapping API
  13645. (defun org-map-entries (func &optional match scope &rest skip)
  13646. "Call FUNC at each headline selected by MATCH in SCOPE.
  13647. FUNC is a function or a lisp form. The function will be called without
  13648. arguments, with the cursor positioned at the beginning of the headline.
  13649. The return values of all calls to the function will be collected and
  13650. returned as a list.
  13651. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13652. does not need to preserve point. After evaluation, the cursor will be
  13653. moved to the end of the line (presumably of the headline of the
  13654. processed entry) and search continues from there. Under some
  13655. circumstances, this may not produce the wanted results. For example,
  13656. if you have removed (e.g. archived) the current (sub)tree it could
  13657. mean that the next entry will be skipped entirely. In such cases, you
  13658. can specify the position from where search should continue by making
  13659. FUNC set the variable `org-map-continue-from' to the desired buffer
  13660. position.
  13661. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13662. Only headlines that are matched by this query will be considered during
  13663. the iteration. When MATCH is nil or t, all headlines will be
  13664. visited by the iteration.
  13665. SCOPE determines the scope of this command. It can be any of:
  13666. nil The current buffer, respecting the restriction if any
  13667. tree The subtree started with the entry at point
  13668. region The entries within the active region, if any
  13669. region-start-level
  13670. The entries within the active region, but only those at
  13671. the same level than the first one.
  13672. file The current buffer, without restriction
  13673. file-with-archives
  13674. The current buffer, and any archives associated with it
  13675. agenda All agenda files
  13676. agenda-with-archives
  13677. All agenda files with any archive files associated with them
  13678. \(file1 file2 ...)
  13679. If this is a list, all files in the list will be scanned
  13680. The remaining args are treated as settings for the skipping facilities of
  13681. the scanner. The following items can be given here:
  13682. archive skip trees with the archive tag
  13683. comment skip trees with the COMMENT keyword
  13684. function or Emacs Lisp form:
  13685. will be used as value for `org-agenda-skip-function', so
  13686. whenever the function returns a position, FUNC will not be
  13687. called for that entry and search will continue from the
  13688. position returned
  13689. If your function needs to retrieve the tags including inherited tags
  13690. at the *current* entry, you can use the value of the variable
  13691. `org-scanner-tags' which will be much faster than getting the value
  13692. with `org-get-tags-at'. If your function gets properties with
  13693. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13694. to t around the call to `org-entry-properties' to get the same speedup.
  13695. Note that if your function moves around to retrieve tags and properties at
  13696. a *different* entry, you cannot use these techniques."
  13697. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13698. (not (org-region-active-p)))
  13699. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13700. (org-agenda-skip-archived-trees (memq 'archive skip))
  13701. (org-agenda-skip-comment-trees (memq 'comment skip))
  13702. (org-agenda-skip-function
  13703. (car (org-delete-all '(comment archive) skip)))
  13704. (org-tags-match-list-sublevels t)
  13705. (start-level (eq scope 'region-start-level))
  13706. matcher file res
  13707. org-todo-keywords-for-agenda
  13708. org-done-keywords-for-agenda
  13709. org-todo-keyword-alist-for-agenda
  13710. org-drawers-for-agenda
  13711. org-tag-alist-for-agenda
  13712. todo-only)
  13713. (cond
  13714. ((eq match t) (setq matcher t))
  13715. ((eq match nil) (setq matcher t))
  13716. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13717. (save-excursion
  13718. (save-restriction
  13719. (cond ((eq scope 'tree)
  13720. (org-back-to-heading t)
  13721. (org-narrow-to-subtree)
  13722. (setq scope nil))
  13723. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13724. (org-region-active-p))
  13725. ;; If needed, set start-level to a string like "2"
  13726. (when start-level
  13727. (save-excursion
  13728. (goto-char (region-beginning))
  13729. (unless (org-at-heading-p) (outline-next-heading))
  13730. (setq start-level (org-current-level))))
  13731. (narrow-to-region (region-beginning)
  13732. (save-excursion
  13733. (goto-char (region-end))
  13734. (unless (and (bolp) (org-at-heading-p))
  13735. (outline-next-heading))
  13736. (point)))
  13737. (setq scope nil)))
  13738. (if (not scope)
  13739. (progn
  13740. (org-agenda-prepare-buffers
  13741. (list (buffer-file-name (current-buffer))))
  13742. (setq res (org-scan-tags func matcher todo-only start-level)))
  13743. ;; Get the right scope
  13744. (cond
  13745. ((and scope (listp scope) (symbolp (car scope)))
  13746. (setq scope (eval scope)))
  13747. ((eq scope 'agenda)
  13748. (setq scope (org-agenda-files t)))
  13749. ((eq scope 'agenda-with-archives)
  13750. (setq scope (org-agenda-files t))
  13751. (setq scope (org-add-archive-files scope)))
  13752. ((eq scope 'file)
  13753. (setq scope (list (buffer-file-name))))
  13754. ((eq scope 'file-with-archives)
  13755. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13756. (org-agenda-prepare-buffers scope)
  13757. (while (setq file (pop scope))
  13758. (with-current-buffer (org-find-base-buffer-visiting file)
  13759. (save-excursion
  13760. (save-restriction
  13761. (widen)
  13762. (goto-char (point-min))
  13763. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13764. res)))
  13765. ;;;; Properties
  13766. ;;; Setting and retrieving properties
  13767. (defconst org-special-properties
  13768. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13769. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13770. "The special properties valid in Org-mode.
  13771. These are properties that are not defined in the property drawer,
  13772. but in some other way.")
  13773. (defconst org-default-properties
  13774. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13775. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13776. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13777. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13778. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13779. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13780. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13781. "Some properties that are used by Org-mode for various purposes.
  13782. Being in this list makes sure that they are offered for completion.")
  13783. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13784. "Regular expression matching the first line of a property drawer.")
  13785. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13786. "Regular expression matching the last line of a property drawer.")
  13787. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13788. "Regular expression matching the first line of a property drawer.")
  13789. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13790. "Regular expression matching the first line of a property drawer.")
  13791. (defconst org-property-drawer-re
  13792. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13793. org-property-end-re "\\)\n?")
  13794. "Matches an entire property drawer.")
  13795. (defconst org-clock-drawer-re
  13796. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13797. org-property-end-re "\\)\n?")
  13798. "Matches an entire clock drawer.")
  13799. (defun org-property-action ()
  13800. "Do an action on properties."
  13801. (interactive)
  13802. (let (c)
  13803. (org-at-property-p)
  13804. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13805. (setq c (read-char-exclusive))
  13806. (cond
  13807. ((equal c ?s)
  13808. (call-interactively 'org-set-property))
  13809. ((equal c ?d)
  13810. (call-interactively 'org-delete-property))
  13811. ((equal c ?D)
  13812. (call-interactively 'org-delete-property-globally))
  13813. ((equal c ?c)
  13814. (call-interactively 'org-compute-property-at-point))
  13815. (t (user-error "No such property action %c" c)))))
  13816. (defun org-inc-effort ()
  13817. "Increment the value of the effort property in the current entry."
  13818. (interactive)
  13819. (org-set-effort nil t))
  13820. (defvar org-clock-effort) ;; Defined in org-clock.el
  13821. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13822. (defun org-set-effort (&optional value increment)
  13823. "Set the effort property of the current entry.
  13824. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13825. 10th allowed value.
  13826. When INCREMENT is non-nil, set the property to the next allowed value."
  13827. (interactive "P")
  13828. (if (equal value 0) (setq value 10))
  13829. (let* ((completion-ignore-case t)
  13830. (prop org-effort-property)
  13831. (cur (org-entry-get nil prop))
  13832. (allowed (org-property-get-allowed-values nil prop 'table))
  13833. (existing (mapcar 'list (org-property-values prop)))
  13834. (heading (nth 4 (org-heading-components)))
  13835. rpl
  13836. (val (cond
  13837. ((stringp value) value)
  13838. ((and allowed (integerp value))
  13839. (or (car (nth (1- value) allowed))
  13840. (car (org-last allowed))))
  13841. ((and allowed increment)
  13842. (or (caadr (member (list cur) allowed))
  13843. (user-error "Allowed effort values are not set")))
  13844. (allowed
  13845. (message "Select 1-9,0, [RET%s]: %s"
  13846. (if cur (concat "=" cur) "")
  13847. (mapconcat 'car allowed " "))
  13848. (setq rpl (read-char-exclusive))
  13849. (if (equal rpl ?\r)
  13850. cur
  13851. (setq rpl (- rpl ?0))
  13852. (if (equal rpl 0) (setq rpl 10))
  13853. (if (and (> rpl 0) (<= rpl (length allowed)))
  13854. (car (nth (1- rpl) allowed))
  13855. (org-completing-read "Effort: " allowed nil))))
  13856. (t
  13857. (let (org-completion-use-ido org-completion-use-iswitchb)
  13858. (org-completing-read
  13859. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13860. (concat "[" cur "]") "")
  13861. ": ")
  13862. existing nil nil "" nil cur))))))
  13863. (unless (equal (org-entry-get nil prop) val)
  13864. (org-entry-put nil prop val))
  13865. (save-excursion
  13866. (org-back-to-heading t)
  13867. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13868. (when (string= heading org-clock-current-task)
  13869. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13870. (org-clock-update-mode-line))
  13871. (message "%s is now %s" prop val)))
  13872. (defun org-at-property-p ()
  13873. "Is cursor inside a property drawer?"
  13874. (save-excursion
  13875. (when (equal 'node-property (car (org-element-at-point)))
  13876. (beginning-of-line 1)
  13877. (looking-at org-property-re))))
  13878. (defun org-get-property-block (&optional beg end force)
  13879. "Return the (beg . end) range of the body of the property drawer.
  13880. BEG and END are the beginning and end of the current subtree, or of
  13881. the part before the first headline. If they are not given, they will
  13882. be found. If the drawer does not exist and FORCE is non-nil, create
  13883. the drawer."
  13884. (catch 'exit
  13885. (save-excursion
  13886. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13887. (progn (org-back-to-heading t) (point))))
  13888. (end (or end (and (not (outline-next-heading)) (point-max))
  13889. (point))))
  13890. (goto-char beg)
  13891. (if (re-search-forward org-property-start-re end t)
  13892. (setq beg (1+ (match-end 0)))
  13893. (if force
  13894. (save-excursion
  13895. (org-insert-property-drawer)
  13896. (setq end (progn (outline-next-heading) (point))))
  13897. (throw 'exit nil))
  13898. (goto-char beg)
  13899. (if (re-search-forward org-property-start-re end t)
  13900. (setq beg (1+ (match-end 0)))))
  13901. (if (re-search-forward org-property-end-re end t)
  13902. (setq end (match-beginning 0))
  13903. (or force (throw 'exit nil))
  13904. (goto-char beg)
  13905. (setq end beg)
  13906. (org-indent-line)
  13907. (insert ":END:\n"))
  13908. (cons beg end)))))
  13909. (defun org-entry-properties (&optional pom which specific)
  13910. "Get all properties of the entry at point-or-marker POM.
  13911. This includes the TODO keyword, the tags, time strings for deadline,
  13912. scheduled, and clocking, and any additional properties defined in the
  13913. entry. The return value is an alist, keys may occur multiple times
  13914. if the property key was used several times.
  13915. POM may also be nil, in which case the current entry is used.
  13916. If WHICH is nil or `all', get all properties. If WHICH is
  13917. `special' or `standard', only get that subclass. If WHICH
  13918. is a string only get exactly this property. SPECIFIC can be a string, the
  13919. specific property we are interested in. Specifying it can speed
  13920. things up because then unnecessary parsing is avoided."
  13921. (setq which (or which 'all))
  13922. (org-with-wide-buffer
  13923. (org-with-point-at pom
  13924. (let ((clockstr (substring org-clock-string 0 -1))
  13925. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13926. (case-fold-search nil)
  13927. beg end range props sum-props key key1 value string clocksum clocksumt)
  13928. (when (and (derived-mode-p 'org-mode)
  13929. (ignore-errors (org-back-to-heading t)))
  13930. (setq beg (point))
  13931. (setq sum-props (get-text-property (point) 'org-summaries))
  13932. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13933. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13934. (outline-next-heading)
  13935. (setq end (point))
  13936. (when (memq which '(all special))
  13937. ;; Get the special properties, like TODO and tags
  13938. (goto-char beg)
  13939. (when (and (or (not specific) (string= specific "TODO"))
  13940. (looking-at org-todo-line-regexp) (match-end 2))
  13941. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13942. (when (and (or (not specific) (string= specific "PRIORITY"))
  13943. (looking-at org-priority-regexp))
  13944. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13945. (when (or (not specific) (string= specific "FILE"))
  13946. (push (cons "FILE" buffer-file-name) props))
  13947. (when (and (or (not specific) (string= specific "TAGS"))
  13948. (setq value (org-get-tags-string))
  13949. (string-match "\\S-" value))
  13950. (push (cons "TAGS" value) props))
  13951. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13952. (setq value (org-get-tags-at)))
  13953. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13954. ":"))
  13955. props))
  13956. (when (or (not specific) (string= specific "BLOCKED"))
  13957. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13958. (when (or (not specific)
  13959. (member specific
  13960. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13961. "TIMESTAMP" "TIMESTAMP_IA")))
  13962. (catch 'match
  13963. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13964. (setq key (if (match-end 1)
  13965. (substring (org-match-string-no-properties 1)
  13966. 0 -1))
  13967. string (if (equal key clockstr)
  13968. (org-trim
  13969. (buffer-substring-no-properties
  13970. (match-beginning 3) (goto-char
  13971. (point-at-eol))))
  13972. (substring (org-match-string-no-properties 3)
  13973. 1 -1)))
  13974. ;; Get the correct property name from the key. This is
  13975. ;; necessary if the user has configured time keywords.
  13976. (setq key1 (concat key ":"))
  13977. (cond
  13978. ((not key)
  13979. (setq key
  13980. (if (= (char-after (match-beginning 3)) ?\[)
  13981. "TIMESTAMP_IA" "TIMESTAMP")))
  13982. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13983. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13984. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13985. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13986. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13987. (progn
  13988. (push (cons key string) props)
  13989. ;; no need to search further if match is found
  13990. (throw 'match t))
  13991. (when (or (equal key "CLOCK") (not (assoc key props)))
  13992. (push (cons key string) props)))))))
  13993. (when (memq which '(all standard))
  13994. ;; Get the standard properties, like :PROP: ...
  13995. (setq range (org-get-property-block beg end))
  13996. (when range
  13997. (goto-char (car range))
  13998. (while (re-search-forward org-property-re
  13999. (cdr range) t)
  14000. (setq key (org-match-string-no-properties 2)
  14001. value (org-trim (or (org-match-string-no-properties 3) "")))
  14002. (unless (member key excluded)
  14003. (push (cons key (or value "")) props)))))
  14004. (if clocksum
  14005. (push (cons "CLOCKSUM"
  14006. (org-columns-number-to-string (/ (float clocksum) 60.)
  14007. 'add_times))
  14008. props))
  14009. (if clocksumt
  14010. (push (cons "CLOCKSUM_T"
  14011. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14012. 'add_times))
  14013. props))
  14014. (unless (assoc "CATEGORY" props)
  14015. (push (cons "CATEGORY" (org-get-category)) props))
  14016. (append sum-props (nreverse props)))))))
  14017. (defun org-entry-get (pom property &optional inherit literal-nil)
  14018. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14019. If INHERIT is non-nil and the entry does not have the property,
  14020. then also check higher levels of the hierarchy.
  14021. If INHERIT is the symbol `selective', use inheritance only if the setting
  14022. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14023. If the property is present but empty, the return value is the empty string.
  14024. If the property is not present at all, nil is returned.
  14025. Return the value as a string.
  14026. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14027. do not interpret it as the list atom nil. This is used for inheritance
  14028. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14029. (org-with-point-at pom
  14030. (if (and inherit (if (eq inherit 'selective)
  14031. (org-property-inherit-p property)
  14032. t))
  14033. (org-entry-get-with-inheritance property literal-nil)
  14034. (if (member property org-special-properties)
  14035. ;; We need a special property. Use `org-entry-properties'
  14036. ;; to retrieve it, but specify the wanted property
  14037. (cdr (assoc property (org-entry-properties nil 'special property)))
  14038. (org-with-wide-buffer
  14039. (let ((range (org-get-property-block)))
  14040. (when (and range (not (eq (car range) (cdr range)))
  14041. (save-excursion
  14042. (goto-char (car range))
  14043. (re-search-forward
  14044. (concat (org-re-property property) "\\|"
  14045. (org-re-property (concat property "+")))
  14046. (cdr range) t)))
  14047. (let* ((props
  14048. (list (or (assoc property org-file-properties)
  14049. (assoc property org-global-properties)
  14050. (assoc property org-global-properties-fixed))))
  14051. (ap (lambda (key)
  14052. (when (re-search-forward
  14053. (org-re-property key) (cdr range) t)
  14054. (setq props
  14055. (org-update-property-plist
  14056. key
  14057. (if (match-end 3)
  14058. (org-match-string-no-properties 3) "")
  14059. props)))))
  14060. val)
  14061. (goto-char (car range))
  14062. (funcall ap property)
  14063. (goto-char (car range))
  14064. (while (funcall ap (concat property "+")))
  14065. (setq val (cdr (assoc property props)))
  14066. (when val (if literal-nil val (org-not-nil val)))))))))))
  14067. (defun org-property-or-variable-value (var &optional inherit)
  14068. "Check if there is a property fixing the value of VAR.
  14069. If yes, return this value. If not, return the current value of the variable."
  14070. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14071. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14072. (read prop)
  14073. (symbol-value var))))
  14074. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14075. "Delete the property PROPERTY from entry at point-or-marker POM.
  14076. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14077. an empty drawer to delete."
  14078. (org-with-point-at pom
  14079. (if (member property org-special-properties)
  14080. nil ; cannot delete these properties.
  14081. (let ((range (org-get-property-block)))
  14082. (if (and range
  14083. (goto-char (car range))
  14084. (re-search-forward
  14085. (org-re-property property)
  14086. (cdr range) t))
  14087. (progn
  14088. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14089. (and delete-empty-drawer
  14090. (org-remove-empty-drawer-at
  14091. delete-empty-drawer (car range)))
  14092. t)
  14093. nil)))))
  14094. ;; Multi-values properties are properties that contain multiple values
  14095. ;; These values are assumed to be single words, separated by whitespace.
  14096. (defun org-entry-add-to-multivalued-property (pom property value)
  14097. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14098. (let* ((old (org-entry-get pom property))
  14099. (values (and old (org-split-string old "[ \t]"))))
  14100. (setq value (org-entry-protect-space value))
  14101. (unless (member value values)
  14102. (setq values (append values (list value)))
  14103. (org-entry-put pom property
  14104. (mapconcat 'identity values " ")))))
  14105. (defun org-entry-remove-from-multivalued-property (pom property value)
  14106. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14107. (let* ((old (org-entry-get pom property))
  14108. (values (and old (org-split-string old "[ \t]"))))
  14109. (setq value (org-entry-protect-space value))
  14110. (when (member value values)
  14111. (setq values (delete value values))
  14112. (org-entry-put pom property
  14113. (mapconcat 'identity values " ")))))
  14114. (defun org-entry-member-in-multivalued-property (pom property value)
  14115. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14116. (let* ((old (org-entry-get pom property))
  14117. (values (and old (org-split-string old "[ \t]"))))
  14118. (setq value (org-entry-protect-space value))
  14119. (member value values)))
  14120. (defun org-entry-get-multivalued-property (pom property)
  14121. "Return a list of values in a multivalued property."
  14122. (let* ((value (org-entry-get pom property))
  14123. (values (and value (org-split-string value "[ \t]"))))
  14124. (mapcar 'org-entry-restore-space values)))
  14125. (defun org-entry-put-multivalued-property (pom property &rest values)
  14126. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14127. VALUES should be a list of strings. Spaces will be protected."
  14128. (org-entry-put pom property
  14129. (mapconcat 'org-entry-protect-space values " "))
  14130. (let* ((value (org-entry-get pom property))
  14131. (values (and value (org-split-string value "[ \t]"))))
  14132. (mapcar 'org-entry-restore-space values)))
  14133. (defun org-entry-protect-space (s)
  14134. "Protect spaces and newline in string S."
  14135. (while (string-match " " s)
  14136. (setq s (replace-match "%20" t t s)))
  14137. (while (string-match "\n" s)
  14138. (setq s (replace-match "%0A" t t s)))
  14139. s)
  14140. (defun org-entry-restore-space (s)
  14141. "Restore spaces and newline in string S."
  14142. (while (string-match "%20" s)
  14143. (setq s (replace-match " " t t s)))
  14144. (while (string-match "%0A" s)
  14145. (setq s (replace-match "\n" t t s)))
  14146. s)
  14147. (defvar org-entry-property-inherited-from (make-marker)
  14148. "Marker pointing to the entry from where a property was inherited.
  14149. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14150. location of the entry where the inheritance search matched. If there was
  14151. no match, the marker will point nowhere.
  14152. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14153. is set.")
  14154. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14155. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14156. The search will stop at the first ancestor which has the property defined.
  14157. If the value found is \"nil\", return nil to show that the property
  14158. should be considered as undefined (this is the meaning of nil here).
  14159. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14160. (move-marker org-entry-property-inherited-from nil)
  14161. (let (tmp)
  14162. (save-excursion
  14163. (save-restriction
  14164. (widen)
  14165. (catch 'ex
  14166. (while t
  14167. (when (setq tmp (org-entry-get nil property nil literal-nil))
  14168. (or (ignore-errors (org-back-to-heading t))
  14169. (goto-char (point-min)))
  14170. (move-marker org-entry-property-inherited-from (point))
  14171. (throw 'ex tmp))
  14172. (or (ignore-errors (org-up-heading-safe))
  14173. (throw 'ex nil))))))
  14174. (setq tmp (or tmp
  14175. (cdr (assoc property org-file-properties))
  14176. (cdr (assoc property org-global-properties))
  14177. (cdr (assoc property org-global-properties-fixed))))
  14178. (if literal-nil tmp (org-not-nil tmp))))
  14179. (defvar org-property-changed-functions nil
  14180. "Hook called when the value of a property has changed.
  14181. Each hook function should accept two arguments, the name of the property
  14182. and the new value.")
  14183. (defun org-entry-put (pom property value)
  14184. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14185. If the value is `nil', it is converted to the empty string.
  14186. If it is not a string, an error is raised."
  14187. (cond ((null value) (setq value ""))
  14188. ((not (stringp value))
  14189. (error "Properties values should be strings.")))
  14190. (org-with-point-at pom
  14191. (org-back-to-heading t)
  14192. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14193. range)
  14194. (cond
  14195. ((equal property "TODO")
  14196. (when (and (string-match "\\S-" value)
  14197. (not (member value org-todo-keywords-1)))
  14198. (user-error "\"%s\" is not a valid TODO state" value))
  14199. (if (or (not value)
  14200. (not (string-match "\\S-" value)))
  14201. (setq value 'none))
  14202. (org-todo value)
  14203. (org-set-tags nil 'align))
  14204. ((equal property "PRIORITY")
  14205. (org-priority (if (and value (string-match "\\S-" value))
  14206. (string-to-char value) ?\ ))
  14207. (org-set-tags nil 'align))
  14208. ((equal property "CLOCKSUM")
  14209. (if (not (re-search-forward
  14210. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14211. (error "Cannot find a clock log")
  14212. (goto-char (- (match-end 1) 2))
  14213. (cond
  14214. ((eq value 'earlier) (org-timestamp-down))
  14215. ((eq value 'later) (org-timestamp-up)))
  14216. (org-clock-sum-current-item)))
  14217. ((equal property "SCHEDULED")
  14218. (if (re-search-forward org-scheduled-time-regexp end t)
  14219. (cond
  14220. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14221. ((eq value 'later) (org-timestamp-change 1 'day))
  14222. (t (call-interactively 'org-schedule)))
  14223. (call-interactively 'org-schedule)))
  14224. ((equal property "DEADLINE")
  14225. (if (re-search-forward org-deadline-time-regexp end t)
  14226. (cond
  14227. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14228. ((eq value 'later) (org-timestamp-change 1 'day))
  14229. (t (call-interactively 'org-deadline)))
  14230. (call-interactively 'org-deadline)))
  14231. ((member property org-special-properties)
  14232. (error "The %s property can not yet be set with `org-entry-put'"
  14233. property))
  14234. (t ; a non-special property
  14235. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14236. (setq range (org-get-property-block beg end 'force))
  14237. (goto-char (car range))
  14238. (if (re-search-forward
  14239. (org-re-property property) (cdr range) t)
  14240. (progn
  14241. (delete-region (match-beginning 0) (match-end 0))
  14242. (goto-char (match-beginning 0)))
  14243. (goto-char (cdr range))
  14244. (insert "\n")
  14245. (backward-char 1)
  14246. (org-indent-line))
  14247. (insert ":" property ":")
  14248. (and value (insert " " value))
  14249. (org-indent-line)))))
  14250. (run-hook-with-args 'org-property-changed-functions property value)))
  14251. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14252. "Get all property keys in the current buffer.
  14253. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14254. like tags and TODO state.
  14255. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14256. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14257. and others.
  14258. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14259. formats in the current buffer."
  14260. (let (rtn range cfmt s p)
  14261. (save-excursion
  14262. (save-restriction
  14263. (widen)
  14264. (goto-char (point-min))
  14265. (while (re-search-forward org-property-start-re nil t)
  14266. (setq range (org-get-property-block))
  14267. (goto-char (car range))
  14268. (while (re-search-forward org-property-re
  14269. (cdr range) t)
  14270. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14271. (outline-next-heading))))
  14272. (when include-specials
  14273. (setq rtn (append org-special-properties rtn)))
  14274. (when include-defaults
  14275. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14276. (add-to-list 'rtn org-effort-property))
  14277. (when include-columns
  14278. (save-excursion
  14279. (save-restriction
  14280. (widen)
  14281. (goto-char (point-min))
  14282. (while (re-search-forward
  14283. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14284. nil t)
  14285. (setq cfmt (match-string 2) s 0)
  14286. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14287. cfmt s)
  14288. (setq s (match-end 0)
  14289. p (match-string 1 cfmt))
  14290. (unless (or (equal p "ITEM")
  14291. (member p org-special-properties))
  14292. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14293. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14294. (defun org-property-values (key)
  14295. "Return a list of all values of property KEY in the current buffer."
  14296. (save-excursion
  14297. (save-restriction
  14298. (widen)
  14299. (goto-char (point-min))
  14300. (let ((re (org-re-property key))
  14301. values)
  14302. (while (re-search-forward re nil t)
  14303. (add-to-list 'values (org-trim (match-string 3))))
  14304. (delete "" values)))))
  14305. (defun org-insert-property-drawer ()
  14306. "Insert a property drawer into the current entry."
  14307. (org-back-to-heading t)
  14308. (looking-at org-outline-regexp)
  14309. (let ((indent (if org-adapt-indentation
  14310. (- (match-end 0) (match-beginning 0))
  14311. 0))
  14312. (beg (point))
  14313. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14314. end hiddenp)
  14315. (outline-next-heading)
  14316. (setq end (point))
  14317. (goto-char beg)
  14318. (while (re-search-forward re end t))
  14319. (setq hiddenp (outline-invisible-p))
  14320. (end-of-line 1)
  14321. (and (equal (char-after) ?\n) (forward-char 1))
  14322. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14323. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14324. ;; just skip this line
  14325. (beginning-of-line 2)
  14326. ;; Drawer start, find the end
  14327. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14328. (beginning-of-line 1)))
  14329. (org-skip-over-state-notes)
  14330. (skip-chars-backward " \t\n\r")
  14331. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14332. (forward-char 1))
  14333. (goto-char (point-at-eol))
  14334. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14335. (beginning-of-line 0)
  14336. (org-indent-to-column indent)
  14337. (beginning-of-line 2)
  14338. (org-indent-to-column indent)
  14339. (beginning-of-line 0)
  14340. (if hiddenp
  14341. (save-excursion
  14342. (org-back-to-heading t)
  14343. (hide-entry))
  14344. (org-flag-drawer t))))
  14345. (defun org-insert-drawer (&optional arg drawer)
  14346. "Insert a drawer at point.
  14347. Optional argument DRAWER, when non-nil, is a string representing
  14348. drawer's name. Otherwise, the user is prompted for a name.
  14349. If a region is active, insert the drawer around that region
  14350. instead.
  14351. Point is left between drawer's boundaries."
  14352. (interactive "P")
  14353. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14354. "LOGBOOK"))
  14355. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14356. ;; internally by Org. They are meant to be inserted
  14357. ;; automatically.
  14358. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14359. ;; Remove system drawers from list. Note: For some reason,
  14360. ;; `org-completing-read' ignores the predicate while
  14361. ;; `completing-read' handles it fine.
  14362. (drawer (if arg "PROPERTIES"
  14363. (or drawer
  14364. (completing-read
  14365. "Drawer: " org-drawers
  14366. (lambda (d) (not (member d system-drawers))))))))
  14367. (cond
  14368. ;; With C-u, fall back on `org-insert-property-drawer'
  14369. (arg (org-insert-property-drawer))
  14370. ;; With an active region, insert a drawer at point.
  14371. ((not (org-region-active-p))
  14372. (progn
  14373. (unless (bolp) (insert "\n"))
  14374. (insert (format ":%s:\n\n:END:\n" drawer))
  14375. (forward-line -2)))
  14376. ;; Otherwise, insert the drawer at point
  14377. (t
  14378. (let ((rbeg (region-beginning))
  14379. (rend (copy-marker (region-end))))
  14380. (unwind-protect
  14381. (progn
  14382. (goto-char rbeg)
  14383. (beginning-of-line)
  14384. (when (save-excursion
  14385. (re-search-forward org-outline-regexp-bol rend t))
  14386. (user-error "Drawers cannot contain headlines"))
  14387. ;; Position point at the beginning of the first
  14388. ;; non-blank line in region. Insert drawer's opening
  14389. ;; there, then indent it.
  14390. (org-skip-whitespace)
  14391. (beginning-of-line)
  14392. (insert ":" drawer ":\n")
  14393. (forward-line -1)
  14394. (indent-for-tab-command)
  14395. ;; Move point to the beginning of the first blank line
  14396. ;; after the last non-blank line in region. Insert
  14397. ;; drawer's closing, then indent it.
  14398. (goto-char rend)
  14399. (skip-chars-backward " \r\t\n")
  14400. (insert "\n:END:")
  14401. (deactivate-mark t)
  14402. (indent-for-tab-command)
  14403. (unless (eolp) (insert "\n")))
  14404. ;; Clear marker, whatever the outcome of insertion is.
  14405. (set-marker rend nil)))))))
  14406. (defvar org-property-set-functions-alist nil
  14407. "Property set function alist.
  14408. Each entry should have the following format:
  14409. (PROPERTY . READ-FUNCTION)
  14410. The read function will be called with the same argument as
  14411. `org-completing-read'.")
  14412. (defun org-set-property-function (property)
  14413. "Get the function that should be used to set PROPERTY.
  14414. This is computed according to `org-property-set-functions-alist'."
  14415. (or (cdr (assoc property org-property-set-functions-alist))
  14416. 'org-completing-read))
  14417. (defun org-read-property-value (property)
  14418. "Read PROPERTY value from user."
  14419. (let* ((completion-ignore-case t)
  14420. (allowed (org-property-get-allowed-values nil property 'table))
  14421. (cur (org-entry-get nil property))
  14422. (prompt (concat property " value"
  14423. (if (and cur (string-match "\\S-" cur))
  14424. (concat " [" cur "]") "") ": "))
  14425. (set-function (org-set-property-function property))
  14426. (val (if allowed
  14427. (funcall set-function prompt allowed nil
  14428. (not (get-text-property 0 'org-unrestricted
  14429. (caar allowed))))
  14430. (let (org-completion-use-ido org-completion-use-iswitchb)
  14431. (funcall set-function prompt
  14432. (mapcar 'list (org-property-values property))
  14433. nil nil "" nil cur)))))
  14434. (if (equal val "")
  14435. cur
  14436. val)))
  14437. (defvar org-last-set-property nil)
  14438. (defvar org-last-set-property-value nil)
  14439. (defun org-read-property-name ()
  14440. "Read a property name."
  14441. (let* ((completion-ignore-case t)
  14442. (keys (org-buffer-property-keys nil t t))
  14443. (default-prop (or (save-excursion
  14444. (save-match-data
  14445. (beginning-of-line)
  14446. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14447. (null (string= (match-string 1) "END"))
  14448. (match-string 1))))
  14449. org-last-set-property))
  14450. (property (org-icompleting-read
  14451. (concat "Property"
  14452. (if default-prop (concat " [" default-prop "]") "")
  14453. ": ")
  14454. (mapcar 'list keys)
  14455. nil nil nil nil
  14456. default-prop)))
  14457. (if (member property keys)
  14458. property
  14459. (or (cdr (assoc (downcase property)
  14460. (mapcar (lambda (x) (cons (downcase x) x))
  14461. keys)))
  14462. property))))
  14463. (defun org-set-property-and-value (use-last)
  14464. "Allow to set [PROPERTY]: [value] direction from prompt.
  14465. When use-default, don't even ask, just use the last
  14466. \"[PROPERTY]: [value]\" string from the history."
  14467. (interactive "P")
  14468. (let* ((completion-ignore-case t)
  14469. (pv (or (and use-last org-last-set-property-value)
  14470. (org-completing-read
  14471. "Enter a \"[Property]: [value]\" pair: "
  14472. nil nil nil nil nil
  14473. org-last-set-property-value)))
  14474. prop val)
  14475. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14476. (setq prop (match-string 1 pv)
  14477. val (match-string 2 pv))
  14478. (org-set-property prop val))))
  14479. (defun org-set-property (property value)
  14480. "In the current entry, set PROPERTY to VALUE.
  14481. When called interactively, this will prompt for a property name, offering
  14482. completion on existing and default properties. And then it will prompt
  14483. for a value, offering completion either on allowed values (via an inherited
  14484. xxx_ALL property) or on existing values in other instances of this property
  14485. in the current file."
  14486. (interactive (list nil nil))
  14487. (let* ((property (or property (org-read-property-name)))
  14488. (value (or value (org-read-property-value property)))
  14489. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14490. (setq org-last-set-property property)
  14491. (setq org-last-set-property-value (concat property ": " value))
  14492. ;; Possibly postprocess the inserted value:
  14493. (when fn (setq value (funcall fn value)))
  14494. (unless (equal (org-entry-get nil property) value)
  14495. (org-entry-put nil property value))))
  14496. (defun org-delete-property (property &optional delete-empty-drawer)
  14497. "In the current entry, delete PROPERTY.
  14498. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14499. an empty drawer to delete."
  14500. (interactive
  14501. (let* ((completion-ignore-case t)
  14502. (cat (org-entry-get (point) "CATEGORY"))
  14503. (props0 (org-entry-properties nil 'standard))
  14504. (props (if cat props0
  14505. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14506. (prop (if (< 1 (length props))
  14507. (org-icompleting-read "Property: " props nil t)
  14508. (caar props))))
  14509. (list prop)))
  14510. (if (org-entry-delete nil property delete-empty-drawer)
  14511. (message "Property %s deleted" property)))
  14512. (defun org-delete-property-globally (property)
  14513. "Remove PROPERTY globally, from all entries."
  14514. (interactive
  14515. (let* ((completion-ignore-case t)
  14516. (prop (org-icompleting-read
  14517. "Globally remove property: "
  14518. (mapcar 'list (org-buffer-property-keys)))))
  14519. (list prop)))
  14520. (save-excursion
  14521. (save-restriction
  14522. (widen)
  14523. (goto-char (point-min))
  14524. (let ((cnt 0))
  14525. (while (re-search-forward
  14526. (org-re-property property)
  14527. nil t)
  14528. (setq cnt (1+ cnt))
  14529. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14530. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14531. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14532. (defun org-compute-property-at-point ()
  14533. "Compute the property at point.
  14534. This looks for an enclosing column format, extracts the operator and
  14535. then applies it to the property in the column format's scope."
  14536. (interactive)
  14537. (unless (org-at-property-p)
  14538. (user-error "Not at a property"))
  14539. (let ((prop (org-match-string-no-properties 2)))
  14540. (org-columns-get-format-and-top-level)
  14541. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14542. (user-error "No operator defined for property %s" prop))
  14543. (org-columns-compute prop)))
  14544. (defvar org-property-allowed-value-functions nil
  14545. "Hook for functions supplying allowed values for a specific property.
  14546. The functions must take a single argument, the name of the property, and
  14547. return a flat list of allowed values. If \":ETC\" is one of
  14548. the values, this means that these values are intended as defaults for
  14549. completion, but that other values should be allowed too.
  14550. The functions must return nil if they are not responsible for this
  14551. property.")
  14552. (defun org-property-get-allowed-values (pom property &optional table)
  14553. "Get allowed values for the property PROPERTY.
  14554. When TABLE is non-nil, return an alist that can directly be used for
  14555. completion."
  14556. (let (vals)
  14557. (cond
  14558. ((equal property "TODO")
  14559. (setq vals (org-with-point-at pom
  14560. (append org-todo-keywords-1 '("")))))
  14561. ((equal property "PRIORITY")
  14562. (let ((n org-lowest-priority))
  14563. (while (>= n org-highest-priority)
  14564. (push (char-to-string n) vals)
  14565. (setq n (1- n)))))
  14566. ((member property org-special-properties))
  14567. ((setq vals (run-hook-with-args-until-success
  14568. 'org-property-allowed-value-functions property)))
  14569. (t
  14570. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14571. (when (and vals (string-match "\\S-" vals))
  14572. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14573. (setq vals (mapcar (lambda (x)
  14574. (cond ((stringp x) x)
  14575. ((numberp x) (number-to-string x))
  14576. ((symbolp x) (symbol-name x))
  14577. (t "???")))
  14578. vals)))))
  14579. (when (member ":ETC" vals)
  14580. (setq vals (remove ":ETC" vals))
  14581. (org-add-props (car vals) '(org-unrestricted t)))
  14582. (if table (mapcar 'list vals) vals)))
  14583. (defun org-property-previous-allowed-value (&optional previous)
  14584. "Switch to the next allowed value for this property."
  14585. (interactive)
  14586. (org-property-next-allowed-value t))
  14587. (defun org-property-next-allowed-value (&optional previous)
  14588. "Switch to the next allowed value for this property."
  14589. (interactive)
  14590. (unless (org-at-property-p)
  14591. (user-error "Not at a property"))
  14592. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14593. (key (match-string 2))
  14594. (value (match-string 3))
  14595. (allowed (or (org-property-get-allowed-values (point) key)
  14596. (and (member value '("[ ]" "[-]" "[X]"))
  14597. '("[ ]" "[X]"))))
  14598. (heading (save-match-data (nth 4 (org-heading-components))))
  14599. nval)
  14600. (unless allowed
  14601. (user-error "Allowed values for this property have not been defined"))
  14602. (if previous (setq allowed (reverse allowed)))
  14603. (if (member value allowed)
  14604. (setq nval (car (cdr (member value allowed)))))
  14605. (setq nval (or nval (car allowed)))
  14606. (if (equal nval value)
  14607. (user-error "Only one allowed value for this property"))
  14608. (org-at-property-p)
  14609. (replace-match (concat " :" key ": " nval) t t)
  14610. (org-indent-line)
  14611. (beginning-of-line 1)
  14612. (skip-chars-forward " \t")
  14613. (when (equal prop org-effort-property)
  14614. (save-excursion
  14615. (org-back-to-heading t)
  14616. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14617. (when (string= org-clock-current-task heading)
  14618. (setq org-clock-effort nval)
  14619. (org-clock-update-mode-line)))
  14620. (run-hook-with-args 'org-property-changed-functions key nval)))
  14621. (defun org-find-olp (path &optional this-buffer)
  14622. "Return a marker pointing to the entry at outline path OLP.
  14623. If anything goes wrong, throw an error.
  14624. You can wrap this call to catch the error like this:
  14625. (condition-case msg
  14626. (org-mobile-locate-entry (match-string 4))
  14627. (error (nth 1 msg)))
  14628. The return value will then be either a string with the error message,
  14629. or a marker if everything is OK.
  14630. If THIS-BUFFER is set, the outline path does not contain a file,
  14631. only headings."
  14632. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14633. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14634. (level 1)
  14635. (lmin 1)
  14636. (lmax 1)
  14637. limit re end found pos heading cnt flevel)
  14638. (unless buffer (error "File not found :%s" file))
  14639. (with-current-buffer buffer
  14640. (save-excursion
  14641. (save-restriction
  14642. (widen)
  14643. (setq limit (point-max))
  14644. (goto-char (point-min))
  14645. (while (setq heading (pop path))
  14646. (setq re (format org-complex-heading-regexp-format
  14647. (regexp-quote heading)))
  14648. (setq cnt 0 pos (point))
  14649. (while (re-search-forward re end t)
  14650. (setq level (- (match-end 1) (match-beginning 1)))
  14651. (if (and (>= level lmin) (<= level lmax))
  14652. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14653. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14654. lmax heading))
  14655. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14656. lmax heading))
  14657. (goto-char found)
  14658. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14659. (setq end (save-excursion (org-end-of-subtree t t))))
  14660. (when (org-at-heading-p)
  14661. (point-marker)))))))
  14662. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14663. "Find node HEADING in BUFFER.
  14664. Return a marker to the heading if it was found, or nil if not.
  14665. If POS-ONLY is set, return just the position instead of a marker.
  14666. The heading text must match exact, but it may have a TODO keyword,
  14667. a priority cookie and tags in the standard locations."
  14668. (with-current-buffer (or buffer (current-buffer))
  14669. (save-excursion
  14670. (save-restriction
  14671. (widen)
  14672. (goto-char (point-min))
  14673. (let (case-fold-search)
  14674. (if (re-search-forward
  14675. (format org-complex-heading-regexp-format
  14676. (regexp-quote heading)) nil t)
  14677. (if pos-only
  14678. (match-beginning 0)
  14679. (move-marker (make-marker) (match-beginning 0)))))))))
  14680. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14681. "Find Org node headline HEADING in all .org files in directory DIR.
  14682. When the target headline is found, return a marker to this location."
  14683. (let ((files (directory-files (or dir default-directory)
  14684. nil "\\`[^.#].*\\.org\\'"))
  14685. file visiting m buffer)
  14686. (catch 'found
  14687. (while (setq file (pop files))
  14688. (message "trying %s" file)
  14689. (setq visiting (org-find-base-buffer-visiting file))
  14690. (setq buffer (or visiting (find-file-noselect file)))
  14691. (setq m (org-find-exact-headline-in-buffer
  14692. heading buffer))
  14693. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14694. (and m (throw 'found m))))))
  14695. (defun org-find-entry-with-id (ident)
  14696. "Locate the entry that contains the ID property with exact value IDENT.
  14697. IDENT can be a string, a symbol or a number, this function will search for
  14698. the string representation of it.
  14699. Return the position where this entry starts, or nil if there is no such entry."
  14700. (interactive "sID: ")
  14701. (let ((id (cond
  14702. ((stringp ident) ident)
  14703. ((symbol-name ident) (symbol-name ident))
  14704. ((numberp ident) (number-to-string ident))
  14705. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14706. (case-fold-search nil))
  14707. (save-excursion
  14708. (save-restriction
  14709. (widen)
  14710. (goto-char (point-min))
  14711. (when (re-search-forward
  14712. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14713. nil t)
  14714. (org-back-to-heading t)
  14715. (point))))))
  14716. ;;;; Timestamps
  14717. (defvar org-last-changed-timestamp nil)
  14718. (defvar org-last-inserted-timestamp nil
  14719. "The last time stamp inserted with `org-insert-time-stamp'.")
  14720. (defvar org-ts-what) ; dynamically scoped parameter
  14721. (defun org-time-stamp (arg &optional inactive)
  14722. "Prompt for a date/time and insert a time stamp.
  14723. If the user specifies a time like HH:MM or if this command is
  14724. called with at least one prefix argument, the time stamp contains
  14725. the date and the time. Otherwise, only the date is be included.
  14726. All parts of a date not specified by the user is filled in from
  14727. the current date/time. So if you just press return without
  14728. typing anything, the time stamp will represent the current
  14729. date/time.
  14730. If there is already a timestamp at the cursor, it will be
  14731. modified.
  14732. With two universal prefix arguments, insert an active timestamp
  14733. with the current time without prompting the user.
  14734. When called from lisp, the timestamp is inactive if INACTIVE is
  14735. non-nil."
  14736. (interactive "P")
  14737. (let* ((ts nil)
  14738. (default-time
  14739. ;; Default time is either today, or, when entering a range,
  14740. ;; the range start.
  14741. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14742. (save-excursion
  14743. (re-search-backward
  14744. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14745. (- (point) 20) t)))
  14746. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14747. (current-time)))
  14748. (default-input (and ts (org-get-compact-tod ts)))
  14749. (repeater (save-excursion
  14750. (save-match-data
  14751. (beginning-of-line)
  14752. (when (re-search-forward
  14753. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14754. (save-excursion (progn (end-of-line) (point))) t)
  14755. (match-string 0)))))
  14756. org-time-was-given org-end-time-was-given time)
  14757. (cond
  14758. ((and (org-at-timestamp-p t)
  14759. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14760. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14761. (insert "--")
  14762. (setq time (let ((this-command this-command))
  14763. (org-read-date arg 'totime nil nil
  14764. default-time default-input inactive)))
  14765. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14766. ((org-at-timestamp-p t)
  14767. (setq time (let ((this-command this-command))
  14768. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14769. (when (org-at-timestamp-p t) ; just to get the match data
  14770. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14771. (replace-match "")
  14772. (setq org-last-changed-timestamp
  14773. (org-insert-time-stamp
  14774. time (or org-time-was-given arg)
  14775. inactive nil nil (list org-end-time-was-given)))
  14776. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14777. (setq org-last-changed-timestamp
  14778. (concat (substring org-last-inserted-timestamp 0 -1)
  14779. " " repeater ">"))))
  14780. (message "Timestamp updated"))
  14781. ((equal arg '(16))
  14782. (org-insert-time-stamp (current-time) t inactive))
  14783. (t
  14784. (setq time (let ((this-command this-command))
  14785. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14786. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14787. nil nil (list org-end-time-was-given))))))
  14788. ;; FIXME: can we use this for something else, like computing time differences?
  14789. (defun org-get-compact-tod (s)
  14790. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14791. (let* ((t1 (match-string 1 s))
  14792. (h1 (string-to-number (match-string 2 s)))
  14793. (m1 (string-to-number (match-string 3 s)))
  14794. (t2 (and (match-end 4) (match-string 5 s)))
  14795. (h2 (and t2 (string-to-number (match-string 6 s))))
  14796. (m2 (and t2 (string-to-number (match-string 7 s))))
  14797. dh dm)
  14798. (if (not t2)
  14799. t1
  14800. (setq dh (- h2 h1) dm (- m2 m1))
  14801. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14802. (concat t1 "+" (number-to-string dh)
  14803. (and (/= 0 dm) (format ":%02d" dm)))))))
  14804. (defun org-time-stamp-inactive (&optional arg)
  14805. "Insert an inactive time stamp.
  14806. An inactive time stamp is enclosed in square brackets instead of angle
  14807. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14808. does not link to the calendar and cannot be changed with the S-cursor keys.
  14809. So these are more for recording a certain time/date."
  14810. (interactive "P")
  14811. (org-time-stamp arg 'inactive))
  14812. (defvar org-date-ovl (make-overlay 1 1))
  14813. (overlay-put org-date-ovl 'face 'org-date-selected)
  14814. (org-detach-overlay org-date-ovl)
  14815. (defvar org-ans1) ; dynamically scoped parameter
  14816. (defvar org-ans2) ; dynamically scoped parameter
  14817. (defvar org-plain-time-of-day-regexp) ; defined below
  14818. (defvar org-overriding-default-time nil) ; dynamically scoped
  14819. (defvar org-read-date-overlay nil)
  14820. (defvar org-dcst nil) ; dynamically scoped
  14821. (defvar org-read-date-history nil)
  14822. (defvar org-read-date-final-answer nil)
  14823. (defvar org-read-date-analyze-futurep nil)
  14824. (defvar org-read-date-analyze-forced-year nil)
  14825. (defvar org-read-date-inactive)
  14826. (defvar org-read-date-minibuffer-local-map
  14827. (let* ((org-replace-disputed-keys nil)
  14828. (map (make-sparse-keymap)))
  14829. (set-keymap-parent map minibuffer-local-map)
  14830. (org-defkey map (kbd ".")
  14831. (lambda () (interactive)
  14832. ;; Are we at the beginning of the prompt?
  14833. (if (looking-back "^[^:]+: ")
  14834. (org-eval-in-calendar '(calendar-goto-today))
  14835. (insert "."))))
  14836. (org-defkey map (kbd "C-.")
  14837. (lambda () (interactive)
  14838. (org-eval-in-calendar '(calendar-goto-today))))
  14839. (org-defkey map [(meta shift left)]
  14840. (lambda () (interactive)
  14841. (org-eval-in-calendar '(calendar-backward-month 1))))
  14842. (org-defkey map [(meta shift right)]
  14843. (lambda () (interactive)
  14844. (org-eval-in-calendar '(calendar-forward-month 1))))
  14845. (org-defkey map [(meta shift up)]
  14846. (lambda () (interactive)
  14847. (org-eval-in-calendar '(calendar-backward-year 1))))
  14848. (org-defkey map [(meta shift down)]
  14849. (lambda () (interactive)
  14850. (org-eval-in-calendar '(calendar-forward-year 1))))
  14851. (org-defkey map [?\e (shift left)]
  14852. (lambda () (interactive)
  14853. (org-eval-in-calendar '(calendar-backward-month 1))))
  14854. (org-defkey map [?\e (shift right)]
  14855. (lambda () (interactive)
  14856. (org-eval-in-calendar '(calendar-forward-month 1))))
  14857. (org-defkey map [?\e (shift up)]
  14858. (lambda () (interactive)
  14859. (org-eval-in-calendar '(calendar-backward-year 1))))
  14860. (org-defkey map [?\e (shift down)]
  14861. (lambda () (interactive)
  14862. (org-eval-in-calendar '(calendar-forward-year 1))))
  14863. (org-defkey map [(shift up)]
  14864. (lambda () (interactive)
  14865. (org-eval-in-calendar '(calendar-backward-week 1))))
  14866. (org-defkey map [(shift down)]
  14867. (lambda () (interactive)
  14868. (org-eval-in-calendar '(calendar-forward-week 1))))
  14869. (org-defkey map [(shift left)]
  14870. (lambda () (interactive)
  14871. (org-eval-in-calendar '(calendar-backward-day 1))))
  14872. (org-defkey map [(shift right)]
  14873. (lambda () (interactive)
  14874. (org-eval-in-calendar '(calendar-forward-day 1))))
  14875. (org-defkey map "!"
  14876. (lambda () (interactive)
  14877. (org-eval-in-calendar '(diary-view-entries))
  14878. (message "")))
  14879. (org-defkey map ">"
  14880. (lambda () (interactive)
  14881. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14882. (org-defkey map "<"
  14883. (lambda () (interactive)
  14884. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14885. (org-defkey map "\C-v"
  14886. (lambda () (interactive)
  14887. (org-eval-in-calendar
  14888. '(calendar-scroll-left-three-months 1))))
  14889. (org-defkey map "\M-v"
  14890. (lambda () (interactive)
  14891. (org-eval-in-calendar
  14892. '(calendar-scroll-right-three-months 1))))
  14893. map)
  14894. "Keymap for minibuffer commands when using `org-read-date'.")
  14895. (defvar org-def)
  14896. (defvar org-defdecode)
  14897. (defvar org-with-time)
  14898. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14899. default-time default-input inactive)
  14900. "Read a date, possibly a time, and make things smooth for the user.
  14901. The prompt will suggest to enter an ISO date, but you can also enter anything
  14902. which will at least partially be understood by `parse-time-string'.
  14903. Unrecognized parts of the date will default to the current day, month, year,
  14904. hour and minute. If this command is called to replace a timestamp at point,
  14905. or to enter the second timestamp of a range, the default time is taken
  14906. from the existing stamp. Furthermore, the command prefers the future,
  14907. so if you are giving a date where the year is not given, and the day-month
  14908. combination is already past in the current year, it will assume you
  14909. mean next year. For details, see the manual. A few examples:
  14910. 3-2-5 --> 2003-02-05
  14911. feb 15 --> currentyear-02-15
  14912. 2/15 --> currentyear-02-15
  14913. sep 12 9 --> 2009-09-12
  14914. 12:45 --> today 12:45
  14915. 22 sept 0:34 --> currentyear-09-22 0:34
  14916. 12 --> currentyear-currentmonth-12
  14917. Fri --> nearest Friday after today
  14918. -Tue --> last Tuesday
  14919. etc.
  14920. Furthermore you can specify a relative date by giving, as the *first* thing
  14921. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14922. change in days weeks, months, years.
  14923. With a single plus or minus, the date is relative to today. With a double
  14924. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14925. +4d --> four days from today
  14926. +4 --> same as above
  14927. +2w --> two weeks from today
  14928. ++5 --> five days from default date
  14929. The function understands only English month and weekday abbreviations.
  14930. While prompting, a calendar is popped up - you can also select the
  14931. date with the mouse (button 1). The calendar shows a period of three
  14932. months. To scroll it to other months, use the keys `>' and `<'.
  14933. If you don't like the calendar, turn it off with
  14934. \(setq org-read-date-popup-calendar nil)
  14935. With optional argument TO-TIME, the date will immediately be converted
  14936. to an internal time.
  14937. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14938. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14939. still enter a time, and this function will inform the calling routine
  14940. about this change. The calling routine may then choose to change the
  14941. format used to insert the time stamp into the buffer to include the time.
  14942. With optional argument FROM-STRING, read from this string instead from
  14943. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14944. the time/date that is used for everything that is not specified by the
  14945. user."
  14946. (require 'parse-time)
  14947. (let* ((org-time-stamp-rounding-minutes
  14948. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14949. (org-dcst org-display-custom-times)
  14950. (ct (org-current-time))
  14951. (org-def (or org-overriding-default-time default-time ct))
  14952. (org-defdecode (decode-time org-def))
  14953. (dummy (progn
  14954. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14955. (setcar (nthcdr 2 org-defdecode) -1)
  14956. (setcar (nthcdr 1 org-defdecode) 59)
  14957. (setq org-def (apply 'encode-time org-defdecode)
  14958. org-defdecode (decode-time org-def)))))
  14959. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14960. (calendar-frame-setup nil)
  14961. (calendar-setup nil)
  14962. (calendar-move-hook nil)
  14963. (calendar-view-diary-initially-flag nil)
  14964. (calendar-view-holidays-initially-flag nil)
  14965. (timestr (format-time-string
  14966. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14967. (prompt (concat (if prompt (concat prompt " ") "")
  14968. (format "Date+time [%s]: " timestr)))
  14969. ans (org-ans0 "") org-ans1 org-ans2 final)
  14970. (cond
  14971. (from-string (setq ans from-string))
  14972. (org-read-date-popup-calendar
  14973. (save-excursion
  14974. (save-window-excursion
  14975. (calendar)
  14976. (org-eval-in-calendar '(setq cursor-type nil) t)
  14977. (unwind-protect
  14978. (progn
  14979. (calendar-forward-day (- (time-to-days org-def)
  14980. (calendar-absolute-from-gregorian
  14981. (calendar-current-date))))
  14982. (org-eval-in-calendar nil t)
  14983. (let* ((old-map (current-local-map))
  14984. (map (copy-keymap calendar-mode-map))
  14985. (minibuffer-local-map
  14986. (copy-keymap org-read-date-minibuffer-local-map)))
  14987. (org-defkey map (kbd "RET") 'org-calendar-select)
  14988. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14989. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14990. (unwind-protect
  14991. (progn
  14992. (use-local-map map)
  14993. (setq org-read-date-inactive inactive)
  14994. (add-hook 'post-command-hook 'org-read-date-display)
  14995. (setq org-ans0 (read-string prompt default-input
  14996. 'org-read-date-history nil))
  14997. ;; org-ans0: from prompt
  14998. ;; org-ans1: from mouse click
  14999. ;; org-ans2: from calendar motion
  15000. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15001. (remove-hook 'post-command-hook 'org-read-date-display)
  15002. (use-local-map old-map)
  15003. (when org-read-date-overlay
  15004. (delete-overlay org-read-date-overlay)
  15005. (setq org-read-date-overlay nil)))))
  15006. (bury-buffer "*Calendar*")))))
  15007. (t ; Naked prompt only
  15008. (unwind-protect
  15009. (setq ans (read-string prompt default-input
  15010. 'org-read-date-history timestr))
  15011. (when org-read-date-overlay
  15012. (delete-overlay org-read-date-overlay)
  15013. (setq org-read-date-overlay nil)))))
  15014. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15015. (when org-read-date-analyze-forced-year
  15016. (message "Year was forced into %s"
  15017. (if org-read-date-force-compatible-dates
  15018. "compatible range (1970-2037)"
  15019. "range representable on this machine"))
  15020. (ding))
  15021. ;; One round trip to get rid of 34th of August and stuff like that....
  15022. (setq final (decode-time (apply 'encode-time final)))
  15023. (setq org-read-date-final-answer ans)
  15024. (if to-time
  15025. (apply 'encode-time final)
  15026. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15027. (format "%04d-%02d-%02d %02d:%02d"
  15028. (nth 5 final) (nth 4 final) (nth 3 final)
  15029. (nth 2 final) (nth 1 final))
  15030. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15031. (defun org-read-date-display ()
  15032. "Display the current date prompt interpretation in the minibuffer."
  15033. (when org-read-date-display-live
  15034. (when org-read-date-overlay
  15035. (delete-overlay org-read-date-overlay))
  15036. (when (minibufferp (current-buffer))
  15037. (save-excursion
  15038. (end-of-line 1)
  15039. (while (not (equal (buffer-substring
  15040. (max (point-min) (- (point) 4)) (point))
  15041. " "))
  15042. (insert " ")))
  15043. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15044. " " (or org-ans1 org-ans2)))
  15045. (org-end-time-was-given nil)
  15046. (f (org-read-date-analyze ans org-def org-defdecode))
  15047. (fmts (if org-dcst
  15048. org-time-stamp-custom-formats
  15049. org-time-stamp-formats))
  15050. (fmt (if (or org-with-time
  15051. (and (boundp 'org-time-was-given) org-time-was-given))
  15052. (cdr fmts)
  15053. (car fmts)))
  15054. (txt (format-time-string fmt (apply 'encode-time f)))
  15055. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15056. (txt (concat "=> " txt)))
  15057. (when (and org-end-time-was-given
  15058. (string-match org-plain-time-of-day-regexp txt))
  15059. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15060. org-end-time-was-given
  15061. (substring txt (match-end 0)))))
  15062. (when org-read-date-analyze-futurep
  15063. (setq txt (concat txt " (=>F)")))
  15064. (setq org-read-date-overlay
  15065. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15066. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15067. (defun org-read-date-analyze (ans org-def org-defdecode)
  15068. "Analyze the combined answer of the date prompt."
  15069. ;; FIXME: cleanup and comment
  15070. (let ((nowdecode (decode-time (current-time)))
  15071. delta deltan deltaw deltadef year month day
  15072. hour minute second wday pm h2 m2 tl wday1
  15073. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15074. (setq org-read-date-analyze-futurep nil
  15075. org-read-date-analyze-forced-year nil)
  15076. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15077. (setq ans "+0"))
  15078. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15079. (setq ans (replace-match "" t t ans)
  15080. deltan (car delta)
  15081. deltaw (nth 1 delta)
  15082. deltadef (nth 2 delta)))
  15083. ;; Check if there is an iso week date in there. If yes, store the
  15084. ;; info and postpone interpreting it until the rest of the parsing
  15085. ;; is done.
  15086. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15087. (setq iso-year (if (match-end 1)
  15088. (org-small-year-to-year
  15089. (string-to-number (match-string 1 ans))))
  15090. iso-weekday (if (match-end 3)
  15091. (string-to-number (match-string 3 ans)))
  15092. iso-week (string-to-number (match-string 2 ans)))
  15093. (setq ans (replace-match "" t t ans)))
  15094. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15095. (when (string-match
  15096. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15097. (setq year (if (match-end 2)
  15098. (string-to-number (match-string 2 ans))
  15099. (progn (setq kill-year t)
  15100. (string-to-number (format-time-string "%Y"))))
  15101. month (string-to-number (match-string 3 ans))
  15102. day (string-to-number (match-string 4 ans)))
  15103. (if (< year 100) (setq year (+ 2000 year)))
  15104. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15105. t nil ans)))
  15106. ;; Help matching dotted european dates
  15107. (when (string-match
  15108. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15109. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15110. (setq kill-year t)
  15111. (string-to-number (format-time-string "%Y")))
  15112. day (string-to-number (match-string 1 ans))
  15113. month (string-to-number (match-string 2 ans))
  15114. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15115. t nil ans)))
  15116. ;; Help matching american dates, like 5/30 or 5/30/7
  15117. (when (string-match
  15118. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15119. (setq year (if (match-end 4)
  15120. (string-to-number (match-string 4 ans))
  15121. (progn (setq kill-year t)
  15122. (string-to-number (format-time-string "%Y"))))
  15123. month (string-to-number (match-string 1 ans))
  15124. day (string-to-number (match-string 2 ans)))
  15125. (if (< year 100) (setq year (+ 2000 year)))
  15126. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15127. t nil ans)))
  15128. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15129. ;; If there is a time with am/pm, and *no* time without it, we convert
  15130. ;; so that matching will be successful.
  15131. (loop for i from 1 to 2 do ; twice, for end time as well
  15132. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15133. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15134. (setq hour (string-to-number (match-string 1 ans))
  15135. minute (if (match-end 3)
  15136. (string-to-number (match-string 3 ans))
  15137. 0)
  15138. pm (equal ?p
  15139. (string-to-char (downcase (match-string 4 ans)))))
  15140. (if (and (= hour 12) (not pm))
  15141. (setq hour 0)
  15142. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15143. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15144. t t ans))))
  15145. ;; Check if a time range is given as a duration
  15146. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15147. (setq hour (string-to-number (match-string 1 ans))
  15148. h2 (+ hour (string-to-number (match-string 3 ans)))
  15149. minute (string-to-number (match-string 2 ans))
  15150. m2 (+ minute (if (match-end 5) (string-to-number
  15151. (match-string 5 ans))0)))
  15152. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15153. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15154. t t ans)))
  15155. ;; Check if there is a time range
  15156. (when (boundp 'org-end-time-was-given)
  15157. (setq org-time-was-given nil)
  15158. (when (and (string-match org-plain-time-of-day-regexp ans)
  15159. (match-end 8))
  15160. (setq org-end-time-was-given (match-string 8 ans))
  15161. (setq ans (concat (substring ans 0 (match-beginning 7))
  15162. (substring ans (match-end 7))))))
  15163. (setq tl (parse-time-string ans)
  15164. day (or (nth 3 tl) (nth 3 org-defdecode))
  15165. month (or (nth 4 tl)
  15166. (if (and org-read-date-prefer-future
  15167. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15168. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15169. (nth 4 org-defdecode)))
  15170. year (or (and (not kill-year) (nth 5 tl))
  15171. (if (and org-read-date-prefer-future
  15172. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15173. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15174. (nth 5 org-defdecode)))
  15175. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15176. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15177. second (or (nth 0 tl) 0)
  15178. wday (nth 6 tl))
  15179. (when (and (eq org-read-date-prefer-future 'time)
  15180. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15181. (equal day (nth 3 nowdecode))
  15182. (equal month (nth 4 nowdecode))
  15183. (equal year (nth 5 nowdecode))
  15184. (nth 2 tl)
  15185. (or (< (nth 2 tl) (nth 2 nowdecode))
  15186. (and (= (nth 2 tl) (nth 2 nowdecode))
  15187. (nth 1 tl)
  15188. (< (nth 1 tl) (nth 1 nowdecode)))))
  15189. (setq day (1+ day)
  15190. futurep t))
  15191. ;; Special date definitions below
  15192. (cond
  15193. (iso-week
  15194. ;; There was an iso week
  15195. (require 'cal-iso)
  15196. (setq futurep nil)
  15197. (setq year (or iso-year year)
  15198. day (or iso-weekday wday 1)
  15199. wday nil ; to make sure that the trigger below does not match
  15200. iso-date (calendar-gregorian-from-absolute
  15201. (calendar-absolute-from-iso
  15202. (list iso-week day year))))
  15203. ; FIXME: Should we also push ISO weeks into the future?
  15204. ; (when (and org-read-date-prefer-future
  15205. ; (not iso-year)
  15206. ; (< (calendar-absolute-from-gregorian iso-date)
  15207. ; (time-to-days (current-time))))
  15208. ; (setq year (1+ year)
  15209. ; iso-date (calendar-gregorian-from-absolute
  15210. ; (calendar-absolute-from-iso
  15211. ; (list iso-week day year)))))
  15212. (setq month (car iso-date)
  15213. year (nth 2 iso-date)
  15214. day (nth 1 iso-date)))
  15215. (deltan
  15216. (setq futurep nil)
  15217. (unless deltadef
  15218. (let ((now (decode-time (current-time))))
  15219. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15220. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15221. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15222. ((equal deltaw "m") (setq month (+ month deltan)))
  15223. ((equal deltaw "y") (setq year (+ year deltan)))))
  15224. ((and wday (not (nth 3 tl)))
  15225. ;; Weekday was given, but no day, so pick that day in the week
  15226. ;; on or after the derived date.
  15227. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15228. (unless (equal wday wday1)
  15229. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15230. (if (and (boundp 'org-time-was-given)
  15231. (nth 2 tl))
  15232. (setq org-time-was-given t))
  15233. (if (< year 100) (setq year (+ 2000 year)))
  15234. ;; Check of the date is representable
  15235. (if org-read-date-force-compatible-dates
  15236. (progn
  15237. (if (< year 1970)
  15238. (setq year 1970 org-read-date-analyze-forced-year t))
  15239. (if (> year 2037)
  15240. (setq year 2037 org-read-date-analyze-forced-year t)))
  15241. (condition-case nil
  15242. (ignore (encode-time second minute hour day month year))
  15243. (error
  15244. (setq year (nth 5 org-defdecode))
  15245. (setq org-read-date-analyze-forced-year t))))
  15246. (setq org-read-date-analyze-futurep futurep)
  15247. (list second minute hour day month year)))
  15248. (defvar parse-time-weekdays)
  15249. (defun org-read-date-get-relative (s today default)
  15250. "Check string S for special relative date string.
  15251. TODAY and DEFAULT are internal times, for today and for a default.
  15252. Return shift list (N what def-flag)
  15253. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15254. N is the number of WHATs to shift.
  15255. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15256. the DEFAULT date rather than TODAY."
  15257. (require 'parse-time)
  15258. (when (and
  15259. (string-match
  15260. (concat
  15261. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15262. "\\([0-9]+\\)?"
  15263. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15264. "\\([ \t]\\|$\\)") s)
  15265. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15266. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15267. (string-to-char (substring (match-string 1 s) -1))
  15268. ?+))
  15269. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15270. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15271. (what (if (match-end 3) (match-string 3 s) "d"))
  15272. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15273. (date (if rel default today))
  15274. (wday (nth 6 (decode-time date)))
  15275. delta)
  15276. (if wday1
  15277. (progn
  15278. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15279. (if (= delta 0) (setq delta 7))
  15280. (if (= dir ?-)
  15281. (progn
  15282. (setq delta (- delta 7))
  15283. (if (= delta 0) (setq delta -7))))
  15284. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15285. (list delta "d" rel))
  15286. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15287. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15288. "Turn a user-specified date into the internal representation.
  15289. The internal representation needed by the calendar is (month day year).
  15290. This is a wrapper to handle the brain-dead convention in calendar that
  15291. user function argument order change dependent on argument order."
  15292. (if (boundp 'calendar-date-style)
  15293. (cond
  15294. ((eq calendar-date-style 'american)
  15295. (list arg1 arg2 arg3))
  15296. ((eq calendar-date-style 'european)
  15297. (list arg2 arg1 arg3))
  15298. ((eq calendar-date-style 'iso)
  15299. (list arg2 arg3 arg1)))
  15300. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15301. (if (org-bound-and-true-p european-calendar-style)
  15302. (list arg2 arg1 arg3)
  15303. (list arg1 arg2 arg3)))))
  15304. (defun org-eval-in-calendar (form &optional keepdate)
  15305. "Eval FORM in the calendar window and return to current window.
  15306. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15307. otherwise stick to the current value of `org-ans2'."
  15308. (let ((sf (selected-frame))
  15309. (sw (selected-window)))
  15310. (select-window (get-buffer-window "*Calendar*" t))
  15311. (eval form)
  15312. (when (and (not keepdate) (calendar-cursor-to-date))
  15313. (let* ((date (calendar-cursor-to-date))
  15314. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15315. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15316. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15317. (select-window sw)
  15318. (org-select-frame-set-input-focus sf)))
  15319. (defun org-calendar-select ()
  15320. "Return to `org-read-date' with the date currently selected.
  15321. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15322. (interactive)
  15323. (when (calendar-cursor-to-date)
  15324. (let* ((date (calendar-cursor-to-date))
  15325. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15326. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15327. (if (active-minibuffer-window) (exit-minibuffer))))
  15328. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15329. "Insert a date stamp for the date given by the internal TIME.
  15330. WITH-HM means use the stamp format that includes the time of the day.
  15331. INACTIVE means use square brackets instead of angular ones, so that the
  15332. stamp will not contribute to the agenda.
  15333. PRE and POST are optional strings to be inserted before and after the
  15334. stamp.
  15335. The command returns the inserted time stamp."
  15336. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15337. stamp)
  15338. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15339. (insert-before-markers (or pre ""))
  15340. (when (listp extra)
  15341. (setq extra (car extra))
  15342. (if (and (stringp extra)
  15343. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15344. (setq extra (format "-%02d:%02d"
  15345. (string-to-number (match-string 1 extra))
  15346. (string-to-number (match-string 2 extra))))
  15347. (setq extra nil)))
  15348. (when extra
  15349. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15350. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15351. (insert-before-markers (or post ""))
  15352. (setq org-last-inserted-timestamp stamp)))
  15353. (defun org-toggle-time-stamp-overlays ()
  15354. "Toggle the use of custom time stamp formats."
  15355. (interactive)
  15356. (setq org-display-custom-times (not org-display-custom-times))
  15357. (unless org-display-custom-times
  15358. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15359. (while (setq p (next-single-property-change p 'display))
  15360. (if (and (get-text-property p 'display)
  15361. (eq (get-text-property p 'face) 'org-date))
  15362. (remove-text-properties
  15363. p (setq p (next-single-property-change p 'display))
  15364. '(display t))))
  15365. (set-buffer-modified-p bmp)))
  15366. (if (featurep 'xemacs)
  15367. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15368. (org-restart-font-lock)
  15369. (setq org-table-may-need-update t)
  15370. (if org-display-custom-times
  15371. (message "Time stamps are overlaid with custom format")
  15372. (message "Time stamp overlays removed")))
  15373. (defun org-display-custom-time (beg end)
  15374. "Overlay modified time stamp format over timestamp between BEG and END."
  15375. (let* ((ts (buffer-substring beg end))
  15376. t1 w1 with-hm tf time str w2 (off 0))
  15377. (save-match-data
  15378. (setq t1 (org-parse-time-string ts t))
  15379. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15380. (setq off (- (match-end 0) (match-beginning 0)))))
  15381. (setq end (- end off))
  15382. (setq w1 (- end beg)
  15383. with-hm (and (nth 1 t1) (nth 2 t1))
  15384. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15385. time (org-fix-decoded-time t1)
  15386. str (org-add-props
  15387. (format-time-string
  15388. (substring tf 1 -1) (apply 'encode-time time))
  15389. nil 'mouse-face 'highlight)
  15390. w2 (length str))
  15391. (if (not (= w2 w1))
  15392. (add-text-properties (1+ beg) (+ 2 beg)
  15393. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15394. (if (featurep 'xemacs)
  15395. (progn
  15396. (put-text-property beg end 'invisible t)
  15397. (put-text-property beg end 'end-glyph (make-glyph str)))
  15398. (put-text-property beg end 'display str))))
  15399. (defun org-translate-time (string)
  15400. "Translate all timestamps in STRING to custom format.
  15401. But do this only if the variable `org-display-custom-times' is set."
  15402. (when org-display-custom-times
  15403. (save-match-data
  15404. (let* ((start 0)
  15405. (re org-ts-regexp-both)
  15406. t1 with-hm inactive tf time str beg end)
  15407. (while (setq start (string-match re string start))
  15408. (setq beg (match-beginning 0)
  15409. end (match-end 0)
  15410. t1 (save-match-data
  15411. (org-parse-time-string (substring string beg end) t))
  15412. with-hm (and (nth 1 t1) (nth 2 t1))
  15413. inactive (equal (substring string beg (1+ beg)) "[")
  15414. tf (funcall (if with-hm 'cdr 'car)
  15415. org-time-stamp-custom-formats)
  15416. time (org-fix-decoded-time t1)
  15417. str (format-time-string
  15418. (concat
  15419. (if inactive "[" "<") (substring tf 1 -1)
  15420. (if inactive "]" ">"))
  15421. (apply 'encode-time time))
  15422. string (replace-match str t t string)
  15423. start (+ start (length str)))))))
  15424. string)
  15425. (defun org-fix-decoded-time (time)
  15426. "Set 0 instead of nil for the first 6 elements of time.
  15427. Don't touch the rest."
  15428. (let ((n 0))
  15429. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15430. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15431. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15432. "Difference between TIMESTAMP-STRING and now in days.
  15433. If SECONDS is non-nil, return the difference in seconds."
  15434. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15435. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15436. (funcall fdiff (current-time)))))
  15437. (defun org-deadline-close (timestamp-string &optional ndays)
  15438. "Is the time in TIMESTAMP-STRING close to the current date?"
  15439. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15440. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15441. (not (org-entry-is-done-p))))
  15442. (defun org-get-wdays (ts &optional delay zero-delay)
  15443. "Get the deadline lead time appropriate for timestring TS.
  15444. When DELAY is non-nil, get the delay time for scheduled items
  15445. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15446. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15447. don't try to find the delay cookie in the scheduled timestamp."
  15448. (let ((tv (if delay org-scheduled-delay-days
  15449. org-deadline-warning-days)))
  15450. (cond
  15451. ((or (and delay (< tv 0))
  15452. (and delay zero-delay (<= tv 0))
  15453. (and (not delay) (<= tv 0)))
  15454. ;; Enforce this value no matter what
  15455. (- tv))
  15456. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15457. ;; lead time is specified.
  15458. (floor (* (string-to-number (match-string 1 ts))
  15459. (cdr (assoc (match-string 2 ts)
  15460. '(("d" . 1) ("w" . 7)
  15461. ("m" . 30.4) ("y" . 365.25)
  15462. ("h" . 0.041667)))))))
  15463. ;; go for the default.
  15464. (t tv))))
  15465. (defun org-calendar-select-mouse (ev)
  15466. "Return to `org-read-date' with the date currently selected.
  15467. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15468. (interactive "e")
  15469. (mouse-set-point ev)
  15470. (when (calendar-cursor-to-date)
  15471. (let* ((date (calendar-cursor-to-date))
  15472. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15473. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15474. (if (active-minibuffer-window) (exit-minibuffer))))
  15475. (defun org-check-deadlines (ndays)
  15476. "Check if there are any deadlines due or past due.
  15477. A deadline is considered due if it happens within `org-deadline-warning-days'
  15478. days from today's date. If the deadline appears in an entry marked DONE,
  15479. it is not shown. The prefix arg NDAYS can be used to test that many
  15480. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15481. (interactive "P")
  15482. (let* ((org-warn-days
  15483. (cond
  15484. ((equal ndays '(4)) 100000)
  15485. (ndays (prefix-numeric-value ndays))
  15486. (t (abs org-deadline-warning-days))))
  15487. (case-fold-search nil)
  15488. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15489. (callback
  15490. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15491. (message "%d deadlines past-due or due within %d days"
  15492. (org-occur regexp nil callback)
  15493. org-warn-days)))
  15494. (defsubst org-re-timestamp (type)
  15495. "Return a regexp for timestamp TYPE.
  15496. Allowed values for TYPE are:
  15497. all: all timestamps
  15498. active: only active timestamps (<...>)
  15499. inactive: only inactive timestamps ([...])
  15500. scheduled: only scheduled timestamps
  15501. deadline: only deadline timestamps
  15502. closed: only closed time-stamps
  15503. When TYPE is nil, fall back on returning a regexp that matches
  15504. both scheduled and deadline timestamps."
  15505. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15506. ((eq type 'active) org-ts-regexp)
  15507. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15508. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15509. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15510. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15511. ((eq type 'scheduled-or-deadline)
  15512. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15513. (defun org-check-before-date (date)
  15514. "Check if there are deadlines or scheduled entries before DATE."
  15515. (interactive (list (org-read-date)))
  15516. (let ((case-fold-search nil)
  15517. (regexp (org-re-timestamp org-ts-type))
  15518. (callback
  15519. (lambda () (time-less-p
  15520. (org-time-string-to-time (match-string 1))
  15521. (org-time-string-to-time date)))))
  15522. (message "%d entries before %s"
  15523. (org-occur regexp nil callback) date)))
  15524. (defun org-check-after-date (date)
  15525. "Check if there are deadlines or scheduled entries after DATE."
  15526. (interactive (list (org-read-date)))
  15527. (let ((case-fold-search nil)
  15528. (regexp (org-re-timestamp org-ts-type))
  15529. (callback
  15530. (lambda () (not
  15531. (time-less-p
  15532. (org-time-string-to-time (match-string 1))
  15533. (org-time-string-to-time date))))))
  15534. (message "%d entries after %s"
  15535. (org-occur regexp nil callback) date)))
  15536. (defun org-check-dates-range (start-date end-date)
  15537. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15538. (interactive (list (org-read-date nil nil nil "Range starts")
  15539. (org-read-date nil nil nil "Range end")))
  15540. (let ((case-fold-search nil)
  15541. (regexp (org-re-timestamp org-ts-type))
  15542. (callback
  15543. (lambda ()
  15544. (let ((match (match-string 1)))
  15545. (and
  15546. (not (time-less-p
  15547. (org-time-string-to-time match)
  15548. (org-time-string-to-time start-date)))
  15549. (time-less-p
  15550. (org-time-string-to-time match)
  15551. (org-time-string-to-time end-date)))))))
  15552. (message "%d entries between %s and %s"
  15553. (org-occur regexp nil callback) start-date end-date)))
  15554. (defun org-evaluate-time-range (&optional to-buffer)
  15555. "Evaluate a time range by computing the difference between start and end.
  15556. Normally the result is just printed in the echo area, but with prefix arg
  15557. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15558. If the time range is actually in a table, the result is inserted into the
  15559. next column.
  15560. For time difference computation, a year is assumed to be exactly 365
  15561. days in order to avoid rounding problems."
  15562. (interactive "P")
  15563. (or
  15564. (org-clock-update-time-maybe)
  15565. (save-excursion
  15566. (unless (org-at-date-range-p t)
  15567. (goto-char (point-at-bol))
  15568. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15569. (if (not (org-at-date-range-p t))
  15570. (user-error "Not at a time-stamp range, and none found in current line")))
  15571. (let* ((ts1 (match-string 1))
  15572. (ts2 (match-string 2))
  15573. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15574. (match-end (match-end 0))
  15575. (time1 (org-time-string-to-time ts1))
  15576. (time2 (org-time-string-to-time ts2))
  15577. (t1 (org-float-time time1))
  15578. (t2 (org-float-time time2))
  15579. (diff (abs (- t2 t1)))
  15580. (negative (< (- t2 t1) 0))
  15581. ;; (ys (floor (* 365 24 60 60)))
  15582. (ds (* 24 60 60))
  15583. (hs (* 60 60))
  15584. (fy "%dy %dd %02d:%02d")
  15585. (fy1 "%dy %dd")
  15586. (fd "%dd %02d:%02d")
  15587. (fd1 "%dd")
  15588. (fh "%02d:%02d")
  15589. y d h m align)
  15590. (if havetime
  15591. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15592. y 0
  15593. d (floor (/ diff ds)) diff (mod diff ds)
  15594. h (floor (/ diff hs)) diff (mod diff hs)
  15595. m (floor (/ diff 60)))
  15596. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15597. y 0
  15598. d (floor (+ (/ diff ds) 0.5))
  15599. h 0 m 0))
  15600. (if (not to-buffer)
  15601. (message "%s" (org-make-tdiff-string y d h m))
  15602. (if (org-at-table-p)
  15603. (progn
  15604. (goto-char match-end)
  15605. (setq align t)
  15606. (and (looking-at " *|") (goto-char (match-end 0))))
  15607. (goto-char match-end))
  15608. (if (looking-at
  15609. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15610. (replace-match ""))
  15611. (if negative (insert " -"))
  15612. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15613. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15614. (insert " " (format fh h m))))
  15615. (if align (org-table-align))
  15616. (message "Time difference inserted")))))
  15617. (defun org-make-tdiff-string (y d h m)
  15618. (let ((fmt "")
  15619. (l nil))
  15620. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15621. l (push y l)))
  15622. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15623. l (push d l)))
  15624. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15625. l (push h l)))
  15626. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15627. l (push m l)))
  15628. (apply 'format fmt (nreverse l))))
  15629. (defun org-time-string-to-time (s &optional buffer pos)
  15630. "Convert a timestamp string into internal time."
  15631. (condition-case errdata
  15632. (apply 'encode-time (org-parse-time-string s))
  15633. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15634. s (if (not (and buffer pos))
  15635. ""
  15636. (format " at %d in buffer `%s'" pos buffer))
  15637. (cdr errdata)))))
  15638. (defun org-time-string-to-seconds (s)
  15639. "Convert a timestamp string to a number of seconds."
  15640. (org-float-time (org-time-string-to-time s)))
  15641. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15642. "Convert a time stamp to an absolute day number.
  15643. If there is a specifier for a cyclic time stamp, get the closest
  15644. date to DAYNR.
  15645. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15646. The variable `date' is bound by the calendar when this is called."
  15647. (cond
  15648. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15649. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15650. daynr
  15651. (+ daynr 1000)))
  15652. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15653. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15654. (time-to-days (current-time))) (match-string 0 s)
  15655. prefer show-all))
  15656. (t (time-to-days
  15657. (condition-case errdata
  15658. (apply 'encode-time (org-parse-time-string s))
  15659. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15660. s (if (not (and buffer pos))
  15661. ""
  15662. (format " at %d in buffer `%s'" pos buffer))
  15663. (cdr errdata))))))))
  15664. (defun org-days-to-iso-week (days)
  15665. "Return the iso week number."
  15666. (require 'cal-iso)
  15667. (car (calendar-iso-from-absolute days)))
  15668. (defun org-small-year-to-year (year)
  15669. "Convert 2-digit years into 4-digit years.
  15670. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15671. The year 2000 cannot be abbreviated. Any year larger than 99
  15672. is returned unchanged."
  15673. (if (< year 38)
  15674. (setq year (+ 2000 year))
  15675. (if (< year 100)
  15676. (setq year (+ 1900 year))))
  15677. year)
  15678. (defun org-time-from-absolute (d)
  15679. "Return the time corresponding to date D.
  15680. D may be an absolute day number, or a calendar-type list (month day year)."
  15681. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15682. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15683. (defun org-calendar-holiday ()
  15684. "List of holidays, for Diary display in Org-mode."
  15685. (require 'holidays)
  15686. (let ((hl (funcall
  15687. (if (fboundp 'calendar-check-holidays)
  15688. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15689. (if hl (mapconcat 'identity hl "; "))))
  15690. (defun org-diary-sexp-entry (sexp entry date)
  15691. "Process a SEXP diary ENTRY for DATE."
  15692. (require 'diary-lib)
  15693. (let ((result (if calendar-debug-sexp
  15694. (let ((stack-trace-on-error t))
  15695. (eval (car (read-from-string sexp))))
  15696. (condition-case nil
  15697. (eval (car (read-from-string sexp)))
  15698. (error
  15699. (beep)
  15700. (message "Bad sexp at line %d in %s: %s"
  15701. (org-current-line)
  15702. (buffer-file-name) sexp)
  15703. (sleep-for 2))))))
  15704. (cond ((stringp result) (split-string result "; "))
  15705. ((and (consp result)
  15706. (not (consp (cdr result)))
  15707. (stringp (cdr result))) (cdr result))
  15708. ((and (consp result)
  15709. (stringp (car result))) result)
  15710. (result entry))))
  15711. (defun org-diary-to-ical-string (frombuf)
  15712. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15713. This uses the icalendar.el library."
  15714. (let* ((tmpdir (if (featurep 'xemacs)
  15715. (temp-directory)
  15716. temporary-file-directory))
  15717. (tmpfile (make-temp-name
  15718. (expand-file-name "orgics" tmpdir)))
  15719. buf rtn b e)
  15720. (with-current-buffer frombuf
  15721. (icalendar-export-region (point-min) (point-max) tmpfile)
  15722. (setq buf (find-buffer-visiting tmpfile))
  15723. (set-buffer buf)
  15724. (goto-char (point-min))
  15725. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15726. (setq b (match-beginning 0)))
  15727. (goto-char (point-max))
  15728. (if (re-search-backward "^END:VEVENT" nil t)
  15729. (setq e (match-end 0)))
  15730. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15731. (kill-buffer buf)
  15732. (delete-file tmpfile)
  15733. rtn))
  15734. (defun org-closest-date (start current change prefer show-all)
  15735. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15736. When PREFER is `past', return a date that is either CURRENT or past.
  15737. When PREFER is `future', return a date that is either CURRENT or future.
  15738. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15739. ;; Make the proper lists from the dates
  15740. (catch 'exit
  15741. (let ((a1 '(("h" . hour)
  15742. ("d" . day)
  15743. ("w" . week)
  15744. ("m" . month)
  15745. ("y" . year)))
  15746. (shour (nth 2 (org-parse-time-string start)))
  15747. dn dw sday cday n1 n2 n0
  15748. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15749. (setq start (org-date-to-gregorian start)
  15750. current (org-date-to-gregorian
  15751. (if show-all
  15752. current
  15753. (time-to-days (current-time))))
  15754. sday (calendar-absolute-from-gregorian start)
  15755. cday (calendar-absolute-from-gregorian current))
  15756. (if (<= cday sday) (throw 'exit sday))
  15757. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15758. (setq dn (string-to-number (match-string 1 change))
  15759. dw (cdr (assoc (match-string 2 change) a1)))
  15760. (user-error "Invalid change specifier: %s" change))
  15761. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15762. (cond
  15763. ((eq dw 'hour)
  15764. (let ((missing-hours
  15765. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15766. dn)))
  15767. (setq n1 (if (zerop missing-hours) cday
  15768. (- cday (1+ (floor (/ missing-hours 24)))))
  15769. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15770. ((eq dw 'day)
  15771. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15772. n2 (+ n1 dn)))
  15773. ((eq dw 'year)
  15774. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15775. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15776. (setq date1 (list m d y1)
  15777. n1 (calendar-absolute-from-gregorian date1)
  15778. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15779. n2 (calendar-absolute-from-gregorian date2)))
  15780. ((eq dw 'month)
  15781. ;; approx number of month between the two dates
  15782. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15783. ;; How often does dn fit in there?
  15784. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15785. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15786. m (+ m nm)
  15787. ny (floor (/ m 12))
  15788. y (+ y ny)
  15789. m (- m (* ny 12)))
  15790. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15791. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15792. (setq m2 (+ m dn) y2 y)
  15793. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15794. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15795. (while (<= n2 cday)
  15796. (setq n1 n2 m m2 y y2)
  15797. (setq m2 (+ m dn) y2 y)
  15798. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15799. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15800. ;; Make sure n1 is the earlier date
  15801. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15802. (if show-all
  15803. (cond
  15804. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15805. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15806. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15807. (cond
  15808. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15809. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15810. (t (if (= cday n1) n1 n2)))))))
  15811. (defun org-date-to-gregorian (date)
  15812. "Turn any specification of DATE into a Gregorian date for the calendar."
  15813. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15814. ((and (listp date) (= (length date) 3)) date)
  15815. ((stringp date)
  15816. (setq date (org-parse-time-string date))
  15817. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15818. ((listp date)
  15819. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15820. (defun org-parse-time-string (s &optional nodefault)
  15821. "Parse the standard Org-mode time string.
  15822. This should be a lot faster than the normal `parse-time-string'.
  15823. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15824. hour and minute fields will be nil if not given."
  15825. (cond ((string-match org-ts-regexp0 s)
  15826. (list 0
  15827. (if (or (match-beginning 8) (not nodefault))
  15828. (string-to-number (or (match-string 8 s) "0")))
  15829. (if (or (match-beginning 7) (not nodefault))
  15830. (string-to-number (or (match-string 7 s) "0")))
  15831. (string-to-number (match-string 4 s))
  15832. (string-to-number (match-string 3 s))
  15833. (string-to-number (match-string 2 s))
  15834. nil nil nil))
  15835. ((string-match "^<[^>]+>$" s)
  15836. (decode-time (seconds-to-time (org-matcher-time s))))
  15837. (t (error "Not a standard Org-mode time string: %s" s))))
  15838. (defun org-timestamp-up (&optional arg)
  15839. "Increase the date item at the cursor by one.
  15840. If the cursor is on the year, change the year. If it is on the month,
  15841. the day or the time, change that.
  15842. With prefix ARG, change by that many units."
  15843. (interactive "p")
  15844. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15845. (defun org-timestamp-down (&optional arg)
  15846. "Decrease the date item at the cursor by one.
  15847. If the cursor is on the year, change the year. If it is on the month,
  15848. the day or the time, change that.
  15849. With prefix ARG, change by that many units."
  15850. (interactive "p")
  15851. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15852. (defun org-timestamp-up-day (&optional arg)
  15853. "Increase the date in the time stamp by one day.
  15854. With prefix ARG, change that many days."
  15855. (interactive "p")
  15856. (if (and (not (org-at-timestamp-p t))
  15857. (org-at-heading-p))
  15858. (org-todo 'up)
  15859. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15860. (defun org-timestamp-down-day (&optional arg)
  15861. "Decrease the date in the time stamp by one day.
  15862. With prefix ARG, change that many days."
  15863. (interactive "p")
  15864. (if (and (not (org-at-timestamp-p t))
  15865. (org-at-heading-p))
  15866. (org-todo 'down)
  15867. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15868. (defun org-at-timestamp-p (&optional inactive-ok)
  15869. "Determine if the cursor is in or at a timestamp."
  15870. (interactive)
  15871. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15872. (pos (point))
  15873. (ans (or (looking-at tsr)
  15874. (save-excursion
  15875. (skip-chars-backward "^[<\n\r\t")
  15876. (if (> (point) (point-min)) (backward-char 1))
  15877. (and (looking-at tsr)
  15878. (> (- (match-end 0) pos) -1))))))
  15879. (and ans
  15880. (boundp 'org-ts-what)
  15881. (setq org-ts-what
  15882. (cond
  15883. ((= pos (match-beginning 0)) 'bracket)
  15884. ;; Point is considered to be "on the bracket" whether
  15885. ;; it's really on it or right after it.
  15886. ((= pos (1- (match-end 0))) 'bracket)
  15887. ((= pos (match-end 0)) 'after)
  15888. ((org-pos-in-match-range pos 2) 'year)
  15889. ((org-pos-in-match-range pos 3) 'month)
  15890. ((org-pos-in-match-range pos 7) 'hour)
  15891. ((org-pos-in-match-range pos 8) 'minute)
  15892. ((or (org-pos-in-match-range pos 4)
  15893. (org-pos-in-match-range pos 5)) 'day)
  15894. ((and (> pos (or (match-end 8) (match-end 5)))
  15895. (< pos (match-end 0)))
  15896. (- pos (or (match-end 8) (match-end 5))))
  15897. (t 'day))))
  15898. ans))
  15899. (defun org-toggle-timestamp-type ()
  15900. "Toggle the type (<active> or [inactive]) of a time stamp."
  15901. (interactive)
  15902. (when (org-at-timestamp-p t)
  15903. (let ((beg (match-beginning 0)) (end (match-end 0))
  15904. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15905. (save-excursion
  15906. (goto-char beg)
  15907. (while (re-search-forward "[][<>]" end t)
  15908. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15909. t t)))
  15910. (message "Timestamp is now %sactive"
  15911. (if (equal (char-after beg) ?<) "" "in")))))
  15912. (defun org-at-clock-log-p nil
  15913. "Is the cursor on the clock log line?"
  15914. (save-excursion
  15915. (move-beginning-of-line 1)
  15916. (looking-at "^[ \t]*CLOCK:")))
  15917. (defvar org-clock-history) ; defined in org-clock.el
  15918. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15919. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15920. "Change the date in the time stamp at point.
  15921. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15922. `year', `minute', `second'. If WHAT is not given, the cursor position
  15923. in the timestamp determines what will be changed.
  15924. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15925. (let ((origin (point)) origin-cat
  15926. with-hm inactive
  15927. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15928. org-ts-what
  15929. extra rem
  15930. ts time time0 fixnext clrgx)
  15931. (if (not (org-at-timestamp-p t))
  15932. (user-error "Not at a timestamp"))
  15933. (if (and (not what) (eq org-ts-what 'bracket))
  15934. (org-toggle-timestamp-type)
  15935. ;; Point isn't on brackets. Remember the part of the time-stamp
  15936. ;; the point was in. Indeed, size of time-stamps may change,
  15937. ;; but point must be kept in the same category nonetheless.
  15938. (setq origin-cat org-ts-what)
  15939. (if (and (not what) (not (eq org-ts-what 'day))
  15940. org-display-custom-times
  15941. (get-text-property (point) 'display)
  15942. (not (get-text-property (1- (point)) 'display)))
  15943. (setq org-ts-what 'day))
  15944. (setq org-ts-what (or what org-ts-what)
  15945. inactive (= (char-after (match-beginning 0)) ?\[)
  15946. ts (match-string 0))
  15947. (replace-match "")
  15948. (when (string-match
  15949. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15950. ts)
  15951. (setq extra (match-string 1 ts))
  15952. (if suppress-tmp-delay
  15953. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15954. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15955. (setq with-hm t))
  15956. (setq time0 (org-parse-time-string ts))
  15957. (when (and updown
  15958. (eq org-ts-what 'minute)
  15959. (not current-prefix-arg))
  15960. ;; This looks like s-up and s-down. Change by one rounding step.
  15961. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15962. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15963. (setcar (cdr time0) (+ (nth 1 time0)
  15964. (if (> n 0) (- rem) (- dm rem))))))
  15965. (setq time
  15966. (encode-time (or (car time0) 0)
  15967. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15968. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15969. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15970. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15971. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15972. (nthcdr 6 time0)))
  15973. (when (and (member org-ts-what '(hour minute))
  15974. extra
  15975. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15976. (setq extra (org-modify-ts-extra
  15977. extra
  15978. (if (eq org-ts-what 'hour) 2 5)
  15979. n dm)))
  15980. (when (integerp org-ts-what)
  15981. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15982. (if (eq what 'calendar)
  15983. (let ((cal-date (org-get-date-from-calendar)))
  15984. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15985. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15986. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15987. (setcar time0 (or (car time0) 0))
  15988. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15989. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15990. (setq time (apply 'encode-time time0))))
  15991. ;; Insert the new time-stamp, and ensure point stays in the same
  15992. ;; category as before (i.e. not after the last position in that
  15993. ;; category).
  15994. (let ((pos (point)))
  15995. ;; Stay before inserted string. `save-excursion' is of no use.
  15996. (setq org-last-changed-timestamp
  15997. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15998. (goto-char pos))
  15999. (save-match-data
  16000. (looking-at org-ts-regexp3)
  16001. (goto-char (cond
  16002. ;; `day' category ends before `hour' if any, or at
  16003. ;; the end of the day name.
  16004. ((eq origin-cat 'day)
  16005. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16006. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16007. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16008. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16009. ;; `year' and `month' have both fixed size: point
  16010. ;; couldn't have moved into another part.
  16011. (t origin))))
  16012. ;; Update clock if on a CLOCK line.
  16013. (org-clock-update-time-maybe)
  16014. ;; Maybe adjust the closest clock in `org-clock-history'
  16015. (when org-clock-adjust-closest
  16016. (if (not (and (org-at-clock-log-p)
  16017. (< 1 (length (delq nil (mapcar 'marker-position
  16018. org-clock-history))))))
  16019. (message "No clock to adjust")
  16020. (cond ((save-excursion ; fix previous clock?
  16021. (re-search-backward org-ts-regexp0 nil t)
  16022. (org-looking-back (concat org-clock-string " \\[")))
  16023. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16024. ((save-excursion ; fix next clock?
  16025. (re-search-backward org-ts-regexp0 nil t)
  16026. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16027. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16028. (save-window-excursion
  16029. ;; Find closest clock to point, adjust the previous/next one in history
  16030. (let* ((p (save-excursion (org-back-to-heading t)))
  16031. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16032. (clfixnth
  16033. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16034. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16035. (if (not clfixpos)
  16036. (message "No clock to adjust")
  16037. (save-excursion
  16038. (org-goto-marker-or-bmk clfixpos)
  16039. (org-show-subtree)
  16040. (when (re-search-forward clrgx nil t)
  16041. (goto-char (match-beginning 1))
  16042. (let (org-clock-adjust-closest)
  16043. (org-timestamp-change n org-ts-what updown))
  16044. (message "Clock adjusted in %s for heading: %s"
  16045. (file-name-nondirectory (buffer-file-name))
  16046. (org-get-heading t t)))))))))
  16047. ;; Try to recenter the calendar window, if any.
  16048. (if (and org-calendar-follow-timestamp-change
  16049. (get-buffer-window "*Calendar*" t)
  16050. (memq org-ts-what '(day month year)))
  16051. (org-recenter-calendar (time-to-days time))))))
  16052. (defun org-modify-ts-extra (s pos n dm)
  16053. "Change the different parts of the lead-time and repeat fields in timestamp."
  16054. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16055. ng h m new rem)
  16056. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16057. (cond
  16058. ((or (org-pos-in-match-range pos 2)
  16059. (org-pos-in-match-range pos 3))
  16060. (setq m (string-to-number (match-string 3 s))
  16061. h (string-to-number (match-string 2 s)))
  16062. (if (org-pos-in-match-range pos 2)
  16063. (setq h (+ h n))
  16064. (setq n (* dm (org-no-warnings (signum n))))
  16065. (when (not (= 0 (setq rem (% m dm))))
  16066. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16067. (setq m (+ m n)))
  16068. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16069. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16070. (setq h (min 24 (max 0 h)))
  16071. (setq ng 1 new (format "-%02d:%02d" h m)))
  16072. ((org-pos-in-match-range pos 6)
  16073. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16074. ((org-pos-in-match-range pos 5)
  16075. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16076. ((org-pos-in-match-range pos 9)
  16077. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16078. ((org-pos-in-match-range pos 8)
  16079. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16080. (when ng
  16081. (setq s (concat
  16082. (substring s 0 (match-beginning ng))
  16083. new
  16084. (substring s (match-end ng))))))
  16085. s))
  16086. (defun org-recenter-calendar (date)
  16087. "If the calendar is visible, recenter it to DATE."
  16088. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16089. (when cwin
  16090. (let ((calendar-move-hook nil))
  16091. (with-selected-window cwin
  16092. (calendar-goto-date (if (listp date) date
  16093. (calendar-gregorian-from-absolute date))))))))
  16094. (defun org-goto-calendar (&optional arg)
  16095. "Go to the Emacs calendar at the current date.
  16096. If there is a time stamp in the current line, go to that date.
  16097. A prefix ARG can be used to force the current date."
  16098. (interactive "P")
  16099. (let ((tsr org-ts-regexp) diff
  16100. (calendar-move-hook nil)
  16101. (calendar-view-holidays-initially-flag nil)
  16102. (calendar-view-diary-initially-flag nil))
  16103. (if (or (org-at-timestamp-p)
  16104. (save-excursion
  16105. (beginning-of-line 1)
  16106. (looking-at (concat ".*" tsr))))
  16107. (let ((d1 (time-to-days (current-time)))
  16108. (d2 (time-to-days
  16109. (org-time-string-to-time (match-string 1)))))
  16110. (setq diff (- d2 d1))))
  16111. (calendar)
  16112. (calendar-goto-today)
  16113. (if (and diff (not arg)) (calendar-forward-day diff))))
  16114. (defun org-get-date-from-calendar ()
  16115. "Return a list (month day year) of date at point in calendar."
  16116. (with-current-buffer "*Calendar*"
  16117. (save-match-data
  16118. (calendar-cursor-to-date))))
  16119. (defun org-date-from-calendar ()
  16120. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16121. If there is already a time stamp at the cursor position, update it."
  16122. (interactive)
  16123. (if (org-at-timestamp-p t)
  16124. (org-timestamp-change 0 'calendar)
  16125. (let ((cal-date (org-get-date-from-calendar)))
  16126. (org-insert-time-stamp
  16127. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16128. (defcustom org-effort-durations
  16129. `(("h" . 60)
  16130. ("d" . ,(* 60 8))
  16131. ("w" . ,(* 60 8 5))
  16132. ("m" . ,(* 60 8 5 4))
  16133. ("y" . ,(* 60 8 5 40)))
  16134. "Conversion factor to minutes for an effort modifier.
  16135. Each entry has the form (MODIFIER . MINUTES).
  16136. In an effort string, a number followed by MODIFIER is multiplied
  16137. by the specified number of MINUTES to obtain an effort in
  16138. minutes.
  16139. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16140. effort string \"2hours\" is equivalent to 120 minutes."
  16141. :group 'org-agenda
  16142. :version "24.1"
  16143. :type '(alist :key-type (string :tag "Modifier")
  16144. :value-type (number :tag "Minutes")))
  16145. (defun org-minutes-to-clocksum-string (m)
  16146. "Format number of minutes as a clocksum string.
  16147. The format is determined by `org-time-clocksum-format',
  16148. `org-time-clocksum-use-fractional' and
  16149. `org-time-clocksum-fractional-format' and
  16150. `org-time-clocksum-use-effort-durations'."
  16151. (let ((clocksum "")
  16152. (m (round m)) ; Don't allow fractions of minutes
  16153. h d w mo y fmt n)
  16154. (setq h (if org-time-clocksum-use-effort-durations
  16155. (cdr (assoc "h" org-effort-durations)) 60)
  16156. d (if org-time-clocksum-use-effort-durations
  16157. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16158. w (if org-time-clocksum-use-effort-durations
  16159. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16160. mo (if org-time-clocksum-use-effort-durations
  16161. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16162. y (if org-time-clocksum-use-effort-durations
  16163. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16164. ;; fractional format
  16165. (if org-time-clocksum-use-fractional
  16166. (cond
  16167. ;; single format string
  16168. ((stringp org-time-clocksum-fractional-format)
  16169. (format org-time-clocksum-fractional-format (/ m (float h))))
  16170. ;; choice of fractional formats for different time units
  16171. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16172. (> (/ (truncate m) (* y d h)) 0))
  16173. (format fmt (/ m (* y d (float h)))))
  16174. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16175. (> (/ (truncate m) (* mo d h)) 0))
  16176. (format fmt (/ m (* mo d (float h)))))
  16177. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16178. (> (/ (truncate m) (* w d h)) 0))
  16179. (format fmt (/ m (* w d (float h)))))
  16180. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16181. (> (/ (truncate m) (* d h)) 0))
  16182. (format fmt (/ m (* d (float h)))))
  16183. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16184. (> (/ (truncate m) h) 0))
  16185. (format fmt (/ m (float h))))
  16186. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16187. (format fmt m))
  16188. ;; fall back to smallest time unit with a format
  16189. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16190. (format fmt (/ m (float h))))
  16191. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16192. (format fmt (/ m (* d (float h)))))
  16193. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16194. (format fmt (/ m (* w d (float h)))))
  16195. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16196. (format fmt (/ m (* mo d (float h)))))
  16197. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16198. (format fmt (/ m (* y d (float h))))))
  16199. ;; standard (non-fractional) format, with single format string
  16200. (if (stringp org-time-clocksum-format)
  16201. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16202. ;; separate formats components
  16203. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16204. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16205. (plist-get org-time-clocksum-format :require-years))
  16206. (setq clocksum (concat clocksum (format fmt n))
  16207. m (- m (* n y d h))))
  16208. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16209. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16210. (plist-get org-time-clocksum-format :require-months))
  16211. (setq clocksum (concat clocksum (format fmt n))
  16212. m (- m (* n mo d h))))
  16213. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16214. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16215. (plist-get org-time-clocksum-format :require-weeks))
  16216. (setq clocksum (concat clocksum (format fmt n))
  16217. m (- m (* n w d h))))
  16218. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16219. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16220. (plist-get org-time-clocksum-format :require-days))
  16221. (setq clocksum (concat clocksum (format fmt n))
  16222. m (- m (* n d h))))
  16223. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16224. (or (> (setq n (/ (truncate m) h)) 0)
  16225. (plist-get org-time-clocksum-format :require-hours))
  16226. (setq clocksum (concat clocksum (format fmt n))
  16227. m (- m (* n h))))
  16228. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16229. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16230. (setq clocksum (concat clocksum (format fmt m))))
  16231. ;; return formatted time duration
  16232. clocksum))))
  16233. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16234. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16235. "Org mode version 8.0")
  16236. (defun org-hours-to-clocksum-string (n)
  16237. (org-minutes-to-clocksum-string (* n 60)))
  16238. (defun org-hh:mm-string-to-minutes (s)
  16239. "Convert a string H:MM to a number of minutes.
  16240. If the string is just a number, interpret it as minutes.
  16241. In fact, the first hh:mm or number in the string will be taken,
  16242. there can be extra stuff in the string.
  16243. If no number is found, the return value is 0."
  16244. (cond
  16245. ((integerp s) s)
  16246. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16247. (+ (* (string-to-number (match-string 1 s)) 60)
  16248. (string-to-number (match-string 2 s))))
  16249. ((string-match "\\([0-9]+\\)" s)
  16250. (string-to-number (match-string 1 s)))
  16251. (t 0)))
  16252. (defcustom org-image-actual-width t
  16253. "Should we use the actual width of images when inlining them?
  16254. When set to `t', always use the image width.
  16255. When set to a number, use imagemagick (when available) to set
  16256. the image's width to this value.
  16257. When set to a number in a list, try to get the width from any
  16258. #+ATTR.* keyword if it matches a width specification like
  16259. #+ATTR_HTML: :width 300px
  16260. and fall back on that number if none is found.
  16261. When set to nil, try to get the width from an #+ATTR.* keyword
  16262. and fall back on the original width if none is found.
  16263. This requires Emacs >= 24.1, build with imagemagick support."
  16264. :group 'org-appearance
  16265. :version "24.4"
  16266. :package-version '(Org . "8.0")
  16267. :type '(choice
  16268. (const :tag "Use the image width" t)
  16269. (integer :tag "Use a number of pixels")
  16270. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16271. (const :tag "Use #+ATTR* or don't resize" nil)))
  16272. (defcustom org-agenda-inhibit-startup nil
  16273. "Inhibit startup when preparing agenda buffers.
  16274. When this variable is `t' (the default), the initialization of
  16275. the Org agenda buffers is inhibited: e.g. the visibility state
  16276. is not set, the tables are not re-aligned, etc."
  16277. :type 'boolean
  16278. :version "24.3"
  16279. :group 'org-agenda)
  16280. (defcustom org-agenda-ignore-drawer-properties nil
  16281. "Avoid updating text properties when building the agenda.
  16282. Properties are used to prepare buffers for effort estimates, appointments,
  16283. and subtree-local categories.
  16284. If you don't use these in the agenda, you can add them to this list and
  16285. agenda building will be a bit faster.
  16286. The value is a list, with zero or more of the symbols `effort', `appt',
  16287. or `category'."
  16288. :type '(set :greedy t
  16289. (const effort)
  16290. (const appt)
  16291. (const category))
  16292. :version "24.3"
  16293. :group 'org-agenda)
  16294. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16295. "Convert a duration string S to minutes.
  16296. A bare number is interpreted as minutes, modifiers can be set by
  16297. customizing `org-effort-durations' (which see).
  16298. Entries containing a colon are interpreted as H:MM by
  16299. `org-hh:mm-string-to-minutes'."
  16300. (let ((result 0)
  16301. (re (concat "\\([0-9.]+\\) *\\("
  16302. (regexp-opt (mapcar 'car org-effort-durations))
  16303. "\\)")))
  16304. (while (string-match re s)
  16305. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16306. (string-to-number (match-string 1 s))))
  16307. (setq s (replace-match "" nil t s)))
  16308. (setq result (floor result))
  16309. (incf result (org-hh:mm-string-to-minutes s))
  16310. (if output-to-string (number-to-string result) result)))
  16311. ;;;; Files
  16312. (defun org-save-all-org-buffers ()
  16313. "Save all Org-mode buffers without user confirmation."
  16314. (interactive)
  16315. (message "Saving all Org-mode buffers...")
  16316. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16317. (when (featurep 'org-id) (org-id-locations-save))
  16318. (message "Saving all Org-mode buffers... done"))
  16319. (defun org-revert-all-org-buffers ()
  16320. "Revert all Org-mode buffers.
  16321. Prompt for confirmation when there are unsaved changes.
  16322. Be sure you know what you are doing before letting this function
  16323. overwrite your changes.
  16324. This function is useful in a setup where one tracks org files
  16325. with a version control system, to revert on one machine after pulling
  16326. changes from another. I believe the procedure must be like this:
  16327. 1. M-x org-save-all-org-buffers
  16328. 2. Pull changes from the other machine, resolve conflicts
  16329. 3. M-x org-revert-all-org-buffers"
  16330. (interactive)
  16331. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16332. (user-error "Abort"))
  16333. (save-excursion
  16334. (save-window-excursion
  16335. (mapc
  16336. (lambda (b)
  16337. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16338. (with-current-buffer b buffer-file-name))
  16339. (org-pop-to-buffer-same-window b)
  16340. (revert-buffer t 'no-confirm)))
  16341. (buffer-list))
  16342. (when (and (featurep 'org-id) org-id-track-globally)
  16343. (org-id-locations-load)))))
  16344. ;;;; Agenda files
  16345. ;;;###autoload
  16346. (defun org-switchb (&optional arg)
  16347. "Switch between Org buffers.
  16348. With one prefix argument, restrict available buffers to files.
  16349. With two prefix arguments, restrict available buffers to agenda files.
  16350. Defaults to `iswitchb' for buffer name completion.
  16351. Set `org-completion-use-ido' to make it use ido instead."
  16352. (interactive "P")
  16353. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16354. ((equal arg '(16)) (org-buffer-list 'agenda))
  16355. (t (org-buffer-list))))
  16356. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16357. (org-completion-use-ido org-completion-use-ido))
  16358. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16359. (setq org-completion-use-iswitchb t))
  16360. (org-pop-to-buffer-same-window
  16361. (org-icompleting-read "Org buffer: "
  16362. (mapcar 'list (mapcar 'buffer-name blist))
  16363. nil t))))
  16364. ;;; Define some older names previously used for this functionality
  16365. ;;;###autoload
  16366. (defalias 'org-ido-switchb 'org-switchb)
  16367. ;;;###autoload
  16368. (defalias 'org-iswitchb 'org-switchb)
  16369. (defun org-buffer-list (&optional predicate exclude-tmp)
  16370. "Return a list of Org buffers.
  16371. PREDICATE can be `export', `files' or `agenda'.
  16372. export restrict the list to Export buffers.
  16373. files restrict the list to buffers visiting Org files.
  16374. agenda restrict the list to buffers visiting agenda files.
  16375. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16376. (let* ((bfn nil)
  16377. (agenda-files (and (eq predicate 'agenda)
  16378. (mapcar 'file-truename (org-agenda-files t))))
  16379. (filter
  16380. (cond
  16381. ((eq predicate 'files)
  16382. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16383. ((eq predicate 'export)
  16384. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16385. ((eq predicate 'agenda)
  16386. (lambda (b)
  16387. (with-current-buffer b
  16388. (and (derived-mode-p 'org-mode)
  16389. (setq bfn (buffer-file-name b))
  16390. (member (file-truename bfn) agenda-files)))))
  16391. (t (lambda (b) (with-current-buffer b
  16392. (or (derived-mode-p 'org-mode)
  16393. (string-match "\*Org .*Export"
  16394. (buffer-name b)))))))))
  16395. (delq nil
  16396. (mapcar
  16397. (lambda(b)
  16398. (if (and (funcall filter b)
  16399. (or (not exclude-tmp)
  16400. (not (string-match "tmp" (buffer-name b)))))
  16401. b
  16402. nil))
  16403. (buffer-list)))))
  16404. (defun org-agenda-files (&optional unrestricted archives)
  16405. "Get the list of agenda files.
  16406. Optional UNRESTRICTED means return the full list even if a restriction
  16407. is currently in place.
  16408. When ARCHIVES is t, include all archive files that are really being
  16409. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16410. `org-agenda-archives-mode' is t."
  16411. (let ((files
  16412. (cond
  16413. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16414. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16415. ((listp org-agenda-files) org-agenda-files)
  16416. (t (error "Invalid value of `org-agenda-files'")))))
  16417. (setq files (apply 'append
  16418. (mapcar (lambda (f)
  16419. (if (file-directory-p f)
  16420. (directory-files
  16421. f t org-agenda-file-regexp)
  16422. (list f)))
  16423. files)))
  16424. (when org-agenda-skip-unavailable-files
  16425. (setq files (delq nil
  16426. (mapcar (function
  16427. (lambda (file)
  16428. (and (file-readable-p file) file)))
  16429. files))))
  16430. (when (or (eq archives t)
  16431. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16432. (setq files (org-add-archive-files files)))
  16433. files))
  16434. (defun org-agenda-file-p (&optional file)
  16435. "Return non-nil, if FILE is an agenda file.
  16436. If FILE is omitted, use the file associated with the current
  16437. buffer."
  16438. (member (or file (buffer-file-name))
  16439. (org-agenda-files t)))
  16440. (defun org-edit-agenda-file-list ()
  16441. "Edit the list of agenda files.
  16442. Depending on setup, this either uses customize to edit the variable
  16443. `org-agenda-files', or it visits the file that is holding the list. In the
  16444. latter case, the buffer is set up in a way that saving it automatically kills
  16445. the buffer and restores the previous window configuration."
  16446. (interactive)
  16447. (if (stringp org-agenda-files)
  16448. (let ((cw (current-window-configuration)))
  16449. (find-file org-agenda-files)
  16450. (org-set-local 'org-window-configuration cw)
  16451. (org-add-hook 'after-save-hook
  16452. (lambda ()
  16453. (set-window-configuration
  16454. (prog1 org-window-configuration
  16455. (kill-buffer (current-buffer))))
  16456. (org-install-agenda-files-menu)
  16457. (message "New agenda file list installed"))
  16458. nil 'local)
  16459. (message "%s" (substitute-command-keys
  16460. "Edit list and finish with \\[save-buffer]")))
  16461. (customize-variable 'org-agenda-files)))
  16462. (defun org-store-new-agenda-file-list (list)
  16463. "Set new value for the agenda file list and save it correctly."
  16464. (if (stringp org-agenda-files)
  16465. (let ((fe (org-read-agenda-file-list t)) b u)
  16466. (while (setq b (find-buffer-visiting org-agenda-files))
  16467. (kill-buffer b))
  16468. (with-temp-file org-agenda-files
  16469. (insert
  16470. (mapconcat
  16471. (lambda (f) ;; Keep un-expanded entries.
  16472. (if (setq u (assoc f fe))
  16473. (cdr u)
  16474. f))
  16475. list "\n")
  16476. "\n")))
  16477. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16478. (org-insert-mode-line-in-empty-file nil))
  16479. (setq org-agenda-files list)
  16480. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16481. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16482. "Read the list of agenda files from a file.
  16483. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16484. filenames, used by `org-store-new-agenda-file-list' to write back
  16485. un-expanded file names."
  16486. (when (file-directory-p org-agenda-files)
  16487. (error "`org-agenda-files' cannot be a single directory"))
  16488. (when (stringp org-agenda-files)
  16489. (with-temp-buffer
  16490. (insert-file-contents org-agenda-files)
  16491. (mapcar
  16492. (lambda (f)
  16493. (let ((e (expand-file-name (substitute-in-file-name f)
  16494. org-directory)))
  16495. (if pair-with-expansion
  16496. (cons e f)
  16497. e)))
  16498. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16499. ;;;###autoload
  16500. (defun org-cycle-agenda-files ()
  16501. "Cycle through the files in `org-agenda-files'.
  16502. If the current buffer visits an agenda file, find the next one in the list.
  16503. If the current buffer does not, find the first agenda file."
  16504. (interactive)
  16505. (let* ((fs (org-agenda-files t))
  16506. (files (append fs (list (car fs))))
  16507. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16508. file)
  16509. (unless files (user-error "No agenda files"))
  16510. (catch 'exit
  16511. (while (setq file (pop files))
  16512. (if (equal (file-truename file) tcf)
  16513. (when (car files)
  16514. (find-file (car files))
  16515. (throw 'exit t))))
  16516. (find-file (car fs)))
  16517. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16518. (defun org-agenda-file-to-front (&optional to-end)
  16519. "Move/add the current file to the top of the agenda file list.
  16520. If the file is not present in the list, it is added to the front. If it is
  16521. present, it is moved there. With optional argument TO-END, add/move to the
  16522. end of the list."
  16523. (interactive "P")
  16524. (let ((org-agenda-skip-unavailable-files nil)
  16525. (file-alist (mapcar (lambda (x)
  16526. (cons (file-truename x) x))
  16527. (org-agenda-files t)))
  16528. (ctf (file-truename
  16529. (or buffer-file-name
  16530. (user-error "Please save the current buffer to a file"))))
  16531. x had)
  16532. (setq x (assoc ctf file-alist) had x)
  16533. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16534. (if to-end
  16535. (setq file-alist (append (delq x file-alist) (list x)))
  16536. (setq file-alist (cons x (delq x file-alist))))
  16537. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16538. (org-install-agenda-files-menu)
  16539. (message "File %s to %s of agenda file list"
  16540. (if had "moved" "added") (if to-end "end" "front"))))
  16541. (defun org-remove-file (&optional file)
  16542. "Remove current file from the list of files in variable `org-agenda-files'.
  16543. These are the files which are being checked for agenda entries.
  16544. Optional argument FILE means use this file instead of the current."
  16545. (interactive)
  16546. (let* ((org-agenda-skip-unavailable-files nil)
  16547. (file (or file buffer-file-name
  16548. (user-error "Current buffer does not visit a file")))
  16549. (true-file (file-truename file))
  16550. (afile (abbreviate-file-name file))
  16551. (files (delq nil (mapcar
  16552. (lambda (x)
  16553. (if (equal true-file
  16554. (file-truename x))
  16555. nil x))
  16556. (org-agenda-files t)))))
  16557. (if (not (= (length files) (length (org-agenda-files t))))
  16558. (progn
  16559. (org-store-new-agenda-file-list files)
  16560. (org-install-agenda-files-menu)
  16561. (message "Removed file: %s" afile))
  16562. (message "File was not in list: %s (not removed)" afile))))
  16563. (defun org-file-menu-entry (file)
  16564. (vector file (list 'find-file file) t))
  16565. (defun org-check-agenda-file (file)
  16566. "Make sure FILE exists. If not, ask user what to do."
  16567. (when (not (file-exists-p file))
  16568. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16569. (abbreviate-file-name file))
  16570. (let ((r (downcase (read-char-exclusive))))
  16571. (cond
  16572. ((equal r ?r)
  16573. (org-remove-file file)
  16574. (throw 'nextfile t))
  16575. (t (error "Abort"))))))
  16576. (defun org-get-agenda-file-buffer (file)
  16577. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16578. it to the list of buffers which might be released later."
  16579. (let ((buf (org-find-base-buffer-visiting file)))
  16580. (if buf
  16581. buf ; just return it
  16582. ;; Make a new buffer and remember it
  16583. (setq buf (find-file-noselect file))
  16584. (if buf (push buf org-agenda-new-buffers))
  16585. buf)))
  16586. (defun org-release-buffers (blist)
  16587. "Release all buffers in list, asking the user for confirmation when needed.
  16588. When a buffer is unmodified, it is just killed. When modified, it is saved
  16589. \(if the user agrees) and then killed."
  16590. (let (buf file)
  16591. (while (setq buf (pop blist))
  16592. (setq file (buffer-file-name buf))
  16593. (when (and (buffer-modified-p buf)
  16594. file
  16595. (y-or-n-p (format "Save file %s? " file)))
  16596. (with-current-buffer buf (save-buffer)))
  16597. (kill-buffer buf))))
  16598. (defun org-agenda-prepare-buffers (files)
  16599. "Create buffers for all agenda files, protect archived trees and comments."
  16600. (interactive)
  16601. (let ((pa '(:org-archived t))
  16602. (pc '(:org-comment t))
  16603. (pall '(:org-archived t :org-comment t))
  16604. (inhibit-read-only t)
  16605. (org-inhibit-startup org-agenda-inhibit-startup)
  16606. (rea (concat ":" org-archive-tag ":"))
  16607. file re pos)
  16608. (setq org-tag-alist-for-agenda nil
  16609. org-tag-groups-alist-for-agenda nil)
  16610. (save-excursion
  16611. (save-restriction
  16612. (while (setq file (pop files))
  16613. (catch 'nextfile
  16614. (if (bufferp file)
  16615. (set-buffer file)
  16616. (org-check-agenda-file file)
  16617. (set-buffer (org-get-agenda-file-buffer file)))
  16618. (widen)
  16619. (org-set-regexps-and-options-for-tags)
  16620. (setq pos (point))
  16621. (goto-char (point-min))
  16622. (let ((case-fold-search t))
  16623. (when (search-forward "#+setupfile" nil t)
  16624. ;; Don't set all regexps and options systematically as
  16625. ;; this is only run for setting agenda tags from setup
  16626. ;; file
  16627. (org-set-regexps-and-options)))
  16628. (or (memq 'category org-agenda-ignore-drawer-properties)
  16629. (org-refresh-category-properties))
  16630. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16631. (org-refresh-properties org-effort-property 'org-effort))
  16632. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16633. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16634. (setq org-todo-keywords-for-agenda
  16635. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16636. (setq org-done-keywords-for-agenda
  16637. (append org-done-keywords-for-agenda org-done-keywords))
  16638. (setq org-todo-keyword-alist-for-agenda
  16639. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16640. (setq org-drawers-for-agenda
  16641. (append org-drawers-for-agenda org-drawers))
  16642. (setq org-tag-alist-for-agenda
  16643. (org-uniquify
  16644. (append org-tag-alist-for-agenda
  16645. org-tag-alist
  16646. org-tag-persistent-alist)))
  16647. (if org-group-tags
  16648. (setq org-tag-groups-alist-for-agenda
  16649. (org-uniquify-alist
  16650. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16651. (org-with-silent-modifications
  16652. (save-excursion
  16653. (remove-text-properties (point-min) (point-max) pall)
  16654. (when org-agenda-skip-archived-trees
  16655. (goto-char (point-min))
  16656. (while (re-search-forward rea nil t)
  16657. (if (org-at-heading-p t)
  16658. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16659. (goto-char (point-min))
  16660. (setq re (format org-heading-keyword-regexp-format
  16661. org-comment-string))
  16662. (while (re-search-forward re nil t)
  16663. (add-text-properties
  16664. (match-beginning 0) (org-end-of-subtree t) pc))))
  16665. (goto-char pos)))))
  16666. (setq org-todo-keywords-for-agenda
  16667. (org-uniquify org-todo-keywords-for-agenda))
  16668. (setq org-todo-keyword-alist-for-agenda
  16669. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16670. ;;;; CDLaTeX minor mode
  16671. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16672. "Keymap for the minor `org-cdlatex-mode'.")
  16673. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16674. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16675. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16676. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16677. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16678. (defvar org-cdlatex-texmathp-advice-is-done nil
  16679. "Flag remembering if we have applied the advice to texmathp already.")
  16680. (define-minor-mode org-cdlatex-mode
  16681. "Toggle the minor `org-cdlatex-mode'.
  16682. This mode supports entering LaTeX environment and math in LaTeX fragments
  16683. in Org-mode.
  16684. \\{org-cdlatex-mode-map}"
  16685. nil " OCDL" nil
  16686. (when org-cdlatex-mode
  16687. (require 'cdlatex)
  16688. (run-hooks 'cdlatex-mode-hook)
  16689. (cdlatex-compute-tables))
  16690. (unless org-cdlatex-texmathp-advice-is-done
  16691. (setq org-cdlatex-texmathp-advice-is-done t)
  16692. (defadvice texmathp (around org-math-always-on activate)
  16693. "Always return t in org-mode buffers.
  16694. This is because we want to insert math symbols without dollars even outside
  16695. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16696. an embedded LaTeX fragment, let texmathp do its job.
  16697. \\[org-cdlatex-mode-map]"
  16698. (interactive)
  16699. (let (p)
  16700. (cond
  16701. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16702. ((eq this-command 'cdlatex-math-symbol)
  16703. (setq ad-return-value t
  16704. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16705. (t
  16706. (let ((p (org-inside-LaTeX-fragment-p)))
  16707. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16708. (setq ad-return-value t
  16709. texmathp-why '("Org-mode embedded math" . 0))
  16710. (if p ad-do-it)))))))))
  16711. (defun turn-on-org-cdlatex ()
  16712. "Unconditionally turn on `org-cdlatex-mode'."
  16713. (org-cdlatex-mode 1))
  16714. (defun org-try-cdlatex-tab ()
  16715. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16716. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16717. - inside a LaTeX fragment, or
  16718. - after the first word in a line, where an abbreviation expansion could
  16719. insert a LaTeX environment."
  16720. (when org-cdlatex-mode
  16721. (cond
  16722. ;; Before any word on the line: No expansion possible.
  16723. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16724. ;; Just after first word on the line: Expand it. Make sure it
  16725. ;; cannot happen on headlines, though.
  16726. ((save-excursion
  16727. (skip-chars-backward "a-zA-Z0-9*")
  16728. (skip-chars-backward " \t")
  16729. (and (bolp) (not (org-at-heading-p))))
  16730. (cdlatex-tab) t)
  16731. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16732. (defun org-cdlatex-underscore-caret (&optional arg)
  16733. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16734. Revert to the normal definition outside of these fragments."
  16735. (interactive "P")
  16736. (if (org-inside-LaTeX-fragment-p)
  16737. (call-interactively 'cdlatex-sub-superscript)
  16738. (let (org-cdlatex-mode)
  16739. (call-interactively (key-binding (vector last-input-event))))))
  16740. (defun org-cdlatex-math-modify (&optional arg)
  16741. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16742. Revert to the normal definition outside of these fragments."
  16743. (interactive "P")
  16744. (if (org-inside-LaTeX-fragment-p)
  16745. (call-interactively 'cdlatex-math-modify)
  16746. (let (org-cdlatex-mode)
  16747. (call-interactively (key-binding (vector last-input-event))))))
  16748. ;;;; LaTeX fragments
  16749. (defvar org-latex-regexps
  16750. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16751. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16752. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16753. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16754. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16755. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16756. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16757. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16758. "Regular expressions for matching embedded LaTeX.")
  16759. (defun org-inside-LaTeX-fragment-p ()
  16760. "Test if point is inside a LaTeX fragment.
  16761. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16762. sequence appearing also before point.
  16763. Even though the matchers for math are configurable, this function assumes
  16764. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16765. delimiters are skipped when they have been removed by customization.
  16766. The return value is nil, or a cons cell with the delimiter and the
  16767. position of this delimiter.
  16768. This function does a reasonably good job, but can locally be fooled by
  16769. for example currency specifications. For example it will assume being in
  16770. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16771. fragments that are properly closed, but during editing, we have to live
  16772. with the uncertainty caused by missing closing delimiters. This function
  16773. looks only before point, not after."
  16774. (catch 'exit
  16775. (let ((pos (point))
  16776. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16777. (lim (progn
  16778. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16779. (point)))
  16780. dd-on str (start 0) m re)
  16781. (goto-char pos)
  16782. (when dodollar
  16783. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16784. re (nth 1 (assoc "$" org-latex-regexps)))
  16785. (while (string-match re str start)
  16786. (cond
  16787. ((= (match-end 0) (length str))
  16788. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16789. ((= (match-end 0) (- (length str) 5))
  16790. (throw 'exit nil))
  16791. (t (setq start (match-end 0))))))
  16792. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16793. (goto-char pos)
  16794. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16795. (and (match-beginning 2) (throw 'exit nil))
  16796. ;; count $$
  16797. (while (re-search-backward "\\$\\$" lim t)
  16798. (setq dd-on (not dd-on)))
  16799. (goto-char pos)
  16800. (if dd-on (cons "$$" m))))))
  16801. (defun org-inside-latex-macro-p ()
  16802. "Is point inside a LaTeX macro or its arguments?"
  16803. (save-match-data
  16804. (org-in-regexp
  16805. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16806. (defvar org-latex-fragment-image-overlays nil
  16807. "List of overlays carrying the images of latex fragments.")
  16808. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16809. (defun org-remove-latex-fragment-image-overlays ()
  16810. "Remove all overlays with LaTeX fragment images in current buffer."
  16811. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16812. (setq org-latex-fragment-image-overlays nil))
  16813. (defun org-preview-latex-fragment (&optional subtree)
  16814. "Preview the LaTeX fragment at point, or all locally or globally.
  16815. If the cursor is in a LaTeX fragment, create the image and overlay
  16816. it over the source code. If there is no fragment at point, display
  16817. all fragments in the current text, from one headline to the next. With
  16818. prefix SUBTREE, display all fragments in the current subtree. With a
  16819. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16820. the cursor is before the first headline,
  16821. display all fragments in the buffer.
  16822. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16823. (interactive "P")
  16824. (unless buffer-file-name
  16825. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16826. (when (display-graphic-p)
  16827. (org-remove-latex-fragment-image-overlays)
  16828. (save-excursion
  16829. (save-restriction
  16830. (let (beg end at msg)
  16831. (cond
  16832. ((or (equal subtree '(16))
  16833. (not (save-excursion
  16834. (re-search-backward org-outline-regexp-bol nil t))))
  16835. (setq beg (point-min) end (point-max)
  16836. msg "Creating images for buffer...%s"))
  16837. ((equal subtree '(4))
  16838. (org-back-to-heading)
  16839. (setq beg (point) end (org-end-of-subtree t)
  16840. msg "Creating images for subtree...%s"))
  16841. (t
  16842. (if (setq at (org-inside-LaTeX-fragment-p))
  16843. (goto-char (max (point-min) (- (cdr at) 2)))
  16844. (org-back-to-heading))
  16845. (setq beg (point) end (progn (outline-next-heading) (point))
  16846. msg (if at "Creating image...%s"
  16847. "Creating images for entry...%s"))))
  16848. (message msg "")
  16849. (narrow-to-region beg end)
  16850. (goto-char beg)
  16851. (org-format-latex
  16852. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16853. (file-name-nondirectory
  16854. buffer-file-name)))
  16855. default-directory 'overlays msg at 'forbuffer
  16856. org-latex-create-formula-image-program)
  16857. (message msg "done. Use `C-c C-c' to remove images."))))))
  16858. (defun org-format-latex (prefix &optional dir overlays msg at
  16859. forbuffer processing-type)
  16860. "Replace LaTeX fragments with links to an image, and produce images.
  16861. Some of the options can be changed using the variable
  16862. `org-format-latex-options'."
  16863. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16864. (let* ((prefixnodir (file-name-nondirectory prefix))
  16865. (absprefix (expand-file-name prefix dir))
  16866. (todir (file-name-directory absprefix))
  16867. (opt org-format-latex-options)
  16868. (optnew org-format-latex-options)
  16869. (matchers (plist-get opt :matchers))
  16870. (re-list org-latex-regexps)
  16871. (cnt 0) txt hash link beg end re e checkdir
  16872. string
  16873. m n block-type block linkfile movefile ov)
  16874. ;; Check the different regular expressions
  16875. (while (setq e (pop re-list))
  16876. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16877. block (if block-type "\n\n" ""))
  16878. (when (member m matchers)
  16879. (goto-char (point-min))
  16880. (while (re-search-forward re nil t)
  16881. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16882. (or (not overlays)
  16883. (not (eq (get-char-property (match-beginning n)
  16884. 'org-overlay-type)
  16885. 'org-latex-overlay))))
  16886. (cond
  16887. ((eq processing-type 'verbatim))
  16888. ((eq processing-type 'mathjax)
  16889. ;; Prepare for MathJax processing.
  16890. (setq string (match-string n))
  16891. (when (member m '("$" "$1"))
  16892. (save-excursion
  16893. (delete-region (match-beginning n) (match-end n))
  16894. (goto-char (match-beginning n))
  16895. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16896. ((or (eq processing-type 'dvipng)
  16897. (eq processing-type 'imagemagick))
  16898. ;; Process to an image.
  16899. (setq txt (match-string n)
  16900. beg (match-beginning n) end (match-end n)
  16901. cnt (1+ cnt))
  16902. (let ((face (face-at-point))
  16903. (fg (plist-get opt :foreground))
  16904. (bg (plist-get opt :background))
  16905. ;; Ensure full list is printed.
  16906. print-length print-level)
  16907. (when forbuffer
  16908. ;; Get the colors from the face at point.
  16909. (goto-char beg)
  16910. (when (eq fg 'auto)
  16911. (setq fg (face-attribute face :foreground nil 'default)))
  16912. (when (eq bg 'auto)
  16913. (setq bg (face-attribute face :background nil 'default)))
  16914. (setq optnew (copy-sequence opt))
  16915. (plist-put optnew :foreground fg)
  16916. (plist-put optnew :background bg))
  16917. (setq hash (sha1 (prin1-to-string
  16918. (list org-format-latex-header
  16919. org-latex-default-packages-alist
  16920. org-latex-packages-alist
  16921. org-format-latex-options
  16922. forbuffer txt fg bg)))
  16923. linkfile (format "%s_%s.png" prefix hash)
  16924. movefile (format "%s_%s.png" absprefix hash)))
  16925. (setq link (concat block "[[file:" linkfile "]]" block))
  16926. (if msg (message msg cnt))
  16927. (goto-char beg)
  16928. (unless checkdir ; Ensure the directory exists.
  16929. (setq checkdir t)
  16930. (or (file-directory-p todir) (make-directory todir t)))
  16931. (unless (file-exists-p movefile)
  16932. (org-create-formula-image
  16933. txt movefile optnew forbuffer processing-type))
  16934. (if overlays
  16935. (progn
  16936. (mapc (lambda (o)
  16937. (if (eq (overlay-get o 'org-overlay-type)
  16938. 'org-latex-overlay)
  16939. (delete-overlay o)))
  16940. (overlays-in beg end))
  16941. (setq ov (make-overlay beg end))
  16942. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16943. (if (featurep 'xemacs)
  16944. (progn
  16945. (overlay-put ov 'invisible t)
  16946. (overlay-put
  16947. ov 'end-glyph
  16948. (make-glyph (vector 'png :file movefile))))
  16949. (overlay-put
  16950. ov 'display
  16951. (list 'image :type 'png :file movefile :ascent 'center)))
  16952. (push ov org-latex-fragment-image-overlays)
  16953. (goto-char end))
  16954. (delete-region beg end)
  16955. (insert (org-add-props link
  16956. (list 'org-latex-src
  16957. (replace-regexp-in-string
  16958. "\"" "" txt)
  16959. 'org-latex-src-embed-type
  16960. (if block-type 'paragraph 'character))))))
  16961. ((eq processing-type 'mathml)
  16962. ;; Process to MathML
  16963. (unless (save-match-data (org-format-latex-mathml-available-p))
  16964. (user-error "LaTeX to MathML converter not configured"))
  16965. (setq txt (match-string n)
  16966. beg (match-beginning n) end (match-end n)
  16967. cnt (1+ cnt))
  16968. (if msg (message msg cnt))
  16969. (goto-char beg)
  16970. (delete-region beg end)
  16971. (insert (org-format-latex-as-mathml
  16972. txt block-type prefix dir)))
  16973. (t
  16974. (error "Unknown conversion type %s for LaTeX fragments"
  16975. processing-type)))))))))
  16976. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16977. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16978. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16979. sucessful, return the portion between \"<math...> </math>\"
  16980. elements otherwise return nil. When MATHML-FILE is specified,
  16981. write the results in to that file. When invoked as an
  16982. interactive command, prompt for LATEX-FRAG, with initial value
  16983. set to the current active region and echo the results for user
  16984. inspection."
  16985. (interactive (list (let ((frag (when (org-region-active-p)
  16986. (buffer-substring-no-properties
  16987. (region-beginning) (region-end)))))
  16988. (read-string "LaTeX Fragment: " frag nil frag))))
  16989. (unless latex-frag (error "Invalid LaTeX fragment"))
  16990. (let* ((tmp-in-file (file-relative-name
  16991. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16992. (ignore (write-region latex-frag nil tmp-in-file))
  16993. (tmp-out-file (file-relative-name
  16994. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16995. (cmd (format-spec
  16996. org-latex-to-mathml-convert-command
  16997. `((?j . ,(shell-quote-argument
  16998. (expand-file-name org-latex-to-mathml-jar-file)))
  16999. (?I . ,(shell-quote-argument tmp-in-file))
  17000. (?o . ,(shell-quote-argument tmp-out-file)))))
  17001. mathml shell-command-output)
  17002. (when (org-called-interactively-p 'any)
  17003. (unless (org-format-latex-mathml-available-p)
  17004. (user-error "LaTeX to MathML converter not configured")))
  17005. (message "Running %s" cmd)
  17006. (setq shell-command-output (shell-command-to-string cmd))
  17007. (setq mathml
  17008. (when (file-readable-p tmp-out-file)
  17009. (with-current-buffer (find-file-noselect tmp-out-file t)
  17010. (goto-char (point-min))
  17011. (when (re-search-forward
  17012. (concat
  17013. (regexp-quote
  17014. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17015. "\\(.\\|\n\\)*"
  17016. (regexp-quote "</math>")) nil t)
  17017. (prog1 (match-string 0) (kill-buffer))))))
  17018. (cond
  17019. (mathml
  17020. (setq mathml
  17021. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17022. (when mathml-file
  17023. (write-region mathml nil mathml-file))
  17024. (when (org-called-interactively-p 'any)
  17025. (message mathml)))
  17026. ((message "LaTeX to MathML conversion failed")
  17027. (message shell-command-output)))
  17028. (delete-file tmp-in-file)
  17029. (when (file-exists-p tmp-out-file)
  17030. (delete-file tmp-out-file))
  17031. mathml))
  17032. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17033. prefix &optional dir)
  17034. "Use `org-create-math-formula' but check local cache first."
  17035. (let* ((absprefix (expand-file-name prefix dir))
  17036. (print-length nil) (print-level nil)
  17037. (formula-id (concat
  17038. "formula-"
  17039. (sha1
  17040. (prin1-to-string
  17041. (list latex-frag
  17042. org-latex-to-mathml-convert-command)))))
  17043. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17044. (formula-cache-dir (file-name-directory formula-cache)))
  17045. (unless (file-directory-p formula-cache-dir)
  17046. (make-directory formula-cache-dir t))
  17047. (unless (file-exists-p formula-cache)
  17048. (org-create-math-formula latex-frag formula-cache))
  17049. (if (file-exists-p formula-cache)
  17050. ;; Successful conversion. Return the link to MathML file.
  17051. (org-add-props
  17052. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17053. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17054. 'org-latex-src-embed-type (if latex-frag-type
  17055. 'paragraph 'character)))
  17056. ;; Failed conversion. Return the LaTeX fragment verbatim
  17057. latex-frag)))
  17058. (defun org-create-formula-image (string tofile options buffer &optional type)
  17059. "Create an image from LaTeX source using dvipng or convert.
  17060. This function calls either `org-create-formula-image-with-dvipng'
  17061. or `org-create-formula-image-with-imagemagick' depending on the
  17062. value of `org-latex-create-formula-image-program' or on the value
  17063. of the optional TYPE variable.
  17064. Note: ultimately these two function should be combined as they
  17065. share a good deal of logic."
  17066. (org-check-external-command
  17067. "latex" "needed to convert LaTeX fragments to images")
  17068. (funcall
  17069. (case (or type org-latex-create-formula-image-program)
  17070. ('dvipng
  17071. (org-check-external-command
  17072. "dvipng" "needed to convert LaTeX fragments to images")
  17073. #'org-create-formula-image-with-dvipng)
  17074. ('imagemagick
  17075. (org-check-external-command
  17076. "convert" "you need to install imagemagick")
  17077. #'org-create-formula-image-with-imagemagick)
  17078. (t (error
  17079. "Invalid value of `org-latex-create-formula-image-program'")))
  17080. string tofile options buffer))
  17081. (declare-function org-export-get-backend "ox" (name))
  17082. (declare-function org-export--get-global-options "ox" (&optional backend))
  17083. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17084. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17085. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17086. (defun org-create-formula--latex-header ()
  17087. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17088. (let ((info (org-combine-plists (org-export--get-global-options
  17089. (org-export-get-backend 'latex))
  17090. (org-export--get-inbuffer-options
  17091. (org-export-get-backend 'latex)))))
  17092. (org-latex-guess-babel-language
  17093. (org-latex-guess-inputenc
  17094. (org-splice-latex-header
  17095. org-format-latex-header
  17096. org-latex-default-packages-alist
  17097. org-latex-packages-alist t
  17098. (plist-get info :latex-header)))
  17099. info)))
  17100. ;; This function borrows from Ganesh Swami's latex2png.el
  17101. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17102. "This calls dvipng."
  17103. (require 'ox-latex)
  17104. (let* ((tmpdir (if (featurep 'xemacs)
  17105. (temp-directory)
  17106. temporary-file-directory))
  17107. (texfilebase (make-temp-name
  17108. (expand-file-name "orgtex" tmpdir)))
  17109. (texfile (concat texfilebase ".tex"))
  17110. (dvifile (concat texfilebase ".dvi"))
  17111. (pngfile (concat texfilebase ".png"))
  17112. (fnh (if (featurep 'xemacs)
  17113. (font-height (face-font 'default))
  17114. (face-attribute 'default :height nil)))
  17115. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17116. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17117. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17118. "Black"))
  17119. (bg (or (plist-get options (if buffer :background :html-background))
  17120. "Transparent")))
  17121. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17122. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17123. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17124. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17125. (let ((latex-header (org-create-formula--latex-header)))
  17126. (with-temp-file texfile
  17127. (insert latex-header)
  17128. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17129. (let ((dir default-directory))
  17130. (condition-case nil
  17131. (progn
  17132. (cd tmpdir)
  17133. (call-process "latex" nil nil nil texfile))
  17134. (error nil))
  17135. (cd dir))
  17136. (if (not (file-exists-p dvifile))
  17137. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17138. (condition-case nil
  17139. (if (featurep 'xemacs)
  17140. (call-process "dvipng" nil nil nil
  17141. "-fg" fg "-bg" bg
  17142. "-T" "tight"
  17143. "-o" pngfile
  17144. dvifile)
  17145. (call-process "dvipng" nil nil nil
  17146. "-fg" fg "-bg" bg
  17147. "-D" dpi
  17148. ;;"-x" scale "-y" scale
  17149. "-T" "tight"
  17150. "-o" pngfile
  17151. dvifile))
  17152. (error nil))
  17153. (if (not (file-exists-p pngfile))
  17154. (if org-format-latex-signal-error
  17155. (error "Failed to create png file from %s" texfile)
  17156. (message "Failed to create png file from %s" texfile)
  17157. nil)
  17158. ;; Use the requested file name and clean up
  17159. (copy-file pngfile tofile 'replace)
  17160. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17161. (if (file-exists-p (concat texfilebase e))
  17162. (delete-file (concat texfilebase e))))
  17163. pngfile))))
  17164. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17165. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17166. "This calls convert, which is included into imagemagick."
  17167. (require 'ox-latex)
  17168. (let* ((tmpdir (if (featurep 'xemacs)
  17169. (temp-directory)
  17170. temporary-file-directory))
  17171. (texfilebase (make-temp-name
  17172. (expand-file-name "orgtex" tmpdir)))
  17173. (texfile (concat texfilebase ".tex"))
  17174. (pdffile (concat texfilebase ".pdf"))
  17175. (pngfile (concat texfilebase ".png"))
  17176. (fnh (if (featurep 'xemacs)
  17177. (font-height (face-font 'default))
  17178. (face-attribute 'default :height nil)))
  17179. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17180. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17181. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17182. "black"))
  17183. (bg (or (plist-get options (if buffer :background :html-background))
  17184. "white")))
  17185. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17186. (setq fg (org-latex-color-format fg)))
  17187. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17188. (setq bg (org-latex-color-format
  17189. (if (string= bg "Transparent") "white" bg))))
  17190. (let ((latex-header (org-create-formula--latex-header)))
  17191. (with-temp-file texfile
  17192. (insert latex-header)
  17193. (insert "\n\\begin{document}\n"
  17194. "\\definecolor{fg}{rgb}{" fg "}\n"
  17195. "\\definecolor{bg}{rgb}{" bg "}\n"
  17196. "\n\\pagecolor{bg}\n"
  17197. "\n{\\color{fg}\n"
  17198. string
  17199. "\n}\n"
  17200. "\n\\end{document}\n")))
  17201. (org-latex-compile texfile t)
  17202. (if (not (file-exists-p pdffile))
  17203. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17204. (condition-case nil
  17205. (if (featurep 'xemacs)
  17206. (call-process "convert" nil nil nil
  17207. "-density" "96"
  17208. "-trim"
  17209. "-antialias"
  17210. pdffile
  17211. "-quality" "100"
  17212. ;; "-sharpen" "0x1.0"
  17213. pngfile)
  17214. (call-process "convert" nil nil nil
  17215. "-density" dpi
  17216. "-trim"
  17217. "-antialias"
  17218. pdffile
  17219. "-quality" "100"
  17220. ;; "-sharpen" "0x1.0"
  17221. pngfile))
  17222. (error nil))
  17223. (if (not (file-exists-p pngfile))
  17224. (if org-format-latex-signal-error
  17225. (error "Failed to create png file from %s" texfile)
  17226. (message "Failed to create png file from %s" texfile)
  17227. nil)
  17228. ;; Use the requested file name and clean up
  17229. (copy-file pngfile tofile 'replace)
  17230. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17231. (if (file-exists-p (concat texfilebase e))
  17232. (delete-file (concat texfilebase e))))
  17233. pngfile))))
  17234. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17235. "Fill a LaTeX header template TPL.
  17236. In the template, the following place holders will be recognized:
  17237. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17238. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17239. [PACKAGES] \\usepackage statements for PKG
  17240. [NO-PACKAGES] do not include PKG
  17241. [EXTRA] the string EXTRA
  17242. [NO-EXTRA] do not include EXTRA
  17243. For backward compatibility, if both the positive and the negative place
  17244. holder is missing, the positive one (without the \"NO-\") will be
  17245. assumed to be present at the end of the template.
  17246. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17247. EXTRA is a string.
  17248. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17249. (let (rpl (end ""))
  17250. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17251. (setq rpl (if (or (match-end 1) (not def-pkg))
  17252. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17253. tpl (replace-match rpl t t tpl))
  17254. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17255. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17256. (setq rpl (if (or (match-end 1) (not pkg))
  17257. "" (org-latex-packages-to-string pkg snippets-p t))
  17258. tpl (replace-match rpl t t tpl))
  17259. (if pkg (setq end
  17260. (concat end "\n"
  17261. (org-latex-packages-to-string pkg snippets-p)))))
  17262. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17263. (setq rpl (if (or (match-end 1) (not extra))
  17264. "" (concat extra "\n"))
  17265. tpl (replace-match rpl t t tpl))
  17266. (if (and extra (string-match "\\S-" extra))
  17267. (setq end (concat end "\n" extra))))
  17268. (if (string-match "\\S-" end)
  17269. (concat tpl "\n" end)
  17270. tpl)))
  17271. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17272. "Turn an alist of packages into a string with the \\usepackage macros."
  17273. (setq pkg (mapconcat (lambda(p)
  17274. (cond
  17275. ((stringp p) p)
  17276. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17277. (format "%% Package %s omitted" (cadr p)))
  17278. ((equal "" (car p))
  17279. (format "\\usepackage{%s}" (cadr p)))
  17280. (t
  17281. (format "\\usepackage[%s]{%s}"
  17282. (car p) (cadr p)))))
  17283. pkg
  17284. "\n"))
  17285. (if newline (concat pkg "\n") pkg))
  17286. (defun org-dvipng-color (attr)
  17287. "Return a RGB color specification for dvipng."
  17288. (apply 'format "rgb %s %s %s"
  17289. (mapcar 'org-normalize-color
  17290. (if (featurep 'xemacs)
  17291. (color-rgb-components
  17292. (face-property 'default
  17293. (cond ((eq attr :foreground) 'foreground)
  17294. ((eq attr :background) 'background))))
  17295. (color-values (face-attribute 'default attr nil))))))
  17296. (defun org-dvipng-color-format (color-name)
  17297. "Convert COLOR-NAME to a RGB color value for dvipng."
  17298. (apply 'format "rgb %s %s %s"
  17299. (mapcar 'org-normalize-color
  17300. (color-values color-name))))
  17301. (defun org-latex-color (attr)
  17302. "Return a RGB color for the LaTeX color package."
  17303. (apply 'format "%s,%s,%s"
  17304. (mapcar 'org-normalize-color
  17305. (if (featurep 'xemacs)
  17306. (color-rgb-components
  17307. (face-property 'default
  17308. (cond ((eq attr :foreground) 'foreground)
  17309. ((eq attr :background) 'background))))
  17310. (color-values (face-attribute 'default attr nil))))))
  17311. (defun org-latex-color-format (color-name)
  17312. "Convert COLOR-NAME to a RGB color value."
  17313. (apply 'format "%s,%s,%s"
  17314. (mapcar 'org-normalize-color
  17315. (color-values color-name))))
  17316. (defun org-normalize-color (value)
  17317. "Return string to be used as color value for an RGB component."
  17318. (format "%g" (/ value 65535.0)))
  17319. ;; Image display
  17320. (defvar org-inline-image-overlays nil)
  17321. (make-variable-buffer-local 'org-inline-image-overlays)
  17322. (defun org-toggle-inline-images (&optional include-linked)
  17323. "Toggle the display of inline images.
  17324. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17325. (interactive "P")
  17326. (if org-inline-image-overlays
  17327. (progn
  17328. (org-remove-inline-images)
  17329. (message "Inline image display turned off"))
  17330. (org-display-inline-images include-linked)
  17331. (if (and (org-called-interactively-p)
  17332. org-inline-image-overlays)
  17333. (message "%d images displayed inline"
  17334. (length org-inline-image-overlays))
  17335. (message "No images to display inline"))))
  17336. (defun org-redisplay-inline-images ()
  17337. "Refresh the display of inline images."
  17338. (interactive)
  17339. (if (not org-inline-image-overlays)
  17340. (org-toggle-inline-images)
  17341. (org-toggle-inline-images)
  17342. (org-toggle-inline-images)))
  17343. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17344. "Display inline images.
  17345. Normally only links without a description part are inlined, because this
  17346. is how it will work for export. When INCLUDE-LINKED is set, also links
  17347. with a description part will be inlined. This can be nice for a quick
  17348. look at those images, but it does not reflect what exported files will look
  17349. like.
  17350. When REFRESH is set, refresh existing images between BEG and END.
  17351. This will create new image displays only if necessary.
  17352. BEG and END default to the buffer boundaries."
  17353. (interactive "P")
  17354. (when (display-graphic-p)
  17355. (unless refresh
  17356. (org-remove-inline-images)
  17357. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17358. (save-excursion
  17359. (save-restriction
  17360. (widen)
  17361. (setq beg (or beg (point-min)) end (or end (point-max)))
  17362. (goto-char beg)
  17363. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17364. (substring (org-image-file-name-regexp) 0 -2)
  17365. "\\)\\]" (if include-linked "" "\\]")))
  17366. (case-fold-search t)
  17367. old file ov img type attrwidth width)
  17368. (while (re-search-forward re end t)
  17369. (setq old (get-char-property-and-overlay (match-beginning 1)
  17370. 'org-image-overlay)
  17371. file (expand-file-name
  17372. (concat (or (match-string 3) "") (match-string 4))))
  17373. (when (image-type-available-p 'imagemagick)
  17374. (setq attrwidth (if (or (listp org-image-actual-width)
  17375. (null org-image-actual-width))
  17376. (save-excursion
  17377. (save-match-data
  17378. (when (re-search-backward
  17379. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17380. (save-excursion
  17381. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17382. (string-to-number (match-string 1))))))
  17383. width (cond ((eq org-image-actual-width t) nil)
  17384. ((null org-image-actual-width) attrwidth)
  17385. ((numberp org-image-actual-width)
  17386. org-image-actual-width)
  17387. ((listp org-image-actual-width)
  17388. (or attrwidth (car org-image-actual-width))))
  17389. type (if width 'imagemagick)))
  17390. (when (file-exists-p file)
  17391. (if (and (car-safe old) refresh)
  17392. (image-refresh (overlay-get (cdr old) 'display))
  17393. (setq img (save-match-data (create-image file type nil :width width)))
  17394. (when img
  17395. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17396. (overlay-put ov 'display img)
  17397. (overlay-put ov 'face 'default)
  17398. (overlay-put ov 'org-image-overlay t)
  17399. (overlay-put ov 'modification-hooks
  17400. (list 'org-display-inline-remove-overlay))
  17401. (push ov org-inline-image-overlays))))))))))
  17402. (define-obsolete-function-alias
  17403. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17404. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17405. "Remove inline-display overlay if a corresponding region is modified."
  17406. (let ((inhibit-modification-hooks t))
  17407. (when (and ov after)
  17408. (delete ov org-inline-image-overlays)
  17409. (delete-overlay ov))))
  17410. (defun org-remove-inline-images ()
  17411. "Remove inline display of images."
  17412. (interactive)
  17413. (mapc 'delete-overlay org-inline-image-overlays)
  17414. (setq org-inline-image-overlays nil))
  17415. ;;;; Key bindings
  17416. ;; Outline functions from `outline-mode-prefix-map'
  17417. ;; that can be remapped in Org:
  17418. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17419. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17420. (define-key org-mode-map [remap outline-forward-same-level]
  17421. 'org-forward-heading-same-level)
  17422. (define-key org-mode-map [remap outline-backward-same-level]
  17423. 'org-backward-heading-same-level)
  17424. (define-key org-mode-map [remap show-branches]
  17425. 'org-kill-note-or-show-branches)
  17426. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17427. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17428. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17429. ;; Outline functions from `outline-mode-prefix-map' that can not
  17430. ;; be remapped in Org:
  17431. ;;
  17432. ;; - the column "key binding" shows whether the Outline function is still
  17433. ;; available in Org mode on the same key that it has been bound to in
  17434. ;; Outline mode:
  17435. ;; - "overridden": key used for a different functionality in Org mode
  17436. ;; - else: key still bound to the same Outline function in Org mode
  17437. ;;
  17438. ;; | Outline function | key binding | Org replacement |
  17439. ;; |------------------------------------+-------------+-----------------------|
  17440. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17441. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17442. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17443. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17444. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17445. ;; | `show-entry' | overridden | no replacement |
  17446. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17447. ;; | `show-branches' | `C-c C-k' | still same function |
  17448. ;; | `show-subtree' | overridden | visibility cycling |
  17449. ;; | `show-all' | overridden | no replacement |
  17450. ;; | `hide-subtree' | overridden | visibility cycling |
  17451. ;; | `hide-body' | overridden | no replacement |
  17452. ;; | `hide-entry' | overridden | visibility cycling |
  17453. ;; | `hide-leaves' | overridden | no replacement |
  17454. ;; | `hide-sublevels' | overridden | no replacement |
  17455. ;; | `hide-other' | overridden | no replacement |
  17456. ;; Make `C-c C-x' a prefix key
  17457. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17458. ;; TAB key with modifiers
  17459. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17460. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17461. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17462. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17463. ;; The following line is necessary under Suse GNU/Linux
  17464. (unless (featurep 'xemacs)
  17465. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17466. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17467. (define-key org-mode-map [backtab] 'org-shifttab)
  17468. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17469. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17470. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17471. ;; Cursor keys with modifiers
  17472. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17473. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17474. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17475. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17476. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17477. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17478. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17479. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17480. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17481. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17482. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17483. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17484. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17485. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17486. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17487. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17488. ;; Babel keys
  17489. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17490. (mapc (lambda (pair)
  17491. (define-key org-babel-map (car pair) (cdr pair)))
  17492. org-babel-key-bindings)
  17493. ;;; Extra keys for tty access.
  17494. ;; We only set them when really needed because otherwise the
  17495. ;; menus don't show the simple keys
  17496. (when (or org-use-extra-keys
  17497. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17498. (not window-system))
  17499. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17500. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17501. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17502. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17503. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17504. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17505. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17506. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17507. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17508. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17509. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17510. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17511. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17512. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17513. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17514. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17515. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17516. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17517. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17518. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17519. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17520. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17521. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17522. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17523. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17524. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17525. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17526. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17527. ;; All the other keys
  17528. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17529. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17530. (if (boundp 'narrow-map)
  17531. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17532. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17533. (if (boundp 'narrow-map)
  17534. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17535. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17536. (if (boundp 'narrow-map)
  17537. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17538. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17539. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17540. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17541. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17542. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17543. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17544. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17545. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17546. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17547. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17548. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17549. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17550. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17551. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17552. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17553. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17554. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17555. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17556. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17557. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17558. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17559. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17560. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17561. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17562. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17563. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17564. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17565. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17566. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17567. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17568. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17569. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17570. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17571. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17572. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17573. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17574. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17575. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17576. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17577. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17578. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17579. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17580. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17581. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17582. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17583. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17584. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17585. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17586. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17587. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17588. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17589. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17590. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17591. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17592. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17593. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17594. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17595. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17596. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17597. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17598. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17599. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17600. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17601. (org-defkey org-mode-map "\C-m" 'org-return)
  17602. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17603. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17604. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17605. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17606. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17607. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17608. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17609. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17610. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17611. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17612. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17613. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17614. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17615. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17616. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17617. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17618. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17619. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17620. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17621. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17622. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17623. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17624. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17625. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17626. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17627. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17628. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17629. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17630. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17631. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17632. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17633. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17634. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17635. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17636. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17637. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17638. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17639. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17640. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17641. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17642. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17643. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17644. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17645. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17646. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17647. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17648. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17649. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17650. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17651. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17652. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17653. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17654. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17655. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17656. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17657. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17658. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17659. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17660. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17661. (when (featurep 'xemacs)
  17662. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17663. (defconst org-speed-commands-default
  17664. '(
  17665. ("Outline Navigation")
  17666. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17667. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17668. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17669. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17670. ("F" . org-next-block)
  17671. ("B" . org-previous-block)
  17672. ("u" . (org-speed-move-safe 'outline-up-heading))
  17673. ("j" . org-goto)
  17674. ("g" . (org-refile t))
  17675. ("Outline Visibility")
  17676. ("c" . org-cycle)
  17677. ("C" . org-shifttab)
  17678. (" " . org-display-outline-path)
  17679. ("s" . org-narrow-to-subtree)
  17680. ("=" . org-columns)
  17681. ("Outline Structure Editing")
  17682. ("U" . org-shiftmetaup)
  17683. ("D" . org-shiftmetadown)
  17684. ("r" . org-metaright)
  17685. ("l" . org-metaleft)
  17686. ("R" . org-shiftmetaright)
  17687. ("L" . org-shiftmetaleft)
  17688. ("i" . (progn (forward-char 1) (call-interactively
  17689. 'org-insert-heading-respect-content)))
  17690. ("^" . org-sort)
  17691. ("w" . org-refile)
  17692. ("a" . org-archive-subtree-default-with-confirmation)
  17693. ("@" . org-mark-subtree)
  17694. ("#" . org-toggle-comment)
  17695. ("Clock Commands")
  17696. ("I" . org-clock-in)
  17697. ("O" . org-clock-out)
  17698. ("Meta Data Editing")
  17699. ("t" . org-todo)
  17700. ("," . (org-priority))
  17701. ("0" . (org-priority ?\ ))
  17702. ("1" . (org-priority ?A))
  17703. ("2" . (org-priority ?B))
  17704. ("3" . (org-priority ?C))
  17705. (":" . org-set-tags-command)
  17706. ("e" . org-set-effort)
  17707. ("E" . org-inc-effort)
  17708. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17709. (org-entry-put (point) "APPT_WARNTIME" m)))
  17710. ("Agenda Views etc")
  17711. ("v" . org-agenda)
  17712. ("/" . org-sparse-tree)
  17713. ("Misc")
  17714. ("o" . org-open-at-point)
  17715. ("?" . org-speed-command-help)
  17716. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17717. (">" . (org-agenda-remove-restriction-lock))
  17718. )
  17719. "The default speed commands.")
  17720. (defun org-print-speed-command (e)
  17721. (if (> (length (car e)) 1)
  17722. (progn
  17723. (princ "\n")
  17724. (princ (car e))
  17725. (princ "\n")
  17726. (princ (make-string (length (car e)) ?-))
  17727. (princ "\n"))
  17728. (princ (car e))
  17729. (princ " ")
  17730. (if (symbolp (cdr e))
  17731. (princ (symbol-name (cdr e)))
  17732. (prin1 (cdr e)))
  17733. (princ "\n")))
  17734. (defun org-speed-command-help ()
  17735. "Show the available speed commands."
  17736. (interactive)
  17737. (if (not org-use-speed-commands)
  17738. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17739. (with-output-to-temp-buffer "*Help*"
  17740. (princ "User-defined Speed commands\n===========================\n")
  17741. (mapc 'org-print-speed-command org-speed-commands-user)
  17742. (princ "\n")
  17743. (princ "Built-in Speed commands\n=======================\n")
  17744. (mapc 'org-print-speed-command org-speed-commands-default))
  17745. (with-current-buffer "*Help*"
  17746. (setq truncate-lines t))))
  17747. (defun org-speed-move-safe (cmd)
  17748. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17749. If not, return to the original position and throw an error."
  17750. (interactive)
  17751. (let ((pos (point)))
  17752. (call-interactively cmd)
  17753. (unless (and (bolp) (org-at-heading-p))
  17754. (goto-char pos)
  17755. (error "Boundary reached while executing %s" cmd))))
  17756. (defvar org-self-insert-command-undo-counter 0)
  17757. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17758. (defvar org-speed-command nil)
  17759. (define-obsolete-function-alias
  17760. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17761. (defun org-speed-command-activate (keys)
  17762. "Hook for activating single-letter speed commands.
  17763. `org-speed-commands-default' specifies a minimal command set.
  17764. Use `org-speed-commands-user' for further customization."
  17765. (when (or (and (bolp) (looking-at org-outline-regexp))
  17766. (and (functionp org-use-speed-commands)
  17767. (funcall org-use-speed-commands)))
  17768. (cdr (assoc keys (append org-speed-commands-user
  17769. org-speed-commands-default)))))
  17770. (define-obsolete-function-alias
  17771. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17772. (defun org-babel-speed-command-activate (keys)
  17773. "Hook for activating single-letter code block commands."
  17774. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17775. (cdr (assoc keys org-babel-key-bindings))))
  17776. (defcustom org-speed-command-hook
  17777. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17778. "Hook for activating speed commands at strategic locations.
  17779. Hook functions are called in sequence until a valid handler is
  17780. found.
  17781. Each hook takes a single argument, a user-pressed command key
  17782. which is also a `self-insert-command' from the global map.
  17783. Within the hook, examine the cursor position and the command key
  17784. and return nil or a valid handler as appropriate. Handler could
  17785. be one of an interactive command, a function, or a form.
  17786. Set `org-use-speed-commands' to non-nil value to enable this
  17787. hook. The default setting is `org-speed-command-activate'."
  17788. :group 'org-structure
  17789. :version "24.1"
  17790. :type 'hook)
  17791. (defun org-self-insert-command (N)
  17792. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17793. If the cursor is in a table looking at whitespace, the whitespace is
  17794. overwritten, and the table is not marked as requiring realignment."
  17795. (interactive "p")
  17796. (org-check-before-invisible-edit 'insert)
  17797. (cond
  17798. ((and org-use-speed-commands
  17799. (setq org-speed-command
  17800. (run-hook-with-args-until-success
  17801. 'org-speed-command-hook (this-command-keys))))
  17802. (cond
  17803. ((commandp org-speed-command)
  17804. (setq this-command org-speed-command)
  17805. (call-interactively org-speed-command))
  17806. ((functionp org-speed-command)
  17807. (funcall org-speed-command))
  17808. ((and org-speed-command (listp org-speed-command))
  17809. (eval org-speed-command))
  17810. (t (let (org-use-speed-commands)
  17811. (call-interactively 'org-self-insert-command)))))
  17812. ((and
  17813. (org-table-p)
  17814. (progn
  17815. ;; check if we blank the field, and if that triggers align
  17816. (and (featurep 'org-table) org-table-auto-blank-field
  17817. (member last-command
  17818. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17819. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17820. ;; got extra space, this field does not determine column width
  17821. (let (org-table-may-need-update) (org-table-blank-field))
  17822. ;; no extra space, this field may determine column width
  17823. (org-table-blank-field)))
  17824. t)
  17825. (eq N 1)
  17826. (looking-at "[^|\n]* |"))
  17827. (let (org-table-may-need-update)
  17828. (goto-char (1- (match-end 0)))
  17829. (backward-delete-char 1)
  17830. (goto-char (match-beginning 0))
  17831. (self-insert-command N)))
  17832. (t
  17833. (setq org-table-may-need-update t)
  17834. (self-insert-command N)
  17835. (org-fix-tags-on-the-fly)
  17836. (if org-self-insert-cluster-for-undo
  17837. (if (not (eq last-command 'org-self-insert-command))
  17838. (setq org-self-insert-command-undo-counter 1)
  17839. (if (>= org-self-insert-command-undo-counter 20)
  17840. (setq org-self-insert-command-undo-counter 1)
  17841. (and (> org-self-insert-command-undo-counter 0)
  17842. buffer-undo-list (listp buffer-undo-list)
  17843. (not (cadr buffer-undo-list)) ; remove nil entry
  17844. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17845. (setq org-self-insert-command-undo-counter
  17846. (1+ org-self-insert-command-undo-counter))))))))
  17847. (defun org-check-before-invisible-edit (kind)
  17848. "Check is editing if kind KIND would be dangerous with invisible text around.
  17849. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17850. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17851. (if (and org-catch-invisible-edits
  17852. (or (not (boundp 'visible-mode)) (not visible-mode))
  17853. (or (get-char-property (point) 'invisible)
  17854. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17855. ;; OK, we need to take a closer look
  17856. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17857. (invisible-before-point (if (bobp) nil (get-char-property
  17858. (1- (point)) 'invisible)))
  17859. (border-and-ok-direction
  17860. (or
  17861. ;; Check if we are acting predictably before invisible text
  17862. (and invisible-at-point (not invisible-before-point)
  17863. (memq kind '(insert delete-backward)))
  17864. ;; Check if we are acting predictably after invisible text
  17865. ;; This works not well, and I have turned it off. It seems
  17866. ;; better to always show and stop after invisible text.
  17867. ;; (and (not invisible-at-point) invisible-before-point
  17868. ;; (memq kind '(insert delete)))
  17869. )))
  17870. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17871. (memq invisible-before-point '(outline org-hide-block t)))
  17872. (if (eq org-catch-invisible-edits 'error)
  17873. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17874. (if (and org-custom-properties-overlays
  17875. (y-or-n-p "Display invisible properties in this buffer? "))
  17876. (org-toggle-custom-properties-visibility)
  17877. ;; Make the area visible
  17878. (save-excursion
  17879. (if invisible-before-point
  17880. (goto-char (previous-single-char-property-change
  17881. (point) 'invisible)))
  17882. (show-subtree))
  17883. (cond
  17884. ((eq org-catch-invisible-edits 'show)
  17885. ;; That's it, we do the edit after showing
  17886. (message
  17887. "Unfolding invisible region around point before editing")
  17888. (sit-for 1))
  17889. ((and (eq org-catch-invisible-edits 'smart)
  17890. border-and-ok-direction)
  17891. (message "Unfolding invisible region around point before editing"))
  17892. (t
  17893. ;; Don't do the edit, make the user repeat it in full visibility
  17894. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17895. (defun org-fix-tags-on-the-fly ()
  17896. (when (and (equal (char-after (point-at-bol)) ?*)
  17897. (org-at-heading-p))
  17898. (org-align-tags-here org-tags-column)))
  17899. (defun org-delete-backward-char (N)
  17900. "Like `delete-backward-char', insert whitespace at field end in tables.
  17901. When deleting backwards, in tables this function will insert whitespace in
  17902. front of the next \"|\" separator, to keep the table aligned. The table will
  17903. still be marked for re-alignment if the field did fill the entire column,
  17904. because, in this case the deletion might narrow the column."
  17905. (interactive "p")
  17906. (save-match-data
  17907. (org-check-before-invisible-edit 'delete-backward)
  17908. (if (and (org-table-p)
  17909. (eq N 1)
  17910. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17911. (looking-at ".*?|"))
  17912. (let ((pos (point))
  17913. (noalign (looking-at "[^|\n\r]* |"))
  17914. (c org-table-may-need-update))
  17915. (backward-delete-char N)
  17916. (if (not overwrite-mode)
  17917. (progn
  17918. (skip-chars-forward "^|")
  17919. (insert " ")
  17920. (goto-char (1- pos))))
  17921. ;; noalign: if there were two spaces at the end, this field
  17922. ;; does not determine the width of the column.
  17923. (if noalign (setq org-table-may-need-update c)))
  17924. (backward-delete-char N)
  17925. (org-fix-tags-on-the-fly))))
  17926. (defun org-delete-char (N)
  17927. "Like `delete-char', but insert whitespace at field end in tables.
  17928. When deleting characters, in tables this function will insert whitespace in
  17929. front of the next \"|\" separator, to keep the table aligned. The table will
  17930. still be marked for re-alignment if the field did fill the entire column,
  17931. because, in this case the deletion might narrow the column."
  17932. (interactive "p")
  17933. (save-match-data
  17934. (org-check-before-invisible-edit 'delete)
  17935. (if (and (org-table-p)
  17936. (not (bolp))
  17937. (not (= (char-after) ?|))
  17938. (eq N 1))
  17939. (if (looking-at ".*?|")
  17940. (let ((pos (point))
  17941. (noalign (looking-at "[^|\n\r]* |"))
  17942. (c org-table-may-need-update))
  17943. (replace-match
  17944. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17945. (goto-char pos)
  17946. ;; noalign: if there were two spaces at the end, this field
  17947. ;; does not determine the width of the column.
  17948. (if noalign (setq org-table-may-need-update c)))
  17949. (delete-char N))
  17950. (delete-char N)
  17951. (org-fix-tags-on-the-fly))))
  17952. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17953. (put 'org-self-insert-command 'delete-selection
  17954. (lambda ()
  17955. (not (run-hook-with-args-until-success
  17956. 'self-insert-uses-region-functions))))
  17957. (put 'orgtbl-self-insert-command 'delete-selection
  17958. (lambda ()
  17959. (not (run-hook-with-args-until-success
  17960. 'self-insert-uses-region-functions))))
  17961. (put 'org-delete-char 'delete-selection 'supersede)
  17962. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17963. (put 'org-yank 'delete-selection 'yank)
  17964. ;; Make `flyspell-mode' delay after some commands
  17965. (put 'org-self-insert-command 'flyspell-delayed t)
  17966. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17967. (put 'org-delete-char 'flyspell-delayed t)
  17968. (put 'org-delete-backward-char 'flyspell-delayed t)
  17969. ;; Make pabbrev-mode expand after org-mode commands
  17970. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17971. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17972. (defun org-remap (map &rest commands)
  17973. "In MAP, remap the functions given in COMMANDS.
  17974. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17975. (let (new old)
  17976. (while commands
  17977. (setq old (pop commands) new (pop commands))
  17978. (if (fboundp 'command-remapping)
  17979. (org-defkey map (vector 'remap old) new)
  17980. (substitute-key-definition old new map global-map)))))
  17981. (defun org-transpose-words ()
  17982. "Transpose words for Org.
  17983. This uses the `org-mode-transpose-word-syntax-table' syntax
  17984. table, which interprets characters in `org-emphasis-alist' as
  17985. word constituents."
  17986. (interactive)
  17987. (with-syntax-table org-mode-transpose-word-syntax-table
  17988. (call-interactively 'transpose-words)))
  17989. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17990. (when (eq org-enable-table-editor 'optimized)
  17991. ;; If the user wants maximum table support, we need to hijack
  17992. ;; some standard editing functions
  17993. (org-remap org-mode-map
  17994. 'self-insert-command 'org-self-insert-command
  17995. 'delete-char 'org-delete-char
  17996. 'delete-backward-char 'org-delete-backward-char)
  17997. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17998. (defvar org-ctrl-c-ctrl-c-hook nil
  17999. "Hook for functions attaching themselves to `C-c C-c'.
  18000. This can be used to add additional functionality to the C-c C-c
  18001. key which executes context-dependent commands. This hook is run
  18002. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18003. run after the last test.
  18004. Each function will be called with no arguments. The function
  18005. must check if the context is appropriate for it to act. If yes,
  18006. it should do its thing and then return a non-nil value. If the
  18007. context is wrong, just do nothing and return nil.")
  18008. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18009. "Hook for functions attaching themselves to `C-c C-c'.
  18010. This can be used to add additional functionality to the C-c C-c
  18011. key which executes context-dependent commands. This hook is run
  18012. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18013. before the first test.
  18014. Each function will be called with no arguments. The function
  18015. must check if the context is appropriate for it to act. If yes,
  18016. it should do its thing and then return a non-nil value. If the
  18017. context is wrong, just do nothing and return nil.")
  18018. (defvar org-tab-first-hook nil
  18019. "Hook for functions to attach themselves to TAB.
  18020. See `org-ctrl-c-ctrl-c-hook' for more information.
  18021. This hook runs as the first action when TAB is pressed, even before
  18022. `org-cycle' messes around with the `outline-regexp' to cater for
  18023. inline tasks and plain list item folding.
  18024. If any function in this hook returns t, any other actions that
  18025. would have been caused by TAB (such as table field motion or visibility
  18026. cycling) will not occur.")
  18027. (defvar org-tab-after-check-for-table-hook nil
  18028. "Hook for functions to attach themselves to TAB.
  18029. See `org-ctrl-c-ctrl-c-hook' for more information.
  18030. This hook runs after it has been established that the cursor is not in a
  18031. table, but before checking if the cursor is in a headline or if global cycling
  18032. should be done.
  18033. If any function in this hook returns t, not other actions like visibility
  18034. cycling will be done.")
  18035. (defvar org-tab-after-check-for-cycling-hook nil
  18036. "Hook for functions to attach themselves to TAB.
  18037. See `org-ctrl-c-ctrl-c-hook' for more information.
  18038. This hook runs after it has been established that not table field motion and
  18039. not visibility should be done because of current context. This is probably
  18040. the place where a package like yasnippets can hook in.")
  18041. (defvar org-tab-before-tab-emulation-hook nil
  18042. "Hook for functions to attach themselves to TAB.
  18043. See `org-ctrl-c-ctrl-c-hook' for more information.
  18044. This hook runs after every other options for TAB have been exhausted, but
  18045. before indentation and \t insertion takes place.")
  18046. (defvar org-metaleft-hook nil
  18047. "Hook for functions attaching themselves to `M-left'.
  18048. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18049. (defvar org-metaright-hook nil
  18050. "Hook for functions attaching themselves to `M-right'.
  18051. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18052. (defvar org-metaup-hook nil
  18053. "Hook for functions attaching themselves to `M-up'.
  18054. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18055. (defvar org-metadown-hook nil
  18056. "Hook for functions attaching themselves to `M-down'.
  18057. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18058. (defvar org-shiftmetaleft-hook nil
  18059. "Hook for functions attaching themselves to `M-S-left'.
  18060. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18061. (defvar org-shiftmetaright-hook nil
  18062. "Hook for functions attaching themselves to `M-S-right'.
  18063. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18064. (defvar org-shiftmetaup-hook nil
  18065. "Hook for functions attaching themselves to `M-S-up'.
  18066. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18067. (defvar org-shiftmetadown-hook nil
  18068. "Hook for functions attaching themselves to `M-S-down'.
  18069. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18070. (defvar org-metareturn-hook nil
  18071. "Hook for functions attaching themselves to `M-RET'.
  18072. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18073. (defvar org-shiftup-hook nil
  18074. "Hook for functions attaching themselves to `S-up'.
  18075. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18076. (defvar org-shiftup-final-hook nil
  18077. "Hook for functions attaching themselves to `S-up'.
  18078. This one runs after all other options except shift-select have been excluded.
  18079. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18080. (defvar org-shiftdown-hook nil
  18081. "Hook for functions attaching themselves to `S-down'.
  18082. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18083. (defvar org-shiftdown-final-hook nil
  18084. "Hook for functions attaching themselves to `S-down'.
  18085. This one runs after all other options except shift-select have been excluded.
  18086. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18087. (defvar org-shiftleft-hook nil
  18088. "Hook for functions attaching themselves to `S-left'.
  18089. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18090. (defvar org-shiftleft-final-hook nil
  18091. "Hook for functions attaching themselves to `S-left'.
  18092. This one runs after all other options except shift-select have been excluded.
  18093. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18094. (defvar org-shiftright-hook nil
  18095. "Hook for functions attaching themselves to `S-right'.
  18096. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18097. (defvar org-shiftright-final-hook nil
  18098. "Hook for functions attaching themselves to `S-right'.
  18099. This one runs after all other options except shift-select have been excluded.
  18100. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18101. (defun org-modifier-cursor-error ()
  18102. "Throw an error, a modified cursor command was applied in wrong context."
  18103. (user-error "This command is active in special context like tables, headlines or items"))
  18104. (defun org-shiftselect-error ()
  18105. "Throw an error because Shift-Cursor command was applied in wrong context."
  18106. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18107. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18108. (user-error "This command works only in special context like headlines or timestamps")))
  18109. (defun org-call-for-shift-select (cmd)
  18110. (let ((this-command-keys-shift-translated t))
  18111. (call-interactively cmd)))
  18112. (defun org-shifttab (&optional arg)
  18113. "Global visibility cycling or move to previous table field.
  18114. Call `org-table-previous-field' within a table.
  18115. When ARG is nil, cycle globally through visibility states.
  18116. When ARG is a numeric prefix, show contents of this level."
  18117. (interactive "P")
  18118. (cond
  18119. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18120. ((integerp arg)
  18121. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18122. (message "Content view to level: %d" arg)
  18123. (org-content (prefix-numeric-value arg2))
  18124. (org-cycle-show-empty-lines t)
  18125. (setq org-cycle-global-status 'overview)))
  18126. (t (call-interactively 'org-global-cycle))))
  18127. (defun org-shiftmetaleft ()
  18128. "Promote subtree or delete table column.
  18129. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18130. `org-table-delete-column', depending on context. See the
  18131. individual commands for more information."
  18132. (interactive)
  18133. (cond
  18134. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18135. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18136. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18137. ((if (not (org-region-active-p)) (org-at-item-p)
  18138. (save-excursion (goto-char (region-beginning))
  18139. (org-at-item-p)))
  18140. (call-interactively 'org-outdent-item-tree))
  18141. (t (org-modifier-cursor-error))))
  18142. (defun org-shiftmetaright ()
  18143. "Demote subtree or insert table column.
  18144. Calls `org-demote-subtree', `org-indent-item-tree', or
  18145. `org-table-insert-column', depending on context. See the
  18146. individual commands for more information."
  18147. (interactive)
  18148. (cond
  18149. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18150. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18151. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18152. ((if (not (org-region-active-p)) (org-at-item-p)
  18153. (save-excursion (goto-char (region-beginning))
  18154. (org-at-item-p)))
  18155. (call-interactively 'org-indent-item-tree))
  18156. (t (org-modifier-cursor-error))))
  18157. (defun org-shiftmetaup (&optional arg)
  18158. "Move subtree up or kill table row.
  18159. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18160. `org-move-item-up' or `org-timestamp-up', depending on context.
  18161. See the individual commands for more information."
  18162. (interactive "P")
  18163. (cond
  18164. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18165. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18166. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18167. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18168. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18169. (call-interactively 'org-timestamp-up)))
  18170. (t (call-interactively 'org-drag-line-backward))))
  18171. (defun org-shiftmetadown (&optional arg)
  18172. "Move subtree down or insert table row.
  18173. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18174. `org-move-item-down' or `org-timestamp-up', depending on context.
  18175. See the individual commands for more information."
  18176. (interactive "P")
  18177. (cond
  18178. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18179. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18180. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18181. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18182. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18183. (call-interactively 'org-timestamp-down)))
  18184. (t (call-interactively 'org-drag-line-forward))))
  18185. (defsubst org-hidden-tree-error ()
  18186. (user-error
  18187. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18188. (defun org-metaleft (&optional arg)
  18189. "Promote heading or move table column to left.
  18190. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18191. With no specific context, calls the Emacs default `backward-word'.
  18192. See the individual commands for more information."
  18193. (interactive "P")
  18194. (cond
  18195. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18196. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18197. ((org-with-limited-levels
  18198. (or (org-at-heading-p)
  18199. (and (org-region-active-p)
  18200. (save-excursion
  18201. (goto-char (region-beginning))
  18202. (org-at-heading-p)))))
  18203. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18204. (call-interactively 'org-do-promote))
  18205. ;; At an inline task.
  18206. ((org-at-heading-p)
  18207. (call-interactively 'org-inlinetask-promote))
  18208. ((or (org-at-item-p)
  18209. (and (org-region-active-p)
  18210. (save-excursion
  18211. (goto-char (region-beginning))
  18212. (org-at-item-p))))
  18213. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18214. (call-interactively 'org-outdent-item))
  18215. (t (call-interactively 'backward-word))))
  18216. (defun org-metaright (&optional arg)
  18217. "Demote a subtree, a list item or move table column to right.
  18218. In front of a drawer or a block keyword, indent it correctly.
  18219. With no specific context, calls the Emacs default `forward-word'.
  18220. See the individual commands for more information."
  18221. (interactive "P")
  18222. (cond
  18223. ((run-hook-with-args-until-success 'org-metaright-hook))
  18224. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18225. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18226. ((org-at-block-p) (call-interactively 'org-indent-block))
  18227. ((org-with-limited-levels
  18228. (or (org-at-heading-p)
  18229. (and (org-region-active-p)
  18230. (save-excursion
  18231. (goto-char (region-beginning))
  18232. (org-at-heading-p)))))
  18233. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18234. (call-interactively 'org-do-demote))
  18235. ;; At an inline task.
  18236. ((org-at-heading-p)
  18237. (call-interactively 'org-inlinetask-demote))
  18238. ((or (org-at-item-p)
  18239. (and (org-region-active-p)
  18240. (save-excursion
  18241. (goto-char (region-beginning))
  18242. (org-at-item-p))))
  18243. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18244. (call-interactively 'org-indent-item))
  18245. (t (call-interactively 'forward-word))))
  18246. (defun org-check-for-hidden (what)
  18247. "Check if there are hidden headlines/items in the current visual line.
  18248. WHAT can be either `headlines' or `items'. If the current line is
  18249. an outline or item heading and it has a folded subtree below it,
  18250. this function returns t, nil otherwise."
  18251. (let ((re (cond
  18252. ((eq what 'headlines) org-outline-regexp-bol)
  18253. ((eq what 'items) (org-item-beginning-re))
  18254. (t (error "This should not happen"))))
  18255. beg end)
  18256. (save-excursion
  18257. (catch 'exit
  18258. (unless (org-region-active-p)
  18259. (setq beg (point-at-bol))
  18260. (beginning-of-line 2)
  18261. (while (and (not (eobp)) ;; this is like `next-line'
  18262. (get-char-property (1- (point)) 'invisible))
  18263. (beginning-of-line 2))
  18264. (setq end (point))
  18265. (goto-char beg)
  18266. (goto-char (point-at-eol))
  18267. (setq end (max end (point)))
  18268. (while (re-search-forward re end t)
  18269. (if (get-char-property (match-beginning 0) 'invisible)
  18270. (throw 'exit t))))
  18271. nil))))
  18272. (defun org-metaup (&optional arg)
  18273. "Move subtree up or move table row up.
  18274. Calls `org-move-subtree-up' or `org-table-move-row' or
  18275. `org-move-item-up', depending on context. See the individual commands
  18276. for more information."
  18277. (interactive "P")
  18278. (cond
  18279. ((run-hook-with-args-until-success 'org-metaup-hook))
  18280. ((org-region-active-p)
  18281. (let* ((a (min (region-beginning) (region-end)))
  18282. (b (1- (max (region-beginning) (region-end))))
  18283. (c (save-excursion (goto-char a)
  18284. (move-beginning-of-line 0)))
  18285. (d (save-excursion (goto-char a)
  18286. (move-end-of-line 0) (point))))
  18287. (transpose-regions a b c d)
  18288. (goto-char c)))
  18289. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18290. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18291. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18292. (t (org-drag-element-backward))))
  18293. (defun org-metadown (&optional arg)
  18294. "Move subtree down or move table row down.
  18295. Calls `org-move-subtree-down' or `org-table-move-row' or
  18296. `org-move-item-down', depending on context. See the individual
  18297. commands for more information."
  18298. (interactive "P")
  18299. (cond
  18300. ((run-hook-with-args-until-success 'org-metadown-hook))
  18301. ((org-region-active-p)
  18302. (let* ((a (min (region-beginning) (region-end)))
  18303. (b (max (region-beginning) (region-end)))
  18304. (c (save-excursion (goto-char b)
  18305. (move-beginning-of-line 1)))
  18306. (d (save-excursion (goto-char b)
  18307. (move-end-of-line 1) (1+ (point)))))
  18308. (transpose-regions a b c d)
  18309. (goto-char d)))
  18310. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18311. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18312. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18313. (t (org-drag-element-forward))))
  18314. (defun org-shiftup (&optional arg)
  18315. "Increase item in timestamp or increase priority of current headline.
  18316. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18317. depending on context. See the individual commands for more information."
  18318. (interactive "P")
  18319. (cond
  18320. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18321. ((and org-support-shift-select (org-region-active-p))
  18322. (org-call-for-shift-select 'previous-line))
  18323. ((org-at-timestamp-p t)
  18324. (call-interactively (if org-edit-timestamp-down-means-later
  18325. 'org-timestamp-down 'org-timestamp-up)))
  18326. ((and (not (eq org-support-shift-select 'always))
  18327. org-enable-priority-commands
  18328. (org-at-heading-p))
  18329. (call-interactively 'org-priority-up))
  18330. ((and (not org-support-shift-select) (org-at-item-p))
  18331. (call-interactively 'org-previous-item))
  18332. ((org-clocktable-try-shift 'up arg))
  18333. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18334. (org-support-shift-select
  18335. (org-call-for-shift-select 'previous-line))
  18336. (t (org-shiftselect-error))))
  18337. (defun org-shiftdown (&optional arg)
  18338. "Decrease item in timestamp or decrease priority of current headline.
  18339. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18340. depending on context. See the individual commands for more information."
  18341. (interactive "P")
  18342. (cond
  18343. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18344. ((and org-support-shift-select (org-region-active-p))
  18345. (org-call-for-shift-select 'next-line))
  18346. ((org-at-timestamp-p t)
  18347. (call-interactively (if org-edit-timestamp-down-means-later
  18348. 'org-timestamp-up 'org-timestamp-down)))
  18349. ((and (not (eq org-support-shift-select 'always))
  18350. org-enable-priority-commands
  18351. (org-at-heading-p))
  18352. (call-interactively 'org-priority-down))
  18353. ((and (not org-support-shift-select) (org-at-item-p))
  18354. (call-interactively 'org-next-item))
  18355. ((org-clocktable-try-shift 'down arg))
  18356. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18357. (org-support-shift-select
  18358. (org-call-for-shift-select 'next-line))
  18359. (t (org-shiftselect-error))))
  18360. (defun org-shiftright (&optional arg)
  18361. "Cycle the thing at point or in the current line, depending on context.
  18362. Depending on context, this does one of the following:
  18363. - switch a timestamp at point one day into the future
  18364. - on a headline, switch to the next TODO keyword.
  18365. - on an item, switch entire list to the next bullet type
  18366. - on a property line, switch to the next allowed value
  18367. - on a clocktable definition line, move time block into the future"
  18368. (interactive "P")
  18369. (cond
  18370. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18371. ((and org-support-shift-select (org-region-active-p))
  18372. (org-call-for-shift-select 'forward-char))
  18373. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18374. ((and (not (eq org-support-shift-select 'always))
  18375. (org-at-heading-p))
  18376. (let ((org-inhibit-logging
  18377. (not org-treat-S-cursor-todo-selection-as-state-change))
  18378. (org-inhibit-blocking
  18379. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18380. (org-call-with-arg 'org-todo 'right)))
  18381. ((or (and org-support-shift-select
  18382. (not (eq org-support-shift-select 'always))
  18383. (org-at-item-bullet-p))
  18384. (and (not org-support-shift-select) (org-at-item-p)))
  18385. (org-call-with-arg 'org-cycle-list-bullet nil))
  18386. ((and (not (eq org-support-shift-select 'always))
  18387. (org-at-property-p))
  18388. (call-interactively 'org-property-next-allowed-value))
  18389. ((org-clocktable-try-shift 'right arg))
  18390. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18391. (org-support-shift-select
  18392. (org-call-for-shift-select 'forward-char))
  18393. (t (org-shiftselect-error))))
  18394. (defun org-shiftleft (&optional arg)
  18395. "Cycle the thing at point or in the current line, depending on context.
  18396. Depending on context, this does one of the following:
  18397. - switch a timestamp at point one day into the past
  18398. - on a headline, switch to the previous TODO keyword.
  18399. - on an item, switch entire list to the previous bullet type
  18400. - on a property line, switch to the previous allowed value
  18401. - on a clocktable definition line, move time block into the past"
  18402. (interactive "P")
  18403. (cond
  18404. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18405. ((and org-support-shift-select (org-region-active-p))
  18406. (org-call-for-shift-select 'backward-char))
  18407. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18408. ((and (not (eq org-support-shift-select 'always))
  18409. (org-at-heading-p))
  18410. (let ((org-inhibit-logging
  18411. (not org-treat-S-cursor-todo-selection-as-state-change))
  18412. (org-inhibit-blocking
  18413. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18414. (org-call-with-arg 'org-todo 'left)))
  18415. ((or (and org-support-shift-select
  18416. (not (eq org-support-shift-select 'always))
  18417. (org-at-item-bullet-p))
  18418. (and (not org-support-shift-select) (org-at-item-p)))
  18419. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18420. ((and (not (eq org-support-shift-select 'always))
  18421. (org-at-property-p))
  18422. (call-interactively 'org-property-previous-allowed-value))
  18423. ((org-clocktable-try-shift 'left arg))
  18424. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18425. (org-support-shift-select
  18426. (org-call-for-shift-select 'backward-char))
  18427. (t (org-shiftselect-error))))
  18428. (defun org-shiftcontrolright ()
  18429. "Switch to next TODO set."
  18430. (interactive)
  18431. (cond
  18432. ((and org-support-shift-select (org-region-active-p))
  18433. (org-call-for-shift-select 'forward-word))
  18434. ((and (not (eq org-support-shift-select 'always))
  18435. (org-at-heading-p))
  18436. (org-call-with-arg 'org-todo 'nextset))
  18437. (org-support-shift-select
  18438. (org-call-for-shift-select 'forward-word))
  18439. (t (org-shiftselect-error))))
  18440. (defun org-shiftcontrolleft ()
  18441. "Switch to previous TODO set."
  18442. (interactive)
  18443. (cond
  18444. ((and org-support-shift-select (org-region-active-p))
  18445. (org-call-for-shift-select 'backward-word))
  18446. ((and (not (eq org-support-shift-select 'always))
  18447. (org-at-heading-p))
  18448. (org-call-with-arg 'org-todo 'previousset))
  18449. (org-support-shift-select
  18450. (org-call-for-shift-select 'backward-word))
  18451. (t (org-shiftselect-error))))
  18452. (defun org-shiftcontrolup (&optional n)
  18453. "Change timestamps synchronously up in CLOCK log lines.
  18454. Optional argument N tells to change by that many units."
  18455. (interactive "P")
  18456. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18457. (let (org-support-shift-select)
  18458. (org-clock-timestamps-up n))
  18459. (user-error "Not at a clock log")))
  18460. (defun org-shiftcontroldown (&optional n)
  18461. "Change timestamps synchronously down in CLOCK log lines.
  18462. Optional argument N tells to change by that many units."
  18463. (interactive "P")
  18464. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18465. (let (org-support-shift-select)
  18466. (org-clock-timestamps-down n))
  18467. (user-error "Not at a clock log")))
  18468. (defun org-ctrl-c-ret ()
  18469. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18470. (interactive)
  18471. (cond
  18472. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18473. (t (call-interactively 'org-insert-heading))))
  18474. (defun org-find-visible ()
  18475. (let ((s (point)))
  18476. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18477. (get-char-property s 'invisible)))
  18478. s))
  18479. (defun org-find-invisible ()
  18480. (let ((s (point)))
  18481. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18482. (not (get-char-property s 'invisible))))
  18483. s))
  18484. (defun org-copy-visible (beg end)
  18485. "Copy the visible parts of the region."
  18486. (interactive "r")
  18487. (let (snippets s)
  18488. (save-excursion
  18489. (save-restriction
  18490. (narrow-to-region beg end)
  18491. (setq s (goto-char (point-min)))
  18492. (while (not (= (point) (point-max)))
  18493. (goto-char (org-find-invisible))
  18494. (push (buffer-substring s (point)) snippets)
  18495. (setq s (goto-char (org-find-visible))))))
  18496. (kill-new (apply 'concat (nreverse snippets)))))
  18497. (defun org-copy-special ()
  18498. "Copy region in table or copy current subtree.
  18499. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18500. See the individual commands for more information."
  18501. (interactive)
  18502. (call-interactively
  18503. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18504. (defun org-cut-special ()
  18505. "Cut region in table or cut current subtree.
  18506. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18507. See the individual commands for more information."
  18508. (interactive)
  18509. (call-interactively
  18510. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18511. (defun org-paste-special (arg)
  18512. "Paste rectangular region into table, or past subtree relative to level.
  18513. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18514. See the individual commands for more information."
  18515. (interactive "P")
  18516. (if (org-at-table-p)
  18517. (org-table-paste-rectangle)
  18518. (org-paste-subtree arg)))
  18519. (defsubst org-in-fixed-width-region-p ()
  18520. "Is point in a fixed-width region?"
  18521. (save-match-data
  18522. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18523. (defun org-edit-special (&optional arg)
  18524. "Call a special editor for the element at point.
  18525. When at a table, call the formula editor with `org-table-edit-formulas'.
  18526. When in a source code block, call `org-edit-src-code'.
  18527. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18528. When at an #+INCLUDE keyword, visit the included file.
  18529. On a link, call `ffap' to visit the link at point.
  18530. Otherwise, return a user error."
  18531. (interactive "P")
  18532. (let ((element (org-element-at-point)))
  18533. (assert (not buffer-read-only) nil
  18534. "Buffer is read-only: %s" (buffer-name))
  18535. (case (org-element-type element)
  18536. (src-block
  18537. (if (not arg) (org-edit-src-code)
  18538. (let* ((info (org-babel-get-src-block-info))
  18539. (lang (nth 0 info))
  18540. (params (nth 2 info))
  18541. (session (cdr (assq :session params))))
  18542. (if (not session) (org-edit-src-code)
  18543. ;; At a src-block with a session and function called with
  18544. ;; an ARG: switch to the buffer related to the inferior
  18545. ;; process.
  18546. (switch-to-buffer
  18547. (funcall (intern (concat "org-babel-prep-session:" lang))
  18548. session params))))))
  18549. (keyword
  18550. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18551. (find-file
  18552. (org-remove-double-quotes
  18553. (car (org-split-string (org-element-property :value element)))))
  18554. (user-error "No special environment to edit here")))
  18555. (table
  18556. (if (eq (org-element-property :type element) 'table.el)
  18557. (org-edit-src-code)
  18558. (call-interactively 'org-table-edit-formulas)))
  18559. ;; Only Org tables contain `table-row' type elements.
  18560. (table-row (call-interactively 'org-table-edit-formulas))
  18561. ((example-block export-block) (org-edit-src-code))
  18562. (fixed-width (org-edit-fixed-width-region))
  18563. (otherwise
  18564. ;; No notable element at point. Though, we may be at a link,
  18565. ;; which is an object. Thus, scan deeper.
  18566. (if (eq (org-element-type (org-element-context element)) 'link)
  18567. (call-interactively 'ffap)
  18568. (user-error "No special environment to edit here"))))))
  18569. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18570. (defun org-ctrl-c-ctrl-c (&optional arg)
  18571. "Set tags in headline, or update according to changed information at point.
  18572. This command does many different things, depending on context:
  18573. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18574. this is what we do.
  18575. - If the cursor is on a statistics cookie, update it.
  18576. - If the cursor is in a headline, prompt for tags and insert them
  18577. into the current line, aligned to `org-tags-column'. When called
  18578. with prefix arg, realign all tags in the current buffer.
  18579. - If the cursor is in one of the special #+KEYWORD lines, this
  18580. triggers scanning the buffer for these lines and updating the
  18581. information.
  18582. - If the cursor is inside a table, realign the table. This command
  18583. works even if the automatic table editor has been turned off.
  18584. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18585. the entire table.
  18586. - If the cursor is at a footnote reference or definition, jump to
  18587. the corresponding definition or references, respectively.
  18588. - If the cursor is a the beginning of a dynamic block, update it.
  18589. - If the current buffer is a capture buffer, close note and file it.
  18590. - If the cursor is on a <<<target>>>, update radio targets and
  18591. corresponding links in this buffer.
  18592. - If the cursor is on a numbered item in a plain list, renumber the
  18593. ordered list.
  18594. - If the cursor is on a checkbox, toggle it.
  18595. - If the cursor is on a code block, evaluate it. The variable
  18596. `org-confirm-babel-evaluate' can be used to control prompting
  18597. before code block evaluation, by default every code block
  18598. evaluation requires confirmation. Code block evaluation can be
  18599. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18600. (interactive "P")
  18601. (cond
  18602. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18603. org-occur-highlights
  18604. org-latex-fragment-image-overlays)
  18605. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18606. (org-remove-occur-highlights)
  18607. (org-remove-latex-fragment-image-overlays)
  18608. (message "Temporary highlights/overlays removed from current buffer"))
  18609. ((and (local-variable-p 'org-finish-function (current-buffer))
  18610. (fboundp org-finish-function))
  18611. (funcall org-finish-function))
  18612. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18613. (t
  18614. (let* ((context (org-element-context)) (type (org-element-type context)))
  18615. ;; Test if point is within a blank line.
  18616. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18617. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18618. (user-error "C-c C-c can do nothing useful at this location"))
  18619. (case type
  18620. ;; When at a link, act according to the parent instead.
  18621. (link (setq context (org-element-property :parent context))
  18622. (setq type (org-element-type context)))
  18623. ;; Unsupported object types: refer to the first supported
  18624. ;; element or object containing it.
  18625. ((bold code entity export-snippet inline-babel-call inline-src-block
  18626. italic latex-fragment line-break macro strike-through subscript
  18627. superscript underline verbatim)
  18628. (while (and (setq context (org-element-property :parent context))
  18629. (not (memq (setq type (org-element-type context))
  18630. '(radio-target paragraph verse-block
  18631. table-cell)))))))
  18632. ;; For convenience: at the first line of a paragraph on the
  18633. ;; same line as an item, apply function on that item instead.
  18634. (when (eq type 'paragraph)
  18635. (let ((parent (org-element-property :parent context)))
  18636. (when (and (eq (org-element-type parent) 'item)
  18637. (= (point-at-bol) (org-element-property :begin parent)))
  18638. (setq context parent type 'item))))
  18639. ;; Act according to type of element or object at point.
  18640. (case type
  18641. (clock (org-clock-update-time-maybe))
  18642. (dynamic-block
  18643. (save-excursion
  18644. (goto-char (org-element-property :post-affiliated context))
  18645. (org-update-dblock)))
  18646. (footnote-definition
  18647. (goto-char (org-element-property :post-affiliated context))
  18648. (call-interactively 'org-footnote-action))
  18649. (footnote-reference (call-interactively 'org-footnote-action))
  18650. ((headline inlinetask)
  18651. (save-excursion (goto-char (org-element-property :begin context))
  18652. (call-interactively 'org-set-tags)))
  18653. (item
  18654. ;; At an item: a double C-u set checkbox to "[-]"
  18655. ;; unconditionally, whereas a single one will toggle its
  18656. ;; presence. Without an universal argument, if the item
  18657. ;; has a checkbox, toggle it. Otherwise repair the list.
  18658. (let* ((box (org-element-property :checkbox context))
  18659. (struct (org-element-property :structure context))
  18660. (old-struct (copy-tree struct))
  18661. (parents (org-list-parents-alist struct))
  18662. (prevs (org-list-prevs-alist struct))
  18663. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18664. (org-list-set-checkbox
  18665. (org-element-property :begin context) struct
  18666. (cond ((equal arg '(16)) "[-]")
  18667. ((and (not box) (equal arg '(4))) "[ ]")
  18668. ((or (not box) (equal arg '(4))) nil)
  18669. ((eq box 'on) "[ ]")
  18670. (t "[X]")))
  18671. ;; Mimic `org-list-write-struct' but with grabbing
  18672. ;; a return value from `org-list-struct-fix-box'.
  18673. (org-list-struct-fix-ind struct parents 2)
  18674. (org-list-struct-fix-item-end struct)
  18675. (org-list-struct-fix-bul struct prevs)
  18676. (org-list-struct-fix-ind struct parents)
  18677. (let ((block-item
  18678. (org-list-struct-fix-box struct parents prevs orderedp)))
  18679. (if (and box (equal struct old-struct))
  18680. (if (equal arg '(16))
  18681. (message "Checkboxes already reset")
  18682. (user-error "Cannot toggle this checkbox: %s"
  18683. (if (eq box 'on)
  18684. "all subitems checked"
  18685. "unchecked subitems")))
  18686. (org-list-struct-apply-struct struct old-struct)
  18687. (org-update-checkbox-count-maybe))
  18688. (when block-item
  18689. (message "Checkboxes were removed due to empty box at line %d"
  18690. (org-current-line block-item))))))
  18691. (keyword
  18692. (let ((org-inhibit-startup-visibility-stuff t)
  18693. (org-startup-align-all-tables nil))
  18694. (when (boundp 'org-table-coordinate-overlays)
  18695. (mapc 'delete-overlay org-table-coordinate-overlays)
  18696. (setq org-table-coordinate-overlays nil))
  18697. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18698. (message "Local setup has been refreshed"))
  18699. (plain-list
  18700. ;; At a plain list, with a double C-u argument, set
  18701. ;; checkboxes of each item to "[-]", whereas a single one
  18702. ;; will toggle their presence according to the state of the
  18703. ;; first item in the list. Without an argument, repair the
  18704. ;; list.
  18705. (let* ((begin (org-element-property :contents-begin context))
  18706. (beginm (move-marker (make-marker) begin))
  18707. (struct (org-element-property :structure context))
  18708. (old-struct (copy-tree struct))
  18709. (first-box (save-excursion
  18710. (goto-char begin)
  18711. (looking-at org-list-full-item-re)
  18712. (match-string-no-properties 3)))
  18713. (new-box (cond ((equal arg '(16)) "[-]")
  18714. ((equal arg '(4)) (unless first-box "[ ]"))
  18715. ((equal first-box "[X]") "[ ]")
  18716. (t "[X]"))))
  18717. (cond
  18718. (arg
  18719. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18720. (org-list-get-all-items
  18721. begin struct (org-list-prevs-alist struct))))
  18722. ((and first-box (eq (point) begin))
  18723. ;; For convenience, when point is at bol on the first
  18724. ;; item of the list and no argument is provided, simply
  18725. ;; toggle checkbox of that item, if any.
  18726. (org-list-set-checkbox begin struct new-box)))
  18727. (org-list-write-struct
  18728. struct (org-list-parents-alist struct) old-struct)
  18729. (org-update-checkbox-count-maybe)
  18730. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18731. ((property-drawer node-property)
  18732. (call-interactively 'org-property-action))
  18733. ((radio-target target)
  18734. (call-interactively 'org-update-radio-target-regexp))
  18735. (statistics-cookie
  18736. (call-interactively 'org-update-statistics-cookies))
  18737. ((table table-cell table-row)
  18738. ;; At a table, recalculate every field and align it. Also
  18739. ;; send the table if necessary. If the table has
  18740. ;; a `table.el' type, just give up. At a table row or
  18741. ;; cell, maybe recalculate line but always align table.
  18742. (if (eq (org-element-property :type context) 'table.el)
  18743. (message "Use C-c ' to edit table.el tables")
  18744. (let ((org-enable-table-editor t))
  18745. (if (or (eq type 'table)
  18746. ;; Check if point is at a TBLFM line.
  18747. (and (eq type 'table-row)
  18748. (= (point) (org-element-property :end context))))
  18749. (save-excursion
  18750. (if (org-at-TBLFM-p)
  18751. (progn (require 'org-table)
  18752. (org-table-calc-current-TBLFM))
  18753. (goto-char (org-element-property :contents-begin context))
  18754. (org-call-with-arg 'org-table-recalculate (or arg t))
  18755. (orgtbl-send-table 'maybe)))
  18756. (org-table-maybe-eval-formula)
  18757. (cond (arg (call-interactively 'org-table-recalculate))
  18758. ((org-table-maybe-recalculate-line))
  18759. (t (org-table-align)))))))
  18760. (timestamp (org-timestamp-change 0 'day))
  18761. (otherwise
  18762. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18763. (user-error
  18764. "C-c C-c can do nothing useful at this location")))))))))
  18765. (defun org-mode-restart ()
  18766. (interactive)
  18767. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  18768. (funcall major-mode)
  18769. (hack-local-variables)
  18770. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  18771. (org-indent-mode -1)))
  18772. (message "%s restarted" major-mode))
  18773. (defun org-kill-note-or-show-branches ()
  18774. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18775. (interactive)
  18776. (if (not org-finish-function)
  18777. (progn
  18778. (hide-subtree)
  18779. (call-interactively 'show-branches))
  18780. (let ((org-note-abort t))
  18781. (funcall org-finish-function))))
  18782. (defun org-open-line (n)
  18783. "Insert a new row in tables, call `open-line' elsewhere.
  18784. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18785. (interactive "*p")
  18786. (cond
  18787. ((not org-special-ctrl-o)
  18788. (open-line n))
  18789. ((org-at-table-p)
  18790. (org-table-insert-row))
  18791. (t
  18792. (open-line n))))
  18793. (defun org-return (&optional indent)
  18794. "Goto next table row or insert a newline.
  18795. Calls `org-table-next-row' or `newline', depending on context.
  18796. See the individual commands for more information."
  18797. (interactive)
  18798. (let (org-ts-what)
  18799. (cond
  18800. ((or (bobp) (org-in-src-block-p))
  18801. (if indent (newline-and-indent) (newline)))
  18802. ((org-at-table-p)
  18803. (org-table-justify-field-maybe)
  18804. (call-interactively 'org-table-next-row))
  18805. ;; when `newline-and-indent' is called within a list, make sure
  18806. ;; text moved stays inside the item.
  18807. ((and (org-in-item-p) indent)
  18808. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18809. (progn
  18810. (save-match-data (newline))
  18811. (org-indent-line-to (length (match-string 0))))
  18812. (let ((ind (org-get-indentation)))
  18813. (newline)
  18814. (if (org-looking-back org-list-end-re)
  18815. (org-indent-line)
  18816. (org-indent-line-to ind)))))
  18817. ((and org-return-follows-link
  18818. (org-at-timestamp-p t)
  18819. (not (eq org-ts-what 'after)))
  18820. (org-follow-timestamp-link))
  18821. ((and org-return-follows-link
  18822. (let ((tprop (get-text-property (point) 'face)))
  18823. (or (eq tprop 'org-link)
  18824. (and (listp tprop) (memq 'org-link tprop)))))
  18825. (call-interactively 'org-open-at-point))
  18826. ((and (org-at-heading-p)
  18827. (looking-at
  18828. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18829. (org-show-entry)
  18830. (end-of-line 1)
  18831. (newline))
  18832. (t (if indent (newline-and-indent) (newline))))))
  18833. (defun org-return-indent ()
  18834. "Goto next table row or insert a newline and indent.
  18835. Calls `org-table-next-row' or `newline-and-indent', depending on
  18836. context. See the individual commands for more information."
  18837. (interactive)
  18838. (org-return t))
  18839. (defun org-ctrl-c-star ()
  18840. "Compute table, or change heading status of lines.
  18841. Calls `org-table-recalculate' or `org-toggle-heading',
  18842. depending on context."
  18843. (interactive)
  18844. (cond
  18845. ((org-at-table-p)
  18846. (call-interactively 'org-table-recalculate))
  18847. (t
  18848. ;; Convert all lines in region to list items
  18849. (call-interactively 'org-toggle-heading))))
  18850. (defun org-ctrl-c-minus ()
  18851. "Insert separator line in table or modify bullet status of line.
  18852. Also turns a plain line or a region of lines into list items.
  18853. Calls `org-table-insert-hline', `org-toggle-item', or
  18854. `org-cycle-list-bullet', depending on context."
  18855. (interactive)
  18856. (cond
  18857. ((org-at-table-p)
  18858. (call-interactively 'org-table-insert-hline))
  18859. ((org-region-active-p)
  18860. (call-interactively 'org-toggle-item))
  18861. ((org-in-item-p)
  18862. (call-interactively 'org-cycle-list-bullet))
  18863. (t
  18864. (call-interactively 'org-toggle-item))))
  18865. (defun org-toggle-item (arg)
  18866. "Convert headings or normal lines to items, items to normal lines.
  18867. If there is no active region, only the current line is considered.
  18868. If the first non blank line in the region is a headline, convert
  18869. all headlines to items, shifting text accordingly.
  18870. If it is an item, convert all items to normal lines.
  18871. If it is normal text, change region into a list of items.
  18872. With a prefix argument ARG, change the region in a single item."
  18873. (interactive "P")
  18874. (let ((shift-text
  18875. (function
  18876. ;; Shift text in current section to IND, from point to END.
  18877. ;; The function leaves point to END line.
  18878. (lambda (ind end)
  18879. (let ((min-i 1000) (end (copy-marker end)))
  18880. ;; First determine the minimum indentation (MIN-I) of
  18881. ;; the text.
  18882. (save-excursion
  18883. (catch 'exit
  18884. (while (< (point) end)
  18885. (let ((i (org-get-indentation)))
  18886. (cond
  18887. ;; Skip blank lines and inline tasks.
  18888. ((looking-at "^[ \t]*$"))
  18889. ((looking-at org-outline-regexp-bol))
  18890. ;; We can't find less than 0 indentation.
  18891. ((zerop i) (throw 'exit (setq min-i 0)))
  18892. ((< i min-i) (setq min-i i))))
  18893. (forward-line))))
  18894. ;; Then indent each line so that a line indented to
  18895. ;; MIN-I becomes indented to IND. Ignore blank lines
  18896. ;; and inline tasks in the process.
  18897. (let ((delta (- ind min-i)))
  18898. (while (< (point) end)
  18899. (unless (or (looking-at "^[ \t]*$")
  18900. (looking-at org-outline-regexp-bol))
  18901. (org-indent-line-to (+ (org-get-indentation) delta)))
  18902. (forward-line)))))))
  18903. (skip-blanks
  18904. (function
  18905. ;; Return beginning of first non-blank line, starting from
  18906. ;; line at POS.
  18907. (lambda (pos)
  18908. (save-excursion
  18909. (goto-char pos)
  18910. (skip-chars-forward " \r\t\n")
  18911. (point-at-bol)))))
  18912. beg end)
  18913. ;; Determine boundaries of changes.
  18914. (if (org-region-active-p)
  18915. (setq beg (funcall skip-blanks (region-beginning))
  18916. end (copy-marker (region-end)))
  18917. (setq beg (funcall skip-blanks (point-at-bol))
  18918. end (copy-marker (point-at-eol))))
  18919. ;; Depending on the starting line, choose an action on the text
  18920. ;; between BEG and END.
  18921. (org-with-limited-levels
  18922. (save-excursion
  18923. (goto-char beg)
  18924. (cond
  18925. ;; Case 1. Start at an item: de-itemize. Note that it only
  18926. ;; happens when a region is active: `org-ctrl-c-minus'
  18927. ;; would call `org-cycle-list-bullet' otherwise.
  18928. ((org-at-item-p)
  18929. (while (< (point) end)
  18930. (when (org-at-item-p)
  18931. (skip-chars-forward " \t")
  18932. (delete-region (point) (match-end 0)))
  18933. (forward-line)))
  18934. ;; Case 2. Start at an heading: convert to items.
  18935. ((org-at-heading-p)
  18936. (let* ((bul (org-list-bullet-string "-"))
  18937. (bul-len (length bul))
  18938. ;; Indentation of the first heading. It should be
  18939. ;; relative to the indentation of its parent, if any.
  18940. (start-ind (save-excursion
  18941. (cond
  18942. ((not org-adapt-indentation) 0)
  18943. ((not (outline-previous-heading)) 0)
  18944. (t (length (match-string 0))))))
  18945. ;; Level of first heading. Further headings will be
  18946. ;; compared to it to determine hierarchy in the list.
  18947. (ref-level (org-reduced-level (org-outline-level))))
  18948. (while (< (point) end)
  18949. (let* ((level (org-reduced-level (org-outline-level)))
  18950. (delta (max 0 (- level ref-level))))
  18951. ;; If current headline is less indented than the first
  18952. ;; one, set it as reference, in order to preserve
  18953. ;; subtrees.
  18954. (when (< level ref-level) (setq ref-level level))
  18955. (replace-match bul t t)
  18956. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18957. ;; Ensure all text down to END (or SECTION-END) belongs
  18958. ;; to the newly created item.
  18959. (let ((section-end (save-excursion
  18960. (or (outline-next-heading) (point)))))
  18961. (forward-line)
  18962. (funcall shift-text
  18963. (+ start-ind (* (1+ delta) bul-len))
  18964. (min end section-end)))))))
  18965. ;; Case 3. Normal line with ARG: make the first line of region
  18966. ;; an item, and shift indentation of others lines to
  18967. ;; set them as item's body.
  18968. (arg (let* ((bul (org-list-bullet-string "-"))
  18969. (bul-len (length bul))
  18970. (ref-ind (org-get-indentation)))
  18971. (skip-chars-forward " \t")
  18972. (insert bul)
  18973. (forward-line)
  18974. (while (< (point) end)
  18975. ;; Ensure that lines less indented than first one
  18976. ;; still get included in item body.
  18977. (funcall shift-text
  18978. (+ ref-ind bul-len)
  18979. (min end (save-excursion (or (outline-next-heading)
  18980. (point)))))
  18981. (forward-line))))
  18982. ;; Case 4. Normal line without ARG: turn each non-item line
  18983. ;; into an item.
  18984. (t
  18985. (while (< (point) end)
  18986. (unless (or (org-at-heading-p) (org-at-item-p))
  18987. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18988. (replace-match
  18989. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18990. (forward-line))))))))
  18991. (defun org-toggle-heading (&optional nstars)
  18992. "Convert headings to normal text, or items or text to headings.
  18993. If there is no active region, only convert the current line.
  18994. With a \\[universal-argument] prefix, convert the whole list at
  18995. point into heading.
  18996. In a region:
  18997. - If the first non blank line is a headline, remove the stars
  18998. from all headlines in the region.
  18999. - If it is a normal line, turn each and every normal line (i.e.,
  19000. not an heading or an item) in the region into headings. If you
  19001. want to convert only the first line of this region, use one
  19002. universal prefix argument.
  19003. - If it is a plain list item, turn all plain list items into headings.
  19004. When converting a line into a heading, the number of stars is chosen
  19005. such that the lines become children of the current entry. However,
  19006. when a numeric prefix argument is given, its value determines the
  19007. number of stars to add."
  19008. (interactive "P")
  19009. (let ((skip-blanks
  19010. (function
  19011. ;; Return beginning of first non-blank line, starting from
  19012. ;; line at POS.
  19013. (lambda (pos)
  19014. (save-excursion
  19015. (goto-char pos)
  19016. (while (org-at-comment-p) (forward-line))
  19017. (skip-chars-forward " \r\t\n")
  19018. (point-at-bol)))))
  19019. beg end toggled)
  19020. ;; Determine boundaries of changes. If a universal prefix has
  19021. ;; been given, put the list in a region. If region ends at a bol,
  19022. ;; do not consider the last line to be in the region.
  19023. (when (and current-prefix-arg (org-at-item-p))
  19024. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19025. (org-mark-element))
  19026. (if (org-region-active-p)
  19027. (setq beg (funcall skip-blanks (region-beginning))
  19028. end (copy-marker (save-excursion
  19029. (goto-char (region-end))
  19030. (if (bolp) (point) (point-at-eol)))))
  19031. (setq beg (funcall skip-blanks (point-at-bol))
  19032. end (copy-marker (point-at-eol))))
  19033. ;; Ensure inline tasks don't count as headings.
  19034. (org-with-limited-levels
  19035. (save-excursion
  19036. (goto-char beg)
  19037. (cond
  19038. ;; Case 1. Started at an heading: de-star headings.
  19039. ((org-at-heading-p)
  19040. (while (< (point) end)
  19041. (when (org-at-heading-p t)
  19042. (looking-at org-outline-regexp) (replace-match "")
  19043. (setq toggled t))
  19044. (forward-line)))
  19045. ;; Case 2. Started at an item: change items into headlines.
  19046. ;; One star will be added by `org-list-to-subtree'.
  19047. ((org-at-item-p)
  19048. (let* ((stars (make-string
  19049. ;; subtract the star that will be added again by
  19050. ;; `org-list-to-subtree'
  19051. (if (numberp nstars) (1- nstars)
  19052. (or (org-current-level) 0))
  19053. ?*))
  19054. (add-stars
  19055. (cond (nstars "") ; stars from prefix only
  19056. ((equal stars "") "") ; before first heading
  19057. (org-odd-levels-only "*") ; inside heading, odd
  19058. (t "")))) ; inside heading, oddeven
  19059. (while (< (point) end)
  19060. (when (org-at-item-p)
  19061. ;; Pay attention to cases when region ends before list.
  19062. (let* ((struct (org-list-struct))
  19063. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19064. (save-restriction
  19065. (narrow-to-region (point) list-end)
  19066. (insert
  19067. (org-list-to-subtree
  19068. (org-list-parse-list t)
  19069. '(:istart (concat stars add-stars (funcall get-stars depth))
  19070. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19071. (setq toggled t))
  19072. (forward-line))))
  19073. ;; Case 3. Started at normal text: make every line an heading,
  19074. ;; skipping headlines and items.
  19075. (t (let* ((stars
  19076. (make-string
  19077. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19078. (add-stars
  19079. (cond (nstars "") ; stars from prefix only
  19080. ((equal stars "") "*") ; before first heading
  19081. (org-odd-levels-only "**") ; inside heading, odd
  19082. (t "*"))) ; inside heading, oddeven
  19083. (rpl (concat stars add-stars " "))
  19084. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19085. (while (< (point) (if (equal nstars '(4)) lend end))
  19086. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19087. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19088. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19089. (forward-line)))))))
  19090. (unless toggled (message "Cannot toggle heading from here"))))
  19091. (defun org-meta-return (&optional arg)
  19092. "Insert a new heading or wrap a region in a table.
  19093. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19094. on context. See the individual commands for more information."
  19095. (interactive "P")
  19096. (org-check-before-invisible-edit 'insert)
  19097. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19098. (let* ((element (org-element-at-point))
  19099. (type (org-element-type element)))
  19100. (when (eq type 'table-row)
  19101. (setq element (org-element-property :parent element))
  19102. (setq type 'table))
  19103. (if (and (eq type 'table)
  19104. (eq (org-element-property :type element) 'org)
  19105. (>= (point) (org-element-property :contents-begin element))
  19106. (< (point) (org-element-property :contents-end element)))
  19107. (call-interactively 'org-table-wrap-region)
  19108. (call-interactively 'org-insert-heading)))))
  19109. ;;; Menu entries
  19110. (defsubst org-in-subtree-not-table-p ()
  19111. "Are we in a subtree and not in a table?"
  19112. (and (not (org-before-first-heading-p))
  19113. (not (org-at-table-p))))
  19114. ;; Define the Org-mode menus
  19115. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19116. '("Tbl"
  19117. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19118. ["Next Field" org-cycle (org-at-table-p)]
  19119. ["Previous Field" org-shifttab (org-at-table-p)]
  19120. ["Next Row" org-return (org-at-table-p)]
  19121. "--"
  19122. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19123. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19124. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19125. "--"
  19126. ("Column"
  19127. ["Move Column Left" org-metaleft (org-at-table-p)]
  19128. ["Move Column Right" org-metaright (org-at-table-p)]
  19129. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19130. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19131. ("Row"
  19132. ["Move Row Up" org-metaup (org-at-table-p)]
  19133. ["Move Row Down" org-metadown (org-at-table-p)]
  19134. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19135. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19136. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19137. "--"
  19138. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19139. ("Rectangle"
  19140. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19141. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19142. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19143. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19144. "--"
  19145. ("Calculate"
  19146. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19147. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19148. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19149. "--"
  19150. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19151. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19152. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19153. "--"
  19154. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19155. "--"
  19156. ["Sum Column/Rectangle" org-table-sum
  19157. (or (org-at-table-p) (org-region-active-p))]
  19158. ["Which Column?" org-table-current-column (org-at-table-p)])
  19159. ["Debug Formulas"
  19160. org-table-toggle-formula-debugger
  19161. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19162. ["Show Col/Row Numbers"
  19163. org-table-toggle-coordinate-overlays
  19164. :style toggle
  19165. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19166. "--"
  19167. ["Create" org-table-create (and (not (org-at-table-p))
  19168. org-enable-table-editor)]
  19169. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19170. ["Import from File" org-table-import (not (org-at-table-p))]
  19171. ["Export to File" org-table-export (org-at-table-p)]
  19172. "--"
  19173. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19174. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19175. '("Org"
  19176. ("Show/Hide"
  19177. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19178. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19179. ["Sparse Tree..." org-sparse-tree t]
  19180. ["Reveal Context" org-reveal t]
  19181. ["Show All" show-all t]
  19182. "--"
  19183. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19184. "--"
  19185. ["New Heading" org-insert-heading t]
  19186. ("Navigate Headings"
  19187. ["Up" outline-up-heading t]
  19188. ["Next" outline-next-visible-heading t]
  19189. ["Previous" outline-previous-visible-heading t]
  19190. ["Next Same Level" outline-forward-same-level t]
  19191. ["Previous Same Level" outline-backward-same-level t]
  19192. "--"
  19193. ["Jump" org-goto t])
  19194. ("Edit Structure"
  19195. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19196. "--"
  19197. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19198. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19199. "--"
  19200. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19201. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19202. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19203. "--"
  19204. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19205. "--"
  19206. ["Copy visible text" org-copy-visible t]
  19207. "--"
  19208. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19209. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19210. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19211. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19212. "--"
  19213. ["Sort Region/Children" org-sort t]
  19214. "--"
  19215. ["Convert to odd levels" org-convert-to-odd-levels t]
  19216. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19217. ("Editing"
  19218. ["Emphasis..." org-emphasize t]
  19219. ["Edit Source Example" org-edit-special t]
  19220. "--"
  19221. ["Footnote new/jump" org-footnote-action t]
  19222. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19223. ("Archive"
  19224. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19225. "--"
  19226. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19227. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19228. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19229. )
  19230. "--"
  19231. ("Hyperlinks"
  19232. ["Store Link (Global)" org-store-link t]
  19233. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19234. ["Insert Link" org-insert-link t]
  19235. ["Follow Link" org-open-at-point t]
  19236. "--"
  19237. ["Next link" org-next-link t]
  19238. ["Previous link" org-previous-link t]
  19239. "--"
  19240. ["Descriptive Links"
  19241. org-toggle-link-display
  19242. :style radio
  19243. :selected org-descriptive-links
  19244. ]
  19245. ["Literal Links"
  19246. org-toggle-link-display
  19247. :style radio
  19248. :selected (not org-descriptive-links)])
  19249. "--"
  19250. ("TODO Lists"
  19251. ["TODO/DONE/-" org-todo t]
  19252. ("Select keyword"
  19253. ["Next keyword" org-shiftright (org-at-heading-p)]
  19254. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19255. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19256. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19257. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19258. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19259. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19260. "--"
  19261. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19262. :selected org-enforce-todo-dependencies :style toggle :active t]
  19263. "Settings for tree at point"
  19264. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19265. :selected (org-entry-get nil "ORDERED")
  19266. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19267. ["Do Children parallel" org-toggle-ordered-property :style radio
  19268. :selected (not (org-entry-get nil "ORDERED"))
  19269. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19270. "--"
  19271. ["Set Priority" org-priority t]
  19272. ["Priority Up" org-shiftup t]
  19273. ["Priority Down" org-shiftdown t]
  19274. "--"
  19275. ["Get news from all feeds" org-feed-update-all t]
  19276. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19277. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19278. ("TAGS and Properties"
  19279. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19280. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19281. "--"
  19282. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19283. ["Column view of properties" org-columns t]
  19284. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19285. ("Dates and Scheduling"
  19286. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19287. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19288. ("Change Date"
  19289. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19290. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19291. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19292. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19293. ["Compute Time Range" org-evaluate-time-range t]
  19294. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19295. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19296. "--"
  19297. ["Custom time format" org-toggle-time-stamp-overlays
  19298. :style radio :selected org-display-custom-times]
  19299. "--"
  19300. ["Goto Calendar" org-goto-calendar t]
  19301. ["Date from Calendar" org-date-from-calendar t]
  19302. "--"
  19303. ["Start/Restart Timer" org-timer-start t]
  19304. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19305. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19306. ["Insert Timer String" org-timer t]
  19307. ["Insert Timer Item" org-timer-item t])
  19308. ("Logging work"
  19309. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19310. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19311. ["Clock out" org-clock-out t]
  19312. ["Clock cancel" org-clock-cancel t]
  19313. "--"
  19314. ["Mark as default task" org-clock-mark-default-task t]
  19315. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19316. ["Goto running clock" org-clock-goto t]
  19317. "--"
  19318. ["Display times" org-clock-display t]
  19319. ["Create clock table" org-clock-report t]
  19320. "--"
  19321. ["Record DONE time"
  19322. (progn (setq org-log-done (not org-log-done))
  19323. (message "Switching to %s will %s record a timestamp"
  19324. (car org-done-keywords)
  19325. (if org-log-done "automatically" "not")))
  19326. :style toggle :selected org-log-done])
  19327. "--"
  19328. ["Agenda Command..." org-agenda t]
  19329. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19330. ("File List for Agenda")
  19331. ("Special views current file"
  19332. ["TODO Tree" org-show-todo-tree t]
  19333. ["Check Deadlines" org-check-deadlines t]
  19334. ["Timeline" org-timeline t]
  19335. ["Tags/Property tree" org-match-sparse-tree t])
  19336. "--"
  19337. ["Export/Publish..." org-export-dispatch t]
  19338. ("LaTeX"
  19339. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19340. :selected org-cdlatex-mode]
  19341. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19342. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19343. ["Modify math symbol" org-cdlatex-math-modify
  19344. (org-inside-LaTeX-fragment-p)]
  19345. ["Insert citation" org-reftex-citation t]
  19346. "--"
  19347. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19348. "--"
  19349. ("MobileOrg"
  19350. ["Push Files and Views" org-mobile-push t]
  19351. ["Get Captured and Flagged" org-mobile-pull t]
  19352. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19353. "--"
  19354. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19355. "--"
  19356. ("Documentation"
  19357. ["Show Version" org-version t]
  19358. ["Info Documentation" org-info t])
  19359. ("Customize"
  19360. ["Browse Org Group" org-customize t]
  19361. "--"
  19362. ["Expand This Menu" org-create-customize-menu
  19363. (fboundp 'customize-menu-create)])
  19364. ["Send bug report" org-submit-bug-report t]
  19365. "--"
  19366. ("Refresh/Reload"
  19367. ["Refresh setup current buffer" org-mode-restart t]
  19368. ["Reload Org (after update)" org-reload t]
  19369. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19370. ))
  19371. (defun org-info (&optional node)
  19372. "Read documentation for Org-mode in the info system.
  19373. With optional NODE, go directly to that node."
  19374. (interactive)
  19375. (info (format "(org)%s" (or node ""))))
  19376. ;;;###autoload
  19377. (defun org-submit-bug-report ()
  19378. "Submit a bug report on Org-mode via mail.
  19379. Don't hesitate to report any problems or inaccurate documentation.
  19380. If you don't have setup sending mail from (X)Emacs, please copy the
  19381. output buffer into your mail program, as it gives us important
  19382. information about your Org-mode version and configuration."
  19383. (interactive)
  19384. (require 'reporter)
  19385. (org-load-modules-maybe)
  19386. (org-require-autoloaded-modules)
  19387. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19388. (reporter-submit-bug-report
  19389. "emacs-orgmode@gnu.org"
  19390. (org-version nil 'full)
  19391. (let (list)
  19392. (save-window-excursion
  19393. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19394. (delete-other-windows)
  19395. (erase-buffer)
  19396. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19397. We would like to add your full Org-mode and Outline configuration to the
  19398. bug report. This greatly simplifies the work of the maintainer and
  19399. other experts on the mailing list.
  19400. HOWEVER, some variables you have customized may contain private
  19401. information. The names of customers, colleagues, or friends, might
  19402. appear in the form of file names, tags, todo states, or search strings.
  19403. If you answer yes to the prompt, you might want to check and remove
  19404. such private information before sending the email.")
  19405. (add-text-properties (point-min) (point-max) '(face org-warning))
  19406. (when (yes-or-no-p "Include your Org-mode configuration ")
  19407. (mapatoms
  19408. (lambda (v)
  19409. (and (boundp v)
  19410. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19411. (or (and (symbol-value v)
  19412. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19413. (and
  19414. (get v 'custom-type) (get v 'standard-value)
  19415. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19416. (push v list)))))
  19417. (kill-buffer (get-buffer "*Warn about privacy*"))
  19418. list))
  19419. nil nil
  19420. "Remember to cover the basics, that is, what you expected to happen and
  19421. what in fact did happen. You don't know how to make a good report? See
  19422. http://orgmode.org/manual/Feedback.html#Feedback
  19423. Your bug report will be posted to the Org-mode mailing list.
  19424. ------------------------------------------------------------------------")
  19425. (save-excursion
  19426. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19427. (replace-match "\\1Bug: \\3 [\\2]")))))
  19428. (defun org-install-agenda-files-menu ()
  19429. (let ((bl (buffer-list)))
  19430. (save-excursion
  19431. (while bl
  19432. (set-buffer (pop bl))
  19433. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19434. (when (derived-mode-p 'org-mode)
  19435. (easy-menu-change
  19436. '("Org") "File List for Agenda"
  19437. (append
  19438. (list
  19439. ["Edit File List" (org-edit-agenda-file-list) t]
  19440. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19441. ["Remove Current File from List" org-remove-file t]
  19442. ["Cycle through agenda files" org-cycle-agenda-files t]
  19443. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19444. "--")
  19445. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19446. ;;;; Documentation
  19447. (defun org-require-autoloaded-modules ()
  19448. (interactive)
  19449. (mapc 'require
  19450. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19451. org-table org-timer)))
  19452. ;;;###autoload
  19453. (defun org-reload (&optional uncompiled)
  19454. "Reload all org lisp files.
  19455. With prefix arg UNCOMPILED, load the uncompiled versions."
  19456. (interactive "P")
  19457. (require 'loadhist)
  19458. (let* ((org-dir (org-find-library-dir "org"))
  19459. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19460. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19461. (remove-re (mapconcat 'identity
  19462. (mapcar (lambda (f) (concat "^" f "$"))
  19463. (list (if (featurep 'xemacs)
  19464. "org-colview"
  19465. "org-colview-xemacs")
  19466. "org" "org-loaddefs" "org-version"))
  19467. "\\|"))
  19468. (feats (delete-dups
  19469. (mapcar 'file-name-sans-extension
  19470. (mapcar 'file-name-nondirectory
  19471. (delq nil
  19472. (mapcar 'feature-file
  19473. features))))))
  19474. (lfeat (append
  19475. (sort
  19476. (setq feats
  19477. (delq nil (mapcar
  19478. (lambda (f)
  19479. (if (and (string-match feature-re f)
  19480. (not (string-match remove-re f)))
  19481. f nil))
  19482. feats)))
  19483. 'string-lessp)
  19484. (list "org-version" "org")))
  19485. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19486. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19487. load-uncore load-misses)
  19488. (setq load-misses
  19489. (delq 't
  19490. (mapcar (lambda (f)
  19491. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19492. (and (string= org-dir contrib-dir)
  19493. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19494. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19495. (add-to-list 'load-uncore f 'append)
  19496. 't)
  19497. f))
  19498. lfeat)))
  19499. (if load-uncore
  19500. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19501. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19502. (if load-misses
  19503. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19504. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19505. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19506. ;;;###autoload
  19507. (defun org-customize ()
  19508. "Call the customize function with org as argument."
  19509. (interactive)
  19510. (org-load-modules-maybe)
  19511. (org-require-autoloaded-modules)
  19512. (customize-browse 'org))
  19513. (defun org-create-customize-menu ()
  19514. "Create a full customization menu for Org-mode, insert it into the menu."
  19515. (interactive)
  19516. (org-load-modules-maybe)
  19517. (org-require-autoloaded-modules)
  19518. (if (fboundp 'customize-menu-create)
  19519. (progn
  19520. (easy-menu-change
  19521. '("Org") "Customize"
  19522. `(["Browse Org group" org-customize t]
  19523. "--"
  19524. ,(customize-menu-create 'org)
  19525. ["Set" Custom-set t]
  19526. ["Save" Custom-save t]
  19527. ["Reset to Current" Custom-reset-current t]
  19528. ["Reset to Saved" Custom-reset-saved t]
  19529. ["Reset to Standard Settings" Custom-reset-standard t]))
  19530. (message "\"Org\"-menu now contains full customization menu"))
  19531. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19532. ;;;; Miscellaneous stuff
  19533. ;;; Generally useful functions
  19534. (defun org-get-at-bol (property)
  19535. "Get text property PROPERTY at beginning of line."
  19536. (get-text-property (point-at-bol) property))
  19537. (defun org-find-text-property-in-string (prop s)
  19538. "Return the first non-nil value of property PROP in string S."
  19539. (or (get-text-property 0 prop s)
  19540. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19541. prop s)))
  19542. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19543. "Display the given MESSAGE as a warning."
  19544. (if (fboundp 'display-warning)
  19545. (display-warning 'org message
  19546. (if (featurep 'xemacs) 'warning :warning))
  19547. (let ((buf (get-buffer-create "*Org warnings*")))
  19548. (with-current-buffer buf
  19549. (goto-char (point-max))
  19550. (insert "Warning (Org): " message)
  19551. (unless (bolp)
  19552. (newline)))
  19553. (display-buffer buf)
  19554. (sit-for 0))))
  19555. (defun org-eval (form)
  19556. "Eval FORM and return result."
  19557. (condition-case error
  19558. (eval form)
  19559. (error (format "%%![Error: %s]" error))))
  19560. (defun org-in-clocktable-p ()
  19561. "Check if the cursor is in a clocktable."
  19562. (let ((pos (point)) start)
  19563. (save-excursion
  19564. (end-of-line 1)
  19565. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19566. (setq start (match-beginning 0))
  19567. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19568. (>= (match-end 0) pos)
  19569. start))))
  19570. (defun org-in-commented-line ()
  19571. "Is point in a line starting with `#'?"
  19572. (equal (char-after (point-at-bol)) ?#))
  19573. (defun org-in-indented-comment-line ()
  19574. "Is point in a line starting with `#' after some white space?"
  19575. (save-excursion
  19576. (save-match-data
  19577. (goto-char (point-at-bol))
  19578. (looking-at "[ \t]*#"))))
  19579. (defun org-in-verbatim-emphasis ()
  19580. (save-match-data
  19581. (and (org-in-regexp org-emph-re 2)
  19582. (>= (point) (match-beginning 3))
  19583. (<= (point) (match-end 4))
  19584. (member (match-string 3) '("=" "~")))))
  19585. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19586. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19587. (if (and marker (marker-buffer marker)
  19588. (buffer-live-p (marker-buffer marker)))
  19589. (progn
  19590. (org-pop-to-buffer-same-window (marker-buffer marker))
  19591. (if (or (> marker (point-max)) (< marker (point-min)))
  19592. (widen))
  19593. (goto-char marker)
  19594. (org-show-context 'org-goto))
  19595. (if bookmark
  19596. (bookmark-jump bookmark)
  19597. (error "Cannot find location"))))
  19598. (defun org-quote-csv-field (s)
  19599. "Quote field for inclusion in CSV material."
  19600. (if (string-match "[\",]" s)
  19601. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19602. s))
  19603. (defun org-force-self-insert (N)
  19604. "Needed to enforce self-insert under remapping."
  19605. (interactive "p")
  19606. (self-insert-command N))
  19607. (defun org-string-width (s)
  19608. "Compute width of string, ignoring invisible characters.
  19609. This ignores character with invisibility property `org-link', and also
  19610. characters with property `org-cwidth', because these will become invisible
  19611. upon the next fontification round."
  19612. (let (b l)
  19613. (when (or (eq t buffer-invisibility-spec)
  19614. (assq 'org-link buffer-invisibility-spec))
  19615. (while (setq b (text-property-any 0 (length s)
  19616. 'invisible 'org-link s))
  19617. (setq s (concat (substring s 0 b)
  19618. (substring s (or (next-single-property-change
  19619. b 'invisible s) (length s)))))))
  19620. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19621. (setq s (concat (substring s 0 b)
  19622. (substring s (or (next-single-property-change
  19623. b 'org-cwidth s) (length s))))))
  19624. (setq l (string-width s) b -1)
  19625. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19626. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19627. l))
  19628. (defun org-shorten-string (s maxlength)
  19629. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19630. If the string is shorter or has length MAXLENGTH, just return the
  19631. original string. If it is longer, the functions finds a space in the
  19632. string, breaks this string off at that locations and adds three dots
  19633. as ellipsis. Including the ellipsis, the string will not be longer
  19634. than MAXLENGTH. If finding a good breaking point in the string does
  19635. not work, the string is just chopped off in the middle of a word
  19636. if necessary."
  19637. (if (<= (length s) maxlength)
  19638. s
  19639. (let* ((n (max (- maxlength 4) 1))
  19640. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19641. (if (string-match re s)
  19642. (concat (match-string 1 s) "...")
  19643. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19644. (defun org-get-indentation (&optional line)
  19645. "Get the indentation of the current line, interpreting tabs.
  19646. When LINE is given, assume it represents a line and compute its indentation."
  19647. (if line
  19648. (if (string-match "^ *" (org-remove-tabs line))
  19649. (match-end 0))
  19650. (save-excursion
  19651. (beginning-of-line 1)
  19652. (skip-chars-forward " \t")
  19653. (current-column))))
  19654. (defun org-get-string-indentation (s)
  19655. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19656. (let ((n -1) (i 0) (w tab-width) c)
  19657. (catch 'exit
  19658. (while (< (setq n (1+ n)) (length s))
  19659. (setq c (aref s n))
  19660. (cond ((= c ?\ ) (setq i (1+ i)))
  19661. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19662. (t (throw 'exit t)))))
  19663. i))
  19664. (defun org-remove-tabs (s &optional width)
  19665. "Replace tabulators in S with spaces.
  19666. Assumes that s is a single line, starting in column 0."
  19667. (setq width (or width tab-width))
  19668. (while (string-match "\t" s)
  19669. (setq s (replace-match
  19670. (make-string
  19671. (- (* width (/ (+ (match-beginning 0) width) width))
  19672. (match-beginning 0)) ?\ )
  19673. t t s)))
  19674. s)
  19675. (defun org-fix-indentation (line ind)
  19676. "Fix indentation in LINE.
  19677. IND is a cons cell with target and minimum indentation.
  19678. If the current indentation in LINE is smaller than the minimum,
  19679. leave it alone. If it is larger than ind, set it to the target."
  19680. (let* ((l (org-remove-tabs line))
  19681. (i (org-get-indentation l))
  19682. (i1 (car ind)) (i2 (cdr ind)))
  19683. (if (>= i i2) (setq l (substring line i2)))
  19684. (if (> i1 0)
  19685. (concat (make-string i1 ?\ ) l)
  19686. l)))
  19687. (defun org-remove-indentation (code &optional n)
  19688. "Remove the maximum common indentation from the lines in CODE.
  19689. N may optionally be the number of spaces to remove."
  19690. (with-temp-buffer
  19691. (insert code)
  19692. (org-do-remove-indentation n)
  19693. (buffer-string)))
  19694. (defun org-do-remove-indentation (&optional n)
  19695. "Remove the maximum common indentation from the buffer."
  19696. (untabify (point-min) (point-max))
  19697. (let ((min 10000) re)
  19698. (if n
  19699. (setq min n)
  19700. (goto-char (point-min))
  19701. (while (re-search-forward "^ *[^ \n]" nil t)
  19702. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19703. (unless (or (= min 0) (= min 10000))
  19704. (setq re (format "^ \\{%d\\}" min))
  19705. (goto-char (point-min))
  19706. (while (re-search-forward re nil t)
  19707. (replace-match "")
  19708. (end-of-line 1))
  19709. min)))
  19710. (defun org-fill-template (template alist)
  19711. "Find each %key of ALIST in TEMPLATE and replace it."
  19712. (let ((case-fold-search nil)
  19713. entry key value)
  19714. (setq alist (sort (copy-sequence alist)
  19715. (lambda (a b) (< (length (car a)) (length (car b))))))
  19716. (while (setq entry (pop alist))
  19717. (setq template
  19718. (replace-regexp-in-string
  19719. (concat "%" (regexp-quote (car entry)))
  19720. (or (cdr entry) "") template t t)))
  19721. template))
  19722. (defun org-base-buffer (buffer)
  19723. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19724. (if (not buffer)
  19725. buffer
  19726. (or (buffer-base-buffer buffer)
  19727. buffer)))
  19728. (defun org-trim (s)
  19729. "Remove whitespace at beginning and end of string."
  19730. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19731. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19732. s)
  19733. (defun org-wrap (string &optional width lines)
  19734. "Wrap string to either a number of lines, or a width in characters.
  19735. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19736. that costs. If there is a word longer than WIDTH, the text is actually
  19737. wrapped to the length of that word.
  19738. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19739. many lines, whatever width that takes.
  19740. The return value is a list of lines, without newlines at the end."
  19741. (let* ((words (org-split-string string "[ \t\n]+"))
  19742. (maxword (apply 'max (mapcar 'org-string-width words)))
  19743. w ll)
  19744. (cond (width
  19745. (org-do-wrap words (max maxword width)))
  19746. (lines
  19747. (setq w maxword)
  19748. (setq ll (org-do-wrap words maxword))
  19749. (if (<= (length ll) lines)
  19750. ll
  19751. (setq ll words)
  19752. (while (> (length ll) lines)
  19753. (setq w (1+ w))
  19754. (setq ll (org-do-wrap words w)))
  19755. ll))
  19756. (t (error "Cannot wrap this")))))
  19757. (defun org-do-wrap (words width)
  19758. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19759. (let (lines line)
  19760. (while words
  19761. (setq line (pop words))
  19762. (while (and words (< (+ (length line) (length (car words))) width))
  19763. (setq line (concat line " " (pop words))))
  19764. (setq lines (push line lines)))
  19765. (nreverse lines)))
  19766. (defun org-split-string (string &optional separators)
  19767. "Splits STRING into substrings at SEPARATORS.
  19768. No empty strings are returned if there are matches at the beginning
  19769. and end of string."
  19770. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19771. (start 0)
  19772. notfirst
  19773. (list nil))
  19774. (while (and (string-match rexp string
  19775. (if (and notfirst
  19776. (= start (match-beginning 0))
  19777. (< start (length string)))
  19778. (1+ start) start))
  19779. (< (match-beginning 0) (length string)))
  19780. (setq notfirst t)
  19781. (or (eq (match-beginning 0) 0)
  19782. (and (eq (match-beginning 0) (match-end 0))
  19783. (eq (match-beginning 0) start))
  19784. (setq list
  19785. (cons (substring string start (match-beginning 0))
  19786. list)))
  19787. (setq start (match-end 0)))
  19788. (or (eq start (length string))
  19789. (setq list
  19790. (cons (substring string start)
  19791. list)))
  19792. (nreverse list)))
  19793. (defun org-quote-vert (s)
  19794. "Replace \"|\" with \"\\vert\"."
  19795. (while (string-match "|" s)
  19796. (setq s (replace-match "\\vert" t t s)))
  19797. s)
  19798. (defun org-uuidgen-p (s)
  19799. "Is S an ID created by UUIDGEN?"
  19800. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19801. (defun org-in-src-block-p (&optional inside)
  19802. "Whether point is in a code source block.
  19803. When INSIDE is non-nil, don't consider we are within a src block
  19804. when point is at #+BEGIN_SRC or #+END_SRC."
  19805. (let ((case-fold-search t) ov)
  19806. (or (and (setq ov (overlays-at (point)))
  19807. (memq 'org-block-background
  19808. (overlay-properties (car ov))))
  19809. (and (not inside)
  19810. (save-match-data
  19811. (save-excursion
  19812. (beginning-of-line)
  19813. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19814. (defun org-context ()
  19815. "Return a list of contexts of the current cursor position.
  19816. If several contexts apply, all are returned.
  19817. Each context entry is a list with a symbol naming the context, and
  19818. two positions indicating start and end of the context. Possible
  19819. contexts are:
  19820. :headline anywhere in a headline
  19821. :headline-stars on the leading stars in a headline
  19822. :todo-keyword on a TODO keyword (including DONE) in a headline
  19823. :tags on the TAGS in a headline
  19824. :priority on the priority cookie in a headline
  19825. :item on the first line of a plain list item
  19826. :item-bullet on the bullet/number of a plain list item
  19827. :checkbox on the checkbox in a plain list item
  19828. :table in an org-mode table
  19829. :table-special on a special filed in a table
  19830. :table-table in a table.el table
  19831. :clocktable in a clocktable
  19832. :src-block in a source block
  19833. :link on a hyperlink
  19834. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19835. :target on a <<target>>
  19836. :radio-target on a <<<radio-target>>>
  19837. :latex-fragment on a LaTeX fragment
  19838. :latex-preview on a LaTeX fragment with overlaid preview image
  19839. This function expects the position to be visible because it uses font-lock
  19840. faces as a help to recognize the following contexts: :table-special, :link,
  19841. and :keyword."
  19842. (let* ((f (get-text-property (point) 'face))
  19843. (faces (if (listp f) f (list f)))
  19844. (case-fold-search t)
  19845. (p (point)) clist o)
  19846. ;; First the large context
  19847. (cond
  19848. ((org-at-heading-p t)
  19849. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19850. (when (progn
  19851. (beginning-of-line 1)
  19852. (looking-at org-todo-line-tags-regexp))
  19853. (push (org-point-in-group p 1 :headline-stars) clist)
  19854. (push (org-point-in-group p 2 :todo-keyword) clist)
  19855. (push (org-point-in-group p 4 :tags) clist))
  19856. (goto-char p)
  19857. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19858. (if (looking-at "\\[#[A-Z0-9]\\]")
  19859. (push (org-point-in-group p 0 :priority) clist)))
  19860. ((org-at-item-p)
  19861. (push (org-point-in-group p 2 :item-bullet) clist)
  19862. (push (list :item (point-at-bol)
  19863. (save-excursion (org-end-of-item) (point)))
  19864. clist)
  19865. (and (org-at-item-checkbox-p)
  19866. (push (org-point-in-group p 0 :checkbox) clist)))
  19867. ((org-at-table-p)
  19868. (push (list :table (org-table-begin) (org-table-end)) clist)
  19869. (if (memq 'org-formula faces)
  19870. (push (list :table-special
  19871. (previous-single-property-change p 'face)
  19872. (next-single-property-change p 'face)) clist)))
  19873. ((org-at-table-p 'any)
  19874. (push (list :table-table) clist)))
  19875. (goto-char p)
  19876. (let ((case-fold-search t))
  19877. ;; New the "medium" contexts: clocktables, source blocks
  19878. (cond ((org-in-clocktable-p)
  19879. (push (list :clocktable
  19880. (and (or (looking-at "#\\+BEGIN: clocktable")
  19881. (search-backward "#+BEGIN: clocktable" nil t))
  19882. (match-beginning 0))
  19883. (and (re-search-forward "#\\+END:?" nil t)
  19884. (match-end 0))) clist))
  19885. ((org-in-src-block-p)
  19886. (push (list :src-block
  19887. (and (or (looking-at "#\\+BEGIN_SRC")
  19888. (search-backward "#+BEGIN_SRC" nil t))
  19889. (match-beginning 0))
  19890. (and (search-forward "#+END_SRC" nil t)
  19891. (match-beginning 0))) clist))))
  19892. (goto-char p)
  19893. ;; Now the small context
  19894. (cond
  19895. ((org-at-timestamp-p)
  19896. (push (org-point-in-group p 0 :timestamp) clist))
  19897. ((memq 'org-link faces)
  19898. (push (list :link
  19899. (previous-single-property-change p 'face)
  19900. (next-single-property-change p 'face)) clist))
  19901. ((memq 'org-special-keyword faces)
  19902. (push (list :keyword
  19903. (previous-single-property-change p 'face)
  19904. (next-single-property-change p 'face)) clist))
  19905. ((org-at-target-p)
  19906. (push (org-point-in-group p 0 :target) clist)
  19907. (goto-char (1- (match-beginning 0)))
  19908. (if (looking-at org-radio-target-regexp)
  19909. (push (org-point-in-group p 0 :radio-target) clist))
  19910. (goto-char p))
  19911. ((setq o (car (delq nil
  19912. (mapcar
  19913. (lambda (x)
  19914. (if (memq x org-latex-fragment-image-overlays) x))
  19915. (overlays-at (point))))))
  19916. (push (list :latex-fragment
  19917. (overlay-start o) (overlay-end o)) clist)
  19918. (push (list :latex-preview
  19919. (overlay-start o) (overlay-end o)) clist))
  19920. ((org-inside-LaTeX-fragment-p)
  19921. ;; FIXME: positions wrong.
  19922. (push (list :latex-fragment (point) (point)) clist)))
  19923. (setq clist (nreverse (delq nil clist)))
  19924. clist))
  19925. ;; FIXME: Compare with at-regexp-p Do we need both?
  19926. (defun org-in-regexp (re &optional nlines visually)
  19927. "Check if point is inside a match of regexp.
  19928. Normally only the current line is checked, but you can include NLINES extra
  19929. lines both before and after point into the search.
  19930. If VISUALLY is set, require that the cursor is not after the match but
  19931. really on, so that the block visually is on the match."
  19932. (catch 'exit
  19933. (let ((pos (point))
  19934. (eol (point-at-eol (+ 1 (or nlines 0))))
  19935. (inc (if visually 1 0)))
  19936. (save-excursion
  19937. (beginning-of-line (- 1 (or nlines 0)))
  19938. (while (re-search-forward re eol t)
  19939. (if (and (<= (match-beginning 0) pos)
  19940. (>= (+ inc (match-end 0)) pos))
  19941. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19942. (defun org-at-regexp-p (regexp)
  19943. "Is point inside a match of REGEXP in the current line?"
  19944. (catch 'exit
  19945. (save-excursion
  19946. (let ((pos (point)) (end (point-at-eol)))
  19947. (beginning-of-line 1)
  19948. (while (re-search-forward regexp end t)
  19949. (if (and (<= (match-beginning 0) pos)
  19950. (>= (match-end 0) pos))
  19951. (throw 'exit t)))
  19952. nil))))
  19953. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19954. "Non-nil when point is between matches of START-RE and END-RE.
  19955. Also return a non-nil value when point is on one of the matches.
  19956. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19957. buffer positions. Default values are the positions of headlines
  19958. surrounding the point.
  19959. The functions returns a cons cell whose car (resp. cdr) is the
  19960. position before START-RE (resp. after END-RE)."
  19961. (save-match-data
  19962. (let ((pos (point))
  19963. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19964. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19965. beg end)
  19966. (save-excursion
  19967. ;; Point is on a block when on START-RE or if START-RE can be
  19968. ;; found before it...
  19969. (and (or (org-at-regexp-p start-re)
  19970. (re-search-backward start-re limit-up t))
  19971. (setq beg (match-beginning 0))
  19972. ;; ... and END-RE after it...
  19973. (goto-char (match-end 0))
  19974. (re-search-forward end-re limit-down t)
  19975. (> (setq end (match-end 0)) pos)
  19976. ;; ... without another START-RE in-between.
  19977. (goto-char (match-beginning 0))
  19978. (not (re-search-backward start-re (1+ beg) t))
  19979. ;; Return value.
  19980. (cons beg end))))))
  19981. (defun org-in-block-p (names)
  19982. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19983. NAMES is a list of strings containing names of blocks.
  19984. Return first block name matched, or nil. Beware that in case of
  19985. nested blocks, the returned name may not belong to the closest
  19986. block from point."
  19987. (save-match-data
  19988. (catch 'exit
  19989. (let ((case-fold-search t)
  19990. (lim-up (save-excursion (outline-previous-heading)))
  19991. (lim-down (save-excursion (outline-next-heading))))
  19992. (mapc (lambda (name)
  19993. (let ((n (regexp-quote name)))
  19994. (when (org-between-regexps-p
  19995. (concat "^[ \t]*#\\+begin_" n)
  19996. (concat "^[ \t]*#\\+end_" n)
  19997. lim-up lim-down)
  19998. (throw 'exit n))))
  19999. names))
  20000. nil)))
  20001. (defun org-in-drawer-p ()
  20002. "Is point within a drawer?"
  20003. (save-match-data
  20004. (let ((case-fold-search t)
  20005. (lim-up (save-excursion (outline-previous-heading)))
  20006. (lim-down (save-excursion (outline-next-heading))))
  20007. (org-between-regexps-p
  20008. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  20009. "^[ \t]*:end:.*$"
  20010. lim-up lim-down))))
  20011. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20012. "Call `multi-occur' with buffers for all agenda files."
  20013. (interactive "sOrg-files matching: \np")
  20014. (let* ((files (org-agenda-files))
  20015. (tnames (mapcar 'file-truename files))
  20016. (extra org-agenda-text-search-extra-files)
  20017. f)
  20018. (when (eq (car extra) 'agenda-archives)
  20019. (setq extra (cdr extra))
  20020. (setq files (org-add-archive-files files)))
  20021. (while (setq f (pop extra))
  20022. (unless (member (file-truename f) tnames)
  20023. (add-to-list 'files f 'append)
  20024. (add-to-list 'tnames (file-truename f) 'append)))
  20025. (multi-occur
  20026. (mapcar (lambda (x)
  20027. (with-current-buffer
  20028. (or (get-file-buffer x) (find-file-noselect x))
  20029. (widen)
  20030. (current-buffer)))
  20031. files)
  20032. regexp)))
  20033. (if (boundp 'occur-mode-find-occurrence-hook)
  20034. ;; Emacs 23
  20035. (add-hook 'occur-mode-find-occurrence-hook
  20036. (lambda ()
  20037. (when (derived-mode-p 'org-mode)
  20038. (org-reveal))))
  20039. ;; Emacs 22
  20040. (defadvice occur-mode-goto-occurrence
  20041. (after org-occur-reveal activate)
  20042. (and (derived-mode-p 'org-mode) (org-reveal)))
  20043. (defadvice occur-mode-goto-occurrence-other-window
  20044. (after org-occur-reveal activate)
  20045. (and (derived-mode-p 'org-mode) (org-reveal)))
  20046. (defadvice occur-mode-display-occurrence
  20047. (after org-occur-reveal activate)
  20048. (when (derived-mode-p 'org-mode)
  20049. (let ((pos (occur-mode-find-occurrence)))
  20050. (with-current-buffer (marker-buffer pos)
  20051. (save-excursion
  20052. (goto-char pos)
  20053. (org-reveal)))))))
  20054. (defun org-occur-link-in-agenda-files ()
  20055. "Create a link and search for it in the agendas.
  20056. The link is not stored in `org-stored-links', it is just created
  20057. for the search purpose."
  20058. (interactive)
  20059. (let ((link (condition-case nil
  20060. (org-store-link nil)
  20061. (error "Unable to create a link to here"))))
  20062. (org-occur-in-agenda-files (regexp-quote link))))
  20063. (defun org-reverse-string (string)
  20064. "Return the reverse of STRING."
  20065. (apply 'string (reverse (string-to-list string))))
  20066. (defsubst org-uniquify (list)
  20067. "Non-destructively remove duplicate elements from LIST."
  20068. (let ((res (copy-sequence list))) (delete-dups res)))
  20069. (defun org-uniquify-alist (alist)
  20070. "Merge elements of ALIST with the same key.
  20071. For example, in this alist:
  20072. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20073. => '((a 1 3) (b 2))
  20074. merge (a 1) and (a 3) into (a 1 3).
  20075. The function returns the new ALIST."
  20076. (let (rtn)
  20077. (mapc
  20078. (lambda (e)
  20079. (let (n)
  20080. (if (not (assoc (car e) rtn))
  20081. (push e rtn)
  20082. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20083. (setq rtn (assq-delete-all (car e) rtn))
  20084. (push n rtn))))
  20085. alist)
  20086. rtn))
  20087. (defun org-delete-all (elts list)
  20088. "Remove all elements in ELTS from LIST."
  20089. (while elts
  20090. (setq list (delete (pop elts) list)))
  20091. list)
  20092. (defun org-count (cl-item cl-seq)
  20093. "Count the number of occurrences of ITEM in SEQ.
  20094. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20095. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20096. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20097. (while (< cl-start cl-end)
  20098. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20099. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20100. (setq cl-start (1+ cl-start)))
  20101. cl-count))
  20102. (defun org-remove-if (predicate seq)
  20103. "Remove everything from SEQ that fulfills PREDICATE."
  20104. (let (res e)
  20105. (while seq
  20106. (setq e (pop seq))
  20107. (if (not (funcall predicate e)) (push e res)))
  20108. (nreverse res)))
  20109. (defun org-remove-if-not (predicate seq)
  20110. "Remove everything from SEQ that does not fulfill PREDICATE."
  20111. (let (res e)
  20112. (while seq
  20113. (setq e (pop seq))
  20114. (if (funcall predicate e) (push e res)))
  20115. (nreverse res)))
  20116. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20117. "Reduce two-argument FUNCTION across SEQ.
  20118. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20119. \":initial-value\" removed."
  20120. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20121. (cadr (memq :initial-value cl-keys)))
  20122. (cl-seq (pop cl-seq))
  20123. (t (funcall cl-func)))))
  20124. (while cl-seq
  20125. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20126. cl-accum))
  20127. (defun org-every (pred seq)
  20128. "Return true if PREDICATE is true of every element of SEQ.
  20129. Adapted from `every' in cl.el."
  20130. (catch 'org-every
  20131. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20132. t))
  20133. (defun org-some (pred seq)
  20134. "Return true if PREDICATE is true of any element of SEQ.
  20135. Adapted from `some' in cl.el."
  20136. (catch 'org-some
  20137. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20138. nil))
  20139. (defun org-back-over-empty-lines ()
  20140. "Move backwards over whitespace, to the beginning of the first empty line.
  20141. Returns the number of empty lines passed."
  20142. (let ((pos (point)))
  20143. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20144. (skip-chars-backward " \t\n\r")
  20145. (unless (eobp)
  20146. (forward-line -1)))
  20147. (beginning-of-line 2)
  20148. (goto-char (min (point) pos))
  20149. (count-lines (point) pos)))
  20150. (defun org-skip-whitespace ()
  20151. (skip-chars-forward " \t\n\r"))
  20152. (defun org-point-in-group (point group &optional context)
  20153. "Check if POINT is in match-group GROUP.
  20154. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20155. match. If the match group does not exist or point is not inside it,
  20156. return nil."
  20157. (and (match-beginning group)
  20158. (>= point (match-beginning group))
  20159. (<= point (match-end group))
  20160. (if context
  20161. (list context (match-beginning group) (match-end group))
  20162. t)))
  20163. (defun org-switch-to-buffer-other-window (&rest args)
  20164. "Switch to buffer in a second window on the current frame.
  20165. In particular, do not allow pop-up frames.
  20166. Returns the newly created buffer."
  20167. (org-no-popups
  20168. (apply 'switch-to-buffer-other-window args)))
  20169. (defun org-combine-plists (&rest plists)
  20170. "Create a single property list from all plists in PLISTS.
  20171. The process starts by copying the first list, and then setting properties
  20172. from the other lists. Settings in the last list are the most significant
  20173. ones and overrule settings in the other lists."
  20174. (let ((rtn (copy-sequence (pop plists)))
  20175. p v ls)
  20176. (while plists
  20177. (setq ls (pop plists))
  20178. (while ls
  20179. (setq p (pop ls) v (pop ls))
  20180. (setq rtn (plist-put rtn p v))))
  20181. rtn))
  20182. (defun org-replace-escapes (string table)
  20183. "Replace %-escapes in STRING with values in TABLE.
  20184. TABLE is an association list with keys like \"%a\" and string values.
  20185. The sequences in STRING may contain normal field width and padding information,
  20186. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20187. so values can contain further %-escapes if they are define later in TABLE."
  20188. (let ((tbl (copy-alist table))
  20189. (case-fold-search nil)
  20190. (pchg 0)
  20191. e re rpl)
  20192. (while (setq e (pop tbl))
  20193. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20194. (when (and (cdr e) (string-match re (cdr e)))
  20195. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20196. (safe "SREF"))
  20197. (add-text-properties 0 3 (list 'sref sref) safe)
  20198. (setcdr e (replace-match safe t t (cdr e)))))
  20199. (while (string-match re string)
  20200. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20201. (cdr e)))
  20202. (setq string (replace-match rpl t t string))))
  20203. (while (setq pchg (next-property-change pchg string))
  20204. (let ((sref (get-text-property pchg 'sref string)))
  20205. (when (and sref (string-match "SREF" string pchg))
  20206. (setq string (replace-match sref t t string)))))
  20207. string))
  20208. (defun org-sublist (list start end)
  20209. "Return a section of LIST, from START to END.
  20210. Counting starts at 1."
  20211. (let (rtn (c start))
  20212. (setq list (nthcdr (1- start) list))
  20213. (while (and list (<= c end))
  20214. (push (pop list) rtn)
  20215. (setq c (1+ c)))
  20216. (nreverse rtn)))
  20217. (defun org-find-base-buffer-visiting (file)
  20218. "Like `find-buffer-visiting' but always return the base buffer and
  20219. not an indirect buffer."
  20220. (let ((buf (or (get-file-buffer file)
  20221. (find-buffer-visiting file))))
  20222. (if buf
  20223. (or (buffer-base-buffer buf) buf)
  20224. nil)))
  20225. (defun org-image-file-name-regexp (&optional extensions)
  20226. "Return regexp matching the file names of images.
  20227. If EXTENSIONS is given, only match these."
  20228. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20229. (image-file-name-regexp)
  20230. (let ((image-file-name-extensions
  20231. (or extensions
  20232. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20233. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20234. (concat "\\."
  20235. (regexp-opt (nconc (mapcar 'upcase
  20236. image-file-name-extensions)
  20237. image-file-name-extensions)
  20238. t)
  20239. "\\'"))))
  20240. (defun org-file-image-p (file &optional extensions)
  20241. "Return non-nil if FILE is an image."
  20242. (save-match-data
  20243. (string-match (org-image-file-name-regexp extensions) file)))
  20244. (defun org-get-cursor-date (&optional with-time)
  20245. "Return the date at cursor in as a time.
  20246. This works in the calendar and in the agenda, anywhere else it just
  20247. returns the current time.
  20248. If WITH-TIME is non-nil, returns the time of the event at point (in
  20249. the agenda) or the current time of the day."
  20250. (let (date day defd tp tm hod mod)
  20251. (when with-time
  20252. (setq tp (get-text-property (point) 'time))
  20253. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20254. (setq hod (string-to-number (match-string 1 tp))
  20255. mod (string-to-number (match-string 2 tp))))
  20256. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20257. mod (nth 1 (decode-time (current-time))))))
  20258. (cond
  20259. ((eq major-mode 'calendar-mode)
  20260. (setq date (calendar-cursor-to-date)
  20261. defd (encode-time 0 (or mod 0) (or hod 0)
  20262. (nth 1 date) (nth 0 date) (nth 2 date))))
  20263. ((eq major-mode 'org-agenda-mode)
  20264. (setq day (get-text-property (point) 'day))
  20265. (if day
  20266. (setq date (calendar-gregorian-from-absolute day)
  20267. defd (encode-time 0 (or mod 0) (or hod 0)
  20268. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20269. (or defd (current-time))))
  20270. (defun org-mark-subtree (&optional up)
  20271. "Mark the current subtree.
  20272. This puts point at the start of the current subtree, and mark at
  20273. the end. If a numeric prefix UP is given, move up into the
  20274. hierarchy of headlines by UP levels before marking the subtree."
  20275. (interactive "P")
  20276. (org-with-limited-levels
  20277. (cond ((org-at-heading-p) (beginning-of-line))
  20278. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20279. (t (outline-previous-visible-heading 1))))
  20280. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20281. (if (org-called-interactively-p 'any)
  20282. (call-interactively 'org-mark-element)
  20283. (org-mark-element)))
  20284. ;;; Indentation
  20285. (defun org-indent-line ()
  20286. "Indent line depending on context."
  20287. (interactive)
  20288. (let* ((pos (point))
  20289. (itemp (org-at-item-p))
  20290. (case-fold-search t)
  20291. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20292. (inline-task-p (and (featurep 'org-inlinetask)
  20293. (org-inlinetask-in-task-p)))
  20294. (inline-re (and inline-task-p
  20295. (org-inlinetask-outline-regexp)))
  20296. column)
  20297. (if (and orgstruct-is-++ (eq pos (point)))
  20298. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20299. (indent-according-to-mode))
  20300. (beginning-of-line 1)
  20301. (cond
  20302. ;; Headings
  20303. ((looking-at org-outline-regexp) (setq column 0))
  20304. ;; Footnote definition
  20305. ((looking-at org-footnote-definition-re) (setq column 0))
  20306. ;; Literal examples
  20307. ((looking-at "[ \t]*:\\( \\|$\\)")
  20308. (setq column (org-get-indentation))) ; do nothing
  20309. ;; Lists
  20310. ((ignore-errors (goto-char (org-in-item-p)))
  20311. (setq column (if itemp
  20312. (org-get-indentation)
  20313. (org-list-item-body-column (point))))
  20314. (goto-char pos))
  20315. ;; Drawers
  20316. ((and (looking-at "[ \t]*:END:")
  20317. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20318. (save-excursion
  20319. (goto-char (1- (match-beginning 1)))
  20320. (setq column (current-column))))
  20321. ;; Special blocks
  20322. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20323. (save-excursion
  20324. (re-search-backward
  20325. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20326. (setq column (org-get-indentation (match-string 0))))
  20327. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20328. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20329. (save-excursion
  20330. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20331. (setq column
  20332. (cond ((equal (downcase (match-string 1)) "src")
  20333. ;; src blocks: let `org-edit-src-exit' handle them
  20334. (org-get-indentation))
  20335. ((equal (downcase (match-string 1)) "example")
  20336. (max (org-get-indentation)
  20337. (org-get-indentation (match-string 0))))
  20338. (t
  20339. (org-get-indentation (match-string 0))))))
  20340. ;; This line has nothing special, look at the previous relevant
  20341. ;; line to compute indentation
  20342. (t
  20343. (beginning-of-line 0)
  20344. (while (and (not (bobp))
  20345. (not (looking-at org-table-line-regexp))
  20346. (not (looking-at org-drawer-regexp))
  20347. ;; When point started in an inline task, do not move
  20348. ;; above task starting line.
  20349. (not (and inline-task-p (looking-at inline-re)))
  20350. ;; Skip drawers, blocks, empty lines, verbatim,
  20351. ;; comments, tables, footnotes definitions, lists,
  20352. ;; inline tasks.
  20353. (or (and (looking-at "[ \t]*:END:")
  20354. (re-search-backward org-drawer-regexp nil t))
  20355. (and (looking-at "[ \t]*#\\+end_")
  20356. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20357. (looking-at "[ \t]*[\n:#|]")
  20358. (looking-at org-footnote-definition-re)
  20359. (and (not inline-task-p)
  20360. (featurep 'org-inlinetask)
  20361. (org-inlinetask-in-task-p)
  20362. (or (org-inlinetask-goto-beginning) t))))
  20363. (beginning-of-line 0))
  20364. (cond
  20365. ;; There was a list item above.
  20366. ((ignore-errors (goto-char (org-in-item-p)))
  20367. (goto-char (org-list-get-top-point (org-list-struct)))
  20368. (setq column (org-get-indentation)))
  20369. ;; There was an heading above.
  20370. ((looking-at "\\*+[ \t]+")
  20371. (if (not org-adapt-indentation)
  20372. (setq column 0)
  20373. (goto-char (match-end 0))
  20374. (setq column (current-column))))
  20375. ;; A drawer had started and is unfinished
  20376. ((looking-at org-drawer-regexp)
  20377. (goto-char (1- (match-beginning 1)))
  20378. (setq column (current-column)))
  20379. ;; Else, nothing noticeable found: get indentation and go on.
  20380. (t (setq column (org-get-indentation))))))
  20381. ;; Now apply indentation and move cursor accordingly
  20382. (goto-char pos)
  20383. (if (<= (current-column) (current-indentation))
  20384. (org-indent-line-to column)
  20385. (save-excursion (org-indent-line-to column)))
  20386. ;; Special polishing for properties, see `org-property-format'
  20387. (setq column (current-column))
  20388. (beginning-of-line 1)
  20389. (if (looking-at org-property-re)
  20390. (replace-match (concat (match-string 4)
  20391. (format org-property-format
  20392. (match-string 1) (match-string 3)))
  20393. t t))
  20394. (org-move-to-column column))))
  20395. (defun org-indent-drawer ()
  20396. "Indent the drawer at point."
  20397. (interactive)
  20398. (let ((p (point))
  20399. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20400. (match-end 0)))
  20401. (folded
  20402. (save-excursion
  20403. (end-of-line)
  20404. (when (overlays-at (point))
  20405. (member 'invisible (overlay-properties
  20406. (car (overlays-at (point)))))))))
  20407. (when folded (org-cycle))
  20408. (indent-for-tab-command)
  20409. (while (and (move-beginning-of-line 2) (< (point) e))
  20410. (indent-for-tab-command))
  20411. (goto-char p)
  20412. (when folded (org-cycle)))
  20413. (message "Drawer at point indented"))
  20414. (defun org-indent-block ()
  20415. "Indent the block at point."
  20416. (interactive)
  20417. (let ((p (point))
  20418. (case-fold-search t)
  20419. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20420. (match-end 0)))
  20421. (folded
  20422. (save-excursion
  20423. (end-of-line)
  20424. (when (overlays-at (point))
  20425. (member 'invisible (overlay-properties
  20426. (car (overlays-at (point)))))))))
  20427. (when folded (org-cycle))
  20428. (indent-for-tab-command)
  20429. (while (and (move-beginning-of-line 2) (< (point) e))
  20430. (indent-for-tab-command))
  20431. (goto-char p)
  20432. (when folded (org-cycle)))
  20433. (message "Block at point indented"))
  20434. (defun org-indent-region (start end)
  20435. "Indent region."
  20436. (interactive "r")
  20437. (save-excursion
  20438. (let ((line-end (org-current-line end)))
  20439. (goto-char start)
  20440. (while (< (org-current-line) line-end)
  20441. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20442. (t (call-interactively 'org-indent-line)))
  20443. (move-beginning-of-line 2)))))
  20444. ;;; Filling
  20445. ;; We use our own fill-paragraph and auto-fill functions.
  20446. ;; `org-fill-paragraph' relies on adaptive filling and context
  20447. ;; checking. Appropriate `fill-prefix' is computed with
  20448. ;; `org-adaptive-fill-function'.
  20449. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20450. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20451. ;; `org-adaptive-fill-function' value. Internally,
  20452. ;; `org-comment-line-break-function' breaks the line.
  20453. ;; `org-setup-filling' installs filling and auto-filling related
  20454. ;; variables during `org-mode' initialization.
  20455. (defvar org-element-paragraph-separate) ; org-element.el
  20456. (defun org-setup-filling ()
  20457. (require 'org-element)
  20458. ;; Prevent auto-fill from inserting unwanted new items.
  20459. (when (boundp 'fill-nobreak-predicate)
  20460. (org-set-local
  20461. 'fill-nobreak-predicate
  20462. (org-uniquify
  20463. (append fill-nobreak-predicate
  20464. '(org-fill-line-break-nobreak-p
  20465. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20466. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20467. (org-set-local 'paragraph-start paragraph-ending)
  20468. (org-set-local 'paragraph-separate paragraph-ending))
  20469. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20470. (org-set-local 'auto-fill-inhibit-regexp nil)
  20471. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20472. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20473. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20474. (defun org-fill-line-break-nobreak-p ()
  20475. "Non-nil when a new line at point would create an Org line break."
  20476. (save-excursion
  20477. (skip-chars-backward "[ \t]")
  20478. (skip-chars-backward "\\\\")
  20479. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20480. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20481. "Non-nil when a new line at point would split a timestamp."
  20482. (and (org-at-timestamp-p t)
  20483. (not (looking-at org-ts-regexp-both))))
  20484. (declare-function message-in-body-p "message" ())
  20485. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20486. (defun org-adaptive-fill-function ()
  20487. "Compute a fill prefix for the current line.
  20488. Return fill prefix, as a string, or nil if current line isn't
  20489. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20490. matches in paragraphs or comments, use it."
  20491. (catch 'exit
  20492. (when (derived-mode-p 'message-mode)
  20493. (save-excursion
  20494. (beginning-of-line)
  20495. (cond ((or (not (message-in-body-p))
  20496. (looking-at orgtbl-line-start-regexp))
  20497. (throw 'exit nil))
  20498. ((looking-at message-cite-prefix-regexp)
  20499. (throw 'exit (match-string-no-properties 0)))
  20500. ((looking-at org-outline-regexp)
  20501. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20502. (org-with-wide-buffer
  20503. (let* ((p (line-beginning-position))
  20504. (element (save-excursion
  20505. (beginning-of-line)
  20506. (or (ignore-errors (org-element-at-point))
  20507. (user-error "An element cannot be parsed line %d"
  20508. (line-number-at-pos (point))))))
  20509. (type (org-element-type element))
  20510. (post-affiliated (org-element-property :post-affiliated element)))
  20511. (unless (and post-affiliated (< p post-affiliated))
  20512. (case type
  20513. (comment
  20514. (save-excursion
  20515. (beginning-of-line)
  20516. (looking-at "[ \t]*")
  20517. (concat (match-string 0) "# ")))
  20518. (footnote-definition "")
  20519. ((item plain-list)
  20520. (make-string (org-list-item-body-column
  20521. (or post-affiliated
  20522. (org-element-property :begin element)))
  20523. ? ))
  20524. (paragraph
  20525. ;; Fill prefix is usually the same as the current line,
  20526. ;; unless the paragraph is at the beginning of an item.
  20527. (let ((parent (org-element-property :parent element)))
  20528. (save-excursion
  20529. (beginning-of-line)
  20530. (cond ((eq (org-element-type parent) 'item)
  20531. (make-string (org-list-item-body-column
  20532. (org-element-property :begin parent))
  20533. ? ))
  20534. ((and adaptive-fill-regexp
  20535. ;; Locally disable
  20536. ;; `adaptive-fill-function' to let
  20537. ;; `fill-context-prefix' handle
  20538. ;; `adaptive-fill-regexp' variable.
  20539. (let (adaptive-fill-function)
  20540. (fill-context-prefix
  20541. post-affiliated
  20542. (org-element-property :end element)))))
  20543. ((looking-at "[ \t]+") (match-string 0))
  20544. (t "")))))
  20545. (comment-block
  20546. ;; Only fill contents if P is within block boundaries.
  20547. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20548. (forward-line)
  20549. (point)))
  20550. (cend (save-excursion
  20551. (goto-char (org-element-property :end element))
  20552. (skip-chars-backward " \r\t\n")
  20553. (line-beginning-position))))
  20554. (when (and (>= p cbeg) (< p cend))
  20555. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20556. (match-string 0)
  20557. ""))))))))))
  20558. (declare-function message-goto-body "message" ())
  20559. (defvar message-cite-prefix-regexp) ; From message.el
  20560. (defun org-fill-paragraph (&optional justify)
  20561. "Fill element at point, when applicable.
  20562. This function only applies to comment blocks, comments, example
  20563. blocks and paragraphs. Also, as a special case, re-align table
  20564. when point is at one.
  20565. If JUSTIFY is non-nil (interactively, with prefix argument),
  20566. justify as well. If `sentence-end-double-space' is non-nil, then
  20567. period followed by one space does not end a sentence, so don't
  20568. break a line there. The variable `fill-column' controls the
  20569. width for filling.
  20570. For convenience, when point is at a plain list, an item or
  20571. a footnote definition, try to fill the first paragraph within."
  20572. (interactive)
  20573. (if (and (derived-mode-p 'message-mode)
  20574. (or (not (message-in-body-p))
  20575. (save-excursion (move-beginning-of-line 1)
  20576. (looking-at message-cite-prefix-regexp))))
  20577. ;; First ensure filling is correct in message-mode.
  20578. (let ((fill-paragraph-function
  20579. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20580. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20581. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20582. (paragraph-separate
  20583. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20584. (fill-paragraph nil))
  20585. (with-syntax-table org-mode-transpose-word-syntax-table
  20586. ;; Move to end of line in order to get the first paragraph
  20587. ;; within a plain list or a footnote definition.
  20588. (let ((element (save-excursion
  20589. (end-of-line)
  20590. (or (ignore-errors (org-element-at-point))
  20591. (user-error "An element cannot be parsed line %d"
  20592. (line-number-at-pos (point)))))))
  20593. ;; First check if point is in a blank line at the beginning of
  20594. ;; the buffer. In that case, ignore filling.
  20595. (case (org-element-type element)
  20596. ;; Use major mode filling function is src blocks.
  20597. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20598. ;; Align Org tables, leave table.el tables as-is.
  20599. (table-row (org-table-align) t)
  20600. (table
  20601. (when (eq (org-element-property :type element) 'org)
  20602. (save-excursion
  20603. (goto-char (org-element-property :post-affiliated element))
  20604. (org-table-align)))
  20605. t)
  20606. (paragraph
  20607. ;; Paragraphs may contain `line-break' type objects.
  20608. (let ((beg (max (point-min)
  20609. (org-element-property :contents-begin element)))
  20610. (end (min (point-max)
  20611. (org-element-property :contents-end element))))
  20612. ;; Do nothing if point is at an affiliated keyword.
  20613. (if (< (line-end-position) beg) t
  20614. (when (derived-mode-p 'message-mode)
  20615. ;; In `message-mode', do not fill following citation
  20616. ;; in current paragraph nor text before message body.
  20617. (let ((body-start (save-excursion (message-goto-body))))
  20618. (when body-start (setq beg (max body-start beg))))
  20619. (when (save-excursion
  20620. (re-search-forward
  20621. (concat "^" message-cite-prefix-regexp) end t))
  20622. (setq end (match-beginning 0))))
  20623. ;; Fill paragraph, taking line breaks into account.
  20624. ;; For that, slice the paragraph using line breaks as
  20625. ;; separators, and fill the parts in reverse order to
  20626. ;; avoid messing with markers.
  20627. (save-excursion
  20628. (goto-char end)
  20629. (mapc
  20630. (lambda (pos)
  20631. (fill-region-as-paragraph pos (point) justify)
  20632. (goto-char pos))
  20633. ;; Find the list of ending positions for line breaks
  20634. ;; in the current paragraph. Add paragraph
  20635. ;; beginning to include first slice.
  20636. (nreverse
  20637. (cons beg
  20638. (org-element-map
  20639. (org-element--parse-objects
  20640. beg end nil (org-element-restriction 'paragraph))
  20641. 'line-break
  20642. (lambda (lb) (org-element-property :end lb)))))))
  20643. t)))
  20644. ;; Contents of `comment-block' type elements should be
  20645. ;; filled as plain text, but only if point is within block
  20646. ;; markers.
  20647. (comment-block
  20648. (let* ((case-fold-search t)
  20649. (beg (save-excursion
  20650. (goto-char (org-element-property :begin element))
  20651. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20652. (forward-line)
  20653. (point)))
  20654. (end (save-excursion
  20655. (goto-char (org-element-property :end element))
  20656. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20657. (line-beginning-position))))
  20658. (if (or (< (point) beg) (> (point) end)) t
  20659. (fill-region-as-paragraph
  20660. (save-excursion (end-of-line)
  20661. (re-search-backward "^[ \t]*$" beg 'move)
  20662. (line-beginning-position))
  20663. (save-excursion (beginning-of-line)
  20664. (re-search-forward "^[ \t]*$" end 'move)
  20665. (line-beginning-position))
  20666. justify))))
  20667. ;; Fill comments.
  20668. (comment
  20669. (let ((begin (org-element-property :post-affiliated element))
  20670. (end (org-element-property :end element)))
  20671. (when (and (>= (point) begin) (<= (point) end))
  20672. (let ((begin (save-excursion
  20673. (end-of-line)
  20674. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20675. (progn (forward-line) (point))
  20676. begin)))
  20677. (end (save-excursion
  20678. (end-of-line)
  20679. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20680. (1- (line-beginning-position))
  20681. (skip-chars-backward " \r\t\n")
  20682. (line-end-position)))))
  20683. ;; Do not fill comments when at a blank line.
  20684. (when (> end begin)
  20685. (let ((fill-prefix
  20686. (save-excursion
  20687. (beginning-of-line)
  20688. (looking-at "[ \t]*#")
  20689. (let ((comment-prefix (match-string 0)))
  20690. (goto-char (match-end 0))
  20691. (if (looking-at adaptive-fill-regexp)
  20692. (concat comment-prefix (match-string 0))
  20693. (concat comment-prefix " "))))))
  20694. (save-excursion
  20695. (fill-region-as-paragraph begin end justify))))))
  20696. t))
  20697. ;; Ignore every other element.
  20698. (otherwise t))))))
  20699. (defun org-auto-fill-function ()
  20700. "Auto-fill function."
  20701. ;; Check if auto-filling is meaningful.
  20702. (let ((fc (current-fill-column)))
  20703. (when (and fc (> (current-column) fc))
  20704. (let* ((fill-prefix (org-adaptive-fill-function))
  20705. ;; Enforce empty fill prefix, if required. Otherwise, it
  20706. ;; will be computed again.
  20707. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20708. (when fill-prefix (do-auto-fill))))))
  20709. (defun org-comment-line-break-function (&optional soft)
  20710. "Break line at point and indent, continuing comment if within one.
  20711. The inserted newline is marked hard if variable
  20712. `use-hard-newlines' is true, unless optional argument SOFT is
  20713. non-nil."
  20714. (if soft (insert-and-inherit ?\n) (newline 1))
  20715. (save-excursion (forward-char -1) (delete-horizontal-space))
  20716. (delete-horizontal-space)
  20717. (indent-to-left-margin)
  20718. (insert-before-markers-and-inherit fill-prefix))
  20719. ;;; Comments
  20720. ;; Org comments syntax is quite complex. It requires the entire line
  20721. ;; to be just a comment. Also, even with the right syntax at the
  20722. ;; beginning of line, some some elements (i.e. verse-block or
  20723. ;; example-block) don't accept comments. Usual Emacs comment commands
  20724. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20725. ;; Org still relies on `comment-dwim', but cannot trust
  20726. ;; `comment-only-p'. So, `comment-region-function' and
  20727. ;; `uncomment-region-function' both point
  20728. ;; to`org-comment-or-uncomment-region'. Eventually,
  20729. ;; `org-insert-comment' takes care of insertion of comments at the
  20730. ;; beginning of line.
  20731. ;; `org-setup-comments-handling' install comments related variables
  20732. ;; during `org-mode' initialization.
  20733. (defun org-setup-comments-handling ()
  20734. (interactive)
  20735. (org-set-local 'comment-use-syntax nil)
  20736. (org-set-local 'comment-start "# ")
  20737. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20738. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20739. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20740. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20741. (defun org-insert-comment ()
  20742. "Insert an empty comment above current line.
  20743. If the line is empty, insert comment at its beginning."
  20744. (beginning-of-line)
  20745. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20746. (org-indent-line)
  20747. (insert "# "))
  20748. (defvar comment-empty-lines) ; From newcomment.el.
  20749. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20750. "Comment or uncomment each non-blank line in the region.
  20751. Uncomment each non-blank line between BEG and END if it only
  20752. contains commented lines. Otherwise, comment them."
  20753. (save-restriction
  20754. ;; Restrict region
  20755. (narrow-to-region (save-excursion (goto-char beg)
  20756. (skip-chars-forward " \r\t\n" end)
  20757. (line-beginning-position))
  20758. (save-excursion (goto-char end)
  20759. (skip-chars-backward " \r\t\n" beg)
  20760. (line-end-position)))
  20761. (let ((uncommentp
  20762. ;; UNCOMMENTP is non-nil when every non blank line between
  20763. ;; BEG and END is a comment.
  20764. (save-excursion
  20765. (goto-char (point-min))
  20766. (while (and (not (eobp))
  20767. (let ((element (org-element-at-point)))
  20768. (and (eq (org-element-type element) 'comment)
  20769. (goto-char (min (point-max)
  20770. (org-element-property
  20771. :end element)))))))
  20772. (eobp))))
  20773. (if uncommentp
  20774. ;; Only blank lines and comments in region: uncomment it.
  20775. (save-excursion
  20776. (goto-char (point-min))
  20777. (while (not (eobp))
  20778. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20779. (replace-match "" nil nil nil 1))
  20780. (forward-line)))
  20781. ;; Comment each line in region.
  20782. (let ((min-indent (point-max)))
  20783. ;; First find the minimum indentation across all lines.
  20784. (save-excursion
  20785. (goto-char (point-min))
  20786. (while (and (not (eobp)) (not (zerop min-indent)))
  20787. (unless (looking-at "[ \t]*$")
  20788. (setq min-indent (min min-indent (current-indentation))))
  20789. (forward-line)))
  20790. ;; Then loop over all lines.
  20791. (save-excursion
  20792. (goto-char (point-min))
  20793. (while (not (eobp))
  20794. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20795. ;; Don't get fooled by invisible text (e.g. link path)
  20796. ;; when moving to column MIN-INDENT.
  20797. (let ((buffer-invisibility-spec nil))
  20798. (org-move-to-column min-indent t))
  20799. (insert comment-start))
  20800. (forward-line))))))))
  20801. ;;; Planning
  20802. ;; This section contains tools to operate on timestamp objects, as
  20803. ;; returned by, e.g. `org-element-context'.
  20804. (defun org-timestamp-has-time-p (timestamp)
  20805. "Non-nil when TIMESTAMP has a time specified."
  20806. (org-element-property :hour-start timestamp))
  20807. (defun org-timestamp-format (timestamp format &optional end utc)
  20808. "Format a TIMESTAMP element into a string.
  20809. FORMAT is a format specifier to be passed to
  20810. `format-time-string'.
  20811. When optional argument END is non-nil, use end of date-range or
  20812. time-range, if possible.
  20813. When optional argument UTC is non-nil, time will be expressed as
  20814. Universal Time."
  20815. (format-time-string
  20816. format
  20817. (apply 'encode-time
  20818. (cons 0
  20819. (mapcar
  20820. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20821. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20822. '(:minute-start :hour-start :day-start :month-start
  20823. :year-start)))))
  20824. utc))
  20825. (defun org-timestamp-split-range (timestamp &optional end)
  20826. "Extract a timestamp object from a date or time range.
  20827. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20828. the end of the range. Otherwise, extract its start.
  20829. Return a new timestamp object sharing the same parent as
  20830. TIMESTAMP."
  20831. (let ((type (org-element-property :type timestamp)))
  20832. (if (memq type '(active inactive diary)) timestamp
  20833. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20834. ;; Set new type.
  20835. (org-element-put-property
  20836. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20837. ;; Copy start properties over end properties if END is
  20838. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20839. (let ((p-alist '((:minute-start . :minute-end)
  20840. (:hour-start . :hour-end)
  20841. (:day-start . :day-end)
  20842. (:month-start . :month-end)
  20843. (:year-start . :year-end))))
  20844. (dolist (p-cell p-alist)
  20845. (org-element-put-property
  20846. split-ts
  20847. (funcall (if end 'car 'cdr) p-cell)
  20848. (org-element-property
  20849. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20850. ;; Eventually refresh `:raw-value'.
  20851. (org-element-put-property split-ts :raw-value nil)
  20852. (org-element-put-property
  20853. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20854. (defun org-timestamp-translate (timestamp &optional boundary)
  20855. "Apply `org-translate-time' on a TIMESTAMP object.
  20856. When optional argument BOUNDARY is non-nil, it is either the
  20857. symbol `start' or `end'. In this case, only translate the
  20858. starting or ending part of TIMESTAMP if it is a date or time
  20859. range. Otherwise, translate both parts."
  20860. (if (and (not boundary)
  20861. (memq (org-element-property :type timestamp)
  20862. '(active-range inactive-range)))
  20863. (concat
  20864. (org-translate-time
  20865. (org-element-property :raw-value
  20866. (org-timestamp-split-range timestamp)))
  20867. "--"
  20868. (org-translate-time
  20869. (org-element-property :raw-value
  20870. (org-timestamp-split-range timestamp t))))
  20871. (org-translate-time
  20872. (org-element-property
  20873. :raw-value
  20874. (if (not boundary) timestamp
  20875. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20876. ;;; Other stuff.
  20877. (defun org-toggle-fixed-width-section (arg)
  20878. "Toggle the fixed-width export.
  20879. If there is no active region, the QUOTE keyword at the current headline is
  20880. inserted or removed. When present, it causes the text between this headline
  20881. and the next to be exported as fixed-width text, and unmodified.
  20882. If there is an active region, this command adds or removes a colon as the
  20883. first character of this line. If the first character of a line is a colon,
  20884. this line is also exported in fixed-width font."
  20885. (interactive "P")
  20886. (let* ((cc 0)
  20887. (regionp (org-region-active-p))
  20888. (beg (if regionp (region-beginning) (point)))
  20889. (end (if regionp (region-end)))
  20890. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20891. (case-fold-search nil)
  20892. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20893. off)
  20894. (if regionp
  20895. (save-excursion
  20896. (goto-char beg)
  20897. (setq cc (current-column))
  20898. (beginning-of-line 1)
  20899. (setq off (looking-at re))
  20900. (while (> nlines 0)
  20901. (setq nlines (1- nlines))
  20902. (beginning-of-line 1)
  20903. (cond
  20904. (arg
  20905. (org-move-to-column cc t)
  20906. (insert ": \n")
  20907. (forward-line -1))
  20908. ((and off (looking-at re))
  20909. (replace-match "" t t nil 1))
  20910. ((not off) (org-move-to-column cc t) (insert ": ")))
  20911. (forward-line 1)))
  20912. (save-excursion
  20913. (org-back-to-heading)
  20914. (cond
  20915. ((looking-at (format org-heading-keyword-regexp-format
  20916. org-quote-string))
  20917. (goto-char (match-end 1))
  20918. (looking-at (concat " +" org-quote-string))
  20919. (replace-match "" t t)
  20920. (when (eolp) (insert " ")))
  20921. ((looking-at org-outline-regexp)
  20922. (goto-char (match-end 0))
  20923. (insert org-quote-string " ")))))))
  20924. (defun org-reftex-citation ()
  20925. "Use reftex-citation to insert a citation into the buffer.
  20926. This looks for a line like
  20927. #+BIBLIOGRAPHY: foo plain option:-d
  20928. and derives from it that foo.bib is the bibliography file relevant
  20929. for this document. It then installs the necessary environment for RefTeX
  20930. to work in this buffer and calls `reftex-citation' to insert a citation
  20931. into the buffer.
  20932. Export of such citations to both LaTeX and HTML is handled by the contributed
  20933. package ox-bibtex by Taru Karttunen."
  20934. (interactive)
  20935. (let ((reftex-docstruct-symbol 'rds)
  20936. (reftex-cite-format "\\cite{%l}")
  20937. rds bib)
  20938. (save-excursion
  20939. (save-restriction
  20940. (widen)
  20941. (let ((case-fold-search t)
  20942. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20943. (if (not (save-excursion
  20944. (or (re-search-forward re nil t)
  20945. (re-search-backward re nil t))))
  20946. (error "No bibliography defined in file")
  20947. (setq bib (concat (match-string 1) ".bib")
  20948. rds (list (list 'bib bib)))))))
  20949. (call-interactively 'reftex-citation)))
  20950. ;;;; Functions extending outline functionality
  20951. (defun org-beginning-of-line (&optional arg)
  20952. "Go to the beginning of the current line. If that is invisible, continue
  20953. to a visible line beginning. This makes the function of C-a more intuitive.
  20954. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20955. first attempt, and only move to after the tags when the cursor is already
  20956. beyond the end of the headline."
  20957. (interactive "P")
  20958. (let ((pos (point))
  20959. (special (if (consp org-special-ctrl-a/e)
  20960. (car org-special-ctrl-a/e)
  20961. org-special-ctrl-a/e))
  20962. deactivate-mark refpos)
  20963. (if (org-bound-and-true-p visual-line-mode)
  20964. (beginning-of-visual-line 1)
  20965. (beginning-of-line 1))
  20966. (if (and arg (fboundp 'move-beginning-of-line))
  20967. (call-interactively 'move-beginning-of-line)
  20968. (if (bobp)
  20969. nil
  20970. (backward-char 1)
  20971. (if (org-truely-invisible-p)
  20972. (while (and (not (bobp)) (org-truely-invisible-p))
  20973. (backward-char 1)
  20974. (beginning-of-line 1))
  20975. (forward-char 1))))
  20976. (when special
  20977. (cond
  20978. ((and (looking-at org-complex-heading-regexp)
  20979. (= (char-after (match-end 1)) ?\ ))
  20980. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20981. (point-at-eol)))
  20982. (goto-char
  20983. (if (eq special t)
  20984. (cond ((> pos refpos) refpos)
  20985. ((= pos (point)) refpos)
  20986. (t (point)))
  20987. (cond ((> pos (point)) (point))
  20988. ((not (eq last-command this-command)) (point))
  20989. (t refpos)))))
  20990. ((org-at-item-p)
  20991. ;; Being at an item and not looking at an the item means point
  20992. ;; was previously moved to beginning of a visual line, which
  20993. ;; doesn't contain the item. Therefore, do nothing special,
  20994. ;; just stay here.
  20995. (when (looking-at org-list-full-item-re)
  20996. ;; Set special position at first white space character after
  20997. ;; bullet, and check-box, if any.
  20998. (let ((after-bullet
  20999. (let ((box (match-end 3)))
  21000. (if (not box) (match-end 1)
  21001. (let ((after (char-after box)))
  21002. (if (and after (= after ? )) (1+ box) box))))))
  21003. ;; Special case: Move point to special position when
  21004. ;; currently after it or at beginning of line.
  21005. (if (eq special t)
  21006. (when (or (> pos after-bullet) (= (point) pos))
  21007. (goto-char after-bullet))
  21008. ;; Reversed case: Move point to special position when
  21009. ;; point was already at beginning of line and command is
  21010. ;; repeated.
  21011. (when (and (= (point) pos) (eq last-command this-command))
  21012. (goto-char after-bullet))))))))
  21013. (org-no-warnings
  21014. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21015. (setq disable-point-adjustment
  21016. (or (not (invisible-p (point)))
  21017. (not (invisible-p (max (point-min) (1- (point))))))))
  21018. (defun org-end-of-line (&optional arg)
  21019. "Go to the end of the line.
  21020. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21021. tags on the first attempt, and only move to after the tags when
  21022. the cursor is already beyond the end of the headline."
  21023. (interactive "P")
  21024. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21025. org-special-ctrl-a/e))
  21026. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21027. 'end-of-visual-line)
  21028. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21029. (t 'end-of-line)))
  21030. deactivate-mark)
  21031. (if (or (not special) arg) (call-interactively move-fun)
  21032. (let* ((element (save-excursion (beginning-of-line)
  21033. (org-element-at-point)))
  21034. (type (org-element-type element)))
  21035. (cond
  21036. ((memq type '(headline inlinetask))
  21037. (let ((pos (point)))
  21038. (beginning-of-line 1)
  21039. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21040. (if (eq special t)
  21041. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21042. (goto-char (match-beginning 1))
  21043. (goto-char (match-end 0)))
  21044. (if (or (< pos (match-end 0))
  21045. (not (eq this-command last-command)))
  21046. (goto-char (match-end 0))
  21047. (goto-char (match-beginning 1))))
  21048. (call-interactively move-fun))))
  21049. ((org-element-property :hiddenp element)
  21050. ;; If element is hidden, `move-end-of-line' would put point
  21051. ;; after it. Use `end-of-line' to stay on current line.
  21052. (call-interactively 'end-of-line))
  21053. (t (call-interactively move-fun)))))
  21054. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21055. (setq disable-point-adjustment
  21056. (or (not (invisible-p (point)))
  21057. (not (invisible-p (max (point-min) (1- (point))))))))
  21058. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21059. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21060. (defun org-backward-sentence (&optional arg)
  21061. "Go to beginning of sentence, or beginning of table field.
  21062. This will call `backward-sentence' or `org-table-beginning-of-field',
  21063. depending on context."
  21064. (interactive "P")
  21065. (cond
  21066. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21067. (t (call-interactively 'backward-sentence))))
  21068. (defun org-forward-sentence (&optional arg)
  21069. "Go to end of sentence, or end of table field.
  21070. This will call `forward-sentence' or `org-table-end-of-field',
  21071. depending on context."
  21072. (interactive "P")
  21073. (cond
  21074. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21075. (t (call-interactively 'forward-sentence))))
  21076. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21077. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21078. (defun org-kill-line (&optional arg)
  21079. "Kill line, to tags or end of line."
  21080. (interactive "P")
  21081. (cond
  21082. ((or (not org-special-ctrl-k)
  21083. (bolp)
  21084. (not (org-at-heading-p)))
  21085. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21086. org-ctrl-k-protect-subtree)
  21087. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21088. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21089. (user-error "C-k aborted as it would kill a hidden subtree")))
  21090. (call-interactively
  21091. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21092. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21093. (kill-region (point) (match-beginning 1))
  21094. (org-set-tags nil t))
  21095. (t (kill-region (point) (point-at-eol)))))
  21096. (define-key org-mode-map "\C-k" 'org-kill-line)
  21097. (defun org-yank (&optional arg)
  21098. "Yank. If the kill is a subtree, treat it specially.
  21099. This command will look at the current kill and check if is a single
  21100. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21101. cursor is at the beginning of a line or after the stars of a currently
  21102. empty headline, then the yank is handled specially. How exactly depends
  21103. on the value of the following variables, both set by default.
  21104. org-yank-folded-subtrees
  21105. When set, the subtree(s) will be folded after insertion, but only
  21106. if doing so would now swallow text after the yanked text.
  21107. org-yank-adjusted-subtrees
  21108. When set, the subtree will be promoted or demoted in order to
  21109. fit into the local outline tree structure, which means that the level
  21110. will be adjusted so that it becomes the smaller one of the two
  21111. *visible* surrounding headings.
  21112. Any prefix to this command will cause `yank' to be called directly with
  21113. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21114. will just
  21115. plainly yank the text as it is.
  21116. \[1] The test checks if the first non-white line is a heading
  21117. and if there are no other headings with fewer stars."
  21118. (interactive "P")
  21119. (org-yank-generic 'yank arg))
  21120. (defun org-yank-generic (command arg)
  21121. "Perform some yank-like command.
  21122. This function implements the behavior described in the `org-yank'
  21123. documentation. However, it has been generalized to work for any
  21124. interactive command with similar behavior."
  21125. ;; pretend to be command COMMAND
  21126. (setq this-command command)
  21127. (if arg
  21128. (call-interactively command)
  21129. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21130. (and (org-kill-is-subtree-p)
  21131. (or (bolp)
  21132. (and (looking-at "[ \t]*$")
  21133. (string-match
  21134. "\\`\\*+\\'"
  21135. (buffer-substring (point-at-bol) (point)))))))
  21136. swallowp)
  21137. (cond
  21138. ((and subtreep org-yank-folded-subtrees)
  21139. (let ((beg (point))
  21140. end)
  21141. (if (and subtreep org-yank-adjusted-subtrees)
  21142. (org-paste-subtree nil nil 'for-yank)
  21143. (call-interactively command))
  21144. (setq end (point))
  21145. (goto-char beg)
  21146. (when (and (bolp) subtreep
  21147. (not (setq swallowp
  21148. (org-yank-folding-would-swallow-text beg end))))
  21149. (org-with-limited-levels
  21150. (or (looking-at org-outline-regexp)
  21151. (re-search-forward org-outline-regexp-bol end t))
  21152. (while (and (< (point) end) (looking-at org-outline-regexp))
  21153. (hide-subtree)
  21154. (org-cycle-show-empty-lines 'folded)
  21155. (condition-case nil
  21156. (outline-forward-same-level 1)
  21157. (error (goto-char end))))))
  21158. (when swallowp
  21159. (message
  21160. "Inserted text not folded because that would swallow text"))
  21161. (goto-char end)
  21162. (skip-chars-forward " \t\n\r")
  21163. (beginning-of-line 1)
  21164. (push-mark beg 'nomsg)))
  21165. ((and subtreep org-yank-adjusted-subtrees)
  21166. (let ((beg (point-at-bol)))
  21167. (org-paste-subtree nil nil 'for-yank)
  21168. (push-mark beg 'nomsg)))
  21169. (t
  21170. (call-interactively command))))))
  21171. (defun org-yank-folding-would-swallow-text (beg end)
  21172. "Would hide-subtree at BEG swallow any text after END?"
  21173. (let (level)
  21174. (org-with-limited-levels
  21175. (save-excursion
  21176. (goto-char beg)
  21177. (when (or (looking-at org-outline-regexp)
  21178. (re-search-forward org-outline-regexp-bol end t))
  21179. (setq level (org-outline-level)))
  21180. (goto-char end)
  21181. (skip-chars-forward " \t\r\n\v\f")
  21182. (if (or (eobp)
  21183. (and (bolp) (looking-at org-outline-regexp)
  21184. (<= (org-outline-level) level)))
  21185. nil ; Nothing would be swallowed
  21186. t))))) ; something would swallow
  21187. (define-key org-mode-map "\C-y" 'org-yank)
  21188. (defun org-truely-invisible-p ()
  21189. "Check if point is at a character currently not visible.
  21190. This version does not only check the character property, but also
  21191. `visible-mode'."
  21192. ;; Early versions of noutline don't have `outline-invisible-p'.
  21193. (if (org-bound-and-true-p visible-mode)
  21194. nil
  21195. (outline-invisible-p)))
  21196. (defun org-invisible-p2 ()
  21197. "Check if point is at a character currently not visible."
  21198. (save-excursion
  21199. (if (and (eolp) (not (bobp))) (backward-char 1))
  21200. ;; Early versions of noutline don't have `outline-invisible-p'.
  21201. (outline-invisible-p)))
  21202. (defun org-back-to-heading (&optional invisible-ok)
  21203. "Call `outline-back-to-heading', but provide a better error message."
  21204. (condition-case nil
  21205. (outline-back-to-heading invisible-ok)
  21206. (error (error "Before first headline at position %d in buffer %s"
  21207. (point) (current-buffer)))))
  21208. (defun org-before-first-heading-p ()
  21209. "Before first heading?"
  21210. (save-excursion
  21211. (end-of-line)
  21212. (null (re-search-backward org-outline-regexp-bol nil t))))
  21213. (defun org-at-heading-p (&optional ignored)
  21214. (outline-on-heading-p t))
  21215. ;; Compatibility alias with Org versions < 7.8.03
  21216. (defalias 'org-on-heading-p 'org-at-heading-p)
  21217. (defun org-at-comment-p nil
  21218. "Is cursor in a line starting with a # character?"
  21219. (save-excursion
  21220. (beginning-of-line)
  21221. (looking-at "^#")))
  21222. (defun org-at-drawer-p nil
  21223. "Is cursor at a drawer keyword?"
  21224. (save-excursion
  21225. (move-beginning-of-line 1)
  21226. (looking-at org-drawer-regexp)))
  21227. (defun org-at-block-p nil
  21228. "Is cursor at a block keyword?"
  21229. (save-excursion
  21230. (move-beginning-of-line 1)
  21231. (looking-at org-block-regexp)))
  21232. (defun org-point-at-end-of-empty-headline ()
  21233. "If point is at the end of an empty headline, return t, else nil.
  21234. If the heading only contains a TODO keyword, it is still still considered
  21235. empty."
  21236. (and (looking-at "[ \t]*$")
  21237. (when org-todo-line-regexp
  21238. (save-excursion
  21239. (beginning-of-line 1)
  21240. (let ((case-fold-search nil))
  21241. (looking-at org-todo-line-regexp)
  21242. (string= (match-string 3) ""))))))
  21243. (defun org-at-heading-or-item-p ()
  21244. (or (org-at-heading-p) (org-at-item-p)))
  21245. (defun org-at-target-p ()
  21246. (or (org-in-regexp org-radio-target-regexp)
  21247. (org-in-regexp org-target-regexp)))
  21248. ;; Compatibility alias with Org versions < 7.8.03
  21249. (defalias 'org-on-target-p 'org-at-target-p)
  21250. (defun org-up-heading-all (arg)
  21251. "Move to the heading line of which the present line is a subheading.
  21252. This function considers both visible and invisible heading lines.
  21253. With argument, move up ARG levels."
  21254. (if (fboundp 'outline-up-heading-all)
  21255. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21256. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21257. (defun org-up-heading-safe ()
  21258. "Move to the heading line of which the present line is a subheading.
  21259. This version will not throw an error. It will return the level of the
  21260. headline found, or nil if no higher level is found.
  21261. Also, this function will be a lot faster than `outline-up-heading',
  21262. because it relies on stars being the outline starters. This can really
  21263. make a significant difference in outlines with very many siblings."
  21264. (let (start-level re)
  21265. (org-back-to-heading t)
  21266. (setq start-level (funcall outline-level))
  21267. (if (equal start-level 1)
  21268. nil
  21269. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21270. (if (re-search-backward re nil t)
  21271. (funcall outline-level)))))
  21272. (defun org-first-sibling-p ()
  21273. "Is this heading the first child of its parents?"
  21274. (interactive)
  21275. (let ((re org-outline-regexp-bol)
  21276. level l)
  21277. (unless (org-at-heading-p t)
  21278. (user-error "Not at a heading"))
  21279. (setq level (funcall outline-level))
  21280. (save-excursion
  21281. (if (not (re-search-backward re nil t))
  21282. t
  21283. (setq l (funcall outline-level))
  21284. (< l level)))))
  21285. (defun org-goto-sibling (&optional previous)
  21286. "Goto the next sibling, even if it is invisible.
  21287. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21288. when a sibling was found. When none is found, return nil and don't
  21289. move point."
  21290. (let ((fun (if previous 're-search-backward 're-search-forward))
  21291. (pos (point))
  21292. (re org-outline-regexp-bol)
  21293. level l)
  21294. (when (condition-case nil (org-back-to-heading t) (error nil))
  21295. (setq level (funcall outline-level))
  21296. (catch 'exit
  21297. (or previous (forward-char 1))
  21298. (while (funcall fun re nil t)
  21299. (setq l (funcall outline-level))
  21300. (when (< l level) (goto-char pos) (throw 'exit nil))
  21301. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21302. (goto-char pos)
  21303. nil))))
  21304. (defun org-show-siblings ()
  21305. "Show all siblings of the current headline."
  21306. (save-excursion
  21307. (while (org-goto-sibling) (org-flag-heading nil)))
  21308. (save-excursion
  21309. (while (org-goto-sibling 'previous)
  21310. (org-flag-heading nil))))
  21311. (defun org-goto-first-child ()
  21312. "Goto the first child, even if it is invisible.
  21313. Return t when a child was found. Otherwise don't move point and
  21314. return nil."
  21315. (let (level (pos (point)) (re org-outline-regexp-bol))
  21316. (when (condition-case nil (org-back-to-heading t) (error nil))
  21317. (setq level (outline-level))
  21318. (forward-char 1)
  21319. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21320. (progn (goto-char (match-beginning 0)) t)
  21321. (goto-char pos) nil))))
  21322. (defun org-show-hidden-entry ()
  21323. "Show an entry where even the heading is hidden."
  21324. (save-excursion
  21325. (org-show-entry)))
  21326. (defun org-flag-heading (flag &optional entry)
  21327. "Flag the current heading. FLAG non-nil means make invisible.
  21328. When ENTRY is non-nil, show the entire entry."
  21329. (save-excursion
  21330. (org-back-to-heading t)
  21331. ;; Check if we should show the entire entry
  21332. (if entry
  21333. (progn
  21334. (org-show-entry)
  21335. (save-excursion
  21336. (and (outline-next-heading)
  21337. (org-flag-heading nil))))
  21338. (outline-flag-region (max (point-min) (1- (point)))
  21339. (save-excursion (outline-end-of-heading) (point))
  21340. flag))))
  21341. (defun org-get-next-sibling ()
  21342. "Move to next heading of the same level, and return point.
  21343. If there is no such heading, return nil.
  21344. This is like outline-next-sibling, but invisible headings are ok."
  21345. (let ((level (funcall outline-level)))
  21346. (outline-next-heading)
  21347. (while (and (not (eobp)) (> (funcall outline-level) level))
  21348. (outline-next-heading))
  21349. (if (or (eobp) (< (funcall outline-level) level))
  21350. nil
  21351. (point))))
  21352. (defun org-get-last-sibling ()
  21353. "Move to previous heading of the same level, and return point.
  21354. If there is no such heading, return nil."
  21355. (let ((opoint (point))
  21356. (level (funcall outline-level)))
  21357. (outline-previous-heading)
  21358. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21359. (while (and (> (funcall outline-level) level)
  21360. (not (bobp)))
  21361. (outline-previous-heading))
  21362. (if (< (funcall outline-level) level)
  21363. nil
  21364. (point)))))
  21365. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21366. "Goto to the end of a subtree."
  21367. ;; This contains an exact copy of the original function, but it uses
  21368. ;; `org-back-to-heading', to make it work also in invisible
  21369. ;; trees. And is uses an invisible-ok argument.
  21370. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21371. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21372. ;; with many children and grandchildren etc, this can be much faster
  21373. ;; than the outline version.
  21374. (org-back-to-heading invisible-ok)
  21375. (let ((first t)
  21376. (level (funcall outline-level)))
  21377. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21378. ;; A true heading (not a plain list item), in Org-mode
  21379. ;; This means we can easily find the end by looking
  21380. ;; only for the right number of stars. Using a regexp to do
  21381. ;; this is so much faster than using a Lisp loop.
  21382. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21383. (forward-char 1)
  21384. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21385. ;; something else, do it the slow way
  21386. (while (and (not (eobp))
  21387. (or first (> (funcall outline-level) level)))
  21388. (setq first nil)
  21389. (outline-next-heading)))
  21390. (unless to-heading
  21391. (if (memq (preceding-char) '(?\n ?\^M))
  21392. (progn
  21393. ;; Go to end of line before heading
  21394. (forward-char -1)
  21395. (if (memq (preceding-char) '(?\n ?\^M))
  21396. ;; leave blank line before heading
  21397. (forward-char -1))))))
  21398. (point))
  21399. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21400. "Use Org version in org-mode, for dramatic speed-up."
  21401. (if (derived-mode-p 'org-mode)
  21402. (progn
  21403. (org-end-of-subtree nil t)
  21404. (unless (eobp) (backward-char 1)))
  21405. ad-do-it))
  21406. (defun org-end-of-meta-data-and-drawers ()
  21407. "Jump to the first text after meta data and drawers in the current entry.
  21408. This will move over empty lines, lines with planning time stamps,
  21409. clocking lines, and drawers."
  21410. (org-back-to-heading t)
  21411. (let ((end (save-excursion (outline-next-heading) (point)))
  21412. (re (concat "\\(" org-drawer-regexp "\\)"
  21413. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21414. (forward-line 1)
  21415. (while (re-search-forward re end t)
  21416. (if (not (match-end 1))
  21417. ;; empty or planning line
  21418. (forward-line 1)
  21419. ;; a drawer, find the end
  21420. (re-search-forward "^[ \t]*:END:" end 'move)
  21421. (forward-line 1)))
  21422. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21423. (point)))
  21424. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21425. "Move forward to the ARG'th subheading at same level as this one.
  21426. Stop at the first and last subheadings of a superior heading.
  21427. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21428. non-nil it will also look at invisible ones."
  21429. (interactive "p")
  21430. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21431. (if (and arg (< arg 0))
  21432. (goto-char (point-min))
  21433. (outline-next-heading))
  21434. (org-at-heading-p)
  21435. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21436. (f (if (and arg (< arg 0))
  21437. 're-search-backward
  21438. 're-search-forward))
  21439. (count (if arg (abs arg) 1))
  21440. (result (point)))
  21441. (while (and (prog1 (> count 0)
  21442. (forward-char (if (and arg (< arg 0)) -1 1)))
  21443. (funcall f org-outline-regexp-bol nil 'move))
  21444. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21445. (cond ((< l level) (setq count 0))
  21446. ((and (= l level)
  21447. (or invisible-ok
  21448. (progn
  21449. (goto-char (line-beginning-position))
  21450. (not (outline-invisible-p)))))
  21451. (setq count (1- count))
  21452. (when (eq l level)
  21453. (setq result (point)))))))
  21454. (goto-char result))
  21455. (beginning-of-line 1)))
  21456. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21457. "Move backward to the ARG'th subheading at same level as this one.
  21458. Stop at the first and last subheadings of a superior heading."
  21459. (interactive "p")
  21460. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21461. (defun org-next-block (arg &optional backward block-regexp)
  21462. "Jump to the next block.
  21463. With a prefix argument ARG, jump forward ARG many source blocks.
  21464. When BACKWARD is non-nil, jump to the previous block.
  21465. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21466. (interactive "p")
  21467. (let ((re (or block-regexp org-block-regexp))
  21468. (re-search-fn (or (and backward 're-search-backward)
  21469. 're-search-forward)))
  21470. (if (looking-at re) (forward-char 1))
  21471. (condition-case nil
  21472. (funcall re-search-fn re nil nil arg)
  21473. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21474. (goto-char (match-beginning 0)) (org-show-context)))
  21475. (defun org-previous-block (arg &optional block-regexp)
  21476. "Jump to the previous block.
  21477. With a prefix argument ARG, jump backward ARG many source blocks.
  21478. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21479. (interactive "p")
  21480. (org-next-block arg t block-regexp))
  21481. (defun org-forward-paragraph ()
  21482. "Move forward to beginning of next paragraph or equivalent.
  21483. The function moves point to the beginning of the next visible
  21484. structural element, which can be a paragraph, a table, a list
  21485. item, etc. It also provides some special moves for convenience:
  21486. - On an affiliated keyword, jump to the beginning of the
  21487. relative element.
  21488. - On an item or a footnote definition, move to the second
  21489. element inside, if any.
  21490. - On a table or a property drawer, jump after it.
  21491. - On a verse or source block, stop after blank lines."
  21492. (interactive)
  21493. (when (eobp) (user-error "Cannot move further down"))
  21494. (let* ((deactivate-mark nil)
  21495. (element (org-element-at-point))
  21496. (type (org-element-type element))
  21497. (post-affiliated (org-element-property :post-affiliated element))
  21498. (contents-begin (org-element-property :contents-begin element))
  21499. (contents-end (org-element-property :contents-end element))
  21500. (end (let ((end (org-element-property :end element)) (parent element))
  21501. (while (and (setq parent (org-element-property :parent parent))
  21502. (= (org-element-property :contents-end parent) end))
  21503. (setq end (org-element-property :end parent)))
  21504. end)))
  21505. (cond ((not element)
  21506. (skip-chars-forward " \r\t\n")
  21507. (or (eobp) (beginning-of-line)))
  21508. ;; On affiliated keywords, move to element's beginning.
  21509. ((and post-affiliated (< (point) post-affiliated))
  21510. (goto-char post-affiliated))
  21511. ;; At a table row, move to the end of the table. Similarly,
  21512. ;; at a node property, move to the end of the property
  21513. ;; drawer.
  21514. ((memq type '(node-property table-row))
  21515. (goto-char (org-element-property
  21516. :end (org-element-property :parent element))))
  21517. ((memq type '(property-drawer table)) (goto-char end))
  21518. ;; Consider blank lines as separators in verse and source
  21519. ;; blocks to ease editing.
  21520. ((memq type '(src-block verse-block))
  21521. (when (eq type 'src-block)
  21522. (setq contents-end
  21523. (save-excursion (goto-char end)
  21524. (skip-chars-backward " \r\t\n")
  21525. (line-beginning-position))))
  21526. (beginning-of-line)
  21527. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21528. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21529. (goto-char end)
  21530. (skip-chars-forward " \r\t\n")
  21531. (if (= (point) contents-end) (goto-char end)
  21532. (beginning-of-line))))
  21533. ;; With no contents, just skip element.
  21534. ((not contents-begin) (goto-char end))
  21535. ;; If contents are invisible, skip the element altogether.
  21536. ((outline-invisible-p (line-end-position))
  21537. (case type
  21538. (headline
  21539. (org-with-limited-levels (outline-next-visible-heading 1)))
  21540. ;; At a plain list, make sure we move to the next item
  21541. ;; instead of skipping the whole list.
  21542. (plain-list (forward-char)
  21543. (org-forward-paragraph))
  21544. (otherwise (goto-char end))))
  21545. ((>= (point) contents-end) (goto-char end))
  21546. ((>= (point) contents-begin)
  21547. ;; This can only happen on paragraphs and plain lists.
  21548. (case type
  21549. (paragraph (goto-char end))
  21550. ;; At a plain list, try to move to second element in
  21551. ;; first item, if possible.
  21552. (plain-list (end-of-line)
  21553. (org-forward-paragraph))))
  21554. ;; When contents start on the middle of a line (e.g. in
  21555. ;; items and footnote definitions), try to reach first
  21556. ;; element starting after current line.
  21557. ((> (line-end-position) contents-begin)
  21558. (end-of-line)
  21559. (org-forward-paragraph))
  21560. (t (goto-char contents-begin)))))
  21561. (defun org-backward-paragraph ()
  21562. "Move backward to start of previous paragraph or equivalent.
  21563. The function moves point to the beginning of the current
  21564. structural element, which can be a paragraph, a table, a list
  21565. item, etc., or to the beginning of the previous visible one if
  21566. point is already there. It also provides some special moves for
  21567. convenience:
  21568. - On an affiliated keyword, jump to the first one.
  21569. - On a table or a property drawer, move to its beginning.
  21570. - On a verse or source block, stop before blank lines."
  21571. (interactive)
  21572. (when (bobp) (user-error "Cannot move further up"))
  21573. (let* ((deactivate-mark nil)
  21574. (element (org-element-at-point))
  21575. (type (org-element-type element))
  21576. (contents-begin (org-element-property :contents-begin element))
  21577. (contents-end (org-element-property :contents-end element))
  21578. (post-affiliated (org-element-property :post-affiliated element))
  21579. (begin (org-element-property :begin element)))
  21580. (cond
  21581. ((not element) (goto-char (point-min)))
  21582. ((= (point) begin)
  21583. (backward-char)
  21584. (org-backward-paragraph))
  21585. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21586. ((memq type '(node-property table-row))
  21587. (goto-char (org-element-property
  21588. :post-affiliated (org-element-property :parent element))))
  21589. ((memq type '(property-drawer table)) (goto-char begin))
  21590. ((memq type '(src-block verse-block))
  21591. (when (eq type 'src-block)
  21592. (setq contents-begin
  21593. (save-excursion (goto-char begin) (forward-line) (point))))
  21594. (if (= (point) contents-begin) (goto-char post-affiliated)
  21595. ;; Inside a verse block, see blank lines as paragraph
  21596. ;; separators.
  21597. (let ((origin (point)))
  21598. (skip-chars-backward " \r\t\n" contents-begin)
  21599. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21600. (skip-chars-forward " \r\t\n" origin)
  21601. (if (= (point) origin) (goto-char contents-begin)
  21602. (beginning-of-line))))))
  21603. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21604. ((eq type 'paragraph)
  21605. (goto-char contents-begin)
  21606. ;; When at first paragraph in an item or a footnote definition,
  21607. ;; move directly to beginning of line.
  21608. (let ((parent-contents
  21609. (org-element-property
  21610. :contents-begin (org-element-property :parent element))))
  21611. (when (and parent-contents (= parent-contents contents-begin))
  21612. (beginning-of-line))))
  21613. ;; At the end of a greater element, move to the beginning of the
  21614. ;; last element within.
  21615. ((>= (point) contents-end)
  21616. (goto-char (1- contents-end))
  21617. (org-backward-paragraph))
  21618. (t (goto-char (or post-affiliated begin))))
  21619. ;; Ensure we never leave point invisible.
  21620. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21621. (defun org-forward-element ()
  21622. "Move forward by one element.
  21623. Move to the next element at the same level, when possible."
  21624. (interactive)
  21625. (cond ((eobp) (user-error "Cannot move further down"))
  21626. ((org-with-limited-levels (org-at-heading-p))
  21627. (let ((origin (point)))
  21628. (goto-char (org-end-of-subtree nil t))
  21629. (unless (org-with-limited-levels (org-at-heading-p))
  21630. (goto-char origin)
  21631. (user-error "Cannot move further down"))))
  21632. (t
  21633. (let* ((elem (org-element-at-point))
  21634. (end (org-element-property :end elem))
  21635. (parent (org-element-property :parent elem)))
  21636. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21637. (goto-char (org-element-property :end parent)))
  21638. ((integer-or-marker-p end) (goto-char end))
  21639. (t (message "No element at point")))))))
  21640. (defun org-backward-element ()
  21641. "Move backward by one element.
  21642. Move to the previous element at the same level, when possible."
  21643. (interactive)
  21644. (cond ((bobp) (user-error "Cannot move further up"))
  21645. ((org-with-limited-levels (org-at-heading-p))
  21646. ;; At a headline, move to the previous one, if any, or stay
  21647. ;; here.
  21648. (let ((origin (point)))
  21649. (org-with-limited-levels (org-backward-heading-same-level 1))
  21650. ;; When current headline has no sibling above, move to its
  21651. ;; parent.
  21652. (when (= (point) origin)
  21653. (or (org-with-limited-levels (org-up-heading-safe))
  21654. (progn (goto-char origin)
  21655. (user-error "Cannot move further up"))))))
  21656. (t
  21657. (let* ((trail (org-element-at-point 'keep-trail))
  21658. (elem (car trail))
  21659. (prev-elem (nth 1 trail))
  21660. (beg (org-element-property :begin elem)))
  21661. (cond
  21662. ;; Move to beginning of current element if point isn't
  21663. ;; there already.
  21664. ((null beg) (message "No element at point"))
  21665. ((/= (point) beg) (goto-char beg))
  21666. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21667. ((org-before-first-heading-p) (goto-char (point-min)))
  21668. (t (org-back-to-heading)))))))
  21669. (defun org-up-element ()
  21670. "Move to upper element."
  21671. (interactive)
  21672. (if (org-with-limited-levels (org-at-heading-p))
  21673. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21674. (let* ((elem (org-element-at-point))
  21675. (parent (org-element-property :parent elem)))
  21676. (if parent (goto-char (org-element-property :begin parent))
  21677. (if (org-with-limited-levels (org-before-first-heading-p))
  21678. (user-error "No surrounding element")
  21679. (org-with-limited-levels (org-back-to-heading)))))))
  21680. (defvar org-element-greater-elements)
  21681. (defun org-down-element ()
  21682. "Move to inner element."
  21683. (interactive)
  21684. (let ((element (org-element-at-point)))
  21685. (cond
  21686. ((memq (org-element-type element) '(plain-list table))
  21687. (goto-char (org-element-property :contents-begin element))
  21688. (forward-char))
  21689. ((memq (org-element-type element) org-element-greater-elements)
  21690. ;; If contents are hidden, first disclose them.
  21691. (when (org-element-property :hiddenp element) (org-cycle))
  21692. (goto-char (or (org-element-property :contents-begin element)
  21693. (user-error "No content for this element"))))
  21694. (t (user-error "No inner element")))))
  21695. (defun org-drag-element-backward ()
  21696. "Move backward element at point."
  21697. (interactive)
  21698. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21699. (let* ((trail (org-element-at-point 'keep-trail))
  21700. (elem (car trail))
  21701. (prev-elem (nth 1 trail)))
  21702. ;; Error out if no previous element or previous element is
  21703. ;; a parent of the current one.
  21704. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21705. (user-error "Cannot drag element backward")
  21706. (let ((pos (point)))
  21707. (org-element-swap-A-B prev-elem elem)
  21708. (goto-char (+ (org-element-property :begin prev-elem)
  21709. (- pos (org-element-property :begin elem)))))))))
  21710. (defun org-drag-element-forward ()
  21711. "Move forward element at point."
  21712. (interactive)
  21713. (let* ((pos (point))
  21714. (elem (org-element-at-point)))
  21715. (when (= (point-max) (org-element-property :end elem))
  21716. (user-error "Cannot drag element forward"))
  21717. (goto-char (org-element-property :end elem))
  21718. (let ((next-elem (org-element-at-point)))
  21719. (when (or (org-element-nested-p elem next-elem)
  21720. (and (eq (org-element-type next-elem) 'headline)
  21721. (not (eq (org-element-type elem) 'headline))))
  21722. (goto-char pos)
  21723. (user-error "Cannot drag element forward"))
  21724. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21725. ;; body's length (without final blanks) and by the length of
  21726. ;; blanks between ELEM and NEXT-ELEM.
  21727. (let ((size-next (- (save-excursion
  21728. (goto-char (org-element-property :end next-elem))
  21729. (skip-chars-backward " \r\t\n")
  21730. (forward-line)
  21731. ;; Small correction if buffer doesn't end
  21732. ;; with a newline character.
  21733. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21734. (org-element-property :begin next-elem)))
  21735. (size-blank (- (org-element-property :end elem)
  21736. (save-excursion
  21737. (goto-char (org-element-property :end elem))
  21738. (skip-chars-backward " \r\t\n")
  21739. (forward-line)
  21740. (point)))))
  21741. (org-element-swap-A-B elem next-elem)
  21742. (goto-char (+ pos size-next size-blank))))))
  21743. (defun org-drag-line-forward (arg)
  21744. "Drag the line at point ARG lines forward."
  21745. (interactive "p")
  21746. (dotimes (n (abs arg))
  21747. (let ((c (current-column)))
  21748. (if (< 0 arg)
  21749. (progn
  21750. (beginning-of-line 2)
  21751. (transpose-lines 1)
  21752. (beginning-of-line 0))
  21753. (transpose-lines 1)
  21754. (beginning-of-line -1))
  21755. (org-move-to-column c))))
  21756. (defun org-drag-line-backward (arg)
  21757. "Drag the line at point ARG lines backward."
  21758. (interactive "p")
  21759. (org-drag-line-forward (- arg)))
  21760. (defun org-mark-element ()
  21761. "Put point at beginning of this element, mark at end.
  21762. Interactively, if this command is repeated or (in Transient Mark
  21763. mode) if the mark is active, it marks the next element after the
  21764. ones already marked."
  21765. (interactive)
  21766. (let (deactivate-mark)
  21767. (if (and (org-called-interactively-p 'any)
  21768. (or (and (eq last-command this-command) (mark t))
  21769. (and transient-mark-mode mark-active)))
  21770. (set-mark
  21771. (save-excursion
  21772. (goto-char (mark))
  21773. (goto-char (org-element-property :end (org-element-at-point)))))
  21774. (let ((element (org-element-at-point)))
  21775. (end-of-line)
  21776. (push-mark (org-element-property :end element) t t)
  21777. (goto-char (org-element-property :begin element))))))
  21778. (defun org-narrow-to-element ()
  21779. "Narrow buffer to current element."
  21780. (interactive)
  21781. (let ((elem (org-element-at-point)))
  21782. (cond
  21783. ((eq (car elem) 'headline)
  21784. (narrow-to-region
  21785. (org-element-property :begin elem)
  21786. (org-element-property :end elem)))
  21787. ((memq (car elem) org-element-greater-elements)
  21788. (narrow-to-region
  21789. (org-element-property :contents-begin elem)
  21790. (org-element-property :contents-end elem)))
  21791. (t
  21792. (narrow-to-region
  21793. (org-element-property :begin elem)
  21794. (org-element-property :end elem))))))
  21795. (defun org-transpose-element ()
  21796. "Transpose current and previous elements, keeping blank lines between.
  21797. Point is moved after both elements."
  21798. (interactive)
  21799. (org-skip-whitespace)
  21800. (let ((end (org-element-property :end (org-element-at-point))))
  21801. (org-drag-element-backward)
  21802. (goto-char end)))
  21803. (defun org-unindent-buffer ()
  21804. "Un-indent the visible part of the buffer.
  21805. Relative indentation (between items, inside blocks, etc.) isn't
  21806. modified."
  21807. (interactive)
  21808. (unless (eq major-mode 'org-mode)
  21809. (user-error "Cannot un-indent a buffer not in Org mode"))
  21810. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21811. unindent-tree ; For byte-compiler.
  21812. (unindent-tree
  21813. (function
  21814. (lambda (contents)
  21815. (mapc
  21816. (lambda (element)
  21817. (if (memq (org-element-type element) '(headline section))
  21818. (funcall unindent-tree (org-element-contents element))
  21819. (save-excursion
  21820. (save-restriction
  21821. (narrow-to-region
  21822. (org-element-property :begin element)
  21823. (org-element-property :end element))
  21824. (org-do-remove-indentation)))))
  21825. (reverse contents))))))
  21826. (funcall unindent-tree (org-element-contents parse-tree))))
  21827. (defun org-show-subtree ()
  21828. "Show everything after this heading at deeper levels."
  21829. (interactive)
  21830. (outline-flag-region
  21831. (point)
  21832. (save-excursion
  21833. (org-end-of-subtree t t))
  21834. nil))
  21835. (defun org-show-entry ()
  21836. "Show the body directly following this heading.
  21837. Show the heading too, if it is currently invisible."
  21838. (interactive)
  21839. (save-excursion
  21840. (condition-case nil
  21841. (progn
  21842. (org-back-to-heading t)
  21843. (outline-flag-region
  21844. (max (point-min) (1- (point)))
  21845. (save-excursion
  21846. (if (re-search-forward
  21847. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21848. (match-beginning 1)
  21849. (point-max)))
  21850. nil)
  21851. (org-cycle-hide-drawers 'children))
  21852. (error nil))))
  21853. (defun org-make-options-regexp (kwds &optional extra)
  21854. "Make a regular expression for keyword lines."
  21855. (concat
  21856. "^#\\+\\("
  21857. (mapconcat 'regexp-quote kwds "\\|")
  21858. (if extra (concat "\\|" extra))
  21859. "\\):[ \t]*\\(.*\\)"))
  21860. ;; Make isearch reveal the necessary context
  21861. (defun org-isearch-end ()
  21862. "Reveal context after isearch exits."
  21863. (when isearch-success ; only if search was successful
  21864. (if (featurep 'xemacs)
  21865. ;; Under XEmacs, the hook is run in the correct place,
  21866. ;; we directly show the context.
  21867. (org-show-context 'isearch)
  21868. ;; In Emacs the hook runs *before* restoring the overlays.
  21869. ;; So we have to use a one-time post-command-hook to do this.
  21870. ;; (Emacs 22 has a special variable, see function `org-mode')
  21871. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21872. isearch-mode-end-hook-quit)
  21873. ;; Only when the isearch was not quitted.
  21874. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21875. 'append 'local)))
  21876. (org-fix-ellipsis-at-bol)))
  21877. (defun org-isearch-post-command ()
  21878. "Remove self from hook, and show context."
  21879. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21880. (org-show-context 'isearch))
  21881. ;;;; Integration with and fixes for other packages
  21882. ;;; Imenu support
  21883. (defvar org-imenu-markers nil
  21884. "All markers currently used by Imenu.")
  21885. (make-variable-buffer-local 'org-imenu-markers)
  21886. (defun org-imenu-new-marker (&optional pos)
  21887. "Return a new marker for use by Imenu, and remember the marker."
  21888. (let ((m (make-marker)))
  21889. (move-marker m (or pos (point)))
  21890. (push m org-imenu-markers)
  21891. m))
  21892. (defun org-imenu-get-tree ()
  21893. "Produce the index for Imenu."
  21894. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21895. (setq org-imenu-markers nil)
  21896. (let* ((n org-imenu-depth)
  21897. (re (concat "^" (org-get-limited-outline-regexp)))
  21898. (subs (make-vector (1+ n) nil))
  21899. (last-level 0)
  21900. m level head0 head)
  21901. (save-excursion
  21902. (save-restriction
  21903. (widen)
  21904. (goto-char (point-max))
  21905. (while (re-search-backward re nil t)
  21906. (setq level (org-reduced-level (funcall outline-level)))
  21907. (when (and (<= level n)
  21908. (looking-at org-complex-heading-regexp)
  21909. (setq head0 (org-match-string-no-properties 4)))
  21910. (setq head (org-link-display-format head0)
  21911. m (org-imenu-new-marker))
  21912. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21913. (if (>= level last-level)
  21914. (push (cons head m) (aref subs level))
  21915. (push (cons head (aref subs (1+ level))) (aref subs level))
  21916. (loop for i from (1+ level) to n do (aset subs i nil)))
  21917. (setq last-level level)))))
  21918. (aref subs 1)))
  21919. (eval-after-load "imenu"
  21920. '(progn
  21921. (add-hook 'imenu-after-jump-hook
  21922. (lambda ()
  21923. (if (derived-mode-p 'org-mode)
  21924. (org-show-context 'org-goto))))))
  21925. (defun org-link-display-format (link)
  21926. "Replace a link with its the description.
  21927. If there is no description, use the link target."
  21928. (save-match-data
  21929. (if (string-match org-bracket-link-analytic-regexp link)
  21930. (replace-match (if (match-end 5)
  21931. (match-string 5 link)
  21932. (concat (match-string 1 link)
  21933. (match-string 3 link)))
  21934. nil t link)
  21935. link)))
  21936. (defun org-toggle-link-display ()
  21937. "Toggle the literal or descriptive display of links."
  21938. (interactive)
  21939. (if org-descriptive-links
  21940. (progn (org-remove-from-invisibility-spec '(org-link))
  21941. (org-restart-font-lock)
  21942. (setq org-descriptive-links nil))
  21943. (progn (add-to-invisibility-spec '(org-link))
  21944. (org-restart-font-lock)
  21945. (setq org-descriptive-links t))))
  21946. ;; Speedbar support
  21947. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21948. "Overlay marking the agenda restriction line in speedbar.")
  21949. (overlay-put org-speedbar-restriction-lock-overlay
  21950. 'face 'org-agenda-restriction-lock)
  21951. (overlay-put org-speedbar-restriction-lock-overlay
  21952. 'help-echo "Agendas are currently limited to this item.")
  21953. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21954. (defun org-speedbar-set-agenda-restriction ()
  21955. "Restrict future agenda commands to the location at point in speedbar.
  21956. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21957. (interactive)
  21958. (require 'org-agenda)
  21959. (let (p m tp np dir txt)
  21960. (cond
  21961. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21962. 'org-imenu t))
  21963. (setq m (get-text-property p 'org-imenu-marker))
  21964. (with-current-buffer (marker-buffer m)
  21965. (goto-char m)
  21966. (org-agenda-set-restriction-lock 'subtree)))
  21967. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21968. 'speedbar-function 'speedbar-find-file))
  21969. (setq tp (previous-single-property-change
  21970. (1+ p) 'speedbar-function)
  21971. np (next-single-property-change
  21972. tp 'speedbar-function)
  21973. dir (speedbar-line-directory)
  21974. txt (buffer-substring-no-properties (or tp (point-min))
  21975. (or np (point-max))))
  21976. (with-current-buffer (find-file-noselect
  21977. (let ((default-directory dir))
  21978. (expand-file-name txt)))
  21979. (unless (derived-mode-p 'org-mode)
  21980. (user-error "Cannot restrict to non-Org-mode file"))
  21981. (org-agenda-set-restriction-lock 'file)))
  21982. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21983. (move-overlay org-speedbar-restriction-lock-overlay
  21984. (point-at-bol) (point-at-eol))
  21985. (setq current-prefix-arg nil)
  21986. (org-agenda-maybe-redo)))
  21987. (defvar speedbar-file-key-map)
  21988. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21989. (eval-after-load "speedbar"
  21990. '(progn
  21991. (speedbar-add-supported-extension ".org")
  21992. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21993. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21994. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21995. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21996. (add-hook 'speedbar-visiting-tag-hook
  21997. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21998. ;;; Fixes and Hacks for problems with other packages
  21999. ;; Make flyspell not check words in links, to not mess up our keymap
  22000. (defvar org-element-affiliated-keywords) ; From org-element.el
  22001. (defvar org-element-block-name-alist) ; From org-element.el
  22002. (defun org-mode-flyspell-verify ()
  22003. "Don't let flyspell put overlays at active buttons, or on
  22004. {todo,all-time,additional-option-like}-keywords."
  22005. (require 'org-element) ; For `org-element-affiliated-keywords'
  22006. (let ((pos (max (1- (point)) (point-min)))
  22007. (word (thing-at-point 'word)))
  22008. (and (not (get-text-property pos 'keymap))
  22009. (not (get-text-property pos 'org-no-flyspell))
  22010. (not (member word org-todo-keywords-1))
  22011. (not (member word org-all-time-keywords))
  22012. (not (member word org-options-keywords))
  22013. (not (member word (mapcar 'car org-startup-options)))
  22014. (not (member-ignore-case word org-element-affiliated-keywords))
  22015. (not (member-ignore-case word (org-get-export-keywords)))
  22016. (not (member-ignore-case
  22017. word (mapcar 'car org-element-block-name-alist)))
  22018. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  22019. (not (org-in-src-block-p)))))
  22020. (defun org-remove-flyspell-overlays-in (beg end)
  22021. "Remove flyspell overlays in region."
  22022. (and (org-bound-and-true-p flyspell-mode)
  22023. (fboundp 'flyspell-delete-region-overlays)
  22024. (flyspell-delete-region-overlays beg end))
  22025. (add-text-properties beg end '(org-no-flyspell t)))
  22026. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22027. (eval-after-load "bookmark"
  22028. '(if (boundp 'bookmark-after-jump-hook)
  22029. ;; We can use the hook
  22030. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22031. ;; Hook not available, use advice
  22032. (defadvice bookmark-jump (after org-make-visible activate)
  22033. "Make the position visible."
  22034. (org-bookmark-jump-unhide))))
  22035. ;; Make sure saveplace shows the location if it was hidden
  22036. (eval-after-load "saveplace"
  22037. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22038. "Make the position visible."
  22039. (org-bookmark-jump-unhide)))
  22040. ;; Make sure ecb shows the location if it was hidden
  22041. (eval-after-load "ecb"
  22042. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22043. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22044. (if (derived-mode-p 'org-mode)
  22045. (org-show-context))))
  22046. (defun org-bookmark-jump-unhide ()
  22047. "Unhide the current position, to show the bookmark location."
  22048. (and (derived-mode-p 'org-mode)
  22049. (or (outline-invisible-p)
  22050. (save-excursion (goto-char (max (point-min) (1- (point))))
  22051. (outline-invisible-p)))
  22052. (org-show-context 'bookmark-jump)))
  22053. ;; Make session.el ignore our circular variable
  22054. (defvar session-globals-exclude)
  22055. (eval-after-load "session"
  22056. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22057. ;;;; Finish up
  22058. (provide 'org)
  22059. (run-hooks 'org-load-hook)
  22060. ;;; org.el ends here